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