Changeset d1356cb for protocols


Ignore:
Timestamp:
2012-11-12T23:57:43Z (11 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
b006464
Parents:
aef2077
Message:

Decode incoming DMs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter_lib.c

    raef2077 rd1356cb  
    448448#endif
    449449
     450static char* expand_entities(char* text, const json_value *entities);
     451
    450452/**
    451453 * Function to fill a twitter_xml_status struct.
     
    500502                txs_free(rtxs);
    501503        } else if (entities) {
    502                 JSON_O_FOREACH (entities, k, v) {
    503                         int i;
    504                        
    505                         if (v->type != json_array)
    506                                 continue;
    507                         if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0)
     504                txs->text = expand_entities(txs->text, entities);
     505        }
     506
     507        return txs->text && txs->user && txs->id;
     508}
     509
     510/**
     511 * Function to fill a twitter_xml_status struct (DM variant).
     512 */
     513static gboolean twitter_xt_get_dm(const json_value *node, struct twitter_xml_status *txs)
     514{
     515        const json_value *entities = NULL;
     516       
     517        if (node->type != json_object)
     518                return FALSE;
     519
     520        JSON_O_FOREACH (node, k, v) {
     521                if (strcmp("text", k) == 0 && v->type == json_string) {
     522                        txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1);
     523                } else if (strcmp("created_at", k) == 0 && v->type == json_string) {
     524                        struct tm parsed;
     525
     526                        /* Very sensitive to changes to the formatting of
     527                           this field. :-( Also assumes the timezone used
     528                           is UTC since C time handling functions suck. */
     529                        if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL)
     530                                txs->created_at = mktime_utc(&parsed);
     531                } else if (strcmp("sender", k) == 0 && v->type == json_object) {
     532                        txs->user = twitter_xt_get_user(v);
     533                } else if (strcmp("id", k) == 0 && v->type == json_integer) {
     534                        txs->id = v->u.integer;
     535                }
     536        }
     537
     538        if (entities) {
     539                txs->text = expand_entities(txs->text, entities);
     540        }
     541
     542        return txs->text && txs->user && txs->id;
     543}
     544
     545static char* expand_entities(char* text, const json_value *entities) {
     546        JSON_O_FOREACH (entities, k, v) {
     547                int i;
     548               
     549                if (v->type != json_array)
     550                        continue;
     551                if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0)
     552                        continue;
     553               
     554                for (i = 0; i < v->u.array.length; i ++) {
     555                        if (v->u.array.values[i]->type != json_object)
    508556                                continue;
    509557                       
    510                         for (i = 0; i < v->u.array.length; i ++) {
    511                                 if (v->u.array.values[i]->type != json_object)
    512                                         continue;
    513                                
    514                                 const char *kort = json_o_str(v->u.array.values[i], "url");
    515                                 const char *disp = json_o_str(v->u.array.values[i], "display_url");
    516                                 char *pos, *new;
    517                                
    518                                 if (!kort || !disp || !(pos = strstr(txs->text, kort)))
    519                                         continue;
    520                                
    521                                 *pos = '\0';
    522                                 new = g_strdup_printf("%s%s &lt;%s&gt;%s", txs->text, kort,
    523                                                       disp, pos + strlen(kort));
    524                                
    525                                 g_free(txs->text);
    526                                 txs->text = new;
    527                         }
     558                        const char *kort = json_o_str(v->u.array.values[i], "url");
     559                        const char *disp = json_o_str(v->u.array.values[i], "display_url");
     560                        char *pos, *new;
     561                       
     562                        if (!kort || !disp || !(pos = strstr(text, kort)))
     563                                continue;
     564                       
     565                        *pos = '\0';
     566                        new = g_strdup_printf("%s%s &lt;%s&gt;%s", text, kort,
     567                                              disp, pos + strlen(kort));
     568                       
     569                        g_free(text);
     570                        text = new;
    528571                }
    529572        }
    530 
    531         return txs->text && txs->user && txs->id;
     573       
     574        return text;
    532575}
    533576
     
    785828{
    786829        struct twitter_xml_status *txs = g_new0(struct twitter_xml_status, 1);
     830        json_value *c;
    787831       
    788832        if (twitter_xt_get_status(o, txs)) {
    789833                GSList *output = g_slist_append(NULL, txs);
    790834                twitter_groupchat(ic, output);
     835                txs_free(txs);
     836                g_slist_free(output);
     837                return TRUE;
     838        } else if ((c = json_o_get(o, "direct_message")) &&
     839                   twitter_xt_get_dm(c, txs)) {
     840                GSList *output = g_slist_append(NULL, txs);
     841                twitter_private_message_chat(ic, output);
    791842                txs_free(txs);
    792843                g_slist_free(output);
Note: See TracChangeset for help on using the changeset viewer.