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