Changes in protocols/twitter/twitter_lib.c [7d53efb:8203da9]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/twitter/twitter_lib.c
r7d53efb r8203da9 42 42 struct twitter_xml_list { 43 43 int type; 44 intnext_cursor;44 gint64 next_cursor; 45 45 GSList *list; 46 46 gpointer data; … … 58 58 guint64 id; 59 59 }; 60 61 static void twitter_groupchat_init(struct im_connection *ic); 60 62 61 63 /** … … 117 119 } 118 120 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; 126 struct xt_parser *xp = NULL; 127 struct xt_node *node; 128 129 g_free(ret); 130 ret = NULL; 131 132 if (req->body_size > 0) 133 { 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 146 xt_free(xp); 147 } 148 149 return req->status_string; 150 } 151 119 152 static void twitter_http_get_friends_ids(struct http_request *req); 120 153 … … 122 155 * Get the friends ids. 123 156 */ 124 void twitter_get_friends_ids(struct im_connection *ic, int next_cursor) 125 { 126 struct twitter_data *td = ic->proto_data; 127 157 void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor) 158 { 128 159 // Primitive, but hey! It works... 129 160 char* args[2]; 130 161 args[0] = "cursor"; 131 args[1] = g_strdup_printf ("% d",next_cursor);132 twitter_http( TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, td->user, td->pass, td->oauth_info, args, 2);162 args[1] = g_strdup_printf ("%lld", (long long) next_cursor); 163 twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2); 133 164 134 165 g_free(args[1]); … … 140 171 static xt_status twitter_xt_next_cursor( struct xt_node *node, struct twitter_xml_list *txl ) 141 172 { 142 // Do something with the cursor. 143 txl->next_cursor = node->text != NULL ? atoi(node->text) : -1; 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; 144 179 145 180 return XT_HANDLED; … … 196 231 // It didn't go well, output the error and return. 197 232 if (++td->http_fails >= 5) 198 imcb_error(ic, "Could not retrieve friends . HTTP STATUS: %d", req->status_code);233 imcb_error(ic, "Could not retrieve friends: %s", twitter_parse_error(req)); 199 234 200 235 return; … … 384 419 * Get the timeline. 385 420 */ 386 void twitter_get_home_timeline(struct im_connection *ic, intnext_cursor)421 void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor) 387 422 { 388 423 struct twitter_data *td = ic->proto_data; … … 390 425 char* args[4]; 391 426 args[0] = "cursor"; 392 args[1] = g_strdup_printf ("% d",next_cursor);427 args[1] = g_strdup_printf ("%lld", (long long) next_cursor); 393 428 if (td->home_timeline_id) { 394 429 args[2] = "since_id"; … … 396 431 } 397 432 398 twitter_http( TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, td->user, td->pass, td->oauth_info, args, td->home_timeline_id ? 4 : 2);433 twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args, td->home_timeline_id ? 4 : 2); 399 434 400 435 g_free(args[1]); … … 402 437 g_free(args[3]); 403 438 } 439 } 440 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 ); 404 452 } 405 453 … … 416 464 // Create a new groupchat if it does not exsist. 417 465 if (!td->home_timeline_gc) 418 { 419 char *name_hint = g_strdup_printf( "Twitter_%s", ic->acc->user ); 420 td->home_timeline_gc = gc = imcb_chat_new( ic, "home/timeline" ); 421 imcb_chat_name_hint( gc, name_hint ); 422 g_free( name_hint ); 423 // Add the current user to the chat... 466 twitter_groupchat_init(ic); 467 468 gc = td->home_timeline_gc; 469 if (!gc->joined) 424 470 imcb_chat_add_buddy( gc, ic->acc->user ); 425 }426 else427 {428 gc = td->home_timeline_gc;429 }430 471 431 472 for ( l = list; l ; l = g_slist_next(l) ) … … 433 474 status = l->data; 434 475 twitter_add_buddy(ic, status->user->screen_name, status->user->name); 476 477 strip_html(status->text); 435 478 436 479 // Say it! … … 471 514 status = l->data; 472 515 516 strip_html( status->text ); 473 517 if( mode_one ) 474 518 text = g_strdup_printf( "\002<\002%s\002>\002 %s", … … 523 567 // It didn't go well, output the error and return. 524 568 if (++td->http_fails >= 5) 525 imcb_error(ic, "Could not retrieve " TWITTER_HOME_TIMELINE_URL " . HTTP STATUS: %d", req->status_code);569 imcb_error(ic, "Could not retrieve " TWITTER_HOME_TIMELINE_URL ": %s", twitter_parse_error(req)); 526 570 527 571 return; … … 572 616 573 617 // Check if the HTTP request went well. 574 if (req->status_code != 200) { 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) { 575 624 // It didn't go well, output the error and return. 576 if (++td->http_fails >= 5) 577 imcb_error(ic, "Could not retrieve " TWITTER_SHOW_FRIENDS_URL " HTTP STATUS: %d", req->status_code); 578 625 imcb_error(ic, "Could not retrieve " TWITTER_SHOW_FRIENDS_URL ": %s", twitter_parse_error(req)); 626 imc_logout( ic, TRUE ); 579 627 return; 580 628 } else { 581 629 td->http_fails = 0; 582 630 } 631 632 if( !td->home_timeline_gc && 633 g_strcasecmp( set_getstr( &ic->acc->set, "mode" ), "chat" ) == 0 ) 634 twitter_groupchat_init( ic ); 583 635 584 636 txl = g_new0(struct twitter_xml_list, 1); … … 602 654 // if the next_cursor is set to something bigger then 0 there are more friends to gather. 603 655 if (txl->next_cursor > 0) 656 { 604 657 twitter_get_statuses_friends(ic, txl->next_cursor); 605 658 } 659 else 660 { 661 td->flags |= TWITTER_HAVE_FRIENDS; 662 twitter_login_finish(ic); 663 } 664 606 665 // Free the structure. 607 666 txl_free(txl); … … 612 671 * Get the friends. 613 672 */ 614 void twitter_get_statuses_friends(struct im_connection *ic, int next_cursor) 615 { 616 struct twitter_data *td = ic->proto_data; 617 673 void twitter_get_statuses_friends(struct im_connection *ic, gint64 next_cursor) 674 { 618 675 char* args[2]; 619 676 args[0] = "cursor"; 620 args[1] = g_strdup_printf ("% d",next_cursor);621 622 twitter_http( TWITTER_SHOW_FRIENDS_URL, twitter_http_get_statuses_friends, ic, 0, td->user, td->pass, td->oauth_info, args, 2);677 args[1] = g_strdup_printf ("%lld", (long long) next_cursor); 678 679 twitter_http(ic, TWITTER_SHOW_FRIENDS_URL, twitter_http_get_statuses_friends, ic, 0, args, 2); 623 680 624 681 g_free(args[1]); … … 639 696 if (req->status_code != 200) { 640 697 // It didn't go well, output the error and return. 641 imcb_error(ic, "HTTP Error... STATUS: %d", req->status_code);698 imcb_error(ic, "HTTP error: %s", twitter_parse_error(req)); 642 699 return; 643 700 } … … 649 706 void twitter_post_status(struct im_connection *ic, char* msg) 650 707 { 651 struct twitter_data *td = ic->proto_data;652 653 708 char* args[2]; 654 709 args[0] = "status"; 655 710 args[1] = msg; 656 twitter_http( TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1, td->user, td->pass, td->oauth_info, args, 2);711 twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post, ic, 1, args, 2); 657 712 // g_free(args[1]); 658 713 } … … 664 719 void twitter_direct_messages_new(struct im_connection *ic, char *who, char *msg) 665 720 { 666 struct twitter_data *td = ic->proto_data;667 668 721 char* args[4]; 669 722 args[0] = "screen_name"; … … 672 725 args[3] = msg; 673 726 // Use the same callback as for twitter_post_status, since it does basically the same. 674 twitter_http( TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, td->user, td->pass, td->oauth_info, args, 4);727 twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post, ic, 1, args, 4); 675 728 // g_free(args[1]); 676 729 // g_free(args[3]); … … 679 732 void twitter_friendships_create_destroy(struct im_connection *ic, char *who, int create) 680 733 { 681 struct twitter_data *td = ic->proto_data;682 683 734 char* args[2]; 684 735 args[0] = "screen_name"; 685 736 args[1] = who; 686 twitter_http(create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL, twitter_http_post, ic, 1, td->user, td->pass, td->oauth_info, args, 2); 687 } 688 689 690 737 twitter_http(ic, create ? TWITTER_FRIENDSHIPS_CREATE_URL : TWITTER_FRIENDSHIPS_DESTROY_URL, twitter_http_post, ic, 1, args, 2); 738 }
Note: See TracChangeset
for help on using the changeset viewer.