Mercurial > pidgin.yaz
annotate src/protocols/jabber/presence.c @ 7813:8bba34c04c9d
[gaim-migrate @ 8460]
oh yeah, I wasn't done committing stuff
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Mon, 08 Dec 2003 23:59:03 +0000 |
parents | 4168b8881746 |
children | 4ed0c93816f6 |
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); |
7249 | 58 } else if(!state || strcmp(state, GAIM_AWAY_CUSTOM)) { |
7247 | 59 /* i can't wait until someone re-writes the status/away stuff */ |
7014 | 60 stripped = g_strdup(""); |
61 } | |
62 | |
7248 | 63 if(gc->away) |
64 g_free(gc->away); | |
7014 | 65 gc->away = stripped; |
66 | |
67 presence = jabber_presence_create(state, msg); | |
68 jabber_send(js, presence); | |
69 g_hash_table_foreach(js->chats, chats_send_presence_foreach, presence); | |
70 xmlnode_free(presence); | |
71 } | |
72 | |
73 xmlnode *jabber_presence_create(const char *state, const char *msg) | |
74 { | |
75 xmlnode *show, *status, *presence; | |
76 | |
77 | |
78 presence = xmlnode_new("presence"); | |
79 | |
80 if(state) { | |
81 const char *show_string = NULL; | |
82 if(!strcmp(state, _("Chatty"))) | |
83 show_string = "chat"; | |
84 else if(!strcmp(state, _("Away")) || | |
85 (msg && !strcmp(state, GAIM_AWAY_CUSTOM))) | |
86 show_string = "away"; | |
87 else if(!strcmp(state, _("Extended Away"))) | |
88 show_string = "xa"; | |
89 else if(!strcmp(state, _("Do Not Disturb"))) | |
90 show_string = "dnd"; | |
91 else if(!strcmp(state, _("Invisible"))) { | |
92 xmlnode_set_attrib(presence, "type", "invisible"); | |
93 } | |
94 | |
95 if(show_string) { | |
96 show = xmlnode_new_child(presence, "show"); | |
97 xmlnode_insert_data(show, show_string, -1); | |
98 } | |
99 } | |
100 | |
101 if(msg && *msg) { | |
102 status = xmlnode_new_child(presence, "status"); | |
103 xmlnode_insert_data(status, msg, -1); | |
104 } | |
105 | |
106 return presence; | |
107 } | |
108 | |
109 struct _jabber_add_permit { | |
110 GaimConnection *gc; | |
111 char *who; | |
112 }; | |
113 | |
114 static void authorize_add_cb(struct _jabber_add_permit *jap) | |
115 { | |
116 if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
117 jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
118 "subscribed"); | |
119 | |
120 if(!gaim_find_buddy(jap->gc->account, jap->who)) | |
7015
dece74f05509
[gaim-migrate @ 7578]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
121 gaim_account_notify_added(jap->gc->account, NULL, jap->who, NULL, NULL); |
7014 | 122 } |
123 | |
124 g_free(jap->who); | |
125 g_free(jap); | |
126 } | |
127 | |
128 static void deny_add_cb(struct _jabber_add_permit *jap) | |
129 { | |
130 if(g_list_find(gaim_connections_get_all(), jap->gc)) { | |
131 jabber_presence_subscription_set(jap->gc->proto_data, jap->who, | |
132 "unsubscribed"); | |
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; | |
7395 | 148 JabberBuddyResource *jbr = FALSE; |
7014 | 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 | |
7280 | 157 if(!jb) |
158 return; | |
159 | |
7014 | 160 if(jb->error_msg) { |
161 g_free(jb->error_msg); | |
162 jb->error_msg = NULL; | |
163 } | |
164 | |
7813 | 165 if(type && !strcmp(type, "error")) { |
7644 | 166 const char *code = NULL; |
167 char *err_txt = NULL; | |
168 | |
7014 | 169 state = JABBER_STATE_ERROR; |
170 if((y = xmlnode_get_child(packet, "error")) != NULL) { | |
7184 | 171 /* XXX: need to handle new XMPP-style errors */ |
7644 | 172 code = xmlnode_get_attrib(y, "code"); |
173 err_txt = xmlnode_get_data(y); | |
7014 | 174 } |
7644 | 175 jb->error_msg = g_strdup_printf("%s%s%s", code ? code : "", |
176 code ? ": " : "", err_txt ? err_txt : | |
177 _("Unknown Error in presence")); | |
178 if(err_txt) | |
179 g_free(err_txt); | |
7813 | 180 } else if(type && !strcmp(type, "subscribe")) { |
7014 | 181 struct _jabber_add_permit *jap = g_new0(struct _jabber_add_permit, 1); |
182 char *msg = g_strdup_printf(_("The user %s wants to add you to their buddy list."), from); | |
183 jap->gc = js->gc; | |
184 jap->who = g_strdup(from); | |
185 | |
186 gaim_request_action(js->gc, NULL, msg, NULL, 0, jap, 2, | |
187 _("Authorize"), G_CALLBACK(authorize_add_cb), | |
188 _("Deny"), G_CALLBACK(deny_add_cb)); | |
189 g_free(msg); | |
7145 | 190 return; |
7813 | 191 } else if(type && !strcmp(type, "subscribed")) { |
7014 | 192 /* we've been allowed to see their presence, but we don't care */ |
193 return; | |
194 } else { | |
195 if((y = xmlnode_get_child(packet, "show"))) { | |
196 char *show = xmlnode_get_data(y); | |
197 if(!show) { | |
198 state = 0; | |
199 } else if(!strcasecmp(show, "away")) { | |
200 state = JABBER_STATE_AWAY; | |
201 } else if(!strcasecmp(show, "chat")) { | |
202 state = JABBER_STATE_CHAT; | |
203 } else if(!strcasecmp(show, "xa")) { | |
204 state = JABBER_STATE_XA; | |
205 } else if(!strcasecmp(show, "dnd")) { | |
206 state = JABBER_STATE_DND; | |
207 } | |
208 g_free(show); | |
209 } else { | |
210 state = 0; | |
211 } | |
212 } | |
213 | |
7310 | 214 if(!(jid = jabber_id_new(from))) |
215 return; | |
216 | |
7014 | 217 for(y = packet->child; y; y = y->next) { |
218 if(y->type != NODE_TYPE_TAG) | |
219 continue; | |
220 | |
221 if(!strcmp(y->name, "status")) { | |
7615 | 222 g_free(status); |
7014 | 223 status = xmlnode_get_data(y); |
224 } else if(!strcmp(y->name, "priority")) { | |
225 char *p = xmlnode_get_data(y); | |
226 if(p) { | |
227 priority = atoi(p); | |
228 g_free(p); | |
229 } | |
230 } else if(!strcmp(y->name, "x")) { | |
231 const char *xmlns = xmlnode_get_attrib(y, "xmlns"); | |
232 if(xmlns && !strcmp(xmlns, "http://jabber.org/protocol/muc#user")) { | |
233 /* this is where we'd normally get the "op" status of the | |
234 * user, but since we don't have a good way to show that yet | |
235 * we'll ignore it */ | |
7629 | 236 xmlnode *z; |
237 | |
7014 | 238 muc = TRUE; |
7629 | 239 if((z = xmlnode_get_child(y, "status"))) { |
240 const char *code = xmlnode_get_attrib(z, "code"); | |
241 if(code && !strcmp(code, "201")) { | |
242 /* we created the room. for now, we'll make it | |
243 * an instant room. XXX: allow fancy room creation */ | |
244 JabberIq *iq = jabber_iq_new_query(js, JABBER_IQ_SET, | |
245 "http://jabber.org/protocol/muc#owner"); | |
246 xmlnode *query = xmlnode_get_child(iq->node, "query"); | |
7630 | 247 xmlnode *x = xmlnode_new_child(query, "x"); |
248 char *room_jid = g_strdup_printf("%s@%s", jid->node, | |
249 jid->domain); | |
7629 | 250 |
7630 | 251 xmlnode_set_attrib(iq->node, "to", room_jid); |
7629 | 252 xmlnode_set_attrib(x, "xmlns", "jabber:x:data"); |
253 xmlnode_set_attrib(x, "type", "submit"); | |
254 | |
255 jabber_iq_send(iq); | |
7630 | 256 |
257 g_free(room_jid); | |
7629 | 258 } |
259 } | |
7014 | 260 } |
261 } | |
262 } | |
263 | |
264 | |
7322 | 265 if(jid->node && (chat = jabber_chat_find(js, jid->node, jid->domain))) { |
7014 | 266 static int i = 0; |
267 char *room_jid = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
268 | |
269 if(state == JABBER_STATE_ERROR) { | |
270 const char *code = NULL; | |
271 char *text = NULL; | |
272 char *buf; | |
273 xmlnode *error = xmlnode_get_child(packet, "error"); | |
274 if(error) { | |
275 /* I should make my own messages so they can be | |
276 * translated, but i'm tired */ | |
277 code = xmlnode_get_attrib(error, "code"); | |
278 text = xmlnode_get_data(error); | |
279 } | |
280 | |
281 if(!code) | |
282 code = ""; | |
283 | |
7321 | 284 if(chat->conv) { |
285 if(!text) | |
286 text = g_strdup(_("Unknown error")); | |
287 buf = g_strdup_printf("Error %s in chat %s: %s", | |
288 code, from, text); | |
289 serv_got_chat_left(js->gc, chat->id); | |
290 } else { | |
291 if(!text) | |
292 text = g_strdup(_("Unable to join chat")); | |
293 buf = g_strdup_printf("Error %s joining chat %s: %s", | |
294 code, from, text); | |
295 } | |
7014 | 296 gaim_notify_error(js->gc, _("Error"), _("Error"), buf); |
297 g_free(text); | |
298 g_free(buf); | |
299 | |
300 jabber_chat_destroy(chat); | |
7310 | 301 jabber_id_free(jid); |
7615 | 302 g_free(status); |
7014 | 303 return; |
304 } | |
305 | |
306 | |
307 if(!chat->conv) { | |
308 chat->id = i++; | |
309 chat->muc = muc; | |
310 chat->conv = serv_got_joined_chat(js->gc, chat->id, room_jid); | |
311 } | |
312 | |
7813 | 313 if(type && !strcmp(type, "unavailable")) { |
7014 | 314 if(!strcmp(jid->resource, chat->nick)) { |
315 serv_got_chat_left(js->gc, chat->id); | |
316 jabber_chat_destroy(chat); | |
317 } else { | |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
318 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
7014 | 319 NULL); |
320 } | |
321 } else { | |
322 if(!jabber_chat_find_buddy(chat->conv, jid->resource)) | |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
323 gaim_conv_chat_add_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
7014 | 324 NULL); |
325 } | |
326 g_free(room_jid); | |
327 } else { | |
7124 | 328 if(state != JABBER_STATE_ERROR && !(jb->subscription & JABBER_SUB_TO)) { |
7014 | 329 gaim_debug(GAIM_DEBUG_INFO, "jabber", |
330 "got unexpected presence from %s, ignoring\n", from); | |
331 jabber_id_free(jid); | |
7615 | 332 g_free(status); |
7014 | 333 return; |
334 } | |
335 | |
7322 | 336 buddy_name = g_strdup_printf("%s%s%s", jid->node ? jid->node : "", |
337 jid->node ? "@" : "", jid->domain); | |
7014 | 338 if((b = gaim_find_buddy(js->gc->account, buddy_name)) == NULL) { |
339 jabber_id_free(jid); | |
340 g_free(buddy_name); | |
7615 | 341 g_free(status); |
7014 | 342 return; |
343 } | |
344 | |
345 if(state == JABBER_STATE_ERROR || | |
7813 | 346 (type && (!strcmp(type, "unavailable") || |
347 !strcmp(type, "unsubscribed")))) { | |
7014 | 348 jabber_buddy_remove_resource(jb, jid->resource); |
7395 | 349 } else { |
7014 | 350 jabber_buddy_track_resource(jb, jid->resource, priority, state, |
351 status); | |
7395 | 352 } |
7014 | 353 |
7395 | 354 if(!jbr) |
355 jbr = jabber_buddy_find_resource(jb, jid->resource); | |
7014 | 356 |
357 if(jbr) | |
358 serv_got_update(js->gc, buddy_name, 1, 0, b->signon, b->idle, | |
359 jbr->state); | |
360 else | |
361 serv_got_update(js->gc, buddy_name, 0, 0, 0, 0, 0); | |
362 | |
363 g_free(buddy_name); | |
364 } | |
365 g_free(status); | |
366 jabber_id_free(jid); | |
367 } | |
368 | |
369 void jabber_presence_subscription_set(JabberStream *js, const char *who, const char *type) | |
370 { | |
371 xmlnode *presence = xmlnode_new("presence"); | |
372 | |
373 xmlnode_set_attrib(presence, "to", who); | |
374 xmlnode_set_attrib(presence, "type", type); | |
375 | |
376 jabber_send(js, presence); | |
377 xmlnode_free(presence); | |
378 } |