[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; |
---|
[7d27962] | 244 | char *def_tul; |
---|
[5983eca] | 245 | |
---|
| 246 | if (strcmp(acc->prpl->name, "twitter") == 0) { |
---|
[ffcdf13] | 247 | def_url = TWITTER_API_URL; |
---|
[7d27962] | 248 | def_tul = "20"; |
---|
[5983eca] | 249 | } else { /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */ |
---|
[ffcdf13] | 250 | def_url = IDENTICA_API_URL; |
---|
[7d27962] | 251 | def_tul = "0"; |
---|
[ffcdf13] | 252 | } |
---|
[5983eca] | 253 | |
---|
| 254 | s = set_add(&acc->set, "auto_reply_timeout", "10800", set_eval_int, acc); |
---|
| 255 | |
---|
| 256 | s = set_add(&acc->set, "base_url", def_url, NULL, acc); |
---|
[bb5ce4d1] | 257 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5983eca] | 258 | |
---|
| 259 | s = set_add(&acc->set, "commands", "true", set_eval_bool, acc); |
---|
| 260 | |
---|
[2322a9f] | 261 | s = set_add(&acc->set, "fetch_interval", "60", set_eval_int, acc); |
---|
| 262 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 263 | |
---|
| 264 | s = set_add(&acc->set, "fetch_mentions", "true", set_eval_bool, acc); |
---|
| 265 | |
---|
[5983eca] | 266 | s = set_add(&acc->set, "message_length", "140", set_eval_int, acc); |
---|
| 267 | |
---|
[7d27962] | 268 | s = set_add(&acc->set, "target_url_length", def_tul, set_eval_int, acc); |
---|
| 269 | |
---|
[5983eca] | 270 | s = set_add(&acc->set, "mode", "chat", set_eval_mode, acc); |
---|
[2abceca] | 271 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5983eca] | 272 | |
---|
[b3d99e3] | 273 | s = set_add(&acc->set, "oauth", "true", set_eval_oauth, acc); |
---|
[ce199b7] | 274 | |
---|
[e93fa05] | 275 | s = set_add(&acc->set, "show_ids", "true", set_eval_bool, acc); |
---|
[ce81acd] | 276 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[5983eca] | 277 | |
---|
[b5fe39b] | 278 | s = set_add(&acc->set, "show_old_mentions", "20", set_eval_int, acc); |
---|
[948abab] | 279 | |
---|
| 280 | s = set_add(&acc->set, "strip_newlines", "false", set_eval_bool, acc); |
---|
[1b221e0] | 281 | } |
---|
| 282 | |
---|
| 283 | /** |
---|
| 284 | * Login method. Since the twitter API works with seperate HTTP request we |
---|
| 285 | * only save the user and pass to the twitter_data object. |
---|
| 286 | */ |
---|
[5983eca] | 287 | static void twitter_login(account_t * acc) |
---|
[1b221e0] | 288 | { |
---|
[5983eca] | 289 | struct im_connection *ic = imcb_new(acc); |
---|
[bb5ce4d1] | 290 | struct twitter_data *td; |
---|
[5983eca] | 291 | char name[strlen(acc->user) + 9]; |
---|
[bb5ce4d1] | 292 | url_t url; |
---|
[c0f33f1] | 293 | char *s; |
---|
| 294 | |
---|
[5983eca] | 295 | if (!url_set(&url, set_getstr(&ic->acc->set, "base_url")) || |
---|
| 296 | (url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS)) { |
---|
| 297 | imcb_error(ic, "Incorrect API base URL: %s", set_getstr(&ic->acc->set, "base_url")); |
---|
| 298 | imc_logout(ic, FALSE); |
---|
[bb5ce4d1] | 299 | return; |
---|
| 300 | } |
---|
[5983eca] | 301 | |
---|
[c0f33f1] | 302 | imcb_log(ic, "Connecting"); |
---|
| 303 | |
---|
[5983eca] | 304 | twitter_connections = g_slist_append(twitter_connections, ic); |
---|
| 305 | td = g_new0(struct twitter_data, 1); |
---|
[713d611] | 306 | ic->proto_data = td; |
---|
[de923d5] | 307 | td->user = g_strdup(acc->user); |
---|
[5983eca] | 308 | |
---|
[bb5ce4d1] | 309 | td->url_ssl = url.proto == PROTO_HTTPS; |
---|
| 310 | td->url_port = url.port; |
---|
[5983eca] | 311 | td->url_host = g_strdup(url.host); |
---|
| 312 | if (strcmp(url.file, "/") != 0) |
---|
| 313 | td->url_path = g_strdup(url.file); |
---|
[c0f33f1] | 314 | else { |
---|
[5983eca] | 315 | td->url_path = g_strdup(""); |
---|
[c0f33f1] | 316 | if (g_str_has_suffix(url.host, "twitter.com")) |
---|
| 317 | /* May fire for people who turned on HTTPS. */ |
---|
| 318 | imcb_error(ic, "Warning: Twitter requires a version number in API calls " |
---|
| 319 | "now. Try resetting the base_url account setting."); |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | /* Hacky string mangling: Turn identi.ca into identi.ca and api.twitter.com |
---|
| 323 | into twitter, and try to be sensible if we get anything else. */ |
---|
| 324 | td->prefix = g_strdup(url.host); |
---|
| 325 | if (g_str_has_suffix(td->prefix, ".com")) |
---|
| 326 | td->prefix[strlen(url.host) - 4] = '\0'; |
---|
[4bc66ae] | 327 | if ((s = strrchr(td->prefix, '.')) && strlen(s) > 4) { |
---|
| 328 | /* If we have at least 3 chars after the last dot, cut off the rest. |
---|
| 329 | (mostly a www/api prefix or sth) */ |
---|
[c0f33f1] | 330 | s = g_strdup(s + 1); |
---|
| 331 | g_free(td->prefix); |
---|
| 332 | td->prefix = s; |
---|
| 333 | } |
---|
| 334 | |
---|
[5983eca] | 335 | if (strstr(acc->pass, "oauth_token=")) |
---|
| 336 | td->oauth_info = oauth_from_string(acc->pass, get_oauth_service(ic)); |
---|
| 337 | |
---|
| 338 | sprintf(name, "%s_%s", td->prefix, acc->user); |
---|
| 339 | imcb_add_buddy(ic, name, NULL); |
---|
| 340 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
| 341 | |
---|
| 342 | if (set_getbool(&acc->set, "show_ids")) |
---|
| 343 | td->log = g_new0(struct twitter_log_data, TWITTER_LOG_LENGTH); |
---|
| 344 | |
---|
| 345 | twitter_login_finish(ic); |
---|
[1b221e0] | 346 | } |
---|
| 347 | |
---|
| 348 | /** |
---|
| 349 | * Logout method. Just free the twitter_data. |
---|
| 350 | */ |
---|
[5983eca] | 351 | static void twitter_logout(struct im_connection *ic) |
---|
[1b221e0] | 352 | { |
---|
| 353 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 354 | |
---|
[1b221e0] | 355 | // Set the status to logged out. |
---|
[5983eca] | 356 | ic->flags &= ~OPT_LOGGED_IN; |
---|
[1b221e0] | 357 | |
---|
[2abceca] | 358 | // Remove the main_loop function from the function queue. |
---|
| 359 | b_event_remove(td->main_loop_id); |
---|
| 360 | |
---|
[2322a9f] | 361 | if (td->timeline_gc) |
---|
| 362 | imcb_chat_free(td->timeline_gc); |
---|
[1014cab] | 363 | |
---|
[5983eca] | 364 | if (td) { |
---|
| 365 | oauth_info_free(td->oauth_info); |
---|
[de923d5] | 366 | g_free(td->user); |
---|
[5983eca] | 367 | g_free(td->prefix); |
---|
| 368 | g_free(td->url_host); |
---|
| 369 | g_free(td->url_path); |
---|
| 370 | g_free(td->log); |
---|
| 371 | g_free(td); |
---|
[1b221e0] | 372 | } |
---|
[62d2cfb] | 373 | |
---|
[5983eca] | 374 | twitter_connections = g_slist_remove(twitter_connections, ic); |
---|
[1b221e0] | 375 | } |
---|
| 376 | |
---|
[5983eca] | 377 | static void twitter_handle_command(struct im_connection *ic, char *message); |
---|
[7b87539] | 378 | |
---|
[1b221e0] | 379 | /** |
---|
| 380 | * |
---|
| 381 | */ |
---|
[5983eca] | 382 | static int twitter_buddy_msg(struct im_connection *ic, char *who, char *message, int away) |
---|
[1b221e0] | 383 | { |
---|
[c42e8b9] | 384 | struct twitter_data *td = ic->proto_data; |
---|
[5983eca] | 385 | int plen = strlen(td->prefix); |
---|
| 386 | |
---|
[ffcdf13] | 387 | if (g_strncasecmp(who, td->prefix, plen) == 0 && who[plen] == '_' && |
---|
[5983eca] | 388 | g_strcasecmp(who + plen + 1, ic->acc->user) == 0) { |
---|
| 389 | if (set_getbool(&ic->acc->set, "oauth") && |
---|
| 390 | td->oauth_info && td->oauth_info->token == NULL) { |
---|
| 391 | char pin[strlen(message) + 1], *s; |
---|
| 392 | |
---|
| 393 | strcpy(pin, message); |
---|
| 394 | for (s = pin + sizeof(pin) - 2; s > pin && isspace(*s); s--) |
---|
[64f8c425] | 395 | *s = '\0'; |
---|
[5983eca] | 396 | for (s = pin; *s && isspace(*s); s++) { |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | if (!oauth_access_token(s, td->oauth_info)) { |
---|
| 400 | imcb_error(ic, "OAuth error: %s", |
---|
| 401 | "Failed to send access token request"); |
---|
| 402 | imc_logout(ic, TRUE); |
---|
[c2ecadc] | 403 | return FALSE; |
---|
| 404 | } |
---|
[5983eca] | 405 | } else |
---|
[7b87539] | 406 | twitter_handle_command(ic, message); |
---|
[5983eca] | 407 | } else { |
---|
[e88fbe27] | 408 | twitter_direct_messages_new(ic, who, message); |
---|
[c42e8b9] | 409 | } |
---|
[5983eca] | 410 | return (0); |
---|
[1b221e0] | 411 | } |
---|
| 412 | |
---|
| 413 | /** |
---|
| 414 | * |
---|
| 415 | */ |
---|
[5983eca] | 416 | static void twitter_set_my_name(struct im_connection *ic, char *info) |
---|
[1b221e0] | 417 | { |
---|
| 418 | } |
---|
| 419 | |
---|
[5983eca] | 420 | static void twitter_get_info(struct im_connection *ic, char *who) |
---|
[1b221e0] | 421 | { |
---|
| 422 | } |
---|
| 423 | |
---|
[5983eca] | 424 | static void twitter_add_buddy(struct im_connection *ic, char *who, char *group) |
---|
[1b221e0] | 425 | { |
---|
[7d53efb] | 426 | twitter_friendships_create_destroy(ic, who, 1); |
---|
[1b221e0] | 427 | } |
---|
| 428 | |
---|
[5983eca] | 429 | static void twitter_remove_buddy(struct im_connection *ic, char *who, char *group) |
---|
[1b221e0] | 430 | { |
---|
[7d53efb] | 431 | twitter_friendships_create_destroy(ic, who, 0); |
---|
[1b221e0] | 432 | } |
---|
| 433 | |
---|
[5983eca] | 434 | static void twitter_chat_msg(struct groupchat *c, char *message, int flags) |
---|
[1b221e0] | 435 | { |
---|
[5983eca] | 436 | if (c && message) |
---|
| 437 | twitter_handle_command(c->ic, message); |
---|
[1b221e0] | 438 | } |
---|
| 439 | |
---|
[5983eca] | 440 | static void twitter_chat_invite(struct groupchat *c, char *who, char *message) |
---|
[1b221e0] | 441 | { |
---|
| 442 | } |
---|
| 443 | |
---|
[5983eca] | 444 | static void twitter_chat_leave(struct groupchat *c) |
---|
[1b221e0] | 445 | { |
---|
[16592d8] | 446 | struct twitter_data *td = c->ic->proto_data; |
---|
[5983eca] | 447 | |
---|
[2322a9f] | 448 | if (c != td->timeline_gc) |
---|
[5983eca] | 449 | return; /* WTF? */ |
---|
| 450 | |
---|
[16592d8] | 451 | /* If the user leaves the channel: Fine. Rejoin him/her once new |
---|
| 452 | tweets come in. */ |
---|
[2322a9f] | 453 | imcb_chat_free(td->timeline_gc); |
---|
| 454 | td->timeline_gc = NULL; |
---|
[1b221e0] | 455 | } |
---|
| 456 | |
---|
[5983eca] | 457 | static void twitter_keepalive(struct im_connection *ic) |
---|
[1b221e0] | 458 | { |
---|
| 459 | } |
---|
| 460 | |
---|
[5983eca] | 461 | static void twitter_add_permit(struct im_connection *ic, char *who) |
---|
[1b221e0] | 462 | { |
---|
| 463 | } |
---|
| 464 | |
---|
[5983eca] | 465 | static void twitter_rem_permit(struct im_connection *ic, char *who) |
---|
[1b221e0] | 466 | { |
---|
| 467 | } |
---|
| 468 | |
---|
[5983eca] | 469 | static void twitter_add_deny(struct im_connection *ic, char *who) |
---|
[1b221e0] | 470 | { |
---|
| 471 | } |
---|
| 472 | |
---|
[5983eca] | 473 | static void twitter_rem_deny(struct im_connection *ic, char *who) |
---|
[1b221e0] | 474 | { |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | //static char *twitter_set_display_name( set_t *set, char *value ) |
---|
| 478 | //{ |
---|
[5983eca] | 479 | // return value; |
---|
[1b221e0] | 480 | //} |
---|
[7b87539] | 481 | |
---|
[5983eca] | 482 | static void twitter_buddy_data_add(struct bee_user *bu) |
---|
[203a2d2] | 483 | { |
---|
[5983eca] | 484 | bu->data = g_new0(struct twitter_user_data, 1); |
---|
[203a2d2] | 485 | } |
---|
| 486 | |
---|
[5983eca] | 487 | static void twitter_buddy_data_free(struct bee_user *bu) |
---|
[203a2d2] | 488 | { |
---|
[5983eca] | 489 | g_free(bu->data); |
---|
[203a2d2] | 490 | } |
---|
| 491 | |
---|
[b61c74c] | 492 | /** Convert the given bitlbee tweet ID, bitlbee username, or twitter tweet ID |
---|
| 493 | * into a twitter tweet ID. |
---|
| 494 | * |
---|
| 495 | * Returns 0 if the user provides garbage. |
---|
| 496 | */ |
---|
| 497 | static guint64 twitter_message_id_from_command_arg(struct im_connection *ic, struct twitter_data *td, char *arg) { |
---|
| 498 | struct twitter_user_data *tud; |
---|
| 499 | bee_user_t *bu; |
---|
| 500 | guint64 id = 0; |
---|
| 501 | if (g_str_has_prefix(arg, "#") && |
---|
| 502 | sscanf(arg + 1, "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
| 503 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
| 504 | id = td->log[id].id; |
---|
| 505 | } else if ((bu = bee_user_by_handle(ic->bee, ic, arg)) && |
---|
| 506 | (tud = bu->data) && tud->last_id) |
---|
| 507 | id = tud->last_id; |
---|
| 508 | else if (sscanf(arg, "%" G_GUINT64_FORMAT, &id) == 1){ |
---|
| 509 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
| 510 | id = td->log[id].id; |
---|
| 511 | } |
---|
| 512 | return id; |
---|
| 513 | } |
---|
| 514 | |
---|
[5983eca] | 515 | static void twitter_handle_command(struct im_connection *ic, char *message) |
---|
[7b87539] | 516 | { |
---|
| 517 | struct twitter_data *td = ic->proto_data; |
---|
[15bc063] | 518 | char *cmds, **cmd, *new = NULL; |
---|
| 519 | guint64 in_reply_to = 0; |
---|
[5983eca] | 520 | |
---|
| 521 | cmds = g_strdup(message); |
---|
| 522 | cmd = split_command_parts(cmds); |
---|
| 523 | |
---|
| 524 | if (cmd[0] == NULL) { |
---|
| 525 | g_free(cmds); |
---|
[7b87539] | 526 | return; |
---|
[5983eca] | 527 | } else if (!set_getbool(&ic->acc->set, "commands")) { |
---|
[7b87539] | 528 | /* Not supporting commands. */ |
---|
[5983eca] | 529 | } else if (g_strcasecmp(cmd[0], "undo") == 0) { |
---|
[7b87539] | 530 | guint64 id; |
---|
[5983eca] | 531 | |
---|
[aa2f575] | 532 | if (cmd[1] == NULL) |
---|
| 533 | twitter_status_destroy(ic, td->last_status_id); |
---|
| 534 | else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
| 535 | if (id < TWITTER_LOG_LENGTH && td->log) |
---|
| 536 | id = td->log[id].id; |
---|
| 537 | |
---|
[5983eca] | 538 | twitter_status_destroy(ic, id); |
---|
[aa2f575] | 539 | } else |
---|
[5983eca] | 540 | twitter_msg(ic, "Could not undo last action"); |
---|
| 541 | |
---|
[b61c74c] | 542 | g_free(cmds); |
---|
| 543 | return; |
---|
| 544 | } else if (g_strcasecmp(cmd[0], "favourite") == 0 && cmd[1]) { |
---|
| 545 | guint64 id; |
---|
| 546 | if ((id = twitter_message_id_from_command_arg(ic, td, cmd[1]))) { |
---|
| 547 | twitter_favourite_tweet(ic, id); |
---|
| 548 | } else { |
---|
| 549 | twitter_msg(ic, "Please provide a message ID or username."); |
---|
| 550 | } |
---|
[5983eca] | 551 | g_free(cmds); |
---|
[7b87539] | 552 | return; |
---|
[5983eca] | 553 | } else if (g_strcasecmp(cmd[0], "follow") == 0 && cmd[1]) { |
---|
| 554 | twitter_add_buddy(ic, cmd[1], NULL); |
---|
| 555 | g_free(cmds); |
---|
[b890626] | 556 | return; |
---|
[5983eca] | 557 | } else if (g_strcasecmp(cmd[0], "unfollow") == 0 && cmd[1]) { |
---|
| 558 | twitter_remove_buddy(ic, cmd[1], NULL); |
---|
| 559 | g_free(cmds); |
---|
[b890626] | 560 | return; |
---|
[d18dee42] | 561 | } else if ((g_strcasecmp(cmd[0], "report") == 0 || |
---|
| 562 | g_strcasecmp(cmd[0], "spam") == 0) && cmd[1]) { |
---|
| 563 | char * screen_name; |
---|
| 564 | guint64 id; |
---|
[7de784c] | 565 | screen_name = cmd[1]; |
---|
[d18dee42] | 566 | /* Report nominally works on users but look up the user who |
---|
| 567 | posted the given ID if the user wants to do it that way */ |
---|
| 568 | if (g_str_has_prefix(cmd[1], "#") && |
---|
| 569 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1) { |
---|
| 570 | if (id < TWITTER_LOG_LENGTH && td->log) { |
---|
| 571 | if (g_slist_find(ic->bee->users, td->log[id].bu)) { |
---|
| 572 | screen_name = td->log[id].bu->handle; |
---|
| 573 | } |
---|
| 574 | } |
---|
| 575 | } |
---|
| 576 | twitter_report_spam(ic, screen_name); |
---|
| 577 | g_free(cmds); |
---|
| 578 | return; |
---|
[5983eca] | 579 | } else if (g_strcasecmp(cmd[0], "rt") == 0 && cmd[1]) { |
---|
[b61c74c] | 580 | guint64 id = twitter_message_id_from_command_arg(ic, td, cmd[1]); |
---|
[5983eca] | 581 | |
---|
[b890626] | 582 | td->last_status_id = 0; |
---|
[5983eca] | 583 | if (id) |
---|
| 584 | twitter_status_retweet(ic, id); |
---|
[665c24f] | 585 | else |
---|
[5983eca] | 586 | twitter_msg(ic, "User `%s' does not exist or didn't " |
---|
| 587 | "post any statuses recently", cmd[1]); |
---|
| 588 | |
---|
| 589 | g_free(cmds); |
---|
[b890626] | 590 | return; |
---|
[5983eca] | 591 | } else if (g_strcasecmp(cmd[0], "reply") == 0 && cmd[1] && cmd[2]) { |
---|
[15bc063] | 592 | struct twitter_user_data *tud; |
---|
| 593 | bee_user_t *bu = NULL; |
---|
| 594 | guint64 id = 0; |
---|
[5983eca] | 595 | |
---|
[aa2f575] | 596 | if (g_str_has_prefix(cmd[1], "#") && |
---|
| 597 | sscanf(cmd[1] + 1, "%" G_GUINT64_FORMAT, &id) == 1 && |
---|
| 598 | (id < TWITTER_LOG_LENGTH) && td->log) { |
---|
| 599 | bu = td->log[id].bu; |
---|
| 600 | if (g_slist_find(ic->bee->users, bu)) |
---|
| 601 | id = td->log[id].id; |
---|
| 602 | else |
---|
| 603 | bu = NULL; |
---|
| 604 | } else if ((bu = bee_user_by_handle(ic->bee, ic, cmd[1])) && |
---|
[5983eca] | 605 | (tud = bu->data) && tud->last_id) { |
---|
[15bc063] | 606 | id = tud->last_id; |
---|
[7ceb6b2] | 607 | } else if (sscanf(cmd[1], "%" G_GUINT64_FORMAT, &id) == 1 && |
---|
| 608 | (id < TWITTER_LOG_LENGTH) && td->log) { |
---|
[15bc063] | 609 | bu = td->log[id].bu; |
---|
[5983eca] | 610 | if (g_slist_find(ic->bee->users, bu)) |
---|
[15bc063] | 611 | id = td->log[id].id; |
---|
| 612 | else |
---|
| 613 | bu = NULL; |
---|
| 614 | } |
---|
[aa2f575] | 615 | |
---|
[5983eca] | 616 | if (!id || !bu) { |
---|
| 617 | twitter_msg(ic, "User `%s' does not exist or didn't " |
---|
| 618 | "post any statuses recently", cmd[1]); |
---|
[d3bfe3d] | 619 | g_free(cmds); |
---|
[15bc063] | 620 | return; |
---|
| 621 | } |
---|
[5983eca] | 622 | message = new = g_strdup_printf("@%s %s", bu->handle, message + (cmd[2] - cmd[0])); |
---|
[15bc063] | 623 | in_reply_to = id; |
---|
[5983eca] | 624 | } else if (g_strcasecmp(cmd[0], "post") == 0) { |
---|
[7b87539] | 625 | message += 5; |
---|
| 626 | } |
---|
[5983eca] | 627 | |
---|
[7b87539] | 628 | { |
---|
[15bc063] | 629 | char *s; |
---|
[7b87539] | 630 | bee_user_t *bu; |
---|
[5983eca] | 631 | |
---|
| 632 | if (!twitter_length_check(ic, message)) { |
---|
| 633 | g_free(new); |
---|
| 634 | g_free(cmds); |
---|
| 635 | return; |
---|
[7b87539] | 636 | } |
---|
[5983eca] | 637 | |
---|
| 638 | s = cmd[0] + strlen(cmd[0]) - 1; |
---|
| 639 | if (!new && s > cmd[0] && (*s == ':' || *s == ',')) { |
---|
[7b87539] | 640 | *s = '\0'; |
---|
[5983eca] | 641 | |
---|
| 642 | if ((bu = bee_user_by_handle(ic->bee, ic, cmd[0]))) { |
---|
[b890626] | 643 | struct twitter_user_data *tud = bu->data; |
---|
[5983eca] | 644 | |
---|
| 645 | new = g_strdup_printf("@%s %s", bu->handle, |
---|
| 646 | message + (s - cmd[0]) + 2); |
---|
[7b87539] | 647 | message = new; |
---|
[5983eca] | 648 | |
---|
| 649 | if (time(NULL) < tud->last_time + |
---|
| 650 | set_getint(&ic->acc->set, "auto_reply_timeout")) |
---|
[b890626] | 651 | in_reply_to = tud->last_id; |
---|
[7b87539] | 652 | } |
---|
| 653 | } |
---|
[5983eca] | 654 | |
---|
[b890626] | 655 | /* If the user runs undo between this request and its response |
---|
| 656 | this would delete the second-last Tweet. Prevent that. */ |
---|
| 657 | td->last_status_id = 0; |
---|
[5983eca] | 658 | twitter_post_status(ic, message, in_reply_to); |
---|
| 659 | g_free(new); |
---|
[7b87539] | 660 | } |
---|
[5983eca] | 661 | g_free(cmds); |
---|
[7b87539] | 662 | } |
---|
[1b221e0] | 663 | |
---|
| 664 | void twitter_initmodule() |
---|
| 665 | { |
---|
| 666 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
[5983eca] | 667 | |
---|
[1dd3470] | 668 | ret->options = OPT_NOOTR; |
---|
[1b221e0] | 669 | ret->name = "twitter"; |
---|
| 670 | ret->login = twitter_login; |
---|
| 671 | ret->init = twitter_init; |
---|
| 672 | ret->logout = twitter_logout; |
---|
| 673 | ret->buddy_msg = twitter_buddy_msg; |
---|
| 674 | ret->get_info = twitter_get_info; |
---|
| 675 | ret->set_my_name = twitter_set_my_name; |
---|
| 676 | ret->add_buddy = twitter_add_buddy; |
---|
| 677 | ret->remove_buddy = twitter_remove_buddy; |
---|
| 678 | ret->chat_msg = twitter_chat_msg; |
---|
| 679 | ret->chat_invite = twitter_chat_invite; |
---|
| 680 | ret->chat_leave = twitter_chat_leave; |
---|
| 681 | ret->keepalive = twitter_keepalive; |
---|
| 682 | ret->add_permit = twitter_add_permit; |
---|
| 683 | ret->rem_permit = twitter_rem_permit; |
---|
| 684 | ret->add_deny = twitter_add_deny; |
---|
| 685 | ret->rem_deny = twitter_rem_deny; |
---|
[203a2d2] | 686 | ret->buddy_data_add = twitter_buddy_data_add; |
---|
| 687 | ret->buddy_data_free = twitter_buddy_data_free; |
---|
[1b221e0] | 688 | ret->handle_cmp = g_strcasecmp; |
---|
[5983eca] | 689 | |
---|
[ffcdf13] | 690 | register_protocol(ret); |
---|
[1b221e0] | 691 | |
---|
[ffcdf13] | 692 | /* And an identi.ca variant: */ |
---|
| 693 | ret = g_memdup(ret, sizeof(struct prpl)); |
---|
| 694 | ret->name = "identica"; |
---|
[1b221e0] | 695 | register_protocol(ret); |
---|
| 696 | } |
---|