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