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