[f06894d] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
[21167d2] | 4 | * Jabber module - Handling of message(s) (tags), etc * |
---|
[f06894d] | 5 | * * |
---|
[0e788f5] | 6 | * Copyright 2006-2012 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
[f06894d] | 7 | * * |
---|
| 8 | * This program is free software; you can redistribute it and/or modify * |
---|
| 9 | * it under the terms of the GNU General Public License as published by * |
---|
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
| 11 | * (at your option) any later version. * |
---|
| 12 | * * |
---|
| 13 | * This program is distributed in the hope that it will be useful, * |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
| 16 | * GNU General Public License for more details. * |
---|
| 17 | * * |
---|
| 18 | * You should have received a copy of the GNU General Public License along * |
---|
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
| 21 | * * |
---|
| 22 | \***************************************************************************/ |
---|
| 23 | |
---|
| 24 | #include "jabber.h" |
---|
| 25 | |
---|
[fa8f57b] | 26 | static xt_status jabber_pkt_message_normal(struct xt_node *node, gpointer data, gboolean carbons_sent) |
---|
[f06894d] | 27 | { |
---|
[0da65d5] | 28 | struct im_connection *ic = data; |
---|
[d11ccbf] | 29 | struct jabber_data *jd = ic->proto_data; |
---|
[fa8f57b] | 30 | char *from = xt_find_attr(node, carbons_sent ? "to" : "from"); |
---|
[5ebff60] | 31 | char *type = xt_find_attr(node, "type"); |
---|
| 32 | char *id = xt_find_attr(node, "id"); |
---|
| 33 | struct xt_node *body = xt_find_node(node->children, "body"), *c; |
---|
| 34 | struct xt_node *request = xt_find_node(node->children, "request"); |
---|
[e35d1a1] | 35 | struct jabber_buddy *bud = NULL; |
---|
[1aa74f55] | 36 | char *s, *room = NULL, *reason = NULL; |
---|
[5ebff60] | 37 | |
---|
| 38 | if (!from) { |
---|
[e35d1a1] | 39 | return XT_HANDLED; /* Consider this packet corrupted. */ |
---|
[5ebff60] | 40 | } |
---|
[ad8a810] | 41 | |
---|
[d11ccbf] | 42 | /* try to detect hipchat's own version of self-messages */ |
---|
| 43 | if (jd->flags & JFLAG_HIPCHAT) { |
---|
| 44 | struct xt_node *c; |
---|
| 45 | |
---|
| 46 | if ((c = xt_find_node_by_attr(node->children, "delay", "xmlns", XMLNS_DELAY)) && |
---|
| 47 | (s = xt_find_attr(c, "from_jid")) && |
---|
| 48 | jabber_compare_jid(s, jd->me)) { |
---|
| 49 | carbons_sent = TRUE; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
[fa8f57b] | 53 | if (request && id && g_strcmp0(type, "groupchat") != 0 && !carbons_sent) { |
---|
[1444be5] | 54 | /* Send a message receipt (XEP-0184), looking like this: |
---|
[ad8a810] | 55 | * <message from='...' id='...' to='...'> |
---|
[1444be5] | 56 | * <received xmlns='urn:xmpp:receipts' id='richard2-4.1.247'/> |
---|
[ad8a810] | 57 | * </message> |
---|
| 58 | * |
---|
| 59 | * MUC messages are excluded, since receipts aren't supposed to be sent over MUCs |
---|
| 60 | * (XEP-0184 section 5.3) and replying to those may result in 'forbidden' errors. |
---|
| 61 | */ |
---|
[91ae87d] | 62 | struct xt_node *received, *receipt; |
---|
[5ebff60] | 63 | |
---|
| 64 | received = xt_new_node("received", NULL, NULL); |
---|
| 65 | xt_add_attr(received, "xmlns", XMLNS_RECEIPTS); |
---|
| 66 | xt_add_attr(received, "id", id); |
---|
| 67 | receipt = jabber_make_packet("message", NULL, from, received); |
---|
| 68 | |
---|
| 69 | jabber_write_packet(ic, receipt); |
---|
| 70 | xt_free_node(receipt); |
---|
[1444be5] | 71 | } |
---|
[5ebff60] | 72 | |
---|
| 73 | bud = jabber_buddy_by_jid(ic, from, GET_BUDDY_EXACT); |
---|
| 74 | |
---|
| 75 | if (type && strcmp(type, "error") == 0) { |
---|
[f0071b7] | 76 | /* Handle type=error packet. */ |
---|
[5ebff60] | 77 | } else if (type && from && strcmp(type, "groupchat") == 0) { |
---|
| 78 | jabber_chat_pkt_message(ic, bud, node); |
---|
| 79 | } else { /* "chat", "normal", "headline", no-type or whatever. Should all be pretty similar. */ |
---|
| 80 | GString *fullmsg = g_string_new(""); |
---|
[5ec4129] | 81 | |
---|
[5ebff60] | 82 | for (c = node->children; (c = xt_find_node(c, "x")); c = c->next) { |
---|
| 83 | char *ns = xt_find_attr(c, "xmlns"); |
---|
[1aa74f55] | 84 | struct xt_node *inv; |
---|
[5ebff60] | 85 | |
---|
| 86 | if (ns && strcmp(ns, XMLNS_MUC_USER) == 0 && |
---|
| 87 | (inv = xt_find_node(c->children, "invite"))) { |
---|
[1aa74f55] | 88 | /* This is an invitation. Set some vars which |
---|
| 89 | will be passed to imcb_chat_invite() below. */ |
---|
[5ec4129] | 90 | room = from; |
---|
[5ebff60] | 91 | if ((from = xt_find_attr(inv, "from")) == NULL) { |
---|
[daae10f] | 92 | from = room; |
---|
[5ebff60] | 93 | } |
---|
| 94 | if ((inv = xt_find_node(inv->children, "reason")) && inv->text_len > 0) { |
---|
[1aa74f55] | 95 | reason = inv->text; |
---|
[5ebff60] | 96 | } |
---|
[5ec4129] | 97 | } |
---|
| 98 | } |
---|
[5ebff60] | 99 | |
---|
| 100 | if ((s = strchr(from, '/'))) { |
---|
| 101 | if (bud) { |
---|
| 102 | bud->last_msg = time(NULL); |
---|
[daae10f] | 103 | from = bud->ext_jid ? bud->ext_jid : bud->bare_jid; |
---|
[5ebff60] | 104 | } else { |
---|
[f0071b7] | 105 | *s = 0; /* We need to generate a bare JID now. */ |
---|
[5ebff60] | 106 | } |
---|
[a21a8ac] | 107 | } |
---|
[5ebff60] | 108 | |
---|
| 109 | if (type && strcmp(type, "headline") == 0) { |
---|
| 110 | if ((c = xt_find_node(node->children, "subject")) && c->text_len > 0) { |
---|
| 111 | g_string_append_printf(fullmsg, "Headline: %s\n", c->text); |
---|
| 112 | } |
---|
| 113 | |
---|
[f0071b7] | 114 | /* <x xmlns="jabber:x:oob"><url>http://....</url></x> can contain a URL, it seems. */ |
---|
[5ebff60] | 115 | for (c = node->children; c; c = c->next) { |
---|
[f0071b7] | 116 | struct xt_node *url; |
---|
[5ebff60] | 117 | |
---|
| 118 | if ((url = xt_find_node(c->children, "url")) && url->text_len > 0) { |
---|
| 119 | g_string_append_printf(fullmsg, "URL: %s\n", url->text); |
---|
| 120 | } |
---|
[f0071b7] | 121 | } |
---|
[5ebff60] | 122 | } else if ((c = xt_find_node(node->children, "subject")) && c->text_len > 0 && |
---|
| 123 | (!bud || !(bud->flags & JBFLAG_HIDE_SUBJECT))) { |
---|
| 124 | g_string_append_printf(fullmsg, "<< \002BitlBee\002 - Message with subject: %s >>\n", c->text); |
---|
| 125 | if (bud) { |
---|
[31dbb90a] | 126 | bud->flags |= JBFLAG_HIDE_SUBJECT; |
---|
[5ebff60] | 127 | } |
---|
| 128 | } else if (bud && !c) { |
---|
[31dbb90a] | 129 | /* Yeah, possibly we're hiding changes to this field now. But nobody uses |
---|
| 130 | this for anything useful anyway, except GMail when people reply to an |
---|
| 131 | e-mail via chat, repeating the same subject all the time. I don't want |
---|
| 132 | to have to remember full subject strings for everyone. */ |
---|
| 133 | bud->flags &= ~JBFLAG_HIDE_SUBJECT; |
---|
[f0071b7] | 134 | } |
---|
[5ebff60] | 135 | |
---|
| 136 | if (body && body->text_len > 0) { /* Could be just a typing notification. */ |
---|
| 137 | fullmsg = g_string_append(fullmsg, body->text); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | if (fullmsg->len > 0) { |
---|
| 141 | imcb_buddy_msg(ic, from, fullmsg->str, |
---|
[fa8f57b] | 142 | carbons_sent ? OPT_SELFMESSAGE : 0, jabber_get_timestamp(node)); |
---|
[5ebff60] | 143 | } |
---|
| 144 | if (room) { |
---|
| 145 | imcb_chat_invite(ic, room, from, reason); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | g_string_free(fullmsg, TRUE); |
---|
| 149 | |
---|
[788a1af] | 150 | /* Handling of incoming typing notifications. */ |
---|
[fa8f57b] | 151 | if (bud == NULL || carbons_sent) { |
---|
| 152 | /* Can't handle these for unknown buddies. |
---|
| 153 | And ignore them if it's just carbons */ |
---|
[5ebff60] | 154 | } else if (xt_find_node(node->children, "composing")) { |
---|
[788a1af] | 155 | bud->flags |= JBFLAG_DOES_XEP85; |
---|
[5ebff60] | 156 | imcb_buddy_typing(ic, from, OPT_TYPING); |
---|
[a21a8ac] | 157 | } |
---|
[5307e88] | 158 | else if (xt_find_node(node->children, "active")) { |
---|
[788a1af] | 159 | bud->flags |= JBFLAG_DOES_XEP85; |
---|
[5307e88] | 160 | |
---|
| 161 | /* No need to send a "stopped typing" signal when there's a message. */ |
---|
| 162 | if (body == NULL) { |
---|
| 163 | imcb_buddy_typing(ic, from, 0); |
---|
| 164 | } |
---|
[5ebff60] | 165 | } else if (xt_find_node(node->children, "paused")) { |
---|
[788a1af] | 166 | bud->flags |= JBFLAG_DOES_XEP85; |
---|
[5ebff60] | 167 | imcb_buddy_typing(ic, from, OPT_THINKING); |
---|
[788a1af] | 168 | } |
---|
[5ebff60] | 169 | |
---|
| 170 | if (s) { |
---|
[788a1af] | 171 | *s = '/'; /* And convert it back to a full JID. */ |
---|
[5ebff60] | 172 | } |
---|
[dd788bb] | 173 | } |
---|
[5ebff60] | 174 | |
---|
[f06894d] | 175 | return XT_HANDLED; |
---|
| 176 | } |
---|
[fa8f57b] | 177 | |
---|
| 178 | static xt_status jabber_carbons_message(struct xt_node *node, gpointer data) |
---|
| 179 | { |
---|
| 180 | struct im_connection *ic = data; |
---|
| 181 | struct xt_node *wrap, *fwd, *msg; |
---|
| 182 | gboolean carbons_sent; |
---|
| 183 | |
---|
| 184 | if ((wrap = xt_find_node(node->children, "received"))) { |
---|
| 185 | carbons_sent = FALSE; |
---|
| 186 | } else if ((wrap = xt_find_node(node->children, "sent"))) { |
---|
| 187 | carbons_sent = TRUE; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | if (wrap == NULL || g_strcmp0(xt_find_attr(wrap, "xmlns"), XMLNS_CARBONS) != 0) { |
---|
| 191 | return XT_NEXT; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | if (!(fwd = xt_find_node(wrap->children, "forwarded")) || |
---|
| 195 | (g_strcmp0(xt_find_attr(fwd, "xmlns"), XMLNS_FORWARDING) != 0) || |
---|
| 196 | !(msg = xt_find_node(fwd->children, "message"))) { |
---|
| 197 | imcb_log(ic, "Error: Invalid carbons message received"); |
---|
| 198 | return XT_ABORT; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | return jabber_pkt_message_normal(msg, data, carbons_sent); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | xt_status jabber_pkt_message(struct xt_node *node, gpointer data) |
---|
| 205 | { |
---|
| 206 | struct im_connection *ic = data; |
---|
| 207 | struct jabber_data *jd = ic->proto_data; |
---|
| 208 | char *from = xt_find_attr(node, "from"); |
---|
| 209 | |
---|
| 210 | if (jabber_compare_jid(jd->me, from)) { /* Probably a Carbons message */ |
---|
| 211 | xt_status st = jabber_carbons_message(node, data); |
---|
| 212 | if (st == XT_HANDLED || st == XT_ABORT) { |
---|
| 213 | return st; |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | return jabber_pkt_message_normal(node, data, FALSE); |
---|
| 217 | } |
---|