close Warning: Failed to sync with repository "(default)": [Errno 12] Cannot allocate memory; repository information may be out of date. Look in the Trac log for more information including mitigation strategies.

Ticket #1244: history-since-last-message_3.4.1.patch

File history-since-last-message_3.4.1.patch, 2.7 KB (added by cbay, at 2016-01-17T17:14:15Z)
  • protocols/jabber/conference.c

    diff -ru bitlbee-3.4.1/protocols/jabber/conference.c bitlbee-3.4.1~/protocols/jabber/conference.c
    old new  
    2626
    2727static xt_status jabber_chat_join_failed(struct im_connection *ic, struct xt_node *node, struct xt_node *orig);
    2828
     29void jabber_chat_update_last_message(struct groupchat *chat, time_t date)
     30{
     31        char path[512];
     32        FILE *fp;
     33
     34        g_snprintf(path, sizeof(path) - 1, "%slast_message_%s_%s", global.conf->configdir, chat->ic->acc->tag, chat->title);
     35
     36        if ((fp = fopen(path, "w"))) {
     37                fprintf(fp, "%ld", (long)date);
     38                fclose(fp);
     39        }
     40}
     41
     42char *jabber_chat_get_last_message(struct groupchat *chat)
     43{
     44        char path[512];
     45        char *buf = NULL;
     46        time_t date;
     47        FILE *fp;
     48
     49        g_snprintf(path, sizeof(path) - 1, "%slast_message_%s_%s", global.conf->configdir, chat->ic->acc->tag, chat->title);
     50
     51        if ((fp = fopen(path, "r"))) {
     52                fscanf(fp, "%ld", &date);
     53                /* receive messages starting one second after this date */
     54                date += 1;
     55                buf = g_malloc(64);
     56                strftime(buf, 63, "%Y-%m-%dT%H:%M:%SZ", gmtime(&date));
     57                fclose(fp);
     58        }
     59       
     60        return buf;
     61}
     62
    2963struct groupchat *jabber_chat_join(struct im_connection *ic, const char *room, const char *nick, const char *password)
    3064{
    3165        struct jabber_chat *jc;
    3266        struct xt_node *node;
    3367        struct groupchat *c;
    3468        char *roomjid;
     69        char *last_message_date;
     70
     71        c = imcb_chat_new(ic, room);
    3572
    3673        roomjid = g_strdup_printf("%s/%s", room, nick);
    3774        node = xt_new_node("x", NULL, NULL);
     
    3976        if (password) {
    4077                xt_add_child(node, xt_new_node("password", password, NULL));
    4178        }
     79       
     80        last_message_date = jabber_chat_get_last_message(c);
     81        if (last_message_date) {
     82                struct xt_node *history;
     83                history = xt_new_node("history", NULL, NULL);
     84                xt_add_attr(history, "since", last_message_date);
     85                xt_add_child(node, history);
     86                g_free(last_message_date);
     87        }
     88       
    4289        node = jabber_make_packet("presence", NULL, roomjid, node);
    4390        jabber_cache_add(ic, node, jabber_chat_join_failed);
    4491
     
    61108           of the nick to send a proper presence update. */
    62109        jc->my_full_jid = roomjid;
    63110
    64         c = imcb_chat_new(ic, room);
    65111        c->data = jc;
    66112
    67113        return c;
     
    359405        char *nick = NULL;
    360406        char *final_from = NULL;
    361407        char *bare_jid = NULL;
     408        time_t date;
    362409
    363410        from = (bud) ? bud->full_jid : xt_find_attr(node, "from");
    364411
     
    433480        } else {
    434481                final_from = nick;
    435482        }
    436 
    437         imcb_chat_msg(chat, final_from, body->text, 0, jabber_get_timestamp(node));
     483       
     484        date = jabber_get_timestamp(node);
     485        jabber_chat_update_last_message(chat, date);
     486        imcb_chat_msg(chat, final_from, body->text, 0, date);
    438487
    439488        g_free(bare_jid);
    440489}