1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Jabber module - Handling of message(s) (tags), etc * |
---|
5 | * * |
---|
6 | * Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
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 | |
---|
26 | xt_status jabber_pkt_message( struct xt_node *node, gpointer data ) |
---|
27 | { |
---|
28 | struct im_connection *ic = data; |
---|
29 | char *from = xt_find_attr( node, "from" ); |
---|
30 | char *type = xt_find_attr( node, "type" ); |
---|
31 | struct xt_node *body = xt_find_node( node->children, "body" ), *c; |
---|
32 | struct jabber_buddy *bud = NULL; |
---|
33 | char *s; |
---|
34 | |
---|
35 | if( !from ) |
---|
36 | return XT_HANDLED; /* Consider this packet corrupted. */ |
---|
37 | |
---|
38 | bud = jabber_buddy_by_jid( ic, from, GET_BUDDY_EXACT ); |
---|
39 | |
---|
40 | if( type && strcmp( type, "error" ) == 0 ) |
---|
41 | { |
---|
42 | /* Handle type=error packet. */ |
---|
43 | } |
---|
44 | else if( type && from && strcmp( type, "groupchat" ) == 0 ) |
---|
45 | { |
---|
46 | jabber_chat_pkt_message( ic, bud, node ); |
---|
47 | } |
---|
48 | else /* "chat", "normal", "headline", no-type or whatever. Should all be pretty similar. */ |
---|
49 | { |
---|
50 | GString *fullmsg = g_string_new( "" ); |
---|
51 | |
---|
52 | for( c = node->children; ( c = xt_find_node( c, "x" ) ); c = c->next ) |
---|
53 | { |
---|
54 | char *ns = xt_find_attr( c, "xmlns" ), *room; |
---|
55 | struct xt_node *inv, *reason; |
---|
56 | |
---|
57 | if( ns && strcmp( ns, XMLNS_MUC_USER ) == 0 && |
---|
58 | ( inv = xt_find_node( c->children, "invite" ) ) ) |
---|
59 | { |
---|
60 | room = from; |
---|
61 | if( ( from = xt_find_attr( inv, "from" ) ) == NULL ) |
---|
62 | from = room; |
---|
63 | |
---|
64 | g_string_append_printf( fullmsg, "<< \002BitlBee\002 - Invitation to chatroom %s >>\n", room ); |
---|
65 | if( ( reason = xt_find_node( inv->children, "reason" ) ) && reason->text_len > 0 ) |
---|
66 | g_string_append( fullmsg, reason->text ); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | if( ( s = strchr( from, '/' ) ) ) |
---|
71 | { |
---|
72 | if( bud ) |
---|
73 | { |
---|
74 | bud->last_msg = time( NULL ); |
---|
75 | from = bud->ext_jid ? bud->ext_jid : bud->bare_jid; |
---|
76 | } |
---|
77 | else |
---|
78 | *s = 0; /* We need to generate a bare JID now. */ |
---|
79 | } |
---|
80 | |
---|
81 | if( type && strcmp( type, "headline" ) == 0 ) |
---|
82 | { |
---|
83 | if( ( c = xt_find_node( node->children, "subject" ) ) && c->text_len > 0 ) |
---|
84 | g_string_append_printf( fullmsg, "Headline: %s\n", c->text ); |
---|
85 | |
---|
86 | /* <x xmlns="jabber:x:oob"><url>http://....</url></x> can contain a URL, it seems. */ |
---|
87 | for( c = node->children; c; c = c->next ) |
---|
88 | { |
---|
89 | struct xt_node *url; |
---|
90 | |
---|
91 | if( ( url = xt_find_node( c->children, "url" ) ) && url->text_len > 0 ) |
---|
92 | g_string_append_printf( fullmsg, "URL: %s\n", url->text ); |
---|
93 | } |
---|
94 | } |
---|
95 | else if( ( c = xt_find_node( node->children, "subject" ) ) && c->text_len > 0 ) |
---|
96 | { |
---|
97 | g_string_append_printf( fullmsg, "<< \002BitlBee\002 - Message with subject: %s >>\n", c->text ); |
---|
98 | } |
---|
99 | |
---|
100 | if( body && body->text_len > 0 ) /* Could be just a typing notification. */ |
---|
101 | fullmsg = g_string_append( fullmsg, body->text ); |
---|
102 | |
---|
103 | if( fullmsg->len > 0 ) |
---|
104 | imcb_buddy_msg( ic, from, fullmsg->str, |
---|
105 | 0, jabber_get_timestamp( node ) ); |
---|
106 | |
---|
107 | g_string_free( fullmsg, TRUE ); |
---|
108 | |
---|
109 | /* Handling of incoming typing notifications. */ |
---|
110 | if( bud == NULL ) |
---|
111 | { |
---|
112 | /* Can't handle these for unknown buddies. */ |
---|
113 | } |
---|
114 | else if( xt_find_node( node->children, "composing" ) ) |
---|
115 | { |
---|
116 | bud->flags |= JBFLAG_DOES_XEP85; |
---|
117 | imcb_buddy_typing( ic, from, OPT_TYPING ); |
---|
118 | } |
---|
119 | /* No need to send a "stopped typing" signal when there's a message. */ |
---|
120 | else if( xt_find_node( node->children, "active" ) && ( body == NULL ) ) |
---|
121 | { |
---|
122 | bud->flags |= JBFLAG_DOES_XEP85; |
---|
123 | imcb_buddy_typing( ic, from, 0 ); |
---|
124 | } |
---|
125 | else if( xt_find_node( node->children, "paused" ) ) |
---|
126 | { |
---|
127 | bud->flags |= JBFLAG_DOES_XEP85; |
---|
128 | imcb_buddy_typing( ic, from, OPT_THINKING ); |
---|
129 | } |
---|
130 | |
---|
131 | if( s ) |
---|
132 | *s = '/'; /* And convert it back to a full JID. */ |
---|
133 | } |
---|
134 | |
---|
135 | return XT_HANDLED; |
---|
136 | } |
---|