[1b221e0] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Simple module to facilitate twitter functionality. * |
---|
| 5 | * * |
---|
| 6 | * Copyright 2009 Geert Mulders <g.c.w.m.mulders@gmail.com> * |
---|
| 7 | * * |
---|
| 8 | * This library is free software; you can redistribute it and/or * |
---|
| 9 | * modify it under the terms of the GNU Lesser General Public * |
---|
| 10 | * License as published by the Free Software Foundation, version * |
---|
| 11 | * 2.1. * |
---|
| 12 | * * |
---|
| 13 | * This library is distributed in the hope that it will be useful, * |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
| 16 | * Lesser General Public License for more details. * |
---|
| 17 | * * |
---|
| 18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
| 19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
---|
| 21 | * * |
---|
| 22 | ****************************************************************************/ |
---|
| 23 | |
---|
[08579a1] | 24 | /* For strptime(): */ |
---|
[daae10f] | 25 | #if(__sun) |
---|
| 26 | #else |
---|
[08579a1] | 27 | #define _XOPEN_SOURCE |
---|
[daae10f] | 28 | #endif |
---|
[08579a1] | 29 | |
---|
[1b221e0] | 30 | #include "twitter_http.h" |
---|
| 31 | #include "twitter.h" |
---|
| 32 | #include "bitlbee.h" |
---|
| 33 | #include "url.h" |
---|
| 34 | #include "misc.h" |
---|
| 35 | #include "base64.h" |
---|
| 36 | #include "xmltree.h" |
---|
| 37 | #include "twitter_lib.h" |
---|
| 38 | #include <ctype.h> |
---|
| 39 | #include <errno.h> |
---|
| 40 | |
---|
[e4e0b37] | 41 | /* GLib < 2.12.0 doesn't have g_ascii_strtoll(), work around using system strtoll(). */ |
---|
| 42 | /* GLib < 2.12.4 can be buggy: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=488013 */ |
---|
| 43 | #if !GLIB_CHECK_VERSION(2,12,5) |
---|
| 44 | #include <stdlib.h> |
---|
| 45 | #include <limits.h> |
---|
| 46 | #define g_ascii_strtoll strtoll |
---|
| 47 | #endif |
---|
| 48 | |
---|
[1b221e0] | 49 | #define TXL_STATUS 1 |
---|
[62d2cfb] | 50 | #define TXL_USER 2 |
---|
| 51 | #define TXL_ID 3 |
---|
| 52 | |
---|
[1b221e0] | 53 | struct twitter_xml_list { |
---|
[62d2cfb] | 54 | int type; |
---|
[8203da9] | 55 | gint64 next_cursor; |
---|
[1b221e0] | 56 | GSList *list; |
---|
| 57 | }; |
---|
| 58 | |
---|
| 59 | struct twitter_xml_user { |
---|
| 60 | char *name; |
---|
| 61 | char *screen_name; |
---|
| 62 | }; |
---|
| 63 | |
---|
| 64 | struct twitter_xml_status { |
---|
[08579a1] | 65 | time_t created_at; |
---|
[1b221e0] | 66 | char *text; |
---|
| 67 | struct twitter_xml_user *user; |
---|
[ce81acd] | 68 | guint64 id, reply_to; |
---|
[1b221e0] | 69 | }; |
---|
| 70 | |
---|
[d6aa6dd] | 71 | static void twitter_groupchat_init(struct im_connection *ic); |
---|
| 72 | |
---|
[62d2cfb] | 73 | /** |
---|
| 74 | * Frees a twitter_xml_user struct. |
---|
| 75 | */ |
---|
| 76 | static void txu_free(struct twitter_xml_user *txu) |
---|
| 77 | { |
---|
[a26af5c] | 78 | if (txu == NULL) |
---|
| 79 | return; |
---|
[62d2cfb] | 80 | g_free(txu->name); |
---|
| 81 | g_free(txu->screen_name); |
---|
[2abceca] | 82 | g_free(txu); |
---|
[62d2cfb] | 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * Frees a twitter_xml_status struct. |
---|
| 88 | */ |
---|
| 89 | static void txs_free(struct twitter_xml_status *txs) |
---|
| 90 | { |
---|
| 91 | g_free(txs->text); |
---|
| 92 | txu_free(txs->user); |
---|
[2abceca] | 93 | g_free(txs); |
---|
[62d2cfb] | 94 | } |
---|
| 95 | |
---|
| 96 | /** |
---|
| 97 | * Free a twitter_xml_list struct. |
---|
| 98 | * type is the type of list the struct holds. |
---|
| 99 | */ |
---|
| 100 | static void txl_free(struct twitter_xml_list *txl) |
---|
| 101 | { |
---|
| 102 | GSList *l; |
---|
[a26af5c] | 103 | if (txl == NULL) |
---|
| 104 | return; |
---|
[5983eca] | 105 | for (l = txl->list; l; l = g_slist_next(l)) |
---|
[62d2cfb] | 106 | if (txl->type == TXL_STATUS) |
---|
[5983eca] | 107 | txs_free((struct twitter_xml_status *) l->data); |
---|
[62d2cfb] | 108 | else if (txl->type == TXL_ID) |
---|
| 109 | g_free(l->data); |
---|
[de923d5] | 110 | else if (txl->type == TXL_USER) |
---|
| 111 | txu_free(l->data); |
---|
[62d2cfb] | 112 | g_slist_free(txl->list); |
---|
[fd65edb] | 113 | g_free(txl); |
---|
[62d2cfb] | 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * Add a buddy if it is not allready added, set the status to logged in. |
---|
| 118 | */ |
---|
[3e69802] | 119 | static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname) |
---|
[62d2cfb] | 120 | { |
---|
[1014cab] | 121 | struct twitter_data *td = ic->proto_data; |
---|
| 122 | |
---|
[de923d5] | 123 | // Check if the buddy is already in the buddy list. |
---|
[5983eca] | 124 | if (!bee_user_by_handle(ic->bee, ic, name)) { |
---|
[e88fbe27] | 125 | char *mode = set_getstr(&ic->acc->set, "mode"); |
---|
[5983eca] | 126 | |
---|
[62d2cfb] | 127 | // The buddy is not in the list, add the buddy and set the status to logged in. |
---|
[5983eca] | 128 | imcb_add_buddy(ic, name, NULL); |
---|
| 129 | imcb_rename_buddy(ic, name, fullname); |
---|
| 130 | if (g_strcasecmp(mode, "chat") == 0) { |
---|
[5c18a76] | 131 | /* Necessary so that nicks always get translated to the |
---|
| 132 | exact Twitter username. */ |
---|
[5983eca] | 133 | imcb_buddy_nick_hint(ic, name, name); |
---|
| 134 | imcb_chat_add_buddy(td->home_timeline_gc, name); |
---|
| 135 | } else if (g_strcasecmp(mode, "many") == 0) |
---|
| 136 | imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); |
---|
[62d2cfb] | 137 | } |
---|
| 138 | } |
---|
[1b221e0] | 139 | |
---|
[a7b9ec7] | 140 | /* Warning: May return a malloc()ed value, which will be free()d on the next |
---|
[de923d5] | 141 | call. Only for short-term use. NOT THREADSAFE! */ |
---|
[6eca2eb] | 142 | char *twitter_parse_error(struct http_request *req) |
---|
[a7b9ec7] | 143 | { |
---|
| 144 | static char *ret = NULL; |
---|
[3d93aed] | 145 | struct xt_parser *xp = NULL; |
---|
[c0f33f1] | 146 | struct xt_node *node, *err; |
---|
[5983eca] | 147 | |
---|
[a7b9ec7] | 148 | g_free(ret); |
---|
| 149 | ret = NULL; |
---|
[5983eca] | 150 | |
---|
| 151 | if (req->body_size > 0) { |
---|
[3d93aed] | 152 | xp = xt_new(NULL, NULL); |
---|
| 153 | xt_feed(xp, req->reply_body, req->body_size); |
---|
[de923d5] | 154 | |
---|
| 155 | for (node = xp->root; node; node = node->next) |
---|
[c0f33f1] | 156 | if ((err = xt_find_node(node->children, "error")) && err->text_len > 0) { |
---|
| 157 | ret = g_strdup_printf("%s (%s)", req->status_string, err->text); |
---|
[de923d5] | 158 | break; |
---|
| 159 | } |
---|
[5983eca] | 160 | |
---|
[a7b9ec7] | 161 | xt_free(xp); |
---|
| 162 | } |
---|
[5983eca] | 163 | |
---|
[6eca2eb] | 164 | return ret ? ret : req->status_string; |
---|
[a7b9ec7] | 165 | } |
---|
| 166 | |
---|
[1b221e0] | 167 | static void twitter_http_get_friends_ids(struct http_request *req); |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * Get the friends ids. |
---|
| 171 | */ |
---|
[8203da9] | 172 | void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor) |
---|
[1b221e0] | 173 | { |
---|
[5983eca] | 174 | // Primitive, but hey! It works... |
---|
| 175 | char *args[2]; |
---|
[1b221e0] | 176 | args[0] = "cursor"; |
---|
[5983eca] | 177 | args[1] = g_strdup_printf("%lld", (long long) next_cursor); |
---|
[bb5ce4d1] | 178 | twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2); |
---|
[1b221e0] | 179 | |
---|
| 180 | g_free(args[1]); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * Function to help fill a list. |
---|
| 185 | */ |
---|
[5983eca] | 186 | static xt_status twitter_xt_next_cursor(struct xt_node *node, struct twitter_xml_list *txl) |
---|
[1b221e0] | 187 | { |
---|
[bd64716] | 188 | char *end = NULL; |
---|
[5983eca] | 189 | |
---|
| 190 | if (node->text) |
---|
| 191 | txl->next_cursor = g_ascii_strtoll(node->text, &end, 10); |
---|
| 192 | if (end == NULL) |
---|
[bd64716] | 193 | txl->next_cursor = -1; |
---|
[1b221e0] | 194 | |
---|
| 195 | return XT_HANDLED; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | * Fill a list of ids. |
---|
| 200 | */ |
---|
[5983eca] | 201 | static xt_status twitter_xt_get_friends_id_list(struct xt_node *node, struct twitter_xml_list *txl) |
---|
[1b221e0] | 202 | { |
---|
| 203 | struct xt_node *child; |
---|
[5983eca] | 204 | |
---|
[62d2cfb] | 205 | // Set the list type. |
---|
| 206 | txl->type = TXL_ID; |
---|
[1b221e0] | 207 | |
---|
| 208 | // The root <statuses> node should hold the list of statuses <status> |
---|
| 209 | // Walk over the nodes children. |
---|
[5983eca] | 210 | for (child = node->children; child; child = child->next) { |
---|
[de923d5] | 211 | if (g_strcasecmp("ids", child->name) == 0) { |
---|
| 212 | struct xt_node *idc; |
---|
| 213 | for (idc = child->children; idc; idc = idc->next) |
---|
| 214 | if (g_strcasecmp(idc->name, "id") == 0) |
---|
| 215 | txl->list = g_slist_prepend(txl->list, |
---|
| 216 | g_memdup(idc->text, idc->text_len + 1)); |
---|
[5983eca] | 217 | } else if (g_strcasecmp("next_cursor", child->name) == 0) { |
---|
[1b221e0] | 218 | twitter_xt_next_cursor(child, txl); |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | return XT_HANDLED; |
---|
| 223 | } |
---|
| 224 | |
---|
[de923d5] | 225 | static void twitter_get_users_lookup(struct im_connection *ic); |
---|
| 226 | |
---|
[1b221e0] | 227 | /** |
---|
| 228 | * Callback for getting the friends ids. |
---|
| 229 | */ |
---|
| 230 | static void twitter_http_get_friends_ids(struct http_request *req) |
---|
| 231 | { |
---|
| 232 | struct im_connection *ic; |
---|
| 233 | struct xt_parser *parser; |
---|
| 234 | struct twitter_xml_list *txl; |
---|
[3bd4a93] | 235 | struct twitter_data *td; |
---|
[1b221e0] | 236 | |
---|
| 237 | ic = req->data; |
---|
| 238 | |
---|
[62d2cfb] | 239 | // Check if the connection is still active. |
---|
[5983eca] | 240 | if (!g_slist_find(twitter_connections, ic)) |
---|
[62d2cfb] | 241 | return; |
---|
[5983eca] | 242 | |
---|
[37aa317] | 243 | td = ic->proto_data; |
---|
[62d2cfb] | 244 | |
---|
[de923d5] | 245 | // Check if the HTTP request went well. More strict checks as this is |
---|
| 246 | // the first request we do in a session. |
---|
| 247 | if (req->status_code == 401) { |
---|
| 248 | imcb_error(ic, "Authentication failure"); |
---|
| 249 | imc_logout(ic, FALSE); |
---|
| 250 | return; |
---|
| 251 | } else if (req->status_code != 200) { |
---|
[1b221e0] | 252 | // It didn't go well, output the error and return. |
---|
[de923d5] | 253 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 254 | TWITTER_FRIENDS_IDS_URL, twitter_parse_error(req)); |
---|
| 255 | imc_logout(ic, TRUE); |
---|
[1b221e0] | 256 | return; |
---|
[3bd4a93] | 257 | } else { |
---|
| 258 | td->http_fails = 0; |
---|
[1b221e0] | 259 | } |
---|
| 260 | |
---|
[de923d5] | 261 | /* Create the room now that we "logged in". */ |
---|
| 262 | if (!td->home_timeline_gc && g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0) |
---|
| 263 | twitter_groupchat_init(ic); |
---|
| 264 | |
---|
[1b221e0] | 265 | txl = g_new0(struct twitter_xml_list, 1); |
---|
[de923d5] | 266 | txl->list = td->follow_ids; |
---|
[1b221e0] | 267 | |
---|
| 268 | // Parse the data. |
---|
[5983eca] | 269 | parser = xt_new(NULL, txl); |
---|
| 270 | xt_feed(parser, req->reply_body, req->body_size); |
---|
[1b221e0] | 271 | twitter_xt_get_friends_id_list(parser->root, txl); |
---|
[5983eca] | 272 | xt_free(parser); |
---|
[1b221e0] | 273 | |
---|
[de923d5] | 274 | td->follow_ids = txl->list; |
---|
[1b221e0] | 275 | if (txl->next_cursor) |
---|
[de923d5] | 276 | /* These were just numbers. Up to 4000 in a response AFAIK so if we get here |
---|
| 277 | we may be using a spammer account. \o/ */ |
---|
[1b221e0] | 278 | twitter_get_friends_ids(ic, txl->next_cursor); |
---|
[de923d5] | 279 | else |
---|
| 280 | /* Now to convert all those numbers into names.. */ |
---|
| 281 | twitter_get_users_lookup(ic); |
---|
| 282 | |
---|
| 283 | txl->list = NULL; |
---|
| 284 | txl_free(txl); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | static xt_status twitter_xt_get_users(struct xt_node *node, struct twitter_xml_list *txl); |
---|
| 288 | static void twitter_http_get_users_lookup(struct http_request *req); |
---|
| 289 | |
---|
| 290 | static void twitter_get_users_lookup(struct im_connection *ic) |
---|
| 291 | { |
---|
| 292 | struct twitter_data *td = ic->proto_data; |
---|
| 293 | char *args[2] = { |
---|
| 294 | "user_id", |
---|
| 295 | NULL, |
---|
| 296 | }; |
---|
| 297 | GString *ids = g_string_new(""); |
---|
| 298 | int i; |
---|
| 299 | |
---|
| 300 | /* We can request up to 100 users at a time. */ |
---|
| 301 | for (i = 0; i < 100 && td->follow_ids; i ++) { |
---|
| 302 | g_string_append_printf(ids, ",%s", (char*) td->follow_ids->data); |
---|
| 303 | g_free(td->follow_ids->data); |
---|
| 304 | td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data); |
---|
| 305 | } |
---|
| 306 | if (ids->len > 0) { |
---|
| 307 | args[1] = ids->str + 1; |
---|
| 308 | /* POST, because I think ids can be up to 1KB long. */ |
---|
| 309 | twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2); |
---|
| 310 | } else { |
---|
| 311 | /* We have all users. Continue with login. (Get statuses.) */ |
---|
| 312 | td->flags |= TWITTER_HAVE_FRIENDS; |
---|
| 313 | twitter_login_finish(ic); |
---|
| 314 | } |
---|
| 315 | g_string_free(ids, TRUE); |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | /** |
---|
| 319 | * Callback for getting (twitter)friends... |
---|
| 320 | * |
---|
| 321 | * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has |
---|
| 322 | * hundreds of friends?" you wonder? You probably not, since you are reading the source of |
---|
| 323 | * BitlBee... Get a life and meet new people! |
---|
| 324 | */ |
---|
| 325 | static void twitter_http_get_users_lookup(struct http_request *req) |
---|
| 326 | { |
---|
| 327 | struct im_connection *ic = req->data; |
---|
| 328 | struct twitter_data *td; |
---|
| 329 | struct xt_parser *parser; |
---|
| 330 | struct twitter_xml_list *txl; |
---|
| 331 | GSList *l = NULL; |
---|
| 332 | struct twitter_xml_user *user; |
---|
| 333 | |
---|
| 334 | // Check if the connection is still active. |
---|
| 335 | if (!g_slist_find(twitter_connections, ic)) |
---|
| 336 | return; |
---|
| 337 | |
---|
| 338 | td = ic->proto_data; |
---|
| 339 | |
---|
| 340 | if (req->status_code != 200) { |
---|
| 341 | // It didn't go well, output the error and return. |
---|
| 342 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 343 | TWITTER_USERS_LOOKUP_URL, twitter_parse_error(req)); |
---|
| 344 | imc_logout(ic, TRUE); |
---|
| 345 | return; |
---|
| 346 | } else { |
---|
| 347 | td->http_fails = 0; |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 351 | txl->list = NULL; |
---|
[1b221e0] | 352 | |
---|
[de923d5] | 353 | // Parse the data. |
---|
| 354 | parser = xt_new(NULL, txl); |
---|
| 355 | xt_feed(parser, req->reply_body, req->body_size); |
---|
| 356 | |
---|
| 357 | // Get the user list from the parsed xml feed. |
---|
| 358 | twitter_xt_get_users(parser->root, txl); |
---|
| 359 | xt_free(parser); |
---|
| 360 | |
---|
| 361 | // Add the users as buddies. |
---|
| 362 | for (l = txl->list; l; l = g_slist_next(l)) { |
---|
| 363 | user = l->data; |
---|
| 364 | twitter_add_buddy(ic, user->screen_name, user->name); |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | // Free the structure. |
---|
[62d2cfb] | 368 | txl_free(txl); |
---|
[de923d5] | 369 | |
---|
| 370 | twitter_get_users_lookup(ic); |
---|
[1b221e0] | 371 | } |
---|
| 372 | |
---|
| 373 | /** |
---|
| 374 | * Function to fill a twitter_xml_user struct. |
---|
| 375 | * It sets: |
---|
| 376 | * - the name and |
---|
| 377 | * - the screen_name. |
---|
| 378 | */ |
---|
[5983eca] | 379 | static xt_status twitter_xt_get_user(struct xt_node *node, struct twitter_xml_user *txu) |
---|
[1b221e0] | 380 | { |
---|
| 381 | struct xt_node *child; |
---|
| 382 | |
---|
| 383 | // Walk over the nodes children. |
---|
[5983eca] | 384 | for (child = node->children; child; child = child->next) { |
---|
| 385 | if (g_strcasecmp("name", child->name) == 0) { |
---|
| 386 | txu->name = g_memdup(child->text, child->text_len + 1); |
---|
| 387 | } else if (g_strcasecmp("screen_name", child->name) == 0) { |
---|
| 388 | txu->screen_name = g_memdup(child->text, child->text_len + 1); |
---|
[1b221e0] | 389 | } |
---|
| 390 | } |
---|
| 391 | return XT_HANDLED; |
---|
| 392 | } |
---|
| 393 | |
---|
[62d2cfb] | 394 | /** |
---|
| 395 | * Function to fill a twitter_xml_list struct. |
---|
| 396 | * It sets: |
---|
| 397 | * - all <user>s from the <users> element. |
---|
| 398 | */ |
---|
[5983eca] | 399 | static xt_status twitter_xt_get_users(struct xt_node *node, struct twitter_xml_list *txl) |
---|
[62d2cfb] | 400 | { |
---|
| 401 | struct twitter_xml_user *txu; |
---|
| 402 | struct xt_node *child; |
---|
| 403 | |
---|
| 404 | // Set the type of the list. |
---|
| 405 | txl->type = TXL_USER; |
---|
| 406 | |
---|
| 407 | // The root <users> node should hold the list of users <user> |
---|
| 408 | // Walk over the nodes children. |
---|
[5983eca] | 409 | for (child = node->children; child; child = child->next) { |
---|
| 410 | if (g_strcasecmp("user", child->name) == 0) { |
---|
[62d2cfb] | 411 | txu = g_new0(struct twitter_xml_user, 1); |
---|
| 412 | twitter_xt_get_user(child, txu); |
---|
| 413 | // Put the item in the front of the list. |
---|
[5983eca] | 414 | txl->list = g_slist_prepend(txl->list, txu); |
---|
[62d2cfb] | 415 | } |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | return XT_HANDLED; |
---|
| 419 | } |
---|
| 420 | |
---|
[2b02617] | 421 | #ifdef __GLIBC__ |
---|
| 422 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y" |
---|
| 423 | #else |
---|
| 424 | #define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y" |
---|
| 425 | #endif |
---|
[62d2cfb] | 426 | |
---|
[1b221e0] | 427 | /** |
---|
| 428 | * Function to fill a twitter_xml_status struct. |
---|
| 429 | * It sets: |
---|
| 430 | * - the status text and |
---|
| 431 | * - the created_at timestamp and |
---|
| 432 | * - the status id and |
---|
| 433 | * - the user in a twitter_xml_user struct. |
---|
| 434 | */ |
---|
[5983eca] | 435 | static xt_status twitter_xt_get_status(struct xt_node *node, struct twitter_xml_status *txs) |
---|
[1b221e0] | 436 | { |
---|
[bd599b9] | 437 | struct xt_node *child, *rt = NULL; |
---|
[e193aeb] | 438 | gboolean truncated = FALSE; |
---|
[1b221e0] | 439 | |
---|
| 440 | // Walk over the nodes children. |
---|
[5983eca] | 441 | for (child = node->children; child; child = child->next) { |
---|
| 442 | if (g_strcasecmp("text", child->name) == 0) { |
---|
| 443 | txs->text = g_memdup(child->text, child->text_len + 1); |
---|
| 444 | } else if (g_strcasecmp("truncated", child->name) == 0 && child->text) { |
---|
[e193aeb] | 445 | truncated = bool2int(child->text); |
---|
[5983eca] | 446 | } else if (g_strcasecmp("retweeted_status", child->name) == 0) { |
---|
[e193aeb] | 447 | rt = child; |
---|
[5983eca] | 448 | } else if (g_strcasecmp("created_at", child->name) == 0) { |
---|
[08579a1] | 449 | struct tm parsed; |
---|
[5983eca] | 450 | |
---|
[08579a1] | 451 | /* Very sensitive to changes to the formatting of |
---|
| 452 | this field. :-( Also assumes the timezone used |
---|
| 453 | is UTC since C time handling functions suck. */ |
---|
[5983eca] | 454 | if (strptime(child->text, TWITTER_TIME_FORMAT, &parsed) != NULL) |
---|
| 455 | txs->created_at = mktime_utc(&parsed); |
---|
| 456 | } else if (g_strcasecmp("user", child->name) == 0) { |
---|
[1b221e0] | 457 | txs->user = g_new0(struct twitter_xml_user, 1); |
---|
[5983eca] | 458 | twitter_xt_get_user(child, txs->user); |
---|
| 459 | } else if (g_strcasecmp("id", child->name) == 0) { |
---|
| 460 | txs->id = g_ascii_strtoull(child->text, NULL, 10); |
---|
| 461 | } else if (g_strcasecmp("in_reply_to_status_id", child->name) == 0) { |
---|
| 462 | txs->reply_to = g_ascii_strtoull(child->text, NULL, 10); |
---|
[ce81acd] | 463 | } |
---|
[1b221e0] | 464 | } |
---|
[5983eca] | 465 | |
---|
[e193aeb] | 466 | /* If it's a truncated retweet, get the original because dots suck. */ |
---|
[5983eca] | 467 | if (truncated && rt) { |
---|
[e193aeb] | 468 | struct twitter_xml_status *rtxs = g_new0(struct twitter_xml_status, 1); |
---|
[5983eca] | 469 | if (twitter_xt_get_status(rt, rtxs) != XT_HANDLED) { |
---|
[e193aeb] | 470 | txs_free(rtxs); |
---|
| 471 | return XT_HANDLED; |
---|
| 472 | } |
---|
[5983eca] | 473 | |
---|
[e193aeb] | 474 | g_free(txs->text); |
---|
| 475 | txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text); |
---|
| 476 | txs_free(rtxs); |
---|
| 477 | } |
---|
[5983eca] | 478 | |
---|
[1b221e0] | 479 | return XT_HANDLED; |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | /** |
---|
| 483 | * Function to fill a twitter_xml_list struct. |
---|
| 484 | * It sets: |
---|
| 485 | * - all <status>es within the <status> element and |
---|
| 486 | * - the next_cursor. |
---|
| 487 | */ |
---|
[5983eca] | 488 | static xt_status twitter_xt_get_status_list(struct im_connection *ic, struct xt_node *node, |
---|
| 489 | struct twitter_xml_list *txl) |
---|
[1b221e0] | 490 | { |
---|
| 491 | struct twitter_xml_status *txs; |
---|
| 492 | struct xt_node *child; |
---|
[203a2d2] | 493 | bee_user_t *bu; |
---|
[1b221e0] | 494 | |
---|
[62d2cfb] | 495 | // Set the type of the list. |
---|
| 496 | txl->type = TXL_STATUS; |
---|
| 497 | |
---|
[1b221e0] | 498 | // The root <statuses> node should hold the list of statuses <status> |
---|
| 499 | // Walk over the nodes children. |
---|
[5983eca] | 500 | for (child = node->children; child; child = child->next) { |
---|
| 501 | if (g_strcasecmp("status", child->name) == 0) { |
---|
[1b221e0] | 502 | txs = g_new0(struct twitter_xml_status, 1); |
---|
| 503 | twitter_xt_get_status(child, txs); |
---|
| 504 | // Put the item in the front of the list. |
---|
[5983eca] | 505 | txl->list = g_slist_prepend(txl->list, txs); |
---|
| 506 | |
---|
[203a2d2] | 507 | if (txs->user && txs->user->screen_name && |
---|
[5983eca] | 508 | (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) { |
---|
[203a2d2] | 509 | struct twitter_user_data *tud = bu->data; |
---|
[5983eca] | 510 | |
---|
| 511 | if (txs->id > tud->last_id) { |
---|
[203a2d2] | 512 | tud->last_id = txs->id; |
---|
| 513 | tud->last_time = txs->created_at; |
---|
| 514 | } |
---|
| 515 | } |
---|
[5983eca] | 516 | } else if (g_strcasecmp("next_cursor", child->name) == 0) { |
---|
[1b221e0] | 517 | twitter_xt_next_cursor(child, txl); |
---|
| 518 | } |
---|
| 519 | } |
---|
| 520 | |
---|
| 521 | return XT_HANDLED; |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | static void twitter_http_get_home_timeline(struct http_request *req); |
---|
| 525 | |
---|
| 526 | /** |
---|
| 527 | * Get the timeline. |
---|
| 528 | */ |
---|
[8203da9] | 529 | void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor) |
---|
[1b221e0] | 530 | { |
---|
| 531 | struct twitter_data *td = ic->proto_data; |
---|
| 532 | |
---|
[5983eca] | 533 | char *args[4]; |
---|
[1b221e0] | 534 | args[0] = "cursor"; |
---|
[5983eca] | 535 | args[1] = g_strdup_printf("%lld", (long long) next_cursor); |
---|
[1b221e0] | 536 | if (td->home_timeline_id) { |
---|
| 537 | args[2] = "since_id"; |
---|
[5983eca] | 538 | args[3] = g_strdup_printf("%llu", (long long unsigned int) td->home_timeline_id); |
---|
[1b221e0] | 539 | } |
---|
| 540 | |
---|
[5983eca] | 541 | twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args, |
---|
| 542 | td->home_timeline_id ? 4 : 2); |
---|
[1b221e0] | 543 | |
---|
| 544 | g_free(args[1]); |
---|
| 545 | if (td->home_timeline_id) { |
---|
| 546 | g_free(args[3]); |
---|
| 547 | } |
---|
| 548 | } |
---|
| 549 | |
---|
[ce81acd] | 550 | static char *twitter_msg_add_id(struct im_connection *ic, |
---|
[5983eca] | 551 | struct twitter_xml_status *txs, const char *prefix) |
---|
[ce81acd] | 552 | { |
---|
| 553 | struct twitter_data *td = ic->proto_data; |
---|
| 554 | char *ret = NULL; |
---|
[5983eca] | 555 | |
---|
| 556 | if (!set_getbool(&ic->acc->set, "show_ids")) { |
---|
[ce81acd] | 557 | if (*prefix) |
---|
| 558 | return g_strconcat(prefix, txs->text, NULL); |
---|
| 559 | else |
---|
| 560 | return NULL; |
---|
| 561 | } |
---|
[5983eca] | 562 | |
---|
[ce81acd] | 563 | td->log[td->log_id].id = txs->id; |
---|
| 564 | td->log[td->log_id].bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name); |
---|
[5983eca] | 565 | if (txs->reply_to) { |
---|
[ce81acd] | 566 | int i; |
---|
[5983eca] | 567 | for (i = 0; i < TWITTER_LOG_LENGTH; i++) |
---|
| 568 | if (td->log[i].id == txs->reply_to) { |
---|
| 569 | ret = g_strdup_printf("\002[\002%02d->%02d\002]\002 %s%s", |
---|
| 570 | td->log_id, i, prefix, txs->text); |
---|
[ce81acd] | 571 | break; |
---|
| 572 | } |
---|
| 573 | } |
---|
| 574 | if (ret == NULL) |
---|
[5983eca] | 575 | ret = g_strdup_printf("\002[\002%02d\002]\002 %s%s", td->log_id, prefix, txs->text); |
---|
[ce81acd] | 576 | td->log_id = (td->log_id + 1) % TWITTER_LOG_LENGTH; |
---|
[5983eca] | 577 | |
---|
[ce81acd] | 578 | return ret; |
---|
| 579 | } |
---|
| 580 | |
---|
[d6aa6dd] | 581 | static void twitter_groupchat_init(struct im_connection *ic) |
---|
| 582 | { |
---|
| 583 | char *name_hint; |
---|
| 584 | struct groupchat *gc; |
---|
| 585 | struct twitter_data *td = ic->proto_data; |
---|
[fd65edb] | 586 | GSList *l; |
---|
[5983eca] | 587 | |
---|
| 588 | td->home_timeline_gc = gc = imcb_chat_new(ic, "home/timeline"); |
---|
| 589 | |
---|
| 590 | name_hint = g_strdup_printf("%s_%s", td->prefix, ic->acc->user); |
---|
| 591 | imcb_chat_name_hint(gc, name_hint); |
---|
| 592 | g_free(name_hint); |
---|
| 593 | |
---|
| 594 | for (l = ic->bee->users; l; l = l->next) { |
---|
[fd65edb] | 595 | bee_user_t *bu = l->data; |
---|
[5983eca] | 596 | if (bu->ic == ic) |
---|
| 597 | imcb_chat_add_buddy(td->home_timeline_gc, bu->handle); |
---|
[fd65edb] | 598 | } |
---|
[d6aa6dd] | 599 | } |
---|
| 600 | |
---|
[62d2cfb] | 601 | /** |
---|
| 602 | * Function that is called to see the statuses in a groupchat window. |
---|
| 603 | */ |
---|
[5983eca] | 604 | static void twitter_groupchat(struct im_connection *ic, GSList * list) |
---|
[62d2cfb] | 605 | { |
---|
| 606 | struct twitter_data *td = ic->proto_data; |
---|
| 607 | GSList *l = NULL; |
---|
| 608 | struct twitter_xml_status *status; |
---|
| 609 | struct groupchat *gc; |
---|
| 610 | |
---|
| 611 | // Create a new groupchat if it does not exsist. |
---|
| 612 | if (!td->home_timeline_gc) |
---|
[d6aa6dd] | 613 | twitter_groupchat_init(ic); |
---|
[5983eca] | 614 | |
---|
[d6aa6dd] | 615 | gc = td->home_timeline_gc; |
---|
| 616 | if (!gc->joined) |
---|
[5983eca] | 617 | imcb_chat_add_buddy(gc, ic->acc->user); |
---|
[62d2cfb] | 618 | |
---|
[5983eca] | 619 | for (l = list; l; l = g_slist_next(l)) { |
---|
[ce81acd] | 620 | char *msg; |
---|
[5983eca] | 621 | |
---|
[62d2cfb] | 622 | status = l->data; |
---|
[a26af5c] | 623 | if (status->user == NULL || status->text == NULL) |
---|
| 624 | continue; |
---|
| 625 | |
---|
[3e69802] | 626 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
[5983eca] | 627 | |
---|
[0b3ffb1] | 628 | strip_html(status->text); |
---|
[ce81acd] | 629 | msg = twitter_msg_add_id(ic, status, ""); |
---|
[5983eca] | 630 | |
---|
[62d2cfb] | 631 | // Say it! |
---|
[d569019] | 632 | if (g_strcasecmp(td->user, status->user->screen_name) == 0) |
---|
[ce81acd] | 633 | imcb_chat_log(gc, "You: %s", msg ? msg : status->text); |
---|
[d569019] | 634 | else |
---|
[ce81acd] | 635 | imcb_chat_msg(gc, status->user->screen_name, |
---|
[5983eca] | 636 | msg ? msg : status->text, 0, status->created_at); |
---|
| 637 | |
---|
[ce81acd] | 638 | g_free(msg); |
---|
[5983eca] | 639 | |
---|
[62d2cfb] | 640 | // Update the home_timeline_id to hold the highest id, so that by the next request |
---|
[ce81acd] | 641 | // we won't pick up the updates already in the list. |
---|
| 642 | td->home_timeline_id = MAX(td->home_timeline_id, status->id); |
---|
[62d2cfb] | 643 | } |
---|
| 644 | } |
---|
| 645 | |
---|
| 646 | /** |
---|
| 647 | * Function that is called to see statuses as private messages. |
---|
| 648 | */ |
---|
[5983eca] | 649 | static void twitter_private_message_chat(struct im_connection *ic, GSList * list) |
---|
[62d2cfb] | 650 | { |
---|
| 651 | struct twitter_data *td = ic->proto_data; |
---|
| 652 | GSList *l = NULL; |
---|
| 653 | struct twitter_xml_status *status; |
---|
[e88fbe27] | 654 | char from[MAX_STRING]; |
---|
| 655 | gboolean mode_one; |
---|
[62d2cfb] | 656 | |
---|
[5983eca] | 657 | mode_one = g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "one") == 0; |
---|
| 658 | |
---|
| 659 | if (mode_one) { |
---|
| 660 | g_snprintf(from, sizeof(from) - 1, "%s_%s", td->prefix, ic->acc->user); |
---|
| 661 | from[MAX_STRING - 1] = '\0'; |
---|
[e88fbe27] | 662 | } |
---|
[5983eca] | 663 | |
---|
| 664 | for (l = list; l; l = g_slist_next(l)) { |
---|
[ce81acd] | 665 | char *prefix = NULL, *text = NULL; |
---|
[5983eca] | 666 | |
---|
[62d2cfb] | 667 | status = l->data; |
---|
[5983eca] | 668 | |
---|
| 669 | strip_html(status->text); |
---|
| 670 | if (mode_one) |
---|
[ce81acd] | 671 | prefix = g_strdup_printf("\002<\002%s\002>\002 ", |
---|
[5983eca] | 672 | status->user->screen_name); |
---|
[55b1e69] | 673 | else |
---|
| 674 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
[5983eca] | 675 | |
---|
[ce81acd] | 676 | text = twitter_msg_add_id(ic, status, prefix ? prefix : ""); |
---|
[5983eca] | 677 | |
---|
| 678 | imcb_buddy_msg(ic, |
---|
| 679 | mode_one ? from : status->user->screen_name, |
---|
| 680 | text ? text : status->text, 0, status->created_at); |
---|
| 681 | |
---|
[62d2cfb] | 682 | // Update the home_timeline_id to hold the highest id, so that by the next request |
---|
[ce81acd] | 683 | // we won't pick up the updates already in the list. |
---|
[5983eca] | 684 | td->home_timeline_id = MAX(td->home_timeline_id, status->id); |
---|
| 685 | |
---|
| 686 | g_free(text); |
---|
| 687 | g_free(prefix); |
---|
[62d2cfb] | 688 | } |
---|
| 689 | } |
---|
| 690 | |
---|
[1b221e0] | 691 | /** |
---|
| 692 | * Callback for getting the home timeline. |
---|
| 693 | */ |
---|
| 694 | static void twitter_http_get_home_timeline(struct http_request *req) |
---|
| 695 | { |
---|
[62d2cfb] | 696 | struct im_connection *ic = req->data; |
---|
[37aa317] | 697 | struct twitter_data *td; |
---|
[1b221e0] | 698 | struct xt_parser *parser; |
---|
| 699 | struct twitter_xml_list *txl; |
---|
[62d2cfb] | 700 | |
---|
| 701 | // Check if the connection is still active. |
---|
[5983eca] | 702 | if (!g_slist_find(twitter_connections, ic)) |
---|
[62d2cfb] | 703 | return; |
---|
[5983eca] | 704 | |
---|
[37aa317] | 705 | td = ic->proto_data; |
---|
[1b221e0] | 706 | |
---|
| 707 | // Check if the HTTP request went well. |
---|
[5983eca] | 708 | if (req->status_code == 200) { |
---|
[3bd4a93] | 709 | td->http_fails = 0; |
---|
[c2ecadc] | 710 | if (!(ic->flags & OPT_LOGGED_IN)) |
---|
[3bd4a93] | 711 | imcb_connected(ic); |
---|
[5983eca] | 712 | } else if (req->status_code == 401) { |
---|
| 713 | imcb_error(ic, "Authentication failure"); |
---|
| 714 | imc_logout(ic, FALSE); |
---|
[3bd4a93] | 715 | return; |
---|
[5983eca] | 716 | } else { |
---|
[1b221e0] | 717 | // It didn't go well, output the error and return. |
---|
[3bd4a93] | 718 | if (++td->http_fails >= 5) |
---|
[de923d5] | 719 | imcb_error(ic, "Could not retrieve %s: %s", |
---|
| 720 | TWITTER_HOME_TIMELINE_URL, twitter_parse_error(req)); |
---|
[5983eca] | 721 | |
---|
[1b221e0] | 722 | return; |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 726 | txl->list = NULL; |
---|
[62d2cfb] | 727 | |
---|
[1b221e0] | 728 | // Parse the data. |
---|
[5983eca] | 729 | parser = xt_new(NULL, txl); |
---|
| 730 | xt_feed(parser, req->reply_body, req->body_size); |
---|
[1b221e0] | 731 | // The root <statuses> node should hold the list of statuses <status> |
---|
[203a2d2] | 732 | twitter_xt_get_status_list(ic, parser->root, txl); |
---|
[5983eca] | 733 | xt_free(parser); |
---|
[1b221e0] | 734 | |
---|
[62d2cfb] | 735 | // See if the user wants to see the messages in a groupchat window or as private messages. |
---|
[5983eca] | 736 | if (txl->list == NULL); |
---|
[fd65edb] | 737 | else if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0) |
---|
[62d2cfb] | 738 | twitter_groupchat(ic, txl->list); |
---|
[b4dd253] | 739 | else |
---|
[62d2cfb] | 740 | twitter_private_message_chat(ic, txl->list); |
---|
[1b221e0] | 741 | |
---|
[5983eca] | 742 | // Free the structure. |
---|
[62d2cfb] | 743 | txl_free(txl); |
---|
[1b221e0] | 744 | } |
---|
| 745 | |
---|
| 746 | /** |
---|
[de923d5] | 747 | * Callback to use after sending a POST request to twitter. |
---|
| 748 | * (Generic, used for a few kinds of queries.) |
---|
[1b221e0] | 749 | */ |
---|
[7d53efb] | 750 | static void twitter_http_post(struct http_request *req) |
---|
[1b221e0] | 751 | { |
---|
| 752 | struct im_connection *ic = req->data; |
---|
[7b87539] | 753 | struct twitter_data *td; |
---|
[1b221e0] | 754 | |
---|
[62d2cfb] | 755 | // Check if the connection is still active. |
---|
[5983eca] | 756 | if (!g_slist_find(twitter_connections, ic)) |
---|
[62d2cfb] | 757 | return; |
---|
| 758 | |
---|
[7b87539] | 759 | td = ic->proto_data; |
---|
| 760 | td->last_status_id = 0; |
---|
[5983eca] | 761 | |
---|
[1b221e0] | 762 | // Check if the HTTP request went well. |
---|
| 763 | if (req->status_code != 200) { |
---|
| 764 | // It didn't go well, output the error and return. |
---|
[ba3233c] | 765 | imcb_error(ic, "HTTP error: %s", twitter_parse_error(req)); |
---|
[1b221e0] | 766 | return; |
---|
| 767 | } |
---|
[5983eca] | 768 | |
---|
| 769 | if (req->body_size > 0) { |
---|
[7b87539] | 770 | struct xt_parser *xp = NULL; |
---|
| 771 | struct xt_node *node; |
---|
[5983eca] | 772 | |
---|
[7b87539] | 773 | xp = xt_new(NULL, NULL); |
---|
| 774 | xt_feed(xp, req->reply_body, req->body_size); |
---|
[5983eca] | 775 | |
---|
[7b87539] | 776 | if ((node = xt_find_node(xp->root, "status")) && |
---|
| 777 | (node = xt_find_node(node->children, "id")) && node->text) |
---|
[5983eca] | 778 | td->last_status_id = g_ascii_strtoull(node->text, NULL, 10); |
---|
| 779 | |
---|
[7b87539] | 780 | xt_free(xp); |
---|
| 781 | } |
---|
[1b221e0] | 782 | } |
---|
| 783 | |
---|
| 784 | /** |
---|
| 785 | * Function to POST a new status to twitter. |
---|
[5983eca] | 786 | */ |
---|
[b890626] | 787 | void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to) |
---|
[1b221e0] | 788 | { |
---|
[5983eca] | 789 | char *args[4] = { |
---|
[b890626] | 790 | "status", msg, |
---|
| 791 | "in_reply_to_status_id", |
---|
| 792 | g_strdup_printf("%llu", (unsigned long long) in_reply_to) |
---|
| 793 | }; |
---|
| 794 | twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1, |
---|
[5983eca] | 795 | args, in_reply_to ? 4 : 2); |
---|
[b890626] | 796 | g_free(args[3]); |
---|
[1b221e0] | 797 | } |
---|
| 798 | |
---|
| 799 | |
---|
[62d2cfb] | 800 | /** |
---|
| 801 | * Function to POST a new message to twitter. |
---|
| 802 | */ |
---|
| 803 | void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) |
---|
| 804 | { |
---|
[5983eca] | 805 | char *args[4]; |
---|
[62d2cfb] | 806 | args[0] = "screen_name"; |
---|
| 807 | args[1] = who; |
---|
| 808 | args[2] = "text"; |
---|
| 809 | args[3] = msg; |
---|
| 810 | // Use the same callback as for twitter_post_status, since it does basically the same. |
---|
[ba3233c] | 811 | twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4); |
---|
[62d2cfb] | 812 | } |
---|
[7d53efb] | 813 | |
---|
| 814 | void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create) |
---|
| 815 | { |
---|
[5983eca] | 816 | char *args[2]; |
---|
[7d53efb] | 817 | args[0] = "screen_name"; |
---|
| 818 | args[1] = who; |
---|
[5983eca] | 819 | twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL, |
---|
| 820 | twitter_http_post, ic, 1, args, 2); |
---|
[a26af5c] | 821 | } |
---|
[7b87539] | 822 | |
---|
| 823 | void twitter_status_destroy(struct im_connection *ic, guint64 id) |
---|
| 824 | { |
---|
| 825 | char *url; |
---|
[de923d5] | 826 | url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_DESTROY_URL, |
---|
| 827 | (unsigned long long) id, ".xml"); |
---|
[7b87539] | 828 | twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0); |
---|
| 829 | g_free(url); |
---|
| 830 | } |
---|
[b890626] | 831 | |
---|
| 832 | void twitter_status_retweet(struct im_connection *ic, guint64 id) |
---|
| 833 | { |
---|
| 834 | char *url; |
---|
[de923d5] | 835 | url = g_strdup_printf("%s%llu%s", TWITTER_STATUS_RETWEET_URL, |
---|
| 836 | (unsigned long long) id, ".xml"); |
---|
[b890626] | 837 | twitter_http(ic, url, twitter_http_post, ic, 1, NULL, 0); |
---|
| 838 | g_free(url); |
---|
| 839 | } |
---|