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