Mercurial > pidgin.yaz
annotate src/protocols/jabber/buddy.c @ 10490:a3a540ed2518
[gaim-migrate @ 11780]
some random jabber stuff, remove a duplicate #define, and shuffle account
loading so we're not emitting signals that haven't been registered yet
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Sat, 08 Jan 2005 21:10:17 +0000 |
parents | 937697da5233 |
children | 1a97d5e88d12 |
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 #include "debug.h" | |
7076 | 23 #include "imgstore.h" |
9713 | 24 #include "prpl.h" |
7014 | 25 #include "notify.h" |
26 #include "request.h" | |
27 #include "util.h" | |
7395 | 28 #include "xmlnode.h" |
7014 | 29 |
30 #include "buddy.h" | |
31 #include "chat.h" | |
32 #include "jabber.h" | |
33 #include "iq.h" | |
34 #include "presence.h" | |
10189 | 35 #include "sha.h" |
7014 | 36 |
37 | |
7116 | 38 void jabber_buddy_free(JabberBuddy *jb) |
39 { | |
40 g_return_if_fail(jb != NULL); | |
41 | |
42 if(jb->error_msg) | |
43 g_free(jb->error_msg); | |
44 while(jb->resources) | |
45 jabber_buddy_resource_free(jb->resources->data); | |
46 | |
47 g_free(jb); | |
48 } | |
49 | |
7014 | 50 JabberBuddy *jabber_buddy_find(JabberStream *js, const char *name, |
51 gboolean create) | |
52 { | |
53 JabberBuddy *jb; | |
7445 | 54 const char *realname; |
7014 | 55 |
7445 | 56 if(!(realname = jabber_normalize(js->gc->account, name))) |
7014 | 57 return NULL; |
58 | |
59 jb = g_hash_table_lookup(js->buddies, realname); | |
60 | |
61 if(!jb && create) { | |
62 jb = g_new0(JabberBuddy, 1); | |
63 g_hash_table_insert(js->buddies, g_strdup(realname), jb); | |
64 } | |
65 | |
66 return jb; | |
67 } | |
68 | |
69 | |
70 JabberBuddyResource *jabber_buddy_find_resource(JabberBuddy *jb, | |
71 const char *resource) | |
72 { | |
73 JabberBuddyResource *jbr = NULL; | |
74 GList *l; | |
75 | |
76 if(!jb) | |
77 return NULL; | |
78 | |
79 for(l = jb->resources; l; l = l->next) | |
80 { | |
81 if(!jbr && !resource) { | |
82 jbr = l->data; | |
83 } else if(!resource) { | |
84 if(((JabberBuddyResource *)l->data)->priority >= jbr->priority) | |
85 jbr = l->data; | |
86 } else if(((JabberBuddyResource *)l->data)->name) { | |
87 if(!strcmp(((JabberBuddyResource *)l->data)->name, resource)) { | |
88 jbr = l->data; | |
89 break; | |
90 } | |
91 } | |
92 } | |
93 | |
94 return jbr; | |
95 } | |
96 | |
9954 | 97 JabberBuddyResource *jabber_buddy_track_resource(JabberBuddy *jb, const char *resource, |
98 int priority, JabberBuddyState state, const char *status) | |
7014 | 99 { |
100 JabberBuddyResource *jbr = jabber_buddy_find_resource(jb, resource); | |
101 | |
102 if(!jbr) { | |
103 jbr = g_new0(JabberBuddyResource, 1); | |
7116 | 104 jbr->jb = jb; |
7014 | 105 jbr->name = g_strdup(resource); |
106 jbr->capabilities = JABBER_CAP_XHTML; | |
107 jb->resources = g_list_append(jb->resources, jbr); | |
108 } | |
109 jbr->priority = priority; | |
110 jbr->state = state; | |
111 if(jbr->status) | |
112 g_free(jbr->status); | |
113 jbr->status = g_strdup(status); | |
9954 | 114 |
115 return jbr; | |
7014 | 116 } |
117 | |
7116 | 118 void jabber_buddy_resource_free(JabberBuddyResource *jbr) |
119 { | |
120 g_return_if_fail(jbr != NULL); | |
121 | |
122 jbr->jb->resources = g_list_remove(jbr->jb->resources, jbr); | |
123 | |
124 g_free(jbr->name); | |
125 if(jbr->status) | |
126 g_free(jbr->status); | |
8400 | 127 if(jbr->thread_id) |
128 g_free(jbr->thread_id); | |
7116 | 129 g_free(jbr); |
130 } | |
131 | |
7014 | 132 void jabber_buddy_remove_resource(JabberBuddy *jb, const char *resource) |
133 { | |
134 JabberBuddyResource *jbr = jabber_buddy_find_resource(jb, resource); | |
135 | |
136 if(!jbr) | |
137 return; | |
138 | |
7116 | 139 jabber_buddy_resource_free(jbr); |
7014 | 140 } |
141 | |
142 const char *jabber_buddy_get_status_msg(JabberBuddy *jb) | |
143 { | |
144 JabberBuddyResource *jbr; | |
145 | |
146 if(!jb) | |
147 return NULL; | |
148 | |
149 jbr = jabber_buddy_find_resource(jb, NULL); | |
150 | |
151 if(!jbr) | |
152 return NULL; | |
153 | |
154 return jbr->status; | |
155 } | |
156 | |
157 /******* | |
158 * This is the old vCard stuff taken from the old prpl. vCards, by definition | |
159 * are a temporary thing until jabber can get its act together and come up | |
160 * with a format for user information, hence the namespace of 'vcard-temp' | |
161 * | |
162 * Since I don't feel like putting that much work into something that's | |
163 * _supposed_ to go away, i'm going to just copy the kludgy old code here, | |
164 * and make it purdy when jabber comes up with a standards-track JEP to | |
165 * replace vcard-temp | |
166 * --Nathan | |
167 *******/ | |
168 | |
169 /*---------------------------------------*/ | |
170 /* Jabber "set info" (vCard) support */ | |
171 /*---------------------------------------*/ | |
172 | |
173 /* | |
174 * V-Card format: | |
175 * | |
176 * <vCard prodid='' version='' xmlns=''> | |
177 * <FN></FN> | |
178 * <N> | |
179 * <FAMILY/> | |
180 * <GIVEN/> | |
181 * </N> | |
182 * <NICKNAME/> | |
183 * <URL/> | |
184 * <ADR> | |
185 * <STREET/> | |
186 * <EXTADD/> | |
187 * <LOCALITY/> | |
188 * <REGION/> | |
189 * <PCODE/> | |
190 * <COUNTRY/> | |
191 * </ADR> | |
192 * <TEL/> | |
193 * <EMAIL/> | |
194 * <ORG> | |
195 * <ORGNAME/> | |
196 * <ORGUNIT/> | |
197 * </ORG> | |
198 * <TITLE/> | |
199 * <ROLE/> | |
200 * <DESC/> | |
201 * <BDAY/> | |
202 * </vCard> | |
203 * | |
204 * See also: | |
205 * | |
206 * http://docs.jabber.org/proto/html/vcard-temp.html | |
207 * http://www.vcard-xml.org/dtd/vCard-XML-v2-20010520.dtd | |
208 */ | |
209 | |
210 /* | |
211 * Cross-reference user-friendly V-Card entry labels to vCard XML tags | |
212 * and attributes. | |
213 * | |
214 * Order is (or should be) unimportant. For example: we have no way of | |
215 * knowing in what order real data will arrive. | |
216 * | |
217 * Format: Label, Pre-set text, "visible" flag, "editable" flag, XML tag | |
218 * name, XML tag's parent tag "path" (relative to vCard node). | |
219 * | |
220 * List is terminated by a NULL label pointer. | |
221 * | |
222 * Entries with no label text, but with XML tag and parent tag | |
223 * entries, are used by V-Card XML construction routines to | |
224 * "automagically" construct the appropriate XML node tree. | |
225 * | |
226 * Thoughts on future direction/expansion | |
227 * | |
228 * This is a "simple" vCard. | |
229 * | |
230 * It is possible for nodes other than the "vCard" node to have | |
231 * attributes. Should that prove necessary/desirable, add an | |
232 * "attributes" pointer to the vcard_template struct, create the | |
233 * necessary tag_attr structs, and add 'em to the vcard_dflt_data | |
234 * array. | |
235 * | |
236 * The above changes will (obviously) require changes to the vCard | |
237 * construction routines. | |
238 */ | |
239 | |
240 struct vcard_template { | |
241 char *label; /* label text pointer */ | |
242 char *text; /* entry text pointer */ | |
243 int visible; /* should entry field be "visible?" */ | |
244 int editable; /* should entry field be editable? */ | |
245 char *tag; /* tag text */ | |
246 char *ptag; /* parent tag "path" text */ | |
247 char *url; /* vCard display format if URL */ | |
248 } vcard_template_data[] = { | |
249 {N_("Full Name"), NULL, TRUE, TRUE, "FN", NULL, NULL}, | |
250 {N_("Family Name"), NULL, TRUE, TRUE, "FAMILY", "N", NULL}, | |
251 {N_("Given Name"), NULL, TRUE, TRUE, "GIVEN", "N", NULL}, | |
252 {N_("Nickname"), NULL, TRUE, TRUE, "NICKNAME", NULL, NULL}, | |
253 {N_("URL"), NULL, TRUE, TRUE, "URL", NULL, "<A HREF=\"%s\">%s</A>"}, | |
254 {N_("Street Address"), NULL, TRUE, TRUE, "STREET", "ADR", NULL}, | |
255 {N_("Extended Address"), NULL, TRUE, TRUE, "EXTADD", "ADR", NULL}, | |
256 {N_("Locality"), NULL, TRUE, TRUE, "LOCALITY", "ADR", NULL}, | |
257 {N_("Region"), NULL, TRUE, TRUE, "REGION", "ADR", NULL}, | |
258 {N_("Postal Code"), NULL, TRUE, TRUE, "PCODE", "ADR", NULL}, | |
259 {N_("Country"), NULL, TRUE, TRUE, "COUNTRY", "ADR", NULL}, | |
10215 | 260 {N_("Telephone"), NULL, TRUE, TRUE, "TEL", NULL, NULL}, |
7014 | 261 {N_("Email"), NULL, TRUE, TRUE, "EMAIL", NULL, "<A HREF=\"mailto:%s\">%s</A>"}, |
262 {N_("Organization Name"), NULL, TRUE, TRUE, "ORGNAME", "ORG", NULL}, | |
263 {N_("Organization Unit"), NULL, TRUE, TRUE, "ORGUNIT", "ORG", NULL}, | |
264 {N_("Title"), NULL, TRUE, TRUE, "TITLE", NULL, NULL}, | |
265 {N_("Role"), NULL, TRUE, TRUE, "ROLE", NULL, NULL}, | |
266 {N_("Birthday"), NULL, TRUE, TRUE, "BDAY", NULL, NULL}, | |
267 {N_("Description"), NULL, TRUE, TRUE, "DESC", NULL, NULL}, | |
268 {"", NULL, TRUE, TRUE, "N", NULL, NULL}, | |
269 {"", NULL, TRUE, TRUE, "ADR", NULL, NULL}, | |
270 {"", NULL, TRUE, TRUE, "ORG", NULL, NULL}, | |
271 {NULL, NULL, 0, 0, NULL, NULL, NULL} | |
272 }; | |
273 | |
274 /* | |
8735
92cbf9713795
[gaim-migrate @ 9490]
Christian Hammond <chipx86@chipx86.com>
parents:
8401
diff
changeset
|
275 * The "vCard" tag's attribute list... |
7014 | 276 */ |
277 struct tag_attr { | |
278 char *attr; | |
279 char *value; | |
280 } vcard_tag_attr_list[] = { | |
281 {"prodid", "-//HandGen//NONSGML vGen v1.0//EN"}, | |
282 {"version", "2.0", }, | |
283 {"xmlns", "vcard-temp", }, | |
284 {NULL, NULL}, | |
285 }; | |
286 | |
287 | |
288 /* | |
289 * Insert a tag node into an xmlnode tree, recursively inserting parent tag | |
290 * nodes as necessary | |
291 * | |
292 * Returns pointer to inserted node | |
293 * | |
294 * Note to hackers: this code is designed to be re-entrant (it's recursive--it | |
295 * calls itself), so don't put any "static"s in here! | |
296 */ | |
297 static xmlnode *insert_tag_to_parent_tag(xmlnode *start, const char *parent_tag, const char *new_tag) | |
298 { | |
299 xmlnode *x = NULL; | |
300 | |
301 /* | |
302 * If the parent tag wasn't specified, see if we can get it | |
303 * from the vCard template struct. | |
304 */ | |
305 if(parent_tag == NULL) { | |
306 struct vcard_template *vc_tp = vcard_template_data; | |
307 | |
308 while(vc_tp->label != NULL) { | |
309 if(strcmp(vc_tp->tag, new_tag) == 0) { | |
310 parent_tag = vc_tp->ptag; | |
311 break; | |
312 } | |
313 ++vc_tp; | |
314 } | |
315 } | |
316 | |
317 /* | |
318 * If we have a parent tag... | |
319 */ | |
320 if(parent_tag != NULL ) { | |
321 /* | |
322 * Try to get the parent node for a tag | |
323 */ | |
324 if((x = xmlnode_get_child(start, parent_tag)) == NULL) { | |
325 /* | |
326 * Descend? | |
327 */ | |
328 char *grand_parent = g_strdup(parent_tag); | |
329 char *parent; | |
330 | |
331 if((parent = strrchr(grand_parent, '/')) != NULL) { | |
332 *(parent++) = '\0'; | |
333 x = insert_tag_to_parent_tag(start, grand_parent, parent); | |
334 } else { | |
335 x = xmlnode_new_child(start, grand_parent); | |
336 } | |
337 g_free(grand_parent); | |
338 } else { | |
339 /* | |
340 * We found *something* to be the parent node. | |
341 * Note: may be the "root" node! | |
342 */ | |
343 xmlnode *y; | |
344 if((y = xmlnode_get_child(x, new_tag)) != NULL) { | |
345 return(y); | |
346 } | |
347 } | |
348 } | |
349 | |
350 /* | |
351 * insert the new tag into its parent node | |
352 */ | |
353 return(xmlnode_new_child((x == NULL? start : x), new_tag)); | |
354 } | |
355 | |
356 /* | |
357 * Send vCard info to Jabber server | |
358 */ | |
359 void jabber_set_info(GaimConnection *gc, const char *info) | |
360 { | |
361 JabberIq *iq; | |
362 JabberStream *js = gc->proto_data; | |
363 xmlnode *vc_node; | |
10189 | 364 const char *avatar_file = NULL; |
7014 | 365 |
10189 | 366 if(js->avatar_hash) |
367 g_free(js->avatar_hash); | |
368 js->avatar_hash = NULL; | |
7014 | 369 |
370 /* | |
371 * Send only if there's actually any *information* to send | |
372 */ | |
373 vc_node = xmlnode_from_str(info, -1); | |
10189 | 374 avatar_file = gaim_account_get_buddy_icon(gc->account); |
375 | |
376 if(!vc_node && avatar_file) { | |
377 vc_node = xmlnode_new("vCard"); | |
378 } | |
7014 | 379 |
380 if(vc_node) { | |
381 if (vc_node->name && | |
10189 | 382 !g_ascii_strncasecmp(vc_node->name, "vCard", 5)) { |
383 GError *error = NULL; | |
10441 | 384 unsigned char *avatar_data; |
10189 | 385 gsize avatar_len; |
386 | |
10442 | 387 if(avatar_file && g_file_get_contents(avatar_file, (gchar **)&avatar_data, &avatar_len, &error)) { |
10189 | 388 xmlnode *photo; |
10441 | 389 unsigned char *enc; |
10189 | 390 int i; |
391 unsigned char hashval[20]; | |
392 char *p, hash[41]; | |
393 | |
394 photo = xmlnode_new_child(vc_node, "PHOTO"); | |
395 enc = gaim_base64_encode(avatar_data, avatar_len); | |
396 shaBlock((unsigned char *)avatar_data, avatar_len, hashval); | |
397 p = hash; | |
398 for(i=0; i<20; i++, p+=2) | |
399 snprintf(p, 3, "%02x", hashval[i]); | |
400 js->avatar_hash = g_strdup(hash); | |
401 | |
402 xmlnode_insert_data(photo, enc, -1); | |
403 g_free(enc); | |
404 g_free(avatar_data); | |
405 } else { | |
406 g_error_free(error); | |
407 } | |
408 | |
7014 | 409 iq = jabber_iq_new(js, JABBER_IQ_SET); |
410 xmlnode_insert_child(iq->node, vc_node); | |
411 jabber_iq_send(iq); | |
412 } else { | |
413 xmlnode_free(vc_node); | |
414 } | |
415 } | |
416 } | |
417 | |
10189 | 418 void jabber_set_buddy_icon(GaimConnection *gc, const char *iconfile) |
419 { | |
420 GaimPresence *gpresence; | |
421 GaimStatus *status; | |
422 | |
423 jabber_set_info(gc, gaim_account_get_user_info(gc->account)); | |
424 | |
425 gpresence = gaim_account_get_presence(gc->account); | |
426 status = gaim_presence_get_active_status(gpresence); | |
10216 | 427 jabber_presence_send(gc->account, status); |
10189 | 428 } |
429 | |
7014 | 430 /* |
431 * This is the callback from the "ok clicked" for "set vCard" | |
432 * | |
433 * Formats GSList data into XML-encoded string and returns a pointer | |
434 * to said string. | |
435 * | |
436 * g_free()'ing the returned string space is the responsibility of | |
437 * the caller. | |
438 */ | |
439 static void | |
440 jabber_format_info(GaimConnection *gc, GaimRequestFields *fields) | |
441 { | |
442 GaimAccount *account; | |
443 xmlnode *vc_node; | |
444 GaimRequestField *field; | |
445 const char *text; | |
446 char *p; | |
447 const struct vcard_template *vc_tp; | |
448 struct tag_attr *tag_attr; | |
449 | |
450 vc_node = xmlnode_new("vCard"); | |
451 | |
452 for(tag_attr = vcard_tag_attr_list; tag_attr->attr != NULL; ++tag_attr) | |
453 xmlnode_set_attrib(vc_node, tag_attr->attr, tag_attr->value); | |
454 | |
455 for (vc_tp = vcard_template_data; vc_tp->label != NULL; vc_tp++) { | |
456 if (*vc_tp->label == '\0') | |
457 continue; | |
458 | |
459 field = gaim_request_fields_get_field(fields, vc_tp->tag); | |
460 text = gaim_request_field_string_get_value(field); | |
461 | |
462 | |
463 if (text != NULL && *text != '\0') { | |
464 xmlnode *xp; | |
465 | |
9339 | 466 gaim_debug(GAIM_DEBUG_INFO, "jabber", |
467 "Setting %s to '%s'\n", vc_tp->tag, text); | |
468 | |
7014 | 469 if ((xp = insert_tag_to_parent_tag(vc_node, |
470 NULL, vc_tp->tag)) != NULL) { | |
471 | |
472 xmlnode_insert_data(xp, text, -1); | |
473 } | |
474 } | |
475 } | |
476 | |
7642 | 477 p = xmlnode_to_str(vc_node, NULL); |
7014 | 478 xmlnode_free(vc_node); |
479 | |
480 account = gaim_connection_get_account(gc); | |
481 | |
482 if (account != NULL) { | |
483 gaim_account_set_user_info(account, p); | |
484 | |
485 if (gc != NULL) | |
486 serv_set_info(gc, p); | |
487 } | |
488 | |
489 g_free(p); | |
490 } | |
491 | |
492 /* | |
493 * This gets executed by the proto action | |
494 * | |
495 * Creates a new GaimRequestFields struct, gets the XML-formatted user_info | |
496 * string (if any) into GSLists for the (multi-entry) edit dialog and | |
497 * calls the set_vcard dialog. | |
498 */ | |
9015 | 499 void jabber_setup_set_info(GaimPluginAction *action) |
7014 | 500 { |
9015 | 501 GaimConnection *gc = (GaimConnection *) action->context; |
7014 | 502 GaimRequestFields *fields; |
503 GaimRequestFieldGroup *group; | |
504 GaimRequestField *field; | |
505 const struct vcard_template *vc_tp; | |
506 char *user_info; | |
507 char *cdata; | |
508 xmlnode *x_vc_data = NULL; | |
509 | |
510 fields = gaim_request_fields_new(); | |
511 group = gaim_request_field_group_new(NULL); | |
512 gaim_request_fields_add_group(fields, group); | |
513 | |
514 /* | |
515 * Get existing, XML-formatted, user info | |
516 */ | |
517 if((user_info = g_strdup(gaim_account_get_user_info(gc->account))) != NULL) | |
518 x_vc_data = xmlnode_from_str(user_info, -1); | |
519 else | |
520 user_info = g_strdup(""); | |
521 | |
522 /* | |
523 * Set up GSLists for edit with labels from "template," data from user info | |
524 */ | |
525 for(vc_tp = vcard_template_data; vc_tp->label != NULL; ++vc_tp) { | |
526 xmlnode *data_node; | |
527 if((vc_tp->label)[0] == '\0') | |
528 continue; | |
529 if(vc_tp->ptag == NULL) { | |
530 data_node = xmlnode_get_child(x_vc_data, vc_tp->tag); | |
531 } else { | |
532 gchar *tag = g_strdup_printf("%s/%s", vc_tp->ptag, vc_tp->tag); | |
533 data_node = xmlnode_get_child(x_vc_data, tag); | |
534 g_free(tag); | |
535 } | |
536 if(data_node) | |
537 cdata = xmlnode_get_data(data_node); | |
538 else | |
539 cdata = NULL; | |
540 | |
541 if(strcmp(vc_tp->tag, "DESC") == 0) { | |
542 field = gaim_request_field_string_new(vc_tp->tag, | |
543 _(vc_tp->label), cdata, | |
544 TRUE); | |
545 } else { | |
546 field = gaim_request_field_string_new(vc_tp->tag, | |
547 _(vc_tp->label), cdata, | |
548 FALSE); | |
549 } | |
550 | |
551 gaim_request_field_group_add_field(group, field); | |
552 } | |
553 | |
554 if(x_vc_data != NULL) | |
555 xmlnode_free(x_vc_data); | |
556 | |
557 g_free(user_info); | |
558 | |
559 gaim_request_fields(gc, _("Edit Jabber vCard"), | |
560 _("Edit Jabber vCard"), | |
561 _("All items below are optional. Enter only the " | |
562 "information with which you feel comfortable."), | |
563 fields, | |
564 _("Save"), G_CALLBACK(jabber_format_info), | |
565 _("Cancel"), NULL, | |
566 gc); | |
567 } | |
568 | |
569 /*---------------------------------------*/ | |
570 /* End Jabber "set info" (vCard) support */ | |
571 /*---------------------------------------*/ | |
572 | |
573 /****** | |
574 * end of that ancient crap that needs to die | |
575 ******/ | |
576 | |
577 | |
7395 | 578 static void jabber_vcard_parse(JabberStream *js, xmlnode *packet, gpointer data) |
7014 | 579 { |
580 GList *resources; | |
581 const char *from = xmlnode_get_attrib(packet, "from"); | |
582 JabberBuddy *jb; | |
583 JabberBuddyResource *jbr; | |
584 GString *info_text; | |
7306 | 585 char *resource_name; |
7955 | 586 char *bare_jid; |
7014 | 587 char *title; |
8213 | 588 char *text; |
7014 | 589 xmlnode *vcard; |
7955 | 590 GaimBuddy *b; |
10189 | 591 GSList *imgids = NULL; |
7014 | 592 |
593 if(!from) | |
594 return; | |
595 | |
10490 | 596 /* XXX: handle the error case */ |
597 | |
7014 | 598 resource_name = jabber_get_resource(from); |
7955 | 599 bare_jid = jabber_get_bare_jid(from); |
600 | |
601 b = gaim_find_buddy(js->gc->account, bare_jid); | |
7014 | 602 |
603 jb = jabber_buddy_find(js, from, TRUE); | |
604 info_text = g_string_new(""); | |
605 | |
8213 | 606 g_string_append_printf(info_text, "<b>%s:</b> %s<br/>", _("Jabber ID"), |
7014 | 607 from); |
608 | |
609 if(resource_name) { | |
610 jbr = jabber_buddy_find_resource(jb, resource_name); | |
611 if(jbr) { | |
7145 | 612 char *purdy = NULL; |
613 if(jbr->status) | |
614 purdy = gaim_strdup_withhtml(jbr->status); | |
8213 | 615 g_string_append_printf(info_text, "<b>%s:</b> %s%s%s<br/>", |
9954 | 616 _("Status"), jabber_buddy_state_get_name(jbr->state), |
7014 | 617 purdy ? ": " : "", |
618 purdy ? purdy : ""); | |
7145 | 619 if(purdy) |
620 g_free(purdy); | |
7014 | 621 } else { |
8213 | 622 g_string_append_printf(info_text, "<b>%s:</b> %s<br/>", |
7014 | 623 _("Status"), _("Unknown")); |
624 } | |
625 } else { | |
626 for(resources = jb->resources; resources; resources = resources->next) { | |
7145 | 627 char *purdy = NULL; |
7014 | 628 jbr = resources->data; |
7145 | 629 if(jbr->status) |
630 purdy = gaim_strdup_withhtml(jbr->status); | |
8213 | 631 g_string_append_printf(info_text, "<b>%s:</b> %s<br/>", |
7014 | 632 _("Resource"), jbr->name); |
8213 | 633 g_string_append_printf(info_text, "<b>%s:</b> %s%s%s<br/><br/>", |
9954 | 634 _("Status"), jabber_buddy_state_get_name(jbr->state), |
7014 | 635 purdy ? ": " : "", |
636 purdy ? purdy : ""); | |
7145 | 637 if(purdy) |
638 g_free(purdy); | |
7014 | 639 } |
640 } | |
641 | |
7306 | 642 g_free(resource_name); |
643 | |
10189 | 644 if((vcard = xmlnode_get_child(packet, "vCard")) || |
645 (vcard = xmlnode_get_child_with_namespace(packet, "query", "vcard-temp"))) { | |
7014 | 646 xmlnode *child; |
647 for(child = vcard->child; child; child = child->next) | |
648 { | |
649 xmlnode *child2; | |
650 | |
8135 | 651 if(child->type != XMLNODE_TYPE_TAG) |
7014 | 652 continue; |
653 | |
654 text = xmlnode_get_data(child); | |
655 if(text && !strcmp(child->name, "FN")) { | |
8213 | 656 g_string_append_printf(info_text, "<b>%s:</b> %s<br/>", |
7014 | 657 _("Full Name"), text); |
658 } else if(!strcmp(child->name, "N")) { | |
659 for(child2 = child->child; child2; child2 = child2->next) | |
660 { | |
661 char *text2; | |
662 | |
8135 | 663 if(child2->type != XMLNODE_TYPE_TAG) |
7014 | 664 continue; |
665 | |
666 text2 = xmlnode_get_data(child2); | |
667 if(text2 && !strcmp(child2->name, "FAMILY")) { | |
668 g_string_append_printf(info_text, | |
8213 | 669 "<b>%s:</b> %s<br/>", |
7014 | 670 _("Family Name"), text2); |
671 } else if(text2 && !strcmp(child2->name, "GIVEN")) { | |
672 g_string_append_printf(info_text, | |
8213 | 673 "<b>%s:</b> %s<br/>", |
7014 | 674 _("Given Name"), text2); |
675 } else if(text2 && !strcmp(child2->name, "MIDDLE")) { | |
676 g_string_append_printf(info_text, | |
8213 | 677 "<b>%s:</b> %s<br/>", |
7014 | 678 _("Middle Name"), text2); |
679 } | |
680 g_free(text2); | |
681 } | |
682 } else if(text && !strcmp(child->name, "NICKNAME")) { | |
683 serv_got_alias(js->gc, from, text); | |
7955 | 684 if(b) { |
685 gaim_blist_node_set_string((GaimBlistNode*)b, "servernick", text); | |
686 } | |
8213 | 687 g_string_append_printf(info_text, "<b>%s:</b> %s<br/>", |
7014 | 688 _("Nickname"), text); |
689 } else if(text && !strcmp(child->name, "BDAY")) { | |
8213 | 690 g_string_append_printf(info_text, "<b>%s:</b> %s<br/>", |
7014 | 691 _("Birthday"), text); |
692 } else if(!strcmp(child->name, "ADR")) { | |
693 /* show which address it is */ | |
694 if(child->child) | |
8213 | 695 g_string_append_printf(info_text, "<b>%s:</b><br/>", |
7014 | 696 _("Address")); |
697 for(child2 = child->child; child2; child2 = child2->next) | |
698 { | |
699 char *text2; | |
700 | |
8135 | 701 if(child2->type != XMLNODE_TYPE_TAG) |
7014 | 702 continue; |
703 | |
704 text2 = xmlnode_get_data(child2); | |
705 if(text2 && !strcmp(child2->name, "POBOX")) { | |
706 g_string_append_printf(info_text, | |
8213 | 707 " <b>%s:</b> %s<br/>", |
7014 | 708 _("P.O. Box"), text2); |
709 } else if(text2 && !strcmp(child2->name, "EXTADR")) { | |
710 g_string_append_printf(info_text, | |
8213 | 711 " <b>%s:</b> %s<br/>", |
7014 | 712 _("Extended Address"), text2); |
713 } else if(text2 && !strcmp(child2->name, "STREET")) { | |
714 g_string_append_printf(info_text, | |
8213 | 715 " <b>%s:</b> %s<br/>", |
7014 | 716 _("Street Address"), text2); |
717 } else if(text2 && !strcmp(child2->name, "LOCALITY")) { | |
718 g_string_append_printf(info_text, | |
8213 | 719 " <b>%s:</b> %s<br/>", |
7014 | 720 _("Locality"), text2); |
721 } else if(text2 && !strcmp(child2->name, "REGION")) { | |
722 g_string_append_printf(info_text, | |
8213 | 723 " <b>%s:</b> %s<br/>", |
7014 | 724 _("Region"), text2); |
725 } else if(text2 && !strcmp(child2->name, "PCODE")) { | |
726 g_string_append_printf(info_text, | |
8213 | 727 " <b>%s:</b> %s<br/>", |
7014 | 728 _("Postal Code"), text2); |
729 } else if(text2 && (!strcmp(child2->name, "CTRY") | |
730 || !strcmp(child2->name, "COUNTRY"))) { | |
731 g_string_append_printf(info_text, | |
8213 | 732 " <b>%s:</b> %s<br/>", |
7014 | 733 _("Country"), text2); |
734 } | |
735 g_free(text2); | |
736 } | |
737 } else if(!strcmp(child->name, "TEL")) { | |
738 char *number; | |
739 if((child2 = xmlnode_get_child(child, "NUMBER"))) { | |
740 /* show what kind of number it is */ | |
741 number = xmlnode_get_data(child2); | |
742 if(number) { | |
743 g_string_append_printf(info_text, | |
8213 | 744 "<b>%s:</b> %s<br/>", _("Telephone"), number); |
7014 | 745 g_free(number); |
746 } | |
747 } else if((number = xmlnode_get_data(child))) { | |
748 /* lots of clients (including gaim) do this, but it's | |
749 * out of spec */ | |
750 g_string_append_printf(info_text, | |
8213 | 751 "<b>%s:</b> %s<br/>", _("Telephone"), number); |
7014 | 752 g_free(number); |
753 } | |
754 } else if(!strcmp(child->name, "EMAIL")) { | |
755 char *userid; | |
756 if((child2 = xmlnode_get_child(child, "USERID"))) { | |
757 /* show what kind of email it is */ | |
758 userid = xmlnode_get_data(child2); | |
759 if(userid) { | |
760 g_string_append_printf(info_text, | |
8213 | 761 "<b>%s:</b> <a href='mailto:%s'>%s</a><br/>", |
7014 | 762 _("Email"), userid, userid); |
763 g_free(userid); | |
764 } | |
765 } else if((userid = xmlnode_get_data(child))) { | |
766 /* lots of clients (including gaim) do this, but it's | |
767 * out of spec */ | |
768 g_string_append_printf(info_text, | |
8213 | 769 "<b>%s:</b> <a href='mailto:%s'>%s</a><br/>", |
7014 | 770 _("Email"), userid, userid); |
771 g_free(userid); | |
772 } | |
773 } else if(!strcmp(child->name, "ORG")) { | |
774 for(child2 = child->child; child2; child2 = child2->next) | |
775 { | |
776 char *text2; | |
777 | |
8135 | 778 if(child2->type != XMLNODE_TYPE_TAG) |
7014 | 779 continue; |
780 | |
781 text2 = xmlnode_get_data(child2); | |
782 if(text2 && !strcmp(child2->name, "ORGNAME")) { | |
783 g_string_append_printf(info_text, | |
8213 | 784 "<b>%s:</b> %s<br/>", |
7014 | 785 _("Organization Name"), text2); |
786 } else if(text2 && !strcmp(child2->name, "ORGUNIT")) { | |
787 g_string_append_printf(info_text, | |
8213 | 788 "<b>%s:</b> %s<br/>", |
7014 | 789 _("Organization Unit"), text2); |
790 } | |
791 g_free(text2); | |
792 } | |
793 } else if(text && !strcmp(child->name, "TITLE")) { | |
8213 | 794 g_string_append_printf(info_text, "<b>%s:</b> %s<br/>", |
7014 | 795 _("Title"), text); |
796 } else if(text && !strcmp(child->name, "ROLE")) { | |
8213 | 797 g_string_append_printf(info_text, "<b>%s:</b> %s<br/>", |
7014 | 798 _("Role"), text); |
799 } else if(text && !strcmp(child->name, "DESC")) { | |
8213 | 800 g_string_append_printf(info_text, "<b>%s:</b> %s<br/>", |
801 _("Description"), text); | |
7076 | 802 } else if(!strcmp(child->name, "PHOTO") || |
803 !strcmp(child->name, "LOGO")) { | |
10189 | 804 int size, i; |
805 unsigned char hashval[20]; | |
806 char *data, *p, hash[41]; | |
807 gboolean photo = (strcmp(child->name, "PHOTO") == 0); | |
808 | |
809 gaim_base64_decode(text, &data, &size); | |
7076 | 810 |
10189 | 811 imgids = g_slist_prepend(imgids, GINT_TO_POINTER(gaim_imgstore_add(data, size, "logo.png"))); |
812 g_string_append_printf(info_text, | |
813 "<b>%s:</b> <img id='%d'><br/>", | |
814 photo ? _("Photo") : _("Logo"), | |
815 GPOINTER_TO_INT(imgids->data)); | |
7076 | 816 |
10189 | 817 gaim_buddy_icons_set_for_user(js->gc->account, bare_jid, |
818 data, size); | |
819 | |
820 shaBlock((unsigned char *)data, size, hashval); | |
821 p = hash; | |
822 for(i=0; i<20; i++, p+=2) | |
823 snprintf(p, 3, "%02x", hashval[i]); | |
824 gaim_blist_node_set_string((GaimBlistNode*)b, "avatar_hash", hash); | |
825 | |
826 g_free(data); | |
7014 | 827 } |
828 g_free(text); | |
829 } | |
830 } | |
831 | |
832 title = g_strdup_printf("User info for %s", from); | |
833 | |
8213 | 834 text = gaim_strdup_withhtml(info_text->str); |
835 | |
9797 | 836 gaim_notify_userinfo(js->gc, from, title, _("Jabber Profile"), |
8213 | 837 NULL, text, NULL, NULL); |
7014 | 838 |
10189 | 839 while(imgids) { |
840 gaim_imgstore_unref(GPOINTER_TO_INT(imgids->data)); | |
841 imgids = g_slist_delete_link(imgids, imgids); | |
842 } | |
7014 | 843 g_free(title); |
844 g_string_free(info_text, TRUE); | |
8213 | 845 g_free(text); |
10189 | 846 g_free(bare_jid); |
7014 | 847 } |
848 | |
10189 | 849 static void jabber_buddy_get_info_for_jid(JabberStream *js, const char *full_jid) |
7014 | 850 { |
851 JabberIq *iq; | |
852 xmlnode *vcard; | |
853 | |
854 iq = jabber_iq_new(js, JABBER_IQ_GET); | |
855 | |
10189 | 856 xmlnode_set_attrib(iq->node, "to", full_jid); |
7014 | 857 vcard = xmlnode_new_child(iq->node, "vCard"); |
858 xmlnode_set_attrib(vcard, "xmlns", "vcard-temp"); | |
859 | |
7395 | 860 jabber_iq_set_callback(iq, jabber_vcard_parse, NULL); |
7014 | 861 |
862 jabber_iq_send(iq); | |
863 } | |
864 | |
10189 | 865 void jabber_buddy_get_info(GaimConnection *gc, const char *who) |
866 { | |
867 JabberStream *js = gc->proto_data; | |
868 char *bare_jid = jabber_get_bare_jid(who); | |
869 | |
870 if(bare_jid) { | |
871 jabber_buddy_get_info_for_jid(js, bare_jid); | |
872 g_free(bare_jid); | |
873 } | |
874 } | |
875 | |
7014 | 876 void jabber_buddy_get_info_chat(GaimConnection *gc, int id, |
877 const char *resource) | |
878 { | |
879 JabberStream *js = gc->proto_data; | |
880 JabberChat *chat = jabber_chat_find_by_id(js, id); | |
881 char *full_jid; | |
882 | |
883 if(!chat) | |
884 return; | |
885 | |
886 full_jid = g_strdup_printf("%s@%s/%s", chat->room, chat->server, resource); | |
10189 | 887 jabber_buddy_get_info_for_jid(js, full_jid); |
7014 | 888 g_free(full_jid); |
889 } | |
890 | |
7395 | 891 |
7014 | 892 static void jabber_buddy_set_invisibility(JabberStream *js, const char *who, |
893 gboolean invisible) | |
894 { | |
9944 | 895 GaimPresence *gpresence; |
896 GaimAccount *account; | |
897 GaimStatus *status; | |
7014 | 898 JabberBuddy *jb = jabber_buddy_find(js, who, TRUE); |
899 xmlnode *presence; | |
9954 | 900 JabberBuddyState state; |
901 const char *msg; | |
902 int priority; | |
7014 | 903 |
9944 | 904 account = gaim_connection_get_account(js->gc); |
905 gpresence = gaim_account_get_presence(account); | |
906 status = gaim_presence_get_active_status(gpresence); | |
907 | |
9954 | 908 gaim_status_to_jabber(status, &state, &msg, &priority); |
909 presence = jabber_presence_create(state, msg, priority); | |
910 | |
7014 | 911 xmlnode_set_attrib(presence, "to", who); |
912 if(invisible) { | |
913 xmlnode_set_attrib(presence, "type", "invisible"); | |
914 jb->invisible |= JABBER_INVIS_BUDDY; | |
915 } else { | |
916 jb->invisible &= ~JABBER_INVIS_BUDDY; | |
917 } | |
918 | |
919 jabber_send(js, presence); | |
920 xmlnode_free(presence); | |
921 } | |
922 | |
9030 | 923 static void jabber_buddy_make_invisible(GaimBlistNode *node, gpointer data) |
7014 | 924 { |
9030 | 925 GaimBuddy *buddy; |
926 GaimConnection *gc; | |
927 JabberStream *js; | |
928 | |
929 g_return_if_fail(GAIM_BLIST_NODE_IS_BUDDY(node)); | |
930 | |
931 buddy = (GaimBuddy *) node; | |
932 gc = gaim_account_get_connection(buddy->account); | |
933 js = gc->proto_data; | |
934 | |
935 jabber_buddy_set_invisibility(js, buddy->name, TRUE); | |
7014 | 936 } |
937 | |
9030 | 938 static void jabber_buddy_make_visible(GaimBlistNode *node, gpointer data) |
7014 | 939 { |
9030 | 940 GaimBuddy *buddy; |
941 GaimConnection *gc; | |
942 JabberStream *js; | |
7014 | 943 |
9030 | 944 g_return_if_fail(GAIM_BLIST_NODE_IS_BUDDY(node)); |
7014 | 945 |
9030 | 946 buddy = (GaimBuddy *) node; |
947 gc = gaim_account_get_connection(buddy->account); | |
948 js = gc->proto_data; | |
949 | |
950 jabber_buddy_set_invisibility(js, buddy->name, FALSE); | |
7014 | 951 } |
952 | |
9030 | 953 static void jabber_buddy_cancel_presence_notification(GaimBlistNode *node, |
954 gpointer data) | |
7014 | 955 { |
9030 | 956 GaimBuddy *buddy; |
957 GaimConnection *gc; | |
958 JabberStream *js; | |
959 | |
960 g_return_if_fail(GAIM_BLIST_NODE_IS_BUDDY(node)); | |
7014 | 961 |
9030 | 962 buddy = (GaimBuddy *) node; |
963 gc = gaim_account_get_connection(buddy->account); | |
964 js = gc->proto_data; | |
965 | |
966 /* I wonder if we should prompt the user before doing this */ | |
967 jabber_presence_subscription_set(js, buddy->name, "unsubscribed"); | |
7014 | 968 } |
969 | |
9030 | 970 static void jabber_buddy_rerequest_auth(GaimBlistNode *node, gpointer data) |
7250 | 971 { |
9030 | 972 GaimBuddy *buddy; |
973 GaimConnection *gc; | |
974 JabberStream *js; | |
975 | |
976 g_return_if_fail(GAIM_BLIST_NODE_IS_BUDDY(node)); | |
7250 | 977 |
9030 | 978 buddy = (GaimBuddy *) node; |
979 gc = gaim_account_get_connection(buddy->account); | |
980 js = gc->proto_data; | |
981 | |
982 jabber_presence_subscription_set(js, buddy->name, "subscribe"); | |
7250 | 983 } |
984 | |
9030 | 985 |
986 static void jabber_buddy_unsubscribe(GaimBlistNode *node, gpointer data) | |
7014 | 987 { |
9030 | 988 GaimBuddy *buddy; |
989 GaimConnection *gc; | |
990 JabberStream *js; | |
991 | |
992 g_return_if_fail(GAIM_BLIST_NODE_IS_BUDDY(node)); | |
993 | |
994 buddy = (GaimBuddy *) node; | |
995 gc = gaim_account_get_connection(buddy->account); | |
996 js = gc->proto_data; | |
997 | |
998 jabber_presence_subscription_set(js, buddy->name, "unsubscribe"); | |
999 } | |
1000 | |
1001 | |
1002 GList *jabber_buddy_menu(GaimBuddy *buddy) | |
1003 { | |
1004 GaimConnection *gc = gaim_account_get_connection(buddy->account); | |
1005 JabberStream *js = gc->proto_data; | |
1006 JabberBuddy *jb = jabber_buddy_find(js, buddy->name, TRUE); | |
1007 | |
7014 | 1008 GList *m = NULL; |
9030 | 1009 GaimBlistNodeAction *act; |
7395 | 1010 |
1011 if(!jb) | |
1012 return m; | |
1013 | |
8185 | 1014 /* XXX: fix the NOT ME below */ |
1015 | |
1016 if(js->protocol_version == JABBER_PROTO_0_9 /* && NOT ME */) { | |
8166 | 1017 if(jb->invisible & JABBER_INVIS_BUDDY) { |
9030 | 1018 act = gaim_blist_node_action_new(_("Un-hide From"), |
1019 jabber_buddy_make_visible, NULL); | |
8166 | 1020 } else { |
9030 | 1021 act = gaim_blist_node_action_new(_("Temporarily Hide From"), |
1022 jabber_buddy_make_invisible, NULL); | |
8166 | 1023 } |
9030 | 1024 m = g_list_append(m, act); |
7014 | 1025 } |
1026 | |
8185 | 1027 if(jb->subscription & JABBER_SUB_FROM /* && NOT ME */) { |
9030 | 1028 act = gaim_blist_node_action_new(_("Cancel Presence Notification"), |
1029 jabber_buddy_cancel_presence_notification, NULL); | |
1030 m = g_list_append(m, act); | |
7014 | 1031 } |
1032 | |
1033 if(!(jb->subscription & JABBER_SUB_TO)) { | |
9030 | 1034 act = gaim_blist_node_action_new(_("(Re-)Request authorization"), |
1035 jabber_buddy_rerequest_auth, NULL); | |
1036 m = g_list_append(m, act); | |
1037 | |
8185 | 1038 } else /* if(NOT ME) */{ |
9030 | 1039 |
1040 /* shouldn't this just happen automatically when the buddy is | |
1041 removed? */ | |
1042 act = gaim_blist_node_action_new(_("Unsubscribe"), | |
1043 jabber_buddy_unsubscribe, NULL); | |
1044 m = g_list_append(m, act); | |
7014 | 1045 } |
1046 | |
1047 return m; | |
1048 } | |
9030 | 1049 |
1050 GList * | |
1051 jabber_blist_node_menu(GaimBlistNode *node) | |
1052 { | |
1053 if(GAIM_BLIST_NODE_IS_BUDDY(node)) { | |
1054 return jabber_buddy_menu((GaimBuddy *) node); | |
1055 } else { | |
1056 return NULL; | |
1057 } | |
1058 } | |
1059 | |
1060 | |
9954 | 1061 const char * |
1062 jabber_buddy_state_get_name(JabberBuddyState state) | |
1063 { | |
1064 switch(state) { | |
1065 case JABBER_BUDDY_STATE_UNKNOWN: | |
1066 return _("Unknown"); | |
1067 case JABBER_BUDDY_STATE_ERROR: | |
1068 return _("Error"); | |
1069 case JABBER_BUDDY_STATE_UNAVAILABLE: | |
1070 return _("Offline"); | |
1071 case JABBER_BUDDY_STATE_ONLINE: | |
1072 return _("Online"); | |
1073 case JABBER_BUDDY_STATE_CHAT: | |
1074 return _("Chatty"); | |
1075 case JABBER_BUDDY_STATE_AWAY: | |
1076 return _("Away"); | |
1077 case JABBER_BUDDY_STATE_XA: | |
1078 return _("Extended Away"); | |
1079 case JABBER_BUDDY_STATE_DND: | |
1080 return _("Do Not Disturb"); | |
1081 } | |
1082 | |
1083 return _("Unknown"); | |
1084 } | |
1085 | |
1086 JabberBuddyState jabber_buddy_status_id_get_state(const char *id) { | |
1087 if(!id) | |
1088 return JABBER_BUDDY_STATE_UNKNOWN; | |
1089 if(!strcmp(id, "online")) | |
1090 return JABBER_BUDDY_STATE_ONLINE; | |
1091 if(!strcmp(id, "chat")) | |
1092 return JABBER_BUDDY_STATE_CHAT; | |
1093 if(!strcmp(id, "away")) | |
1094 return JABBER_BUDDY_STATE_AWAY; | |
1095 if(!strcmp(id, "xa")) | |
1096 return JABBER_BUDDY_STATE_XA; | |
1097 if(!strcmp(id, "dnd")) | |
1098 return JABBER_BUDDY_STATE_DND; | |
1099 if(!strcmp(id, "offline")) | |
1100 return JABBER_BUDDY_STATE_UNAVAILABLE; | |
1101 if(!strcmp(id, "error")) | |
1102 return JABBER_BUDDY_STATE_ERROR; | |
1103 | |
1104 return JABBER_BUDDY_STATE_UNKNOWN; | |
1105 } | |
1106 | |
1107 const char *jabber_buddy_state_get_status_id(JabberBuddyState state) { | |
1108 switch(state) { | |
1109 case JABBER_BUDDY_STATE_CHAT: | |
1110 return "chat"; | |
1111 case JABBER_BUDDY_STATE_AWAY: | |
1112 return "away"; | |
1113 case JABBER_BUDDY_STATE_XA: | |
1114 return "xa"; | |
1115 case JABBER_BUDDY_STATE_DND: | |
1116 return "dnd"; | |
1117 case JABBER_BUDDY_STATE_ONLINE: | |
1118 return "online"; | |
1119 case JABBER_BUDDY_STATE_UNKNOWN: | |
1120 case JABBER_BUDDY_STATE_ERROR: | |
1121 return NULL; | |
1122 case JABBER_BUDDY_STATE_UNAVAILABLE: | |
1123 return "offline"; | |
1124 } | |
1125 return NULL; | |
1126 } |