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