[1b221e0] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Simple module to facilitate twitter functionality. * |
---|
| 5 | * * |
---|
[199fea6] | 6 | * Copyright 2009-2010 Geert Mulders <g.c.w.m.mulders@gmail.com> * |
---|
[2a6da96] | 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 | |
---|
[08579a1] | 25 | /* For strptime(): */ |
---|
[daae10f] | 26 | #if(__sun) |
---|
| 27 | #else |
---|
[08579a1] | 28 | #define _XOPEN_SOURCE |
---|
[daae10f] | 29 | #endif |
---|
[08579a1] | 30 | |
---|
[1b221e0] | 31 | #include "twitter_http.h" |
---|
| 32 | #include "twitter.h" |
---|
| 33 | #include "bitlbee.h" |
---|
| 34 | #include "url.h" |
---|
| 35 | #include "misc.h" |
---|
| 36 | #include "base64.h" |
---|
| 37 | #include "twitter_lib.h" |
---|
[e08ae0c] | 38 | #include "json_util.h" |
---|
[1b221e0] | 39 | #include <ctype.h> |
---|
| 40 | #include <errno.h> |
---|
| 41 | |
---|
[e4e0b37] | 42 | /* GLib < 2.12.0 doesn't have g_ascii_strtoll(), work around using system strtoll(). */ |
---|
| 43 | /* GLib < 2.12.4 can be buggy: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=488013 */ |
---|
| 44 | #if !GLIB_CHECK_VERSION(2,12,5) |
---|
| 45 | #include <stdlib.h> |
---|
| 46 | #include <limits.h> |
---|
| 47 | #define g_ascii_strtoll strtoll |
---|
| 48 | #endif |
---|
| 49 | |
---|
[1b221e0] | 50 | #define TXL_STATUS 1 |
---|
[62d2cfb] | 51 | #define TXL_USER 2 |
---|
| 52 | #define TXL_ID 3 |
---|
| 53 | |
---|
[1b221e0] | 54 | struct twitter_xml_list { |
---|
[62d2cfb] | 55 | int type; |
---|
[8203da9] | 56 | gint64 next_cursor; |
---|
[1b221e0] | 57 | GSList *list; |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | struct twitter_xml_user { |
---|
| 61 | char *name; |
---|
| 62 | char *screen_name; |
---|
| 63 | }; |
---|
| 64 | |
---|
| 65 | struct twitter_xml_status { |
---|
[08579a1] | 66 | time_t created_at; |
---|
[1b221e0] | 67 | char *text; |
---|
| 68 | struct twitter_xml_user *user; |
---|
[67f6828] | 69 | guint64 id, rt_id; /* Usually equal, with RTs id == *original* id */ |
---|
| 70 | guint64 reply_to; |
---|
[1b221e0] | 71 | }; |
---|
| 72 | |
---|
[62d2cfb] | 73 | /** |
---|
| 74 | * Frees a twitter_xml_user struct. |
---|
| 75 | */ |
---|
| 76 | static void txu_free(struct twitter_xml_user *txu) |
---|
| 77 | { |
---|
[a26af5c] | 78 | if (txu == NULL) |
---|
| 79 | return; |
---|
[2322a9f] | 80 | |
---|
[62d2cfb] | 81 | g_free(txu->name); |
---|
| 82 | g_free(txu->screen_name); |
---|
[2abceca] | 83 | g_free(txu); |
---|
[62d2cfb] | 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * Frees a twitter_xml_status struct. |
---|
| 88 | */ |
---|
| 89 | static void txs_free(struct twitter_xml_status *txs) |
---|
| 90 | { |
---|
[2322a9f] | 91 | if (txs == NULL) |
---|
| 92 | return; |
---|
| 93 | |
---|
[62d2cfb] | 94 | g_free(txs->text); |
---|
| 95 | txu_free(txs->user); |
---|
[2abceca] | 96 | g_free(txs); |
---|
[62d2cfb] | 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Free a twitter_xml_list struct. |
---|
| 101 | * type is the type of list the struct holds. |
---|
| 102 | */ |
---|
| 103 | static void txl_free(struct twitter_xml_list *txl) |
---|
| 104 | { |
---|
| 105 | GSList *l; |
---|
[a26af5c] | 106 | if (txl == NULL) |
---|
| 107 | return; |
---|
[2322a9f] | 108 | |
---|
| 109 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
| 110 | if (txl->type == TXL_STATUS) { |
---|
[5983eca] | 111 | txs_free((struct twitter_xml_status *) l->data); |
---|
[2322a9f] | 112 | } else if (txl->type == TXL_ID) { |
---|
[62d2cfb] | 113 | g_free(l->data); |
---|
[2322a9f] | 114 | } else if (txl->type == TXL_USER) { |
---|
[de923d5] | 115 | txu_free(l->data); |
---|
[2322a9f] | 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
[62d2cfb] | 119 | g_slist_free(txl->list); |
---|
[fd65edb] | 120 | g_free(txl); |
---|
[62d2cfb] | 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
[2322a9f] | 124 | * Compare status elements |
---|
| 125 | */ |
---|
| 126 | static gint twitter_compare_elements(gconstpointer a, gconstpointer b) |
---|
| 127 | { |
---|
| 128 | struct twitter_xml_status *a_status = (struct twitter_xml_status *) a; |
---|
| 129 | struct twitter_xml_status *b_status = (struct twitter_xml_status *) b; |
---|
| 130 | |
---|
| 131 | if (a_status->created_at < b_status->created_at) { |
---|
| 132 | return -1; |
---|
| 133 | } else if (a_status->created_at > b_status->created_at) { |
---|
| 134 | return 1; |
---|
| 135 | } else { |
---|
| 136 | return 0; |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | * Add a buddy if it is not already added, set the status to logged in. |
---|
[62d2cfb] | 142 | */ |
---|
[3e69802] | 143 | static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname) |
---|
[62d2cfb] | 144 | { |
---|
[1014cab] | 145 | struct twitter_data *td = ic->proto_data; |
---|
| 146 | |
---|
[de923d5] | 147 | // Check if the buddy is already in the buddy list. |
---|
[5983eca] | 148 | if (!bee_user_by_handle(ic->bee, ic, name)) { |
---|
[62d2cfb] | 149 | // The buddy is not in the list, add the buddy and set the status to logged in. |
---|
[5983eca] | 150 | imcb_add_buddy(ic, name, NULL); |
---|
| 151 | imcb_rename_buddy(ic, name, fullname); |
---|
[631ec80] | 152 | if (td->flags & TWITTER_MODE_CHAT) { |
---|
[5c18a76] | 153 | /* Necessary so that nicks always get translated to the |
---|
| 154 | exact Twitter username. */ |
---|
[5983eca] | 155 | imcb_buddy_nick_hint(ic, name, name); |
---|
[7f557d5] | 156 | if (td->timeline_gc) |
---|
| 157 | imcb_chat_add_buddy(td->timeline_gc, name); |
---|
[631ec80] | 158 | } else if (td->flags & TWITTER_MODE_MANY) |
---|
[5983eca] | 159 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
[62d2cfb] | 160 | } |
---|
| 161 | } |
---|
[1b221e0] | 162 | |
---|
[a7b9ec7] | 163 | /* Warning: May return a malloc()ed value, which will be free()d on the next |
---|
[de923d5] | 164 | call. Only for short-term use. NOT THREADSAFE! */ |
---|
[6eca2eb] | 165 | char *twitter_parse_error(struct http_request *req) |
---|
[a7b9ec7] | 166 | { |
---|
| 167 | static char *ret = NULL; |
---|
[5246133] | 168 | json_value *root, *err; |
---|
[5983eca] | 169 | |
---|
[a7b9ec7] | 170 | g_free(ret); |
---|
| 171 | ret = NULL; |
---|
[5983eca] | 172 | |
---|
| 173 | if (req->body_size > 0) { |
---|
[5246133] | 174 | root = json_parse(req->reply_body); |
---|
| 175 | err = json_o_get(root, "errors"); |
---|
[dff0e0b] | 176 | if (err && err->type == json_array && (err = err->u.array.values[0]) && |
---|
[5246133] | 177 | err->type == json_object) { |
---|
| 178 | const char *msg = json_o_str(err, "message"); |
---|
| 179 | if (msg) |
---|
| 180 | ret = g_strdup_printf("%s (%s)", req->status_string, msg); |
---|
| 181 | } |
---|
| 182 | json_value_free(root); |
---|
[a7b9ec7] | 183 | } |
---|
[5983eca] | 184 | |
---|
[6eca2eb] | 185 | return ret ? ret : req->status_string; |
---|
[a7b9ec7] | 186 | } |
---|
| 187 | |
---|
[fb351ce] | 188 | /* WATCH OUT: This function might or might not destroy your connection. |
---|
| 189 | Sub-optimal indeed, but just be careful when this returns NULL! */ |
---|
[e08ae0c] | 190 | static json_value *twitter_parse_response(struct im_connection *ic, struct http_request *req) |
---|
[2a6da96] | 191 | { |
---|
| 192 | gboolean logging_in = !(ic->flags & OPT_LOGGED_IN); |
---|
| 193 | gboolean periodic; |
---|
| 194 | struct twitter_data *td = ic->proto_data; |
---|
[e08ae0c] | 195 | json_value *ret; |
---|
[2a6da96] | 196 | char path[64] = "", *s; |
---|
| 197 | |
---|
| 198 | if ((s = strchr(req->request, ' '))) { |
---|
| 199 | path[sizeof(path)-1] = '\0'; |
---|
| 200 | strncpy(path, s + 1, sizeof(path) - 1); |
---|
| 201 | if ((s = strchr(path, '?')) || (s = strchr(path, ' '))) |
---|
| 202 | *s = '\0'; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /* Kinda nasty. :-( Trying to suppress error messages, but only |
---|
| 206 | for periodic (i.e. mentions/timeline) queries. */ |
---|
| 207 | periodic = strstr(path, "timeline") || strstr(path, "mentions"); |
---|
| 208 | |
---|
| 209 | if (req->status_code == 401 && logging_in) { |
---|
| 210 | /* IIRC Twitter once had an outage where they were randomly |
---|
| 211 | throwing 401s so I'll keep treating this one as fatal |
---|
| 212 | only during login. */ |
---|
[5246133] | 213 | imcb_error(ic, "Authentication failure (%s)", |
---|
| 214 | twitter_parse_error(req)); |
---|
[2a6da96] | 215 | imc_logout(ic, FALSE); |
---|
| 216 | return NULL; |
---|
| 217 | } else if (req->status_code != 200) { |
---|
| 218 | // It didn't go well, output the error and return. |
---|
| 219 | if (!periodic || logging_in || ++td->http_fails >= 5) |
---|
[96dd574] | 220 | twitter_log(ic, "Error: Could not retrieve %s: %s", |
---|
| 221 | path, twitter_parse_error(req)); |
---|
[2a6da96] | 222 | |
---|
| 223 | if (logging_in) |
---|
| 224 | imc_logout(ic, TRUE); |
---|
| 225 | return NULL; |
---|
| 226 | } else { |
---|
| 227 | td->http_fails = 0; |
---|
| 228 | } |
---|
| 229 | |
---|
[e08ae0c] | 230 | if ((ret = json_parse(req->reply_body)) == NULL) { |
---|
[2a6da96] | 231 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 232 | path, "XML parse error"); |
---|
| 233 | } |
---|
| 234 | return ret; |
---|
| 235 | } |
---|
| 236 | |
---|
[1b221e0] | 237 | static void twitter_http_get_friends_ids(struct http_request *req); |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * Get the friends ids. |
---|
| 241 | */ |
---|
[8203da9] | 242 | void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor) |
---|
[1b221e0] | 243 | { |
---|
[5983eca] | 244 | // Primitive, but hey! It works... |
---|
| 245 | char *args[2]; |
---|
[1b221e0] | 246 | args[0] = "cursor"; |
---|
[5983eca] | 247 | args[1] = g_strdup_printf("%lld", (long long) next_cursor); |
---|
[bb5ce4d1] | 248 | twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2); |
---|
[1b221e0] | 249 | |
---|
| 250 | g_free(args[1]); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | /** |
---|
| 254 | * Fill a list of ids. |
---|
| 255 | */ |
---|
[9e8c945] | 256 | static gboolean twitter_xt_get_friends_id_list(json_value *node, struct twitter_xml_list *txl) |
---|
[1b221e0] | 257 | { |
---|
[e08ae0c] | 258 | json_value *c; |
---|
| 259 | int i; |
---|
[5983eca] | 260 | |
---|
[62d2cfb] | 261 | // Set the list type. |
---|
| 262 | txl->type = TXL_ID; |
---|
[1b221e0] | 263 | |
---|
[e08ae0c] | 264 | c = json_o_get(node, "ids"); |
---|
| 265 | if (!c || c->type != json_array) |
---|
[9e8c945] | 266 | return FALSE; |
---|
[1b221e0] | 267 | |
---|
[e08ae0c] | 268 | for (i = 0; i < c->u.array.length; i ++) { |
---|
[0688e99] | 269 | if (c->u.array.values[i]->type != json_integer) |
---|
| 270 | continue; |
---|
| 271 | |
---|
[e08ae0c] | 272 | txl->list = g_slist_prepend(txl->list, |
---|
[8e3b7ac] | 273 | g_strdup_printf("%lld", c->u.array.values[i]->u.integer)); |
---|
[e08ae0c] | 274 | } |
---|
| 275 | |
---|
| 276 | c = json_o_get(node, "next_cursor"); |
---|
| 277 | if (c && c->type == json_integer) |
---|
| 278 | txl->next_cursor = c->u.integer; |
---|
| 279 | else |
---|
| 280 | txl->next_cursor = -1; |
---|
| 281 | |
---|
[9e8c945] | 282 | return TRUE; |
---|
[1b221e0] | 283 | } |
---|
| 284 | |
---|
[de923d5] | 285 | static void twitter_get_users_lookup(struct im_connection *ic); |
---|
| 286 | |
---|
[1b221e0] | 287 | /** |
---|
| 288 | * Callback for getting the friends ids. |
---|
| 289 | */ |
---|
| 290 | static void twitter_http_get_friends_ids(struct http_request *req) |
---|
| 291 | { |
---|
| 292 | struct im_connection *ic; |
---|
[e08ae0c] | 293 | json_value *parsed; |
---|
[1b221e0] | 294 | struct twitter_xml_list *txl; |
---|
[3bd4a93] | 295 | struct twitter_data *td; |
---|
[1b221e0] | 296 | |
---|
| 297 | ic = req->data; |
---|
| 298 | |
---|
[62d2cfb] | 299 | // Check if the connection is still active. |
---|
[5983eca] | 300 | if (!g_slist_find(twitter_connections, ic)) |
---|
[62d2cfb] | 301 | return; |
---|
[5983eca] | 302 | |
---|
[37aa317] | 303 | td = ic->proto_data; |
---|
[62d2cfb] | 304 | |
---|
[1b221e0] | 305 | txl = g_new0(struct twitter_xml_list, 1); |
---|
[de923d5] | 306 | txl->list = td->follow_ids; |
---|
[1b221e0] | 307 | |
---|
| 308 | // Parse the data. |
---|
[2a6da96] | 309 | if (!(parsed = twitter_parse_response(ic, req))) |
---|
| 310 | return; |
---|
[e08ae0c] | 311 | |
---|
[d0752e8] | 312 | twitter_xt_get_friends_id_list(parsed, txl); |
---|
[e08ae0c] | 313 | json_value_free(parsed); |
---|
[1b221e0] | 314 | |
---|
[de923d5] | 315 | td->follow_ids = txl->list; |
---|
[1b221e0] | 316 | if (txl->next_cursor) |
---|
[de923d5] | 317 | /* These were just numbers. Up to 4000 in a response AFAIK so if we get here |
---|
| 318 | we may be using a spammer account. \o/ */ |
---|
[1b221e0] | 319 | twitter_get_friends_ids(ic, txl->next_cursor); |
---|
[de923d5] | 320 | else |
---|
| 321 | /* Now to convert all those numbers into names.. */ |
---|
| 322 | twitter_get_users_lookup(ic); |
---|
| 323 | |
---|
| 324 | txl->list = NULL; |
---|
| 325 | txl_free(txl); |
---|
| 326 | } |
---|
| 327 | |
---|
[9e8c945] | 328 | static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl); |
---|
[de923d5] | 329 | static void twitter_http_get_users_lookup(struct http_request *req); |
---|
| 330 | |
---|
| 331 | static void twitter_get_users_lookup(struct im_connection *ic) |
---|
| 332 | { |
---|
| 333 | struct twitter_data *td = ic->proto_data; |
---|
| 334 | char *args[2] = { |
---|
| 335 | "user_id", |
---|
| 336 | NULL, |
---|
| 337 | }; |
---|
| 338 | GString *ids = g_string_new(""); |
---|
| 339 | int i; |
---|
| 340 | |
---|
| 341 | /* We can request up to 100 users at a time. */ |
---|
| 342 | for (i = 0; i < 100 && td->follow_ids; i ++) { |
---|
| 343 | g_string_append_printf(ids, ",%s", (char*) td->follow_ids->data); |
---|
| 344 | g_free(td->follow_ids->data); |
---|
| 345 | td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data); |
---|
| 346 | } |
---|
| 347 | if (ids->len > 0) { |
---|
| 348 | args[1] = ids->str + 1; |
---|
| 349 | /* POST, because I think ids can be up to 1KB long. */ |
---|
| 350 | twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2); |
---|
| 351 | } else { |
---|
| 352 | /* We have all users. Continue with login. (Get statuses.) */ |
---|
| 353 | td->flags |= TWITTER_HAVE_FRIENDS; |
---|
| 354 | twitter_login_finish(ic); |
---|
| 355 | } |
---|
| 356 | g_string_free(ids, TRUE); |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | /** |
---|
| 360 | * Callback for getting (twitter)friends... |
---|
| 361 | * |
---|
| 362 | * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has |
---|
| 363 | * hundreds of friends?" you wonder? You probably not, since you are reading the source of |
---|
| 364 | * BitlBee... Get a life and meet new people! |
---|
| 365 | */ |
---|
| 366 | static void twitter_http_get_users_lookup(struct http_request *req) |
---|
| 367 | { |
---|
| 368 | struct im_connection *ic = req->data; |
---|
[e08ae0c] | 369 | json_value *parsed; |
---|
[de923d5] | 370 | struct twitter_xml_list *txl; |
---|
| 371 | GSList *l = NULL; |
---|
| 372 | struct twitter_xml_user *user; |
---|
| 373 | |
---|
| 374 | // Check if the connection is still active. |
---|
| 375 | if (!g_slist_find(twitter_connections, ic)) |
---|
| 376 | return; |
---|
| 377 | |
---|
| 378 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 379 | txl->list = NULL; |
---|
[1b221e0] | 380 | |
---|
[de923d5] | 381 | // Get the user list from the parsed xml feed. |
---|
[2a6da96] | 382 | if (!(parsed = twitter_parse_response(ic, req))) |
---|
| 383 | return; |
---|
[d0752e8] | 384 | twitter_xt_get_users(parsed, txl); |
---|
[e08ae0c] | 385 | json_value_free(parsed); |
---|
[de923d5] | 386 | |
---|
| 387 | // Add the users as buddies. |
---|
| 388 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
| 389 | user = l->data; |
---|
| 390 | twitter_add_buddy(ic, user->screen_name, user->name); |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | // Free the structure. |
---|
[62d2cfb] | 394 | txl_free(txl); |
---|
[de923d5] | 395 | |
---|
| 396 | twitter_get_users_lookup(ic); |
---|
[1b221e0] | 397 | } |
---|
| 398 | |
---|
[8e3b7ac] | 399 | struct twitter_xml_user *twitter_xt_get_user(const json_value *node) |
---|
| 400 | { |
---|
| 401 | struct twitter_xml_user *txu; |
---|
| 402 | |
---|
| 403 | txu = g_new0(struct twitter_xml_user, 1); |
---|
| 404 | txu->name = g_strdup(json_o_str(node, "name")); |
---|
| 405 | txu->screen_name = g_strdup(json_o_str(node, "screen_name")); |
---|
| 406 | |
---|
| 407 | return txu; |
---|
| 408 | } |
---|
| 409 | |
---|
[62d2cfb] | 410 | /** |
---|
| 411 | * Function to fill a twitter_xml_list struct. |
---|
| 412 | * It sets: |
---|
| 413 | * - all <user>s from the <users> element. |
---|
| 414 | */ |
---|
[9e8c945] | 415 | static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl) |
---|
[62d2cfb] | 416 | { |
---|
| 417 | struct twitter_xml_user *txu; |
---|
[e08ae0c] | 418 | int i; |
---|
[62d2cfb] | 419 | |
---|
| 420 | // Set the type of the list. |
---|
| 421 | txl->type = TXL_USER; |
---|
| 422 | |
---|
[e08ae0c] | 423 | if (!node || node->type != json_array) |
---|
[9e8c945] | 424 | return FALSE; |
---|
[e08ae0c] | 425 | |
---|
[62d2cfb] | 426 | // The root <users> node should hold the list of users <user> |
---|
| 427 | // Walk over the nodes children. |
---|
[e08ae0c] | 428 | for (i = 0; i < node->u.array.length; i ++) { |
---|
[8e3b7ac] | 429 | txu = twitter_xt_get_user(node->u.array.values[i]); |
---|
| 430 | if (txu) |
---|
| 431 | txl->list = g_slist_prepend(txl->list, txu); |
---|
[62d2cfb] | 432 | } |
---|
| 433 | |
---|
[9e8c945] | 434 | return TRUE; |
---|
[62d2cfb] | 435 | } |
---|
| 436 | |
---|
[2b02617] | 437 | #ifdef __GLIBC__ |
---|
| 438 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y" |
---|
| 439 | #else |
---|
| 440 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y" |
---|
| 441 | #endif |
---|
[62d2cfb] | 442 | |
---|
[d1356cb] | 443 | static char* expand_entities(char* text, const json_value *entities); |
---|
| 444 | |
---|
[1b221e0] | 445 | /** |
---|
| 446 | * Function to fill a twitter_xml_status struct. |
---|
| 447 | * It sets: |
---|
| 448 | * - the status text and |
---|
| 449 | * - the created_at timestamp and |
---|
| 450 | * - the status id and |
---|
| 451 | * - the user in a twitter_xml_user struct. |
---|
| 452 | */ |
---|
[c9b5817] | 453 | static struct twitter_xml_status *twitter_xt_get_status(const json_value *node) |
---|
[1b221e0] | 454 | { |
---|
[c9b5817] | 455 | struct twitter_xml_status *txs; |
---|
[fb351ce] | 456 | const json_value *rt = NULL, *entities = NULL; |
---|
[8e3b7ac] | 457 | |
---|
| 458 | if (node->type != json_object) |
---|
[9e8c945] | 459 | return FALSE; |
---|
[c9b5817] | 460 | txs = g_new0(struct twitter_xml_status, 1); |
---|
[1b221e0] | 461 | |
---|
[9e8c945] | 462 | JSON_O_FOREACH (node, k, v) { |
---|
[8e3b7ac] | 463 | if (strcmp("text", k) == 0 && v->type == json_string) { |
---|
[fb351ce] | 464 | txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1); |
---|
[29f72b7] | 465 | strip_html(txs->text); |
---|
[8e3b7ac] | 466 | } else if (strcmp("retweeted_status", k) == 0 && v->type == json_object) { |
---|
| 467 | rt = v; |
---|
| 468 | } else if (strcmp("created_at", k) == 0 && v->type == json_string) { |
---|
[08579a1] | 469 | struct tm parsed; |
---|
[5983eca] | 470 | |
---|
[08579a1] | 471 | /* Very sensitive to changes to the formatting of |
---|
| 472 | this field. :-( Also assumes the timezone used |
---|
| 473 | is UTC since C time handling functions suck. */ |
---|
[8e3b7ac] | 474 | if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) |
---|
[5983eca] | 475 | txs->created_at = mktime_utc(&parsed); |
---|
[8e3b7ac] | 476 | } else if (strcmp("user", k) == 0 && v->type == json_object) { |
---|
| 477 | txs->user = twitter_xt_get_user(v); |
---|
| 478 | } else if (strcmp("id", k) == 0 && v->type == json_integer) { |
---|
[573e274] | 479 | txs->rt_id = txs->id = v->u.integer; |
---|
[8e3b7ac] | 480 | } else if (strcmp("in_reply_to_status_id", k) == 0 && v->type == json_integer) { |
---|
| 481 | txs->reply_to = v->u.integer; |
---|
| 482 | } else if (strcmp("entities", k) == 0 && v->type == json_object) { |
---|
[fb351ce] | 483 | entities = v; |
---|
[ce81acd] | 484 | } |
---|
[1b221e0] | 485 | } |
---|
[5983eca] | 486 | |
---|
[0fff0b2] | 487 | /* If it's a (truncated) retweet, get the original. Even if the API claims it |
---|
| 488 | wasn't truncated because it may be lying. */ |
---|
| 489 | if (rt) { |
---|
[c9b5817] | 490 | struct twitter_xml_status *rtxs = twitter_xt_get_status(rt); |
---|
| 491 | if (rtxs) { |
---|
| 492 | g_free(txs->text); |
---|
| 493 | txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text); |
---|
| 494 | txs->id = rtxs->id; |
---|
[e193aeb] | 495 | txs_free(rtxs); |
---|
| 496 | } |
---|
[fb351ce] | 497 | } else if (entities) { |
---|
[d1356cb] | 498 | txs->text = expand_entities(txs->text, entities); |
---|
| 499 | } |
---|
| 500 | |
---|
[c9b5817] | 501 | if (txs->text && txs->user && txs->id) |
---|
| 502 | return txs; |
---|
| 503 | |
---|
| 504 | txs_free(txs); |
---|
| 505 | return NULL; |
---|
[d1356cb] | 506 | } |
---|
| 507 | |
---|
| 508 | /** |
---|
| 509 | * Function to fill a twitter_xml_status struct (DM variant). |
---|
| 510 | */ |
---|
[2cd8540] | 511 | static struct twitter_xml_status *twitter_xt_get_dm(const json_value *node) |
---|
[d1356cb] | 512 | { |
---|
[2cd8540] | 513 | struct twitter_xml_status *txs; |
---|
[d1356cb] | 514 | const json_value *entities = NULL; |
---|
| 515 | |
---|
| 516 | if (node->type != json_object) |
---|
| 517 | return FALSE; |
---|
[2cd8540] | 518 | txs = g_new0(struct twitter_xml_status, 1); |
---|
[d1356cb] | 519 | |
---|
| 520 | JSON_O_FOREACH (node, k, v) { |
---|
| 521 | if (strcmp("text", k) == 0 && v->type == json_string) { |
---|
| 522 | txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1); |
---|
[29f72b7] | 523 | strip_html(txs->text); |
---|
[d1356cb] | 524 | } else if (strcmp("created_at", k) == 0 && v->type == json_string) { |
---|
| 525 | struct tm parsed; |
---|
| 526 | |
---|
| 527 | /* Very sensitive to changes to the formatting of |
---|
| 528 | this field. :-( Also assumes the timezone used |
---|
| 529 | is UTC since C time handling functions suck. */ |
---|
| 530 | if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) |
---|
| 531 | txs->created_at = mktime_utc(&parsed); |
---|
| 532 | } else if (strcmp("sender", k) == 0 && v->type == json_object) { |
---|
| 533 | txs->user = twitter_xt_get_user(v); |
---|
| 534 | } else if (strcmp("id", k) == 0 && v->type == json_integer) { |
---|
| 535 | txs->id = v->u.integer; |
---|
| 536 | } |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | if (entities) { |
---|
| 540 | txs->text = expand_entities(txs->text, entities); |
---|
| 541 | } |
---|
| 542 | |
---|
[2cd8540] | 543 | if (txs->text && txs->user && txs->id) |
---|
| 544 | return txs; |
---|
| 545 | |
---|
| 546 | txs_free(txs); |
---|
| 547 | return NULL; |
---|
[d1356cb] | 548 | } |
---|
| 549 | |
---|
| 550 | static char* expand_entities(char* text, const json_value *entities) { |
---|
| 551 | JSON_O_FOREACH (entities, k, v) { |
---|
| 552 | int i; |
---|
| 553 | |
---|
| 554 | if (v->type != json_array) |
---|
| 555 | continue; |
---|
| 556 | if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0) |
---|
| 557 | continue; |
---|
| 558 | |
---|
| 559 | for (i = 0; i < v->u.array.length; i ++) { |
---|
| 560 | if (v->u.array.values[i]->type != json_object) |
---|
[8e3b7ac] | 561 | continue; |
---|
[d1356cb] | 562 | |
---|
| 563 | const char *kort = json_o_str(v->u.array.values[i], "url"); |
---|
| 564 | const char *disp = json_o_str(v->u.array.values[i], "display_url"); |
---|
| 565 | char *pos, *new; |
---|
| 566 | |
---|
| 567 | if (!kort || !disp || !(pos = strstr(text, kort))) |
---|
[429a9b1] | 568 | continue; |
---|
| 569 | |
---|
[d1356cb] | 570 | *pos = '\0'; |
---|
[29f72b7] | 571 | new = g_strdup_printf("%s%s <%s>%s", text, kort, |
---|
[d1356cb] | 572 | disp, pos + strlen(kort)); |
---|
| 573 | |
---|
| 574 | g_free(text); |
---|
| 575 | text = new; |
---|
[429a9b1] | 576 | } |
---|
[e193aeb] | 577 | } |
---|
[d1356cb] | 578 | |
---|
| 579 | return text; |
---|
[1b221e0] | 580 | } |
---|
| 581 | |
---|
| 582 | /** |
---|
| 583 | * Function to fill a twitter_xml_list struct. |
---|
| 584 | * It sets: |
---|
| 585 | * - all <status>es within the <status> element and |
---|
| 586 | * - the next_cursor. |
---|
| 587 | */ |
---|
[9e8c945] | 588 | static gboolean twitter_xt_get_status_list(struct im_connection *ic, const json_value *node, |
---|
| 589 | struct twitter_xml_list *txl) |
---|
[1b221e0] | 590 | { |
---|
| 591 | struct twitter_xml_status *txs; |
---|
[8e3b7ac] | 592 | int i; |
---|
[1b221e0] | 593 | |
---|
[62d2cfb] | 594 | // Set the type of the list. |
---|
| 595 | txl->type = TXL_STATUS; |
---|
[8e3b7ac] | 596 | |
---|
| 597 | if (node->type != json_array) |
---|
[9e8c945] | 598 | return FALSE; |
---|
[62d2cfb] | 599 | |
---|
[1b221e0] | 600 | // The root <statuses> node should hold the list of statuses <status> |
---|
| 601 | // Walk over the nodes children. |
---|
[8e3b7ac] | 602 | for (i = 0; i < node->u.array.length; i ++) { |
---|
[c9b5817] | 603 | txs = twitter_xt_get_status(node->u.array.values[i]); |
---|
| 604 | if (!txs) |
---|
| 605 | continue; |
---|
| 606 | |
---|
[8e3b7ac] | 607 | txl->list = g_slist_prepend(txl->list, txs); |
---|
[1b221e0] | 608 | } |
---|
| 609 | |
---|
[9e8c945] | 610 | return TRUE; |
---|
[1b221e0] | 611 | } |
---|
| 612 | |
---|
[c9b5817] | 613 | /* Will log messages either way. Need to keep track of IDs for stream deduping. |
---|
| 614 | Plus, show_ids is on by default and I don't see why anyone would disable it. */ |
---|
[ce81acd] | 615 | static char *twitter_msg_add_id(struct im_connection *ic, |
---|
[5983eca] | 616 | struct twitter_xml_status *txs, const char *prefix) |
---|
[ce81acd] | 617 | { |
---|
| 618 | struct twitter_data *td = ic->proto_data; |
---|
[c9b5817] | 619 | int reply_to = -1; |
---|
| 620 | bee_user_t *bu; |
---|
[5983eca] | 621 | |
---|
| 622 | if (txs->reply_to) { |
---|
[ce81acd] | 623 | int i; |
---|
[5983eca] | 624 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) |
---|
| 625 | if (td->log[i].id == txs->reply_to) { |
---|
[c9b5817] | 626 | reply_to = i; |
---|
[ce81acd] | 627 | break; |
---|
| 628 | } |
---|
| 629 | } |
---|
[5983eca] | 630 | |
---|
[c9b5817] | 631 | if (txs->user && txs->user->screen_name && |
---|
| 632 | (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
| 633 | struct twitter_user_data *tud = bu->data; |
---|
| 634 | |
---|
| 635 | if (txs->id > tud->last_id) { |
---|
| 636 | tud->last_id = txs->id; |
---|
| 637 | tud->last_time = txs->created_at; |
---|
| 638 | } |
---|
| 639 | } |
---|
| 640 | |
---|
| 641 | td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH; |
---|
| 642 | td->log[td->log_id].id = txs->id; |
---|
| 643 | td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name); |
---|
| 644 | |
---|
[67f6828] | 645 | /* This is all getting hairy. :-( If we RT'ed something ourselves, |
---|
| 646 | remember OUR id instead so undo will work. In other cases, the |
---|
| 647 | original tweet's id should be remembered for deduplicating. */ |
---|
[573e274] | 648 | if (strcmp(txs->user->screen_name, td->user) == 0) |
---|
[67f6828] | 649 | td->log[td->log_id].id = txs->rt_id; |
---|
| 650 | |
---|
[c9b5817] | 651 | if (set_getbool(&ic->acc->set, "show_ids")) { |
---|
| 652 | if (reply_to != -1) |
---|
[f97b8e9] | 653 | return g_strdup_printf("\002[\002%02x->%02x\002]\002 %s%s", |
---|
[c9b5817] | 654 | td->log_id, reply_to, prefix, txs->text); |
---|
| 655 | else |
---|
[f97b8e9] | 656 | return g_strdup_printf("\002[\002%02x\002]\002 %s%s", |
---|
[c9b5817] | 657 | td->log_id, prefix, txs->text); |
---|
| 658 | } else { |
---|
| 659 | if (*prefix) |
---|
| 660 | return g_strconcat(prefix, txs->text, NULL); |
---|
| 661 | else |
---|
| 662 | return NULL; |
---|
| 663 | } |
---|
[ce81acd] | 664 | } |
---|
| 665 | |
---|
[62d2cfb] | 666 | /** |
---|
| 667 | * Function that is called to see the statuses in a groupchat window. |
---|
| 668 | */ |
---|
[29f72b7] | 669 | static void twitter_status_show_chat(struct im_connection *ic, struct twitter_xml_status *status) |
---|
[62d2cfb] | 670 | { |
---|
| 671 | struct twitter_data *td = ic->proto_data; |
---|
| 672 | struct groupchat *gc; |
---|
[29f72b7] | 673 | gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0; |
---|
| 674 | char *msg; |
---|
[62d2cfb] | 675 | |
---|
| 676 | // Create a new groupchat if it does not exsist. |
---|
[631ec80] | 677 | gc = twitter_groupchat_init(ic); |
---|
[62d2cfb] | 678 | |
---|
[29f72b7] | 679 | if (!me) |
---|
| 680 | /* MUST be done before twitter_msg_add_id() to avoid #872. */ |
---|
| 681 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
| 682 | msg = twitter_msg_add_id(ic, status, ""); |
---|
| 683 | |
---|
| 684 | // Say it! |
---|
| 685 | if (me) { |
---|
| 686 | imcb_chat_log(gc, "You: %s", msg ? msg : status->text); |
---|
| 687 | } else { |
---|
| 688 | imcb_chat_msg(gc, status->user->screen_name, |
---|
| 689 | msg ? msg : status->text, 0, status->created_at); |
---|
[62d2cfb] | 690 | } |
---|
[29f72b7] | 691 | |
---|
| 692 | g_free(msg); |
---|
[62d2cfb] | 693 | } |
---|
| 694 | |
---|
| 695 | /** |
---|
| 696 | * Function that is called to see statuses as private messages. |
---|
| 697 | */ |
---|
[29f72b7] | 698 | static void twitter_status_show_msg(struct im_connection *ic, struct twitter_xml_status *status) |
---|
[62d2cfb] | 699 | { |
---|
| 700 | struct twitter_data *td = ic->proto_data; |
---|
[631ec80] | 701 | char from[MAX_STRING] = ""; |
---|
[29f72b7] | 702 | char *prefix = NULL, *text = NULL; |
---|
| 703 | gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0; |
---|
[62d2cfb] | 704 | |
---|
[631ec80] | 705 | if (td->flags & TWITTER_MODE_ONE) { |
---|
[5983eca] | 706 | g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user); |
---|
| 707 | from[MAX_STRING - 1] = '\0'; |
---|
[e88fbe27] | 708 | } |
---|
[5983eca] | 709 | |
---|
[29f72b7] | 710 | if (td->flags & TWITTER_MODE_ONE) |
---|
| 711 | prefix = g_strdup_printf("\002<\002%s\002>\002 ", |
---|
| 712 | status->user->screen_name); |
---|
| 713 | else if (!me) |
---|
| 714 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
| 715 | else |
---|
| 716 | prefix = g_strdup("You: "); |
---|
[5983eca] | 717 | |
---|
[29f72b7] | 718 | text = twitter_msg_add_id(ic, status, prefix ? prefix : ""); |
---|
[5983eca] | 719 | |
---|
[29f72b7] | 720 | imcb_buddy_msg(ic, |
---|
| 721 | *from ? from : status->user->screen_name, |
---|
| 722 | text ? text : status->text, 0, status->created_at); |
---|
[5983eca] | 723 | |
---|
[29f72b7] | 724 | g_free(text); |
---|
| 725 | g_free(prefix); |
---|
| 726 | } |
---|
[5983eca] | 727 | |
---|
[29f72b7] | 728 | static void twitter_status_show(struct im_connection *ic, struct twitter_xml_status *status) |
---|
| 729 | { |
---|
| 730 | struct twitter_data *td = ic->proto_data; |
---|
| 731 | |
---|
| 732 | if (status->user == NULL || status->text == NULL) |
---|
| 733 | return; |
---|
| 734 | |
---|
| 735 | /* Grrrr. Would like to do this during parsing, but can't access |
---|
| 736 | settings from there. */ |
---|
| 737 | if (set_getbool(&ic->acc->set, "strip_newlines")) |
---|
| 738 | strip_newlines(status->text); |
---|
| 739 | |
---|
| 740 | if (td->flags & TWITTER_MODE_CHAT) |
---|
| 741 | twitter_status_show_chat(ic, status); |
---|
| 742 | else |
---|
| 743 | twitter_status_show_msg(ic, status); |
---|
[5983eca] | 744 | |
---|
[29f72b7] | 745 | // Update the timeline_id to hold the highest id, so that by the next request |
---|
| 746 | // we won't pick up the updates already in the list. |
---|
[573e274] | 747 | td->timeline_id = MAX(td->timeline_id, status->rt_id); |
---|
[62d2cfb] | 748 | } |
---|
| 749 | |
---|
[dff0e0b] | 750 | static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o); |
---|
[ddc2de5] | 751 | |
---|
| 752 | static void twitter_http_stream(struct http_request *req) |
---|
| 753 | { |
---|
[dff0e0b] | 754 | struct im_connection *ic = req->data; |
---|
[dd672e2] | 755 | struct twitter_data *td; |
---|
[dff0e0b] | 756 | json_value *parsed; |
---|
[62f6b45] | 757 | int len = 0; |
---|
| 758 | char c, *nl; |
---|
[dff0e0b] | 759 | |
---|
| 760 | if (!g_slist_find(twitter_connections, ic)) |
---|
| 761 | return; |
---|
[ddc2de5] | 762 | |
---|
[e132b60] | 763 | ic->flags |= OPT_PONGED; |
---|
[dd672e2] | 764 | td = ic->proto_data; |
---|
| 765 | |
---|
| 766 | if ((req->flags & HTTPC_EOF) || !req->reply_body) { |
---|
| 767 | td->stream = NULL; |
---|
| 768 | imcb_error(ic, "Stream closed (%s)", req->status_string); |
---|
| 769 | imc_logout(ic, TRUE); |
---|
| 770 | return; |
---|
| 771 | } |
---|
| 772 | |
---|
[62f6b45] | 773 | /* MUST search for CRLF, not just LF: |
---|
| 774 | https://dev.twitter.com/docs/streaming-apis/processing#Parsing_responses */ |
---|
[c1d9c95] | 775 | if (!(nl = strstr(req->reply_body, "\r\n"))) |
---|
[ddc2de5] | 776 | return; |
---|
| 777 | |
---|
[62f6b45] | 778 | len = nl - req->reply_body; |
---|
| 779 | if (len > 0) { |
---|
| 780 | c = req->reply_body[len]; |
---|
| 781 | req->reply_body[len] = '\0'; |
---|
| 782 | |
---|
[14d0b02] | 783 | if ((parsed = json_parse(req->reply_body))) { |
---|
[62f6b45] | 784 | twitter_stream_handle_object(ic, parsed); |
---|
| 785 | } |
---|
| 786 | json_value_free(parsed); |
---|
| 787 | req->reply_body[len] = c; |
---|
[dff0e0b] | 788 | } |
---|
[ddc2de5] | 789 | |
---|
[62f6b45] | 790 | http_flush_bytes(req, len + 2); |
---|
[dff0e0b] | 791 | |
---|
| 792 | /* One notification might bring multiple events! */ |
---|
| 793 | if (req->body_size > 0) |
---|
| 794 | twitter_http_stream(req); |
---|
[ddc2de5] | 795 | } |
---|
[2322a9f] | 796 | |
---|
[5aa96fc8] | 797 | static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o); |
---|
[c9b5817] | 798 | static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs); |
---|
[5aa96fc8] | 799 | |
---|
[dff0e0b] | 800 | static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o) |
---|
| 801 | { |
---|
[5aa96fc8] | 802 | struct twitter_data *td = ic->proto_data; |
---|
[c9b5817] | 803 | struct twitter_xml_status *txs; |
---|
[d1356cb] | 804 | json_value *c; |
---|
[dff0e0b] | 805 | |
---|
[c9b5817] | 806 | if ((txs = twitter_xt_get_status(o))) { |
---|
[2dcde94] | 807 | gboolean ret = twitter_stream_handle_status(ic, txs); |
---|
| 808 | txs_free(txs); |
---|
| 809 | return ret; |
---|
[d1356cb] | 810 | } else if ((c = json_o_get(o, "direct_message")) && |
---|
[2cd8540] | 811 | (txs = twitter_xt_get_dm(c))) { |
---|
| 812 | if (strcmp(txs->user->screen_name, td->user) != 0) |
---|
| 813 | imcb_buddy_msg(ic, txs->user->screen_name, |
---|
| 814 | txs->text, 0, txs->created_at); |
---|
[d1356cb] | 815 | txs_free(txs); |
---|
| 816 | return TRUE; |
---|
[5aa96fc8] | 817 | } else if ((c = json_o_get(o, "event")) && c->type == json_string) { |
---|
| 818 | twitter_stream_handle_event(ic, o); |
---|
| 819 | return TRUE; |
---|
| 820 | } else if ((c = json_o_get(o, "disconnect")) && c->type == json_object) { |
---|
| 821 | /* HACK: Because we're inside an event handler, we can't just |
---|
| 822 | disconnect here. Instead, just change the HTTP status string |
---|
| 823 | into a Twitter status string. */ |
---|
| 824 | char *reason = json_o_strdup(c, "reason"); |
---|
| 825 | if (reason) { |
---|
| 826 | g_free(td->stream->status_string); |
---|
| 827 | td->stream->status_string = reason; |
---|
| 828 | } |
---|
| 829 | return TRUE; |
---|
[dff0e0b] | 830 | } |
---|
| 831 | return FALSE; |
---|
| 832 | } |
---|
| 833 | |
---|
[c9b5817] | 834 | static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs) |
---|
| 835 | { |
---|
| 836 | struct twitter_data *td = ic->proto_data; |
---|
| 837 | int i; |
---|
| 838 | |
---|
| 839 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) { |
---|
| 840 | if (td->log[i].id == txs->id) { |
---|
[29f72b7] | 841 | /* Got a duplicate (RT, probably). Drop it. */ |
---|
[c9b5817] | 842 | return TRUE; |
---|
| 843 | } |
---|
| 844 | } |
---|
| 845 | |
---|
[2dcde94] | 846 | if (!(strcmp(txs->user->screen_name, td->user) == 0 || |
---|
| 847 | set_getbool(&ic->acc->set, "fetch_mentions") || |
---|
[c9b5817] | 848 | bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
| 849 | /* Tweet is from an unknown person and the user does not want |
---|
| 850 | to see @mentions, so drop it. twitter_stream_handle_event() |
---|
| 851 | picks up new follows so this simple filter should be safe. */ |
---|
| 852 | /* TODO: The streaming API seems to do poor @mention matching. |
---|
| 853 | I.e. I'm getting mentions for @WilmerSomething, not just for |
---|
| 854 | @Wilmer. But meh. You want spam, you get spam. */ |
---|
| 855 | return TRUE; |
---|
| 856 | } |
---|
| 857 | |
---|
[29f72b7] | 858 | twitter_status_show(ic, txs); |
---|
| 859 | |
---|
[c9b5817] | 860 | return TRUE; |
---|
| 861 | } |
---|
| 862 | |
---|
[5aa96fc8] | 863 | static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o) |
---|
| 864 | { |
---|
| 865 | struct twitter_data *td = ic->proto_data; |
---|
| 866 | json_value *source = json_o_get(o, "source"); |
---|
| 867 | json_value *target = json_o_get(o, "target"); |
---|
| 868 | const char *type = json_o_str(o, "event"); |
---|
| 869 | |
---|
| 870 | if (!type || !source || source->type != json_object |
---|
| 871 | || !target || target->type != json_object) { |
---|
| 872 | return FALSE; |
---|
| 873 | } |
---|
| 874 | |
---|
| 875 | if (strcmp(type, "follow") == 0) { |
---|
| 876 | struct twitter_xml_user *us = twitter_xt_get_user(source); |
---|
| 877 | struct twitter_xml_user *ut = twitter_xt_get_user(target); |
---|
| 878 | if (strcmp(us->screen_name, td->user) == 0) { |
---|
| 879 | twitter_add_buddy(ic, ut->screen_name, ut->name); |
---|
| 880 | } |
---|
| 881 | txu_free(us); |
---|
| 882 | txu_free(ut); |
---|
| 883 | } |
---|
| 884 | |
---|
| 885 | return TRUE; |
---|
| 886 | } |
---|
| 887 | |
---|
[dff0e0b] | 888 | gboolean twitter_open_stream(struct im_connection *ic) |
---|
| 889 | { |
---|
| 890 | struct twitter_data *td = ic->proto_data; |
---|
[62f6b45] | 891 | char *args[2] = {"with", "followings"}; |
---|
[dff0e0b] | 892 | |
---|
| 893 | if ((td->stream = twitter_http(ic, TWITTER_USER_STREAM_URL, |
---|
[62f6b45] | 894 | twitter_http_stream, ic, 0, args, 2))) { |
---|
[dff0e0b] | 895 | /* This flag must be enabled or we'll get no data until EOF |
---|
| 896 | (which err, kind of, defeats the purpose of a streaming API). */ |
---|
| 897 | td->stream->flags |= HTTPC_STREAMING; |
---|
| 898 | return TRUE; |
---|
| 899 | } |
---|
| 900 | |
---|
| 901 | return FALSE; |
---|
| 902 | } |
---|
| 903 | |
---|
| 904 | static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor); |
---|
| 905 | static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor); |
---|
| 906 | |
---|
[2322a9f] | 907 | /** |
---|
| 908 | * Get the timeline with optionally mentions |
---|
| 909 | */ |
---|
| 910 | void twitter_get_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
| 911 | { |
---|
| 912 | struct twitter_data *td = ic->proto_data; |
---|
| 913 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
| 914 | |
---|
| 915 | if (td->flags & TWITTER_DOING_TIMELINE) { |
---|
[11ec078] | 916 | if (++td->http_fails >= 5) { |
---|
| 917 | imcb_error(ic, "Fetch timeout (%d)", td->flags); |
---|
| 918 | imc_logout(ic, TRUE); |
---|
| 919 | } |
---|
[2322a9f] | 920 | } |
---|
| 921 | |
---|
| 922 | td->flags |= TWITTER_DOING_TIMELINE; |
---|
| 923 | |
---|
| 924 | twitter_get_home_timeline(ic, next_cursor); |
---|
| 925 | |
---|
| 926 | if (include_mentions) { |
---|
| 927 | twitter_get_mentions(ic, next_cursor); |
---|
| 928 | } |
---|
| 929 | } |
---|
| 930 | |
---|
| 931 | /** |
---|
| 932 | * Call this one after receiving timeline/mentions. Show to user once we have |
---|
| 933 | * both. |
---|
| 934 | */ |
---|
| 935 | void twitter_flush_timeline(struct im_connection *ic) |
---|
| 936 | { |
---|
| 937 | struct twitter_data *td = ic->proto_data; |
---|
| 938 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
[b5fe39b] | 939 | int show_old_mentions = set_getint(&ic->acc->set, "show_old_mentions"); |
---|
[2322a9f] | 940 | struct twitter_xml_list *home_timeline = td->home_timeline_obj; |
---|
| 941 | struct twitter_xml_list *mentions = td->mentions_obj; |
---|
[29f72b7] | 942 | guint64 last_id = 0; |
---|
[2322a9f] | 943 | GSList *output = NULL; |
---|
| 944 | GSList *l; |
---|
| 945 | |
---|
[29f72b7] | 946 | imcb_connected(ic); |
---|
| 947 | |
---|
[2322a9f] | 948 | if (!(td->flags & TWITTER_GOT_TIMELINE)) { |
---|
| 949 | return; |
---|
| 950 | } |
---|
| 951 | |
---|
| 952 | if (include_mentions && !(td->flags & TWITTER_GOT_MENTIONS)) { |
---|
| 953 | return; |
---|
| 954 | } |
---|
| 955 | |
---|
| 956 | if (home_timeline && home_timeline->list) { |
---|
| 957 | for (l = home_timeline->list; l; l = g_slist_next(l)) { |
---|
| 958 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
| 959 | } |
---|
| 960 | } |
---|
| 961 | |
---|
| 962 | if (include_mentions && mentions && mentions->list) { |
---|
| 963 | for (l = mentions->list; l; l = g_slist_next(l)) { |
---|
[b5fe39b] | 964 | if (show_old_mentions < 1 && output && twitter_compare_elements(l->data, output->data) < 0) { |
---|
[2322a9f] | 965 | continue; |
---|
| 966 | } |
---|
| 967 | |
---|
| 968 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
| 969 | } |
---|
| 970 | } |
---|
| 971 | |
---|
| 972 | // See if the user wants to see the messages in a groupchat window or as private messages. |
---|
[29f72b7] | 973 | while (output) { |
---|
| 974 | struct twitter_xml_status *txs = output->data; |
---|
| 975 | if (txs->id != last_id) |
---|
| 976 | twitter_status_show(ic, txs); |
---|
| 977 | last_id = txs->id; |
---|
| 978 | output = g_slist_remove(output, txs); |
---|
| 979 | } |
---|
[2322a9f] | 980 | |
---|
[199fea6] | 981 | txl_free(home_timeline); |
---|
| 982 | txl_free(mentions); |
---|
[2322a9f] | 983 | |
---|
| 984 | td->flags &= ~(TWITTER_DOING_TIMELINE | TWITTER_GOT_TIMELINE | TWITTER_GOT_MENTIONS); |
---|
[199fea6] | 985 | td->home_timeline_obj = td->mentions_obj = NULL; |
---|
[2322a9f] | 986 | } |
---|
| 987 | |
---|
[ddc2de5] | 988 | static void twitter_http_get_home_timeline(struct http_request *req); |
---|
| 989 | static void twitter_http_get_mentions(struct http_request *req); |
---|
| 990 | |
---|
[2322a9f] | 991 | /** |
---|
| 992 | * Get the timeline. |
---|
| 993 | */ |
---|
[ddc2de5] | 994 | static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
[2322a9f] | 995 | { |
---|
| 996 | struct twitter_data *td = ic->proto_data; |
---|
| 997 | |
---|
[199fea6] | 998 | txl_free(td->home_timeline_obj); |
---|
[2322a9f] | 999 | td->home_timeline_obj = NULL; |
---|
| 1000 | td->flags &= ~TWITTER_GOT_TIMELINE; |
---|
| 1001 | |
---|
[429a9b1] | 1002 | char *args[6]; |
---|
[2322a9f] | 1003 | args[0] = "cursor"; |
---|
| 1004 | args[1] = g_strdup_printf("%lld", (long long) next_cursor); |
---|
[429a9b1] | 1005 | args[2] = "include_entities"; |
---|
| 1006 | args[3] = "true"; |
---|
[2322a9f] | 1007 | if (td->timeline_id) { |
---|
[429a9b1] | 1008 | args[4] = "since_id"; |
---|
| 1009 | args[5] = g_strdup_printf("%llu", (long long unsigned int) td->timeline_id); |
---|
[2322a9f] | 1010 | } |
---|
| 1011 | |
---|
[90fc864] | 1012 | if (twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args, |
---|
| 1013 | td->timeline_id ? 6 : 4) == NULL) { |
---|
| 1014 | if (++td->http_fails >= 5) |
---|
| 1015 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 1016 | TWITTER_HOME_TIMELINE_URL, "connection failed"); |
---|
| 1017 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
| 1018 | twitter_flush_timeline(ic); |
---|
| 1019 | } |
---|
[2322a9f] | 1020 | |
---|
| 1021 | g_free(args[1]); |
---|
| 1022 | if (td->timeline_id) { |
---|
[429a9b1] | 1023 | g_free(args[5]); |
---|
[2322a9f] | 1024 | } |
---|
| 1025 | } |
---|
| 1026 | |
---|
| 1027 | /** |
---|
| 1028 | * Get mentions. |
---|
| 1029 | */ |
---|
[ddc2de5] | 1030 | static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor) |
---|
[2322a9f] | 1031 | { |
---|
| 1032 | struct twitter_data *td = ic->proto_data; |
---|
| 1033 | |
---|
[199fea6] | 1034 | txl_free(td->mentions_obj); |
---|
[2322a9f] | 1035 | td->mentions_obj = NULL; |
---|
| 1036 | td->flags &= ~TWITTER_GOT_MENTIONS; |
---|
| 1037 | |
---|
[429a9b1] | 1038 | char *args[6]; |
---|
[2322a9f] | 1039 | args[0] = "cursor"; |
---|
| 1040 | args[1] = g_strdup_printf("%lld", (long long) next_cursor); |
---|
[429a9b1] | 1041 | args[2] = "include_entities"; |
---|
| 1042 | args[3] = "true"; |
---|
[2322a9f] | 1043 | if (td->timeline_id) { |
---|
[429a9b1] | 1044 | args[4] = "since_id"; |
---|
| 1045 | args[5] = g_strdup_printf("%llu", (long long unsigned int) td->timeline_id); |
---|
[b5fe39b] | 1046 | } else { |
---|
| 1047 | args[4] = "count"; |
---|
| 1048 | args[5] = g_strdup_printf("%d", set_getint(&ic->acc->set, "show_old_mentions")); |
---|
[2322a9f] | 1049 | } |
---|
| 1050 | |
---|
[b5fe39b] | 1051 | if (twitter_http(ic, TWITTER_MENTIONS_URL, twitter_http_get_mentions, |
---|
| 1052 | ic, 0, args, 6) == NULL) { |
---|
[90fc864] | 1053 | if (++td->http_fails >= 5) |
---|
| 1054 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 1055 | TWITTER_MENTIONS_URL, "connection failed"); |
---|
| 1056 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
| 1057 | twitter_flush_timeline(ic); |
---|
| 1058 | } |
---|
[2322a9f] | 1059 | |
---|
| 1060 | g_free(args[1]); |
---|
[2fb1262] | 1061 | g_free(args[5]); |
---|
[2322a9f] | 1062 | } |
---|
| 1063 | |
---|
[1b221e0] | 1064 | /** |
---|
| 1065 | * Callback for getting the home timeline. |
---|
| 1066 | */ |
---|
| 1067 | static void twitter_http_get_home_timeline(struct http_request *req) |
---|
| 1068 | { |
---|
[62d2cfb] | 1069 | struct im_connection *ic = req->data; |
---|
[37aa317] | 1070 | struct twitter_data *td; |
---|
[8e3b7ac] | 1071 | json_value *parsed; |
---|
[1b221e0] | 1072 | struct twitter_xml_list *txl; |
---|
[62d2cfb] | 1073 | |
---|
| 1074 | // Check if the connection is still active. |
---|
[5983eca] | 1075 | if (!g_slist_find(twitter_connections, ic)) |
---|
[62d2cfb] | 1076 | return; |
---|
[5983eca] | 1077 | |
---|
[37aa317] | 1078 | td = ic->proto_data; |
---|
[1b221e0] | 1079 | |
---|
[2322a9f] | 1080 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 1081 | txl->list = NULL; |
---|
| 1082 | |
---|
| 1083 | // The root <statuses> node should hold the list of statuses <status> |
---|
[2a6da96] | 1084 | if (!(parsed = twitter_parse_response(ic, req))) |
---|
| 1085 | goto end; |
---|
[d0752e8] | 1086 | twitter_xt_get_status_list(ic, parsed, txl); |
---|
[2fb1262] | 1087 | json_value_free(parsed); |
---|
[2322a9f] | 1088 | |
---|
| 1089 | td->home_timeline_obj = txl; |
---|
| 1090 | |
---|
| 1091 | end: |
---|
[fb351ce] | 1092 | if (!g_slist_find(twitter_connections, ic)) |
---|
| 1093 | return; |
---|
| 1094 | |
---|
[2322a9f] | 1095 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
| 1096 | |
---|
| 1097 | twitter_flush_timeline(ic); |
---|
| 1098 | } |
---|
| 1099 | |
---|
| 1100 | /** |
---|
| 1101 | * Callback for getting mentions. |
---|
| 1102 | */ |
---|
| 1103 | static void twitter_http_get_mentions(struct http_request *req) |
---|
| 1104 | { |
---|
| 1105 | struct im_connection *ic = req->data; |
---|
| 1106 | struct twitter_data *td; |
---|
[8e3b7ac] | 1107 | json_value *parsed; |
---|
[2322a9f] | 1108 | struct twitter_xml_list *txl; |
---|
| 1109 | |
---|
| 1110 | // Check if the connection is still active. |
---|
| 1111 | if (!g_slist_find(twitter_connections, ic)) |
---|
[1b221e0] | 1112 | return; |
---|
[2322a9f] | 1113 | |
---|
| 1114 | td = ic->proto_data; |
---|
| 1115 | |
---|
[1b221e0] | 1116 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 1117 | txl->list = NULL; |
---|
[62d2cfb] | 1118 | |
---|
[1b221e0] | 1119 | // The root <statuses> node should hold the list of statuses <status> |
---|
[2a6da96] | 1120 | if (!(parsed = twitter_parse_response(ic, req))) |
---|
| 1121 | goto end; |
---|
[d0752e8] | 1122 | twitter_xt_get_status_list(ic, parsed, txl); |
---|
[2fb1262] | 1123 | json_value_free(parsed); |
---|
[1b221e0] | 1124 | |
---|
[2322a9f] | 1125 | td->mentions_obj = txl; |
---|
[1b221e0] | 1126 | |
---|
[2322a9f] | 1127 | end: |
---|
[fb351ce] | 1128 | if (!g_slist_find(twitter_connections, ic)) |
---|
| 1129 | return; |
---|
| 1130 | |
---|
[2322a9f] | 1131 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
| 1132 | |
---|
| 1133 | twitter_flush_timeline(ic); |
---|
[1b221e0] | 1134 | } |
---|
| 1135 | |
---|
| 1136 | /** |
---|
[de923d5] | 1137 | * Callback to use after sending a POST request to twitter. |
---|
| 1138 | * (Generic, used for a few kinds of queries.) |
---|
[1b221e0] | 1139 | */ |
---|
[7d53efb] | 1140 | static void twitter_http_post(struct http_request *req) |
---|
[1b221e0] | 1141 | { |
---|
| 1142 | struct im_connection *ic = req->data; |
---|
[7b87539] | 1143 | struct twitter_data *td; |
---|
[24132ec] | 1144 | json_value *parsed, *id; |
---|
[1b221e0] | 1145 | |
---|
[62d2cfb] | 1146 | // Check if the connection is still active. |
---|
[5983eca] | 1147 | if (!g_slist_find(twitter_connections, ic)) |
---|
[62d2cfb] | 1148 | return; |
---|
| 1149 | |
---|
[7b87539] | 1150 | td = ic->proto_data; |
---|
| 1151 | td->last_status_id = 0; |
---|
[5983eca] | 1152 | |
---|
[2a6da96] | 1153 | if (!(parsed = twitter_parse_response(ic, req))) |
---|
[1b221e0] | 1154 | return; |
---|
[2a6da96] | 1155 | |
---|
[67f6828] | 1156 | if ((id = json_o_get(parsed, "id")) && id->type == json_integer) { |
---|
[24132ec] | 1157 | td->last_status_id = id->u.integer; |
---|
[67f6828] | 1158 | } |
---|
[c751e51] | 1159 | |
---|
| 1160 | json_value_free(parsed); |
---|
[b235228] | 1161 | |
---|
| 1162 | if (req->flags & TWITTER_HTTP_USER_ACK) |
---|
| 1163 | twitter_log(ic, "Command processed successfully"); |
---|
[1b221e0] | 1164 | } |
---|
| 1165 | |
---|
| 1166 | /** |
---|
| 1167 | * Function to POST a new status to twitter. |
---|
[5983eca] | 1168 | */ |
---|
[b890626] | 1169 | void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to) |
---|
[1b221e0] | 1170 | { |
---|
[5983eca] | 1171 | char *args[4] = { |
---|
[b890626] | 1172 | "status", msg, |
---|
| 1173 | "in_reply_to_status_id", |
---|
| 1174 | g_strdup_printf("%llu", (unsigned long long) in_reply_to) |
---|
| 1175 | }; |
---|
| 1176 | twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1, |
---|
[5983eca] | 1177 | args, in_reply_to ? 4 : 2); |
---|
[b890626] | 1178 | g_free(args[3]); |
---|
[1b221e0] | 1179 | } |
---|
| 1180 | |
---|
| 1181 | |
---|
[62d2cfb] | 1182 | /** |
---|
| 1183 | * Function to POST a new message to twitter. |
---|
| 1184 | */ |
---|
| 1185 | void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) |
---|
| 1186 | { |
---|
[5983eca] | 1187 | char *args[4]; |
---|
[62d2cfb] | 1188 | args[0] = "screen_name"; |
---|
| 1189 | args[1] = who; |
---|
| 1190 | args[2] = "text"; |
---|
| 1191 | args[3] = msg; |
---|
| 1192 | // Use the same callback as for twitter_post_status, since it does basically the same. |
---|
[ba3233c] | 1193 | twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4); |
---|
[62d2cfb] | 1194 | } |
---|
[7d53efb] | 1195 | |
---|
| 1196 | void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create) |
---|
| 1197 | { |
---|
[5983eca] | 1198 | char *args[2]; |
---|
[7d53efb] | 1199 | args[0] = "screen_name"; |
---|
| 1200 | args[1] = who; |
---|
[5983eca] | 1201 | twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL, |
---|
| 1202 | twitter_http_post, ic, 1, args, 2); |
---|
[a26af5c] | 1203 | } |
---|
[7b87539] | 1204 | |
---|
| 1205 | void twitter_status_destroy(struct im_connection *ic, guint64 id) |
---|
| 1206 | { |
---|
| 1207 | char *url; |
---|
[de923d5] | 1208 | url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_DESTROY_URL, |
---|
[3b4a22a] | 1209 | (unsigned long long) id, ".json"); |
---|
[b235228] | 1210 | twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0, |
---|
| 1211 | TWITTER_HTTP_USER_ACK); |
---|
[7b87539] | 1212 | g_free(url); |
---|
| 1213 | } |
---|
[b890626] | 1214 | |
---|
| 1215 | void twitter_status_retweet(struct im_connection *ic, guint64 id) |
---|
| 1216 | { |
---|
| 1217 | char *url; |
---|
[de923d5] | 1218 | url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_RETWEET_URL, |
---|
[3b4a22a] | 1219 | (unsigned long long) id, ".json"); |
---|
[b235228] | 1220 | twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0, |
---|
| 1221 | TWITTER_HTTP_USER_ACK); |
---|
[b890626] | 1222 | g_free(url); |
---|
| 1223 | } |
---|
[d18dee42] | 1224 | |
---|
| 1225 | /** |
---|
| 1226 | * Report a user for sending spam. |
---|
| 1227 | */ |
---|
| 1228 | void twitter_report_spam(struct im_connection *ic, char *screen_name) |
---|
| 1229 | { |
---|
| 1230 | char *args[2] = { |
---|
| 1231 | "screen_name", |
---|
| 1232 | NULL, |
---|
| 1233 | }; |
---|
| 1234 | args[1] = screen_name; |
---|
[b235228] | 1235 | twitter_http_f(ic, TWITTER_REPORT_SPAM_URL, twitter_http_post, |
---|
| 1236 | ic, 1, args, 2, TWITTER_HTTP_USER_ACK); |
---|
[d18dee42] | 1237 | } |
---|
[b61c74c] | 1238 | |
---|
| 1239 | /** |
---|
| 1240 | * Favourite a tweet. |
---|
| 1241 | */ |
---|
| 1242 | void twitter_favourite_tweet(struct im_connection *ic, guint64 id) |
---|
| 1243 | { |
---|
| 1244 | char *url; |
---|
| 1245 | url = g_strdup_printf("%s%llu%s", TWITTER_FAVORITE_CREATE_URL, |
---|
[3b4a22a] | 1246 | (unsigned long long) id, ".json"); |
---|
[b235228] | 1247 | twitter_http_f(ic, url, twitter_http_post, ic, 1, NULL, 0, |
---|
| 1248 | TWITTER_HTTP_USER_ACK); |
---|
[b61c74c] | 1249 | g_free(url); |
---|
| 1250 | } |
---|