[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> * |
---|
[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 | |
---|
[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 | |
---|
| 42 | #define TXL_STATUS 1 |
---|
[62d2cfb] | 43 | #define TXL_USER 2 |
---|
| 44 | #define TXL_ID 3 |
---|
| 45 | |
---|
[1b221e0] | 46 | struct twitter_xml_list { |
---|
[62d2cfb] | 47 | int type; |
---|
[8203da9] | 48 | gint64 next_cursor; |
---|
[1b221e0] | 49 | GSList *list; |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | struct twitter_xml_user { |
---|
| 53 | char *name; |
---|
| 54 | char *screen_name; |
---|
| 55 | }; |
---|
| 56 | |
---|
| 57 | struct twitter_xml_status { |
---|
[08579a1] | 58 | time_t created_at; |
---|
[1b221e0] | 59 | char *text; |
---|
| 60 | struct twitter_xml_user *user; |
---|
[67f6828] | 61 | guint64 id, rt_id; /* Usually equal, with RTs id == *original* id */ |
---|
| 62 | guint64 reply_to; |
---|
[1b221e0] | 63 | }; |
---|
| 64 | |
---|
[62d2cfb] | 65 | /** |
---|
| 66 | * Frees a twitter_xml_user struct. |
---|
| 67 | */ |
---|
| 68 | static void txu_free(struct twitter_xml_user *txu) |
---|
| 69 | { |
---|
[a26af5c] | 70 | if (txu == NULL) |
---|
| 71 | return; |
---|
[2322a9f] | 72 | |
---|
[62d2cfb] | 73 | g_free(txu->name); |
---|
| 74 | g_free(txu->screen_name); |
---|
[2abceca] | 75 | g_free(txu); |
---|
[62d2cfb] | 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * Frees a twitter_xml_status struct. |
---|
| 80 | */ |
---|
| 81 | static void txs_free(struct twitter_xml_status *txs) |
---|
| 82 | { |
---|
[2322a9f] | 83 | if (txs == NULL) |
---|
| 84 | return; |
---|
| 85 | |
---|
[62d2cfb] | 86 | g_free(txs->text); |
---|
| 87 | txu_free(txs->user); |
---|
[2abceca] | 88 | g_free(txs); |
---|
[62d2cfb] | 89 | } |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | * Free a twitter_xml_list struct. |
---|
| 93 | * type is the type of list the struct holds. |
---|
| 94 | */ |
---|
| 95 | static void txl_free(struct twitter_xml_list *txl) |
---|
| 96 | { |
---|
| 97 | GSList *l; |
---|
[a26af5c] | 98 | if (txl == NULL) |
---|
| 99 | return; |
---|
[2322a9f] | 100 | |
---|
| 101 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
| 102 | if (txl->type == TXL_STATUS) { |
---|
[5983eca] | 103 | txs_free((struct twitter_xml_status *) l->data); |
---|
[2322a9f] | 104 | } else if (txl->type == TXL_ID) { |
---|
[62d2cfb] | 105 | g_free(l->data); |
---|
[2322a9f] | 106 | } else if (txl->type == TXL_USER) { |
---|
[de923d5] | 107 | txu_free(l->data); |
---|
[2322a9f] | 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
[62d2cfb] | 111 | g_slist_free(txl->list); |
---|
[fd65edb] | 112 | g_free(txl); |
---|
[62d2cfb] | 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
[2322a9f] | 116 | * Compare status elements |
---|
| 117 | */ |
---|
| 118 | static gint twitter_compare_elements(gconstpointer a, gconstpointer b) |
---|
| 119 | { |
---|
| 120 | struct twitter_xml_status *a_status = (struct twitter_xml_status *) a; |
---|
| 121 | struct twitter_xml_status *b_status = (struct twitter_xml_status *) b; |
---|
| 122 | |
---|
| 123 | if (a_status->created_at < b_status->created_at) { |
---|
| 124 | return -1; |
---|
| 125 | } else if (a_status->created_at > b_status->created_at) { |
---|
| 126 | return 1; |
---|
| 127 | } else { |
---|
| 128 | return 0; |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * Add a buddy if it is not already added, set the status to logged in. |
---|
[62d2cfb] | 134 | */ |
---|
[3e69802] | 135 | static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname) |
---|
[62d2cfb] | 136 | { |
---|
[1014cab] | 137 | struct twitter_data *td = ic->proto_data; |
---|
| 138 | |
---|
[de923d5] | 139 | // Check if the buddy is already in the buddy list. |
---|
[5983eca] | 140 | if (!bee_user_by_handle(ic->bee, ic, name)) { |
---|
[62d2cfb] | 141 | // The buddy is not in the list, add the buddy and set the status to logged in. |
---|
[5983eca] | 142 | imcb_add_buddy(ic, name, NULL); |
---|
| 143 | imcb_rename_buddy(ic, name, fullname); |
---|
[631ec80] | 144 | if (td->flags & TWITTER_MODE_CHAT) { |
---|
[5c18a76] | 145 | /* Necessary so that nicks always get translated to the |
---|
| 146 | exact Twitter username. */ |
---|
[5983eca] | 147 | imcb_buddy_nick_hint(ic, name, name); |
---|
[7f557d5] | 148 | if (td->timeline_gc) |
---|
| 149 | imcb_chat_add_buddy(td->timeline_gc, name); |
---|
[631ec80] | 150 | } else if (td->flags & TWITTER_MODE_MANY) |
---|
[5983eca] | 151 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
[62d2cfb] | 152 | } |
---|
| 153 | } |
---|
[1b221e0] | 154 | |
---|
[a7b9ec7] | 155 | /* Warning: May return a malloc()ed value, which will be free()d on the next |
---|
[de923d5] | 156 | call. Only for short-term use. NOT THREADSAFE! */ |
---|
[6eca2eb] | 157 | char *twitter_parse_error(struct http_request *req) |
---|
[a7b9ec7] | 158 | { |
---|
| 159 | static char *ret = NULL; |
---|
[5246133] | 160 | json_value *root, *err; |
---|
[5983eca] | 161 | |
---|
[a7b9ec7] | 162 | g_free(ret); |
---|
| 163 | ret = NULL; |
---|
[5983eca] | 164 | |
---|
| 165 | if (req->body_size > 0) { |
---|
[7a80925] | 166 | root = json_parse(req->reply_body, req->body_size); |
---|
[5246133] | 167 | err = json_o_get(root, "errors"); |
---|
[dff0e0b] | 168 | if (err && err->type == json_array && (err = err->u.array.values[0]) && |
---|
[5246133] | 169 | err->type == json_object) { |
---|
| 170 | const char *msg = json_o_str(err, "message"); |
---|
| 171 | if (msg) |
---|
| 172 | ret = g_strdup_printf("%s (%s)", req->status_string, msg); |
---|
| 173 | } |
---|
| 174 | json_value_free(root); |
---|
[a7b9ec7] | 175 | } |
---|
[5983eca] | 176 | |
---|
[6eca2eb] | 177 | return ret ? ret : req->status_string; |
---|
[a7b9ec7] | 178 | } |
---|
| 179 | |
---|
[fb351ce] | 180 | /* WATCH OUT: This function might or might not destroy your connection. |
---|
| 181 | Sub-optimal indeed, but just be careful when this returns NULL! */ |
---|
[e08ae0c] | 182 | static json_value *twitter_parse_response(struct im_connection *ic, struct http_request *req) |
---|
[2a6da96] | 183 | { |
---|
| 184 | gboolean logging_in = !(ic->flags & OPT_LOGGED_IN); |
---|
| 185 | gboolean periodic; |
---|
| 186 | struct twitter_data *td = ic->proto_data; |
---|
[e08ae0c] | 187 | json_value *ret; |
---|
[2a6da96] | 188 | char path[64] = "", *s; |
---|
| 189 | |
---|
| 190 | if ((s = strchr(req->request, ' '))) { |
---|
| 191 | path[sizeof(path)-1] = '\0'; |
---|
| 192 | strncpy(path, s + 1, sizeof(path) - 1); |
---|
| 193 | if ((s = strchr(path, '?')) || (s = strchr(path, ' '))) |
---|
| 194 | *s = '\0'; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /* Kinda nasty. :-( Trying to suppress error messages, but only |
---|
| 198 | for periodic (i.e. mentions/timeline) queries. */ |
---|
| 199 | periodic = strstr(path, "timeline") || strstr(path, "mentions"); |
---|
| 200 | |
---|
| 201 | if (req->status_code == 401 && logging_in) { |
---|
| 202 | /* IIRC Twitter once had an outage where they were randomly |
---|
| 203 | throwing 401s so I'll keep treating this one as fatal |
---|
| 204 | only during login. */ |
---|
[5246133] | 205 | imcb_error(ic, "Authentication failure (%s)", |
---|
| 206 | twitter_parse_error(req)); |
---|
[2a6da96] | 207 | imc_logout(ic, FALSE); |
---|
| 208 | return NULL; |
---|
| 209 | } else if (req->status_code != 200) { |
---|
| 210 | // It didn't go well, output the error and return. |
---|
| 211 | if (!periodic || logging_in || ++td->http_fails >= 5) |
---|
[96dd574] | 212 | twitter_log(ic, "Error: Could not retrieve %s: %s", |
---|
| 213 | path, twitter_parse_error(req)); |
---|
[2a6da96] | 214 | |
---|
| 215 | if (logging_in) |
---|
| 216 | imc_logout(ic, TRUE); |
---|
| 217 | return NULL; |
---|
| 218 | } else { |
---|
| 219 | td->http_fails = 0; |
---|
| 220 | } |
---|
| 221 | |
---|
[7a80925] | 222 | if ((ret = json_parse(req->reply_body, req->body_size)) == NULL) { |
---|
[2a6da96] | 223 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 224 | path, "XML parse error"); |
---|
| 225 | } |
---|
| 226 | return ret; |
---|
| 227 | } |
---|
| 228 | |
---|
[1b221e0] | 229 | static void twitter_http_get_friends_ids(struct http_request *req); |
---|
| 230 | |
---|
| 231 | /** |
---|
| 232 | * Get the friends ids. |
---|
| 233 | */ |
---|
[8203da9] | 234 | void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor) |
---|
[1b221e0] | 235 | { |
---|
[5983eca] | 236 | // Primitive, but hey! It works... |
---|
| 237 | char *args[2]; |
---|
[1b221e0] | 238 | args[0] = "cursor"; |
---|
[5983eca] | 239 | args[1] = g_strdup_printf("%lld", (long long) next_cursor); |
---|
[bb5ce4d1] | 240 | twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2); |
---|
[1b221e0] | 241 | |
---|
| 242 | g_free(args[1]); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | /** |
---|
| 246 | * Fill a list of ids. |
---|
| 247 | */ |
---|
[9e8c945] | 248 | static gboolean twitter_xt_get_friends_id_list(json_value *node, struct twitter_xml_list *txl) |
---|
[1b221e0] | 249 | { |
---|
[e08ae0c] | 250 | json_value *c; |
---|
| 251 | int i; |
---|
[5983eca] | 252 | |
---|
[62d2cfb] | 253 | // Set the list type. |
---|
| 254 | txl->type = TXL_ID; |
---|
[1b221e0] | 255 | |
---|
[e08ae0c] | 256 | c = json_o_get(node, "ids"); |
---|
| 257 | if (!c || c->type != json_array) |
---|
[9e8c945] | 258 | return FALSE; |
---|
[1b221e0] | 259 | |
---|
[e08ae0c] | 260 | for (i = 0; i < c->u.array.length; i ++) { |
---|
[0688e99] | 261 | if (c->u.array.values[i]->type != json_integer) |
---|
| 262 | continue; |
---|
| 263 | |
---|
[e08ae0c] | 264 | txl->list = g_slist_prepend(txl->list, |
---|
[7a80925] | 265 | g_strdup_printf("%" PRIu64, c->u.array.values[i]->u.integer)); |
---|
[e08ae0c] | 266 | } |
---|
| 267 | |
---|
| 268 | c = json_o_get(node, "next_cursor"); |
---|
| 269 | if (c && c->type == json_integer) |
---|
| 270 | txl->next_cursor = c->u.integer; |
---|
| 271 | else |
---|
| 272 | txl->next_cursor = -1; |
---|
| 273 | |
---|
[9e8c945] | 274 | return TRUE; |
---|
[1b221e0] | 275 | } |
---|
| 276 | |
---|
[de923d5] | 277 | static void twitter_get_users_lookup(struct im_connection *ic); |
---|
| 278 | |
---|
[1b221e0] | 279 | /** |
---|
| 280 | * Callback for getting the friends ids. |
---|
| 281 | */ |
---|
| 282 | static void twitter_http_get_friends_ids(struct http_request *req) |
---|
| 283 | { |
---|
| 284 | struct im_connection *ic; |
---|
[e08ae0c] | 285 | json_value *parsed; |
---|
[1b221e0] | 286 | struct twitter_xml_list *txl; |
---|
[3bd4a93] | 287 | struct twitter_data *td; |
---|
[1b221e0] | 288 | |
---|
| 289 | ic = req->data; |
---|
| 290 | |
---|
[62d2cfb] | 291 | // Check if the connection is still active. |
---|
[5983eca] | 292 | if (!g_slist_find(twitter_connections, ic)) |
---|
[62d2cfb] | 293 | return; |
---|
[5983eca] | 294 | |
---|
[37aa317] | 295 | td = ic->proto_data; |
---|
[62d2cfb] | 296 | |
---|
[1b221e0] | 297 | txl = g_new0(struct twitter_xml_list, 1); |
---|
[de923d5] | 298 | txl->list = td->follow_ids; |
---|
[1b221e0] | 299 | |
---|
| 300 | // Parse the data. |
---|
[2a6da96] | 301 | if (!(parsed = twitter_parse_response(ic, req))) |
---|
| 302 | return; |
---|
[e08ae0c] | 303 | |
---|
[d0752e8] | 304 | twitter_xt_get_friends_id_list(parsed, txl); |
---|
[e08ae0c] | 305 | json_value_free(parsed); |
---|
[1b221e0] | 306 | |
---|
[de923d5] | 307 | td->follow_ids = txl->list; |
---|
[1b221e0] | 308 | if (txl->next_cursor) |
---|
[de923d5] | 309 | /* These were just numbers. Up to 4000 in a response AFAIK so if we get here |
---|
| 310 | we may be using a spammer account. \o/ */ |
---|
[1b221e0] | 311 | twitter_get_friends_ids(ic, txl->next_cursor); |
---|
[de923d5] | 312 | else |
---|
| 313 | /* Now to convert all those numbers into names.. */ |
---|
| 314 | twitter_get_users_lookup(ic); |
---|
| 315 | |
---|
| 316 | txl->list = NULL; |
---|
| 317 | txl_free(txl); |
---|
| 318 | } |
---|
| 319 | |
---|
[9e8c945] | 320 | static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl); |
---|
[de923d5] | 321 | static void twitter_http_get_users_lookup(struct http_request *req); |
---|
| 322 | |
---|
| 323 | static void twitter_get_users_lookup(struct im_connection *ic) |
---|
| 324 | { |
---|
| 325 | struct twitter_data *td = ic->proto_data; |
---|
| 326 | char *args[2] = { |
---|
| 327 | "user_id", |
---|
| 328 | NULL, |
---|
| 329 | }; |
---|
| 330 | GString *ids = g_string_new(""); |
---|
| 331 | int i; |
---|
| 332 | |
---|
| 333 | /* We can request up to 100 users at a time. */ |
---|
| 334 | for (i = 0; i < 100 && td->follow_ids; i ++) { |
---|
| 335 | g_string_append_printf(ids, ",%s", (char*) td->follow_ids->data); |
---|
| 336 | g_free(td->follow_ids->data); |
---|
| 337 | td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data); |
---|
| 338 | } |
---|
| 339 | if (ids->len > 0) { |
---|
| 340 | args[1] = ids->str + 1; |
---|
| 341 | /* POST, because I think ids can be up to 1KB long. */ |
---|
| 342 | twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2); |
---|
| 343 | } else { |
---|
| 344 | /* We have all users. Continue with login. (Get statuses.) */ |
---|
| 345 | td->flags |= TWITTER_HAVE_FRIENDS; |
---|
| 346 | twitter_login_finish(ic); |
---|
| 347 | } |
---|
| 348 | g_string_free(ids, TRUE); |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | /** |
---|
| 352 | * Callback for getting (twitter)friends... |
---|
| 353 | * |
---|
| 354 | * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has |
---|
| 355 | * hundreds of friends?" you wonder? You probably not, since you are reading the source of |
---|
| 356 | * BitlBee... Get a life and meet new people! |
---|
| 357 | */ |
---|
| 358 | static void twitter_http_get_users_lookup(struct http_request *req) |
---|
| 359 | { |
---|
| 360 | struct im_connection *ic = req->data; |
---|
[e08ae0c] | 361 | json_value *parsed; |
---|
[de923d5] | 362 | struct twitter_xml_list *txl; |
---|
| 363 | GSList *l = NULL; |
---|
| 364 | struct twitter_xml_user *user; |
---|
| 365 | |
---|
| 366 | // Check if the connection is still active. |
---|
| 367 | if (!g_slist_find(twitter_connections, ic)) |
---|
| 368 | return; |
---|
| 369 | |
---|
| 370 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 371 | txl->list = NULL; |
---|
[1b221e0] | 372 | |
---|
[de923d5] | 373 | // Get the user list from the parsed xml feed. |
---|
[2a6da96] | 374 | if (!(parsed = twitter_parse_response(ic, req))) |
---|
| 375 | return; |
---|
[d0752e8] | 376 | twitter_xt_get_users(parsed, txl); |
---|
[e08ae0c] | 377 | json_value_free(parsed); |
---|
[de923d5] | 378 | |
---|
| 379 | // Add the users as buddies. |
---|
| 380 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
| 381 | user = l->data; |
---|
| 382 | twitter_add_buddy(ic, user->screen_name, user->name); |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | // Free the structure. |
---|
[62d2cfb] | 386 | txl_free(txl); |
---|
[de923d5] | 387 | |
---|
| 388 | twitter_get_users_lookup(ic); |
---|
[1b221e0] | 389 | } |
---|
| 390 | |
---|
[8e3b7ac] | 391 | struct twitter_xml_user *twitter_xt_get_user(const json_value *node) |
---|
| 392 | { |
---|
| 393 | struct twitter_xml_user *txu; |
---|
| 394 | |
---|
| 395 | txu = g_new0(struct twitter_xml_user, 1); |
---|
| 396 | txu->name = g_strdup(json_o_str(node, "name")); |
---|
| 397 | txu->screen_name = g_strdup(json_o_str(node, "screen_name")); |
---|
| 398 | |
---|
| 399 | return txu; |
---|
| 400 | } |
---|
| 401 | |
---|
[62d2cfb] | 402 | /** |
---|
| 403 | * Function to fill a twitter_xml_list struct. |
---|
| 404 | * It sets: |
---|
| 405 | * - all <user>s from the <users> element. |
---|
| 406 | */ |
---|
[9e8c945] | 407 | static gboolean twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl) |
---|
[62d2cfb] | 408 | { |
---|
| 409 | struct twitter_xml_user *txu; |
---|
[e08ae0c] | 410 | int i; |
---|
[62d2cfb] | 411 | |
---|
| 412 | // Set the type of the list. |
---|
| 413 | txl->type = TXL_USER; |
---|
| 414 | |
---|
[e08ae0c] | 415 | if (!node || node->type != json_array) |
---|
[9e8c945] | 416 | return FALSE; |
---|
[e08ae0c] | 417 | |
---|
[62d2cfb] | 418 | // The root <users> node should hold the list of users <user> |
---|
| 419 | // Walk over the nodes children. |
---|
[e08ae0c] | 420 | for (i = 0; i < node->u.array.length; i ++) { |
---|
[8e3b7ac] | 421 | txu = twitter_xt_get_user(node->u.array.values[i]); |
---|
| 422 | if (txu) |
---|
| 423 | txl->list = g_slist_prepend(txl->list, txu); |
---|
[62d2cfb] | 424 | } |
---|
| 425 | |
---|
[9e8c945] | 426 | return TRUE; |
---|
[62d2cfb] | 427 | } |
---|
| 428 | |
---|
[2b02617] | 429 | #ifdef __GLIBC__ |
---|
| 430 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y" |
---|
| 431 | #else |
---|
| 432 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y" |
---|
| 433 | #endif |
---|
[62d2cfb] | 434 | |
---|
[d1356cb] | 435 | static char* expand_entities(char* text, const json_value *entities); |
---|
| 436 | |
---|
[1b221e0] | 437 | /** |
---|
| 438 | * Function to fill a twitter_xml_status struct. |
---|
| 439 | * It sets: |
---|
| 440 | * - the status text and |
---|
| 441 | * - the created_at timestamp and |
---|
| 442 | * - the status id and |
---|
| 443 | * - the user in a twitter_xml_user struct. |
---|
| 444 | */ |
---|
[c9b5817] | 445 | static struct twitter_xml_status *twitter_xt_get_status(const json_value *node) |
---|
[1b221e0] | 446 | { |
---|
[c9b5817] | 447 | struct twitter_xml_status *txs; |
---|
[fb351ce] | 448 | const json_value *rt = NULL, *entities = NULL; |
---|
[8e3b7ac] | 449 | |
---|
| 450 | if (node->type != json_object) |
---|
[9e8c945] | 451 | return FALSE; |
---|
[c9b5817] | 452 | txs = g_new0(struct twitter_xml_status, 1); |
---|
[1b221e0] | 453 | |
---|
[9e8c945] | 454 | JSON_O_FOREACH (node, k, v) { |
---|
[8e3b7ac] | 455 | if (strcmp("text", k) == 0 && v->type == json_string) { |
---|
[fb351ce] | 456 | txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1); |
---|
[29f72b7] | 457 | strip_html(txs->text); |
---|
[8e3b7ac] | 458 | } else if (strcmp("retweeted_status", k) == 0 && v->type == json_object) { |
---|
| 459 | rt = v; |
---|
| 460 | } else if (strcmp("created_at", k) == 0 && v->type == json_string) { |
---|
[08579a1] | 461 | struct tm parsed; |
---|
[5983eca] | 462 | |
---|
[08579a1] | 463 | /* Very sensitive to changes to the formatting of |
---|
| 464 | this field. :-( Also assumes the timezone used |
---|
| 465 | is UTC since C time handling functions suck. */ |
---|
[8e3b7ac] | 466 | if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) |
---|
[5983eca] | 467 | txs->created_at = mktime_utc(&parsed); |
---|
[8e3b7ac] | 468 | } else if (strcmp("user", k) == 0 && v->type == json_object) { |
---|
| 469 | txs->user = twitter_xt_get_user(v); |
---|
| 470 | } else if (strcmp("id", k) == 0 && v->type == json_integer) { |
---|
[573e274] | 471 | txs->rt_id = txs->id = v->u.integer; |
---|
[8e3b7ac] | 472 | } else if (strcmp("in_reply_to_status_id", k) == 0 && v->type == json_integer) { |
---|
| 473 | txs->reply_to = v->u.integer; |
---|
| 474 | } else if (strcmp("entities", k) == 0 && v->type == json_object) { |
---|
[fb351ce] | 475 | entities = v; |
---|
[ce81acd] | 476 | } |
---|
[1b221e0] | 477 | } |
---|
[5983eca] | 478 | |
---|
[0fff0b2] | 479 | /* If it's a (truncated) retweet, get the original. Even if the API claims it |
---|
| 480 | wasn't truncated because it may be lying. */ |
---|
| 481 | if (rt) { |
---|
[c9b5817] | 482 | struct twitter_xml_status *rtxs = twitter_xt_get_status(rt); |
---|
| 483 | if (rtxs) { |
---|
| 484 | g_free(txs->text); |
---|
| 485 | txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text); |
---|
| 486 | txs->id = rtxs->id; |
---|
[e193aeb] | 487 | txs_free(rtxs); |
---|
| 488 | } |
---|
[fb351ce] | 489 | } else if (entities) { |
---|
[d1356cb] | 490 | txs->text = expand_entities(txs->text, entities); |
---|
| 491 | } |
---|
| 492 | |
---|
[c9b5817] | 493 | if (txs->text && txs->user && txs->id) |
---|
| 494 | return txs; |
---|
| 495 | |
---|
| 496 | txs_free(txs); |
---|
| 497 | return NULL; |
---|
[d1356cb] | 498 | } |
---|
| 499 | |
---|
| 500 | /** |
---|
| 501 | * Function to fill a twitter_xml_status struct (DM variant). |
---|
| 502 | */ |
---|
[2cd8540] | 503 | static struct twitter_xml_status *twitter_xt_get_dm(const json_value *node) |
---|
[d1356cb] | 504 | { |
---|
[2cd8540] | 505 | struct twitter_xml_status *txs; |
---|
[d1356cb] | 506 | const json_value *entities = NULL; |
---|
| 507 | |
---|
| 508 | if (node->type != json_object) |
---|
| 509 | return FALSE; |
---|
[2cd8540] | 510 | txs = g_new0(struct twitter_xml_status, 1); |
---|
[d1356cb] | 511 | |
---|
| 512 | JSON_O_FOREACH (node, k, v) { |
---|
| 513 | if (strcmp("text", k) == 0 && v->type == json_string) { |
---|
| 514 | txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1); |
---|
[29f72b7] | 515 | strip_html(txs->text); |
---|
[d1356cb] | 516 | } else if (strcmp("created_at", k) == 0 && v->type == json_string) { |
---|
| 517 | struct tm parsed; |
---|
| 518 | |
---|
| 519 | /* Very sensitive to changes to the formatting of |
---|
| 520 | this field. :-( Also assumes the timezone used |
---|
| 521 | is UTC since C time handling functions suck. */ |
---|
| 522 | if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL) |
---|
| 523 | txs->created_at = mktime_utc(&parsed); |
---|
| 524 | } else if (strcmp("sender", k) == 0 && v->type == json_object) { |
---|
| 525 | txs->user = twitter_xt_get_user(v); |
---|
| 526 | } else if (strcmp("id", k) == 0 && v->type == json_integer) { |
---|
| 527 | txs->id = v->u.integer; |
---|
| 528 | } |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | if (entities) { |
---|
| 532 | txs->text = expand_entities(txs->text, entities); |
---|
| 533 | } |
---|
| 534 | |
---|
[2cd8540] | 535 | if (txs->text && txs->user && txs->id) |
---|
| 536 | return txs; |
---|
| 537 | |
---|
| 538 | txs_free(txs); |
---|
| 539 | return NULL; |
---|
[d1356cb] | 540 | } |
---|
| 541 | |
---|
| 542 | static char* expand_entities(char* text, const json_value *entities) { |
---|
| 543 | JSON_O_FOREACH (entities, k, v) { |
---|
| 544 | int i; |
---|
| 545 | |
---|
| 546 | if (v->type != json_array) |
---|
| 547 | continue; |
---|
| 548 | if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0) |
---|
| 549 | continue; |
---|
| 550 | |
---|
| 551 | for (i = 0; i < v->u.array.length; i ++) { |
---|
| 552 | if (v->u.array.values[i]->type != json_object) |
---|
[8e3b7ac] | 553 | continue; |
---|
[d1356cb] | 554 | |
---|
| 555 | const char *kort = json_o_str(v->u.array.values[i], "url"); |
---|
| 556 | const char *disp = json_o_str(v->u.array.values[i], "display_url"); |
---|
| 557 | char *pos, *new; |
---|
| 558 | |
---|
| 559 | if (!kort || !disp || !(pos = strstr(text, kort))) |
---|
[429a9b1] | 560 | continue; |
---|
| 561 | |
---|
[d1356cb] | 562 | *pos = '\0'; |
---|
[29f72b7] | 563 | new = g_strdup_printf("%s%s <%s>%s", text, kort, |
---|
[d1356cb] | 564 | disp, pos + strlen(kort)); |
---|
| 565 | |
---|
| 566 | g_free(text); |
---|
| 567 | text = new; |
---|
[429a9b1] | 568 | } |
---|
[e193aeb] | 569 | } |
---|
[d1356cb] | 570 | |
---|
| 571 | return text; |
---|
[1b221e0] | 572 | } |
---|
| 573 | |
---|
| 574 | /** |
---|
| 575 | * Function to fill a twitter_xml_list struct. |
---|
| 576 | * It sets: |
---|
| 577 | * - all <status>es within the <status> element and |
---|
| 578 | * - the next_cursor. |
---|
| 579 | */ |
---|
[9e8c945] | 580 | static gboolean twitter_xt_get_status_list(struct im_connection *ic, const json_value *node, |
---|
| 581 | struct twitter_xml_list *txl) |
---|
[1b221e0] | 582 | { |
---|
| 583 | struct twitter_xml_status *txs; |
---|
[8e3b7ac] | 584 | int i; |
---|
[1b221e0] | 585 | |
---|
[62d2cfb] | 586 | // Set the type of the list. |
---|
| 587 | txl->type = TXL_STATUS; |
---|
[8e3b7ac] | 588 | |
---|
| 589 | if (node->type != json_array) |
---|
[9e8c945] | 590 | return FALSE; |
---|
[62d2cfb] | 591 | |
---|
[1b221e0] | 592 | // The root <statuses> node should hold the list of statuses <status> |
---|
| 593 | // Walk over the nodes children. |
---|
[8e3b7ac] | 594 | for (i = 0; i < node->u.array.length; i ++) { |
---|
[c9b5817] | 595 | txs = twitter_xt_get_status(node->u.array.values[i]); |
---|
| 596 | if (!txs) |
---|
| 597 | continue; |
---|
| 598 | |
---|
[8e3b7ac] | 599 | txl->list = g_slist_prepend(txl->list, txs); |
---|
[1b221e0] | 600 | } |
---|
| 601 | |
---|
[9e8c945] | 602 | return TRUE; |
---|
[1b221e0] | 603 | } |
---|
| 604 | |
---|
[c9b5817] | 605 | /* Will log messages either way. Need to keep track of IDs for stream deduping. |
---|
| 606 | Plus, show_ids is on by default and I don't see why anyone would disable it. */ |
---|
[ce81acd] | 607 | static char *twitter_msg_add_id(struct im_connection *ic, |
---|
[5983eca] | 608 | struct twitter_xml_status *txs, const char *prefix) |
---|
[ce81acd] | 609 | { |
---|
| 610 | struct twitter_data *td = ic->proto_data; |
---|
[c9b5817] | 611 | int reply_to = -1; |
---|
| 612 | bee_user_t *bu; |
---|
[5983eca] | 613 | |
---|
| 614 | if (txs->reply_to) { |
---|
[ce81acd] | 615 | int i; |
---|
[5983eca] | 616 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) |
---|
| 617 | if (td->log[i].id == txs->reply_to) { |
---|
[c9b5817] | 618 | reply_to = i; |
---|
[ce81acd] | 619 | break; |
---|
| 620 | } |
---|
| 621 | } |
---|
[5983eca] | 622 | |
---|
[c9b5817] | 623 | if (txs->user && txs->user->screen_name && |
---|
| 624 | (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
| 625 | struct twitter_user_data *tud = bu->data; |
---|
| 626 | |
---|
| 627 | if (txs->id > tud->last_id) { |
---|
| 628 | tud->last_id = txs->id; |
---|
| 629 | tud->last_time = txs->created_at; |
---|
| 630 | } |
---|
| 631 | } |
---|
| 632 | |
---|
| 633 | td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH; |
---|
| 634 | td->log[td->log_id].id = txs->id; |
---|
| 635 | td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name); |
---|
| 636 | |
---|
[67f6828] | 637 | /* This is all getting hairy. :-( If we RT'ed something ourselves, |
---|
| 638 | remember OUR id instead so undo will work. In other cases, the |
---|
| 639 | original tweet's id should be remembered for deduplicating. */ |
---|
[9f8bb17] | 640 | if (g_strcasecmp(txs->user->screen_name, td->user) == 0) |
---|
[67f6828] | 641 | td->log[td->log_id].id = txs->rt_id; |
---|
| 642 | |
---|
[c9b5817] | 643 | if (set_getbool(&ic->acc->set, "show_ids")) { |
---|
| 644 | if (reply_to != -1) |
---|
[f97b8e9] | 645 | return g_strdup_printf("\002[\002%02x->%02x\002]\002 %s%s", |
---|
[c9b5817] | 646 | td->log_id, reply_to, prefix, txs->text); |
---|
| 647 | else |
---|
[f97b8e9] | 648 | return g_strdup_printf("\002[\002%02x\002]\002 %s%s", |
---|
[c9b5817] | 649 | td->log_id, prefix, txs->text); |
---|
| 650 | } else { |
---|
| 651 | if (*prefix) |
---|
| 652 | return g_strconcat(prefix, txs->text, NULL); |
---|
| 653 | else |
---|
| 654 | return NULL; |
---|
| 655 | } |
---|
[ce81acd] | 656 | } |
---|
| 657 | |
---|
[62d2cfb] | 658 | /** |
---|
| 659 | * Function that is called to see the statuses in a groupchat window. |
---|
| 660 | */ |
---|
[29f72b7] | 661 | static void twitter_status_show_chat(struct im_connection *ic, struct twitter_xml_status *status) |
---|
[62d2cfb] | 662 | { |
---|
| 663 | struct twitter_data *td = ic->proto_data; |
---|
| 664 | struct groupchat *gc; |
---|
[29f72b7] | 665 | gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0; |
---|
| 666 | char *msg; |
---|
[62d2cfb] | 667 | |
---|
| 668 | // Create a new groupchat if it does not exsist. |
---|
[631ec80] | 669 | gc = twitter_groupchat_init(ic); |
---|
[62d2cfb] | 670 | |
---|
[29f72b7] | 671 | if (!me) |
---|
| 672 | /* MUST be done before twitter_msg_add_id() to avoid #872. */ |
---|
| 673 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
| 674 | msg = twitter_msg_add_id(ic, status, ""); |
---|
| 675 | |
---|
| 676 | // Say it! |
---|
| 677 | if (me) { |
---|
| 678 | imcb_chat_log(gc, "You: %s", msg ? msg : status->text); |
---|
| 679 | } else { |
---|
| 680 | imcb_chat_msg(gc, status->user->screen_name, |
---|
| 681 | msg ? msg : status->text, 0, status->created_at); |
---|
[62d2cfb] | 682 | } |
---|
[29f72b7] | 683 | |
---|
| 684 | g_free(msg); |
---|
[62d2cfb] | 685 | } |
---|
| 686 | |
---|
| 687 | /** |
---|
| 688 | * Function that is called to see statuses as private messages. |
---|
| 689 | */ |
---|
[29f72b7] | 690 | static void twitter_status_show_msg(struct im_connection *ic, struct twitter_xml_status *status) |
---|
[62d2cfb] | 691 | { |
---|
| 692 | struct twitter_data *td = ic->proto_data; |
---|
[631ec80] | 693 | char from[MAX_STRING] = ""; |
---|
[29f72b7] | 694 | char *prefix = NULL, *text = NULL; |
---|
| 695 | gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0; |
---|
[62d2cfb] | 696 | |
---|
[631ec80] | 697 | if (td->flags & TWITTER_MODE_ONE) { |
---|
[5983eca] | 698 | g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user); |
---|
| 699 | from[MAX_STRING - 1] = '\0'; |
---|
[e88fbe27] | 700 | } |
---|
[5983eca] | 701 | |
---|
[29f72b7] | 702 | if (td->flags & TWITTER_MODE_ONE) |
---|
| 703 | prefix = g_strdup_printf("\002<\002%s\002>\002 ", |
---|
| 704 | status->user->screen_name); |
---|
| 705 | else if (!me) |
---|
| 706 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
| 707 | else |
---|
| 708 | prefix = g_strdup("You: "); |
---|
[5983eca] | 709 | |
---|
[29f72b7] | 710 | text = twitter_msg_add_id(ic, status, prefix ? prefix : ""); |
---|
[5983eca] | 711 | |
---|
[29f72b7] | 712 | imcb_buddy_msg(ic, |
---|
| 713 | *from ? from : status->user->screen_name, |
---|
| 714 | text ? text : status->text, 0, status->created_at); |
---|
[5983eca] | 715 | |
---|
[29f72b7] | 716 | g_free(text); |
---|
| 717 | g_free(prefix); |
---|
| 718 | } |
---|
[5983eca] | 719 | |
---|
[29f72b7] | 720 | static void twitter_status_show(struct im_connection *ic, struct twitter_xml_status *status) |
---|
| 721 | { |
---|
| 722 | struct twitter_data *td = ic->proto_data; |
---|
[7549d00] | 723 | char *last_id_str; |
---|
[29f72b7] | 724 | |
---|
| 725 | if (status->user == NULL || status->text == NULL) |
---|
| 726 | return; |
---|
| 727 | |
---|
| 728 | /* Grrrr. Would like to do this during parsing, but can't access |
---|
| 729 | settings from there. */ |
---|
| 730 | if (set_getbool(&ic->acc->set, "strip_newlines")) |
---|
| 731 | strip_newlines(status->text); |
---|
| 732 | |
---|
| 733 | if (td->flags & TWITTER_MODE_CHAT) |
---|
| 734 | twitter_status_show_chat(ic, status); |
---|
| 735 | else |
---|
| 736 | twitter_status_show_msg(ic, status); |
---|
[5983eca] | 737 | |
---|
[29f72b7] | 738 | // Update the timeline_id to hold the highest id, so that by the next request |
---|
| 739 | // we won't pick up the updates already in the list. |
---|
[573e274] | 740 | td->timeline_id = MAX(td->timeline_id, status->rt_id); |
---|
[7549d00] | 741 | |
---|
| 742 | last_id_str = g_strdup_printf("%" G_GUINT64_FORMAT, td->timeline_id); |
---|
| 743 | set_setstr(&ic->acc->set, "last_tweet", last_id_str); |
---|
| 744 | g_free(last_id_str); |
---|
[62d2cfb] | 745 | } |
---|
| 746 | |
---|
[dff0e0b] | 747 | static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o); |
---|
[ddc2de5] | 748 | |
---|
| 749 | static void twitter_http_stream(struct http_request *req) |
---|
| 750 | { |
---|
[dff0e0b] | 751 | struct im_connection *ic = req->data; |
---|
[dd672e2] | 752 | struct twitter_data *td; |
---|
[dff0e0b] | 753 | json_value *parsed; |
---|
[62f6b45] | 754 | int len = 0; |
---|
| 755 | char c, *nl; |
---|
[dff0e0b] | 756 | |
---|
| 757 | if (!g_slist_find(twitter_connections, ic)) |
---|
| 758 | return; |
---|
[ddc2de5] | 759 | |
---|
[e132b60] | 760 | ic->flags |= OPT_PONGED; |
---|
[dd672e2] | 761 | td = ic->proto_data; |
---|
| 762 | |
---|
| 763 | if ((req->flags & HTTPC_EOF) || !req->reply_body) { |
---|
| 764 | td->stream = NULL; |
---|
| 765 | imcb_error(ic, "Stream closed (%s)", req->status_string); |
---|
| 766 | imc_logout(ic, TRUE); |
---|
| 767 | return; |
---|
| 768 | } |
---|
| 769 | |
---|
[62f6b45] | 770 | /* MUST search for CRLF, not just LF: |
---|
| 771 | https://dev.twitter.com/docs/streaming-apis/processing#Parsing_responses */ |
---|
[c1d9c95] | 772 | if (!(nl = strstr(req->reply_body, "\r\n"))) |
---|
[ddc2de5] | 773 | return; |
---|
| 774 | |
---|
[62f6b45] | 775 | len = nl - req->reply_body; |
---|
| 776 | if (len > 0) { |
---|
| 777 | c = req->reply_body[len]; |
---|
| 778 | req->reply_body[len] = '\0'; |
---|
| 779 | |
---|
[7a80925] | 780 | if ((parsed = json_parse(req->reply_body, req->body_size))) { |
---|
[62f6b45] | 781 | twitter_stream_handle_object(ic, parsed); |
---|
| 782 | } |
---|
| 783 | json_value_free(parsed); |
---|
| 784 | req->reply_body[len] = c; |
---|
[dff0e0b] | 785 | } |
---|
[ddc2de5] | 786 | |
---|
[62f6b45] | 787 | http_flush_bytes(req, len + 2); |
---|
[dff0e0b] | 788 | |
---|
| 789 | /* One notification might bring multiple events! */ |
---|
| 790 | if (req->body_size > 0) |
---|
| 791 | twitter_http_stream(req); |
---|
[ddc2de5] | 792 | } |
---|
[2322a9f] | 793 | |
---|
[5aa96fc8] | 794 | static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o); |
---|
[c9b5817] | 795 | static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs); |
---|
[5aa96fc8] | 796 | |
---|
[dff0e0b] | 797 | static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o) |
---|
| 798 | { |
---|
[5aa96fc8] | 799 | struct twitter_data *td = ic->proto_data; |
---|
[c9b5817] | 800 | struct twitter_xml_status *txs; |
---|
[d1356cb] | 801 | json_value *c; |
---|
[dff0e0b] | 802 | |
---|
[c9b5817] | 803 | if ((txs = twitter_xt_get_status(o))) { |
---|
[2dcde94] | 804 | gboolean ret = twitter_stream_handle_status(ic, txs); |
---|
| 805 | txs_free(txs); |
---|
| 806 | return ret; |
---|
[d1356cb] | 807 | } else if ((c = json_o_get(o, "direct_message")) && |
---|
[2cd8540] | 808 | (txs = twitter_xt_get_dm(c))) { |
---|
[9f8bb17] | 809 | if (g_strcasecmp(txs->user->screen_name, td->user) != 0) |
---|
[2cd8540] | 810 | imcb_buddy_msg(ic, txs->user->screen_name, |
---|
| 811 | txs->text, 0, txs->created_at); |
---|
[d1356cb] | 812 | txs_free(txs); |
---|
| 813 | return TRUE; |
---|
[5aa96fc8] | 814 | } else if ((c = json_o_get(o, "event")) && c->type == json_string) { |
---|
| 815 | twitter_stream_handle_event(ic, o); |
---|
| 816 | return TRUE; |
---|
| 817 | } else if ((c = json_o_get(o, "disconnect")) && c->type == json_object) { |
---|
| 818 | /* HACK: Because we're inside an event handler, we can't just |
---|
| 819 | disconnect here. Instead, just change the HTTP status string |
---|
| 820 | into a Twitter status string. */ |
---|
| 821 | char *reason = json_o_strdup(c, "reason"); |
---|
| 822 | if (reason) { |
---|
| 823 | g_free(td->stream->status_string); |
---|
| 824 | td->stream->status_string = reason; |
---|
| 825 | } |
---|
| 826 | return TRUE; |
---|
[dff0e0b] | 827 | } |
---|
| 828 | return FALSE; |
---|
| 829 | } |
---|
| 830 | |
---|
[c9b5817] | 831 | static gboolean twitter_stream_handle_status(struct im_connection *ic, struct twitter_xml_status *txs) |
---|
| 832 | { |
---|
| 833 | struct twitter_data *td = ic->proto_data; |
---|
| 834 | int i; |
---|
| 835 | |
---|
| 836 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) { |
---|
| 837 | if (td->log[i].id == txs->id) { |
---|
[29f72b7] | 838 | /* Got a duplicate (RT, probably). Drop it. */ |
---|
[c9b5817] | 839 | return TRUE; |
---|
| 840 | } |
---|
| 841 | } |
---|
| 842 | |
---|
[9f8bb17] | 843 | if (!(g_strcasecmp(txs->user->screen_name, td->user) == 0 || |
---|
[2dcde94] | 844 | set_getbool(&ic->acc->set, "fetch_mentions") || |
---|
[c9b5817] | 845 | bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
| 846 | /* Tweet is from an unknown person and the user does not want |
---|
| 847 | to see @mentions, so drop it. twitter_stream_handle_event() |
---|
| 848 | picks up new follows so this simple filter should be safe. */ |
---|
| 849 | /* TODO: The streaming API seems to do poor @mention matching. |
---|
| 850 | I.e. I'm getting mentions for @WilmerSomething, not just for |
---|
| 851 | @Wilmer. But meh. You want spam, you get spam. */ |
---|
| 852 | return TRUE; |
---|
| 853 | } |
---|
| 854 | |
---|
[29f72b7] | 855 | twitter_status_show(ic, txs); |
---|
| 856 | |
---|
[c9b5817] | 857 | return TRUE; |
---|
| 858 | } |
---|
| 859 | |
---|
[5aa96fc8] | 860 | static gboolean twitter_stream_handle_event(struct im_connection *ic, json_value *o) |
---|
| 861 | { |
---|
| 862 | struct twitter_data *td = ic->proto_data; |
---|
| 863 | json_value *source = json_o_get(o, "source"); |
---|
| 864 | json_value *target = json_o_get(o, "target"); |
---|
| 865 | const char *type = json_o_str(o, "event"); |
---|
| 866 | |
---|
| 867 | if (!type || !source || source->type != json_object |
---|
| 868 | || !target || target->type != json_object) { |
---|
| 869 | return FALSE; |
---|
| 870 | } |
---|
| 871 | |
---|
| 872 | if (strcmp(type, "follow") == 0) { |
---|
| 873 | struct twitter_xml_user *us = twitter_xt_get_user(source); |
---|
| 874 | struct twitter_xml_user *ut = twitter_xt_get_user(target); |
---|
[9f8bb17] | 875 | if (g_strcasecmp(us->screen_name, td->user) == 0) { |
---|
[5aa96fc8] | 876 | twitter_add_buddy(ic, ut->screen_name, ut->name); |
---|
| 877 | } |
---|
| 878 | txu_free(us); |
---|
| 879 | txu_free(ut); |
---|
| 880 | } |
---|
| 881 | |
---|
| 882 | return TRUE; |
---|
| 883 | } |
---|
| 884 | |
---|
[dff0e0b] | 885 | gboolean twitter_open_stream(struct im_connection *ic) |
---|
| 886 | { |
---|
| 887 | struct twitter_data *td = ic->proto_data; |
---|
[62f6b45] | 888 | char *args[2] = {"with", "followings"}; |
---|
[dff0e0b] | 889 | |
---|
| 890 | if ((td->stream = twitter_http(ic, TWITTER_USER_STREAM_URL, |
---|
[62f6b45] | 891 | twitter_http_stream, ic, 0, args, 2))) { |
---|
[dff0e0b] | 892 | /* This flag must be enabled or we'll get no data until EOF |
---|
| 893 | (which err, kind of, defeats the purpose of a streaming API). */ |
---|
| 894 | td->stream->flags |= HTTPC_STREAMING; |
---|
| 895 | return TRUE; |
---|
| 896 | } |
---|
| 897 | |
---|
| 898 | return FALSE; |
---|
| 899 | } |
---|
| 900 | |
---|
| 901 | static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor); |
---|
| 902 | static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor); |
---|
| 903 | |
---|
[2322a9f] | 904 | /** |
---|
| 905 | * Get the timeline with optionally mentions |
---|
| 906 | */ |
---|
[2f9027c] | 907 | gboolean twitter_get_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
[2322a9f] | 908 | { |
---|
| 909 | struct twitter_data *td = ic->proto_data; |
---|
| 910 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
| 911 | |
---|
| 912 | if (td->flags & TWITTER_DOING_TIMELINE) { |
---|
[11ec078] | 913 | if (++td->http_fails >= 5) { |
---|
| 914 | imcb_error(ic, "Fetch timeout (%d)", td->flags); |
---|
| 915 | imc_logout(ic, TRUE); |
---|
[2f9027c] | 916 | return FALSE; |
---|
[11ec078] | 917 | } |
---|
[2322a9f] | 918 | } |
---|
| 919 | |
---|
| 920 | td->flags |= TWITTER_DOING_TIMELINE; |
---|
| 921 | |
---|
| 922 | twitter_get_home_timeline(ic, next_cursor); |
---|
| 923 | |
---|
| 924 | if (include_mentions) { |
---|
| 925 | twitter_get_mentions(ic, next_cursor); |
---|
| 926 | } |
---|
[2f9027c] | 927 | |
---|
| 928 | return TRUE; |
---|
[2322a9f] | 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"; |
---|
[7549d00] | 1009 | args[5] = g_strdup_printf("%" G_GUINT64_FORMAT, 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 | { |
---|
[75bda8b] | 1244 | char *args[2] = { |
---|
| 1245 | "id", |
---|
| 1246 | NULL, |
---|
| 1247 | }; |
---|
| 1248 | args[1] = g_strdup_printf("%llu", (unsigned long long) id); |
---|
| 1249 | twitter_http_f(ic, TWITTER_FAVORITE_CREATE_URL, twitter_http_post, |
---|
| 1250 | ic, 1, args, 2, TWITTER_HTTP_USER_ACK); |
---|
| 1251 | g_free(args[1]); |
---|
[b61c74c] | 1252 | } |
---|