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