Mercurial > pidgin.yaz
annotate src/protocols/jabber/presence.c @ 7895:ddd80ffd3815
[gaim-migrate @ 8555]
wanted to get this committed before I went home
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Fri, 19 Dec 2003 08:33:03 +0000 |
parents | 4ed0c93816f6 |
children | e87e7d9d0132 |
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; | |
7835 | 148 JabberBuddyResource *jbr = NULL; |
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")) { | |
7895 | 242 chat = jabber_chat_find(js, jid->node, jid->domain); |
243 /* XXX: finish this | |
244 gaim_request_action(js->gc, _("Create New Room"), | |
245 _("Create New Room"), | |
246 _("You are creating a new room. Would you like to " | |
247 "configure it, or accept the default settings?"), | |
248 1, chat, 2, _("Configure Room"), | |
249 G_CALLBACK(jabber_chat_start_room_configure), | |
250 _("Accept Defaults"), | |
251 G_CALLBACK(jabber_chat_create_instant_room)); | |
252 */ | |
253 jabber_chat_create_instant_room(chat); | |
7629 | 254 } |
255 } | |
7014 | 256 } |
257 } | |
258 } | |
259 | |
260 | |
7322 | 261 if(jid->node && (chat = jabber_chat_find(js, jid->node, jid->domain))) { |
7014 | 262 static int i = 0; |
263 char *room_jid = g_strdup_printf("%s@%s", jid->node, jid->domain); | |
264 | |
265 if(state == JABBER_STATE_ERROR) { | |
266 const char *code = NULL; | |
267 char *text = NULL; | |
268 char *buf; | |
269 xmlnode *error = xmlnode_get_child(packet, "error"); | |
270 if(error) { | |
271 /* I should make my own messages so they can be | |
272 * translated, but i'm tired */ | |
273 code = xmlnode_get_attrib(error, "code"); | |
274 text = xmlnode_get_data(error); | |
275 } | |
276 | |
277 if(!code) | |
278 code = ""; | |
279 | |
7321 | 280 if(chat->conv) { |
281 if(!text) | |
282 text = g_strdup(_("Unknown error")); | |
283 buf = g_strdup_printf("Error %s in chat %s: %s", | |
284 code, from, text); | |
285 serv_got_chat_left(js->gc, chat->id); | |
286 } else { | |
287 if(!text) | |
288 text = g_strdup(_("Unable to join chat")); | |
289 buf = g_strdup_printf("Error %s joining chat %s: %s", | |
290 code, from, text); | |
291 } | |
7014 | 292 gaim_notify_error(js->gc, _("Error"), _("Error"), buf); |
293 g_free(text); | |
294 g_free(buf); | |
295 | |
296 jabber_chat_destroy(chat); | |
7310 | 297 jabber_id_free(jid); |
7615 | 298 g_free(status); |
7014 | 299 return; |
300 } | |
301 | |
302 | |
303 if(!chat->conv) { | |
304 chat->id = i++; | |
305 chat->muc = muc; | |
306 chat->conv = serv_got_joined_chat(js->gc, chat->id, room_jid); | |
307 } | |
308 | |
7813 | 309 if(type && !strcmp(type, "unavailable")) { |
7014 | 310 if(!strcmp(jid->resource, chat->nick)) { |
311 serv_got_chat_left(js->gc, chat->id); | |
312 jabber_chat_destroy(chat); | |
313 } else { | |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
314 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
7014 | 315 NULL); |
316 } | |
317 } else { | |
318 if(!jabber_chat_find_buddy(chat->conv, jid->resource)) | |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7095
diff
changeset
|
319 gaim_conv_chat_add_user(GAIM_CONV_CHAT(chat->conv), jid->resource, |
7014 | 320 NULL); |
321 } | |
322 g_free(room_jid); | |
323 } else { | |
7124 | 324 if(state != JABBER_STATE_ERROR && !(jb->subscription & JABBER_SUB_TO)) { |
7014 | 325 gaim_debug(GAIM_DEBUG_INFO, "jabber", |
326 "got unexpected presence from %s, ignoring\n", from); | |
327 jabber_id_free(jid); | |
7615 | 328 g_free(status); |
7014 | 329 return; |
330 } | |
331 | |
7322 | 332 buddy_name = g_strdup_printf("%s%s%s", jid->node ? jid->node : "", |
333 jid->node ? "@" : "", jid->domain); | |
7014 | 334 if((b = gaim_find_buddy(js->gc->account, buddy_name)) == NULL) { |
335 jabber_id_free(jid); | |
336 g_free(buddy_name); | |
7615 | 337 g_free(status); |
7014 | 338 return; |
339 } | |
340 | |
341 if(state == JABBER_STATE_ERROR || | |
7813 | 342 (type && (!strcmp(type, "unavailable") || |
343 !strcmp(type, "unsubscribed")))) { | |
7014 | 344 jabber_buddy_remove_resource(jb, jid->resource); |
7395 | 345 } else { |
7014 | 346 jabber_buddy_track_resource(jb, jid->resource, priority, state, |
347 status); | |
7395 | 348 } |
7014 | 349 |
7835 | 350 if((jbr = jabber_buddy_find_resource(jb, NULL))) |
7014 | 351 serv_got_update(js->gc, buddy_name, 1, 0, b->signon, b->idle, |
352 jbr->state); | |
353 else | |
354 serv_got_update(js->gc, buddy_name, 0, 0, 0, 0, 0); | |
355 | |
356 g_free(buddy_name); | |
357 } | |
358 g_free(status); | |
359 jabber_id_free(jid); | |
360 } | |
361 | |
362 void jabber_presence_subscription_set(JabberStream *js, const char *who, const char *type) | |
363 { | |
364 xmlnode *presence = xmlnode_new("presence"); | |
365 | |
366 xmlnode_set_attrib(presence, "to", who); | |
367 xmlnode_set_attrib(presence, "type", type); | |
368 | |
369 jabber_send(js, presence); | |
370 xmlnode_free(presence); | |
371 } |