Ignore:
Timestamp:
2015-05-28T05:26:24Z (9 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
faeb521
Parents:
3d31618
git-author:
Artem Savkov <artem.savkov@…> (28-03-15 01:23:42)
git-committer:
dequis <dx@…> (28-05-15 05:26:24)
Message:

Gmail notifications support through new imcb_notify_email() API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/jabber/iq.c

    r3d31618 rdd43c62  
    2727static xt_status jabber_parse_roster(struct im_connection *ic, struct xt_node *node, struct xt_node *orig);
    2828static xt_status jabber_iq_display_vcard(struct im_connection *ic, struct xt_node *node, struct xt_node *orig);
     29static xt_status jabber_gmail_handle_new(struct im_connection *ic, struct xt_node *node);
    2930
    3031xt_status jabber_pkt_iq(struct xt_node *node, gpointer data)
     
    141142                    (strcmp(s, XMLNS_SI) == 0)) {
    142143                        return jabber_si_handle_request(ic, node, c);
     144                } else if ((c = xt_find_node(node->children, "new-mail")) &&
     145                           (s = xt_find_attr(c, "xmlns")) &&
     146                           (strcmp(s, XMLNS_GMAILNOTIFY) == 0)) {
     147                        return jabber_gmail_handle_new(ic, node);
    143148                } else if (!(c = xt_find_node(node->children, "query")) ||
    144149                           !(s = xt_find_attr(c, "xmlns"))) {
     
    342347                        return XT_ABORT;
    343348                }
     349                if (jd->flags & JFLAG_GMAILNOTIFY && node == NULL) {
     350                        jabber_iq_query_server(ic, jd->server, XMLNS_DISCO_INFO);
     351                }
    344352        } else if ((jd->flags & (JFLAG_WANT_BIND | JFLAG_WANT_SESSION)) == 0) {
    345353                if (!jabber_get_roster(ic)) {
     
    369377
    370378        return st;
     379}
     380
     381xt_status jabber_iq_query_gmail(struct im_connection *ic);
     382
     383static xt_status jabber_gmail_handle_new(struct im_connection *ic, struct xt_node *node)
     384{
     385        struct xt_node *response;
     386        struct jabber_data *jd = ic->proto_data;
     387
     388        response = jabber_make_packet("iq", "result", g_strdup_printf("%s@%s", jd->username, jd->server), NULL);
     389
     390        jabber_cache_add(ic, response, NULL);
     391        if (!jabber_write_packet(ic, response)) {
     392                return XT_ABORT;
     393        }
     394
     395        jabber_iq_query_gmail(ic);
     396
     397        return XT_HANDLED;
    371398}
    372399
     
    710737}
    711738
     739xt_status jabber_iq_parse_gmail(struct im_connection *ic, struct xt_node *node, struct xt_node *orig);
     740
     741xt_status jabber_iq_query_gmail(struct im_connection *ic)
     742{
     743        struct xt_node *node, *query;
     744        struct jabber_data *jd = ic->proto_data;
     745
     746        node = xt_new_node("query", NULL, NULL);
     747        xt_add_attr(node, "xmlns", XMLNS_GMAILNOTIFY);
     748        if (jd->gmail_time) {
     749                char *formatted = g_strdup_printf("%" G_GUINT64_FORMAT, (jd->gmail_time + 1));
     750                xt_add_attr(node, "newer-than-time", formatted);
     751                g_free(formatted);
     752        }
     753        if (jd->gmail_tid) {
     754                xt_add_attr(node, "newer-than-tid", jd->gmail_tid);
     755        }
     756
     757        if (!(query = jabber_make_packet("iq", "get", jd->me, node))) {
     758                imcb_log(ic, "WARNING: Couldn't generate server query");
     759                xt_free_node(node);
     760        }
     761
     762        jabber_cache_add(ic, query, jabber_iq_parse_gmail);
     763
     764        return jabber_write_packet(ic, query) ? XT_HANDLED : XT_ABORT;
     765}
     766
    712767xt_status jabber_iq_parse_server_features(struct im_connection *ic, struct xt_node *node, struct xt_node *orig);
    713768
     
    729784
    730785        return jabber_write_packet(ic, query) ? XT_HANDLED : XT_ABORT;
     786}
     787
     788xt_status jabber_iq_parse_gmail(struct im_connection *ic, struct xt_node *node, struct xt_node *orig)
     789{
     790        struct xt_node *c;
     791        struct jabber_data *jd = ic->proto_data;
     792        char *xmlns, *from;
     793        guint64 l_time = 0;
     794        char *tid = NULL;
     795
     796        if (!(c = xt_find_node(node->children, "mailbox")) ||
     797            !(from = xt_find_attr(node, "from")) ||
     798            !(xmlns = xt_find_attr(c, "xmlns")) ||
     799            (g_strcmp0(xmlns, XMLNS_GMAILNOTIFY) != 0)) {
     800                imcb_log(ic, "WARNING: Received incomplete mailbox packet for gmail notify");
     801                return XT_HANDLED;
     802        }
     803
     804        c = c->children;
     805
     806        while ((c = xt_find_node(c, "mail-thread-info"))) {
     807                struct xt_node *thread, *s;
     808                char *subject = NULL;
     809                char *snippet = NULL;
     810                char *msg = NULL;
     811                guint64 t_time;
     812
     813                t_time = g_ascii_strtoull(xt_find_attr(c, "date"), NULL, 10);
     814                if (t_time && t_time > l_time) {
     815                        l_time = t_time;
     816                        tid = xt_find_attr(c, "tid");
     817                }
     818
     819                thread = c->children;
     820
     821                if ((s = xt_find_node(thread, "subject"))) {
     822                        subject = s->text;
     823                }
     824
     825                if ((s = xt_find_node(thread, "snippet"))) {
     826                        snippet = s->text;
     827                }
     828
     829                if (subject) {
     830                        msg = g_strdup_printf("New mail for %s. Subj: %s", from, subject);
     831                } else {
     832                        msg = g_strdup_printf("New mail for %s.", from);
     833                }
     834                imcb_notify_email(ic, set_getstr(&ic->acc->set, "notify_handle"), msg, 0, 0);
     835
     836                if (snippet) {
     837                        imcb_notify_email(ic, set_getstr(&ic->acc->set, "notify_handle"), snippet, 0, 0);
     838                }
     839
     840                c = c->next;
     841                g_free(msg);
     842        }
     843
     844        if (l_time && (!jd->gmail_time || l_time > jd->gmail_time)) {
     845                jd->gmail_time = l_time;
     846                if (tid) {
     847                        g_free(jd->gmail_tid);
     848                        jd->gmail_tid = g_strdup(tid);
     849                }
     850        }
     851
     852        return XT_HANDLED;
    731853}
    732854
     
    781903                        c = c->next;
    782904                }
     905
     906                if (jd->flags & JFLAG_GMAILNOTIFY) {
     907                        /* search for gmail notification feature */
     908                        c = xt_find_node(node->children, "query");
     909                        c = c->children;
     910                        while ((c = xt_find_node(c, "feature"))) {
     911                                if (strcmp(xt_find_attr(c, "var"), XMLNS_GMAILNOTIFY) == 0) {
     912                                        jabber_iq_query_gmail(ic);
     913                                }
     914                                c = c->next;
     915                        }
     916                }
     917
    783918        } else if (strcmp(xmlns, XMLNS_BYTESTREAMS) == 0) {
    784919                char *host, *jid, *port_s;
Note: See TracChangeset for help on using the changeset viewer.