Mercurial > pidgin.yaz
annotate src/protocols/jabber/presence.c @ 9554:8b2451878e26
[gaim-migrate @ 10387]
" This patch adds chat user status icons (voice / halfop
/ op / founder) to chats
There's a screenshot here, showing ops, voices and
ignored ops and voices
http://nosnilmot.com/gaim/chatusers.png
This required some changes in how the core stores the
list of users in chats to be able to store the status
too, which are detailed below.
I also fixed up some memory leaks as I came across them
(string values returned by gtk_tree_model_get() not
being g_free()'d) and a minor bug in signals-test.c
Conversation API:
Changed:
gaim_conv_chat_add_user() (added flags parameter)
gaim_conv_chat_add_users() now (added GList of flags
parameter)
gaim_conv_chat_get_users() now returns a GList of
GaimChatBuddy's
gaim_conv_chat_set_users() now expects a GList of
GaimChatBuddy's
Added:
gaim_conv_chat_set_user_flags()
gaim_conv_chat_get_user_flags()
gaim_conv_chat_find_user()
gaim_conv_chat_cb_new()
gaim_conv_chat_cb_find()
gaim_conv_chat_cb_destroy()
gaim_conv_chat_cb_get_name()
Conversation UI ops:
added: chat_update_user()
Signals:
Changed: chat-buddy-joining & chat-buddy-joined now
include the user's flags
Added: chat-buddy-flags for when user's flags change
Added:
gaim_marshal_VOID__POINTER_POINTER_POINTER_UINT_UINT
(required for the new chat-buddy-flags signal)
Protocol Plugins:
All updated to work with above changes (obviously)
User flags support added to IRC, Jabber and SILC
New Files:
pixmaps/status/default/ voice.svg halfop.svg op.svg
founder.svg
" --Stu Tomlinson
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Sat, 17 Jul 2004 18:11:12 +0000 |
parents | 7a9261d979df |
children | 92adc34d45da |
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"; | |
9320 | 126 else if(!strcmp(state, _("Invisible"))) |
7014 | 127 xmlnode_set_attrib(presence, "type", "invisible"); |
9320 | 128 else if(!strcmp(state, "unavailable")) |
129 xmlnode_set_attrib(presence, "type", "unavailable"); | |
7014 | 130 |
131 if(show_string) { | |
132 show = xmlnode_new_child(presence, "show"); | |
133 xmlnode_insert_data(show, show_string, -1); | |
134 } | |
135 } | |
136 | |
137 if(msg && *msg) { | |
138 status = xmlnode_new_child(presence, "status"); | |
139 xmlnode_insert_data(status, msg, -1); | |
140 } | |
141 | |
142 return presence; | |
143 } | |
144 | |
145 struct _jabber_add_permit { | |
146 GaimConnection *gc; | |
147 char *who; | |
148 }; | |
149 | |
150 static void authorize_add_cb(struct _jabber_add_permit *jap) | |
151 { | |
152 if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
153 jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
154 "subscribed"); | |
155 | |
156 if(!gaim_find_buddy(jap->gc->account, jap->who)) | |
7015
dece74f05509
[gaim-migrate @ 7578]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
157 gaim_account_notify_added(jap->gc->account, NULL, jap->who, NULL, NULL); |
7014 | 158 } |
159 | |
160 g_free(jap->who); | |
161 g_free(jap); | |
162 } | |
163 | |
164 static void deny_add_cb(struct _jabber_add_permit *jap) | |
165 { | |
166 if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
167 jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
168 "unsubscribed"); | |
169 } | |
170 | |
171 g_free(jap->who); | |
172 g_free(jap); | |
173 } | |
174 | |
8193 | 175 static int show_to_state(const char *show) { |
176 if(!show) | |
177 return 0; | |
178 else if(!strcmp(show, "away")) | |
179 return JABBER_STATE_AWAY; | |
180 else if(!strcmp(show, "chat")) | |
181 return JABBER_STATE_CHAT; | |
182 else if(!strcmp(show, "xa")) | |
183 return JABBER_STATE_XA; | |
184 else if(!strcmp(show, "dnd")) | |
185 return JABBER_STATE_DND; | |
186 return 0; | |
187 } | |
188 | |
7014 | 189 void jabber_presence_parse(JabberStream *js, xmlnode *packet) |
190 { | |
191 const char *from = xmlnode_get_attrib(packet, "from"); | |
192 const char *type = xmlnode_get_attrib(packet, "type"); | |
7944 | 193 const char *real_jid = NULL; |
9152 | 194 const char *affiliation = NULL; |
195 const char *role = NULL; | |
7014 | 196 char *status = NULL; |
197 int priority = 0; | |
198 JabberID *jid; | |
199 JabberChat *chat; | |
200 JabberBuddy *jb; | |
7835 | 201 JabberBuddyResource *jbr = NULL; |
9554 | 202 GaimConvChatBuddyFlags flags = GAIM_CBFLAGS_NONE; |
7014 | 203 GaimBuddy *b; |
204 char *buddy_name; | |
205 int state = 0; | |
206 xmlnode *y; | |
207 gboolean muc = FALSE; | |
208 | |
209 | |
8043 | 210 if(!(jb = jabber_buddy_find(js, from, TRUE))) |
211 return; | |
212 | |
213 if(!(jid = jabber_id_new(from))) | |
7280 | 214 return; |
215 | |
7014 | 216 if(jb->error_msg) { |
217 g_free(jb->error_msg); | |
218 jb->error_msg = NULL; | |
219 } | |
220 | |
7813 | 221 if(type && !strcmp(type, "error")) { |
8401 | 222 char *msg = jabber_parse_error(js, packet); |
7644 | 223 |
7014 | 224 state = JABBER_STATE_ERROR; |
8401 | 225 jb->error_msg = msg ? msg : g_strdup(_("Unknown Error in presence")); |
7813 | 226 } else if(type && !strcmp(type, "subscribe")) { |
7014 | 227 struct _jabber_add_permit *jap = g_new0(struct _jabber_add_permit, 1); |
228 char *msg = g_strdup_printf(_("The user %s wants to add you to their buddy list."), from); | |
229 jap->gc = js->gc; | |
230 jap->who = g_strdup(from); | |
231 | |
232 gaim_request_action(js->gc, NULL, msg, NULL, 0, jap, 2, | |
233 _("Authorize"), G_CALLBACK(authorize_add_cb), | |
234 _("Deny"), G_CALLBACK(deny_add_cb)); | |
235 g_free(msg); | |
8043 | 236 jabber_id_free(jid); |
7145 | 237 return; |
7813 | 238 } else if(type && !strcmp(type, "subscribed")) { |
7014 | 239 /* we've been allowed to see their presence, but we don't care */ |
8043 | 240 jabber_id_free(jid); |
7014 | 241 return; |
242 } else { | |
243 if((y = xmlnode_get_child(packet, "show"))) { | |
244 char *show = xmlnode_get_data(y); | |
8185 | 245 state = show_to_state(show); |
7014 | 246 g_free(show); |
247 } else { | |
248 state = 0; | |
249 } | |
250 } | |
251 | |
7310 | 252 |
7014 | 253 for(y = packet->child; y; y = y->next) { |
8135 | 254 if(y->type != XMLNODE_TYPE_TAG) |
7014 | 255 continue; |
256 | |
257 if(!strcmp(y->name, "status")) { | |
7615 | 258 g_free(status); |
7014 | 259 status = xmlnode_get_data(y); |
260 } else if(!strcmp(y->name, "priority")) { | |
261 char *p = xmlnode_get_data(y); | |
262 if(p) { | |
263 priority = atoi(p); | |
264 g_free(p); | |
265 } | |
266 } else if(!strcmp(y->name, "x")) { | |
267 const char *xmlns = xmlnode_get_attrib(y, "xmlns"); | |
268 if(xmlns && !strcmp(xmlns, "http://jabber.org/protocol/muc#user")) { | |
269 /* this is where we'd normally get the "op" status of the | |
270 * user, but since we don't have a good way to show that yet | |
271 * we'll ignore it */ | |
7629 | 272 xmlnode *z; |
273 | |
7014 | 274 muc = TRUE; |
7629 | 275 if((z = xmlnode_get_child(y, "status"))) { |
276 const char *code = xmlnode_get_attrib(z, "code"); | |
277 if(code && !strcmp(code, "201")) { | |
7895 | 278 chat = jabber_chat_find(js, jid->node, jid->domain); |
8396 | 279 chat->config_dialog_type = GAIM_REQUEST_ACTION; |
280 chat->config_dialog_handle = | |
281 gaim_request_action(js->gc, _("Create New Room"), | |
7895 | 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"); | |
9152 | 293 affiliation = xmlnode_get_attrib(z, "affiliation"); |
294 role = xmlnode_get_attrib(z, "role"); | |
7944 | 295 } |
7014 | 296 } |
297 } | |
298 } | |
299 | |
300 | |
7322 | 301 if(jid->node && (chat = jabber_chat_find(js, jid->node, jid->domain))) { |
8462 | 302 static int i = 1; |
7014 | 303 char *room_jid = g_strdup_printf("%s@%s", jid->node, jid->domain); |
304 | |
305 if(state == JABBER_STATE_ERROR) { | |
8401 | 306 char *title, *msg = jabber_parse_error(js, packet); |
7014 | 307 |
7321 | 308 if(chat->conv) { |
8401 | 309 title = g_strdup_printf(_("Error in chat %s"), from); |
7321 | 310 serv_got_chat_left(js->gc, chat->id); |
311 } else { | |
8401 | 312 title = g_strdup_printf(_("Error joining chat %s"), from); |
7321 | 313 } |
8401 | 314 gaim_notify_error(js->gc, title, title, msg); |
315 g_free(title); | |
316 g_free(msg); | |
7014 | 317 |
318 jabber_chat_destroy(chat); | |
7310 | 319 jabber_id_free(jid); |
7615 | 320 g_free(status); |
8182 | 321 g_free(room_jid); |
7014 | 322 return; |
323 } | |
324 | |
325 | |
7813 | 326 if(type && !strcmp(type, "unavailable")) { |
7972 | 327 gboolean nick_change = FALSE; |
7973 | 328 |
9152 | 329 /* If we haven't joined the chat yet, we don't care that someone |
330 * left, or it was us leaving after we closed the chat */ | |
8182 | 331 if(!chat->conv) { |
9152 | 332 if(!strcmp(jid->resource, chat->handle)) |
333 jabber_chat_destroy(chat); | |
8182 | 334 jabber_id_free(jid); |
335 g_free(status); | |
336 g_free(room_jid); | |
337 return; | |
338 } | |
339 | |
7973 | 340 jabber_buddy_remove_resource(jb, jid->resource); |
7972 | 341 if(chat->muc) { |
342 xmlnode *x; | |
8135 | 343 for(x = xmlnode_get_child(packet, "x"); x; x = xmlnode_get_next_twin(x)) { |
7972 | 344 const char *xmlns, *nick, *code; |
345 xmlnode *stat, *item; | |
346 if(!(xmlns = xmlnode_get_attrib(x, "xmlns")) || | |
347 strcmp(xmlns, "http://jabber.org/protocol/muc#user")) | |
348 continue; | |
349 if(!(stat = xmlnode_get_child(x, "status"))) | |
350 continue; | |
9152 | 351 if(!(code = xmlnode_get_attrib(stat, "code"))) |
7972 | 352 continue; |
9152 | 353 if(!strcmp(code, "301")) { |
354 /* XXX: we got banned */ | |
355 } else if(!strcmp(code, "303")) { | |
356 if(!(item = xmlnode_get_child(x, "item"))) | |
357 continue; | |
358 if(!(nick = xmlnode_get_attrib(item, "nick"))) | |
359 continue; | |
360 nick_change = TRUE; | |
361 if(!strcmp(jid->resource, chat->handle)) { | |
362 g_free(chat->handle); | |
363 chat->handle = g_strdup(nick); | |
364 } | |
365 gaim_conv_chat_rename_user(GAIM_CONV_CHAT(chat->conv), jid->resource, nick); | |
366 jabber_chat_remove_handle(chat, jid->resource); | |
367 break; | |
368 } else if(!strcmp(code, "307")) { | |
369 /* XXX: we got kicked */ | |
370 } else if(!strcmp(code, "321")) { | |
371 /* XXX: removed due to an affiliation change */ | |
372 } else if(!strcmp(code, "322")) { | |
373 /* XXX: removed because room is now members-only */ | |
374 } else if(!strcmp(code, "332")) { | |
375 /* XXX: removed due to system shutdown */ | |
8401 | 376 } |
7972 | 377 } |
378 } | |
379 if(!nick_change) { | |
9152 | 380 if(!g_utf8_collate(jid->resource, chat->handle)) { |
7972 | 381 serv_got_chat_left(js->gc, chat->id); |
382 jabber_chat_destroy(chat); | |
383 } else { | |
384 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(chat->conv), jid->resource, | |
7974 | 385 status); |
9152 | 386 jabber_chat_remove_handle(chat, jid->resource); |
7972 | 387 } |
7014 | 388 } |
389 } else { | |
8182 | 390 if(!chat->conv) { |
391 chat->id = i++; | |
392 chat->muc = muc; | |
393 chat->conv = serv_got_joined_chat(js->gc, chat->id, room_jid); | |
8401 | 394 g_free(chat->handle); |
395 chat->handle = g_strdup(jid->resource); | |
8182 | 396 gaim_conv_chat_set_nick(GAIM_CONV_CHAT(chat->conv), jid->resource); |
397 } | |
398 | |
7973 | 399 jabber_buddy_track_resource(jb, jid->resource, priority, state, |
400 status); | |
401 | |
9152 | 402 jabber_chat_track_handle(chat, jid->resource, real_jid, affiliation, role); |
403 | |
9554 | 404 if (!strcmp(role, "moderator")) |
405 flags = GAIM_CBFLAGS_OP; | |
406 else if (!strcmp(role, "participant")) | |
407 flags = GAIM_CBFLAGS_VOICE; | |
408 | |
7014 | 409 if(!jabber_chat_find_buddy(chat->conv, jid->resource)) |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
410 gaim_conv_chat_add_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
9554 | 411 real_jid, flags); |
412 else | |
413 gaim_conv_chat_user_set_flags(GAIM_CONV_CHAT(chat->conv), jid->resource, | |
414 flags); | |
7014 | 415 } |
416 g_free(room_jid); | |
417 } else { | |
7124 | 418 if(state != JABBER_STATE_ERROR && !(jb->subscription & JABBER_SUB_TO)) { |
7014 | 419 gaim_debug(GAIM_DEBUG_INFO, "jabber", |
420 "got unexpected presence from %s, ignoring\n", from); | |
421 jabber_id_free(jid); | |
7615 | 422 g_free(status); |
7014 | 423 return; |
424 } | |
425 | |
7322 | 426 buddy_name = g_strdup_printf("%s%s%s", jid->node ? jid->node : "", |
427 jid->node ? "@" : "", jid->domain); | |
7014 | 428 if((b = gaim_find_buddy(js->gc->account, buddy_name)) == NULL) { |
429 jabber_id_free(jid); | |
430 g_free(buddy_name); | |
7615 | 431 g_free(status); |
7014 | 432 return; |
433 } | |
434 | |
435 if(state == JABBER_STATE_ERROR || | |
7813 | 436 (type && (!strcmp(type, "unavailable") || |
437 !strcmp(type, "unsubscribed")))) { | |
8043 | 438 GaimConversation *conv; |
439 | |
7014 | 440 jabber_buddy_remove_resource(jb, jid->resource); |
8043 | 441 if((conv = jabber_find_unnormalized_conv(from, js->gc->account))) |
442 gaim_conversation_set_name(conv, buddy_name); | |
443 | |
7395 | 444 } else { |
7014 | 445 jabber_buddy_track_resource(jb, jid->resource, priority, state, |
446 status); | |
7395 | 447 } |
7014 | 448 |
7835 | 449 if((jbr = jabber_buddy_find_resource(jb, NULL))) |
7014 | 450 serv_got_update(js->gc, buddy_name, 1, 0, b->signon, b->idle, |
451 jbr->state); | |
452 else | |
453 serv_got_update(js->gc, buddy_name, 0, 0, 0, 0, 0); | |
454 | |
455 g_free(buddy_name); | |
456 } | |
457 g_free(status); | |
458 jabber_id_free(jid); | |
459 } | |
460 | |
461 void jabber_presence_subscription_set(JabberStream *js, const char *who, const char *type) | |
462 { | |
463 xmlnode *presence = xmlnode_new("presence"); | |
464 | |
465 xmlnode_set_attrib(presence, "to", who); | |
466 xmlnode_set_attrib(presence, "type", type); | |
467 | |
468 jabber_send(js, presence); | |
469 xmlnode_free(presence); | |
470 } |