[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 "xmltree.h" |
---|
| 38 | #include "twitter_lib.h" |
---|
[e08ae0c] | 39 | #include "json_util.h" |
---|
[1b221e0] | 40 | #include <ctype.h> |
---|
| 41 | #include <errno.h> |
---|
| 42 | |
---|
[e4e0b37] | 43 | /* GLib < 2.12.0 doesn't have g_ascii_strtoll(), work around using system strtoll(). */ |
---|
| 44 | /* GLib < 2.12.4 can be buggy: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=488013 */ |
---|
| 45 | #if !GLIB_CHECK_VERSION(2,12,5) |
---|
| 46 | #include <stdlib.h> |
---|
| 47 | #include <limits.h> |
---|
| 48 | #define g_ascii_strtoll strtoll |
---|
| 49 | #endif |
---|
| 50 | |
---|
[1b221e0] | 51 | #define TXL_STATUS 1 |
---|
[62d2cfb] | 52 | #define TXL_USER 2 |
---|
| 53 | #define TXL_ID 3 |
---|
| 54 | |
---|
[1b221e0] | 55 | struct twitter_xml_list { |
---|
[62d2cfb] | 56 | int type; |
---|
[8203da9] | 57 | gint64 next_cursor; |
---|
[1b221e0] | 58 | GSList *list; |
---|
| 59 | }; |
---|
| 60 | |
---|
| 61 | struct twitter_xml_user { |
---|
| 62 | char *name; |
---|
| 63 | char *screen_name; |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | struct twitter_xml_status { |
---|
[08579a1] | 67 | time_t created_at; |
---|
[1b221e0] | 68 | char *text; |
---|
| 69 | struct twitter_xml_user *user; |
---|
[ce81acd] | 70 | guint64 id, reply_to; |
---|
[1b221e0] | 71 | }; |
---|
| 72 | |
---|
[d6aa6dd] | 73 | static void twitter_groupchat_init(struct im_connection *ic); |
---|
| 74 | |
---|
[62d2cfb] | 75 | /** |
---|
| 76 | * Frees a twitter_xml_user struct. |
---|
| 77 | */ |
---|
| 78 | static void txu_free(struct twitter_xml_user *txu) |
---|
| 79 | { |
---|
[a26af5c] | 80 | if (txu == NULL) |
---|
| 81 | return; |
---|
[2322a9f] | 82 | |
---|
[62d2cfb] | 83 | g_free(txu->name); |
---|
| 84 | g_free(txu->screen_name); |
---|
[2abceca] | 85 | g_free(txu); |
---|
[62d2cfb] | 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * Frees a twitter_xml_status struct. |
---|
| 90 | */ |
---|
| 91 | static void txs_free(struct twitter_xml_status *txs) |
---|
| 92 | { |
---|
[2322a9f] | 93 | if (txs == NULL) |
---|
| 94 | return; |
---|
| 95 | |
---|
[62d2cfb] | 96 | g_free(txs->text); |
---|
| 97 | txu_free(txs->user); |
---|
[2abceca] | 98 | g_free(txs); |
---|
[62d2cfb] | 99 | } |
---|
| 100 | |
---|
| 101 | /** |
---|
| 102 | * Free a twitter_xml_list struct. |
---|
| 103 | * type is the type of list the struct holds. |
---|
| 104 | */ |
---|
| 105 | static void txl_free(struct twitter_xml_list *txl) |
---|
| 106 | { |
---|
| 107 | GSList *l; |
---|
[a26af5c] | 108 | if (txl == NULL) |
---|
| 109 | return; |
---|
[2322a9f] | 110 | |
---|
| 111 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
| 112 | if (txl->type == TXL_STATUS) { |
---|
[5983eca] | 113 | txs_free((struct twitter_xml_status *) l->data); |
---|
[2322a9f] | 114 | } else if (txl->type == TXL_ID) { |
---|
[62d2cfb] | 115 | g_free(l->data); |
---|
[2322a9f] | 116 | } else if (txl->type == TXL_USER) { |
---|
[de923d5] | 117 | txu_free(l->data); |
---|
[2322a9f] | 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
[62d2cfb] | 121 | g_slist_free(txl->list); |
---|
[fd65edb] | 122 | g_free(txl); |
---|
[62d2cfb] | 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
[2322a9f] | 126 | * Compare status elements |
---|
| 127 | */ |
---|
| 128 | static gint twitter_compare_elements(gconstpointer a, gconstpointer b) |
---|
| 129 | { |
---|
| 130 | struct twitter_xml_status *a_status = (struct twitter_xml_status *) a; |
---|
| 131 | struct twitter_xml_status *b_status = (struct twitter_xml_status *) b; |
---|
| 132 | |
---|
| 133 | if (a_status->created_at < b_status->created_at) { |
---|
| 134 | return -1; |
---|
| 135 | } else if (a_status->created_at > b_status->created_at) { |
---|
| 136 | return 1; |
---|
| 137 | } else { |
---|
| 138 | return 0; |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | * Add a buddy if it is not already added, set the status to logged in. |
---|
[62d2cfb] | 144 | */ |
---|
[3e69802] | 145 | static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname) |
---|
[62d2cfb] | 146 | { |
---|
[1014cab] | 147 | struct twitter_data *td = ic->proto_data; |
---|
| 148 | |
---|
[de923d5] | 149 | // Check if the buddy is already in the buddy list. |
---|
[5983eca] | 150 | if (!bee_user_by_handle(ic->bee, ic, name)) { |
---|
[e88fbe27] | 151 | char *mode = set_getstr(&ic->acc->set, "mode"); |
---|
[5983eca] | 152 | |
---|
[62d2cfb] | 153 | // The buddy is not in the list, add the buddy and set the status to logged in. |
---|
[5983eca] | 154 | imcb_add_buddy(ic, name, NULL); |
---|
| 155 | imcb_rename_buddy(ic, name, fullname); |
---|
| 156 | if (g_strcasecmp(mode, "chat") == 0) { |
---|
[5c18a76] | 157 | /* Necessary so that nicks always get translated to the |
---|
| 158 | exact Twitter username. */ |
---|
[5983eca] | 159 | imcb_buddy_nick_hint(ic, name, name); |
---|
[2322a9f] | 160 | imcb_chat_add_buddy(td->timeline_gc, name); |
---|
[5983eca] | 161 | } else if (g_strcasecmp(mode, "many") == 0) |
---|
| 162 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
[62d2cfb] | 163 | } |
---|
| 164 | } |
---|
[1b221e0] | 165 | |
---|
[a7b9ec7] | 166 | /* Warning: May return a malloc()ed value, which will be free()d on the next |
---|
[de923d5] | 167 | call. Only for short-term use. NOT THREADSAFE! */ |
---|
[6eca2eb] | 168 | char *twitter_parse_error(struct http_request *req) |
---|
[a7b9ec7] | 169 | { |
---|
| 170 | static char *ret = NULL; |
---|
[d0752e8] | 171 | struct xt_node *root, *node, *err; |
---|
[5983eca] | 172 | |
---|
[a7b9ec7] | 173 | g_free(ret); |
---|
| 174 | ret = NULL; |
---|
[5983eca] | 175 | |
---|
| 176 | if (req->body_size > 0) { |
---|
[d0752e8] | 177 | root = xt_from_string(req->reply_body, req->body_size); |
---|
[de923d5] | 178 | |
---|
[d0752e8] | 179 | for (node = root; node; node = node->next) |
---|
[c0f33f1] | 180 | if ((err = xt_find_node(node->children, "error")) && err->text_len > 0) { |
---|
| 181 | ret = g_strdup_printf("%s (%s)", req->status_string, err->text); |
---|
[de923d5] | 182 | break; |
---|
| 183 | } |
---|
[5983eca] | 184 | |
---|
[d0752e8] | 185 | xt_free_node(root); |
---|
[a7b9ec7] | 186 | } |
---|
[5983eca] | 187 | |
---|
[6eca2eb] | 188 | return ret ? ret : req->status_string; |
---|
[a7b9ec7] | 189 | } |
---|
| 190 | |
---|
[0688e99] | 191 | /* TODO: NULL means failure, but not always that the connection is dead. This sucks for callers. */ |
---|
[e08ae0c] | 192 | static json_value *twitter_parse_response(struct im_connection *ic, struct http_request *req) |
---|
[2a6da96] | 193 | { |
---|
| 194 | gboolean logging_in = !(ic->flags & OPT_LOGGED_IN); |
---|
| 195 | gboolean periodic; |
---|
| 196 | struct twitter_data *td = ic->proto_data; |
---|
[e08ae0c] | 197 | json_value *ret; |
---|
[2a6da96] | 198 | char path[64] = "", *s; |
---|
| 199 | |
---|
| 200 | if ((s = strchr(req->request, ' '))) { |
---|
| 201 | path[sizeof(path)-1] = '\0'; |
---|
| 202 | strncpy(path, s + 1, sizeof(path) - 1); |
---|
| 203 | if ((s = strchr(path, '?')) || (s = strchr(path, ' '))) |
---|
| 204 | *s = '\0'; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | /* Kinda nasty. :-( Trying to suppress error messages, but only |
---|
| 208 | for periodic (i.e. mentions/timeline) queries. */ |
---|
| 209 | periodic = strstr(path, "timeline") || strstr(path, "mentions"); |
---|
| 210 | |
---|
| 211 | if (req->status_code == 401 && logging_in) { |
---|
| 212 | /* IIRC Twitter once had an outage where they were randomly |
---|
| 213 | throwing 401s so I'll keep treating this one as fatal |
---|
| 214 | only during login. */ |
---|
| 215 | imcb_error(ic, "Authentication failure"); |
---|
| 216 | imc_logout(ic, FALSE); |
---|
| 217 | return NULL; |
---|
| 218 | } else if (req->status_code != 200) { |
---|
| 219 | // It didn't go well, output the error and return. |
---|
| 220 | if (!periodic || logging_in || ++td->http_fails >= 5) |
---|
| 221 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 222 | path, twitter_parse_error(req)); |
---|
| 223 | |
---|
| 224 | if (logging_in) |
---|
| 225 | imc_logout(ic, TRUE); |
---|
| 226 | return NULL; |
---|
| 227 | } else { |
---|
| 228 | td->http_fails = 0; |
---|
| 229 | } |
---|
| 230 | |
---|
[e08ae0c] | 231 | if ((ret = json_parse(req->reply_body)) == NULL) { |
---|
[2a6da96] | 232 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 233 | path, "XML parse error"); |
---|
| 234 | } |
---|
| 235 | return ret; |
---|
| 236 | } |
---|
| 237 | |
---|
[1b221e0] | 238 | static void twitter_http_get_friends_ids(struct http_request *req); |
---|
| 239 | |
---|
| 240 | /** |
---|
| 241 | * Get the friends ids. |
---|
| 242 | */ |
---|
[8203da9] | 243 | void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor) |
---|
[1b221e0] | 244 | { |
---|
[5983eca] | 245 | // Primitive, but hey! It works... |
---|
| 246 | char *args[2]; |
---|
[1b221e0] | 247 | args[0] = "cursor"; |
---|
[5983eca] | 248 | args[1] = g_strdup_printf("%lld", (long long) next_cursor); |
---|
[bb5ce4d1] | 249 | twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2); |
---|
[1b221e0] | 250 | |
---|
| 251 | g_free(args[1]); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | /** |
---|
| 255 | * Fill a list of ids. |
---|
| 256 | */ |
---|
[e08ae0c] | 257 | static xt_status twitter_xt_get_friends_id_list(json_value *node, struct twitter_xml_list *txl) |
---|
[1b221e0] | 258 | { |
---|
[e08ae0c] | 259 | json_value *c; |
---|
| 260 | int i; |
---|
[5983eca] | 261 | |
---|
[62d2cfb] | 262 | // Set the list type. |
---|
| 263 | txl->type = TXL_ID; |
---|
[1b221e0] | 264 | |
---|
[e08ae0c] | 265 | c = json_o_get(node, "ids"); |
---|
| 266 | if (!c || c->type != json_array) |
---|
| 267 | return XT_ABORT; |
---|
[1b221e0] | 268 | |
---|
[e08ae0c] | 269 | for (i = 0; i < c->u.array.length; i ++) { |
---|
[0688e99] | 270 | if (c->u.array.values[i]->type != json_integer) |
---|
| 271 | continue; |
---|
| 272 | |
---|
[e08ae0c] | 273 | txl->list = g_slist_prepend(txl->list, |
---|
| 274 | g_strdup_printf("%ld", c->u.array.values[i]->u.integer)); |
---|
| 275 | |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | c = json_o_get(node, "next_cursor"); |
---|
| 279 | if (c && c->type == json_integer) |
---|
| 280 | txl->next_cursor = c->u.integer; |
---|
| 281 | else |
---|
| 282 | txl->next_cursor = -1; |
---|
| 283 | |
---|
[1b221e0] | 284 | return XT_HANDLED; |
---|
| 285 | } |
---|
| 286 | |
---|
[de923d5] | 287 | static void twitter_get_users_lookup(struct im_connection *ic); |
---|
| 288 | |
---|
[1b221e0] | 289 | /** |
---|
| 290 | * Callback for getting the friends ids. |
---|
| 291 | */ |
---|
| 292 | static void twitter_http_get_friends_ids(struct http_request *req) |
---|
| 293 | { |
---|
| 294 | struct im_connection *ic; |
---|
[e08ae0c] | 295 | json_value *parsed; |
---|
[1b221e0] | 296 | struct twitter_xml_list *txl; |
---|
[3bd4a93] | 297 | struct twitter_data *td; |
---|
[1b221e0] | 298 | |
---|
| 299 | ic = req->data; |
---|
| 300 | |
---|
[62d2cfb] | 301 | // Check if the connection is still active. |
---|
[5983eca] | 302 | if (!g_slist_find(twitter_connections, ic)) |
---|
[62d2cfb] | 303 | return; |
---|
[5983eca] | 304 | |
---|
[37aa317] | 305 | td = ic->proto_data; |
---|
[62d2cfb] | 306 | |
---|
[de923d5] | 307 | /* Create the room now that we "logged in". */ |
---|
[2322a9f] | 308 | if (!td->timeline_gc && g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0) |
---|
[de923d5] | 309 | twitter_groupchat_init(ic); |
---|
| 310 | |
---|
[1b221e0] | 311 | txl = g_new0(struct twitter_xml_list, 1); |
---|
[de923d5] | 312 | txl->list = td->follow_ids; |
---|
[1b221e0] | 313 | |
---|
| 314 | // Parse the data. |
---|
[2a6da96] | 315 | if (!(parsed = twitter_parse_response(ic, req))) |
---|
| 316 | return; |
---|
[e08ae0c] | 317 | |
---|
[d0752e8] | 318 | twitter_xt_get_friends_id_list(parsed, txl); |
---|
[e08ae0c] | 319 | json_value_free(parsed); |
---|
[1b221e0] | 320 | |
---|
[de923d5] | 321 | td->follow_ids = txl->list; |
---|
[1b221e0] | 322 | if (txl->next_cursor) |
---|
[de923d5] | 323 | /* These were just numbers. Up to 4000 in a response AFAIK so if we get here |
---|
| 324 | we may be using a spammer account. \o/ */ |
---|
[1b221e0] | 325 | twitter_get_friends_ids(ic, txl->next_cursor); |
---|
[de923d5] | 326 | else |
---|
| 327 | /* Now to convert all those numbers into names.. */ |
---|
| 328 | twitter_get_users_lookup(ic); |
---|
| 329 | |
---|
| 330 | txl->list = NULL; |
---|
| 331 | txl_free(txl); |
---|
| 332 | } |
---|
| 333 | |
---|
[e08ae0c] | 334 | static xt_status twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl); |
---|
[de923d5] | 335 | static void twitter_http_get_users_lookup(struct http_request *req); |
---|
| 336 | |
---|
| 337 | static void twitter_get_users_lookup(struct im_connection *ic) |
---|
| 338 | { |
---|
| 339 | struct twitter_data *td = ic->proto_data; |
---|
| 340 | char *args[2] = { |
---|
| 341 | "user_id", |
---|
| 342 | NULL, |
---|
| 343 | }; |
---|
| 344 | GString *ids = g_string_new(""); |
---|
| 345 | int i; |
---|
| 346 | |
---|
| 347 | /* We can request up to 100 users at a time. */ |
---|
| 348 | for (i = 0; i < 100 && td->follow_ids; i ++) { |
---|
| 349 | g_string_append_printf(ids, ",%s", (char*) td->follow_ids->data); |
---|
| 350 | g_free(td->follow_ids->data); |
---|
| 351 | td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data); |
---|
| 352 | } |
---|
| 353 | if (ids->len > 0) { |
---|
| 354 | args[1] = ids->str + 1; |
---|
| 355 | /* POST, because I think ids can be up to 1KB long. */ |
---|
| 356 | twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2); |
---|
| 357 | } else { |
---|
| 358 | /* We have all users. Continue with login. (Get statuses.) */ |
---|
| 359 | td->flags |= TWITTER_HAVE_FRIENDS; |
---|
| 360 | twitter_login_finish(ic); |
---|
| 361 | } |
---|
| 362 | g_string_free(ids, TRUE); |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | /** |
---|
| 366 | * Callback for getting (twitter)friends... |
---|
| 367 | * |
---|
| 368 | * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has |
---|
| 369 | * hundreds of friends?" you wonder? You probably not, since you are reading the source of |
---|
| 370 | * BitlBee... Get a life and meet new people! |
---|
| 371 | */ |
---|
| 372 | static void twitter_http_get_users_lookup(struct http_request *req) |
---|
| 373 | { |
---|
| 374 | struct im_connection *ic = req->data; |
---|
[e08ae0c] | 375 | json_value *parsed; |
---|
[de923d5] | 376 | struct twitter_xml_list *txl; |
---|
| 377 | GSList *l = NULL; |
---|
| 378 | struct twitter_xml_user *user; |
---|
| 379 | |
---|
| 380 | // Check if the connection is still active. |
---|
| 381 | if (!g_slist_find(twitter_connections, ic)) |
---|
| 382 | return; |
---|
| 383 | |
---|
| 384 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 385 | txl->list = NULL; |
---|
[1b221e0] | 386 | |
---|
[de923d5] | 387 | // Get the user list from the parsed xml feed. |
---|
[2a6da96] | 388 | if (!(parsed = twitter_parse_response(ic, req))) |
---|
| 389 | return; |
---|
[d0752e8] | 390 | twitter_xt_get_users(parsed, txl); |
---|
[e08ae0c] | 391 | json_value_free(parsed); |
---|
[de923d5] | 392 | |
---|
| 393 | // Add the users as buddies. |
---|
| 394 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
| 395 | user = l->data; |
---|
| 396 | twitter_add_buddy(ic, user->screen_name, user->name); |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | // Free the structure. |
---|
[62d2cfb] | 400 | txl_free(txl); |
---|
[de923d5] | 401 | |
---|
| 402 | twitter_get_users_lookup(ic); |
---|
[1b221e0] | 403 | } |
---|
| 404 | |
---|
[62d2cfb] | 405 | /** |
---|
| 406 | * Function to fill a twitter_xml_list struct. |
---|
| 407 | * It sets: |
---|
| 408 | * - all <user>s from the <users> element. |
---|
| 409 | */ |
---|
[e08ae0c] | 410 | static xt_status twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl) |
---|
[62d2cfb] | 411 | { |
---|
| 412 | struct twitter_xml_user *txu; |
---|
[e08ae0c] | 413 | int i; |
---|
[62d2cfb] | 414 | |
---|
| 415 | // Set the type of the list. |
---|
| 416 | txl->type = TXL_USER; |
---|
| 417 | |
---|
[e08ae0c] | 418 | if (!node || node->type != json_array) |
---|
| 419 | return XT_ABORT; |
---|
| 420 | |
---|
[62d2cfb] | 421 | // The root <users> node should hold the list of users <user> |
---|
| 422 | // Walk over the nodes children. |
---|
[e08ae0c] | 423 | for (i = 0; i < node->u.array.length; i ++) { |
---|
| 424 | txu = g_new0(struct twitter_xml_user, 1); |
---|
| 425 | txu->name = g_strdup(json_o_str(node->u.array.values[i], "name")); |
---|
| 426 | txu->screen_name = g_strdup(json_o_str(node->u.array.values[i], "screen_name")); |
---|
| 427 | txl->list = g_slist_prepend(txl->list, txu); |
---|
[62d2cfb] | 428 | } |
---|
| 429 | |
---|
| 430 | return XT_HANDLED; |
---|
| 431 | } |
---|
| 432 | |
---|
[2b02617] | 433 | #ifdef __GLIBC__ |
---|
| 434 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y" |
---|
| 435 | #else |
---|
| 436 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y" |
---|
| 437 | #endif |
---|
[62d2cfb] | 438 | |
---|
[1b221e0] | 439 | /** |
---|
| 440 | * Function to fill a twitter_xml_status struct. |
---|
| 441 | * It sets: |
---|
| 442 | * - the status text and |
---|
| 443 | * - the created_at timestamp and |
---|
| 444 | * - the status id and |
---|
| 445 | * - the user in a twitter_xml_user struct. |
---|
| 446 | */ |
---|
[5983eca] | 447 | static xt_status twitter_xt_get_status(struct xt_node *node, struct twitter_xml_status *txs) |
---|
[1b221e0] | 448 | { |
---|
[bd599b9] | 449 | struct xt_node *child, *rt = NULL; |
---|
[1b221e0] | 450 | |
---|
| 451 | // Walk over the nodes children. |
---|
[5983eca] | 452 | for (child = node->children; child; child = child->next) { |
---|
| 453 | if (g_strcasecmp("text", child->name) == 0) { |
---|
| 454 | txs->text = g_memdup(child->text, child->text_len + 1); |
---|
| 455 | } else if (g_strcasecmp("retweeted_status", child->name) == 0) { |
---|
[e193aeb] | 456 | rt = child; |
---|
[5983eca] | 457 | } else if (g_strcasecmp("created_at", child->name) == 0) { |
---|
[08579a1] | 458 | struct tm parsed; |
---|
[5983eca] | 459 | |
---|
[08579a1] | 460 | /* Very sensitive to changes to the formatting of |
---|
| 461 | this field. :-( Also assumes the timezone used |
---|
| 462 | is UTC since C time handling functions suck. */ |
---|
[5983eca] | 463 | if (strptime(child->text, TWITTER_TIME_FORMAT, &parsed) != NULL) |
---|
| 464 | txs->created_at = mktime_utc(&parsed); |
---|
| 465 | } else if (g_strcasecmp("user", child->name) == 0) { |
---|
[1b221e0] | 466 | txs->user = g_new0(struct twitter_xml_user, 1); |
---|
[e08ae0c] | 467 | // twitter_xt_get_user(child, txs->user); |
---|
[5983eca] | 468 | } else if (g_strcasecmp("id", child->name) == 0) { |
---|
| 469 | txs->id = g_ascii_strtoull(child->text, NULL, 10); |
---|
| 470 | } else if (g_strcasecmp("in_reply_to_status_id", child->name) == 0) { |
---|
| 471 | txs->reply_to = g_ascii_strtoull(child->text, NULL, 10); |
---|
[ce81acd] | 472 | } |
---|
[1b221e0] | 473 | } |
---|
[5983eca] | 474 | |
---|
[0fff0b2] | 475 | /* If it's a (truncated) retweet, get the original. Even if the API claims it |
---|
| 476 | wasn't truncated because it may be lying. */ |
---|
| 477 | if (rt) { |
---|
[e193aeb] | 478 | struct twitter_xml_status *rtxs = g_new0(struct twitter_xml_status, 1); |
---|
[5983eca] | 479 | if (twitter_xt_get_status(rt, rtxs) != XT_HANDLED) { |
---|
[e193aeb] | 480 | txs_free(rtxs); |
---|
| 481 | return XT_HANDLED; |
---|
| 482 | } |
---|
[5983eca] | 483 | |
---|
[e193aeb] | 484 | g_free(txs->text); |
---|
| 485 | txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text); |
---|
| 486 | txs_free(rtxs); |
---|
[429a9b1] | 487 | } else { |
---|
| 488 | struct xt_node *urls, *url; |
---|
| 489 | |
---|
[7d2ce9a] | 490 | urls = xt_find_path(node, "entities"); |
---|
| 491 | if (urls != NULL) |
---|
| 492 | urls = urls->children; |
---|
| 493 | for (; urls; urls = urls->next) { |
---|
| 494 | if (strcmp(urls->name, "urls") != 0 && strcmp(urls->name, "media") != 0) |
---|
[429a9b1] | 495 | continue; |
---|
| 496 | |
---|
[7d2ce9a] | 497 | for (url = urls ? urls->children : NULL; url; url = url->next) { |
---|
| 498 | /* "short" is a reserved word. :-P */ |
---|
| 499 | struct xt_node *kort = xt_find_node(url->children, "url"); |
---|
| 500 | struct xt_node *disp = xt_find_node(url->children, "display_url"); |
---|
| 501 | char *pos, *new; |
---|
| 502 | |
---|
| 503 | if (!kort || !kort->text || !disp || !disp->text || |
---|
| 504 | !(pos = strstr(txs->text, kort->text))) |
---|
| 505 | continue; |
---|
| 506 | |
---|
| 507 | *pos = '\0'; |
---|
| 508 | new = g_strdup_printf("%s%s <%s>%s", txs->text, kort->text, |
---|
| 509 | disp->text, pos + strlen(kort->text)); |
---|
| 510 | |
---|
| 511 | g_free(txs->text); |
---|
| 512 | txs->text = new; |
---|
| 513 | } |
---|
[429a9b1] | 514 | } |
---|
[e193aeb] | 515 | } |
---|
[5983eca] | 516 | |
---|
[1b221e0] | 517 | return XT_HANDLED; |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | /** |
---|
| 521 | * Function to fill a twitter_xml_list struct. |
---|
| 522 | * It sets: |
---|
| 523 | * - all <status>es within the <status> element and |
---|
| 524 | * - the next_cursor. |
---|
| 525 | */ |
---|
[5983eca] | 526 | static xt_status twitter_xt_get_status_list(struct im_connection *ic, struct xt_node *node, |
---|
| 527 | struct twitter_xml_list *txl) |
---|
[1b221e0] | 528 | { |
---|
| 529 | struct twitter_xml_status *txs; |
---|
| 530 | struct xt_node *child; |
---|
[203a2d2] | 531 | bee_user_t *bu; |
---|
[1b221e0] | 532 | |
---|
[62d2cfb] | 533 | // Set the type of the list. |
---|
| 534 | txl->type = TXL_STATUS; |
---|
| 535 | |
---|
[1b221e0] | 536 | // The root <statuses> node should hold the list of statuses <status> |
---|
| 537 | // Walk over the nodes children. |
---|
[5983eca] | 538 | for (child = node->children; child; child = child->next) { |
---|
| 539 | if (g_strcasecmp("status", child->name) == 0) { |
---|
[1b221e0] | 540 | txs = g_new0(struct twitter_xml_status, 1); |
---|
| 541 | twitter_xt_get_status(child, txs); |
---|
| 542 | // Put the item in the front of the list. |
---|
[5983eca] | 543 | txl->list = g_slist_prepend(txl->list, txs); |
---|
| 544 | |
---|
[203a2d2] | 545 | if (txs->user && txs->user->screen_name && |
---|
[5983eca] | 546 | (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
[203a2d2] | 547 | struct twitter_user_data *tud = bu->data; |
---|
[5983eca] | 548 | |
---|
| 549 | if (txs->id > tud->last_id) { |
---|
[203a2d2] | 550 | tud->last_id = txs->id; |
---|
| 551 | tud->last_time = txs->created_at; |
---|
| 552 | } |
---|
| 553 | } |
---|
[5983eca] | 554 | } else if (g_strcasecmp("next_cursor", child->name) == 0) { |
---|
[e08ae0c] | 555 | // twitter_xt_next_cursor(child, txl); |
---|
[1b221e0] | 556 | } |
---|
| 557 | } |
---|
| 558 | |
---|
| 559 | return XT_HANDLED; |
---|
| 560 | } |
---|
| 561 | |
---|
[ce81acd] | 562 | static char *twitter_msg_add_id(struct im_connection *ic, |
---|
[5983eca] | 563 | struct twitter_xml_status *txs, const char *prefix) |
---|
[ce81acd] | 564 | { |
---|
| 565 | struct twitter_data *td = ic->proto_data; |
---|
| 566 | char *ret = NULL; |
---|
[5983eca] | 567 | |
---|
| 568 | if (!set_getbool(&ic->acc->set, "show_ids")) { |
---|
[ce81acd] | 569 | if (*prefix) |
---|
| 570 | return g_strconcat(prefix, txs->text, NULL); |
---|
| 571 | else |
---|
| 572 | return NULL; |
---|
| 573 | } |
---|
[5983eca] | 574 | |
---|
[ce81acd] | 575 | td->log[td->log_id].id = txs->id; |
---|
| 576 | td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name); |
---|
[5983eca] | 577 | if (txs->reply_to) { |
---|
[ce81acd] | 578 | int i; |
---|
[5983eca] | 579 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) |
---|
| 580 | if (td->log[i].id == txs->reply_to) { |
---|
| 581 | ret = g_strdup_printf("\002[\002%02d->%02d\002]\002 %s%s", |
---|
| 582 | td->log_id, i, prefix, txs->text); |
---|
[ce81acd] | 583 | break; |
---|
| 584 | } |
---|
| 585 | } |
---|
| 586 | if (ret == NULL) |
---|
[5983eca] | 587 | ret = g_strdup_printf("\002[\002%02d\002]\002 %s%s", td->log_id, prefix, txs->text); |
---|
[ce81acd] | 588 | td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH; |
---|
[5983eca] | 589 | |
---|
[ce81acd] | 590 | return ret; |
---|
| 591 | } |
---|
| 592 | |
---|
[d6aa6dd] | 593 | static void twitter_groupchat_init(struct im_connection *ic) |
---|
| 594 | { |
---|
| 595 | char *name_hint; |
---|
| 596 | struct groupchat *gc; |
---|
| 597 | struct twitter_data *td = ic->proto_data; |
---|
[fd65edb] | 598 | GSList *l; |
---|
[5983eca] | 599 | |
---|
[2322a9f] | 600 | td->timeline_gc = gc = imcb_chat_new(ic, "twitter/timeline"); |
---|
[5983eca] | 601 | |
---|
| 602 | name_hint = g_strdup_printf("%s_%s", td->prefix, ic->acc->user); |
---|
| 603 | imcb_chat_name_hint(gc, name_hint); |
---|
| 604 | g_free(name_hint); |
---|
| 605 | |
---|
| 606 | for (l = ic->bee->users; l; l = l->next) { |
---|
[fd65edb] | 607 | bee_user_t *bu = l->data; |
---|
[5983eca] | 608 | if (bu->ic == ic) |
---|
[2322a9f] | 609 | imcb_chat_add_buddy(td->timeline_gc, bu->handle); |
---|
[fd65edb] | 610 | } |
---|
[d6aa6dd] | 611 | } |
---|
| 612 | |
---|
[62d2cfb] | 613 | /** |
---|
| 614 | * Function that is called to see the statuses in a groupchat window. |
---|
| 615 | */ |
---|
[5983eca] | 616 | static void twitter_groupchat(struct im_connection *ic, GSList * list) |
---|
[62d2cfb] | 617 | { |
---|
| 618 | struct twitter_data *td = ic->proto_data; |
---|
| 619 | GSList *l = NULL; |
---|
| 620 | struct twitter_xml_status *status; |
---|
| 621 | struct groupchat *gc; |
---|
[2322a9f] | 622 | guint64 last_id = 0; |
---|
[62d2cfb] | 623 | |
---|
| 624 | // Create a new groupchat if it does not exsist. |
---|
[2322a9f] | 625 | if (!td->timeline_gc) |
---|
[d6aa6dd] | 626 | twitter_groupchat_init(ic); |
---|
[5983eca] | 627 | |
---|
[2322a9f] | 628 | gc = td->timeline_gc; |
---|
[d6aa6dd] | 629 | if (!gc->joined) |
---|
[5983eca] | 630 | imcb_chat_add_buddy(gc, ic->acc->user); |
---|
[62d2cfb] | 631 | |
---|
[5983eca] | 632 | for (l = list; l; l = g_slist_next(l)) { |
---|
[ce81acd] | 633 | char *msg; |
---|
[5983eca] | 634 | |
---|
[62d2cfb] | 635 | status = l->data; |
---|
[2322a9f] | 636 | if (status->user == NULL || status->text == NULL || last_id == status->id) |
---|
[a26af5c] | 637 | continue; |
---|
| 638 | |
---|
[2322a9f] | 639 | last_id = status->id; |
---|
[5983eca] | 640 | |
---|
[0b3ffb1] | 641 | strip_html(status->text); |
---|
[2322a9f] | 642 | |
---|
[948abab] | 643 | if (set_getbool(&ic->acc->set, "strip_newlines")) |
---|
[cf0dbdd] | 644 | strip_newlines(status->text); |
---|
[948abab] | 645 | |
---|
[ce81acd] | 646 | msg = twitter_msg_add_id(ic, status, ""); |
---|
[5983eca] | 647 | |
---|
[62d2cfb] | 648 | // Say it! |
---|
[2322a9f] | 649 | if (g_strcasecmp(td->user, status->user->screen_name) == 0) { |
---|
[ce81acd] | 650 | imcb_chat_log(gc, "You: %s", msg ? msg : status->text); |
---|
[2322a9f] | 651 | } else { |
---|
| 652 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
| 653 | |
---|
[ce81acd] | 654 | imcb_chat_msg(gc, status->user->screen_name, |
---|
[5983eca] | 655 | msg ? msg : status->text, 0, status->created_at); |
---|
[2322a9f] | 656 | } |
---|
[5983eca] | 657 | |
---|
[ce81acd] | 658 | g_free(msg); |
---|
[5983eca] | 659 | |
---|
[2322a9f] | 660 | // Update the timeline_id to hold the highest id, so that by the next request |
---|
[ce81acd] | 661 | // we won't pick up the updates already in the list. |
---|
[2322a9f] | 662 | td->timeline_id = MAX(td->timeline_id, status->id); |
---|
[62d2cfb] | 663 | } |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | /** |
---|
| 667 | * Function that is called to see statuses as private messages. |
---|
| 668 | */ |
---|
[5983eca] | 669 | static void twitter_private_message_chat(struct im_connection *ic, GSList * list) |
---|
[62d2cfb] | 670 | { |
---|
| 671 | struct twitter_data *td = ic->proto_data; |
---|
| 672 | GSList *l = NULL; |
---|
| 673 | struct twitter_xml_status *status; |
---|
[e88fbe27] | 674 | char from[MAX_STRING]; |
---|
| 675 | gboolean mode_one; |
---|
[2322a9f] | 676 | guint64 last_id = 0; |
---|
[62d2cfb] | 677 | |
---|
[5983eca] | 678 | mode_one = g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "one") == 0; |
---|
| 679 | |
---|
| 680 | if (mode_one) { |
---|
| 681 | g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user); |
---|
| 682 | from[MAX_STRING - 1] = '\0'; |
---|
[e88fbe27] | 683 | } |
---|
[5983eca] | 684 | |
---|
| 685 | for (l = list; l; l = g_slist_next(l)) { |
---|
[ce81acd] | 686 | char *prefix = NULL, *text = NULL; |
---|
[5983eca] | 687 | |
---|
[62d2cfb] | 688 | status = l->data; |
---|
[2322a9f] | 689 | if (status->user == NULL || status->text == NULL || last_id == status->id) |
---|
| 690 | continue; |
---|
| 691 | |
---|
| 692 | last_id = status->id; |
---|
[5983eca] | 693 | |
---|
| 694 | strip_html(status->text); |
---|
| 695 | if (mode_one) |
---|
[ce81acd] | 696 | prefix = g_strdup_printf("\002<\002%s\002>\002 ", |
---|
[5983eca] | 697 | status->user->screen_name); |
---|
[55b1e69] | 698 | else |
---|
| 699 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
[5983eca] | 700 | |
---|
[ce81acd] | 701 | text = twitter_msg_add_id(ic, status, prefix ? prefix : ""); |
---|
[5983eca] | 702 | |
---|
| 703 | imcb_buddy_msg(ic, |
---|
| 704 | mode_one ? from : status->user->screen_name, |
---|
| 705 | text ? text : status->text, 0, status->created_at); |
---|
| 706 | |
---|
[2322a9f] | 707 | // Update the timeline_id to hold the highest id, so that by the next request |
---|
[ce81acd] | 708 | // we won't pick up the updates already in the list. |
---|
[2322a9f] | 709 | td->timeline_id = MAX(td->timeline_id, status->id); |
---|
[5983eca] | 710 | |
---|
| 711 | g_free(text); |
---|
| 712 | g_free(prefix); |
---|
[62d2cfb] | 713 | } |
---|
| 714 | } |
---|
| 715 | |
---|
[2322a9f] | 716 | static void twitter_http_get_home_timeline(struct http_request *req); |
---|
| 717 | static void twitter_http_get_mentions(struct http_request *req); |
---|
| 718 | |
---|
| 719 | /** |
---|
| 720 | * Get the timeline with optionally mentions |
---|
| 721 | */ |
---|
| 722 | void twitter_get_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
| 723 | { |
---|
| 724 | struct twitter_data *td = ic->proto_data; |
---|
| 725 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
| 726 | |
---|
| 727 | if (td->flags & TWITTER_DOING_TIMELINE) { |
---|
[11ec078] | 728 | if (++td->http_fails >= 5) { |
---|
| 729 | imcb_error(ic, "Fetch timeout (%d)", td->flags); |
---|
| 730 | imc_logout(ic, TRUE); |
---|
| 731 | } |
---|
[2322a9f] | 732 | } |
---|
| 733 | |
---|
| 734 | td->flags |= TWITTER_DOING_TIMELINE; |
---|
| 735 | |
---|
| 736 | twitter_get_home_timeline(ic, next_cursor); |
---|
| 737 | |
---|
| 738 | if (include_mentions) { |
---|
| 739 | twitter_get_mentions(ic, next_cursor); |
---|
| 740 | } |
---|
| 741 | } |
---|
| 742 | |
---|
| 743 | /** |
---|
| 744 | * Call this one after receiving timeline/mentions. Show to user once we have |
---|
| 745 | * both. |
---|
| 746 | */ |
---|
| 747 | void twitter_flush_timeline(struct im_connection *ic) |
---|
| 748 | { |
---|
| 749 | struct twitter_data *td = ic->proto_data; |
---|
| 750 | gboolean include_mentions = set_getbool(&ic->acc->set, "fetch_mentions"); |
---|
[b5fe39b] | 751 | int show_old_mentions = set_getint(&ic->acc->set, "show_old_mentions"); |
---|
[2322a9f] | 752 | struct twitter_xml_list *home_timeline = td->home_timeline_obj; |
---|
| 753 | struct twitter_xml_list *mentions = td->mentions_obj; |
---|
| 754 | GSList *output = NULL; |
---|
| 755 | GSList *l; |
---|
| 756 | |
---|
| 757 | if (!(td->flags & TWITTER_GOT_TIMELINE)) { |
---|
| 758 | return; |
---|
| 759 | } |
---|
| 760 | |
---|
| 761 | if (include_mentions && !(td->flags & TWITTER_GOT_MENTIONS)) { |
---|
| 762 | return; |
---|
| 763 | } |
---|
| 764 | |
---|
| 765 | if (home_timeline && home_timeline->list) { |
---|
| 766 | for (l = home_timeline->list; l; l = g_slist_next(l)) { |
---|
| 767 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
| 768 | } |
---|
| 769 | } |
---|
| 770 | |
---|
| 771 | if (include_mentions && mentions && mentions->list) { |
---|
| 772 | for (l = mentions->list; l; l = g_slist_next(l)) { |
---|
[b5fe39b] | 773 | if (show_old_mentions < 1 && output && twitter_compare_elements(l->data, output->data) < 0) { |
---|
[2322a9f] | 774 | continue; |
---|
| 775 | } |
---|
| 776 | |
---|
| 777 | output = g_slist_insert_sorted(output, l->data, twitter_compare_elements); |
---|
| 778 | } |
---|
| 779 | } |
---|
[2a6da96] | 780 | |
---|
| 781 | if (!(ic->flags & OPT_LOGGED_IN)) |
---|
| 782 | imcb_connected(ic); |
---|
[2322a9f] | 783 | |
---|
| 784 | // See if the user wants to see the messages in a groupchat window or as private messages. |
---|
| 785 | if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0) |
---|
| 786 | twitter_groupchat(ic, output); |
---|
| 787 | else |
---|
| 788 | twitter_private_message_chat(ic, output); |
---|
| 789 | |
---|
| 790 | g_slist_free(output); |
---|
| 791 | |
---|
[199fea6] | 792 | txl_free(home_timeline); |
---|
| 793 | txl_free(mentions); |
---|
[2322a9f] | 794 | |
---|
| 795 | td->flags &= ~(TWITTER_DOING_TIMELINE | TWITTER_GOT_TIMELINE | TWITTER_GOT_MENTIONS); |
---|
[199fea6] | 796 | td->home_timeline_obj = td->mentions_obj = NULL; |
---|
[2322a9f] | 797 | } |
---|
| 798 | |
---|
| 799 | /** |
---|
| 800 | * Get the timeline. |
---|
| 801 | */ |
---|
| 802 | void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
| 803 | { |
---|
| 804 | struct twitter_data *td = ic->proto_data; |
---|
| 805 | |
---|
[199fea6] | 806 | txl_free(td->home_timeline_obj); |
---|
[2322a9f] | 807 | td->home_timeline_obj = NULL; |
---|
| 808 | td->flags &= ~TWITTER_GOT_TIMELINE; |
---|
| 809 | |
---|
[429a9b1] | 810 | char *args[6]; |
---|
[2322a9f] | 811 | args[0] = "cursor"; |
---|
| 812 | args[1] = g_strdup_printf("%lld", (long long) next_cursor); |
---|
[429a9b1] | 813 | args[2] = "include_entities"; |
---|
| 814 | args[3] = "true"; |
---|
[2322a9f] | 815 | if (td->timeline_id) { |
---|
[429a9b1] | 816 | args[4] = "since_id"; |
---|
| 817 | args[5] = g_strdup_printf("%llu", (long long unsigned int) td->timeline_id); |
---|
[2322a9f] | 818 | } |
---|
| 819 | |
---|
[90fc864] | 820 | if (twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args, |
---|
| 821 | td->timeline_id ? 6 : 4) == NULL) { |
---|
| 822 | if (++td->http_fails >= 5) |
---|
| 823 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 824 | TWITTER_HOME_TIMELINE_URL, "connection failed"); |
---|
| 825 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
| 826 | twitter_flush_timeline(ic); |
---|
| 827 | } |
---|
[2322a9f] | 828 | |
---|
| 829 | g_free(args[1]); |
---|
| 830 | if (td->timeline_id) { |
---|
[429a9b1] | 831 | g_free(args[5]); |
---|
[2322a9f] | 832 | } |
---|
| 833 | } |
---|
| 834 | |
---|
| 835 | /** |
---|
| 836 | * Get mentions. |
---|
| 837 | */ |
---|
| 838 | void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor) |
---|
| 839 | { |
---|
| 840 | struct twitter_data *td = ic->proto_data; |
---|
| 841 | |
---|
[199fea6] | 842 | txl_free(td->mentions_obj); |
---|
[2322a9f] | 843 | td->mentions_obj = NULL; |
---|
| 844 | td->flags &= ~TWITTER_GOT_MENTIONS; |
---|
| 845 | |
---|
[429a9b1] | 846 | char *args[6]; |
---|
[2322a9f] | 847 | args[0] = "cursor"; |
---|
| 848 | args[1] = g_strdup_printf("%lld", (long long) next_cursor); |
---|
[429a9b1] | 849 | args[2] = "include_entities"; |
---|
| 850 | args[3] = "true"; |
---|
[2322a9f] | 851 | if (td->timeline_id) { |
---|
[429a9b1] | 852 | args[4] = "since_id"; |
---|
| 853 | args[5] = g_strdup_printf("%llu", (long long unsigned int) td->timeline_id); |
---|
[b5fe39b] | 854 | } else { |
---|
| 855 | args[4] = "count"; |
---|
| 856 | args[5] = g_strdup_printf("%d", set_getint(&ic->acc->set, "show_old_mentions")); |
---|
[2322a9f] | 857 | } |
---|
| 858 | |
---|
[b5fe39b] | 859 | if (twitter_http(ic, TWITTER_MENTIONS_URL, twitter_http_get_mentions, |
---|
| 860 | ic, 0, args, 6) == NULL) { |
---|
[90fc864] | 861 | if (++td->http_fails >= 5) |
---|
| 862 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 863 | TWITTER_MENTIONS_URL, "connection failed"); |
---|
| 864 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
| 865 | twitter_flush_timeline(ic); |
---|
| 866 | } |
---|
[2322a9f] | 867 | |
---|
| 868 | g_free(args[1]); |
---|
| 869 | if (td->timeline_id) { |
---|
[429a9b1] | 870 | g_free(args[5]); |
---|
[2322a9f] | 871 | } |
---|
| 872 | } |
---|
| 873 | |
---|
[1b221e0] | 874 | /** |
---|
| 875 | * Callback for getting the home timeline. |
---|
| 876 | */ |
---|
| 877 | static void twitter_http_get_home_timeline(struct http_request *req) |
---|
| 878 | { |
---|
[62d2cfb] | 879 | struct im_connection *ic = req->data; |
---|
[37aa317] | 880 | struct twitter_data *td; |
---|
[d0752e8] | 881 | struct xt_node *parsed; |
---|
[1b221e0] | 882 | struct twitter_xml_list *txl; |
---|
[62d2cfb] | 883 | |
---|
| 884 | // Check if the connection is still active. |
---|
[5983eca] | 885 | if (!g_slist_find(twitter_connections, ic)) |
---|
[62d2cfb] | 886 | return; |
---|
[5983eca] | 887 | |
---|
[37aa317] | 888 | td = ic->proto_data; |
---|
[1b221e0] | 889 | |
---|
[2322a9f] | 890 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 891 | txl->list = NULL; |
---|
| 892 | |
---|
| 893 | // The root <statuses> node should hold the list of statuses <status> |
---|
[2a6da96] | 894 | if (!(parsed = twitter_parse_response(ic, req))) |
---|
| 895 | goto end; |
---|
[d0752e8] | 896 | twitter_xt_get_status_list(ic, parsed, txl); |
---|
[e08ae0c] | 897 | // json_value_free(parsed); |
---|
[2322a9f] | 898 | |
---|
| 899 | td->home_timeline_obj = txl; |
---|
| 900 | |
---|
| 901 | end: |
---|
| 902 | td->flags |= TWITTER_GOT_TIMELINE; |
---|
| 903 | |
---|
| 904 | twitter_flush_timeline(ic); |
---|
| 905 | } |
---|
| 906 | |
---|
| 907 | /** |
---|
| 908 | * Callback for getting mentions. |
---|
| 909 | */ |
---|
| 910 | static void twitter_http_get_mentions(struct http_request *req) |
---|
| 911 | { |
---|
| 912 | struct im_connection *ic = req->data; |
---|
| 913 | struct twitter_data *td; |
---|
[d0752e8] | 914 | struct xt_node *parsed; |
---|
[2322a9f] | 915 | struct twitter_xml_list *txl; |
---|
| 916 | |
---|
| 917 | // Check if the connection is still active. |
---|
| 918 | if (!g_slist_find(twitter_connections, ic)) |
---|
[1b221e0] | 919 | return; |
---|
[2322a9f] | 920 | |
---|
| 921 | td = ic->proto_data; |
---|
| 922 | |
---|
[1b221e0] | 923 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 924 | txl->list = NULL; |
---|
[62d2cfb] | 925 | |
---|
[1b221e0] | 926 | // The root <statuses> node should hold the list of statuses <status> |
---|
[2a6da96] | 927 | if (!(parsed = twitter_parse_response(ic, req))) |
---|
| 928 | goto end; |
---|
[d0752e8] | 929 | twitter_xt_get_status_list(ic, parsed, txl); |
---|
[e08ae0c] | 930 | // json_value_free(parsed); |
---|
[1b221e0] | 931 | |
---|
[2322a9f] | 932 | td->mentions_obj = txl; |
---|
[1b221e0] | 933 | |
---|
[2322a9f] | 934 | end: |
---|
| 935 | td->flags |= TWITTER_GOT_MENTIONS; |
---|
| 936 | |
---|
| 937 | twitter_flush_timeline(ic); |
---|
[1b221e0] | 938 | } |
---|
| 939 | |
---|
| 940 | /** |
---|
[de923d5] | 941 | * Callback to use after sending a POST request to twitter. |
---|
| 942 | * (Generic, used for a few kinds of queries.) |
---|
[1b221e0] | 943 | */ |
---|
[7d53efb] | 944 | static void twitter_http_post(struct http_request *req) |
---|
[1b221e0] | 945 | { |
---|
| 946 | struct im_connection *ic = req->data; |
---|
[7b87539] | 947 | struct twitter_data *td; |
---|
[2a6da96] | 948 | struct xt_node *parsed, *node; |
---|
[1b221e0] | 949 | |
---|
[62d2cfb] | 950 | // Check if the connection is still active. |
---|
[5983eca] | 951 | if (!g_slist_find(twitter_connections, ic)) |
---|
[62d2cfb] | 952 | return; |
---|
| 953 | |
---|
[7b87539] | 954 | td = ic->proto_data; |
---|
| 955 | td->last_status_id = 0; |
---|
[5983eca] | 956 | |
---|
[2a6da96] | 957 | if (!(parsed = twitter_parse_response(ic, req))) |
---|
[1b221e0] | 958 | return; |
---|
[2a6da96] | 959 | |
---|
| 960 | if ((node = xt_find_node(parsed, "status")) && |
---|
| 961 | (node = xt_find_node(node->children, "id")) && node->text) |
---|
| 962 | td->last_status_id = g_ascii_strtoull(node->text, NULL, 10); |
---|
[1b221e0] | 963 | } |
---|
| 964 | |
---|
| 965 | /** |
---|
| 966 | * Function to POST a new status to twitter. |
---|
[5983eca] | 967 | */ |
---|
[b890626] | 968 | void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to) |
---|
[1b221e0] | 969 | { |
---|
[5983eca] | 970 | char *args[4] = { |
---|
[b890626] | 971 | "status", msg, |
---|
| 972 | "in_reply_to_status_id", |
---|
| 973 | g_strdup_printf("%llu", (unsigned long long) in_reply_to) |
---|
| 974 | }; |
---|
| 975 | twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1, |
---|
[5983eca] | 976 | args, in_reply_to ? 4 : 2); |
---|
[b890626] | 977 | g_free(args[3]); |
---|
[1b221e0] | 978 | } |
---|
| 979 | |
---|
| 980 | |
---|
[62d2cfb] | 981 | /** |
---|
| 982 | * Function to POST a new message to twitter. |
---|
| 983 | */ |
---|
| 984 | void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) |
---|
| 985 | { |
---|
[5983eca] | 986 | char *args[4]; |
---|
[62d2cfb] | 987 | args[0] = "screen_name"; |
---|
| 988 | args[1] = who; |
---|
| 989 | args[2] = "text"; |
---|
| 990 | args[3] = msg; |
---|
| 991 | // Use the same callback as for twitter_post_status, since it does basically the same. |
---|
[ba3233c] | 992 | twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4); |
---|
[62d2cfb] | 993 | } |
---|
[7d53efb] | 994 | |
---|
| 995 | void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create) |
---|
| 996 | { |
---|
[5983eca] | 997 | char *args[2]; |
---|
[7d53efb] | 998 | args[0] = "screen_name"; |
---|
| 999 | args[1] = who; |
---|
[5983eca] | 1000 | twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL, |
---|
| 1001 | twitter_http_post, ic, 1, args, 2); |
---|
[a26af5c] | 1002 | } |
---|
[7b87539] | 1003 | |
---|
| 1004 | void twitter_status_destroy(struct im_connection *ic, guint64 id) |
---|
| 1005 | { |
---|
| 1006 | char *url; |
---|
[de923d5] | 1007 | url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_DESTROY_URL, |
---|
[3b4a22a] | 1008 | (unsigned long long) id, ".json"); |
---|
[7b87539] | 1009 | twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0); |
---|
| 1010 | g_free(url); |
---|
| 1011 | } |
---|
[b890626] | 1012 | |
---|
| 1013 | void twitter_status_retweet(struct im_connection *ic, guint64 id) |
---|
| 1014 | { |
---|
| 1015 | char *url; |
---|
[de923d5] | 1016 | url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_RETWEET_URL, |
---|
[3b4a22a] | 1017 | (unsigned long long) id, ".json"); |
---|
[b890626] | 1018 | twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0); |
---|
| 1019 | g_free(url); |
---|
| 1020 | } |
---|
[d18dee42] | 1021 | |
---|
| 1022 | /** |
---|
| 1023 | * Report a user for sending spam. |
---|
| 1024 | */ |
---|
| 1025 | void twitter_report_spam(struct im_connection *ic, char *screen_name) |
---|
| 1026 | { |
---|
| 1027 | char *args[2] = { |
---|
| 1028 | "screen_name", |
---|
| 1029 | NULL, |
---|
| 1030 | }; |
---|
| 1031 | args[1] = screen_name; |
---|
| 1032 | twitter_http(ic, TWITTER_REPORT_SPAM_URL, twitter_http_post, |
---|
| 1033 | ic, 1, args, 2); |
---|
| 1034 | } |
---|
[b61c74c] | 1035 | |
---|
| 1036 | /** |
---|
| 1037 | * Favourite a tweet. |
---|
| 1038 | */ |
---|
| 1039 | void twitter_favourite_tweet(struct im_connection *ic, guint64 id) |
---|
| 1040 | { |
---|
| 1041 | char *url; |
---|
| 1042 | url = g_strdup_printf("%s%llu%s", TWITTER_FAVORITE_CREATE_URL, |
---|
[3b4a22a] | 1043 | (unsigned long long) id, ".json"); |
---|
[b61c74c] | 1044 | twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0); |
---|
| 1045 | g_free(url); |
---|
| 1046 | } |
---|