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