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