[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 | |
---|
[5ebff60] | 26 | xt_status jabber_pkt_message(struct xt_node *node, gpointer data) |
---|
[f06894d] | 27 | { |
---|
[0da65d5] | 28 | struct im_connection *ic = data; |
---|
[5ebff60] | 29 | char *from = xt_find_attr(node, "from"); |
---|
| 30 | char *type = xt_find_attr(node, "type"); |
---|
| 31 | char *id = xt_find_attr(node, "id"); |
---|
| 32 | struct xt_node *body = xt_find_node(node->children, "body"), *c; |
---|
| 33 | struct xt_node *request = xt_find_node(node->children, "request"); |
---|
[e35d1a1] | 34 | struct jabber_buddy *bud = NULL; |
---|
[1aa74f55] | 35 | char *s, *room = NULL, *reason = NULL; |
---|
[5ebff60] | 36 | |
---|
| 37 | if (!from) { |
---|
[e35d1a1] | 38 | return XT_HANDLED; /* Consider this packet corrupted. */ |
---|
[1444be5] | 39 | |
---|
[5ebff60] | 40 | } |
---|
| 41 | if (request && id) { |
---|
[1444be5] | 42 | /* Send a message receipt (XEP-0184), looking like this: |
---|
| 43 | * <message |
---|
| 44 | * from='kingrichard@royalty.england.lit/throne' |
---|
| 45 | * id='bi29sg183b4v' |
---|
| 46 | * to='northumberland@shakespeare.lit/westminster'> |
---|
| 47 | * <received xmlns='urn:xmpp:receipts' id='richard2-4.1.247'/> |
---|
| 48 | * </message> */ |
---|
[91ae87d] | 49 | struct xt_node *received, *receipt; |
---|
[5ebff60] | 50 | |
---|
| 51 | received = xt_new_node("received", NULL, NULL); |
---|
| 52 | xt_add_attr(received, "xmlns", XMLNS_RECEIPTS); |
---|
| 53 | xt_add_attr(received, "id", id); |
---|
| 54 | receipt = jabber_make_packet("message", NULL, from, received); |
---|
| 55 | |
---|
| 56 | jabber_write_packet(ic, receipt); |
---|
| 57 | xt_free_node(receipt); |
---|
[1444be5] | 58 | } |
---|
[5ebff60] | 59 | |
---|
| 60 | bud = jabber_buddy_by_jid(ic, from, GET_BUDDY_EXACT); |
---|
| 61 | |
---|
| 62 | if (type && strcmp(type, "error") == 0) { |
---|
[f0071b7] | 63 | /* Handle type=error packet. */ |
---|
[5ebff60] | 64 | } else if (type && from && strcmp(type, "groupchat") == 0) { |
---|
| 65 | jabber_chat_pkt_message(ic, bud, node); |
---|
| 66 | } else { /* "chat", "normal", "headline", no-type or whatever. Should all be pretty similar. */ |
---|
| 67 | GString *fullmsg = g_string_new(""); |
---|
[5ec4129] | 68 | |
---|
[5ebff60] | 69 | for (c = node->children; (c = xt_find_node(c, "x")); c = c->next) { |
---|
| 70 | char *ns = xt_find_attr(c, "xmlns"); |
---|
[1aa74f55] | 71 | struct xt_node *inv; |
---|
[5ebff60] | 72 | |
---|
| 73 | if (ns && strcmp(ns, XMLNS_MUC_USER) == 0 && |
---|
| 74 | (inv = xt_find_node(c->children, "invite"))) { |
---|
[1aa74f55] | 75 | /* This is an invitation. Set some vars which |
---|
| 76 | will be passed to imcb_chat_invite() below. */ |
---|
[5ec4129] | 77 | room = from; |
---|
[5ebff60] | 78 | if ((from = xt_find_attr(inv, "from")) == NULL) { |
---|
[daae10f] | 79 | from = room; |
---|
[5ebff60] | 80 | } |
---|
| 81 | if ((inv = xt_find_node(inv->children, "reason")) && inv->text_len > 0) { |
---|
[1aa74f55] | 82 | reason = inv->text; |
---|
[5ebff60] | 83 | } |
---|
[5ec4129] | 84 | } |
---|
| 85 | } |
---|
[5ebff60] | 86 | |
---|
| 87 | if ((s = strchr(from, '/'))) { |
---|
| 88 | if (bud) { |
---|
| 89 | bud->last_msg = time(NULL); |
---|
[daae10f] | 90 | from = bud->ext_jid ? bud->ext_jid : bud->bare_jid; |
---|
[5ebff60] | 91 | } else { |
---|
[f0071b7] | 92 | *s = 0; /* We need to generate a bare JID now. */ |
---|
[5ebff60] | 93 | } |
---|
[a21a8ac] | 94 | } |
---|
[5ebff60] | 95 | |
---|
| 96 | if (type && strcmp(type, "headline") == 0) { |
---|
| 97 | if ((c = xt_find_node(node->children, "subject")) && c->text_len > 0) { |
---|
| 98 | g_string_append_printf(fullmsg, "Headline: %s\n", c->text); |
---|
| 99 | } |
---|
| 100 | |
---|
[f0071b7] | 101 | /* <x xmlns="jabber:x:oob"><url>http://....</url></x> can contain a URL, it seems. */ |
---|
[5ebff60] | 102 | for (c = node->children; c; c = c->next) { |
---|
[f0071b7] | 103 | struct xt_node *url; |
---|
[5ebff60] | 104 | |
---|
| 105 | if ((url = xt_find_node(c->children, "url")) && url->text_len > 0) { |
---|
| 106 | g_string_append_printf(fullmsg, "URL: %s\n", url->text); |
---|
| 107 | } |
---|
[f0071b7] | 108 | } |
---|
[5ebff60] | 109 | } else if ((c = xt_find_node(node->children, "subject")) && c->text_len > 0 && |
---|
| 110 | (!bud || !(bud->flags & JBFLAG_HIDE_SUBJECT))) { |
---|
| 111 | g_string_append_printf(fullmsg, "<< \002BitlBee\002 - Message with subject: %s >>\n", c->text); |
---|
| 112 | if (bud) { |
---|
[31dbb90a] | 113 | bud->flags |= JBFLAG_HIDE_SUBJECT; |
---|
[5ebff60] | 114 | } |
---|
| 115 | } else if (bud && !c) { |
---|
[31dbb90a] | 116 | /* Yeah, possibly we're hiding changes to this field now. But nobody uses |
---|
| 117 | this for anything useful anyway, except GMail when people reply to an |
---|
| 118 | e-mail via chat, repeating the same subject all the time. I don't want |
---|
| 119 | to have to remember full subject strings for everyone. */ |
---|
| 120 | bud->flags &= ~JBFLAG_HIDE_SUBJECT; |
---|
[f0071b7] | 121 | } |
---|
[5ebff60] | 122 | |
---|
| 123 | if (body && body->text_len > 0) { /* Could be just a typing notification. */ |
---|
| 124 | fullmsg = g_string_append(fullmsg, body->text); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | if (fullmsg->len > 0) { |
---|
| 128 | imcb_buddy_msg(ic, from, fullmsg->str, |
---|
| 129 | 0, jabber_get_timestamp(node)); |
---|
| 130 | } |
---|
| 131 | if (room) { |
---|
| 132 | imcb_chat_invite(ic, room, from, reason); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | g_string_free(fullmsg, TRUE); |
---|
| 136 | |
---|
[788a1af] | 137 | /* Handling of incoming typing notifications. */ |
---|
[5ebff60] | 138 | if (bud == NULL) { |
---|
[82135c7] | 139 | /* Can't handle these for unknown buddies. */ |
---|
[5ebff60] | 140 | } else if (xt_find_node(node->children, "composing")) { |
---|
[788a1af] | 141 | bud->flags |= JBFLAG_DOES_XEP85; |
---|
[5ebff60] | 142 | imcb_buddy_typing(ic, from, OPT_TYPING); |
---|
[a21a8ac] | 143 | } |
---|
[788a1af] | 144 | /* No need to send a "stopped typing" signal when there's a message. */ |
---|
[5ebff60] | 145 | else if (xt_find_node(node->children, "active") && (body == NULL)) { |
---|
[788a1af] | 146 | bud->flags |= JBFLAG_DOES_XEP85; |
---|
[5ebff60] | 147 | imcb_buddy_typing(ic, from, 0); |
---|
| 148 | } else if (xt_find_node(node->children, "paused")) { |
---|
[788a1af] | 149 | bud->flags |= JBFLAG_DOES_XEP85; |
---|
[5ebff60] | 150 | imcb_buddy_typing(ic, from, OPT_THINKING); |
---|
[788a1af] | 151 | } |
---|
[5ebff60] | 152 | |
---|
| 153 | if (s) { |
---|
[788a1af] | 154 | *s = '/'; /* And convert it back to a full JID. */ |
---|
[5ebff60] | 155 | } |
---|
[dd788bb] | 156 | } |
---|
[5ebff60] | 157 | |
---|
[f06894d] | 158 | return XT_HANDLED; |
---|
| 159 | } |
---|