Mercurial > pidgin
annotate src/protocols/jabber/presence.c @ 7183:071dee25c48e
[gaim-migrate @ 7751]
fix jabber segfault on changing of topic in a chat
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Tue, 07 Oct 2003 14:20:48 +0000 |
parents | 0e4894b3e2a6 |
children | 89dc8a119918 |
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); |
7014 | 58 } else { |
59 stripped = g_strdup(""); | |
60 } | |
61 | |
62 gc->away = stripped; | |
63 | |
64 presence = jabber_presence_create(state, msg); | |
65 jabber_send(js, presence); | |
66 g_hash_table_foreach(js->chats, chats_send_presence_foreach, presence); | |
67 xmlnode_free(presence); | |
68 } | |
69 | |
70 xmlnode *jabber_presence_create(const char *state, const char *msg) | |
71 { | |
72 xmlnode *show, *status, *presence; | |
73 | |
74 | |
75 presence = xmlnode_new("presence"); | |
76 | |
77 if(state) { | |
78 const char *show_string = NULL; | |
79 if(!strcmp(state, _("Chatty"))) | |
80 show_string = "chat"; | |
81 else if(!strcmp(state, _("Away")) || | |
82 (msg && !strcmp(state, GAIM_AWAY_CUSTOM))) | |
83 show_string = "away"; | |
84 else if(!strcmp(state, _("Extended Away"))) | |
85 show_string = "xa"; | |
86 else if(!strcmp(state, _("Do Not Disturb"))) | |
87 show_string = "dnd"; | |
88 else if(!strcmp(state, _("Invisible"))) { | |
89 xmlnode_set_attrib(presence, "type", "invisible"); | |
90 } | |
91 | |
92 if(show_string) { | |
93 show = xmlnode_new_child(presence, "show"); | |
94 xmlnode_insert_data(show, show_string, -1); | |
95 } | |
96 } | |
97 | |
98 if(msg && *msg) { | |
99 status = xmlnode_new_child(presence, "status"); | |
100 xmlnode_insert_data(status, msg, -1); | |
101 } | |
102 | |
103 return presence; | |
104 } | |
105 | |
106 struct _jabber_add_permit { | |
107 GaimConnection *gc; | |
108 char *who; | |
109 }; | |
110 | |
111 static void authorize_add_cb(struct _jabber_add_permit *jap) | |
112 { | |
113 if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
114 jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
115 "subscribed"); | |
116 | |
117 if(!gaim_find_buddy(jap->gc->account, jap->who)) | |
7015
dece74f05509
[gaim-migrate @ 7578]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
118 gaim_account_notify_added(jap->gc->account, NULL, jap->who, NULL, NULL); |
7014 | 119 } |
120 | |
121 g_free(jap->who); | |
122 g_free(jap); | |
123 } | |
124 | |
125 static void deny_add_cb(struct _jabber_add_permit *jap) | |
126 { | |
127 if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
128 jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
129 "unsubscribed"); | |
130 | |
131 if(!gaim_find_buddy(jap->gc->account, jap->who)) | |
7015
dece74f05509
[gaim-migrate @ 7578]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
132 gaim_account_notify_added(jap->gc->account, NULL, jap->who, NULL, NULL); |
7014 | 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"); | |
143 char *status = NULL; | |
144 int priority = 0; | |
145 JabberID *jid; | |
146 JabberChat *chat; | |
147 JabberBuddy *jb; | |
148 JabberBuddyResource *jbr; | |
149 GaimBuddy *b; | |
150 char *buddy_name; | |
151 int state = 0; | |
152 xmlnode *y; | |
153 gboolean muc = FALSE; | |
154 | |
155 jb = jabber_buddy_find(js, from, TRUE); | |
156 | |
157 if(jb->error_msg) { | |
158 g_free(jb->error_msg); | |
159 jb->error_msg = NULL; | |
160 } | |
161 | |
162 if(type && !strcasecmp(type, "error")) { | |
163 state = JABBER_STATE_ERROR; | |
164 if((y = xmlnode_get_child(packet, "error")) != NULL) { | |
165 char *txt = xmlnode_get_data(y); | |
166 jb->error_msg = g_strdup_printf(_("%s (Code %s)"), | |
167 txt, xmlnode_get_attrib(y, "code")); | |
168 g_free(txt); | |
169 } else { | |
170 jb->error_msg = g_strdup(_("Unknown Error in presence")); | |
171 } | |
172 } else if(type && !strcasecmp(type, "subscribe")) { | |
173 struct _jabber_add_permit *jap = g_new0(struct _jabber_add_permit, 1); | |
174 char *msg = g_strdup_printf(_("The user %s wants to add you to their buddy list."), from); | |
175 jap->gc = js->gc; | |
176 jap->who = g_strdup(from); | |
177 | |
178 gaim_request_action(js->gc, NULL, msg, NULL, 0, jap, 2, | |
179 _("Authorize"), G_CALLBACK(authorize_add_cb), | |
180 _("Deny"), G_CALLBACK(deny_add_cb)); | |
181 g_free(msg); | |
7145 | 182 return; |
7014 | 183 } else if(type && (!strcmp(type, "subscribed") || |
184 !strcmp(type, "unsubscribed"))) { | |
185 /* we've been allowed to see their presence, but we don't care */ | |
186 return; | |
187 } else { | |
188 if((y = xmlnode_get_child(packet, "show"))) { | |
189 char *show = xmlnode_get_data(y); | |
190 if(!show) { | |
191 state = 0; | |
192 } else if(!strcasecmp(show, "away")) { | |
193 state = JABBER_STATE_AWAY; | |
194 } else if(!strcasecmp(show, "chat")) { | |
195 state = JABBER_STATE_CHAT; | |
196 } else if(!strcasecmp(show, "xa")) { | |
197 state = JABBER_STATE_XA; | |
198 } else if(!strcasecmp(show, "dnd")) { | |
199 state = JABBER_STATE_DND; | |
200 } | |
201 g_free(show); | |
202 } else { | |
203 state = 0; | |
204 } | |
205 } | |
206 | |
207 for(y = packet->child; y; y = y->next) { | |
208 if(y->type != NODE_TYPE_TAG) | |
209 continue; | |
210 | |
211 if(!strcmp(y->name, "status")) { | |
212 status = xmlnode_get_data(y); | |
213 } else if(!strcmp(y->name, "priority")) { | |
214 char *p = xmlnode_get_data(y); | |
215 if(p) { | |
216 priority = atoi(p); | |
217 g_free(p); | |
218 } | |
219 } else if(!strcmp(y->name, "x")) { | |
220 const char *xmlns = xmlnode_get_attrib(y, "xmlns"); | |
221 if(xmlns && !strcmp(xmlns, "http://jabber.org/protocol/muc#user")) { | |
222 /* this is where we'd normally get the "op" status of the | |
223 * user, but since we don't have a good way to show that yet | |
224 * we'll ignore it */ | |
225 muc = TRUE; | |
226 } | |
227 } | |
228 } | |
229 | |
230 jid = jabber_id_new(from); | |
231 | |
232 if((chat = jabber_chat_find(js, jid->node, jid->domain))) { | |
233 static int i = 0; | |
234 char *room_jid = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
235 | |
236 if(state == JABBER_STATE_ERROR) { | |
237 const char *code = NULL; | |
238 char *text = NULL; | |
239 char *buf; | |
240 xmlnode *error = xmlnode_get_child(packet, "error"); | |
241 if(error) { | |
242 /* I should make my own messages so they can be | |
243 * translated, but i'm tired */ | |
244 code = xmlnode_get_attrib(error, "code"); | |
245 text = xmlnode_get_data(error); | |
246 } | |
247 | |
248 if(!code) | |
249 code = ""; | |
250 if(!text) | |
251 text = g_strdup(_("Unable to join chat")); | |
252 | |
253 buf = g_strdup_printf("Error %s joining chat %s: %s", | |
254 code, from, text); | |
255 gaim_notify_error(js->gc, _("Error"), _("Error"), buf); | |
256 g_free(text); | |
257 g_free(buf); | |
258 | |
259 jabber_chat_destroy(chat); | |
260 return; | |
261 } | |
262 | |
263 | |
264 if(!chat->conv) { | |
265 chat->id = i++; | |
266 chat->muc = muc; | |
267 chat->conv = serv_got_joined_chat(js->gc, chat->id, room_jid); | |
268 } | |
269 | |
270 if(type && !strcasecmp(type, "unavailable")) { | |
271 if(!strcmp(jid->resource, chat->nick)) { | |
272 serv_got_chat_left(js->gc, chat->id); | |
273 jabber_chat_destroy(chat); | |
274 } else { | |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
275 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
7014 | 276 NULL); |
277 } | |
278 } else { | |
279 if(!jabber_chat_find_buddy(chat->conv, jid->resource)) | |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
280 gaim_conv_chat_add_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
7014 | 281 NULL); |
282 } | |
283 g_free(room_jid); | |
284 } else { | |
7124 | 285 if(state != JABBER_STATE_ERROR && !(jb->subscription & JABBER_SUB_TO)) { |
7014 | 286 gaim_debug(GAIM_DEBUG_INFO, "jabber", |
287 "got unexpected presence from %s, ignoring\n", from); | |
288 jabber_id_free(jid); | |
289 return; | |
290 } | |
291 | |
292 buddy_name = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
293 if((b = gaim_find_buddy(js->gc->account, buddy_name)) == NULL) { | |
294 jabber_id_free(jid); | |
295 g_free(buddy_name); | |
296 return; | |
297 } | |
298 | |
299 if(state == JABBER_STATE_ERROR || | |
300 (type && !strcasecmp(type, "unavailable"))) | |
301 jabber_buddy_remove_resource(jb, jid->resource); | |
302 else | |
303 jabber_buddy_track_resource(jb, jid->resource, priority, state, | |
304 status); | |
305 | |
306 jbr = jabber_buddy_find_resource(jb, jid->resource); | |
307 | |
308 if(jbr) | |
309 serv_got_update(js->gc, buddy_name, 1, 0, b->signon, b->idle, | |
310 jbr->state); | |
311 else | |
312 serv_got_update(js->gc, buddy_name, 0, 0, 0, 0, 0); | |
313 #if 0 | |
314 iq = jabber_iq_new_query(js, JABBER_IQ_GET, | |
315 "http://jabber.org/protocol/disco#items"); | |
316 query = xmlnode_get_child(iq->node, "query"); | |
317 xmlnode_set_attrib(query, "node", | |
318 "http://jabber.org/protocol/avatar"); | |
319 xmlnode_set_attrib(iq->node, "to", buddy_name); | |
320 jabber_iq_send(iq); | |
321 #endif | |
322 | |
323 g_free(buddy_name); | |
324 } | |
325 g_free(status); | |
326 jabber_id_free(jid); | |
327 } | |
328 | |
329 void jabber_presence_subscription_set(JabberStream *js, const char *who, const char *type) | |
330 { | |
331 xmlnode *presence = xmlnode_new("presence"); | |
332 | |
333 xmlnode_set_attrib(presence, "to", who); | |
334 xmlnode_set_attrib(presence, "type", type); | |
335 | |
336 jabber_send(js, presence); | |
337 xmlnode_free(presence); | |
338 } |