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 | /* For strptime(): */ |
---|
25 | #define _XOPEN_SOURCE |
---|
26 | |
---|
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 |
---|
39 | #define TXL_USER 2 |
---|
40 | #define TXL_ID 3 |
---|
41 | |
---|
42 | struct twitter_xml_list { |
---|
43 | int type; |
---|
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 { |
---|
55 | time_t created_at; |
---|
56 | char *text; |
---|
57 | struct twitter_xml_user *user; |
---|
58 | guint64 id; |
---|
59 | }; |
---|
60 | |
---|
61 | /** |
---|
62 | * Frees a twitter_xml_user struct. |
---|
63 | */ |
---|
64 | static void txu_free(struct twitter_xml_user *txu) |
---|
65 | { |
---|
66 | g_free(txu->name); |
---|
67 | g_free(txu->screen_name); |
---|
68 | g_free(txu); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | /** |
---|
73 | * Frees a twitter_xml_status struct. |
---|
74 | */ |
---|
75 | static void txs_free(struct twitter_xml_status *txs) |
---|
76 | { |
---|
77 | g_free(txs->text); |
---|
78 | txu_free(txs->user); |
---|
79 | g_free(txs); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Free a twitter_xml_list struct. |
---|
84 | * type is the type of list the struct holds. |
---|
85 | */ |
---|
86 | static void txl_free(struct twitter_xml_list *txl) |
---|
87 | { |
---|
88 | GSList *l; |
---|
89 | for ( l = txl->list; l ; l = g_slist_next(l) ) |
---|
90 | if (txl->type == TXL_STATUS) |
---|
91 | txs_free((struct twitter_xml_status *)l->data); |
---|
92 | else if (txl->type == TXL_ID) |
---|
93 | g_free(l->data); |
---|
94 | g_slist_free(txl->list); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Add a buddy if it is not allready added, set the status to logged in. |
---|
99 | */ |
---|
100 | static void twitter_add_buddy(struct im_connection *ic, char *name, const char *fullname) |
---|
101 | { |
---|
102 | struct twitter_data *td = ic->proto_data; |
---|
103 | |
---|
104 | // Check if the buddy is allready in the buddy list. |
---|
105 | if (!imcb_find_buddy( ic, name )) |
---|
106 | { |
---|
107 | // The buddy is not in the list, add the buddy and set the status to logged in. |
---|
108 | imcb_add_buddy( ic, name, NULL ); |
---|
109 | imcb_rename_buddy( ic, name, fullname ); |
---|
110 | if (set_getbool( &ic->acc->set, "use_groupchat" )) |
---|
111 | imcb_chat_add_buddy( td->home_timeline_gc, name ); |
---|
112 | else |
---|
113 | imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL ); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | static void twitter_http_get_friends_ids(struct http_request *req); |
---|
118 | |
---|
119 | /** |
---|
120 | * Get the friends ids. |
---|
121 | */ |
---|
122 | void twitter_get_friends_ids(struct im_connection *ic, int next_cursor) |
---|
123 | { |
---|
124 | struct twitter_data *td = ic->proto_data; |
---|
125 | |
---|
126 | // Primitive, but hey! It works... |
---|
127 | char* args[2]; |
---|
128 | args[0] = "cursor"; |
---|
129 | args[1] = g_strdup_printf ("%d", next_cursor); |
---|
130 | twitter_http(TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, td->user, td->pass, args, 2); |
---|
131 | |
---|
132 | g_free(args[1]); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Function to help fill a list. |
---|
137 | */ |
---|
138 | static xt_status twitter_xt_next_cursor( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
139 | { |
---|
140 | // Do something with the cursor. |
---|
141 | txl->next_cursor = node->text != NULL ? atoi(node->text) : -1; |
---|
142 | |
---|
143 | return XT_HANDLED; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Fill a list of ids. |
---|
148 | */ |
---|
149 | static xt_status twitter_xt_get_friends_id_list( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
150 | { |
---|
151 | struct xt_node *child; |
---|
152 | |
---|
153 | // Set the list type. |
---|
154 | txl->type = TXL_ID; |
---|
155 | |
---|
156 | // The root <statuses> node should hold the list of statuses <status> |
---|
157 | // Walk over the nodes children. |
---|
158 | for( child = node->children ; child ; child = child->next ) |
---|
159 | { |
---|
160 | if ( g_strcasecmp( "id", child->name ) == 0) |
---|
161 | { |
---|
162 | // Add the item to the list. |
---|
163 | txl->list = g_slist_append (txl->list, g_memdup( child->text, child->text_len + 1 )); |
---|
164 | } |
---|
165 | else if ( g_strcasecmp( "next_cursor", child->name ) == 0) |
---|
166 | { |
---|
167 | twitter_xt_next_cursor(child, txl); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | return XT_HANDLED; |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * Callback for getting the friends ids. |
---|
176 | */ |
---|
177 | static void twitter_http_get_friends_ids(struct http_request *req) |
---|
178 | { |
---|
179 | struct im_connection *ic; |
---|
180 | struct xt_parser *parser; |
---|
181 | struct twitter_xml_list *txl; |
---|
182 | |
---|
183 | ic = req->data; |
---|
184 | |
---|
185 | // Check if the connection is still active. |
---|
186 | if( !g_slist_find( twitter_connections, ic ) ) |
---|
187 | return; |
---|
188 | |
---|
189 | // Check if the HTTP request went well. |
---|
190 | if (req->status_code != 200) { |
---|
191 | // It didn't go well, output the error and return. |
---|
192 | imcb_error(ic, "Could not retrieve friends. HTTP STATUS: %d", req->status_code); |
---|
193 | return; |
---|
194 | } |
---|
195 | |
---|
196 | txl = g_new0(struct twitter_xml_list, 1); |
---|
197 | |
---|
198 | // Parse the data. |
---|
199 | parser = xt_new( NULL, txl ); |
---|
200 | xt_feed( parser, req->reply_body, req->body_size ); |
---|
201 | twitter_xt_get_friends_id_list(parser->root, txl); |
---|
202 | xt_free( parser ); |
---|
203 | |
---|
204 | if (txl->next_cursor) |
---|
205 | twitter_get_friends_ids(ic, txl->next_cursor); |
---|
206 | |
---|
207 | txl_free(txl); |
---|
208 | g_free(txl); |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * Function to fill a twitter_xml_user struct. |
---|
213 | * It sets: |
---|
214 | * - the name and |
---|
215 | * - the screen_name. |
---|
216 | */ |
---|
217 | static xt_status twitter_xt_get_user( struct xt_node *node, struct twitter_xml_user *txu ) |
---|
218 | { |
---|
219 | struct xt_node *child; |
---|
220 | |
---|
221 | // Walk over the nodes children. |
---|
222 | for( child = node->children ; child ; child = child->next ) |
---|
223 | { |
---|
224 | if ( g_strcasecmp( "name", child->name ) == 0) |
---|
225 | { |
---|
226 | txu->name = g_memdup( child->text, child->text_len + 1 ); |
---|
227 | } |
---|
228 | else if (g_strcasecmp( "screen_name", child->name ) == 0) |
---|
229 | { |
---|
230 | txu->screen_name = g_memdup( child->text, child->text_len + 1 ); |
---|
231 | } |
---|
232 | } |
---|
233 | return XT_HANDLED; |
---|
234 | } |
---|
235 | |
---|
236 | /** |
---|
237 | * Function to fill a twitter_xml_list struct. |
---|
238 | * It sets: |
---|
239 | * - all <user>s from the <users> element. |
---|
240 | */ |
---|
241 | static xt_status twitter_xt_get_users( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
242 | { |
---|
243 | struct twitter_xml_user *txu; |
---|
244 | struct xt_node *child; |
---|
245 | |
---|
246 | // Set the type of the list. |
---|
247 | txl->type = TXL_USER; |
---|
248 | |
---|
249 | // The root <users> node should hold the list of users <user> |
---|
250 | // Walk over the nodes children. |
---|
251 | for( child = node->children ; child ; child = child->next ) |
---|
252 | { |
---|
253 | if ( g_strcasecmp( "user", child->name ) == 0) |
---|
254 | { |
---|
255 | txu = g_new0(struct twitter_xml_user, 1); |
---|
256 | twitter_xt_get_user(child, txu); |
---|
257 | // Put the item in the front of the list. |
---|
258 | txl->list = g_slist_prepend (txl->list, txu); |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | return XT_HANDLED; |
---|
263 | } |
---|
264 | |
---|
265 | /** |
---|
266 | * Function to fill a twitter_xml_list struct. |
---|
267 | * It calls twitter_xt_get_users to get the <user>s from a <users> element. |
---|
268 | * It sets: |
---|
269 | * - the next_cursor. |
---|
270 | */ |
---|
271 | static xt_status twitter_xt_get_user_list( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
272 | { |
---|
273 | struct xt_node *child; |
---|
274 | |
---|
275 | // Set the type of the list. |
---|
276 | txl->type = TXL_USER; |
---|
277 | |
---|
278 | // The root <user_list> node should hold a users <users> element |
---|
279 | // Walk over the nodes children. |
---|
280 | for( child = node->children ; child ; child = child->next ) |
---|
281 | { |
---|
282 | if ( g_strcasecmp( "users", child->name ) == 0) |
---|
283 | { |
---|
284 | twitter_xt_get_users(child, txl); |
---|
285 | } |
---|
286 | else if ( g_strcasecmp( "next_cursor", child->name ) == 0) |
---|
287 | { |
---|
288 | twitter_xt_next_cursor(child, txl); |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | return XT_HANDLED; |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | /** |
---|
297 | * Function to fill a twitter_xml_status struct. |
---|
298 | * It sets: |
---|
299 | * - the status text and |
---|
300 | * - the created_at timestamp and |
---|
301 | * - the status id and |
---|
302 | * - the user in a twitter_xml_user struct. |
---|
303 | */ |
---|
304 | static xt_status twitter_xt_get_status( struct xt_node *node, struct twitter_xml_status *txs ) |
---|
305 | { |
---|
306 | struct xt_node *child; |
---|
307 | |
---|
308 | // Walk over the nodes children. |
---|
309 | for( child = node->children ; child ; child = child->next ) |
---|
310 | { |
---|
311 | if ( g_strcasecmp( "text", child->name ) == 0) |
---|
312 | { |
---|
313 | txs->text = g_memdup( child->text, child->text_len + 1 ); |
---|
314 | } |
---|
315 | else if (g_strcasecmp( "created_at", child->name ) == 0) |
---|
316 | { |
---|
317 | struct tm parsed; |
---|
318 | |
---|
319 | /* Very sensitive to changes to the formatting of |
---|
320 | this field. :-( Also assumes the timezone used |
---|
321 | is UTC since C time handling functions suck. */ |
---|
322 | if( strptime( child->text, "%a %b %d %H:%M:%S %z %Y", &parsed ) != NULL ) |
---|
323 | txs->created_at = mktime_utc( &parsed ); |
---|
324 | } |
---|
325 | else if (g_strcasecmp( "user", child->name ) == 0) |
---|
326 | { |
---|
327 | txs->user = g_new0(struct twitter_xml_user, 1); |
---|
328 | twitter_xt_get_user( child, txs->user ); |
---|
329 | } |
---|
330 | else if (g_strcasecmp( "id", child->name ) == 0) |
---|
331 | { |
---|
332 | txs->id = g_ascii_strtoull (child->text, NULL, 10); |
---|
333 | } |
---|
334 | } |
---|
335 | return XT_HANDLED; |
---|
336 | } |
---|
337 | |
---|
338 | /** |
---|
339 | * Function to fill a twitter_xml_list struct. |
---|
340 | * It sets: |
---|
341 | * - all <status>es within the <status> element and |
---|
342 | * - the next_cursor. |
---|
343 | */ |
---|
344 | static xt_status twitter_xt_get_status_list( struct xt_node *node, struct twitter_xml_list *txl ) |
---|
345 | { |
---|
346 | struct twitter_xml_status *txs; |
---|
347 | struct xt_node *child; |
---|
348 | |
---|
349 | // Set the type of the list. |
---|
350 | txl->type = TXL_STATUS; |
---|
351 | |
---|
352 | // The root <statuses> node should hold the list of statuses <status> |
---|
353 | // Walk over the nodes children. |
---|
354 | for( child = node->children ; child ; child = child->next ) |
---|
355 | { |
---|
356 | if ( g_strcasecmp( "status", child->name ) == 0) |
---|
357 | { |
---|
358 | txs = g_new0(struct twitter_xml_status, 1); |
---|
359 | twitter_xt_get_status(child, txs); |
---|
360 | // Put the item in the front of the list. |
---|
361 | txl->list = g_slist_prepend (txl->list, txs); |
---|
362 | } |
---|
363 | else if ( g_strcasecmp( "next_cursor", child->name ) == 0) |
---|
364 | { |
---|
365 | twitter_xt_next_cursor(child, txl); |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | return XT_HANDLED; |
---|
370 | } |
---|
371 | |
---|
372 | static void twitter_http_get_home_timeline(struct http_request *req); |
---|
373 | |
---|
374 | /** |
---|
375 | * Get the timeline. |
---|
376 | */ |
---|
377 | void twitter_get_home_timeline(struct im_connection *ic, int next_cursor) |
---|
378 | { |
---|
379 | struct twitter_data *td = ic->proto_data; |
---|
380 | |
---|
381 | char* args[4]; |
---|
382 | args[0] = "cursor"; |
---|
383 | args[1] = g_strdup_printf ("%d", next_cursor); |
---|
384 | if (td->home_timeline_id) { |
---|
385 | args[2] = "since_id"; |
---|
386 | args[3] = g_strdup_printf ("%llu", (long long unsigned int) td->home_timeline_id); |
---|
387 | } |
---|
388 | |
---|
389 | twitter_http(TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, td->user, td->pass, args, td->home_timeline_id ? 4 : 2); |
---|
390 | |
---|
391 | g_free(args[1]); |
---|
392 | if (td->home_timeline_id) { |
---|
393 | g_free(args[3]); |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | /** |
---|
398 | * Function that is called to see the statuses in a groupchat window. |
---|
399 | */ |
---|
400 | static void twitter_groupchat(struct im_connection *ic, GSList *list) |
---|
401 | { |
---|
402 | struct twitter_data *td = ic->proto_data; |
---|
403 | GSList *l = NULL; |
---|
404 | struct twitter_xml_status *status; |
---|
405 | struct groupchat *gc; |
---|
406 | |
---|
407 | // Create a new groupchat if it does not exsist. |
---|
408 | if (!td->home_timeline_gc) |
---|
409 | { |
---|
410 | char *name_hint = g_strdup_printf( "Twitter_%s", ic->acc->user ); |
---|
411 | td->home_timeline_gc = gc = imcb_chat_new( ic, "home/timeline" ); |
---|
412 | imcb_chat_name_hint( gc, name_hint ); |
---|
413 | g_free( name_hint ); |
---|
414 | // Add the current user to the chat... |
---|
415 | imcb_chat_add_buddy( gc, ic->acc->user ); |
---|
416 | } |
---|
417 | else |
---|
418 | { |
---|
419 | gc = td->home_timeline_gc; |
---|
420 | } |
---|
421 | |
---|
422 | for ( l = list; l ; l = g_slist_next(l) ) |
---|
423 | { |
---|
424 | status = l->data; |
---|
425 | twitter_add_buddy(ic, status->user->screen_name, status->user->name); |
---|
426 | |
---|
427 | // Say it! |
---|
428 | if (g_strcasecmp(td->user, status->user->screen_name) == 0) |
---|
429 | imcb_chat_log (gc, "Your Tweet: %s", status->text); |
---|
430 | else |
---|
431 | imcb_chat_msg (gc, status->user->screen_name, status->text, 0, status->created_at ); |
---|
432 | |
---|
433 | // Update the home_timeline_id to hold the highest id, so that by the next request |
---|
434 | // we won't pick up the updates allready in the list. |
---|
435 | td->home_timeline_id = td->home_timeline_id < status->id ? status->id : td->home_timeline_id; |
---|
436 | } |
---|
437 | } |
---|
438 | |
---|
439 | /** |
---|
440 | * Function that is called to see statuses as private messages. |
---|
441 | */ |
---|
442 | static void twitter_private_message_chat(struct im_connection *ic, GSList *list) |
---|
443 | { |
---|
444 | struct twitter_data *td = ic->proto_data; |
---|
445 | GSList *l = NULL; |
---|
446 | struct twitter_xml_status *status; |
---|
447 | |
---|
448 | for ( l = list; l ; l = g_slist_next(l) ) |
---|
449 | { |
---|
450 | status = l->data; |
---|
451 | imcb_buddy_msg( ic, status->user->screen_name, status->text, 0, status->created_at ); |
---|
452 | // Update the home_timeline_id to hold the highest id, so that by the next request |
---|
453 | // we won't pick up the updates allready in the list. |
---|
454 | td->home_timeline_id = td->home_timeline_id < status->id ? status->id : td->home_timeline_id; |
---|
455 | } |
---|
456 | } |
---|
457 | |
---|
458 | /** |
---|
459 | * Callback for getting the home timeline. |
---|
460 | */ |
---|
461 | static void twitter_http_get_home_timeline(struct http_request *req) |
---|
462 | { |
---|
463 | struct im_connection *ic = req->data; |
---|
464 | struct xt_parser *parser; |
---|
465 | struct twitter_xml_list *txl; |
---|
466 | |
---|
467 | // Check if the connection is still active. |
---|
468 | if( !g_slist_find( twitter_connections, ic ) ) |
---|
469 | return; |
---|
470 | |
---|
471 | // Check if the HTTP request went well. |
---|
472 | if (req->status_code != 200) { |
---|
473 | // It didn't go well, output the error and return. |
---|
474 | imcb_error(ic, "Could not retrieve " TWITTER_HOME_TIMELINE_URL ". HTTP STATUS: %d", req->status_code); |
---|
475 | return; |
---|
476 | } |
---|
477 | |
---|
478 | txl = g_new0(struct twitter_xml_list, 1); |
---|
479 | txl->list = NULL; |
---|
480 | |
---|
481 | // Parse the data. |
---|
482 | parser = xt_new( NULL, txl ); |
---|
483 | xt_feed( parser, req->reply_body, req->body_size ); |
---|
484 | // The root <statuses> node should hold the list of statuses <status> |
---|
485 | twitter_xt_get_status_list(parser->root, txl); |
---|
486 | xt_free( parser ); |
---|
487 | |
---|
488 | // See if the user wants to see the messages in a groupchat window or as private messages. |
---|
489 | if (set_getbool( &ic->acc->set, "use_groupchat" )) |
---|
490 | twitter_groupchat(ic, txl->list); |
---|
491 | else |
---|
492 | twitter_private_message_chat(ic, txl->list); |
---|
493 | |
---|
494 | // Free the structure. |
---|
495 | txl_free(txl); |
---|
496 | g_free(txl); |
---|
497 | } |
---|
498 | |
---|
499 | /** |
---|
500 | * Callback for getting (twitter)friends... |
---|
501 | * |
---|
502 | * Be afraid, be very afraid! This function will potentially add hundreds of "friends". "Who has |
---|
503 | * hundreds of friends?" you wonder? You probably not, since you are reading the source of |
---|
504 | * BitlBee... Get a life and meet new people! |
---|
505 | */ |
---|
506 | static void twitter_http_get_statuses_friends(struct http_request *req) |
---|
507 | { |
---|
508 | struct im_connection *ic = req->data; |
---|
509 | struct xt_parser *parser; |
---|
510 | struct twitter_xml_list *txl; |
---|
511 | GSList *l = NULL; |
---|
512 | struct twitter_xml_user *user; |
---|
513 | |
---|
514 | // Check if the connection is still active. |
---|
515 | if( !g_slist_find( twitter_connections, ic ) ) |
---|
516 | return; |
---|
517 | |
---|
518 | // Check if the HTTP request went well. |
---|
519 | if (req->status_code != 200) { |
---|
520 | // It didn't go well, output the error and return. |
---|
521 | imcb_error(ic, "Could not retrieve " TWITTER_SHOW_FRIENDS_URL " HTTP STATUS: %d", req->status_code); |
---|
522 | return; |
---|
523 | } |
---|
524 | |
---|
525 | txl = g_new0(struct twitter_xml_list, 1); |
---|
526 | txl->list = NULL; |
---|
527 | |
---|
528 | // Parse the data. |
---|
529 | parser = xt_new( NULL, txl ); |
---|
530 | xt_feed( parser, req->reply_body, req->body_size ); |
---|
531 | |
---|
532 | // Get the user list from the parsed xml feed. |
---|
533 | twitter_xt_get_user_list(parser->root, txl); |
---|
534 | xt_free( parser ); |
---|
535 | |
---|
536 | // Add the users as buddies. |
---|
537 | for ( l = txl->list; l ; l = g_slist_next(l) ) |
---|
538 | { |
---|
539 | user = l->data; |
---|
540 | twitter_add_buddy(ic, user->screen_name, user->name); |
---|
541 | } |
---|
542 | |
---|
543 | // if the next_cursor is set to something bigger then 0 there are more friends to gather. |
---|
544 | if (txl->next_cursor > 0) |
---|
545 | twitter_get_statuses_friends(ic, txl->next_cursor); |
---|
546 | |
---|
547 | // Free the structure. |
---|
548 | txl_free(txl); |
---|
549 | g_free(txl); |
---|
550 | } |
---|
551 | |
---|
552 | /** |
---|
553 | * Get the friends. |
---|
554 | */ |
---|
555 | void twitter_get_statuses_friends(struct im_connection *ic, int next_cursor) |
---|
556 | { |
---|
557 | struct twitter_data *td = ic->proto_data; |
---|
558 | |
---|
559 | char* args[2]; |
---|
560 | args[0] = "cursor"; |
---|
561 | args[1] = g_strdup_printf ("%d", next_cursor); |
---|
562 | |
---|
563 | twitter_http(TWITTER_SHOW_FRIENDS_URL, twitter_http_get_statuses_friends, ic, 0, td->user, td->pass, args, 2); |
---|
564 | |
---|
565 | g_free(args[1]); |
---|
566 | } |
---|
567 | |
---|
568 | /** |
---|
569 | * Callback after sending a new update to twitter. |
---|
570 | */ |
---|
571 | static void twitter_http_post_status(struct http_request *req) |
---|
572 | { |
---|
573 | struct im_connection *ic = req->data; |
---|
574 | |
---|
575 | // Check if the connection is still active. |
---|
576 | if( !g_slist_find( twitter_connections, ic ) ) |
---|
577 | return; |
---|
578 | |
---|
579 | // Check if the HTTP request went well. |
---|
580 | if (req->status_code != 200) { |
---|
581 | // It didn't go well, output the error and return. |
---|
582 | imcb_error(ic, "Could not post tweet... HTTP STATUS: %d", req->status_code); |
---|
583 | return; |
---|
584 | } |
---|
585 | } |
---|
586 | |
---|
587 | /** |
---|
588 | * Function to POST a new status to twitter. |
---|
589 | */ |
---|
590 | void twitter_post_status(struct im_connection *ic, char* msg) |
---|
591 | { |
---|
592 | struct twitter_data *td = ic->proto_data; |
---|
593 | |
---|
594 | char* args[2]; |
---|
595 | args[0] = "status"; |
---|
596 | args[1] = msg; |
---|
597 | twitter_http(TWITTER_STATUS_UPDATE_URL, twitter_http_post_status, ic, 1, td->user, td->pass, args, 2); |
---|
598 | // g_free(args[1]); |
---|
599 | } |
---|
600 | |
---|
601 | |
---|
602 | /** |
---|
603 | * Function to POST a new message to twitter. |
---|
604 | */ |
---|
605 | void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) |
---|
606 | { |
---|
607 | struct twitter_data *td = ic->proto_data; |
---|
608 | |
---|
609 | char* args[4]; |
---|
610 | args[0] = "screen_name"; |
---|
611 | args[1] = who; |
---|
612 | args[2] = "text"; |
---|
613 | args[3] = msg; |
---|
614 | // Use the same callback as for twitter_post_status, since it does basically the same. |
---|
615 | twitter_http(TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post_status, ic, 1, td->user, td->pass, args, 4); |
---|
616 | // g_free(args[1]); |
---|
617 | // g_free(args[3]); |
---|
618 | } |
---|