diff options
Diffstat (limited to 'protocols/jabber')
-rw-r--r-- | protocols/jabber/conference.c | 120 | ||||
-rw-r--r-- | protocols/jabber/iq.c | 125 | ||||
-rw-r--r-- | protocols/jabber/jabber.c | 18 | ||||
-rw-r--r-- | protocols/jabber/jabber.h | 5 | ||||
-rw-r--r-- | protocols/jabber/jabber_util.c | 16 | ||||
-rw-r--r-- | protocols/jabber/presence.c | 20 |
6 files changed, 248 insertions, 56 deletions
diff --git a/protocols/jabber/conference.c b/protocols/jabber/conference.c index bcf05b3c..be63e115 100644 --- a/protocols/jabber/conference.c +++ b/protocols/jabber/conference.c @@ -353,78 +353,88 @@ void jabber_chat_pkt_message(struct im_connection *ic, struct jabber_buddy *bud, { struct xt_node *subject = xt_find_node(node->children, "subject"); struct xt_node *body = xt_find_node(node->children, "body"); - struct groupchat *chat = bud ? jabber_chat_by_jid(ic, bud->bare_jid) : NULL; - struct jabber_chat *jc = chat ? chat->data : NULL; - char *s; + struct groupchat *chat = NULL; + struct jabber_chat *jc = NULL; + char *from = NULL; + char *nick = NULL; + char *final_from = NULL; + char *bare_jid = NULL; - if (subject && chat) { - s = (bud && bud->ext_jid) ? strchr(bud->ext_jid, '/') : NULL; - if (s) { - *s = 0; + from = (bud) ? bud->full_jid : xt_find_attr(node, "from"); + + if (from) { + nick = strchr(from, '/'); + if (nick) { + *nick = 0; } - imcb_chat_topic(chat, bud ? bud->ext_jid : NULL, subject->text_len > 0 ? - subject->text : NULL, jabber_get_timestamp(node)); - if (s) { - *s = '/'; + chat = jabber_chat_by_jid(ic, from); + if (nick) { + *nick = '/'; + nick++; } } - if (bud == NULL || (jc && ~jc->flags & JCFLAG_MESSAGE_SENT && bud == jc->me)) { - char *nick; + jc = (chat) ? chat->data : NULL; - if (body == NULL || body->text_len == 0) { - /* Meh. Empty messages aren't very interesting, no matter - how much some servers love to send them. */ - return; + if (!bud) { + struct xt_node *c; + char *s; + + /* Try some clever stuff to find out the real JID here */ + c = xt_find_node_by_attr(node->children, "delay", "xmlns", XMLNS_DELAY); + + if (c && ((s = xt_find_attr(c, "from")) || + (s = xt_find_attr(c, "from_jid")))) { + /* This won't be useful if it's the MUC JID */ + if (!(jc && jabber_compare_jid(s, jc->name))) { + /* Hopefully this one makes more sense! */ + bud = jabber_buddy_by_jid(ic, s, GET_BUDDY_FIRST | GET_BUDDY_CREAT); + } } - s = xt_find_attr(node, "from"); /* pkt_message() already NULL-checked this one. */ - nick = strchr(s, '/'); - if (nick) { - /* If this message included a resource/nick we don't know, - we might still know the groupchat itself. */ - *nick = 0; - chat = jabber_chat_by_jid(ic, s); - *nick = '/'; + } - nick++; - } else { - /* message.c uses the EXACT_JID option, so bud should - always be NULL here for bare JIDs. */ - chat = jabber_chat_by_jid(ic, s); + if (subject && chat) { + char *subject_text = subject->text_len > 0 ? subject->text : NULL; + if (g_strcmp0(chat->topic, subject_text) != 0) { + bare_jid = (bud) ? jabber_get_bare_jid(bud->ext_jid) : NULL; + imcb_chat_topic(chat, bare_jid, subject_text, + jabber_get_timestamp(node)); + g_free(bare_jid); } + } + if (body == NULL || body->text_len == 0) { + /* Meh. Empty messages aren't very interesting, no matter + how much some servers love to send them. */ + return; + } + + if (chat == NULL) { if (nick == NULL) { - /* This is fine, the groupchat itself isn't in jd->buddies. */ - if (chat) { - imcb_chat_log(chat, "From conference server: %s", body->text); - } else { - imcb_log(ic, "System message from unknown groupchat %s: %s", s, body->text); - } + imcb_log(ic, "System message from unknown groupchat %s: %s", from, body->text); } else { - /* This can happen too, at least when receiving a backlog when - just joining a channel. */ - if (chat) { - imcb_chat_log(chat, "Message from unknown participant %s: %s", nick, body->text); - } else { - imcb_log(ic, "Groupchat message from unknown JID %s: %s", s, body->text); - } + imcb_log(ic, "Groupchat message from unknown JID %s: %s", from, body->text); } return; - } else if (chat == NULL) { - /* How could this happen?? We could do kill( self, 11 ) - now or just wait for the OS to do it. :-) */ + } else if (chat != NULL && bud == NULL && nick == NULL) { + imcb_chat_log(chat, "From conference server: %s", body->text); + return; + } else if (jc && jc->flags & JCFLAG_MESSAGE_SENT && bud == jc->me) { + /* exclude self-messages since they would get filtered out + * but not the ones in the backlog */ return; } - if (body && body->text_len > 0) { - s = (bud->ext_jid) ? strchr(bud->ext_jid, '/') : NULL; - if (s) { - *s = 0; - } - imcb_chat_msg(chat, bud->ext_jid, body->text, 0, jabber_get_timestamp(node)); - if (s) { - *s = '/'; - } + + if (bud && jc && bud != jc->me) { + bare_jid = jabber_get_bare_jid(bud->ext_jid ? bud->ext_jid : bud->full_jid); + final_from = bare_jid; + } else { + final_from = nick; } + + imcb_chat_msg(chat, final_from, body->text, 0, jabber_get_timestamp(node)); + + g_free(bare_jid); } diff --git a/protocols/jabber/iq.c b/protocols/jabber/iq.c index 33889d32..327cc859 100644 --- a/protocols/jabber/iq.c +++ b/protocols/jabber/iq.c @@ -26,6 +26,7 @@ static xt_status jabber_parse_roster(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); static xt_status jabber_iq_display_vcard(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); +static xt_status jabber_gmail_handle_new(struct im_connection *ic, struct xt_node *node); xt_status jabber_pkt_iq(struct xt_node *node, gpointer data) { @@ -140,6 +141,10 @@ xt_status jabber_pkt_iq(struct xt_node *node, gpointer data) (s = xt_find_attr(c, "xmlns")) && (strcmp(s, XMLNS_SI) == 0)) { return jabber_si_handle_request(ic, node, c); + } else if ((c = xt_find_node(node->children, "new-mail")) && + (s = xt_find_attr(c, "xmlns")) && + (strcmp(s, XMLNS_GMAILNOTIFY) == 0)) { + return jabber_gmail_handle_new(ic, node); } else if (!(c = xt_find_node(node->children, "query")) || !(s = xt_find_attr(c, "xmlns"))) { return XT_HANDLED; @@ -341,6 +346,9 @@ xt_status jabber_pkt_bind_sess(struct im_connection *ic, struct xt_node *node, s if (!jabber_write_packet(ic, reply)) { return XT_ABORT; } + if (jd->flags & JFLAG_GMAILNOTIFY && node == NULL) { + jabber_iq_query_server(ic, jd->server, XMLNS_DISCO_INFO); + } } else if ((jd->flags & (JFLAG_WANT_BIND | JFLAG_WANT_SESSION)) == 0) { if (!jabber_get_roster(ic)) { return XT_ABORT; @@ -370,6 +378,25 @@ int jabber_get_roster(struct im_connection *ic) return st; } +xt_status jabber_iq_query_gmail(struct im_connection *ic); + +static xt_status jabber_gmail_handle_new(struct im_connection *ic, struct xt_node *node) +{ + struct xt_node *response; + struct jabber_data *jd = ic->proto_data; + + response = jabber_make_packet("iq", "result", g_strdup_printf("%s@%s", jd->username, jd->server), NULL); + + jabber_cache_add(ic, response, NULL); + if (!jabber_write_packet(ic, response)) { + return XT_ABORT; + } + + jabber_iq_query_gmail(ic); + + return XT_HANDLED; +} + static xt_status jabber_parse_roster(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) { struct jabber_data *jd = ic->proto_data; @@ -709,6 +736,34 @@ xt_status jabber_iq_parse_features(struct im_connection *ic, struct xt_node *nod return XT_HANDLED; } +xt_status jabber_iq_parse_gmail(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); + +xt_status jabber_iq_query_gmail(struct im_connection *ic) +{ + struct xt_node *node, *query; + struct jabber_data *jd = ic->proto_data; + + node = xt_new_node("query", NULL, NULL); + xt_add_attr(node, "xmlns", XMLNS_GMAILNOTIFY); + if (jd->gmail_time) { + char *formatted = g_strdup_printf("%" G_GUINT64_FORMAT, (jd->gmail_time + 1)); + xt_add_attr(node, "newer-than-time", formatted); + g_free(formatted); + } + if (jd->gmail_tid) { + xt_add_attr(node, "newer-than-tid", jd->gmail_tid); + } + + if (!(query = jabber_make_packet("iq", "get", jd->me, node))) { + imcb_log(ic, "WARNING: Couldn't generate server query"); + xt_free_node(node); + } + + jabber_cache_add(ic, query, jabber_iq_parse_gmail); + + return jabber_write_packet(ic, query) ? XT_HANDLED : XT_ABORT; +} + xt_status jabber_iq_parse_server_features(struct im_connection *ic, struct xt_node *node, struct xt_node *orig); xt_status jabber_iq_query_server(struct im_connection *ic, char *jid, char *xmlns) @@ -730,6 +785,63 @@ xt_status jabber_iq_query_server(struct im_connection *ic, char *jid, char *xmln return jabber_write_packet(ic, query) ? XT_HANDLED : XT_ABORT; } +xt_status jabber_iq_parse_gmail(struct im_connection *ic, struct xt_node *node, struct xt_node *orig) +{ + struct xt_node *c; + struct jabber_data *jd = ic->proto_data; + char *xmlns, *from; + guint64 l_time = 0; + char *tid = NULL; + int max = 0; + + if (!(c = xt_find_node(node->children, "mailbox")) || + !(from = xt_find_attr(node, "from")) || + !(xmlns = xt_find_attr(c, "xmlns")) || + (g_strcmp0(xmlns, XMLNS_GMAILNOTIFY) != 0)) { + imcb_log(ic, "WARNING: Received incomplete mailbox packet for gmail notify"); + return XT_HANDLED; + } + + max = set_getint(&ic->acc->set, "mail_notifications_limit"); + c = c->children; + + while ((max-- > 0) && (c = xt_find_node(c, "mail-thread-info"))) { + struct xt_node *s; + char *subject = "<no subject>"; + char *sender = "<no sender>"; + guint64 t_time; + + t_time = g_ascii_strtoull(xt_find_attr(c, "date"), NULL, 10); + if (t_time && t_time > l_time) { + l_time = t_time; + tid = xt_find_attr(c, "tid"); + } + + if ((s = xt_find_node(c->children, "senders")) && + (s = xt_find_node_by_attr(s->children, "sender", "unread", "1"))) { + sender = xt_find_attr(s, "name"); + } + + if ((s = xt_find_node(c->children, "subject")) && s->text) { + subject = s->text; + } + + imcb_notify_email(ic, "New mail from %s: %s", sender, subject); + + c = c->next; + } + + if (l_time && (!jd->gmail_time || l_time > jd->gmail_time)) { + jd->gmail_time = l_time; + if (tid) { + g_free(jd->gmail_tid); + jd->gmail_tid = g_strdup(tid); + } + } + + return XT_HANDLED; +} + /* * Query the server for "items", query each "item" for identities, query each "item" that's a proxy for it's bytestream info */ @@ -780,6 +892,19 @@ xt_status jabber_iq_parse_server_features(struct im_connection *ic, struct xt_no c = c->next; } + + if (jd->flags & JFLAG_GMAILNOTIFY) { + /* search for gmail notification feature */ + c = xt_find_node(node->children, "query"); + c = c->children; + while ((c = xt_find_node(c, "feature"))) { + if (strcmp(xt_find_attr(c, "var"), XMLNS_GMAILNOTIFY) == 0) { + jabber_iq_query_gmail(ic); + } + c = c->next; + } + } + } else if (strcmp(xmlns, XMLNS_BYTESTREAMS) == 0) { char *host, *jid, *port_s; int port; diff --git a/protocols/jabber/jabber.c b/protocols/jabber/jabber.c index 04db365e..431b3e54 100644 --- a/protocols/jabber/jabber.c +++ b/protocols/jabber/jabber.c @@ -99,8 +99,17 @@ static void jabber_init(account_t *acc) s = set_add(&acc->set, "user_agent", "BitlBee", NULL, acc); s = set_add(&acc->set, "xmlconsole", "false", set_eval_bool, acc); + + s = set_add(&acc->set, "mail_notifications", "false", set_eval_bool, acc); s->flags |= ACC_SET_OFFLINE_ONLY; + /* changing this is rarely needed so keeping it secret */ + s = set_add(&acc->set, "mail_notifications_limit", "5", set_eval_int, acc); + s->flags |= SET_HIDDEN_DEFAULT; + + s = set_add(&acc->set, "mail_notifications_handle", NULL, NULL, acc); + s->flags |= ACC_SET_OFFLINE_ONLY | SET_NULL_OK; + acc->flags |= ACC_FLAG_AWAY_MESSAGE | ACC_FLAG_STATUS_MESSAGE | ACC_FLAG_HANDLE_DOMAINS; } @@ -259,6 +268,14 @@ void jabber_connect(struct im_connection *ic) imcb_add_buddy(ic, JABBER_XMLCONSOLE_HANDLE, NULL); } + if (set_getbool(&acc->set, "mail_notifications")) { + /* It's gmail specific, but it checks for server support before enabling it */ + jd->flags |= JFLAG_GMAILNOTIFY; + if (set_getstr(&acc->set, "mail_notifications_handle")) { + imcb_add_buddy(ic, set_getstr(&acc->set, "mail_notifications_handle"), NULL); + } + } + jabber_generate_id_hash(jd); } @@ -333,6 +350,7 @@ static void jabber_logout(struct im_connection *ic) g_free(jd->oauth2_access_token); g_free(jd->away_message); g_free(jd->internal_jid); + g_free(jd->gmail_tid); g_free(jd->username); g_free(jd->me); g_free(jd); diff --git a/protocols/jabber/jabber.h b/protocols/jabber/jabber.h index 8416e1a7..37c99ff0 100644 --- a/protocols/jabber/jabber.h +++ b/protocols/jabber/jabber.h @@ -45,6 +45,7 @@ typedef enum { activates all XEP-85 related code. */ JFLAG_XMLCONSOLE = 64, /* If the user added an xmlconsole buddy. */ JFLAG_STARTTLS_DONE = 128, /* If a plaintext session was converted to TLS. */ + JFLAG_GMAILNOTIFY = 256, /* If gmail notification is enabled */ JFLAG_GTALK = 0x100000, /* Is Google Talk, as confirmed by iq discovery */ JFLAG_HIPCHAT = 0x200000, /* Is hipchat, because prpl->name says so */ @@ -101,6 +102,8 @@ struct jabber_data { presence_send_update() to inform the server about the changes. */ const struct jabber_away_state *away_state; char *away_message; + guint64 gmail_time; + char *gmail_tid; md5_state_t cached_id_prefix; GHashTable *node_cache; @@ -223,6 +226,7 @@ struct jabber_transfer { #define XMLNS_DELAY_OLD "jabber:x:delay" /* XEP-0091 */ #define XMLNS_DELAY "urn:xmpp:delay" /* XEP-0203 */ #define XMLNS_XDATA "jabber:x:data" /* XEP-0004 */ +#define XMLNS_GMAILNOTIFY "google:mail:notify" /* Not a XEP */ #define XMLNS_CHATSTATES "http://jabber.org/protocol/chatstates" /* XEP-0085 */ #define XMLNS_DISCO_INFO "http://jabber.org/protocol/disco#info" /* XEP-0030 */ #define XMLNS_DISCO_ITEMS "http://jabber.org/protocol/disco#items" /* XEP-0030 */ @@ -311,6 +315,7 @@ time_t jabber_get_timestamp(struct xt_node *xt); struct jabber_error *jabber_error_parse(struct xt_node *node, char *xmlns); void jabber_error_free(struct jabber_error *err); gboolean jabber_set_me(struct im_connection *ic, const char *me); +char *jabber_get_bare_jid(char *jid); extern const struct jabber_away_state jabber_away_state_list[]; diff --git a/protocols/jabber/jabber_util.c b/protocols/jabber/jabber_util.c index 779d502b..38daaa26 100644 --- a/protocols/jabber/jabber_util.c +++ b/protocols/jabber/jabber_util.c @@ -819,3 +819,19 @@ gboolean jabber_set_me(struct im_connection *ic, const char *me) return TRUE; } + +/* Returns new reference! g_free() afterwards. */ +char *jabber_get_bare_jid(char *jid) +{ + char *s = NULL; + + if (jid == NULL) { + return NULL; + } + + if ((s = strchr(jid, '/'))) { + return g_strndup(jid, s - jid); + } else { + return g_strdup(jid); + } +} diff --git a/protocols/jabber/presence.c b/protocols/jabber/presence.c index 82ea5f8f..c8664a24 100644 --- a/protocols/jabber/presence.c +++ b/protocols/jabber/presence.c @@ -180,6 +180,22 @@ xt_status jabber_pkt_presence(struct xt_node *node, gpointer data) return XT_HANDLED; } +static char *choose_priority(struct im_connection *ic) +{ + struct jabber_data *jd = ic->proto_data; + char *prio = set_getstr(&ic->acc->set, "priority"); + + if (jd->away_state->code != NULL) { + int new_prio = (atoi(prio) - 5); + if (new_prio < 0) { + new_prio = 0; + } + return g_strdup_printf("%d", new_prio); + } + + return g_strdup(prio); +} + /* Whenever presence information is updated, call this function to inform the server. */ int presence_send_update(struct im_connection *ic) @@ -188,9 +204,10 @@ int presence_send_update(struct im_connection *ic) struct xt_node *node, *cap; GSList *l; int st; + char *prio = choose_priority(ic); node = jabber_make_packet("presence", NULL, NULL, NULL); - xt_add_child(node, xt_new_node("priority", set_getstr(&ic->acc->set, "priority"), NULL)); + xt_add_child(node, xt_new_node("priority", prio, NULL)); if (jd->away_state) { xt_add_child(node, xt_new_node("show", jd->away_state->code, NULL)); } @@ -221,6 +238,7 @@ int presence_send_update(struct im_connection *ic) } xt_free_node(node); + g_free(prio); return st; } |