[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 | |
---|
| 24 | #include "twitter_http.h" |
---|
| 25 | #include "twitter.h" |
---|
| 26 | #include "bitlbee.h" |
---|
| 27 | #include "url.h" |
---|
| 28 | #include "misc.h" |
---|
| 29 | #include "base64.h" |
---|
| 30 | #include "xmltree.h" |
---|
| 31 | #include "twitter_lib.h" |
---|
| 32 | #include <ctype.h> |
---|
| 33 | #include <errno.h> |
---|
| 34 | |
---|
| 35 | #define TXL_STATUS 1 |
---|
| 36 | #define TXL_ID 1 |
---|
| 37 | |
---|
| 38 | struct twitter_xml_list { |
---|
| 39 | int next_cursor; |
---|
| 40 | GSList *list; |
---|
| 41 | gpointer data; |
---|
| 42 | }; |
---|
| 43 | |
---|
| 44 | struct twitter_xml_user { |
---|
| 45 | char *name; |
---|
| 46 | char *screen_name; |
---|
| 47 | }; |
---|
| 48 | |
---|
| 49 | struct twitter_xml_status { |
---|
| 50 | char *created_at; |
---|
| 51 | char *text; |
---|
| 52 | struct twitter_xml_user *user; |
---|
| 53 | guint64 id; |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | void txl_free(struct twitter_xml_list *txl, int type); |
---|
| 57 | void txs_free(struct twitter_xml_status *txs); |
---|
| 58 | void txu_free(struct twitter_xml_user *txu); |
---|
| 59 | |
---|
| 60 | static void twitter_http_get_friends_ids(struct http_request *req); |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * Get the friends ids. |
---|
| 64 | */ |
---|
| 65 | void twitter_get_friends_ids(struct im_connection *ic, int next_cursor) |
---|
| 66 | { |
---|
| 67 | struct twitter_data *td = ic->proto_data; |
---|
| 68 | |
---|
| 69 | // Primitive, but hey! It works... |
---|
| 70 | char* args[2]; |
---|
| 71 | args[0] = "cursor"; |
---|
| 72 | args[1] = g_strdup_printf ("%d", next_cursor); |
---|
| 73 | twitter_http(TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, td->user, td->pass, args, 2); |
---|
| 74 | |
---|
| 75 | g_free(args[1]); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * Function to help fill a list. |
---|
| 80 | */ |
---|
| 81 | static xt_status twitter_xt_next_cursor( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
| 82 | { |
---|
| 83 | // Do something with the cursor. |
---|
| 84 | txl->next_cursor = atoi(node->text); |
---|
| 85 | |
---|
| 86 | return XT_HANDLED; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * Fill a list of ids. |
---|
| 91 | */ |
---|
| 92 | static xt_status twitter_xt_get_friends_id_list( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
| 93 | { |
---|
| 94 | struct xt_node *child; |
---|
| 95 | |
---|
| 96 | // The root <statuses> node should hold the list of statuses <status> |
---|
| 97 | // Walk over the nodes children. |
---|
| 98 | for( child = node->children ; child ; child = child->next ) |
---|
| 99 | { |
---|
| 100 | if ( g_strcasecmp( "id", child->name ) == 0) |
---|
| 101 | { |
---|
| 102 | // Add the item to the list. |
---|
| 103 | txl->list = g_slist_append (txl->list, g_memdup( node->text, node->text_len + 1 )); |
---|
| 104 | } |
---|
| 105 | else if ( g_strcasecmp( "next_cursor", child->name ) == 0) |
---|
| 106 | { |
---|
| 107 | twitter_xt_next_cursor(child, txl); |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | return XT_HANDLED; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | * Callback for getting the friends ids. |
---|
| 116 | */ |
---|
| 117 | static void twitter_http_get_friends_ids(struct http_request *req) |
---|
| 118 | { |
---|
| 119 | struct im_connection *ic; |
---|
| 120 | struct xt_parser *parser; |
---|
| 121 | struct twitter_xml_list *txl; |
---|
| 122 | |
---|
| 123 | ic = req->data; |
---|
| 124 | |
---|
| 125 | // Check if the HTTP request went well. |
---|
| 126 | if (req->status_code != 200) { |
---|
| 127 | // It didn't go well, output the error and return. |
---|
| 128 | imcb_error(ic, "Could not retrieve friends. HTTP STATUS: %d", req->status_code); |
---|
| 129 | return; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 133 | txl->list = NULL; |
---|
| 134 | |
---|
| 135 | // Parse the data. |
---|
| 136 | parser = xt_new( NULL, txl ); |
---|
| 137 | xt_feed( parser, req->reply_body, req->body_size ); |
---|
| 138 | twitter_xt_get_friends_id_list(parser->root, txl); |
---|
| 139 | xt_free( parser ); |
---|
| 140 | |
---|
| 141 | if (txl->next_cursor) |
---|
| 142 | twitter_get_friends_ids(ic, txl->next_cursor); |
---|
| 143 | |
---|
| 144 | txl_free(txl, TXL_ID); |
---|
| 145 | g_free(txl); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * Function to fill a twitter_xml_user struct. |
---|
| 150 | * It sets: |
---|
| 151 | * - the name and |
---|
| 152 | * - the screen_name. |
---|
| 153 | */ |
---|
| 154 | static xt_status twitter_xt_get_user( struct xt_node *node, struct twitter_xml_user *txu ) |
---|
| 155 | { |
---|
| 156 | struct xt_node *child; |
---|
| 157 | |
---|
| 158 | // Walk over the nodes children. |
---|
| 159 | for( child = node->children ; child ; child = child->next ) |
---|
| 160 | { |
---|
| 161 | if ( g_strcasecmp( "name", child->name ) == 0) |
---|
| 162 | { |
---|
| 163 | txu->name = g_memdup( child->text, child->text_len + 1 ); |
---|
| 164 | } |
---|
| 165 | else if (g_strcasecmp( "screen_name", child->name ) == 0) |
---|
| 166 | { |
---|
| 167 | txu->screen_name = g_memdup( child->text, child->text_len + 1 ); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | return XT_HANDLED; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * Function to fill a twitter_xml_status struct. |
---|
| 175 | * It sets: |
---|
| 176 | * - the status text and |
---|
| 177 | * - the created_at timestamp and |
---|
| 178 | * - the status id and |
---|
| 179 | * - the user in a twitter_xml_user struct. |
---|
| 180 | */ |
---|
| 181 | static xt_status twitter_xt_get_status( struct xt_node *node, struct twitter_xml_status *txs ) |
---|
| 182 | { |
---|
| 183 | struct xt_node *child; |
---|
| 184 | |
---|
| 185 | // Walk over the nodes children. |
---|
| 186 | for( child = node->children ; child ; child = child->next ) |
---|
| 187 | { |
---|
| 188 | if ( g_strcasecmp( "text", child->name ) == 0) |
---|
| 189 | { |
---|
| 190 | txs->text = g_memdup( child->text, child->text_len + 1 ); |
---|
| 191 | } |
---|
| 192 | else if (g_strcasecmp( "created_at", child->name ) == 0) |
---|
| 193 | { |
---|
| 194 | txs->created_at = g_memdup( child->text, child->text_len + 1 ); |
---|
| 195 | } |
---|
| 196 | else if (g_strcasecmp( "user", child->name ) == 0) |
---|
| 197 | { |
---|
| 198 | txs->user = g_new0(struct twitter_xml_user, 1); |
---|
| 199 | twitter_xt_get_user( child, txs->user ); |
---|
| 200 | } |
---|
| 201 | else if (g_strcasecmp( "id", child->name ) == 0) |
---|
| 202 | { |
---|
| 203 | txs->id = g_ascii_strtoull (child->text, NULL, 10); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | return XT_HANDLED; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | * Function to fill a twitter_xml_list struct. |
---|
| 211 | * It sets: |
---|
| 212 | * - all <status>es within the <status> element and |
---|
| 213 | * - the next_cursor. |
---|
| 214 | */ |
---|
| 215 | static xt_status twitter_xt_get_status_list( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
| 216 | { |
---|
| 217 | struct twitter_xml_status *txs; |
---|
| 218 | struct xt_node *child; |
---|
| 219 | |
---|
| 220 | // The root <statuses> node should hold the list of statuses <status> |
---|
| 221 | // Walk over the nodes children. |
---|
| 222 | for( child = node->children ; child ; child = child->next ) |
---|
| 223 | { |
---|
| 224 | if ( g_strcasecmp( "status", child->name ) == 0) |
---|
| 225 | { |
---|
| 226 | txs = g_new0(struct twitter_xml_status, 1); |
---|
| 227 | twitter_xt_get_status(child, txs); |
---|
| 228 | // Put the item in the front of the list. |
---|
| 229 | txl->list = g_slist_prepend (txl->list, txs); |
---|
| 230 | } |
---|
| 231 | else if ( g_strcasecmp( "next_cursor", child->name ) == 0) |
---|
| 232 | { |
---|
| 233 | twitter_xt_next_cursor(child, txl); |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | return XT_HANDLED; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | static void twitter_http_get_home_timeline(struct http_request *req); |
---|
| 241 | |
---|
| 242 | /** |
---|
| 243 | * Get the timeline. |
---|
| 244 | */ |
---|
| 245 | void twitter_get_home_timeline(struct im_connection *ic, int next_cursor) |
---|
| 246 | { |
---|
| 247 | struct twitter_data *td = ic->proto_data; |
---|
| 248 | |
---|
| 249 | char* args[4]; |
---|
| 250 | args[0] = "cursor"; |
---|
| 251 | args[1] = g_strdup_printf ("%d", next_cursor); |
---|
| 252 | if (td->home_timeline_id) { |
---|
| 253 | args[2] = "since_id"; |
---|
| 254 | args[3] = g_strdup_printf ("%llu", td->home_timeline_id); |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | twitter_http(TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, td->user, td->pass, args, td->home_timeline_id ? 4 : 2); |
---|
| 258 | |
---|
| 259 | g_free(args[1]); |
---|
| 260 | if (td->home_timeline_id) { |
---|
| 261 | g_free(args[3]); |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | /** |
---|
| 266 | * Callback for getting the home timeline. |
---|
| 267 | */ |
---|
| 268 | static void twitter_http_get_home_timeline(struct http_request *req) |
---|
| 269 | { |
---|
| 270 | struct im_connection *ic = req->data;; |
---|
| 271 | struct xt_parser *parser; |
---|
| 272 | struct twitter_xml_list *txl; |
---|
| 273 | struct twitter_data *td = ic->proto_data; |
---|
| 274 | |
---|
| 275 | // Check if the HTTP request went well. |
---|
| 276 | if (req->status_code != 200) { |
---|
| 277 | // It didn't go well, output the error and return. |
---|
| 278 | imcb_error(ic, "Could not retrieve home/timeline. HTTP STATUS: %d", req->status_code); |
---|
| 279 | return; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | txl = g_new0(struct twitter_xml_list, 1); |
---|
| 283 | txl->list = NULL; |
---|
| 284 | |
---|
| 285 | // Parse the data. |
---|
| 286 | parser = xt_new( NULL, txl ); |
---|
| 287 | xt_feed( parser, req->reply_body, req->body_size ); |
---|
| 288 | // The root <statuses> node should hold the list of statuses <status> |
---|
| 289 | twitter_xt_get_status_list(parser->root, txl); |
---|
| 290 | xt_free( parser ); |
---|
| 291 | |
---|
| 292 | GSList *l; |
---|
| 293 | struct twitter_xml_status *status; |
---|
| 294 | |
---|
| 295 | imcb_add_buddy( ic, "home_timeline", NULL ); |
---|
| 296 | imcb_buddy_status( ic, "home_timeline", OPT_LOGGED_IN, NULL, NULL ); |
---|
| 297 | |
---|
| 298 | for ( l = txl->list; l ; l = g_slist_next(l) ) |
---|
| 299 | { |
---|
| 300 | status = l->data; |
---|
| 301 | imcb_buddy_msg( ic, "home_timeline", status->text, 0, 0 ); |
---|
| 302 | td->home_timeline_id = td->home_timeline_id < status->id ? status->id : td->home_timeline_id; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | // Free the structure. |
---|
| 306 | txl_free(txl, TXL_STATUS); |
---|
| 307 | g_free(txl); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | /** |
---|
| 311 | * Free a twitter_xml_list struct. |
---|
| 312 | * type is the type of list the struct holds. |
---|
| 313 | */ |
---|
| 314 | void txl_free(struct twitter_xml_list *txl, int type) |
---|
| 315 | { |
---|
| 316 | GSList *l; |
---|
| 317 | for ( l = txl->list; l ; l = g_slist_next(l) ) |
---|
| 318 | if (type == TXL_STATUS) |
---|
| 319 | txs_free((struct twitter_xml_status *)l->data); |
---|
| 320 | else if (type == TXL_ID) |
---|
| 321 | g_free(l->data); |
---|
| 322 | g_slist_free(txl->list); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | /** |
---|
| 326 | * Frees a twitter_xml_status struct. |
---|
| 327 | */ |
---|
| 328 | void txs_free(struct twitter_xml_status *txs) |
---|
| 329 | { |
---|
| 330 | g_free(txs->created_at); |
---|
| 331 | g_free(txs->text); |
---|
| 332 | txu_free(txs->user); |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | /** |
---|
| 336 | * Frees a twitter_xml_user struct. |
---|
| 337 | */ |
---|
| 338 | void txu_free(struct twitter_xml_user *txu) |
---|
| 339 | { |
---|
| 340 | g_free(txu->name); |
---|
| 341 | g_free(txu->screen_name); |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | /** |
---|
| 345 | * Callback after sending a new update to twitter. |
---|
| 346 | */ |
---|
| 347 | static void twitter_http_post_status(struct http_request *req) |
---|
| 348 | { |
---|
| 349 | struct im_connection *ic = req->data; |
---|
| 350 | |
---|
| 351 | // Check if the HTTP request went well. |
---|
| 352 | if (req->status_code != 200) { |
---|
| 353 | // It didn't go well, output the error and return. |
---|
| 354 | imcb_error(ic, "Could not post tweed... HTTP STATUS: %d", req->status_code); |
---|
| 355 | imcb_error(ic, req->reply_body); |
---|
| 356 | return; |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | /** |
---|
| 361 | * Function to POST a new status to twitter. |
---|
| 362 | */ |
---|
| 363 | void twitter_post_status(struct im_connection *ic, char* msg) |
---|
| 364 | { |
---|
| 365 | struct twitter_data *td = ic->proto_data; |
---|
| 366 | |
---|
| 367 | char* args[2]; |
---|
| 368 | args[0] = "status"; |
---|
| 369 | args[1] = msg; |
---|
| 370 | twitter_http(TWITTER_STATUS_UPDATE_URL, twitter_http_post_status, ic, 1, td->user, td->pass, args, 2); |
---|
| 371 | g_free(args[1]); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | |
---|