Mercurial > pidgin
annotate src/protocols/jabber/presence.c @ 8401:c13a4913a071
[gaim-migrate @ 9130]
better error processing for jabber
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Fri, 05 Mar 2004 23:42:34 +0000 |
parents | 387ba791d5f9 |
children | 5a39fce812f4 |
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; | |
8401 | 42 char *chat_full_jid = g_strdup_printf("%s@%s/%s", chat->room, chat->server, |
43 chat->handle); | |
7014 | 44 |
8401 | 45 xmlnode_set_attrib(presence, "to", chat_full_jid); |
7014 | 46 jabber_send(chat->js, presence); |
8401 | 47 g_free(chat_full_jid); |
7014 | 48 } |
49 | |
8193 | 50 void jabber_presence_fake_to_self(JabberStream *js, const char *away_state, const char *msg) { |
8185 | 51 char *my_base_jid = g_strdup_printf("%s@%s", js->user->node, js->user->domain); |
52 if(gaim_find_buddy(js->gc->account, my_base_jid)) { | |
53 JabberBuddy *jb; | |
54 JabberBuddyResource *jbr; | |
55 if((jb = jabber_buddy_find(js, my_base_jid, TRUE))) { | |
8193 | 56 int state = 0; |
57 if(away_state) { | |
58 if(!strcmp(away_state, _("Away")) || | |
59 (msg && *msg && !strcmp(away_state, GAIM_AWAY_CUSTOM))) | |
60 state = JABBER_STATE_AWAY; | |
61 else if(!strcmp(away_state, _("Chatty"))) | |
62 state = JABBER_STATE_CHAT; | |
63 else if(!strcmp(away_state, _("Extended Away"))) | |
64 state = JABBER_STATE_XA; | |
65 else if(!strcmp(away_state, _("Do Not Disturb"))) | |
66 state = JABBER_STATE_DND; | |
67 } | |
68 jabber_buddy_track_resource(jb, js->user->resource, 0, state, (msg && *msg) ? msg : NULL); | |
8185 | 69 if((jbr = jabber_buddy_find_resource(jb, NULL))) |
70 serv_got_update(js->gc, my_base_jid, 1, 0, 0, 0, jbr->state); | |
71 } | |
72 } | |
73 g_free(my_base_jid); | |
74 } | |
75 | |
7014 | 76 |
77 void jabber_presence_send(GaimConnection *gc, const char *state, | |
78 const char *msg) | |
79 { | |
80 JabberStream *js = gc->proto_data; | |
81 xmlnode *presence; | |
82 char *stripped = NULL; | |
83 | |
84 if(msg) { | |
7095
c8bf2da398e3
[gaim-migrate @ 7660]
Christian Hammond <chipx86@chipx86.com>
parents:
7015
diff
changeset
|
85 gaim_markup_html_to_xhtml(msg, NULL, &stripped); |
7249 | 86 } else if(!state || strcmp(state, GAIM_AWAY_CUSTOM)) { |
7247 | 87 /* i can't wait until someone re-writes the status/away stuff */ |
7014 | 88 stripped = g_strdup(""); |
89 } | |
90 | |
7248 | 91 if(gc->away) |
92 g_free(gc->away); | |
7014 | 93 gc->away = stripped; |
94 | |
8185 | 95 presence = jabber_presence_create(state, stripped); |
7014 | 96 jabber_send(js, presence); |
97 g_hash_table_foreach(js->chats, chats_send_presence_foreach, presence); | |
98 xmlnode_free(presence); | |
8185 | 99 |
100 jabber_presence_fake_to_self(js, state, stripped); | |
7014 | 101 } |
102 | |
103 xmlnode *jabber_presence_create(const char *state, const char *msg) | |
104 { | |
105 xmlnode *show, *status, *presence; | |
106 | |
107 | |
108 presence = xmlnode_new("presence"); | |
109 | |
110 if(state) { | |
111 const char *show_string = NULL; | |
112 if(!strcmp(state, _("Chatty"))) | |
113 show_string = "chat"; | |
114 else if(!strcmp(state, _("Away")) || | |
115 (msg && !strcmp(state, GAIM_AWAY_CUSTOM))) | |
116 show_string = "away"; | |
117 else if(!strcmp(state, _("Extended Away"))) | |
118 show_string = "xa"; | |
119 else if(!strcmp(state, _("Do Not Disturb"))) | |
120 show_string = "dnd"; | |
121 else if(!strcmp(state, _("Invisible"))) { | |
122 xmlnode_set_attrib(presence, "type", "invisible"); | |
123 } | |
124 | |
125 if(show_string) { | |
126 show = xmlnode_new_child(presence, "show"); | |
127 xmlnode_insert_data(show, show_string, -1); | |
128 } | |
129 } | |
130 | |
131 if(msg && *msg) { | |
132 status = xmlnode_new_child(presence, "status"); | |
133 xmlnode_insert_data(status, msg, -1); | |
134 } | |
135 | |
136 return presence; | |
137 } | |
138 | |
139 struct _jabber_add_permit { | |
140 GaimConnection *gc; | |
141 char *who; | |
142 }; | |
143 | |
144 static void authorize_add_cb(struct _jabber_add_permit *jap) | |
145 { | |
146 if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
147 jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
148 "subscribed"); | |
149 | |
150 if(!gaim_find_buddy(jap->gc->account, jap->who)) | |
7015
dece74f05509
[gaim-migrate @ 7578]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
151 gaim_account_notify_added(jap->gc->account, NULL, jap->who, NULL, NULL); |
7014 | 152 } |
153 | |
154 g_free(jap->who); | |
155 g_free(jap); | |
156 } | |
157 | |
158 static void deny_add_cb(struct _jabber_add_permit *jap) | |
159 { | |
160 if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
161 jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
162 "unsubscribed"); | |
163 } | |
164 | |
165 g_free(jap->who); | |
166 g_free(jap); | |
167 } | |
168 | |
8193 | 169 static int show_to_state(const char *show) { |
170 if(!show) | |
171 return 0; | |
172 else if(!strcmp(show, "away")) | |
173 return JABBER_STATE_AWAY; | |
174 else if(!strcmp(show, "chat")) | |
175 return JABBER_STATE_CHAT; | |
176 else if(!strcmp(show, "xa")) | |
177 return JABBER_STATE_XA; | |
178 else if(!strcmp(show, "dnd")) | |
179 return JABBER_STATE_DND; | |
180 return 0; | |
181 } | |
182 | |
7014 | 183 void jabber_presence_parse(JabberStream *js, xmlnode *packet) |
184 { | |
185 const char *from = xmlnode_get_attrib(packet, "from"); | |
186 const char *type = xmlnode_get_attrib(packet, "type"); | |
7944 | 187 const char *real_jid = NULL; |
7014 | 188 char *status = NULL; |
189 int priority = 0; | |
190 JabberID *jid; | |
191 JabberChat *chat; | |
192 JabberBuddy *jb; | |
7835 | 193 JabberBuddyResource *jbr = NULL; |
7014 | 194 GaimBuddy *b; |
195 char *buddy_name; | |
196 int state = 0; | |
197 xmlnode *y; | |
198 gboolean muc = FALSE; | |
199 | |
200 | |
8043 | 201 if(!(jb = jabber_buddy_find(js, from, TRUE))) |
202 return; | |
203 | |
204 if(!(jid = jabber_id_new(from))) | |
7280 | 205 return; |
206 | |
7014 | 207 if(jb->error_msg) { |
208 g_free(jb->error_msg); | |
209 jb->error_msg = NULL; | |
210 } | |
211 | |
7813 | 212 if(type && !strcmp(type, "error")) { |
8401 | 213 char *msg = jabber_parse_error(js, packet); |
7644 | 214 |
7014 | 215 state = JABBER_STATE_ERROR; |
8401 | 216 jb->error_msg = msg ? msg : g_strdup(_("Unknown Error in presence")); |
7813 | 217 } else if(type && !strcmp(type, "subscribe")) { |
7014 | 218 struct _jabber_add_permit *jap = g_new0(struct _jabber_add_permit, 1); |
219 char *msg = g_strdup_printf(_("The user %s wants to add you to their buddy list."), from); | |
220 jap->gc = js->gc; | |
221 jap->who = g_strdup(from); | |
222 | |
223 gaim_request_action(js->gc, NULL, msg, NULL, 0, jap, 2, | |
224 _("Authorize"), G_CALLBACK(authorize_add_cb), | |
225 _("Deny"), G_CALLBACK(deny_add_cb)); | |
226 g_free(msg); | |
8043 | 227 jabber_id_free(jid); |
7145 | 228 return; |
7813 | 229 } else if(type && !strcmp(type, "subscribed")) { |
7014 | 230 /* we've been allowed to see their presence, but we don't care */ |
8043 | 231 jabber_id_free(jid); |
7014 | 232 return; |
233 } else { | |
234 if((y = xmlnode_get_child(packet, "show"))) { | |
235 char *show = xmlnode_get_data(y); | |
8185 | 236 state = show_to_state(show); |
7014 | 237 g_free(show); |
238 } else { | |
239 state = 0; | |
240 } | |
241 } | |
242 | |
7310 | 243 |
7014 | 244 for(y = packet->child; y; y = y->next) { |
8135 | 245 if(y->type != XMLNODE_TYPE_TAG) |
7014 | 246 continue; |
247 | |
248 if(!strcmp(y->name, "status")) { | |
7615 | 249 g_free(status); |
7014 | 250 status = xmlnode_get_data(y); |
251 } else if(!strcmp(y->name, "priority")) { | |
252 char *p = xmlnode_get_data(y); | |
253 if(p) { | |
254 priority = atoi(p); | |
255 g_free(p); | |
256 } | |
257 } else if(!strcmp(y->name, "x")) { | |
258 const char *xmlns = xmlnode_get_attrib(y, "xmlns"); | |
259 if(xmlns && !strcmp(xmlns, "http://jabber.org/protocol/muc#user")) { | |
260 /* this is where we'd normally get the "op" status of the | |
261 * user, but since we don't have a good way to show that yet | |
262 * we'll ignore it */ | |
7629 | 263 xmlnode *z; |
264 | |
7014 | 265 muc = TRUE; |
7629 | 266 if((z = xmlnode_get_child(y, "status"))) { |
267 const char *code = xmlnode_get_attrib(z, "code"); | |
268 if(code && !strcmp(code, "201")) { | |
7895 | 269 chat = jabber_chat_find(js, jid->node, jid->domain); |
8396 | 270 chat->config_dialog_type = GAIM_REQUEST_ACTION; |
271 chat->config_dialog_handle = | |
272 gaim_request_action(js->gc, _("Create New Room"), | |
7895 | 273 _("Create New Room"), |
274 _("You are creating a new room. Would you like to " | |
275 "configure it, or accept the default settings?"), | |
276 1, chat, 2, _("Configure Room"), | |
7923 | 277 G_CALLBACK(jabber_chat_request_room_configure), |
7895 | 278 _("Accept Defaults"), |
279 G_CALLBACK(jabber_chat_create_instant_room)); | |
7629 | 280 } |
281 } | |
7944 | 282 if((z = xmlnode_get_child(y, "item"))) { |
283 real_jid = xmlnode_get_attrib(z, "jid"); | |
284 } | |
7014 | 285 } |
286 } | |
287 } | |
288 | |
289 | |
7322 | 290 if(jid->node && (chat = jabber_chat_find(js, jid->node, jid->domain))) { |
7014 | 291 static int i = 0; |
292 char *room_jid = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
293 | |
294 if(state == JABBER_STATE_ERROR) { | |
8401 | 295 char *title, *msg = jabber_parse_error(js, packet); |
7014 | 296 |
7321 | 297 if(chat->conv) { |
8401 | 298 title = g_strdup_printf(_("Error in chat %s"), from); |
7321 | 299 serv_got_chat_left(js->gc, chat->id); |
300 } else { | |
8401 | 301 title = g_strdup_printf(_("Error joining chat %s"), from); |
7321 | 302 } |
8401 | 303 gaim_notify_error(js->gc, title, title, msg); |
304 g_free(title); | |
305 g_free(msg); | |
7014 | 306 |
307 jabber_chat_destroy(chat); | |
7310 | 308 jabber_id_free(jid); |
7615 | 309 g_free(status); |
8182 | 310 g_free(room_jid); |
7014 | 311 return; |
312 } | |
313 | |
314 | |
7813 | 315 if(type && !strcmp(type, "unavailable")) { |
7972 | 316 gboolean nick_change = FALSE; |
7973 | 317 |
8182 | 318 /* If we haven't joined the chat yet, we don't care that someone left */ |
319 if(!chat->conv) { | |
320 jabber_id_free(jid); | |
321 g_free(status); | |
322 g_free(room_jid); | |
323 return; | |
324 } | |
325 | |
7973 | 326 jabber_buddy_remove_resource(jb, jid->resource); |
7972 | 327 if(chat->muc) { |
328 xmlnode *x; | |
8135 | 329 for(x = xmlnode_get_child(packet, "x"); x; x = xmlnode_get_next_twin(x)) { |
7972 | 330 const char *xmlns, *nick, *code; |
331 xmlnode *stat, *item; | |
332 if(!(xmlns = xmlnode_get_attrib(x, "xmlns")) || | |
333 strcmp(xmlns, "http://jabber.org/protocol/muc#user")) | |
334 continue; | |
335 if(!(stat = xmlnode_get_child(x, "status"))) | |
336 continue; | |
337 if(!(code = xmlnode_get_attrib(stat, "code")) || strcmp(code, "303")) | |
338 continue; | |
339 if(!(item = xmlnode_get_child(x, "item"))) | |
340 continue; | |
341 if(!(nick = xmlnode_get_attrib(item, "nick"))) | |
342 continue; | |
343 nick_change = TRUE; | |
8401 | 344 if(!strcmp(jid->resource, chat->handle)) { |
345 g_free(chat->handle); | |
346 chat->handle = g_strdup(nick); | |
347 } | |
7972 | 348 gaim_conv_chat_rename_user(GAIM_CONV_CHAT(chat->conv), jid->resource, nick); |
349 break; | |
350 } | |
351 } | |
352 if(!nick_change) { | |
8158 | 353 if(!g_utf8_collate(jid->resource, gaim_conv_chat_get_nick(GAIM_CONV_CHAT(chat->conv)))) { |
7972 | 354 serv_got_chat_left(js->gc, chat->id); |
355 jabber_chat_destroy(chat); | |
356 } else { | |
357 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(chat->conv), jid->resource, | |
7974 | 358 status); |
7972 | 359 } |
7014 | 360 } |
361 } else { | |
8182 | 362 if(!chat->conv) { |
363 chat->id = i++; | |
364 chat->muc = muc; | |
365 chat->conv = serv_got_joined_chat(js->gc, chat->id, room_jid); | |
8401 | 366 g_free(chat->handle); |
367 chat->handle = g_strdup(jid->resource); | |
8182 | 368 gaim_conv_chat_set_nick(GAIM_CONV_CHAT(chat->conv), jid->resource); |
369 } | |
370 | |
7973 | 371 jabber_buddy_track_resource(jb, jid->resource, priority, state, |
372 status); | |
373 | |
7014 | 374 if(!jabber_chat_find_buddy(chat->conv, jid->resource)) |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
375 gaim_conv_chat_add_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
7944 | 376 real_jid); |
7014 | 377 } |
378 g_free(room_jid); | |
379 } else { | |
7124 | 380 if(state != JABBER_STATE_ERROR && !(jb->subscription & JABBER_SUB_TO)) { |
7014 | 381 gaim_debug(GAIM_DEBUG_INFO, "jabber", |
382 "got unexpected presence from %s, ignoring\n", from); | |
383 jabber_id_free(jid); | |
7615 | 384 g_free(status); |
7014 | 385 return; |
386 } | |
387 | |
7322 | 388 buddy_name = g_strdup_printf("%s%s%s", jid->node ? jid->node : "", |
389 jid->node ? "@" : "", jid->domain); | |
7014 | 390 if((b = gaim_find_buddy(js->gc->account, buddy_name)) == NULL) { |
391 jabber_id_free(jid); | |
392 g_free(buddy_name); | |
7615 | 393 g_free(status); |
7014 | 394 return; |
395 } | |
396 | |
397 if(state == JABBER_STATE_ERROR || | |
7813 | 398 (type && (!strcmp(type, "unavailable") || |
399 !strcmp(type, "unsubscribed")))) { | |
8043 | 400 GaimConversation *conv; |
401 | |
7014 | 402 jabber_buddy_remove_resource(jb, jid->resource); |
8043 | 403 if((conv = jabber_find_unnormalized_conv(from, js->gc->account))) |
404 gaim_conversation_set_name(conv, buddy_name); | |
405 | |
7395 | 406 } else { |
7014 | 407 jabber_buddy_track_resource(jb, jid->resource, priority, state, |
408 status); | |
7395 | 409 } |
7014 | 410 |
7835 | 411 if((jbr = jabber_buddy_find_resource(jb, NULL))) |
7014 | 412 serv_got_update(js->gc, buddy_name, 1, 0, b->signon, b->idle, |
413 jbr->state); | |
414 else | |
415 serv_got_update(js->gc, buddy_name, 0, 0, 0, 0, 0); | |
416 | |
417 g_free(buddy_name); | |
418 } | |
419 g_free(status); | |
420 jabber_id_free(jid); | |
421 } | |
422 | |
423 void jabber_presence_subscription_set(JabberStream *js, const char *who, const char *type) | |
424 { | |
425 xmlnode *presence = xmlnode_new("presence"); | |
426 | |
427 xmlnode_set_attrib(presence, "to", who); | |
428 xmlnode_set_attrib(presence, "type", type); | |
429 | |
430 jabber_send(js, presence); | |
431 xmlnode_free(presence); | |
432 } |