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