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