Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter_lib.c

    r2a6da96 r11ec078  
    55*                                                                           *
    66*  Copyright 2009-2010 Geert Mulders <g.c.w.m.mulders@gmail.com>            *
    7 *  Copyright 2010-2012 Wilmer van der Gaast <wilmer@gaast.net>              *
     7*  Copyright 2010-2011 Wilmer van der Gaast <wilmer@gaast.net>              *
    88*                                                                           *
    99*  This library is free software; you can redistribute it and/or            *
     
    188188}
    189189
    190 static struct xt_node *twitter_parse_response(struct im_connection *ic, struct http_request *req)
    191 {
    192         gboolean logging_in = !(ic->flags & OPT_LOGGED_IN);
    193         gboolean periodic;
    194         struct twitter_data *td = ic->proto_data;
    195         struct xt_node *ret;
    196         char path[64] = "", *s;
    197        
    198         if ((s = strchr(req->request, ' '))) {
    199                 path[sizeof(path)-1] = '\0';
    200                 strncpy(path, s + 1, sizeof(path) - 1);
    201                 if ((s = strchr(path, '?')) || (s = strchr(path, ' ')))
    202                         *s = '\0';
    203         }
    204        
    205         /* Kinda nasty. :-( Trying to suppress error messages, but only
    206            for periodic (i.e. mentions/timeline) queries. */
    207         periodic = strstr(path, "timeline") || strstr(path, "mentions");
    208        
    209         if (req->status_code == 401 && logging_in) {
    210                 /* IIRC Twitter once had an outage where they were randomly
    211                    throwing 401s so I'll keep treating this one as fatal
    212                    only during login. */
    213                 imcb_error(ic, "Authentication failure");
    214                 imc_logout(ic, FALSE);
    215                 return NULL;
    216         } else if (req->status_code != 200) {
    217                 // It didn't go well, output the error and return.
    218                 if (!periodic || logging_in || ++td->http_fails >= 5)
    219                         imcb_error(ic, "Could not retrieve %s: %s",
    220                                    path, twitter_parse_error(req));
    221                
    222                 if (logging_in)
    223                         imc_logout(ic, TRUE);
    224                 return NULL;
    225         } else {
    226                 td->http_fails = 0;
    227         }
    228 
    229         if ((ret = xt_from_string(req->reply_body, req->body_size)) == NULL) {
    230                 imcb_error(ic, "Could not retrieve %s: %s",
    231                            path, "XML parse error");
    232         }
    233         return ret;
    234 }
    235 
    236190static void twitter_http_get_friends_ids(struct http_request *req);
    237191
     
    312266        td = ic->proto_data;
    313267
     268        // Check if the HTTP request went well. More strict checks as this is
     269        // the first request we do in a session.
     270        if (req->status_code == 401) {
     271                imcb_error(ic, "Authentication failure");
     272                imc_logout(ic, FALSE);
     273                return;
     274        } else if (req->status_code != 200) {
     275                // It didn't go well, output the error and return.
     276                imcb_error(ic, "Could not retrieve %s: %s",
     277                           TWITTER_FRIENDS_IDS_URL, twitter_parse_error(req));
     278                imc_logout(ic, TRUE);
     279                return;
     280        } else {
     281                td->http_fails = 0;
     282        }
     283
    314284        /* Create the room now that we "logged in". */
    315285        if (!td->timeline_gc && g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "chat") == 0)
     
    320290
    321291        // Parse the data.
    322         if (!(parsed = twitter_parse_response(ic, req)))
    323                 return;
     292        parsed = xt_from_string(req->reply_body, req->body_size);
    324293        twitter_xt_get_friends_id_list(parsed, txl);
    325294        xt_free_node(parsed);
     
    379348{
    380349        struct im_connection *ic = req->data;
     350        struct twitter_data *td;
    381351        struct xt_node *parsed;
    382352        struct twitter_xml_list *txl;
     
    388358                return;
    389359
     360        td = ic->proto_data;
     361
     362        if (req->status_code != 200) {
     363                // It didn't go well, output the error and return.
     364                imcb_error(ic, "Could not retrieve %s: %s",
     365                           TWITTER_USERS_LOOKUP_URL, twitter_parse_error(req));
     366                imc_logout(ic, TRUE);
     367                return;
     368        } else {
     369                td->http_fails = 0;
     370        }
     371
    390372        txl = g_new0(struct twitter_xml_list, 1);
    391373        txl->list = NULL;
    392374
     375        // Parse the data.
     376        parsed = xt_from_string(req->reply_body, req->body_size);
     377
    393378        // Get the user list from the parsed xml feed.
    394         if (!(parsed = twitter_parse_response(ic, req)))
    395                 return;
    396379        twitter_xt_get_users(parsed, txl);
    397380        xt_free_node(parsed);
     
    804787                }
    805788        }
    806        
    807         if (!(ic->flags & OPT_LOGGED_IN))
    808                 imcb_connected(ic);
    809789
    810790        // See if the user wants to see the messages in a groupchat window or as private messages.
     
    914894        td = ic->proto_data;
    915895
     896        // Check if the HTTP request went well.
     897        if (req->status_code == 200) {
     898                td->http_fails = 0;
     899                if (!(ic->flags & OPT_LOGGED_IN))
     900                        imcb_connected(ic);
     901        } else {
     902                // It didn't go well, output the error and return.
     903                if (++td->http_fails >= 5)
     904                        imcb_error(ic, "Could not retrieve %s: %s",
     905                                   TWITTER_HOME_TIMELINE_URL, twitter_parse_error(req));
     906
     907                goto end;
     908        }
     909
    916910        txl = g_new0(struct twitter_xml_list, 1);
    917911        txl->list = NULL;
    918912
     913        // Parse the data.
     914        parsed = xt_from_string(req->reply_body, req->body_size);
    919915        // The root <statuses> node should hold the list of statuses <status>
    920         if (!(parsed = twitter_parse_response(ic, req)))
    921                 goto end;
    922916        twitter_xt_get_status_list(ic, parsed, txl);
    923917        xt_free_node(parsed);
     
    947941        td = ic->proto_data;
    948942
     943        // Check if the HTTP request went well.
     944        if (req->status_code == 200) {
     945                td->http_fails = 0;
     946                if (!(ic->flags & OPT_LOGGED_IN))
     947                        imcb_connected(ic);
     948        } else {
     949                // It didn't go well, output the error and return.
     950                if (++td->http_fails >= 5)
     951                        imcb_error(ic, "Could not retrieve %s: %s",
     952                                   TWITTER_MENTIONS_URL, twitter_parse_error(req));
     953
     954                goto end;
     955        }
     956
    949957        txl = g_new0(struct twitter_xml_list, 1);
    950958        txl->list = NULL;
    951959
     960        // Parse the data.
     961        parsed = xt_from_string(req->reply_body, req->body_size);
    952962        // The root <statuses> node should hold the list of statuses <status>
    953         if (!(parsed = twitter_parse_response(ic, req)))
    954                 goto end;
    955963        twitter_xt_get_status_list(ic, parsed, txl);
    956964        xt_free_node(parsed);
     
    972980        struct im_connection *ic = req->data;
    973981        struct twitter_data *td;
    974         struct xt_node *parsed, *node;
    975982
    976983        // Check if the connection is still active.
     
    981988        td->last_status_id = 0;
    982989
    983         if (!(parsed = twitter_parse_response(ic, req)))
    984                 return;
    985        
    986         if ((node = xt_find_node(parsed, "status")) &&
    987             (node = xt_find_node(node->children, "id")) && node->text)
    988                 td->last_status_id = g_ascii_strtoull(node->text, NULL, 10);
     990        // Check if the HTTP request went well.
     991        if (req->status_code != 200) {
     992                // It didn't go well, output the error and return.
     993                imcb_error(ic, "HTTP error: %s", twitter_parse_error(req));
     994                return;
     995        }
     996
     997        if (req->body_size > 0) {
     998                struct xt_node *parsed, *node;
     999
     1000                parsed = xt_from_string(req->reply_body, req->body_size);
     1001
     1002                if ((node = xt_find_node(parsed, "status")) &&
     1003                    (node = xt_find_node(node->children, "id")) && node->text)
     1004                        td->last_status_id = g_ascii_strtoull(node->text, NULL, 10);
     1005
     1006                xt_free_node(parsed);
     1007        }
    9891008}
    9901009
Note: See TracChangeset for help on using the changeset viewer.