[1b221e0] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Simple module to facilitate twitter functionality. * |
---|
| 5 | * * |
---|
| 6 | * Copyright 2009 Geert Mulders <g.c.w.m.mulders@gmail.com> * |
---|
| 7 | * * |
---|
| 8 | * This library is free software; you can redistribute it and/or * |
---|
| 9 | * modify it under the terms of the GNU Lesser General Public * |
---|
| 10 | * License as published by the Free Software Foundation, version * |
---|
| 11 | * 2.1. * |
---|
| 12 | * * |
---|
| 13 | * This library is distributed in the hope that it will be useful, * |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
| 16 | * Lesser General Public License for more details. * |
---|
| 17 | * * |
---|
| 18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
| 19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
---|
| 21 | * * |
---|
| 22 | ****************************************************************************/ |
---|
| 23 | |
---|
| 24 | #include "nogaim.h" |
---|
[713d611] | 25 | #include "oauth.h" |
---|
[1b221e0] | 26 | #include "twitter.h" |
---|
| 27 | #include "twitter_http.h" |
---|
| 28 | #include "twitter_lib.h" |
---|
[bb5ce4d1] | 29 | #include "url.h" |
---|
[1b221e0] | 30 | |
---|
[665c24f] | 31 | #define twitter_msg( ic, fmt... ) \ |
---|
[2322a9f] | 32 | do { \ |
---|
| 33 | struct twitter_data *td = ic->proto_data; \ |
---|
| 34 | if( td->timeline_gc ) \ |
---|
| 35 | imcb_chat_log( td->timeline_gc, fmt ); \ |
---|
| 36 | else \ |
---|
| 37 | imcb_log( ic, fmt ); \ |
---|
[665c24f] | 38 | } while( 0 ); |
---|
[5983eca] | 39 | |
---|
[9c9a29c] | 40 | GSList *twitter_connections = NULL; |
---|
[665c24f] | 41 | |
---|
[1b221e0] | 42 | /** |
---|
[62d2cfb] | 43 | * Main loop function |
---|
| 44 | */ |
---|
[1b221e0] | 45 | gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond) |
---|
| 46 | { |
---|
| 47 | struct im_connection *ic = data; |
---|
[5983eca] | 48 | |
---|
[1b221e0] | 49 | // Check if we are still logged in... |
---|
[5983eca] | 50 | if (!g_slist_find(twitter_connections, ic)) |
---|
[1b221e0] | 51 | return 0; |
---|
| 52 | |
---|
| 53 | // Do stuff.. |
---|
[2322a9f] | 54 | twitter_get_timeline(ic, -1); |
---|
[1b221e0] | 55 | |
---|
| 56 | // If we are still logged in run this function again after timeout. |
---|
| 57 | return (ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN; |
---|
| 58 | } |
---|
| 59 | |
---|
[5983eca] | 60 | static void twitter_main_loop_start(struct im_connection *ic) |
---|
[713d611] | 61 | { |
---|
| 62 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 63 | |
---|
| 64 | imcb_log(ic, "Getting initial statuses"); |
---|
[713d611] | 65 | |
---|
| 66 | // Run this once. After this queue the main loop function. |
---|
| 67 | twitter_main_loop(ic, -1, 0); |
---|
| 68 | |
---|
| 69 | // Queue the main_loop |
---|
| 70 | // Save the return value, so we can remove the timeout on logout. |
---|
[2322a9f] | 71 | td->main_loop_id = |
---|
| 72 | b_timeout_add(set_getint(&ic->acc->set, "fetch_interval") * 1000, twitter_main_loop, ic); |
---|
[713d611] | 73 | } |
---|
| 74 | |
---|
[5983eca] | 75 | static void twitter_oauth_start(struct im_connection *ic); |
---|
[d6aa6dd] | 76 | |
---|
[5983eca] | 77 | void twitter_login_finish(struct im_connection *ic) |
---|
[d6aa6dd] | 78 | { |
---|
| 79 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 80 | |
---|
[2322a9f] | 81 | td->flags &= ~TWITTER_DOING_TIMELINE; |
---|
| 82 | |
---|
[5983eca] | 83 | if (set_getbool(&ic->acc->set, "oauth") && !td->oauth_info) |
---|
| 84 | twitter_oauth_start(ic); |
---|
| 85 | else if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "one") != 0 && |
---|
| 86 | !(td->flags & TWITTER_HAVE_FRIENDS)) { |
---|
| 87 | imcb_log(ic, "Getting contact list"); |
---|
[de923d5] | 88 | twitter_get_friends_ids(ic, -1); |
---|
| 89 | //twitter_get_statuses_friends(ic, -1); |
---|
[5983eca] | 90 | } else |
---|
| 91 | twitter_main_loop_start(ic); |
---|
[d6aa6dd] | 92 | } |
---|
[c2ecadc] | 93 | |
---|
[5983eca] | 94 | static const struct oauth_service twitter_oauth = { |
---|
[3f808ca] | 95 | "https://api.twitter.com/oauth/request_token", |
---|
| 96 | "https://api.twitter.com/oauth/access_token", |
---|
[c01bbd1] | 97 | "https://api.twitter.com/oauth/authorize", |
---|
[c2ecadc] | 98 | .consumer_key = "xsDNKJuNZYkZyMcu914uEA", |
---|
| 99 | .consumer_secret = "FCxqcr0pXKzsF9ajmP57S3VQ8V6Drk4o2QYtqMcOszo", |
---|
| 100 | }; |
---|
| 101 | |
---|
[5983eca] | 102 | static const struct oauth_service identica_oauth = { |
---|
[3f808ca] | 103 | "https://identi.ca/api/oauth/request_token", |
---|
| 104 | "https://identi.ca/api/oauth/access_token", |
---|
[ce617f0] | 105 | "https://identi.ca/api/oauth/authorize", |
---|
| 106 | .consumer_key = "e147ff789fcbd8a5a07963afbb43f9da", |
---|
| 107 | .consumer_secret = "c596267f277457ec0ce1ab7bb788d828", |
---|
| 108 | }; |
---|
| 109 | |
---|
[5983eca] | 110 | static gboolean twitter_oauth_callback(struct oauth_info *info); |
---|
[713d611] | 111 | |
---|
[5983eca] | 112 | static const struct oauth_service *get_oauth_service(struct im_connection *ic) |
---|
[ce617f0] | 113 | { |
---|
| 114 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 115 | |
---|
| 116 | if (strstr(td->url_host, "identi.ca")) |
---|
[ce617f0] | 117 | return &identica_oauth; |
---|
| 118 | else |
---|
| 119 | return &twitter_oauth; |
---|
[5983eca] | 120 | |
---|
[ce617f0] | 121 | /* Could add more services, or allow configuring your own base URL + |
---|
| 122 | API keys. */ |
---|
| 123 | } |
---|
| 124 | |
---|
[5983eca] | 125 | static void twitter_oauth_start(struct im_connection *ic) |
---|
[713d611] | 126 | { |
---|
[18dbb20] | 127 | struct twitter_data *td = ic->proto_data; |
---|
[c42e8b9] | 128 | |
---|
[5983eca] | 129 | imcb_log(ic, "Requesting OAuth request token"); |
---|
| 130 | |
---|
| 131 | td->oauth_info = oauth_request_token(get_oauth_service(ic), twitter_oauth_callback, ic); |
---|
| 132 | |
---|
[748bcdd] | 133 | /* We need help from the user to complete OAuth login, so don't time |
---|
| 134 | out on this login. */ |
---|
| 135 | ic->flags |= OPT_SLOW_LOGIN; |
---|
[713d611] | 136 | } |
---|
| 137 | |
---|
[5983eca] | 138 | static gboolean twitter_oauth_callback(struct oauth_info *info) |
---|
[713d611] | 139 | { |
---|
| 140 | struct im_connection *ic = info->data; |
---|
[18dbb20] | 141 | struct twitter_data *td; |
---|
[5983eca] | 142 | |
---|
| 143 | if (!g_slist_find(twitter_connections, ic)) |
---|
[18dbb20] | 144 | return FALSE; |
---|
[5983eca] | 145 | |
---|
[18dbb20] | 146 | td = ic->proto_data; |
---|
[5983eca] | 147 | if (info->stage == OAUTH_REQUEST_TOKEN) { |
---|
| 148 | char name[strlen(ic->acc->user) + 9], *msg; |
---|
| 149 | |
---|
| 150 | if (info->request_token == NULL) { |
---|
| 151 | imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http)); |
---|
| 152 | imc_logout(ic, TRUE); |
---|
[18dbb20] | 153 | return FALSE; |
---|
[c42e8b9] | 154 | } |
---|
[5983eca] | 155 | |
---|
| 156 | sprintf(name, "%s_%s", td->prefix, ic->acc->user); |
---|
| 157 | msg = g_strdup_printf("To finish OAuth authentication, please visit " |
---|
| 158 | "%s and respond with the resulting PIN code.", |
---|
| 159 | info->auth_url); |
---|
| 160 | imcb_buddy_msg(ic, name, msg, 0, 0); |
---|
| 161 | g_free(msg); |
---|
| 162 | } else if (info->stage == OAUTH_ACCESS_TOKEN) { |
---|
| 163 | if (info->token == NULL || info->token_secret == NULL) { |
---|
| 164 | imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http)); |
---|
| 165 | imc_logout(ic, TRUE); |
---|
[18dbb20] | 166 | return FALSE; |
---|
[5983eca] | 167 | } else { |
---|
| 168 | const char *sn = oauth_params_get(&info->params, "screen_name"); |
---|
| 169 | |
---|
| 170 | if (sn != NULL && ic->acc->prpl->handle_cmp(sn, ic->acc->user) != 0) { |
---|
| 171 | imcb_log(ic, "Warning: You logged in via OAuth as %s " |
---|
| 172 | "instead of %s.", sn, ic->acc->user); |
---|
[93cc86f] | 173 | } |
---|
[de923d5] | 174 | g_free(td->user); |
---|
| 175 | td->user = g_strdup(sn); |
---|
[93cc86f] | 176 | } |
---|
[5983eca] | 177 | |
---|
[288b215] | 178 | /* IM mods didn't do this so far and it's ugly but I should |
---|
| 179 | be able to get away with it... */ |
---|
[5983eca] | 180 | g_free(ic->acc->pass); |
---|
| 181 | ic->acc->pass = oauth_to_string(info); |
---|
| 182 | |
---|
| 183 | twitter_login_finish(ic); |
---|
[c42e8b9] | 184 | } |
---|
[5983eca] | 185 | |
---|
[18dbb20] | 186 | return TRUE; |
---|
[713d611] | 187 | } |
---|
| 188 | |
---|
[c2ecadc] | 189 | |
---|
[5983eca] | 190 | static char *set_eval_mode(set_t * set, char *value) |
---|
[e88fbe27] | 191 | { |
---|
[5983eca] | 192 | if (g_strcasecmp(value, "one") == 0 || |
---|
| 193 | g_strcasecmp(value, "many") == 0 || g_strcasecmp(value, "chat") == 0) |
---|
[e88fbe27] | 194 | return value; |
---|
| 195 | else |
---|
| 196 | return NULL; |
---|
| 197 | } |
---|
[1b221e0] | 198 | |
---|
[7d27962] | 199 | int twitter_url_len_diff(gchar *msg, unsigned int target_len) |
---|
| 200 | { |
---|
| 201 | int url_len_diff = 0; |
---|
| 202 | |
---|
| 203 | static GRegex *regex = NULL; |
---|
| 204 | GMatchInfo *match_info; |
---|
| 205 | |
---|
| 206 | if (regex == NULL) |
---|
| 207 | regex = g_regex_new("(^|\\s)(http(s)?://[^\\s$]+)", 0, 0, NULL); |
---|
| 208 | |
---|
| 209 | g_regex_match(regex, msg, 0, &match_info); |
---|
| 210 | while (g_match_info_matches(match_info)) { |
---|
| 211 | gchar *url = g_match_info_fetch(match_info, 2); |
---|
| 212 | url_len_diff += target_len - g_utf8_strlen(url, -1); |
---|
| 213 | if (g_match_info_fetch(match_info, 3) != NULL) |
---|
| 214 | url_len_diff += 1; |
---|
| 215 | g_free(url); |
---|
| 216 | g_match_info_next(match_info, NULL); |
---|
| 217 | } |
---|
| 218 | g_match_info_free(match_info); |
---|
| 219 | |
---|
| 220 | return url_len_diff; |
---|
| 221 | } |
---|
| 222 | |
---|
[5983eca] | 223 | static gboolean twitter_length_check(struct im_connection *ic, gchar * msg) |
---|
[9997691] | 224 | { |
---|
[5983eca] | 225 | int max = set_getint(&ic->acc->set, "message_length"), len; |
---|
[7d27962] | 226 | int target_len = set_getint(&ic->acc->set, "target_url_length"); |
---|
| 227 | int url_len_diff = 0; |
---|
| 228 | |
---|
| 229 | if (target_len > 0) |
---|
| 230 | url_len_diff = twitter_url_len_diff(msg, target_len); |
---|
[5983eca] | 231 | |
---|
[7d27962] | 232 | if (max == 0 || (len = g_utf8_strlen(msg, -1) + url_len_diff) <= max) |
---|
[9997691] | 233 | return TRUE; |
---|
[5983eca] | 234 | |
---|
| 235 | imcb_error(ic, "Maximum message length exceeded: %d > %d", len, max); |
---|
| 236 | |
---|
[9997691] | 237 | return FALSE; |
---|
| 238 | } |
---|
| 239 | |
---|
[5983eca] | 240 | static void twitter_init(account_t * acc) |
---|
[1b221e0] | 241 | { |
---|
[62d2cfb] | 242 | set_t *s; |
---|
[ffcdf13] | 243 | char *def_url; |
---|
| 244 | char *def_oauth; |
---|
[7d27962] | 245 | char *def_tul; |
---|
[5983eca] | 246 | |
---|
| 247 | if (strcmp(acc->prpl->name, "twitter") == 0) { |
---|
[ffcdf13] | 248 | def_url = TWITTER_API_URL; |
---|
| 249 | def_oauth = "true"; |
---|
[7d27962] | 250 | def_tul = "20"; |
---|
[5983eca] | 251 | } else { /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */ |
---|
[ffcdf13] | 252 | def_url = IDENTICA_API_URL; |
---|
[e306fbf] | 253 | def_oauth = "true"; |
---|
[7d27962] | 254 | def_tul = "0"; |
---|
[ffcdf13] | 255 | } |
---|
[5983eca] | 256 | |
---|
| 257 | s = set_add(&acc->set, "auto_reply_timeout", "10800", set_eval_int, acc); |
---|
| 258 | |
---|
| 259 | s = set_add(&acc->set, "base_url", def_url, NULL, acc); |
---|
[bb5ce4d1] | 260 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5983eca] | 261 | |
---|
| 262 | s = set_add(&acc->set, "commands", "true", set_eval_bool, acc); |
---|
| 263 | |
---|
[2322a9f] | 264 | s = set_add(&acc->set, "fetch_interval", "60", set_eval_int, acc); |
---|
| 265 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 266 | |
---|
| 267 | s = set_add(&acc->set, "fetch_mentions", "true", set_eval_bool, acc); |
---|
| 268 | |
---|
[5983eca] | 269 | s = set_add(&acc->set, "message_length", "140", set_eval_int, acc); |
---|
| 270 | |
---|
[7d27962] | 271 | s = set_add(&acc->set, "target_url_length", def_tul, set_eval_int, acc); |
---|
| 272 | |
---|
[5983eca] | 273 | s = set_add(&acc->set, "mode", "chat", set_eval_mode, acc); |
---|
[2abceca] | 274 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5983eca] | 275 | |
---|
[ce199b7] | 276 | s = set_add(&acc->set, "oauth", def_oauth, set_eval_oauth, acc); |
---|
| 277 | |
---|
[e93fa05] | 278 | s = set_add(&acc->set, "show_ids", "true", set_eval_bool, acc); |
---|
[ce81acd] | 279 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5983eca] | 280 | |
---|
[b5fe39b] | 281 | s = set_add(&acc->set, "show_old_mentions", "20", set_eval_int, acc); |
---|
[948abab] | 282 | |
---|
| 283 | s = set_add(&acc->set, "strip_newlines", "false", set_eval_bool, acc); |
---|
[1b221e0] | 284 | } |
---|
| 285 | |
---|
| 286 | /** |
---|
| 287 | * Login method. Since the twitter API works with seperate HTTP request we |
---|
| 288 | * only save the user and pass to the twitter_data object. |
---|
| 289 | */ |
---|
[5983eca] | 290 | static void twitter_login(account_t * acc) |
---|
[1b221e0] | 291 | { |
---|
[5983eca] | 292 | struct im_connection *ic = imcb_new(acc); |
---|
[bb5ce4d1] | 293 | struct twitter_data *td; |
---|
[5983eca] | 294 | char name[strlen(acc->user) + 9]; |
---|
[bb5ce4d1] | 295 | url_t url; |
---|
[c0f33f1] | 296 | char *s; |
---|
| 297 | |
---|
[5983eca] | 298 | if (!url_set(&url, set_getstr(&ic->acc->set, "base_url")) || |
---|
| 299 | (url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS)) { |
---|
| 300 | imcb_error(ic, "Incorrect API base URL: %s", set_getstr(&ic->acc->set, "base_url")); |
---|
| 301 | imc_logout(ic, FALSE); |
---|
[bb5ce4d1] | 302 | return; |
---|
| 303 | } |
---|
[5983eca] | 304 | |
---|
[c0f33f1] | 305 | imcb_log(ic, "Connecting"); |
---|
| 306 | |
---|
[5983eca] | 307 | twitter_connections = g_slist_append(twitter_connections, ic); |
---|
| 308 | td = g_new0(struct twitter_data, 1); |
---|
[713d611] | 309 | ic->proto_data = td; |
---|
[de923d5] | 310 | td->user = g_strdup(acc->user); |
---|
[5983eca] | 311 | |
---|
[bb5ce4d1] | 312 | td->url_ssl = url.proto == PROTO_HTTPS; |
---|
| 313 | td->url_port = url.port; |
---|
[5983eca] | 314 | td->url_host = g_strdup(url.host); |
---|
| 315 | if (strcmp(url.file, "/") != 0) |
---|
| 316 | td->url_path = g_strdup(url.file); |
---|
[c0f33f1] | 317 | else { |
---|
[5983eca] | 318 | td->url_path = g_strdup(""); |
---|
[c0f33f1] | 319 | if (g_str_has_suffix(url.host, "twitter.com")) |
---|
| 320 | /* May fire for people who turned on HTTPS. */ |
---|
| 321 | imcb_error(ic, "Warning: Twitter requires a version number in API calls " |
---|
| 322 | "now. Try resetting the base_url account setting."); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | /* Hacky string mangling: Turn identi.ca into identi.ca and api.twitter.com |
---|
| 326 | into twitter, and try to be sensible if we get anything else. */ |
---|
| 327 | td->prefix = g_strdup(url.host); |
---|
| 328 | if (g_str_has_suffix(td->prefix, ".com")) |
---|
| 329 | td->prefix[strlen(url.host) - 4] = '\0'; |
---|
[4bc66ae] | 330 | if ((s = strrchr(td->prefix, '.')) && strlen(s) > 4) { |
---|
| 331 | /* If we have at least 3 chars after the last dot, cut off the rest. |
---|
| 332 | (mostly a www/api prefix or sth) */ |
---|
[c0f33f1] | 333 | s = g_strdup(s + 1); |
---|
| 334 | g_free(td->prefix); |
---|
| 335 | td->prefix = s; |
---|
| 336 | } |
---|
| 337 | |
---|
[5983eca] | 338 | if (strstr(acc->pass, "oauth_token=")) |
---|
| 339 | td->oauth_info = oauth_from_string(acc->pass, get_oauth_service(ic)); |
---|
| 340 | |
---|
| 341 | sprintf(name, "%s_%s", td->prefix, acc->user); |
---|
| 342 | imcb_add_buddy(ic, name, NULL); |
---|
| 343 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
| 344 | |
---|
| 345 | if (set_getbool(&acc->set, "show_ids")) |
---|
| 346 | td->log = g_new0(struct twitter_log_data, TWITTER_LOG_LENGTH); |
---|
| 347 | |
---|
| 348 | twitter_login_finish(ic); |
---|
[1b221e0] | 349 | } |
---|
| 350 | |
---|
| 351 | /** |
---|
| 352 | * Logout method. Just free the twitter_data. |
---|
| 353 | */ |
---|
[5983eca] | 354 | static void twitter_logout(struct im_connection *ic) |
---|
[1b221e0] | 355 | { |
---|
| 356 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 357 | |
---|
[1b221e0] | 358 | // Set the status to logged out. |
---|
[5983eca] | 359 | ic->flags &= ~OPT_LOGGED_IN; |
---|
[1b221e0] | 360 | |
---|
[2abceca] | 361 | // Remove the main_loop function from the function queue. |
---|
| 362 | b_event_remove(td->main_loop_id); |
---|
| 363 | |
---|
[2322a9f] | 364 | if (td->timeline_gc) |
---|
| 365 | imcb_chat_free(td->timeline_gc); |
---|
[1014cab] | 366 | |
---|
[5983eca] | 367 | if (td) { |
---|
| 368 | oauth_info_free(td->oauth_info); |
---|
[de923d5] | 369 | g_free(td->user); |
---|
[5983eca] | 370 | g_free(td->prefix); |
---|
| 371 | g_free(td->url_host); |
---|
| 372 | g_free(td->url_path); |
---|
| 373 | g_free(td->log); |
---|
| 374 | g_free(td); |
---|
[1b221e0] | 375 | } |
---|
[62d2cfb] | 376 | |
---|
[5983eca] | 377 | twitter_connections = g_slist_remove(twitter_connections, ic); |
---|
[1b221e0] | 378 | } |
---|
| 379 | |
---|
[5983eca] | 380 | static void twitter_handle_command(struct im_connection *ic, char *message); |
---|
[7b87539] | 381 | |
---|
[1b221e0] | 382 | /** |
---|
| 383 | * |
---|
| 384 | */ |
---|
[5983eca] | 385 | static int twitter_buddy_msg(struct im_connection *ic, char *who, char *message, int away) |
---|
[1b221e0] | 386 | { |
---|
[c42e8b9] | 387 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 388 | int plen = strlen(td->prefix); |
---|
| 389 | |
---|
[ffcdf13] | 390 | if (g_strncasecmp(who, td->prefix, plen) == 0 && who[plen] == '_' && |
---|
[5983eca] | 391 | g_strcasecmp(who + plen + 1, ic->acc->user) == 0) { |
---|
| 392 | if (set_getbool(&ic->acc->set, "oauth") && |
---|
| 393 | td->oauth_info && td->oauth_info->token == NULL) { |
---|
| 394 | char pin[strlen(message) + 1], *s; |
---|
| 395 | |
---|
| 396 | strcpy(pin, message); |
---|
| 397 | for (s = pin + sizeof(pin) - 2; s > pin && isspace(*s); s--) |
---|
[64f8c425] | 398 | *s = '\0'; |
---|
[5983eca] | 399 | for (s = pin; *s && isspace(*s); s++) { |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | if (!oauth_access_token(s, td->oauth_info)) { |
---|
| 403 | imcb_error(ic, "OAuth error: %s", |
---|
| 404 | "Failed to send access token request"); |
---|
| 405 | imc_logout(ic, TRUE); |
---|
[c2ecadc] | 406 | return FALSE; |
---|
| 407 | } |
---|
[5983eca] | 408 | } else |
---|
[7b87539] | 409 | twitter_handle_command(ic, message); |
---|
[5983eca] | 410 | } else { |
---|
[e88fbe27] | 411 | twitter_direct_messages_new(ic, who, message); |
---|
[c42e8b9] | 412 | } |
---|
[5983eca] | 413 | return (0); |
---|
[1b221e0] | 414 | } |
---|
| 415 | |
---|
| 416 | /** |
---|
| 417 | * |
---|
| 418 | */ |
---|
[5983eca] | 419 | static void twitter_set_my_name(struct im_connection *ic, char *info) |
---|
[1b221e0] | 420 | { |
---|
| 421 | } |
---|
| 422 | |
---|
[5983eca] | 423 | static void twitter_get_info(struct im_connection *ic, char *who) |
---|
[1b221e0] | 424 | { |
---|
| 425 | } |
---|
| 426 | |
---|
[5983eca] | 427 | static void twitter_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
[1b221e0] | 428 | { |
---|
[7d53efb] | 429 | twitter_friendships_create_destroy(ic, who, 1); |
---|
[1b221e0] | 430 | } |
---|
| 431 | |
---|
[5983eca] | 432 | static void twitter_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
[1b221e0] | 433 | { |
---|
[7d53efb] | 434 | twitter_friendships_create_destroy(ic, who, 0); |
---|
[1b221e0] | 435 | } |
---|
| 436 | |
---|
[5983eca] | 437 | static void twitter_chat_msg(struct groupchat *c, char *message, int flags) |
---|
[1b221e0] | 438 | { |
---|
[5983eca] | 439 | if (c && message) |
---|
| 440 | twitter_handle_command(c->ic, message); |
---|
[1b221e0] | 441 | } |
---|
| 442 | |
---|
[5983eca] | 443 | static void twitter_chat_invite(struct groupchat *c, char *who, char *message) |
---|
[1b221e0] | 444 | { |
---|
| 445 | } |
---|
| 446 | |
---|
[5983eca] | 447 | static void twitter_chat_leave(struct groupchat *c) |
---|
[1b221e0] | 448 | { |
---|
[16592d8] | 449 | struct twitter_data *td = c->ic->proto_data; |
---|
[5983eca] | 450 | |
---|
[2322a9f] | 451 | if (c != td->timeline_gc) |
---|
[5983eca] | 452 | return; /* WTF? */ |
---|
| 453 | |
---|
[16592d8] | 454 | /* If the user leaves the channel: Fine. Rejoin him/her once new |
---|
| 455 | tweets come in. */ |
---|
[2322a9f] | 456 | imcb_chat_free(td->timeline_gc); |
---|
| 457 | td->timeline_gc = NULL; |
---|
[1b221e0] | 458 | } |
---|
| 459 | |
---|
[5983eca] | 460 | static void twitter_keepalive(struct im_connection *ic) |
---|
[1b221e0] | 461 | { |
---|
| 462 | } |
---|
| 463 | |
---|
[5983eca] | 464 | static void twitter_add_permit(struct im_connection *ic, char *who) |
---|
[1b221e0] | 465 | { |
---|
| 466 | } |
---|
| 467 | |
---|
[5983eca] | 468 | static void twitter_rem_permit(struct im_connection *ic, char *who) |
---|
[1b221e0] | 469 | { |
---|
| 470 | } |
---|
| 471 | |
---|
[5983eca] | 472 | static void twitter_add_deny(struct im_connection *ic, char *who) |
---|
[1b221e0] | 473 | { |
---|
| 474 | } |
---|
| 475 | |
---|
[5983eca] | 476 | static void twitter_rem_deny(struct im_connection *ic, char *who) |
---|
[1b221e0] | 477 | { |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | //static char *twitter_set_display_name( set_t *set, char *value ) |
---|
| 481 | //{ |
---|
[5983eca] | 482 | // return value; |
---|
[1b221e0] | 483 | //} |
---|
[7b87539] | 484 | |
---|
[5983eca] | 485 | static void twitter_buddy_data_add(struct bee_user *bu) |
---|
[203a2d2] | 486 | { |
---|
[5983eca] | 487 | bu->data = g_new0(struct twitter_user_data, 1); |
---|
[203a2d2] | 488 | } |
---|
| 489 | |
---|
[5983eca] | 490 | static void twitter_buddy_data_free(struct bee_user *bu) |
---|
[203a2d2] | 491 | { |
---|
[5983eca] | 492 | g_free(bu->data); |
---|
[203a2d2] | 493 | } |
---|
| 494 | |
---|
[5983eca] | 495 | static void twitter_handle_command(struct im_connection *ic, char *message) |
---|
[7b87539] | 496 | { |
---|
| 497 | struct twitter_data *td = ic->proto_data; |
---|
[15bc063] | 498 | char *cmds, **cmd, *new = NULL; |
---|
| 499 | guint64 in_reply_to = 0; |
---|
[5983eca] | 500 | |
---|
| 501 | cmds = g_strdup(message); |
---|
| 502 | cmd = split_command_parts(cmds); |
---|
| 503 | |
---|
| 504 | if (cmd[0] == NULL) { |
---|
| 505 | g_free(cmds); |
---|
[7b87539] | 506 | return; |
---|
[5983eca] | 507 | } else if (!set_getbool(&ic->acc->set, "commands")) { |
---|
[7b87539] | 508 | /* Not supporting commands. */ |
---|
[5983eca] | 509 | } else if (g_strcasecmp(cmd[0], "undo") == 0) { |
---|
[7b87539] | 510 | guint64 id; |
---|
[5983eca] | 511 | |
---|
[aa2f575] | 512 | if (cmd[1] == NULL) |
---|
| 513 | twitter_status_destroy(ic, td->last_status_id); |
---|
| 514 | else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
| 515 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
| 516 | id = td->log[id].id; |
---|
| 517 | |
---|
[5983eca] | 518 | twitter_status_destroy(ic, id); |
---|
[aa2f575] | 519 | } else |
---|
[5983eca] | 520 | twitter_msg(ic, "Could not undo last action"); |
---|
| 521 | |
---|
| 522 | g_free(cmds); |
---|
[7b87539] | 523 | return; |
---|
[5983eca] | 524 | } else if (g_strcasecmp(cmd[0], "follow") == 0 && cmd[1]) { |
---|
| 525 | twitter_add_buddy(ic, cmd[1], NULL); |
---|
| 526 | g_free(cmds); |
---|
[b890626] | 527 | return; |
---|
[5983eca] | 528 | } else if (g_strcasecmp(cmd[0], "unfollow") == 0 && cmd[1]) { |
---|
| 529 | twitter_remove_buddy(ic, cmd[1], NULL); |
---|
| 530 | g_free(cmds); |
---|
[b890626] | 531 | return; |
---|
[d18dee42] | 532 | } else if ((g_strcasecmp(cmd[0], "report") == 0 || |
---|
| 533 | g_strcasecmp(cmd[0], "spam") == 0) && cmd[1]) { |
---|
| 534 | char * screen_name; |
---|
| 535 | guint64 id; |
---|
[7de784c] | 536 | screen_name = cmd[1]; |
---|
[d18dee42] | 537 | /* Report nominally works on users but look up the user who |
---|
| 538 | posted the given ID if the user wants to do it that way */ |
---|
| 539 | if (g_str_has_prefix(cmd[1], "#") && |
---|
| 540 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
| 541 | if (id < TWITTER_LOG_LENGTH && td->log) { |
---|
| 542 | if (g_slist_find(ic->bee->users, td->log[id].bu)) { |
---|
| 543 | screen_name = td->log[id].bu->handle; |
---|
| 544 | } |
---|
| 545 | } |
---|
| 546 | } |
---|
| 547 | twitter_report_spam(ic, screen_name); |
---|
| 548 | g_free(cmds); |
---|
| 549 | return; |
---|
[5983eca] | 550 | } else if (g_strcasecmp(cmd[0], "rt") == 0 && cmd[1]) { |
---|
[b890626] | 551 | struct twitter_user_data *tud; |
---|
| 552 | bee_user_t *bu; |
---|
| 553 | guint64 id; |
---|
[5983eca] | 554 | |
---|
[aa2f575] | 555 | if (g_str_has_prefix(cmd[1], "#") && |
---|
| 556 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
[5f1e78d] | 557 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
| 558 | id = td->log[id].id; |
---|
[d6b6906] | 559 | } else if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) && |
---|
[5983eca] | 560 | (tud = bu->data) && tud->last_id) |
---|
[b890626] | 561 | id = tud->last_id; |
---|
[aa2f575] | 562 | else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1){ |
---|
[5983eca] | 563 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
[4f50ea5] | 564 | id = td->log[id].id; |
---|
| 565 | } |
---|
[5983eca] | 566 | |
---|
[b890626] | 567 | td->last_status_id = 0; |
---|
[5983eca] | 568 | if (id) |
---|
| 569 | twitter_status_retweet(ic, id); |
---|
[665c24f] | 570 | else |
---|
[5983eca] | 571 | twitter_msg(ic, "User `%s' does not exist or didn't " |
---|
| 572 | "post any statuses recently", cmd[1]); |
---|
| 573 | |
---|
| 574 | g_free(cmds); |
---|
[b890626] | 575 | return; |
---|
[5983eca] | 576 | } else if (g_strcasecmp(cmd[0], "reply") == 0 && cmd[1] && cmd[2]) { |
---|
[15bc063] | 577 | struct twitter_user_data *tud; |
---|
| 578 | bee_user_t *bu = NULL; |
---|
| 579 | guint64 id = 0; |
---|
[5983eca] | 580 | |
---|
[aa2f575] | 581 | if (g_str_has_prefix(cmd[1], "#") && |
---|
| 582 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1 && |
---|
| 583 | (id < TWITTER_LOG_LENGTH) && td->log) { |
---|
| 584 | bu = td->log[id].bu; |
---|
| 585 | if (g_slist_find(ic->bee->users, bu)) |
---|
| 586 | id = td->log[id].id; |
---|
| 587 | else |
---|
| 588 | bu = NULL; |
---|
| 589 | } else if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) && |
---|
[5983eca] | 590 | (tud = bu->data) && tud->last_id) { |
---|
[15bc063] | 591 | id = tud->last_id; |
---|
[7ceb6b2] | 592 | } else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1 && |
---|
| 593 | (id < TWITTER_LOG_LENGTH) && td->log) { |
---|
[15bc063] | 594 | bu = td->log[id].bu; |
---|
[5983eca] | 595 | if (g_slist_find(ic->bee->users, bu)) |
---|
[15bc063] | 596 | id = td->log[id].id; |
---|
| 597 | else |
---|
| 598 | bu = NULL; |
---|
| 599 | } |
---|
[aa2f575] | 600 | |
---|
[5983eca] | 601 | if (!id || !bu) { |
---|
| 602 | twitter_msg(ic, "User `%s' does not exist or didn't " |
---|
| 603 | "post any statuses recently", cmd[1]); |
---|
[15bc063] | 604 | return; |
---|
| 605 | } |
---|
[5983eca] | 606 | message = new = g_strdup_printf("@%s %s", bu->handle, message + (cmd[2] - cmd[0])); |
---|
[15bc063] | 607 | in_reply_to = id; |
---|
[5983eca] | 608 | } else if (g_strcasecmp(cmd[0], "post") == 0) { |
---|
[7b87539] | 609 | message += 5; |
---|
| 610 | } |
---|
[5983eca] | 611 | |
---|
[7b87539] | 612 | { |
---|
[15bc063] | 613 | char *s; |
---|
[7b87539] | 614 | bee_user_t *bu; |
---|
[5983eca] | 615 | |
---|
| 616 | if (!twitter_length_check(ic, message)) { |
---|
| 617 | g_free(new); |
---|
| 618 | g_free(cmds); |
---|
| 619 | return; |
---|
[7b87539] | 620 | } |
---|
[5983eca] | 621 | |
---|
| 622 | s = cmd[0] + strlen(cmd[0]) - 1; |
---|
| 623 | if (!new && s > cmd[0] && (*s == ':' || *s == ',')) { |
---|
[7b87539] | 624 | *s = '\0'; |
---|
[5983eca] | 625 | |
---|
| 626 | if ((bu = bee_user_by_handle(ic->bee, ic, cmd[0]))) { |
---|
[b890626] | 627 | struct twitter_user_data *tud = bu->data; |
---|
[5983eca] | 628 | |
---|
| 629 | new = g_strdup_printf("@%s %s", bu->handle, |
---|
| 630 | message + (s - cmd[0]) + 2); |
---|
[7b87539] | 631 | message = new; |
---|
[5983eca] | 632 | |
---|
| 633 | if (time(NULL) < tud->last_time + |
---|
| 634 | set_getint(&ic->acc->set, "auto_reply_timeout")) |
---|
[b890626] | 635 | in_reply_to = tud->last_id; |
---|
[7b87539] | 636 | } |
---|
| 637 | } |
---|
[5983eca] | 638 | |
---|
[b890626] | 639 | /* If the user runs undo between this request and its response |
---|
| 640 | this would delete the second-last Tweet. Prevent that. */ |
---|
| 641 | td->last_status_id = 0; |
---|
[5983eca] | 642 | twitter_post_status(ic, message, in_reply_to); |
---|
| 643 | g_free(new); |
---|
[7b87539] | 644 | } |
---|
[5983eca] | 645 | g_free(cmds); |
---|
[7b87539] | 646 | } |
---|
[1b221e0] | 647 | |
---|
| 648 | void twitter_initmodule() |
---|
| 649 | { |
---|
| 650 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
[5983eca] | 651 | |
---|
[1dd3470] | 652 | ret->options = OPT_NOOTR; |
---|
[1b221e0] | 653 | ret->name = "twitter"; |
---|
| 654 | ret->login = twitter_login; |
---|
| 655 | ret->init = twitter_init; |
---|
| 656 | ret->logout = twitter_logout; |
---|
| 657 | ret->buddy_msg = twitter_buddy_msg; |
---|
| 658 | ret->get_info = twitter_get_info; |
---|
| 659 | ret->set_my_name = twitter_set_my_name; |
---|
| 660 | ret->add_buddy = twitter_add_buddy; |
---|
| 661 | ret->remove_buddy = twitter_remove_buddy; |
---|
| 662 | ret->chat_msg = twitter_chat_msg; |
---|
| 663 | ret->chat_invite = twitter_chat_invite; |
---|
| 664 | ret->chat_leave = twitter_chat_leave; |
---|
| 665 | ret->keepalive = twitter_keepalive; |
---|
| 666 | ret->add_permit = twitter_add_permit; |
---|
| 667 | ret->rem_permit = twitter_rem_permit; |
---|
| 668 | ret->add_deny = twitter_add_deny; |
---|
| 669 | ret->rem_deny = twitter_rem_deny; |
---|
[203a2d2] | 670 | ret->buddy_data_add = twitter_buddy_data_add; |
---|
| 671 | ret->buddy_data_free = twitter_buddy_data_free; |
---|
[1b221e0] | 672 | ret->handle_cmp = g_strcasecmp; |
---|
[5983eca] | 673 | |
---|
[ffcdf13] | 674 | register_protocol(ret); |
---|
[1b221e0] | 675 | |
---|
[ffcdf13] | 676 | /* And an identi.ca variant: */ |
---|
| 677 | ret = g_memdup(ret, sizeof(struct prpl)); |
---|
| 678 | ret->name = "identica"; |
---|
[1b221e0] | 679 | register_protocol(ret); |
---|
| 680 | } |
---|