1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Jabber module - Conference rooms * |
---|
5 | * * |
---|
6 | * Copyright 2007 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 | struct groupchat *jabber_chat_join( struct im_connection *ic, char *room, char *nick, char *password ) |
---|
27 | { |
---|
28 | struct jabber_chat *jc; |
---|
29 | struct xt_node *node; |
---|
30 | struct groupchat *c; |
---|
31 | char *roomjid; |
---|
32 | |
---|
33 | roomjid = g_strdup_printf( "%s/%s", room, nick ); |
---|
34 | node = xt_new_node( "x", NULL, NULL ); |
---|
35 | xt_add_attr( node, "xmlns", XMLNS_MUC ); |
---|
36 | node = jabber_make_packet( "presence", NULL, roomjid, node ); |
---|
37 | |
---|
38 | if( !jabber_write_packet( ic, node ) ) |
---|
39 | { |
---|
40 | g_free( roomjid ); |
---|
41 | xt_free_node( node ); |
---|
42 | return NULL; |
---|
43 | } |
---|
44 | xt_free_node( node ); |
---|
45 | |
---|
46 | jc = g_new0( struct jabber_chat, 1 ); |
---|
47 | jc->name = jabber_normalize( room ); |
---|
48 | |
---|
49 | if( ( jc->me = jabber_buddy_add( ic, roomjid ) ) == NULL ) |
---|
50 | { |
---|
51 | g_free( roomjid ); |
---|
52 | g_free( jc->name ); |
---|
53 | g_free( jc ); |
---|
54 | return NULL; |
---|
55 | } |
---|
56 | g_free( roomjid ); |
---|
57 | |
---|
58 | c = imcb_chat_new( ic, room ); |
---|
59 | c->data = jc; |
---|
60 | |
---|
61 | return c; |
---|
62 | } |
---|
63 | |
---|
64 | int jabber_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
65 | { |
---|
66 | struct im_connection *ic = c->ic; |
---|
67 | struct jabber_chat *jc = c->data; |
---|
68 | struct xt_node *node; |
---|
69 | |
---|
70 | node = xt_new_node( "body", message, NULL ); |
---|
71 | node = jabber_make_packet( "message", "groupchat", jc->name, node ); |
---|
72 | |
---|
73 | if( !jabber_write_packet( ic, node ) ) |
---|
74 | { |
---|
75 | xt_free_node( node ); |
---|
76 | return 0; |
---|
77 | } |
---|
78 | xt_free_node( node ); |
---|
79 | |
---|
80 | return 1; |
---|
81 | } |
---|
82 | |
---|
83 | int jabber_chat_leave( struct groupchat *c, const char *reason ) |
---|
84 | { |
---|
85 | struct im_connection *ic = c->ic; |
---|
86 | struct jabber_chat *jc = c->data; |
---|
87 | struct xt_node *node; |
---|
88 | |
---|
89 | node = xt_new_node( "x", NULL, NULL ); |
---|
90 | xt_add_attr( node, "xmlns", XMLNS_MUC ); |
---|
91 | node = jabber_make_packet( "presence", "unavailable", jc->me->full_jid, node ); |
---|
92 | |
---|
93 | if( !jabber_write_packet( ic, node ) ) |
---|
94 | { |
---|
95 | xt_free_node( node ); |
---|
96 | return 0; |
---|
97 | } |
---|
98 | xt_free_node( node ); |
---|
99 | |
---|
100 | return 1; |
---|
101 | } |
---|
102 | |
---|
103 | /* Not really the same syntax as the normal pkt_ functions, but this isn't |
---|
104 | called by the xmltree parser directly and this way I can add some extra |
---|
105 | parameters so we won't have to repeat too many things done by the caller |
---|
106 | already. */ |
---|
107 | void jabber_chat_pkt_presence( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node ) |
---|
108 | { |
---|
109 | struct groupchat *chat; |
---|
110 | struct xt_node *c; |
---|
111 | char *type = xt_find_attr( node, "type" ); |
---|
112 | struct jabber_chat *jc; |
---|
113 | char *s; |
---|
114 | |
---|
115 | if( ( chat = jabber_chat_by_name( ic, bud->bare_jid ) ) == NULL ) |
---|
116 | { |
---|
117 | /* How could this happen?? We could do kill( self, 11 ) |
---|
118 | now or just wait for the OS to do it. :-) */ |
---|
119 | return; |
---|
120 | } |
---|
121 | |
---|
122 | jc = chat->data; |
---|
123 | |
---|
124 | if( type == NULL && !( bud->flags & JBFLAG_IS_CHATROOM ) ) |
---|
125 | { |
---|
126 | bud->flags |= JBFLAG_IS_CHATROOM; |
---|
127 | /* If this one wasn't set yet, this buddy just joined the chat. |
---|
128 | Slightly hackish way of finding out eh? ;-) */ |
---|
129 | |
---|
130 | /* This is pretty messy... */ |
---|
131 | for( c = node->children; ( c = xt_find_node( c, "x" ) ); c = c->next ) |
---|
132 | if( ( s = xt_find_attr( c, "xmlns" ) ) && |
---|
133 | ( strcmp( s, XMLNS_MUC_USER ) == 0 ) ) |
---|
134 | { |
---|
135 | c = xt_find_node( c->children, "item" ); |
---|
136 | if( ( s = xt_find_attr( c, "jid" ) ) ) |
---|
137 | { |
---|
138 | /* Yay, found what we need. :-) */ |
---|
139 | bud->orig_jid = g_strdup( s ); |
---|
140 | break; |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | /* Won't handle this for now. */ |
---|
145 | if( bud->orig_jid == NULL ) |
---|
146 | return; |
---|
147 | |
---|
148 | s = strchr( bud->orig_jid, '/' ); |
---|
149 | if( s ) *s = 0; /* Should NEVER be NULL, but who knows... */ |
---|
150 | imcb_chat_add_buddy( chat, bud->orig_jid ); |
---|
151 | if( s ) *s = '/'; |
---|
152 | } |
---|
153 | else if( type ) /* This only gets called if type is NULL or "unavailable" */ |
---|
154 | { |
---|
155 | /* Won't handle this for now. */ |
---|
156 | if( bud->orig_jid == NULL ) |
---|
157 | return; |
---|
158 | s = strchr( bud->orig_jid, '/' ); |
---|
159 | if( s ) *s = 0; /* Should NEVER be NULL, but who knows... */ |
---|
160 | imcb_chat_remove_buddy( chat, bud->orig_jid, NULL ); |
---|
161 | if( s ) *s = '/'; |
---|
162 | |
---|
163 | if( bud == jc->me ) |
---|
164 | { |
---|
165 | jabber_buddy_remove_bare( ic, jc->name ); |
---|
166 | |
---|
167 | g_free( jc->name ); |
---|
168 | g_free( jc ); |
---|
169 | imcb_chat_free( chat ); |
---|
170 | } |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | void jabber_chat_pkt_message( struct im_connection *ic, struct jabber_buddy *bud, struct xt_node *node ) |
---|
175 | { |
---|
176 | struct xt_node *body = xt_find_node( node->children, "body" ); |
---|
177 | struct groupchat *chat; |
---|
178 | char *s; |
---|
179 | |
---|
180 | if( bud == NULL ) |
---|
181 | { |
---|
182 | s = xt_find_attr( node, "from" ); /* pkt_message() already NULL-checked this one. */ |
---|
183 | if( strchr( s, '/' ) == NULL ) |
---|
184 | /* This is fine, the groupchat itself isn't in jd->buddies. */ |
---|
185 | imcb_log( ic, "System message from groupchat %s: %s", s, body? body->text : "NULL" ); |
---|
186 | else |
---|
187 | /* This, however, isn't fine! */ |
---|
188 | imcb_log( ic, "Groupchat message from unknown participant %s: %s", s, body ? body->text : "NULL" ); |
---|
189 | |
---|
190 | return; |
---|
191 | } |
---|
192 | else if( ( chat = jabber_chat_by_name( ic, bud->bare_jid ) ) == NULL ) |
---|
193 | { |
---|
194 | /* How could this happen?? We could do kill( self, 11 ) |
---|
195 | now or just wait for the OS to do it. :-) */ |
---|
196 | return; |
---|
197 | } |
---|
198 | |
---|
199 | if( body && body->text_len > 0 ) |
---|
200 | { |
---|
201 | s = strchr( bud->orig_jid, '/' ); |
---|
202 | if( s ) *s = 0; |
---|
203 | imcb_chat_msg( chat, bud->orig_jid, body->text, 0, jabber_get_timestamp( node ) ); |
---|
204 | if( s ) *s = '/'; |
---|
205 | } |
---|
206 | } |
---|