Ignore:
Timestamp:
2012-11-25T17:43:13Z (11 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
9ed0081
Parents:
7f557d5
Message:

Finally cleaned up the show-tweet functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter_lib.c

    r7f557d5 r29f72b7  
    463463                if (strcmp("text", k) == 0 && v->type == json_string) {
    464464                        txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1);
     465                        strip_html(txs->text);
    465466                } else if (strcmp("retweeted_status", k) == 0 && v->type == json_object) {
    466467                        rt = v;
     
    520521                if (strcmp("text", k) == 0 && v->type == json_string) {
    521522                        txs->text = g_memdup(v->u.string.ptr, v->u.string.length + 1);
     523                        strip_html(txs->text);
    522524                } else if (strcmp("created_at", k) == 0 && v->type == json_string) {
    523525                        struct tm parsed;
     
    567569                       
    568570                        *pos = '\0';
    569                         new = g_strdup_printf("%s%s &lt;%s&gt;%s", text, kort,
     571                        new = g_strdup_printf("%s%s <%s>%s", text, kort,
    570572                                              disp, pos + strlen(kort));
    571573                       
     
    659661 * Function that is called to see the statuses in a groupchat window.
    660662 */
    661 static void twitter_groupchat(struct im_connection *ic, GSList * list)
    662 {
    663         struct twitter_data *td = ic->proto_data;
    664         GSList *l = NULL;
    665         struct twitter_xml_status *status;
     663static void twitter_status_show_chat(struct im_connection *ic, struct twitter_xml_status *status)
     664{
     665        struct twitter_data *td = ic->proto_data;
    666666        struct groupchat *gc;
    667         guint64 last_id = 0;
     667        gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0;
     668        char *msg;
    668669
    669670        // Create a new groupchat if it does not exsist.
    670671        gc = twitter_groupchat_init(ic);
    671672
    672         for (l = list; l; l = g_slist_next(l)) {
    673                 char *msg;
    674 
    675                 status = l->data;
    676                 if (status->user == NULL || status->text == NULL || last_id == status->id)
    677                         continue;
    678 
    679                 last_id = status->id;
    680 
    681                 strip_html(status->text);
    682 
    683                 if (set_getbool(&ic->acc->set, "strip_newlines"))
    684                         strip_newlines(status->text);
    685 
    686                 msg = twitter_msg_add_id(ic, status, "");
    687 
    688                 // Say it!
    689                 if (g_strcasecmp(td->user, status->user->screen_name) == 0) {
    690                         imcb_chat_log(gc, "You: %s", msg ? msg : status->text);
    691                 } else {
    692                         twitter_add_buddy(ic, status->user->screen_name, status->user->name);
    693 
    694                         imcb_chat_msg(gc, status->user->screen_name,
    695                                       msg ? msg : status->text, 0, status->created_at);
    696                 }
    697 
    698                 g_free(msg);
    699 
    700                 // Update the timeline_id to hold the highest id, so that by the next request
    701                 // we won't pick up the updates already in the list.
    702                 td->timeline_id = MAX(td->timeline_id, status->id);
    703         }
     673        if (!me)
     674                /* MUST be done before twitter_msg_add_id() to avoid #872. */
     675                twitter_add_buddy(ic, status->user->screen_name, status->user->name);
     676        msg = twitter_msg_add_id(ic, status, "");
     677       
     678        // Say it!
     679        if (me) {
     680                imcb_chat_log(gc, "You: %s", msg ? msg : status->text);
     681        } else {
     682                imcb_chat_msg(gc, status->user->screen_name,
     683                              msg ? msg : status->text, 0, status->created_at);
     684        }
     685
     686        g_free(msg);
    704687}
    705688
     
    707690 * Function that is called to see statuses as private messages.
    708691 */
    709 static void twitter_private_message_chat(struct im_connection *ic, GSList * list)
    710 {
    711         struct twitter_data *td = ic->proto_data;
    712         GSList *l = NULL;
    713         struct twitter_xml_status *status;
     692static void twitter_status_show_msg(struct im_connection *ic, struct twitter_xml_status *status)
     693{
     694        struct twitter_data *td = ic->proto_data;
    714695        char from[MAX_STRING] = "";
    715         guint64 last_id = 0;
     696        char *prefix = NULL, *text = NULL;
     697        gboolean me = g_strcasecmp(td->user, status->user->screen_name) == 0;
    716698
    717699        if (td->flags & TWITTER_MODE_ONE) {
     
    720702        }
    721703
    722         for (l = list; l; l = g_slist_next(l)) {
    723                 char *prefix = NULL, *text = NULL;
    724 
    725                 status = l->data;
    726                 if (status->user == NULL || status->text == NULL || last_id == status->id)
    727                         continue;
    728 
    729                 last_id = status->id;
    730 
    731                 strip_html(status->text);
    732                 if (td->flags & TWITTER_MODE_ONE)
    733                         prefix = g_strdup_printf("\002<\002%s\002>\002 ",
    734                                                  status->user->screen_name);
    735                 else
    736                         twitter_add_buddy(ic, status->user->screen_name, status->user->name);
    737 
    738                 text = twitter_msg_add_id(ic, status, prefix ? prefix : "");
    739 
    740                 imcb_buddy_msg(ic,
    741                                *from ? from : status->user->screen_name,
    742                                text ? text : status->text, 0, status->created_at);
    743 
    744                 // Update the timeline_id to hold the highest id, so that by the next request
    745                 // we won't pick up the updates already in the list.
    746                 td->timeline_id = MAX(td->timeline_id, status->id);
    747 
    748                 g_free(text);
    749                 g_free(prefix);
    750         }
     704        if (td->flags & TWITTER_MODE_ONE)
     705                prefix = g_strdup_printf("\002<\002%s\002>\002 ",
     706                                         status->user->screen_name);
     707        else if (!me)
     708                twitter_add_buddy(ic, status->user->screen_name, status->user->name);
     709        else
     710                prefix = g_strdup("You: ");
     711
     712        text = twitter_msg_add_id(ic, status, prefix ? prefix : "");
     713
     714        imcb_buddy_msg(ic,
     715                       *from ? from : status->user->screen_name,
     716                       text ? text : status->text, 0, status->created_at);
     717
     718        g_free(text);
     719        g_free(prefix);
     720}
     721
     722static void twitter_status_show(struct im_connection *ic, struct twitter_xml_status *status)
     723{
     724        struct twitter_data *td = ic->proto_data;
     725       
     726        if (status->user == NULL || status->text == NULL)
     727                return;
     728       
     729        /* Grrrr. Would like to do this during parsing, but can't access
     730           settings from there. */
     731        if (set_getbool(&ic->acc->set, "strip_newlines"))
     732                strip_newlines(status->text);
     733       
     734        if (td->flags & TWITTER_MODE_CHAT)
     735                twitter_status_show_chat(ic, status);
     736        else
     737                twitter_status_show_msg(ic, status);
     738
     739        // Update the timeline_id to hold the highest id, so that by the next request
     740        // we won't pick up the updates already in the list.
     741        td->timeline_id = MAX(td->timeline_id, status->id);
    751742}
    752743
     
    848839        for (i = 0; i < TWITTER_LOG_LENGTH; i++) {
    849840                if (td->log[i].id == txs->id) {
    850                         /* Got a duplicate (RT, surely). Drop it. */
     841                        /* Got a duplicate (RT, probably). Drop it. */
    851842                        txs_free(txs);
    852843                        return TRUE;
     
    865856        }
    866857       
    867         GSList *output = g_slist_append(NULL, txs);
    868         twitter_groupchat(ic, output);
     858        twitter_status_show(ic, txs);
    869859        txs_free(txs);
    870         g_slist_free(output);
     860       
    871861        return TRUE;
    872862}
     
    951941        struct twitter_xml_list *home_timeline = td->home_timeline_obj;
    952942        struct twitter_xml_list *mentions = td->mentions_obj;
     943        guint64 last_id = 0;
    953944        GSList *output = NULL;
    954945        GSList *l;
    955946
     947        imcb_connected(ic);
     948       
    956949        if (!(td->flags & TWITTER_GOT_TIMELINE)) {
    957950                return;
     
    977970                }
    978971        }
    979        
    980         if (!(ic->flags & OPT_LOGGED_IN))
    981                 imcb_connected(ic);
    982972
    983973        // See if the user wants to see the messages in a groupchat window or as private messages.
    984         if (td->flags & TWITTER_MODE_CHAT)
    985                 twitter_groupchat(ic, output);
    986         else
    987                 twitter_private_message_chat(ic, output);
    988 
    989         g_slist_free(output);
     974        while (output) {
     975                struct twitter_xml_status *txs = output->data;
     976                if (txs->id != last_id)
     977                        twitter_status_show(ic, txs);
     978                last_id = txs->id;
     979                output = g_slist_remove(output, txs);
     980        }
    990981
    991982        txl_free(home_timeline);
Note: See TracChangeset for help on using the changeset viewer.