Mercurial > pidgin.yaz
annotate src/protocols/jabber/presence.c @ 7974:415df6fa0395
[gaim-migrate @ 8651]
jabber /part support (with /part messages!)
the list of the cool jabber chat stuff I can do w/o certain extras
from people who know who they are is getting shorter and shorter
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Fri, 02 Jan 2004 15:34:04 +0000 |
parents | aa99b8390092 |
children | c4b5a35c7a12 |
rev | line source |
---|---|
7014 | 1 /* |
2 * gaim - Jabber Protocol Plugin | |
3 * | |
4 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 #include "internal.h" | |
22 | |
23 #include "debug.h" | |
24 #include "notify.h" | |
25 #include "request.h" | |
26 #include "server.h" | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7015
diff
changeset
|
27 #include "util.h" |
7014 | 28 |
29 #include "buddy.h" | |
30 #include "chat.h" | |
31 #include "presence.h" | |
32 #include "iq.h" | |
33 #include "jutil.h" | |
34 #include "xmlnode.h" | |
35 | |
36 | |
37 static void chats_send_presence_foreach(gpointer key, gpointer val, | |
38 gpointer user_data) | |
39 { | |
40 JabberChat *chat = val; | |
41 xmlnode *presence = user_data; | |
42 char *chat_jid = key; | |
43 | |
44 xmlnode_set_attrib(presence, "to", chat_jid); | |
45 jabber_send(chat->js, presence); | |
46 } | |
47 | |
48 | |
49 void jabber_presence_send(GaimConnection *gc, const char *state, | |
50 const char *msg) | |
51 { | |
52 JabberStream *js = gc->proto_data; | |
53 xmlnode *presence; | |
54 char *stripped = NULL; | |
55 | |
56 if(msg) { | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7015
diff
changeset
|
57 gaim_markup_html_to_xhtml(msg, NULL, &stripped); |
7249 | 58 } else if(!state || strcmp(state, GAIM_AWAY_CUSTOM)) { |
7247 | 59 /* i can't wait until someone re-writes the status/away stuff */ |
7014 | 60 stripped = g_strdup(""); |
61 } | |
62 | |
7248 | 63 if(gc->away) |
64 g_free(gc->away); | |
7014 | 65 gc->away = stripped; |
66 | |
67 presence = jabber_presence_create(state, msg); | |
68 jabber_send(js, presence); | |
69 g_hash_table_foreach(js->chats, chats_send_presence_foreach, presence); | |
70 xmlnode_free(presence); | |
71 } | |
72 | |
73 xmlnode *jabber_presence_create(const char *state, const char *msg) | |
74 { | |
75 xmlnode *show, *status, *presence; | |
76 | |
77 | |
78 presence = xmlnode_new("presence"); | |
79 | |
80 if(state) { | |
81 const char *show_string = NULL; | |
82 if(!strcmp(state, _("Chatty"))) | |
83 show_string = "chat"; | |
84 else if(!strcmp(state, _("Away")) || | |
85 (msg && !strcmp(state, GAIM_AWAY_CUSTOM))) | |
86 show_string = "away"; | |
87 else if(!strcmp(state, _("Extended Away"))) | |
88 show_string = "xa"; | |
89 else if(!strcmp(state, _("Do Not Disturb"))) | |
90 show_string = "dnd"; | |
91 else if(!strcmp(state, _("Invisible"))) { | |
92 xmlnode_set_attrib(presence, "type", "invisible"); | |
93 } | |
94 | |
95 if(show_string) { | |
96 show = xmlnode_new_child(presence, "show"); | |
97 xmlnode_insert_data(show, show_string, -1); | |
98 } | |
99 } | |
100 | |
101 if(msg && *msg) { | |
102 status = xmlnode_new_child(presence, "status"); | |
103 xmlnode_insert_data(status, msg, -1); | |
104 } | |
105 | |
106 return presence; | |
107 } | |
108 | |
109 struct _jabber_add_permit { | |
110 GaimConnection *gc; | |
111 char *who; | |
112 }; | |
113 | |
114 static void authorize_add_cb(struct _jabber_add_permit *jap) | |
115 { | |
116 if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
117 jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
118 "subscribed"); | |
119 | |
120 if(!gaim_find_buddy(jap->gc->account, jap->who)) | |
7015
dece74f05509
[gaim-migrate @ 7578]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
121 gaim_account_notify_added(jap->gc->account, NULL, jap->who, NULL, NULL); |
7014 | 122 } |
123 | |
124 g_free(jap->who); | |
125 g_free(jap); | |
126 } | |
127 | |
128 static void deny_add_cb(struct _jabber_add_permit *jap) | |
129 { | |
130 if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
131 jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
132 "unsubscribed"); | |
133 } | |
134 | |
135 g_free(jap->who); | |
136 g_free(jap); | |
137 } | |
138 | |
139 void jabber_presence_parse(JabberStream *js, xmlnode *packet) | |
140 { | |
141 const char *from = xmlnode_get_attrib(packet, "from"); | |
142 const char *type = xmlnode_get_attrib(packet, "type"); | |
7944 | 143 const char *real_jid = NULL; |
7014 | 144 char *status = NULL; |
145 int priority = 0; | |
146 JabberID *jid; | |
147 JabberChat *chat; | |
148 JabberBuddy *jb; | |
7835 | 149 JabberBuddyResource *jbr = NULL; |
7014 | 150 GaimBuddy *b; |
151 char *buddy_name; | |
152 int state = 0; | |
153 xmlnode *y; | |
154 gboolean muc = FALSE; | |
155 | |
156 jb = jabber_buddy_find(js, from, TRUE); | |
157 | |
7280 | 158 if(!jb) |
159 return; | |
160 | |
7014 | 161 if(jb->error_msg) { |
162 g_free(jb->error_msg); | |
163 jb->error_msg = NULL; | |
164 } | |
165 | |
7813 | 166 if(type && !strcmp(type, "error")) { |
7644 | 167 const char *code = NULL; |
168 char *err_txt = NULL; | |
169 | |
7014 | 170 state = JABBER_STATE_ERROR; |
171 if((y = xmlnode_get_child(packet, "error")) != NULL) { | |
7184 | 172 /* XXX: need to handle new XMPP-style errors */ |
7644 | 173 code = xmlnode_get_attrib(y, "code"); |
174 err_txt = xmlnode_get_data(y); | |
7014 | 175 } |
7644 | 176 jb->error_msg = g_strdup_printf("%s%s%s", code ? code : "", |
177 code ? ": " : "", err_txt ? err_txt : | |
178 _("Unknown Error in presence")); | |
179 if(err_txt) | |
180 g_free(err_txt); | |
7813 | 181 } else if(type && !strcmp(type, "subscribe")) { |
7014 | 182 struct _jabber_add_permit *jap = g_new0(struct _jabber_add_permit, 1); |
183 char *msg = g_strdup_printf(_("The user %s wants to add you to their buddy list."), from); | |
184 jap->gc = js->gc; | |
185 jap->who = g_strdup(from); | |
186 | |
187 gaim_request_action(js->gc, NULL, msg, NULL, 0, jap, 2, | |
188 _("Authorize"), G_CALLBACK(authorize_add_cb), | |
189 _("Deny"), G_CALLBACK(deny_add_cb)); | |
190 g_free(msg); | |
7145 | 191 return; |
7813 | 192 } else if(type && !strcmp(type, "subscribed")) { |
7014 | 193 /* we've been allowed to see their presence, but we don't care */ |
194 return; | |
195 } else { | |
196 if((y = xmlnode_get_child(packet, "show"))) { | |
197 char *show = xmlnode_get_data(y); | |
198 if(!show) { | |
199 state = 0; | |
200 } else if(!strcasecmp(show, "away")) { | |
201 state = JABBER_STATE_AWAY; | |
202 } else if(!strcasecmp(show, "chat")) { | |
203 state = JABBER_STATE_CHAT; | |
204 } else if(!strcasecmp(show, "xa")) { | |
205 state = JABBER_STATE_XA; | |
206 } else if(!strcasecmp(show, "dnd")) { | |
207 state = JABBER_STATE_DND; | |
208 } | |
209 g_free(show); | |
210 } else { | |
211 state = 0; | |
212 } | |
213 } | |
214 | |
7310 | 215 if(!(jid = jabber_id_new(from))) |
216 return; | |
217 | |
7014 | 218 for(y = packet->child; y; y = y->next) { |
219 if(y->type != NODE_TYPE_TAG) | |
220 continue; | |
221 | |
222 if(!strcmp(y->name, "status")) { | |
7615 | 223 g_free(status); |
7014 | 224 status = xmlnode_get_data(y); |
225 } else if(!strcmp(y->name, "priority")) { | |
226 char *p = xmlnode_get_data(y); | |
227 if(p) { | |
228 priority = atoi(p); | |
229 g_free(p); | |
230 } | |
231 } else if(!strcmp(y->name, "x")) { | |
232 const char *xmlns = xmlnode_get_attrib(y, "xmlns"); | |
233 if(xmlns && !strcmp(xmlns, "http://jabber.org/protocol/muc#user")) { | |
234 /* this is where we'd normally get the "op" status of the | |
235 * user, but since we don't have a good way to show that yet | |
236 * we'll ignore it */ | |
7629 | 237 xmlnode *z; |
238 | |
7014 | 239 muc = TRUE; |
7629 | 240 if((z = xmlnode_get_child(y, "status"))) { |
241 const char *code = xmlnode_get_attrib(z, "code"); | |
242 if(code && !strcmp(code, "201")) { | |
7895 | 243 chat = jabber_chat_find(js, jid->node, jid->domain); |
244 gaim_request_action(js->gc, _("Create New Room"), | |
245 _("Create New Room"), | |
246 _("You are creating a new room. Would you like to " | |
247 "configure it, or accept the default settings?"), | |
248 1, chat, 2, _("Configure Room"), | |
7923 | 249 G_CALLBACK(jabber_chat_request_room_configure), |
7895 | 250 _("Accept Defaults"), |
251 G_CALLBACK(jabber_chat_create_instant_room)); | |
7629 | 252 } |
253 } | |
7944 | 254 if((z = xmlnode_get_child(y, "item"))) { |
255 real_jid = xmlnode_get_attrib(z, "jid"); | |
256 } | |
7014 | 257 } |
258 } | |
259 } | |
260 | |
261 | |
7322 | 262 if(jid->node && (chat = jabber_chat_find(js, jid->node, jid->domain))) { |
7014 | 263 static int i = 0; |
264 char *room_jid = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
265 | |
266 if(state == JABBER_STATE_ERROR) { | |
267 const char *code = NULL; | |
268 char *text = NULL; | |
269 char *buf; | |
270 xmlnode *error = xmlnode_get_child(packet, "error"); | |
271 if(error) { | |
272 /* I should make my own messages so they can be | |
273 * translated, but i'm tired */ | |
274 code = xmlnode_get_attrib(error, "code"); | |
275 text = xmlnode_get_data(error); | |
276 } | |
277 | |
278 if(!code) | |
279 code = ""; | |
280 | |
7321 | 281 if(chat->conv) { |
282 if(!text) | |
283 text = g_strdup(_("Unknown error")); | |
284 buf = g_strdup_printf("Error %s in chat %s: %s", | |
285 code, from, text); | |
286 serv_got_chat_left(js->gc, chat->id); | |
287 } else { | |
288 if(!text) | |
289 text = g_strdup(_("Unable to join chat")); | |
290 buf = g_strdup_printf("Error %s joining chat %s: %s", | |
291 code, from, text); | |
292 } | |
7014 | 293 gaim_notify_error(js->gc, _("Error"), _("Error"), buf); |
294 g_free(text); | |
295 g_free(buf); | |
296 | |
297 jabber_chat_destroy(chat); | |
7310 | 298 jabber_id_free(jid); |
7615 | 299 g_free(status); |
7014 | 300 return; |
301 } | |
302 | |
303 | |
304 if(!chat->conv) { | |
305 chat->id = i++; | |
306 chat->muc = muc; | |
307 chat->conv = serv_got_joined_chat(js->gc, chat->id, room_jid); | |
308 } | |
309 | |
7813 | 310 if(type && !strcmp(type, "unavailable")) { |
7972 | 311 gboolean nick_change = FALSE; |
7973 | 312 |
313 jabber_buddy_remove_resource(jb, jid->resource); | |
7972 | 314 if(chat->muc) { |
315 xmlnode *x; | |
316 for(x = packet->child; x; x = x->next) { | |
317 const char *xmlns, *nick, *code; | |
318 xmlnode *stat, *item; | |
319 if(strcmp(x->name, "x")) | |
320 continue; | |
321 if(!(xmlns = xmlnode_get_attrib(x, "xmlns")) || | |
322 strcmp(xmlns, "http://jabber.org/protocol/muc#user")) | |
323 continue; | |
324 if(!(stat = xmlnode_get_child(x, "status"))) | |
325 continue; | |
326 if(!(code = xmlnode_get_attrib(stat, "code")) || strcmp(code, "303")) | |
327 continue; | |
328 if(!(item = xmlnode_get_child(x, "item"))) | |
329 continue; | |
330 if(!(nick = xmlnode_get_attrib(item, "nick"))) | |
331 continue; | |
332 nick_change = TRUE; | |
333 gaim_conv_chat_rename_user(GAIM_CONV_CHAT(chat->conv), jid->resource, nick); | |
334 if(!g_utf8_collate(jid->resource, chat->nick)) { | |
335 g_free(chat->nick); | |
336 chat->nick = g_strdup(nick); | |
337 } | |
338 break; | |
339 } | |
340 } | |
341 if(!nick_change) { | |
342 if(!strcmp(jid->resource, chat->nick)) { | |
343 serv_got_chat_left(js->gc, chat->id); | |
344 jabber_chat_destroy(chat); | |
345 } else { | |
346 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(chat->conv), jid->resource, | |
7974 | 347 status); |
7972 | 348 } |
7014 | 349 } |
350 } else { | |
7973 | 351 jabber_buddy_track_resource(jb, jid->resource, priority, state, |
352 status); | |
353 | |
7014 | 354 if(!jabber_chat_find_buddy(chat->conv, jid->resource)) |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
355 gaim_conv_chat_add_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
7944 | 356 real_jid); |
7014 | 357 } |
358 g_free(room_jid); | |
359 } else { | |
7124 | 360 if(state != JABBER_STATE_ERROR && !(jb->subscription & JABBER_SUB_TO)) { |
7014 | 361 gaim_debug(GAIM_DEBUG_INFO, "jabber", |
362 "got unexpected presence from %s, ignoring\n", from); | |
363 jabber_id_free(jid); | |
7615 | 364 g_free(status); |
7014 | 365 return; |
366 } | |
367 | |
7322 | 368 buddy_name = g_strdup_printf("%s%s%s", jid->node ? jid->node : "", |
369 jid->node ? "@" : "", jid->domain); | |
7014 | 370 if((b = gaim_find_buddy(js->gc->account, buddy_name)) == NULL) { |
371 jabber_id_free(jid); | |
372 g_free(buddy_name); | |
7615 | 373 g_free(status); |
7014 | 374 return; |
375 } | |
376 | |
377 if(state == JABBER_STATE_ERROR || | |
7813 | 378 (type && (!strcmp(type, "unavailable") || |
379 !strcmp(type, "unsubscribed")))) { | |
7014 | 380 jabber_buddy_remove_resource(jb, jid->resource); |
7395 | 381 } else { |
7014 | 382 jabber_buddy_track_resource(jb, jid->resource, priority, state, |
383 status); | |
7395 | 384 } |
7014 | 385 |
7835 | 386 if((jbr = jabber_buddy_find_resource(jb, NULL))) |
7014 | 387 serv_got_update(js->gc, buddy_name, 1, 0, b->signon, b->idle, |
388 jbr->state); | |
389 else | |
390 serv_got_update(js->gc, buddy_name, 0, 0, 0, 0, 0); | |
391 | |
392 g_free(buddy_name); | |
393 } | |
394 g_free(status); | |
395 jabber_id_free(jid); | |
396 } | |
397 | |
398 void jabber_presence_subscription_set(JabberStream *js, const char *who, const char *type) | |
399 { | |
400 xmlnode *presence = xmlnode_new("presence"); | |
401 | |
402 xmlnode_set_attrib(presence, "to", who); | |
403 xmlnode_set_attrib(presence, "type", type); | |
404 | |
405 jabber_send(js, presence); | |
406 xmlnode_free(presence); | |
407 } |