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