Mercurial > pidgin.yaz
annotate src/protocols/jabber/jabber.c @ 3259:8b1309bea7e5
[gaim-migrate @ 3277]
Certain types of Jabber presence errors now show "broken light-bulb"
graphic and status available in "Get Away Msg".
committer: Tailor Script <tailor@pidgin.im>
author | Jim Seymour <jseymour> |
---|---|
date | Sat, 18 May 2002 20:34:55 +0000 |
parents | 4c28751180f9 |
children | 778646c2adbc |
rev | line source |
---|---|
2086 | 1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
2 /* | |
3 * gaim | |
4 * | |
5 * Some code copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
6 * libfaim code copyright 1998, 1999 Adam Fritzler <afritz@auk.cx> | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 * | |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include "config.h" | |
26 #endif | |
27 | |
28 | |
29 #include <netdb.h> | |
30 #include <unistd.h> | |
31 #include <errno.h> | |
32 #include <netinet/in.h> | |
33 #include <arpa/inet.h> | |
34 #include <string.h> | |
35 #include <stdlib.h> | |
36 #include <stdio.h> | |
37 #include <time.h> | |
38 #include <sys/socket.h> | |
39 #include <sys/utsname.h> | |
40 #include <sys/stat.h> | |
41 #include "multi.h" | |
42 #include "prpl.h" | |
43 #include "gaim.h" | |
2232
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
44 #ifdef MAX |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
45 #undef MAX |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
46 #endif |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
47 #ifdef MIN |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
48 #undef MIN |
14e8978f86bb
[gaim-migrate @ 2242]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2231
diff
changeset
|
49 #endif |
2086 | 50 #include "jabber.h" |
51 #include "proxy.h" | |
52 | |
53 #include "pixmaps/available.xpm" | |
54 #include "pixmaps/available-away.xpm" | |
55 #include "pixmaps/available-chat.xpm" | |
56 #include "pixmaps/available-xa.xpm" | |
57 #include "pixmaps/available-dnd.xpm" | |
3259 | 58 #include "pixmaps/available-error.xpm" |
2086 | 59 |
60 /* The priv member of gjconn's is a gaim_connection for now. */ | |
61 #define GJ_GC(x) ((struct gaim_connection *)(x)->priv) | |
62 | |
63 #define IQID_AUTH "__AUTH__" | |
64 | |
65 #define IQ_NONE -1 | |
66 #define IQ_AUTH 0 | |
67 #define IQ_ROSTER 1 | |
68 | |
3259 | 69 #define UC_AWAY (0x02 | UC_UNAVAILABLE) |
70 #define UC_CHAT 0x04 | |
71 #define UC_XA (0x08 | UC_UNAVAILABLE) | |
72 #define UC_DND (0x10 | UC_UNAVAILABLE) | |
73 #define UC_ERROR (0x20 | UC_UNAVAILABLE) | |
2086 | 74 |
75 #define DEFAULT_SERVER "jabber.org" | |
76 #define DEFAULT_GROUPCHAT "conference.jabber.org" | |
77 #define DEFAULT_PORT 5222 | |
78 | |
79 #define USEROPT_PORT 0 | |
80 | |
3074 | 81 /* |
82 * Note: "was_connected" may seem redundant, but it was needed and I | |
83 * didn't want to touch the Jabber state stuff not specific to Gaim. | |
84 */ | |
2086 | 85 typedef struct gjconn_struct { |
86 /* Core structure */ | |
87 pool p; /* Memory allocation pool */ | |
88 int state; /* Connection state flag */ | |
3074 | 89 int was_connected; /* We were once connected */ |
2086 | 90 int fd; /* Connection file descriptor */ |
91 jid user; /* User info */ | |
92 char *pass; /* User passwd */ | |
93 | |
94 /* Stream stuff */ | |
95 int id; /* id counter for jab_getid() function */ | |
96 char idbuf[9]; /* temporary storage for jab_getid() */ | |
97 char *sid; /* stream id from server, for digest auth */ | |
98 XML_Parser parser; /* Parser instance */ | |
99 xmlnode current; /* Current node in parsing instance.. */ | |
100 | |
101 /* Event callback ptrs */ | |
2956 | 102 void (*on_state)(struct gjconn_struct *gjc, int state); |
103 void (*on_packet)(struct gjconn_struct *gjc, jpacket p); | |
104 | |
105 GHashTable *queries; /* query tracker */ | |
2086 | 106 |
107 void *priv; | |
108 | |
109 } *gjconn, gjconn_struct; | |
110 | |
2956 | 111 typedef void (*gjconn_state_h)(gjconn gjc, int state); |
112 typedef void (*gjconn_packet_h)(gjconn gjc, jpacket p); | |
2086 | 113 |
114 static gjconn gjab_new(char *user, char *pass, void *priv); | |
2956 | 115 static void gjab_delete(gjconn gjc); |
116 static void gjab_state_handler(gjconn gjc, gjconn_state_h h); | |
117 static void gjab_packet_handler(gjconn gjc, gjconn_packet_h h); | |
118 static void gjab_start(gjconn gjc); | |
119 static void gjab_stop(gjconn gjc); | |
2086 | 120 /* |
2956 | 121 static int gjab_getfd(gjconn gjc); |
122 static jid gjab_getjid(gjconn gjc); | |
123 static char *gjab_getsid(gjconn gjc); | |
2086 | 124 */ |
2956 | 125 static char *gjab_getid(gjconn gjc); |
126 static void gjab_send(gjconn gjc, xmlnode x); | |
127 static void gjab_send_raw(gjconn gjc, const char *str); | |
128 static void gjab_recv(gjconn gjc); | |
129 static void gjab_auth(gjconn gjc); | |
130 | |
131 /* | |
132 * It is *this* to which we point the gaim_connection proto_data | |
133 */ | |
2086 | 134 struct jabber_data { |
2956 | 135 gjconn gjc; |
2086 | 136 gboolean did_import; |
2956 | 137 GSList *chats; |
2086 | 138 GHashTable *hash; |
139 time_t idle; | |
2800
0ad63a625eec
[gaim-migrate @ 2813]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2791
diff
changeset
|
140 gboolean die; |
2086 | 141 }; |
142 | |
2956 | 143 /* |
144 * Jabber "chat group" info. Pointers to these go in jabber_data | |
145 * pending and existing chats lists. | |
146 */ | |
2086 | 147 struct jabber_chat { |
148 jid Jid; | |
149 struct gaim_connection *gc; | |
150 struct conversation *b; | |
151 int id; | |
2956 | 152 int state; |
2086 | 153 }; |
154 | |
2956 | 155 /* |
156 * Jabber chat states... | |
157 * | |
158 * Note: due to a bug in one version of the Jabber server, subscriptions | |
159 * to chat groups aren't (always?) properly removed at the server. The | |
160 * result is clients receive Jabber "presence" notifications for JIDs | |
161 * they no longer care about. The problem with such vestigial notifies is | |
162 * that we really have no way of telling if it's vestigial or if it's a | |
163 * valid "buddy" presence notification. So we keep jabber_chat structs | |
164 * around after leaving a chat group and simply mark them "closed." That | |
165 * way we can test for such errant presence notifications. I.e.: if we | |
166 * get a presence notfication from a JID that matches a chat group JID, | |
167 * we disregard it. | |
168 */ | |
169 #define JCS_PENDING 1 /* pending */ | |
170 #define JCS_ACTIVE 2 /* active */ | |
171 #define JCS_CLOSED 3 /* closed */ | |
172 | |
173 | |
2086 | 174 static char *jabber_name() |
175 { | |
176 return "Jabber"; | |
177 } | |
178 | |
2956 | 179 #define STATE_EVT(arg) if(gjc->on_state) { (gjc->on_state)(gjc, (arg) ); } |
180 | |
181 static void jabber_handlevcard(gjconn, xmlnode, char *); | |
2086 | 182 |
183 static char *create_valid_jid(const char *given, char *server, char *resource) | |
184 { | |
185 char *valid; | |
186 | |
187 if (!strchr(given, '@')) | |
188 valid = g_strdup_printf("%s@%s/%s", given, server, resource); | |
189 else if (!strchr(strchr(given, '@'), '/')) | |
190 valid = g_strdup_printf("%s/%s", given, resource); | |
191 else | |
192 valid = g_strdup(given); | |
193 | |
194 return valid; | |
195 } | |
196 | |
197 static gjconn gjab_new(char *user, char *pass, void *priv) | |
198 { | |
199 pool p; | |
2956 | 200 gjconn gjc; |
2086 | 201 |
202 if (!user) | |
203 return (NULL); | |
204 | |
205 p = pool_new(); | |
206 if (!p) | |
207 return (NULL); | |
2956 | 208 gjc = pmalloc_x(p, sizeof(gjconn_struct), 0); |
209 if (!gjc) { | |
210 pool_free(p); /* no need for this anymore! */ | |
2086 | 211 return (NULL); |
2956 | 212 } |
213 gjc->p = p; | |
214 | |
3236 | 215 if((gjc->user = jid_new(p, user)) == NULL) { |
216 pool_free(p); /* no need for this anymore! */ | |
217 return (NULL); | |
218 } | |
3257 | 219 |
220 gjc->pass = strdup(pass); | |
2956 | 221 |
222 gjc->state = JCONN_STATE_OFF; | |
3074 | 223 gjc->was_connected = 0; |
2956 | 224 gjc->id = 1; |
225 gjc->fd = -1; | |
226 | |
227 gjc->priv = priv; | |
228 | |
229 return gjc; | |
2086 | 230 } |
231 | |
2956 | 232 static void gjab_delete(gjconn gjc) |
2086 | 233 { |
2956 | 234 if (!gjc) |
2086 | 235 return; |
236 | |
2956 | 237 gjab_stop(gjc); |
3257 | 238 free(gjc->pass); |
2956 | 239 pool_free(gjc->p); |
2086 | 240 } |
241 | |
2956 | 242 static void gjab_state_handler(gjconn gjc, gjconn_state_h h) |
2086 | 243 { |
2956 | 244 if (!gjc) |
2086 | 245 return; |
246 | |
2956 | 247 gjc->on_state = h; |
2086 | 248 } |
249 | |
2956 | 250 static void gjab_packet_handler(gjconn gjc, gjconn_packet_h h) |
2086 | 251 { |
2956 | 252 if (!gjc) |
2086 | 253 return; |
254 | |
2956 | 255 gjc->on_packet = h; |
2086 | 256 } |
257 | |
2956 | 258 static void gjab_stop(gjconn gjc) |
2086 | 259 { |
2956 | 260 if (!gjc || gjc->state == JCONN_STATE_OFF) |
2086 | 261 return; |
262 | |
2956 | 263 gjab_send_raw(gjc, "</stream:stream>"); |
264 gjc->state = JCONN_STATE_OFF; | |
3074 | 265 gjc->was_connected = 0; |
2956 | 266 close(gjc->fd); |
267 gjc->fd = -1; | |
268 XML_ParserFree(gjc->parser); | |
269 gjc->parser = NULL; | |
2086 | 270 } |
271 | |
272 /* | |
2956 | 273 static int gjab_getfd(gjconn gjc) |
2086 | 274 { |
2956 | 275 if (gjc) |
276 return gjc->fd; | |
2086 | 277 else |
278 return -1; | |
279 } | |
280 | |
2956 | 281 static jid gjab_getjid(gjconn gjc) |
2086 | 282 { |
2956 | 283 if (gjc) |
284 return (gjc->user); | |
2086 | 285 else |
286 return NULL; | |
287 } | |
288 | |
2956 | 289 static char *gjab_getsid(gjconn gjc) |
2086 | 290 { |
2956 | 291 if (gjc) |
292 return (gjc->sid); | |
2086 | 293 else |
294 return NULL; | |
295 } | |
296 */ | |
297 | |
2956 | 298 static char *gjab_getid(gjconn gjc) |
2086 | 299 { |
2956 | 300 snprintf(gjc->idbuf, 8, "%d", gjc->id++); |
301 return &gjc->idbuf[0]; | |
2086 | 302 } |
303 | |
2956 | 304 static void gjab_send(gjconn gjc, xmlnode x) |
2086 | 305 { |
2956 | 306 if (gjc && gjc->state != JCONN_STATE_OFF) { |
2086 | 307 char *buf = xmlnode2str(x); |
308 if (buf) | |
2956 | 309 write(gjc->fd, buf, strlen(buf)); |
2086 | 310 debug_printf("gjab_send: %s\n", buf); |
311 } | |
312 } | |
313 | |
2956 | 314 static void gjab_send_raw(gjconn gjc, const char *str) |
2086 | 315 { |
2956 | 316 if (gjc && gjc->state != JCONN_STATE_OFF) { |
317 /* | |
318 * JFIXME: No error detection?!?! | |
319 */ | |
320 if(write(gjc->fd, str, strlen(str)) < 0) { | |
321 fprintf(stderr, "DBG: Problem sending. Error: %d\n", errno); | |
322 fflush(stderr); | |
323 } | |
2086 | 324 debug_printf("gjab_send_raw: %s\n", str); |
325 } | |
326 } | |
327 | |
2956 | 328 static void gjab_reqroster(gjconn gjc) |
2086 | 329 { |
330 xmlnode x; | |
331 | |
332 x = jutil_iqnew(JPACKET__GET, NS_ROSTER); | |
2956 | 333 xmlnode_put_attrib(x, "id", gjab_getid(gjc)); |
334 | |
335 gjab_send(gjc, x); | |
2086 | 336 xmlnode_free(x); |
337 } | |
338 | |
2956 | 339 static void gjab_reqauth(gjconn gjc) |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
340 { |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
341 xmlnode x, y, z; |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
342 char *user; |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
343 |
2956 | 344 if (!gjc) |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
345 return; |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
346 |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
347 x = jutil_iqnew(JPACKET__GET, NS_AUTH); |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
348 xmlnode_put_attrib(x, "id", IQID_AUTH); |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
349 y = xmlnode_get_tag(x, "query"); |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
350 |
2956 | 351 user = gjc->user->user; |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
352 |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
353 if (user) { |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
354 z = xmlnode_insert_tag(y, "username"); |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
355 xmlnode_insert_cdata(z, user, -1); |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
356 } |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
357 |
2956 | 358 gjab_send(gjc, x); |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
359 xmlnode_free(x); |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
360 } |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
361 |
2956 | 362 static void gjab_auth(gjconn gjc) |
2086 | 363 { |
364 xmlnode x, y, z; | |
365 char *hash, *user; | |
366 | |
2956 | 367 if (!gjc) |
2086 | 368 return; |
369 | |
370 x = jutil_iqnew(JPACKET__SET, NS_AUTH); | |
371 xmlnode_put_attrib(x, "id", IQID_AUTH); | |
372 y = xmlnode_get_tag(x, "query"); | |
373 | |
2956 | 374 user = gjc->user->user; |
2086 | 375 |
376 if (user) { | |
377 z = xmlnode_insert_tag(y, "username"); | |
378 xmlnode_insert_cdata(z, user, -1); | |
379 } | |
380 | |
381 z = xmlnode_insert_tag(y, "resource"); | |
2956 | 382 xmlnode_insert_cdata(z, gjc->user->resource, -1); |
383 | |
384 if (gjc->sid) { | |
385 debug_printf("digest authentication (sid %s)\n", gjc->sid); | |
2086 | 386 z = xmlnode_insert_tag(y, "digest"); |
2956 | 387 hash = pmalloc(x->p, strlen(gjc->sid) + strlen(gjc->pass) + 1); |
388 strcpy(hash, gjc->sid); | |
389 strcat(hash, gjc->pass); | |
2086 | 390 hash = shahash(hash); |
391 xmlnode_insert_cdata(z, hash, 40); | |
392 } else { | |
393 z = xmlnode_insert_tag(y, "password"); | |
2956 | 394 xmlnode_insert_cdata(z, gjc->pass, -1); |
2086 | 395 } |
396 | |
2956 | 397 gjab_send(gjc, x); |
2086 | 398 xmlnode_free(x); |
399 | |
400 return; | |
401 } | |
402 | |
2956 | 403 static void gjab_recv(gjconn gjc) |
2086 | 404 { |
405 static char buf[4096]; | |
406 int len; | |
407 | |
2956 | 408 if (!gjc || gjc->state == JCONN_STATE_OFF) |
2086 | 409 return; |
410 | |
3234 | 411 if ((len = read(gjc->fd, buf, sizeof(buf) - 1)) > 0) { |
2956 | 412 struct jabber_data *jd = GJ_GC(gjc)->proto_data; |
2086 | 413 buf[len] = '\0'; |
414 debug_printf("input (len %d): %s\n", len, buf); | |
2956 | 415 XML_Parse(gjc->parser, buf, len, 0); |
2800
0ad63a625eec
[gaim-migrate @ 2813]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2791
diff
changeset
|
416 if (jd->die) |
2956 | 417 signoff(GJ_GC(gjc)); |
3105 | 418 } else if (len < 0 || errno != EAGAIN) { |
2086 | 419 STATE_EVT(JCONN_STATE_OFF) |
420 } | |
421 } | |
422 | |
423 static void startElement(void *userdata, const char *name, const char **attribs) | |
424 { | |
425 xmlnode x; | |
2956 | 426 gjconn gjc = (gjconn) userdata; |
427 | |
428 if (gjc->current) { | |
2086 | 429 /* Append the node to the current one */ |
2956 | 430 x = xmlnode_insert_tag(gjc->current, name); |
2086 | 431 xmlnode_put_expat_attribs(x, attribs); |
432 | |
2956 | 433 gjc->current = x; |
2086 | 434 } else { |
435 x = xmlnode_new_tag(name); | |
436 xmlnode_put_expat_attribs(x, attribs); | |
437 if (strcmp(name, "stream:stream") == 0) { | |
438 /* special case: name == stream:stream */ | |
439 /* id attrib of stream is stored for digest auth */ | |
2956 | 440 gjc->sid = g_strdup(xmlnode_get_attrib(x, "id")); |
2086 | 441 /* STATE_EVT(JCONN_STATE_AUTH) */ |
2635
8c75e59e4bdf
[gaim-migrate @ 2648]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2607
diff
changeset
|
442 xmlnode_free(x); |
2086 | 443 } else { |
2956 | 444 gjc->current = x; |
2086 | 445 } |
446 } | |
447 } | |
448 | |
449 static void endElement(void *userdata, const char *name) | |
450 { | |
2956 | 451 gjconn gjc = (gjconn) userdata; |
2086 | 452 xmlnode x; |
453 jpacket p; | |
454 | |
2956 | 455 if (gjc->current == NULL) { |
2086 | 456 /* we got </stream:stream> */ |
457 STATE_EVT(JCONN_STATE_OFF) | |
458 return; | |
459 } | |
460 | |
2956 | 461 x = xmlnode_get_parent(gjc->current); |
2086 | 462 |
463 if (!x) { | |
464 /* it is time to fire the event */ | |
2956 | 465 p = jpacket_new(gjc->current); |
466 | |
467 if (gjc->on_packet) | |
468 (gjc->on_packet) (gjc, p); | |
2086 | 469 else |
2956 | 470 xmlnode_free(gjc->current); |
2086 | 471 } |
472 | |
2956 | 473 gjc->current = x; |
2086 | 474 } |
475 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
476 static void jabber_callback(gpointer data, gint source, GaimInputCondition condition) |
2086 | 477 { |
478 struct gaim_connection *gc = (struct gaim_connection *)data; | |
479 struct jabber_data *jd = (struct jabber_data *)gc->proto_data; | |
480 | |
2956 | 481 gjab_recv(jd->gjc); |
2086 | 482 } |
483 | |
484 static void charData(void *userdata, const char *s, int slen) | |
485 { | |
2956 | 486 gjconn gjc = (gjconn) userdata; |
487 | |
488 if (gjc->current) | |
489 xmlnode_insert_cdata(gjc->current, s, slen); | |
2086 | 490 } |
491 | |
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
492 static void gjab_connected(gpointer data, gint source, GaimInputCondition cond) |
2086 | 493 { |
494 xmlnode x; | |
495 char *t, *t2; | |
496 struct gaim_connection *gc = data; | |
497 struct jabber_data *jd; | |
2956 | 498 gjconn gjc; |
2086 | 499 |
500 if (!g_slist_find(connections, gc)) { | |
501 close(source); | |
502 return; | |
503 } | |
504 | |
505 jd = gc->proto_data; | |
2956 | 506 gjc = jd->gjc; |
507 | |
508 if (gjc->fd != source) | |
509 gjc->fd = source; | |
2300
d2686f757d6e
[gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2289
diff
changeset
|
510 |
2086 | 511 if (source == -1) { |
512 STATE_EVT(JCONN_STATE_OFF) | |
513 return; | |
514 } | |
515 | |
2956 | 516 gjc->state = JCONN_STATE_CONNECTED; |
2086 | 517 STATE_EVT(JCONN_STATE_CONNECTED) |
518 | |
519 /* start stream */ | |
2956 | 520 x = jutil_header(NS_CLIENT, gjc->user->server); |
2086 | 521 t = xmlnode2str(x); |
522 /* this is ugly, we can create the string here instead of jutil_header */ | |
523 /* what do you think about it? -madcat */ | |
524 t2 = strstr(t, "/>"); | |
525 *t2++ = '>'; | |
526 *t2 = '\0'; | |
2956 | 527 gjab_send_raw(gjc, "<?xml version='1.0'?>"); |
528 gjab_send_raw(gjc, t); | |
2086 | 529 xmlnode_free(x); |
530 | |
2956 | 531 gjc->state = JCONN_STATE_ON; |
2086 | 532 STATE_EVT(JCONN_STATE_ON); |
533 | |
2956 | 534 gc = GJ_GC(gjc); |
535 gc->inpa = gaim_input_add(gjc->fd, GAIM_INPUT_READ, jabber_callback, gc); | |
2086 | 536 } |
537 | |
2956 | 538 static void gjab_start(gjconn gjc) |
2086 | 539 { |
540 struct aim_user *user; | |
541 int port; | |
542 | |
2956 | 543 if (!gjc || gjc->state != JCONN_STATE_OFF) |
2086 | 544 return; |
545 | |
2956 | 546 user = GJ_GC(gjc)->user; |
2086 | 547 port = user->proto_opt[USEROPT_PORT][0] ? atoi(user->proto_opt[USEROPT_PORT]) : DEFAULT_PORT; |
548 | |
2956 | 549 gjc->parser = XML_ParserCreate(NULL); |
550 XML_SetUserData(gjc->parser, (void *)gjc); | |
551 XML_SetElementHandler(gjc->parser, startElement, endElement); | |
552 XML_SetCharacterDataHandler(gjc->parser, charData); | |
553 | |
554 gjc->fd = proxy_connect(gjc->user->server, port, gjab_connected, GJ_GC(gjc)); | |
555 if (!user->gc || (gjc->fd < 0)) { | |
2086 | 556 STATE_EVT(JCONN_STATE_OFF) |
557 return; | |
558 } | |
559 } | |
560 | |
2956 | 561 /* |
562 * Find chat by chat group name | |
563 */ | |
2086 | 564 static struct conversation *find_chat(struct gaim_connection *gc, char *name) |
565 { | |
566 GSList *bcs = gc->buddy_chats; | |
567 struct conversation *b = NULL; | |
568 char *chat = g_strdup(normalize(name)); | |
569 | |
570 while (bcs) { | |
571 b = bcs->data; | |
572 if (!strcasecmp(normalize(b->name), chat)) | |
573 break; | |
574 b = NULL; | |
575 bcs = bcs->next; | |
576 } | |
577 | |
578 g_free(chat); | |
579 return b; | |
580 } | |
581 | |
2956 | 582 /* |
583 * Find chat by "chat id" | |
584 * | |
585 * Returns: 0 on success and jabber_chat pointer set | |
586 * or -EINVAL on error and jabber_chat pointer is | |
587 * undefined. | |
588 * | |
589 * TBD: Slogging through the buddy_chats list seems | |
590 * redundant since the chat i.d. is mirrored in the | |
591 * jabber_chat struct list. But that's the way it | |
592 * was, so that's the way I'm leaving it--for now. | |
593 */ | |
594 static int jabber_find_chat_by_convo_id(struct gaim_connection *gc, int id, struct jabber_chat **jc) | |
2086 | 595 { |
2956 | 596 GSList *bcs = gc->buddy_chats; |
597 struct conversation *b = NULL; | |
598 struct jabber_data *jd = gc->proto_data; | |
599 | |
600 *jc = NULL; | |
601 | |
602 while(bcs != NULL) { | |
603 b = bcs->data; | |
604 if (id == b->id) | |
605 break; | |
606 bcs = bcs->next; | |
607 } | |
608 | |
609 if (bcs != NULL) { | |
610 bcs = jd->chats; | |
611 while (bcs != NULL) { | |
612 *jc = bcs->data; | |
613 if ((*jc)->state == JCS_ACTIVE && (*jc)->b == b) | |
614 break; | |
615 bcs = bcs->next; | |
616 } | |
617 } | |
618 | |
619 return(bcs == NULL? -EINVAL : 0); | |
620 } | |
621 | |
622 /* | |
623 * Find any chat | |
624 */ | |
625 static struct jabber_chat *find_any_chat(struct gaim_connection *gc, jid chat) | |
626 { | |
627 GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats; | |
2086 | 628 struct jabber_chat *jc = NULL; |
629 | |
2956 | 630 while (jcs) { |
631 jc = jcs->data; | |
2086 | 632 if (!jid_cmpx(chat, jc->Jid, JID_USER | JID_SERVER)) |
633 break; | |
634 jc = NULL; | |
2956 | 635 jcs = jcs->next; |
2086 | 636 } |
637 | |
638 return jc; | |
639 } | |
640 | |
2956 | 641 |
642 /* | |
643 * Find existing/active Jabber chat | |
644 */ | |
645 static struct jabber_chat *find_existing_chat(struct gaim_connection *gc, jid chat) | |
646 { | |
647 GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats; | |
648 struct jabber_chat *jc = NULL; | |
649 | |
650 while (jcs) { | |
651 jc = jcs->data; | |
652 if (jc->state == JCS_ACTIVE && !jid_cmpx(chat, jc->Jid, JID_USER | JID_SERVER)) | |
653 break; | |
654 jc = NULL; | |
655 jcs = jcs->next; | |
656 } | |
657 | |
658 return jc; | |
659 } | |
660 | |
661 /* | |
662 * Find pending chat | |
663 */ | |
2086 | 664 static struct jabber_chat *find_pending_chat(struct gaim_connection *gc, jid chat) |
665 { | |
2956 | 666 GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats; |
2086 | 667 struct jabber_chat *jc = NULL; |
668 | |
2956 | 669 while (jcs) { |
670 jc = jcs->data; | |
671 if (jc->state == JCS_PENDING && !jid_cmpx(chat, jc->Jid, JID_USER | JID_SERVER)) | |
2086 | 672 break; |
673 jc = NULL; | |
2956 | 674 jcs = jcs->next; |
2086 | 675 } |
676 | |
677 return jc; | |
678 } | |
679 | |
680 static gboolean find_chat_buddy(struct conversation *b, char *name) | |
681 { | |
682 GList *m = b->in_room; | |
683 | |
684 while (m) { | |
685 if (!strcmp(m->data, name)) | |
686 return TRUE; | |
687 m = m->next; | |
688 } | |
689 | |
690 return FALSE; | |
691 } | |
692 | |
2956 | 693 /* |
3236 | 694 * Remove a buddy from the (gaim) buddylist (if he's on it) |
695 */ | |
696 static void jabber_remove_gaim_buddy(struct gaim_connection *gc, char *buddyname) | |
697 { | |
698 struct buddy *b; | |
699 | |
700 if ((b = find_buddy(gc, buddyname)) != NULL) { | |
701 struct group *group; | |
702 | |
703 group = find_group_by_buddy(gc, buddyname); | |
704 debug_printf("removing buddy [1]: %s, from group: %s\n", buddyname, group->name); | |
705 remove_buddy(gc, group, b); | |
706 do_export(gc); | |
707 } | |
708 } | |
709 | |
3257 | 710 static void jabber_change_passwd(struct gaim_connection *gc, char *old, char *new) |
711 { | |
712 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; | |
713 | |
714 if(strcmp(old, gjc->pass)) | |
715 { | |
716 do_error_dialog(_("Incorrect current password! Password NOT Changed!"), | |
717 _("Password Change Error!")); | |
718 } | |
719 else if(!strcmp(old, new)) | |
720 { | |
721 do_error_dialog(_("New password same as old password! Password NOT Changed!"), | |
722 _("Password Change Error!")); | |
723 } | |
724 else | |
725 { | |
726 xmlnode x, y, z; | |
727 char *id; | |
728 | |
729 x = jutil_iqnew(JPACKET__SET, NS_REGISTER); | |
730 xmlnode_put_attrib(x, "to", gjc->user->server); | |
731 y = xmlnode_get_tag(x, "query"); | |
732 z = xmlnode_insert_tag(y, "username"); | |
733 xmlnode_insert_cdata(z, gjc->user->user, -1); | |
734 z = xmlnode_insert_tag(y, "password"); | |
735 xmlnode_insert_cdata(z, new, -1); | |
736 | |
737 id = gjab_getid(gjc); | |
738 xmlnode_put_attrib(x, "id", id); | |
739 | |
740 free(gjc->pass); | |
741 gjc->pass = strdup(new); | |
742 | |
743 g_hash_table_insert(gjc->queries, g_strdup(id), g_strdup("change_password")); | |
744 | |
745 gjab_send(gjc, x); | |
746 xmlnode_free(x); | |
747 } | |
748 } | |
749 | |
3236 | 750 /* |
2956 | 751 * keep track of away msg same as yahoo plugin |
752 */ | |
3105 | 753 static void jabber_track_away(gjconn gjc, jpacket p, char *name, char *type) |
2956 | 754 { |
755 struct jabber_data *jd = GJ_GC(gjc)->proto_data; | |
756 gpointer val = g_hash_table_lookup(jd->hash, name); | |
757 char *show; | |
758 char *vshow = NULL; | |
759 char *status = NULL; | |
760 char *msg = NULL; | |
761 | |
3105 | 762 if (type && (strcasecmp(type, "unavailable") == 0)) { |
763 vshow = _("Unavailable"); | |
764 } else { | |
765 if((show = xmlnode_get_tag_data(p->x, "show")) != NULL) { | |
766 if (!strcasecmp(show, "away")) { | |
767 vshow = _("Away"); | |
768 } else if (!strcasecmp(show, "chat")) { | |
769 vshow = _("Online"); | |
770 } else if (!strcasecmp(show, "xa")) { | |
771 vshow = _("Extended Away"); | |
772 } else if (!strcasecmp(show, "dnd")) { | |
773 vshow = _("Do Not Disturb"); | |
774 } | |
2956 | 775 } |
776 } | |
777 | |
3259 | 778 if (type && (strcasecmp(type, "error") == 0)) { |
779 xmlnode enode; | |
780 | |
781 if((enode = xmlnode_get_tag(p->x, "error")) != NULL) { | |
782 status = g_strdup_printf(_("Error %s: %s"), | |
783 xmlnode_get_attrib(enode, "code"), xmlnode_get_data(enode)); | |
784 } else { | |
785 status = g_strdup(_("Unknown Error in presence")); | |
786 } | |
787 } else { | |
788 status = g_strdup(xmlnode_get_tag_data(p->x, "status")); | |
789 } | |
2956 | 790 |
791 if(vshow != NULL || status != NULL ) { | |
792 /* kinda hokey, but it works :-) */ | |
793 msg = g_strdup_printf("%s%s%s", | |
794 (vshow == NULL? "" : vshow), | |
795 (vshow == NULL || status == NULL? "" : ": "), | |
796 (status == NULL? "" : status)); | |
3105 | 797 } else { |
798 msg = g_strdup(_("Online")); | |
2956 | 799 } |
800 | |
3259 | 801 g_free(status); |
802 | |
2956 | 803 if (val) { |
804 g_free(val); | |
805 g_hash_table_insert(jd->hash, name, msg); | |
806 } else { | |
807 g_hash_table_insert(jd->hash, g_strdup(name), msg); | |
808 } | |
809 } | |
810 | |
3159 | 811 static time_t iso8601_to_time(char *timestamp) |
812 { | |
3229 | 813 struct tm t; |
814 time_t retval = 0; | |
815 | |
816 if(sscanf(timestamp,"%04d%02d%02dT%02d:%02d:%02d", | |
817 &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec)) | |
818 { | |
819 t.tm_year -= 1900; | |
820 t.tm_mon -= 1; | |
821 t.tm_isdst = 0; | |
822 retval = mktime(&t); | |
823 # ifdef HAVE_TM_GMTOFF | |
824 retval += t.tm_gmtoff; | |
825 # else | |
826 # ifdef HAVE_TIMEZONE | |
827 tzset(); /* making sure */ | |
828 retval -= timezone; | |
829 # endif | |
830 # endif | |
831 } | |
832 | |
833 return retval; | |
3159 | 834 } |
835 | |
2956 | 836 static void jabber_handlemessage(gjconn gjc, jpacket p) |
2086 | 837 { |
3159 | 838 xmlnode y, xmlns, subj, z; |
839 time_t time_sent = time(NULL); | |
2086 | 840 |
841 char *from = NULL, *msg = NULL, *type = NULL, *topic = NULL; | |
842 char m[BUF_LONG * 2]; | |
843 | |
844 type = xmlnode_get_attrib(p->x, "type"); | |
845 | |
3159 | 846 z = xmlnode_get_firstchild(p->x); |
847 | |
848 while(z) | |
849 { | |
850 if(NSCHECK(z,NS_DELAY)) | |
851 { | |
852 char *timestamp = xmlnode_get_attrib(z,"stamp"); | |
853 time_sent = iso8601_to_time(timestamp); | |
854 } | |
855 z = xmlnode_get_nextsibling(z); | |
856 } | |
857 | |
2086 | 858 if (!type || !strcasecmp(type, "normal") || !strcasecmp(type, "chat")) { |
859 | |
860 /* XXX namespaces could be handled better. (mid) */ | |
861 if ((xmlns = xmlnode_get_tag(p->x, "x"))) | |
862 type = xmlnode_get_attrib(xmlns, "xmlns"); | |
863 | |
864 from = jid_full(p->from); | |
865 /* | |
866 if ((y = xmlnode_get_tag(p->x, "html"))) { | |
867 msg = xmlnode_get_data(y); | |
868 } else | |
869 */ | |
870 if ((y = xmlnode_get_tag(p->x, "body"))) { | |
871 msg = xmlnode_get_data(y); | |
872 } | |
873 | |
874 msg = utf8_to_str(msg); | |
875 | |
876 if (!from) | |
877 return; | |
878 | |
879 if (type && !strcasecmp(type, "jabber:x:conference")) { | |
880 char *room; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
881 GList *m = NULL; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
882 char **data; |
2086 | 883 |
884 room = xmlnode_get_attrib(xmlns, "jid"); | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
885 data = g_strsplit(room, "@", 2); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
886 m = g_list_append(m, g_strdup(data[0])); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
887 m = g_list_append(m, g_strdup(data[1])); |
2956 | 888 m = g_list_append(m, g_strdup(gjc->user->user)); |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
889 g_strfreev(data); |
2086 | 890 |
2956 | 891 serv_got_chat_invite(GJ_GC(gjc), room, from, msg, m); |
2086 | 892 } else if (msg) { /* whisper */ |
893 struct jabber_chat *jc; | |
894 g_snprintf(m, sizeof(m), "%s", msg); | |
2956 | 895 if (((jc = find_existing_chat(GJ_GC(gjc), p->from)) != NULL) && jc->b) |
3159 | 896 serv_got_chat_in(GJ_GC(gjc), jc->b->id, p->from->resource, 1, m, time_sent); |
2086 | 897 else { |
2278
00a8b7bcef6c
[gaim-migrate @ 2288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2232
diff
changeset
|
898 int flags = 0; |
00a8b7bcef6c
[gaim-migrate @ 2288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2232
diff
changeset
|
899 if (xmlnode_get_tag(p->x, "gaim")) |
00a8b7bcef6c
[gaim-migrate @ 2288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2232
diff
changeset
|
900 flags = IM_FLAG_GAIMUSER; |
2086 | 901 if (find_conversation(jid_full(p->from))) |
3159 | 902 serv_got_im(GJ_GC(gjc), jid_full(p->from), m, flags, time_sent, -1); |
2086 | 903 else { |
2956 | 904 if(p->from->user) { |
905 from = g_strdup_printf("%s@%s", p->from->user, p->from->server); | |
906 } else { | |
907 /* server message? */ | |
908 from = g_strdup(p->from->server); | |
909 } | |
3159 | 910 serv_got_im(GJ_GC(gjc), from, m, flags, time_sent, -1); |
2086 | 911 g_free(from); |
912 } | |
913 } | |
914 } | |
915 | |
916 if (msg) | |
917 g_free(msg); | |
918 | |
919 } else if (!strcasecmp(type, "error")) { | |
920 if ((y = xmlnode_get_tag(p->x, "error"))) { | |
921 type = xmlnode_get_attrib(y, "code"); | |
922 msg = xmlnode_get_data(y); | |
923 } | |
924 | |
925 if (msg) { | |
926 from = g_strdup_printf("Error %s", type ? type : ""); | |
927 do_error_dialog(msg, from); | |
928 g_free(from); | |
929 } | |
930 } else if (!strcasecmp(type, "groupchat")) { | |
931 struct jabber_chat *jc; | |
932 static int i = 0; | |
933 | |
934 /* | |
935 if ((y = xmlnode_get_tag(p->x, "html"))) { | |
936 msg = xmlnode_get_data(y); | |
937 } else | |
938 */ | |
939 if ((y = xmlnode_get_tag(p->x, "body"))) { | |
940 msg = xmlnode_get_data(y); | |
941 } | |
942 | |
943 msg = utf8_to_str(msg); | |
944 | |
945 if ((subj = xmlnode_get_tag(p->x, "subject"))) { | |
946 topic = xmlnode_get_data(subj); | |
947 } | |
948 topic = utf8_to_str(topic); | |
949 | |
2956 | 950 jc = find_existing_chat(GJ_GC(gjc), p->from); |
2086 | 951 if (!jc) { |
952 /* we're not in this chat. are we supposed to be? */ | |
2956 | 953 if ((jc = find_pending_chat(GJ_GC(gjc), p->from)) != NULL) { |
2086 | 954 /* yes, we're supposed to be. so now we are. */ |
2956 | 955 jc->b = serv_got_joined_chat(GJ_GC(gjc), i++, p->from->user); |
2086 | 956 jc->id = jc->b->id; |
2956 | 957 jc->state = JCS_ACTIVE; |
2086 | 958 } else { |
959 /* no, we're not supposed to be. */ | |
960 g_free(msg); | |
961 return; | |
962 } | |
963 } | |
964 if (p->from->resource) { | |
965 if (!y) { | |
2956 | 966 if (!find_chat_buddy(jc->b, p->from->resource)) { |
2086 | 967 add_chat_buddy(jc->b, p->from->resource); |
2956 | 968 } else if ((y = xmlnode_get_tag(p->x, "status"))) { |
3105 | 969 char *buf; |
970 | |
971 buf = g_strdup_printf("%s@%s/%s", | |
2956 | 972 p->from->user, p->from->server, p->from->resource); |
3105 | 973 jabber_track_away(gjc, p, buf, NULL); |
974 g_free(buf); | |
2956 | 975 |
2086 | 976 } |
977 } else if (jc->b && msg) { | |
978 char buf[8192]; | |
979 | |
980 if (topic) { | |
981 char tbuf[8192]; | |
982 g_snprintf(tbuf, sizeof(tbuf), "%s", topic); | |
983 chat_set_topic(jc->b, p->from->resource, tbuf); | |
984 } | |
985 | |
986 | |
987 g_snprintf(buf, sizeof(buf), "%s", msg); | |
3159 | 988 serv_got_chat_in(GJ_GC(gjc), jc->b->id, p->from->resource, 0, buf, time_sent); |
2086 | 989 } |
990 } else { /* message from the server */ | |
991 if(jc->b && topic) { | |
992 char tbuf[8192]; | |
993 g_snprintf(tbuf, sizeof(tbuf), "%s", topic); | |
994 chat_set_topic(jc->b, "", tbuf); | |
995 } | |
996 } | |
997 | |
998 g_free(msg); | |
999 g_free(topic); | |
1000 | |
1001 } else { | |
1002 debug_printf("unhandled message %s\n", type); | |
1003 } | |
1004 } | |
3159 | 1005 |
2956 | 1006 static void jabber_handlepresence(gjconn gjc, jpacket p) |
2086 | 1007 { |
1008 char *to, *from, *type; | |
1009 struct buddy *b = NULL; | |
1010 jid who; | |
1011 char *buddy; | |
3194 | 1012 xmlnode y; |
2086 | 1013 char *show; |
2501
227cc42ffa6e
[gaim-migrate @ 2514]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
1014 int state = 0; |
2086 | 1015 GSList *resources; |
1016 char *res; | |
1017 struct conversation *cnv = NULL; | |
1018 struct jabber_chat *jc = NULL; | |
1019 | |
1020 to = xmlnode_get_attrib(p->x, "to"); | |
1021 from = xmlnode_get_attrib(p->x, "from"); | |
1022 type = xmlnode_get_attrib(p->x, "type"); | |
3150 | 1023 |
3259 | 1024 if(type && !strcasecmp(type, "error")) { |
1025 state = UC_ERROR; | |
1026 } else { | |
1027 if ((y = xmlnode_get_tag(p->x, "show"))) { | |
1028 show = xmlnode_get_data(y); | |
1029 if (!show) { | |
1030 state = 0; | |
1031 } else if (!strcasecmp(show, "away")) { | |
1032 state = UC_AWAY; | |
1033 } else if (!strcasecmp(show, "chat")) { | |
1034 state = UC_CHAT; | |
1035 } else if (!strcasecmp(show, "xa")) { | |
1036 state = UC_XA; | |
1037 } else if (!strcasecmp(show, "dnd")) { | |
1038 state = UC_DND; | |
1039 } | |
1040 } else { | |
2501
227cc42ffa6e
[gaim-migrate @ 2514]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
1041 state = 0; |
2086 | 1042 } |
1043 } | |
1044 | |
2956 | 1045 who = jid_new(gjc->p, from); |
2086 | 1046 if (who->user == NULL) { |
1047 /* FIXME: transport */ | |
1048 return; | |
1049 } | |
1050 | |
1051 buddy = g_strdup_printf("%s@%s", who->user, who->server); | |
1052 | |
1053 /* um. we're going to check if it's a chat. if it isn't, and there are pending | |
2956 | 1054 * chats, create the chat. if there aren't pending chats and we don't have the |
1055 * buddy on our list, simply bail out. */ | |
1056 if ((cnv = find_chat(GJ_GC(gjc), who->user)) == NULL) { | |
2086 | 1057 static int i = 0x70; |
2956 | 1058 if ((jc = find_pending_chat(GJ_GC(gjc), who)) != NULL) { |
1059 jc->b = cnv = serv_got_joined_chat(GJ_GC(gjc), i++, who->user); | |
2086 | 1060 jc->id = jc->b->id; |
2956 | 1061 jc->state = JCS_ACTIVE; |
1062 } else if ((b = find_buddy(GJ_GC(gjc), buddy)) == NULL) { | |
1063 g_free(buddy); | |
1064 return; | |
2086 | 1065 } |
1066 } | |
1067 | |
1068 if (!cnv) { | |
1069 resources = b->proto_data; | |
1070 res = who->resource; | |
1071 if (res) | |
1072 while (resources) { | |
3259 | 1073 if (resources->data && !strcmp(res, resources->data)) |
2086 | 1074 break; |
1075 resources = resources->next; | |
1076 } | |
1077 | |
3105 | 1078 /* keep track of away msg same as yahoo plugin */ |
1079 jabber_track_away(gjc, p, normalize(b->name), type); | |
1080 | |
3259 | 1081 if (type && (strcasecmp(type, "unavailable") == 0)) { |
2086 | 1082 if (resources) { |
1083 g_free(resources->data); | |
1084 b->proto_data = g_slist_remove(b->proto_data, resources->data); | |
1085 } | |
1086 if (!b->proto_data) { | |
2956 | 1087 serv_got_update(GJ_GC(gjc), buddy, 0, 0, 0, 0, 0, 0); |
2086 | 1088 } |
1089 } else { | |
1090 if (!resources) { | |
1091 b->proto_data = g_slist_append(b->proto_data, g_strdup(res)); | |
1092 } | |
1093 | |
3194 | 1094 serv_got_update(GJ_GC(gjc), buddy, 1, 0, b->signon, b->idle, state, 0); |
2086 | 1095 |
1096 } | |
1097 } else { | |
1098 if (who->resource) { | |
3105 | 1099 char *buf; |
1100 | |
1101 buf = g_strdup_printf("%s@%s/%s", who->user, who->server, who->resource); | |
1102 jabber_track_away(gjc, p, buf, type); | |
1103 g_free(buf); | |
1104 | |
3259 | 1105 if (type && (!strcasecmp(type, "unavailable"))) { |
2086 | 1106 struct jabber_data *jd; |
2956 | 1107 if (!jc && !(jc = find_existing_chat(GJ_GC(gjc), who))) { |
2086 | 1108 g_free(buddy); |
1109 return; | |
1110 } | |
1111 jd = jc->gc->proto_data; | |
2956 | 1112 /* if it's not ourselves...*/ |
2086 | 1113 if (strcmp(who->resource, jc->Jid->resource) && jc->b) { |
2820
b917845dad3c
[gaim-migrate @ 2833]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2814
diff
changeset
|
1114 remove_chat_buddy(jc->b, who->resource, NULL); |
2086 | 1115 g_free(buddy); |
1116 return; | |
1117 } | |
2956 | 1118 |
1119 jc->state = JCS_CLOSED; | |
1120 serv_got_chat_left(GJ_GC(gjc), jc->id); | |
1121 /* | |
1122 * TBD: put back some day? | |
1123 jd->chats = g_slist_remove(jd->chats, jc); | |
1124 g_free(jc); | |
1125 */ | |
1126 } else { | |
1127 if ((!jc && !(jc = find_existing_chat(GJ_GC(gjc), who))) || !jc->b) { | |
1128 g_free(buddy); | |
1129 return; | |
1130 } | |
1131 if (!find_chat_buddy(jc->b, who->resource)) { | |
2086 | 1132 add_chat_buddy(jc->b, who->resource); |
1133 } | |
1134 } | |
1135 } | |
1136 } | |
1137 | |
1138 g_free(buddy); | |
1139 | |
1140 return; | |
1141 } | |
1142 | |
3229 | 1143 /* |
1144 * Used only by Jabber accept/deny add stuff just below | |
1145 */ | |
1146 struct jabber_add_permit { | |
1147 gjconn gjc; | |
1148 gchar *user; | |
1149 }; | |
1150 | |
1151 /* | |
1152 * Common part for Jabber accept/deny adds | |
1153 * | |
1154 * "type" says whether we'll permit/deny the subscribe request | |
1155 */ | |
1156 static void jabber_accept_deny_add(struct jabber_add_permit *jap, const char *type) | |
1157 { | |
1158 xmlnode g = xmlnode_new_tag("presence"); | |
1159 | |
1160 xmlnode_put_attrib(g, "to", jap->user); | |
1161 xmlnode_put_attrib(g, "type", type); | |
1162 gjab_send(jap->gjc, g); | |
1163 | |
1164 xmlnode_free(g); | |
1165 } | |
1166 | |
1167 /* | |
1168 * Callback from "accept" in do_ask_dialog() invoked by jabber_handles10n() | |
1169 */ | |
1170 static void jabber_accept_add(gpointer w, struct jabber_add_permit *jap) | |
1171 { | |
1172 jabber_accept_deny_add(jap, "subscribed"); | |
1173 /* | |
1174 * If we don't already have the buddy on *our* buddylist, | |
1175 * ask if we want him or her added. | |
1176 */ | |
1177 if(find_buddy(GJ_GC(jap->gjc), jap->user) == NULL) { | |
1178 show_got_added(GJ_GC(jap->gjc), NULL, jap->user, NULL, NULL); | |
1179 } | |
1180 g_free(jap->user); | |
1181 g_free(jap); | |
1182 } | |
1183 | |
1184 /* | |
1185 * Callback from "deny/cancel" in do_ask_dialog() invoked by jabber_handles10n() | |
1186 */ | |
1187 static void jabber_deny_add(gpointer w, struct jabber_add_permit *jap) | |
1188 { | |
1189 jabber_accept_deny_add(jap, "unsubscribed"); | |
1190 g_free(jap->user); | |
1191 g_free(jap); | |
1192 } | |
1193 | |
1194 /* | |
1195 * Handle subscription requests | |
1196 */ | |
2956 | 1197 static void jabber_handles10n(gjconn gjc, jpacket p) |
2086 | 1198 { |
1199 xmlnode g; | |
1200 char *Jid = xmlnode_get_attrib(p->x, "from"); | |
3136 | 1201 char *type = xmlnode_get_attrib(p->x, "type"); |
2086 | 1202 |
1203 g = xmlnode_new_tag("presence"); | |
1204 xmlnode_put_attrib(g, "to", Jid); | |
3229 | 1205 |
1206 if (!strcmp(type, "subscribe")) { | |
1207 /* | |
1208 * A "subscribe to us" request was received - put up the approval dialog | |
1209 */ | |
1210 struct jabber_add_permit *jap = g_new0(struct jabber_add_permit, 1); | |
1211 gchar *msg = g_strdup_printf(_("The user %s wants to add you to their buddy list."), | |
1212 Jid); | |
1213 | |
1214 jap->gjc = gjc; | |
1215 jap->user = g_strdup(Jid); | |
1216 do_ask_dialog(msg, jap, jabber_accept_add, jabber_deny_add); | |
1217 | |
1218 g_free(msg); | |
1219 xmlnode_free(g); /* Never needed it here anyway */ | |
1220 return; | |
1221 | |
1222 } else if (!strcmp(type, "unsubscribe")) { | |
1223 /* | |
1224 * An "unsubscribe to us" was received - simply "approve" it | |
1225 */ | |
2086 | 1226 xmlnode_put_attrib(g, "type", "unsubscribed"); |
3229 | 1227 } else { |
1228 /* | |
1229 * Did we attempt to subscribe to somebody and they do not exist? | |
1230 */ | |
3136 | 1231 if (!strcmp(type, "unsubscribed")) { |
1232 xmlnode y; | |
1233 char *status; | |
1234 if((y = xmlnode_get_tag(p->x, "status")) && (status = xmlnode_get_data(y)) && | |
1235 !strcmp(status, "Not Found")) { | |
1236 char *msg = g_strdup_printf("%s: \"%s\"", _("No such user"), | |
1237 xmlnode_get_attrib(p->x, "from")); | |
1238 do_error_dialog(msg, _("Jabber Error")); | |
1239 g_free(msg); | |
1240 } | |
1241 } | |
1242 | |
2956 | 1243 xmlnode_free(g); |
2086 | 1244 return; |
2956 | 1245 } |
1246 | |
1247 gjab_send(gjc, g); | |
1248 xmlnode_free(g); | |
2086 | 1249 } |
1250 | |
2956 | 1251 /* |
1252 * Pending subscription to a buddy? | |
1253 */ | |
1254 #define BUD_SUB_TO_PEND(sub, ask) ((!strcasecmp((sub), "none") || !strcasecmp((sub), "from")) && \ | |
1255 (ask) != NULL && !strcasecmp((ask), "subscribe")) | |
1256 | |
1257 /* | |
1258 * Subscribed to a buddy? | |
1259 */ | |
1260 #define BUD_SUBD_TO(sub, ask) ((!strcasecmp((sub), "to") || !strcasecmp((sub), "both")) && \ | |
1261 ((ask) == NULL || !strcasecmp((ask), "subscribe"))) | |
1262 | |
1263 /* | |
1264 * Pending unsubscription to a buddy? | |
1265 */ | |
1266 #define BUD_USUB_TO_PEND(sub, ask) ((!strcasecmp((sub), "to") || !strcasecmp((sub), "both")) && \ | |
1267 (ask) != NULL && !strcasecmp((ask), "unsubscribe")) | |
1268 | |
1269 /* | |
1270 * Unsubscribed to a buddy? | |
1271 */ | |
1272 #define BUD_USUBD_TO(sub, ask) ((!strcasecmp((sub), "none") || !strcasecmp((sub), "from")) && \ | |
1273 ((ask) == NULL || !strcasecmp((ask), "unsubscribe"))) | |
1274 | |
1275 /* | |
1276 * If a buddy is added or removed from the roster on another resource | |
1277 * jabber_handlebuddy is called | |
1278 * | |
1279 * Called with roster item node. | |
1280 */ | |
1281 static void jabber_handlebuddy(gjconn gjc, xmlnode x) | |
1282 { | |
1283 xmlnode g; | |
1284 char *Jid, *name, *sub, *ask; | |
1285 jid who; | |
1286 struct buddy *b = NULL; | |
3136 | 1287 char *buddyname, *groupname = NULL; |
2956 | 1288 |
1289 Jid = xmlnode_get_attrib(x, "jid"); | |
1290 name = xmlnode_get_attrib(x, "name"); | |
1291 sub = xmlnode_get_attrib(x, "subscription"); | |
1292 ask = xmlnode_get_attrib(x, "ask"); | |
1293 who = jid_new(gjc->p, Jid); | |
1294 | |
1295 /* JFIXME: jabber_handleroster() had a "FIXME: transport" at this | |
1296 * equivilent point. So... | |
1297 * | |
1298 * We haven't allocated any memory or done anything interesting to | |
1299 * this point, so we'll violate Good Coding Structure here by | |
1300 * simply bailing out. | |
1301 */ | |
1302 if (!who || !who->user) { | |
1303 return; | |
1304 } | |
1305 | |
1306 buddyname = g_strdup_printf("%s@%s", who->user, who->server); | |
1307 | |
3236 | 1308 if((g = xmlnode_get_tag(x, "group")) != NULL) { |
3136 | 1309 groupname = xmlnode_get_data(g); |
2956 | 1310 } |
1311 | |
3059 | 1312 /* |
3136 | 1313 * Add or remove a buddy? Change buddy's alias or group? |
3059 | 1314 */ |
2956 | 1315 if (BUD_SUB_TO_PEND(sub, ask) || BUD_SUBD_TO(sub, ask)) { |
1316 if ((b = find_buddy(GJ_GC(gjc), buddyname)) == NULL) { | |
1317 debug_printf("adding buddy [4]: %s\n", buddyname); | |
3136 | 1318 b = add_buddy(GJ_GC(gjc), groupname ? groupname : _("Buddies"), buddyname, |
2956 | 1319 name ? name : buddyname); |
1320 do_export(GJ_GC(gjc)); | |
3136 | 1321 } else { |
1322 struct group *c_grp = find_group_by_buddy(GJ_GC(gjc), buddyname); | |
1323 | |
1324 /* | |
1325 * If the buddy's in a new group or his/her alias is changed... | |
1326 */ | |
1327 if(groupname && c_grp && strcmp(c_grp->name, groupname)) { | |
1328 int present = b->present; /* save presence state */ | |
1329 int uc = b->uc; /* and away state (?) */ | |
3150 | 1330 int idle = b->idle; |
1331 int signon = b->signon; | |
3136 | 1332 |
1333 /* | |
1334 * seems rude, but it seems to be the only way... | |
1335 */ | |
1336 remove_buddy(GJ_GC(gjc), c_grp, b); | |
1337 b = add_buddy(GJ_GC(gjc), groupname, buddyname, | |
1338 name ? name : buddyname); | |
1339 do_export(GJ_GC(gjc)); | |
1340 if(present) { | |
3150 | 1341 serv_got_update(GJ_GC(gjc), buddyname, 1, 0, signon, idle, uc, 0); |
3136 | 1342 } |
1343 } else if(name != NULL && strcmp(b->show, name)) { | |
1344 strncpy(b->show, name, BUDDY_ALIAS_MAXLEN); | |
1345 b->show[BUDDY_ALIAS_MAXLEN - 1] = '\0'; /* cheap safety feature */ | |
1346 handle_buddy_rename(b, buddyname); | |
1347 } | |
2956 | 1348 } |
1349 } else if (BUD_USUB_TO_PEND(sub, ask) || BUD_USUBD_TO(sub, ask) || !strcasecmp(sub, "remove")) { | |
3236 | 1350 jabber_remove_gaim_buddy(GJ_GC(gjc), buddyname); |
2956 | 1351 } |
1352 g_free(buddyname); | |
1353 | |
1354 } | |
1355 | |
1356 static void jabber_handleroster(gjconn gjc, xmlnode querynode) | |
2086 | 1357 { |
1358 xmlnode x; | |
1359 | |
1360 x = xmlnode_get_firstchild(querynode); | |
1361 while (x) { | |
2956 | 1362 jabber_handlebuddy(gjc, x); |
2086 | 1363 x = xmlnode_get_nextsibling(x); |
1364 } | |
1365 | |
1366 x = jutil_presnew(0, NULL, "Online"); | |
2956 | 1367 gjab_send(gjc, x); |
2086 | 1368 xmlnode_free(x); |
1369 } | |
1370 | |
2956 | 1371 static void jabber_handleauthresp(gjconn gjc, jpacket p) |
2086 | 1372 { |
1373 if (jpacket_subtype(p) == JPACKET__RESULT) { | |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1374 if (xmlnode_has_children(p->x)) { |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1375 xmlnode query = xmlnode_get_tag(p->x, "query"); |
2975 | 1376 set_login_progress(GJ_GC(gjc), 4, _("Authenticating")); |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1377 if (!xmlnode_get_tag(query, "digest")) { |
2956 | 1378 g_free(gjc->sid); |
1379 gjc->sid = NULL; | |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1380 } |
2956 | 1381 gjab_auth(gjc); |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1382 } else { |
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1383 debug_printf("auth success\n"); |
2086 | 1384 |
2956 | 1385 account_online(GJ_GC(gjc)); |
1386 serv_finish_login(GJ_GC(gjc)); | |
1387 | |
1388 if (bud_list_cache_exists(GJ_GC(gjc))) | |
1389 do_import(GJ_GC(gjc), NULL); | |
1390 | |
1391 ((struct jabber_data *)GJ_GC(gjc)->proto_data)->did_import = TRUE; | |
1392 | |
1393 gjab_reqroster(gjc); | |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1394 } |
2086 | 1395 } else { |
1396 xmlnode xerr; | |
1397 char *errmsg = NULL; | |
1398 int errcode = 0; | |
2956 | 1399 struct jabber_data *jd = GJ_GC(gjc)->proto_data; |
2086 | 1400 |
1401 debug_printf("auth failed\n"); | |
1402 xerr = xmlnode_get_tag(p->x, "error"); | |
1403 if (xerr) { | |
1404 char msg[BUF_LONG]; | |
1405 errmsg = xmlnode_get_data(xerr); | |
1406 if (xmlnode_get_attrib(xerr, "code")) { | |
1407 errcode = atoi(xmlnode_get_attrib(xerr, "code")); | |
1408 g_snprintf(msg, sizeof(msg), "Error %d: %s", errcode, errmsg); | |
1409 } else | |
1410 g_snprintf(msg, sizeof(msg), "%s", errmsg); | |
2956 | 1411 hide_login_progress(GJ_GC(gjc), msg); |
2086 | 1412 } else { |
2975 | 1413 hide_login_progress(GJ_GC(gjc), _("Unknown login error")); |
2086 | 1414 } |
1415 | |
2800
0ad63a625eec
[gaim-migrate @ 2813]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2791
diff
changeset
|
1416 jd->die = TRUE; |
2086 | 1417 } |
1418 } | |
1419 | |
2956 | 1420 static void jabber_handleversion(gjconn gjc, xmlnode iqnode) { |
2086 | 1421 xmlnode querynode, x; |
1422 char *id, *from; | |
1423 char os[1024]; | |
1424 struct utsname osinfo; | |
1425 | |
1426 uname(&osinfo); | |
1427 g_snprintf(os, sizeof os, "%s %s %s", osinfo.sysname, osinfo.release, osinfo.machine); | |
1428 | |
1429 id = xmlnode_get_attrib(iqnode, "id"); | |
1430 from = xmlnode_get_attrib(iqnode, "from"); | |
1431 | |
1432 x = jutil_iqnew(JPACKET__RESULT, NS_VERSION); | |
1433 | |
1434 xmlnode_put_attrib(x, "to", from); | |
1435 xmlnode_put_attrib(x, "id", id); | |
1436 querynode = xmlnode_get_tag(x, "query"); | |
1437 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "name"), PACKAGE, -1); | |
1438 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "version"), VERSION, -1); | |
1439 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "os"), os, -1); | |
1440 | |
2956 | 1441 gjab_send(gjc, x); |
2086 | 1442 |
1443 xmlnode_free(x); | |
1444 } | |
1445 | |
2956 | 1446 static void jabber_handletime(gjconn gjc, xmlnode iqnode) { |
2086 | 1447 xmlnode querynode, x; |
1448 char *id, *from; | |
1449 time_t now_t; | |
1450 struct tm *now; | |
1451 char buf[1024]; | |
1452 | |
1453 time(&now_t); | |
1454 now = localtime(&now_t); | |
1455 | |
1456 id = xmlnode_get_attrib(iqnode, "id"); | |
1457 from = xmlnode_get_attrib(iqnode, "from"); | |
1458 | |
1459 x = jutil_iqnew(JPACKET__RESULT, NS_TIME); | |
1460 | |
1461 xmlnode_put_attrib(x, "to", from); | |
1462 xmlnode_put_attrib(x, "id", id); | |
1463 querynode = xmlnode_get_tag(x, "query"); | |
1464 | |
1465 strftime(buf, 1024, "%Y%m%dT%T", now); | |
1466 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "utc"), buf, -1); | |
1467 strftime(buf, 1024, "%Z", now); | |
1468 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "tz"), buf, -1); | |
1469 strftime(buf, 1024, "%d %b %Y %T", now); | |
1470 xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "display"), buf, -1); | |
1471 | |
2956 | 1472 gjab_send(gjc, x); |
2086 | 1473 |
1474 xmlnode_free(x); | |
1475 } | |
1476 | |
2956 | 1477 static void jabber_handlelast(gjconn gjc, xmlnode iqnode) { |
2086 | 1478 xmlnode x, querytag; |
1479 char *id, *from; | |
2956 | 1480 struct jabber_data *jd = GJ_GC(gjc)->proto_data; |
2086 | 1481 char idle_time[32]; |
1482 | |
1483 id = xmlnode_get_attrib(iqnode, "id"); | |
1484 from = xmlnode_get_attrib(iqnode, "from"); | |
1485 | |
1486 x = jutil_iqnew(JPACKET__RESULT, "jabber:iq:last"); | |
1487 | |
1488 xmlnode_put_attrib(x, "to", from); | |
1489 xmlnode_put_attrib(x, "id", id); | |
1490 querytag = xmlnode_get_tag(x, "query"); | |
1491 g_snprintf(idle_time, sizeof idle_time, "%ld", jd->idle ? time(NULL) - jd->idle : 0); | |
1492 xmlnode_put_attrib(querytag, "seconds", idle_time); | |
1493 | |
2956 | 1494 gjab_send(gjc, x); |
2086 | 1495 xmlnode_free(x); |
1496 } | |
1497 | |
2956 | 1498 /* |
1499 * delete == TRUE: delete found entry | |
1500 * | |
1501 * returns pointer to (local) copy of value if found, NULL otherwise | |
1502 * | |
1503 * Note: non-reentrant! Local static storage re-used on subsequent calls. | |
1504 * If you're going to need to keep the returned value, make a copy! | |
1505 */ | |
1506 static gchar *jabber_track_queries(GHashTable *queries, gchar *key, gboolean delete) | |
1507 { | |
1508 gpointer my_key, my_val; | |
1509 static gchar *ret_val = NULL; | |
1510 | |
1511 if(ret_val != NULL) { | |
1512 g_free(ret_val); | |
1513 ret_val = NULL; | |
1514 } | |
1515 | |
1516 /* self-protection */ | |
1517 if(queries != NULL && key != NULL) { | |
1518 if(g_hash_table_lookup_extended(queries, key, &my_key, &my_val)) { | |
1519 ret_val = g_strdup((gchar *) my_val); | |
1520 if(delete) { | |
1521 g_hash_table_remove(queries, key); | |
1522 g_free(my_key); | |
1523 g_free(my_val); | |
1524 } | |
1525 } | |
1526 } | |
1527 | |
1528 return(ret_val); | |
1529 } | |
1530 | |
1531 static void jabber_handlepacket(gjconn gjc, jpacket p) | |
2086 | 1532 { |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1533 char *id; |
2086 | 1534 switch (p->type) { |
1535 case JPACKET_MESSAGE: | |
2956 | 1536 jabber_handlemessage(gjc, p); |
2086 | 1537 break; |
1538 case JPACKET_PRESENCE: | |
2956 | 1539 jabber_handlepresence(gjc, p); |
2086 | 1540 break; |
1541 case JPACKET_IQ: | |
1542 debug_printf("jpacket_subtype: %d\n", jpacket_subtype(p)); | |
1543 | |
2956 | 1544 id = xmlnode_get_attrib(p->x, "id"); |
1545 if (id != NULL && !strcmp(id, IQID_AUTH)) { | |
1546 jabber_handleauthresp(gjc, p); | |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1547 break; |
2086 | 1548 } |
1549 | |
1550 if (jpacket_subtype(p) == JPACKET__SET) { | |
2956 | 1551 xmlnode querynode; |
1552 querynode = xmlnode_get_tag(p->x, "query"); | |
1553 if (NSCHECK(querynode, "jabber:iq:roster")) { | |
1554 jabber_handlebuddy(gjc, xmlnode_get_firstchild(querynode)); | |
1555 } | |
2086 | 1556 } else if (jpacket_subtype(p) == JPACKET__GET) { |
1557 xmlnode querynode; | |
1558 querynode = xmlnode_get_tag(p->x, "query"); | |
2814
f4f9e5a01890
[gaim-migrate @ 2827]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2800
diff
changeset
|
1559 if (NSCHECK(querynode, NS_VERSION)) { |
2956 | 1560 jabber_handleversion(gjc, p->x); |
2086 | 1561 } else if (NSCHECK(querynode, NS_TIME)) { |
2956 | 1562 jabber_handletime(gjc, p->x); |
2086 | 1563 } else if (NSCHECK(querynode, "jabber:iq:last")) { |
2956 | 1564 jabber_handlelast(gjc, p->x); |
2086 | 1565 } |
1566 } else if (jpacket_subtype(p) == JPACKET__RESULT) { | |
1567 xmlnode querynode, vcard; | |
1568 char *xmlns, *from; | |
1569 | |
2956 | 1570 /* |
1571 * TBD: ISTM maybe this part could use a serious re-work? | |
1572 */ | |
2086 | 1573 from = xmlnode_get_attrib(p->x, "from"); |
1574 querynode = xmlnode_get_tag(p->x, "query"); | |
1575 vcard = xmlnode_get_tag(p->x, "vCard"); | |
2316
ebb5ecb2cd5b
[gaim-migrate @ 2326]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2309
diff
changeset
|
1576 if (!vcard) |
ebb5ecb2cd5b
[gaim-migrate @ 2326]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2309
diff
changeset
|
1577 vcard = xmlnode_get_tag(p->x, "VCARD"); |
2086 | 1578 |
1579 if (NSCHECK(querynode, NS_ROSTER)) { | |
2956 | 1580 jabber_handleroster(gjc, querynode); |
2086 | 1581 } else if (NSCHECK(querynode, NS_VCARD)) { |
2956 | 1582 jabber_track_queries(gjc->queries, id, TRUE); /* delete query track */ |
1583 jabber_handlevcard(gjc, querynode, from); | |
2316
ebb5ecb2cd5b
[gaim-migrate @ 2326]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2309
diff
changeset
|
1584 } else if (vcard) { |
2956 | 1585 jabber_track_queries(gjc->queries, id, TRUE); /* delete query track */ |
1586 jabber_handlevcard(gjc, vcard, from); | |
1587 } else if((xmlns = xmlnode_get_attrib(querynode, "xmlns")) != NULL) { | |
1588 debug_printf("jabber:iq:query: %s\n", xmlns); | |
2086 | 1589 } else { |
2956 | 1590 char *val; |
1591 | |
1592 debug_printf("jabber:iq: %s\n", xmlnode2str(p->x)); | |
1593 | |
1594 /* handle "null" query results */ | |
1595 if((val = jabber_track_queries(gjc->queries, id, TRUE)) != NULL) { | |
1596 if(strcmp((char *) val, "vCard") == 0) { | |
1597 /* | |
1598 * No actual vCard, but there's other stuff. This | |
1599 * way the user always gets some kind of response. | |
1600 */ | |
1601 jabber_handlevcard(gjc, NULL, from); | |
3257 | 1602 } else if(!strcmp((char *) val, "change_password")) { |
1603 char buf[BUF_LONG]; | |
1604 sprintf(buf,_("Password successfully changed.")); | |
1605 | |
1606 do_error_dialog(buf, _("Password Change")); | |
2956 | 1607 } |
1608 } | |
2086 | 1609 } |
1610 | |
1611 } else if (jpacket_subtype(p) == JPACKET__ERROR) { | |
1612 xmlnode xerr; | |
1613 char *from, *errmsg = NULL; | |
1614 int errcode = 0; | |
1615 | |
1616 from = xmlnode_get_attrib(p->x, "from"); | |
1617 xerr = xmlnode_get_tag(p->x, "error"); | |
1618 if (xerr) { | |
1619 errmsg = xmlnode_get_data(xerr); | |
1620 if (xmlnode_get_attrib(xerr, "code")) | |
1621 errcode = atoi(xmlnode_get_attrib(xerr, "code")); | |
1622 } | |
1623 | |
1624 from = g_strdup_printf("Error %d (%s)", errcode, from); | |
1625 do_error_dialog(errmsg, from); | |
1626 g_free(from); | |
1627 | |
1628 } | |
1629 | |
1630 break; | |
1631 case JPACKET_S10N: | |
2956 | 1632 jabber_handles10n(gjc, p); |
2086 | 1633 break; |
1634 default: | |
1635 debug_printf("jabber: packet type %d (%s)\n", p->type, xmlnode2str(p->x)); | |
1636 } | |
1637 | |
1638 xmlnode_free(p->x); | |
1639 | |
1640 return; | |
1641 } | |
1642 | |
2956 | 1643 static void jabber_handlestate(gjconn gjc, int state) |
2086 | 1644 { |
1645 switch (state) { | |
1646 case JCONN_STATE_OFF: | |
3074 | 1647 if(gjc->was_connected) { |
1648 hide_login_progress_error(GJ_GC(gjc), _("Connection lost")); | |
1649 } else { | |
1650 hide_login_progress(GJ_GC(gjc), _("Unable to connect")); | |
1651 } | |
2956 | 1652 signoff(GJ_GC(gjc)); |
2086 | 1653 break; |
1654 case JCONN_STATE_CONNECTED: | |
3074 | 1655 gjc->was_connected = 1; |
2975 | 1656 set_login_progress(GJ_GC(gjc), 2, _("Connected")); |
2086 | 1657 break; |
1658 case JCONN_STATE_ON: | |
2975 | 1659 set_login_progress(GJ_GC(gjc), 3, _("Requesting Authentication Method")); |
2956 | 1660 gjab_reqauth(gjc); |
2086 | 1661 break; |
1662 default: | |
1663 debug_printf("state change: %d\n", state); | |
1664 } | |
1665 return; | |
1666 } | |
1667 | |
1668 static void jabber_login(struct aim_user *user) | |
1669 { | |
1670 struct gaim_connection *gc = new_gaim_conn(user); | |
1671 struct jabber_data *jd = gc->proto_data = g_new0(struct jabber_data, 1); | |
1672 char *loginname = create_valid_jid(user->username, DEFAULT_SERVER, "GAIM"); | |
1673 | |
1674 jd->hash = g_hash_table_new(g_str_hash, g_str_equal); | |
2956 | 1675 jd->chats = NULL; /* we have no chats yet */ |
2086 | 1676 |
2975 | 1677 set_login_progress(gc, 1, _("Connecting")); |
2086 | 1678 |
2956 | 1679 if (!(jd->gjc = gjab_new(loginname, user->password, gc))) { |
2086 | 1680 g_free(loginname); |
1681 debug_printf("jabber: unable to connect (jab_new failed)\n"); | |
2975 | 1682 hide_login_progress(gc, _("Unable to connect")); |
2086 | 1683 signoff(gc); |
1684 return; | |
1685 } | |
1686 | |
1687 g_free(loginname); | |
2956 | 1688 gjab_state_handler(jd->gjc, jabber_handlestate); |
1689 gjab_packet_handler(jd->gjc, jabber_handlepacket); | |
1690 jd->gjc->queries = g_hash_table_new(g_str_hash, g_str_equal); | |
1691 gjab_start(jd->gjc); | |
2086 | 1692 } |
1693 | |
1694 static gboolean jabber_destroy_hash(gpointer key, gpointer val, gpointer data) { | |
1695 g_free(key); | |
1696 g_free(val); | |
1697 return TRUE; | |
1698 } | |
1699 | |
1700 static gboolean jabber_free(gpointer data) | |
1701 { | |
2956 | 1702 struct jabber_data *jd = data; |
1703 | |
3236 | 1704 if(jd->gjc != NULL) { |
1705 gjab_delete(jd->gjc); | |
1706 g_free(jd->gjc->sid); | |
1707 jd->gjc = NULL; | |
1708 } | |
2956 | 1709 g_free(jd); |
1710 | |
2086 | 1711 return FALSE; |
1712 } | |
1713 | |
1714 static void jabber_close(struct gaim_connection *gc) | |
1715 { | |
1716 struct jabber_data *jd = gc->proto_data; | |
2956 | 1717 |
1718 if(jd) { | |
1719 GSList *jcs = jd->chats; | |
1720 | |
1721 /* Free-up the jabber_chat struct allocs and the list */ | |
1722 while (jcs) { | |
1723 g_free(jcs->data); | |
1724 jcs = jcs->next; | |
1725 } | |
1726 g_slist_free(jd->chats); | |
1727 | |
1728 /* Free-up the away status memories and the list */ | |
1729 if(jd->hash != NULL) { | |
1730 g_hash_table_foreach_remove(jd->hash, jabber_destroy_hash, NULL); | |
1731 g_hash_table_destroy(jd->hash); | |
1732 jd->hash = NULL; | |
1733 } | |
1734 | |
1735 /* Free-up the pending queries memories and the list */ | |
3236 | 1736 if(jd->gjc != NULL && jd->gjc->queries != NULL) { |
2956 | 1737 g_hash_table_foreach_remove(jd->gjc->queries, jabber_destroy_hash, NULL); |
1738 g_hash_table_destroy(jd->gjc->queries); | |
1739 jd->gjc->queries = NULL; | |
1740 } | |
1741 } | |
2300
d2686f757d6e
[gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2289
diff
changeset
|
1742 if (gc->inpa) |
d2686f757d6e
[gaim-migrate @ 2310]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2289
diff
changeset
|
1743 gaim_input_remove(gc->inpa); |
2956 | 1744 |
1745 if(jd) { | |
1746 g_timeout_add(50, jabber_free, jd); | |
3236 | 1747 if(jd->gjc != NULL) |
1748 xmlnode_free(jd->gjc->current); | |
2956 | 1749 } |
2086 | 1750 gc->proto_data = NULL; |
1751 } | |
1752 | |
3033 | 1753 static int jabber_send_im(struct gaim_connection *gc, char *who, char *message, int len, int flags) |
2086 | 1754 { |
1755 xmlnode x, y; | |
1756 char *realwho; | |
2956 | 1757 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
2086 | 1758 |
1759 if (!who || !message) | |
2123
56c4382f2909
[gaim-migrate @ 2133]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2112
diff
changeset
|
1760 return 0; |
2086 | 1761 |
1762 x = xmlnode_new_tag("message"); | |
2956 | 1763 /* Bare username and "username" not the server itself? */ |
1764 if (!strchr(who, '@') && strcmp(who, gjc->user->server) != 0) | |
1765 realwho = g_strdup_printf("%s@%s", who, gjc->user->server); | |
2086 | 1766 else |
1767 realwho = g_strdup(who); | |
1768 xmlnode_put_attrib(x, "to", realwho); | |
1769 g_free(realwho); | |
1770 | |
2278
00a8b7bcef6c
[gaim-migrate @ 2288]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2232
diff
changeset
|
1771 xmlnode_insert_tag(x, "gaim"); |
2086 | 1772 xmlnode_put_attrib(x, "type", "chat"); |
1773 | |
1774 if (message && strlen(message)) { | |
1775 char *utf8 = str_to_utf8(message); | |
1776 y = xmlnode_insert_tag(x, "body"); | |
1777 xmlnode_insert_cdata(y, utf8, -1); | |
1778 g_free(utf8); | |
1779 } | |
1780 | |
2956 | 1781 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
2086 | 1782 xmlnode_free(x); |
2303
f5bf315e6104
[gaim-migrate @ 2313]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2300
diff
changeset
|
1783 return 1; |
2086 | 1784 } |
1785 | |
3105 | 1786 /* |
1787 * Add/update buddy's roster entry on server | |
1788 */ | |
1789 static void jabber_roster_update(struct gaim_connection *gc, char *name) | |
1790 { | |
1791 xmlnode x, y; | |
1792 char *realwho; | |
1793 gjconn gjc; | |
1794 struct buddy *buddy = NULL; | |
1795 struct group *buddy_group = NULL; | |
1796 | |
1797 if(gc && gc->proto_data && ((struct jabber_data *)gc->proto_data)->gjc && name) { | |
1798 gjc = ((struct jabber_data *)gc->proto_data)->gjc; | |
1799 | |
1800 if (!strchr(name, '@')) | |
1801 realwho = g_strdup_printf("%s@%s", name, gjc->user->server); | |
1802 else { | |
1803 jid who = jid_new(gjc->p, name); | |
1804 if (who->user == NULL) { | |
1805 /* FIXME: transport */ | |
1806 return; | |
1807 } | |
1808 realwho = g_strdup_printf("%s@%s", who->user, who->server); | |
1809 } | |
1810 | |
1811 | |
1812 x = jutil_iqnew(JPACKET__SET, NS_ROSTER); | |
1813 y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item"); | |
1814 xmlnode_put_attrib(y, "jid", realwho); | |
1815 | |
1816 | |
1817 /* If we can find the buddy, there's an alias for him, it's not 0-length | |
1818 * and it doesn't match his JID, add the "name" attribute. | |
1819 */ | |
1820 if((buddy = find_buddy(gc, realwho)) != NULL && | |
3136 | 1821 buddy->show != NULL && buddy->show[0] != '\0' && strcmp(realwho, buddy->show)) { |
3105 | 1822 |
1823 xmlnode_put_attrib(y, "name", buddy->show); | |
1824 } | |
1825 | |
1826 /* | |
1827 * Find out what group the buddy's in and send that along | |
1828 * with the roster item. | |
1829 */ | |
1830 if((buddy_group = find_group_by_buddy(gc, realwho)) != NULL) { | |
1831 xmlnode z; | |
1832 z = xmlnode_insert_tag(y, "group"); | |
1833 xmlnode_insert_cdata(z, buddy_group->name, -1); | |
1834 } | |
1835 | |
1836 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); | |
1837 | |
1838 xmlnode_free(x); | |
1839 g_free(realwho); | |
1840 } | |
1841 } | |
1842 | |
3136 | 1843 /* |
1844 * Change buddy's group on server roster | |
1845 */ | |
1846 static void jabber_group_change(struct gaim_connection *gc, char *name, char *old_group, char *new_group) | |
1847 { | |
1848 if(strcmp(old_group, new_group)) { | |
1849 jabber_roster_update(gc, name); | |
1850 } | |
1851 } | |
1852 | |
2086 | 1853 static void jabber_add_buddy(struct gaim_connection *gc, char *name) |
1854 { | |
3136 | 1855 xmlnode x; |
2086 | 1856 char *realwho; |
2956 | 1857 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
2086 | 1858 |
1859 if (!((struct jabber_data *)gc->proto_data)->did_import) | |
1860 return; | |
1861 | |
1862 if (!name) | |
1863 return; | |
1864 | |
1865 if (!strcmp(gc->username, name)) | |
1866 return; | |
1867 | |
1868 if (!strchr(name, '@')) | |
2956 | 1869 realwho = g_strdup_printf("%s@%s", name, gjc->user->server); |
2086 | 1870 else { |
3236 | 1871 jid who; |
1872 | |
1873 if((who = jid_new(gjc->p, name)) == NULL) { | |
1874 char *msg = g_strdup_printf("%s: \"%s\"", _("Invalid Jabber I.D."), name); | |
1875 do_error_dialog(msg, _("Jabber Error")); | |
1876 g_free(msg); | |
1877 jabber_remove_gaim_buddy(gc, name); | |
1878 return; | |
1879 } | |
2086 | 1880 if (who->user == NULL) { |
1881 /* FIXME: transport */ | |
1882 return; | |
1883 } | |
1884 realwho = g_strdup_printf("%s@%s", who->user, who->server); | |
1885 } | |
1886 | |
1887 x = xmlnode_new_tag("presence"); | |
1888 xmlnode_put_attrib(x, "to", realwho); | |
1889 xmlnode_put_attrib(x, "type", "subscribe"); | |
2956 | 1890 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
1891 xmlnode_free(x); | |
2086 | 1892 |
3105 | 1893 jabber_roster_update(gc, realwho); |
1894 | |
2086 | 1895 g_free(realwho); |
1896 } | |
1897 | |
2681
37d80035e77f
[gaim-migrate @ 2694]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2635
diff
changeset
|
1898 static void jabber_remove_buddy(struct gaim_connection *gc, char *name, char *group) |
2086 | 1899 { |
3048 | 1900 xmlnode x; |
2086 | 1901 char *realwho; |
2956 | 1902 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
2086 | 1903 |
1904 if (!name) | |
1905 return; | |
1906 | |
1907 if (!strchr(name, '@')) | |
2956 | 1908 realwho = g_strdup_printf("%s@%s", name, gjc->user->server); |
2086 | 1909 else |
1910 realwho = g_strdup(name); | |
1911 | |
2956 | 1912 x = xmlnode_new_tag("presence"); |
1913 xmlnode_put_attrib(x, "to", realwho); | |
1914 xmlnode_put_attrib(x, "type", "unsubscribe"); | |
1915 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); | |
2086 | 1916 g_free(realwho); |
1917 xmlnode_free(x); | |
1918 } | |
1919 | |
1920 static char **jabber_list_icon(int uc) | |
1921 { | |
1922 switch (uc) { | |
1923 case UC_AWAY: | |
1924 return available_away_xpm; | |
1925 case UC_CHAT: | |
1926 return available_chat_xpm; | |
1927 case UC_XA: | |
1928 return available_xa_xpm; | |
1929 case UC_DND: | |
1930 return available_dnd_xpm; | |
3259 | 1931 case UC_ERROR: |
1932 return available_error_xpm; | |
2086 | 1933 default: |
1934 return available_xpm; | |
1935 } | |
1936 } | |
1937 | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1938 static GList *jabber_chat_info(struct gaim_connection *gc) |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1939 { |
2956 | 1940 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
1941 | |
1942 static char *confserv = NULL; /* this pointer must be persistent */ | |
1943 gchar *server; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1944 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1945 GList *m = NULL; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1946 struct proto_chat_entry *pce; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1947 |
2956 | 1948 /* This is a scientific wild-ass guess... |
1949 * | |
1950 * If there are more than two "components" to the current server name, | |
1951 * lop-off the left-most component and replace with "conference." | |
1952 */ | |
1953 if(confserv != NULL) { | |
1954 g_free(confserv); /* dispose of the old value */ | |
1955 } | |
1956 | |
1957 if((server = g_strdup(gjc->user->server)) == NULL) { | |
1958 confserv = g_strdup(DEFAULT_GROUPCHAT); | |
1959 } else { | |
1960 gchar **splits, **index; | |
1961 gchar *tmp; | |
1962 int cnt = 0; | |
1963 | |
1964 | |
1965 index = splits = g_strsplit(server, ".", -1); /* split the connected server */ | |
1966 | |
1967 while(*(index++)) /* index to the end--counting the parts */ | |
1968 ++cnt; | |
1969 | |
1970 /* | |
1971 * If we've more than two parts, point to the second part. Else point | |
1972 * to the start. | |
1973 */ | |
1974 if(cnt > 2) { | |
1975 index -= cnt; | |
1976 } else { | |
1977 index = splits; | |
1978 } | |
1979 | |
1980 /* Put it together */ | |
1981 confserv = g_strjoin(".", "conference", (tmp = g_strjoinv(".", index)), NULL); | |
1982 | |
1983 g_free(server); /* we don't need this stuff no more */ | |
1984 g_free(tmp); | |
1985 g_strfreev(splits); | |
1986 } | |
1987 | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1988 pce = g_new0(struct proto_chat_entry, 1); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1989 pce->label = _("Room:"); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1990 m = g_list_append(m, pce); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1991 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1992 pce = g_new0(struct proto_chat_entry, 1); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1993 pce->label = _("Server:"); |
2956 | 1994 pce->def = confserv; |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1995 m = g_list_append(m, pce); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1996 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1997 pce = g_new0(struct proto_chat_entry, 1); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
1998 pce->label = _("Handle:"); |
2956 | 1999 pce->def = gjc->user->user; |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2000 m = g_list_append(m, pce); |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2001 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2002 return m; |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2003 } |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2004 |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2005 static void jabber_join_chat(struct gaim_connection *gc, GList *data) |
2086 | 2006 { |
2007 xmlnode x; | |
2008 char *realwho; | |
2956 | 2009 gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; |
2010 GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats; | |
2086 | 2011 struct jabber_chat *jc; |
2956 | 2012 jid Jid; |
2086 | 2013 |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2014 if (!data || !data->next || !data->next->next) |
2086 | 2015 return; |
2016 | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2017 realwho = create_valid_jid(data->data, data->next->data, |
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2018 data->next->next->data); |
2086 | 2019 debug_printf("%s\n", realwho); |
2020 | |
3236 | 2021 if((Jid = jid_new(gjc->p, realwho)) == NULL) { |
2022 char *msg = g_strdup_printf("%s: \"%s\"", _("Invalid Jabber I.D."), realwho); | |
2023 do_error_dialog(msg, _("Jabber Error")); | |
2024 g_free(msg); | |
2025 return; | |
2026 } | |
2956 | 2027 |
2028 if((jc = find_any_chat(gc, Jid)) != NULL) { | |
2029 free(Jid); /* don't need it, after all */ | |
2030 switch(jc->state) { | |
2031 case JCS_PENDING: | |
2032 debug_printf("attempt to re-join already pending Jabber chat! (ignoring)\n"); | |
2033 g_free(realwho); /* yuck! */ | |
2034 return; | |
2035 case JCS_ACTIVE: | |
2036 debug_printf("attempt to re-join already active Jabber chat! (ignoring)\n"); | |
2037 g_free(realwho); /* yuck! */ | |
2038 return; | |
2039 case JCS_CLOSED: | |
2040 debug_printf("rejoining previously closed Jabber chat\n"); | |
2041 break; | |
2042 default: | |
2043 debug_printf("found Jabber chat in unknown state! (ignoring)\n"); | |
2044 g_free(realwho); /* yuck! */ | |
2045 return; | |
2046 } | |
2047 } else { | |
2048 debug_printf("joining completely new Jabber chat\n"); | |
2049 jc = g_new0(struct jabber_chat, 1); | |
2050 jc->Jid = Jid; | |
2051 jc->gc = gc; | |
2052 ((struct jabber_data *)gc->proto_data)->chats = g_slist_append(jcs, jc); | |
2053 } | |
2054 | |
2055 jc->state = JCS_PENDING; | |
2056 | |
2086 | 2057 x = jutil_presnew(0, realwho, NULL); |
2956 | 2058 gjab_send(gjc, x); |
2086 | 2059 xmlnode_free(x); |
2060 g_free(realwho); | |
2061 } | |
2062 | |
2063 static void jabber_chat_invite(struct gaim_connection *gc, int id, char *message, char *name) | |
2064 { | |
2065 xmlnode x, y; | |
2066 struct jabber_data *jd = gc->proto_data; | |
2956 | 2067 gjconn gjc = jd->gjc; |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2068 struct jabber_chat *jc = NULL; |
2086 | 2069 char *realwho, *subject; |
2070 | |
2071 if (!name) | |
2072 return; | |
2073 | |
2074 /* find which chat we're inviting to */ | |
2956 | 2075 if(jabber_find_chat_by_convo_id(gc, id, &jc) != 0) |
2086 | 2076 return; |
2077 | |
2078 x = xmlnode_new_tag("message"); | |
2079 if (!strchr(name, '@')) | |
2956 | 2080 realwho = g_strdup_printf("%s@%s", name, gjc->user->server); |
2086 | 2081 else |
2082 realwho = g_strdup(name); | |
2083 xmlnode_put_attrib(x, "to", realwho); | |
2084 g_free(realwho); | |
2085 | |
2086 y = xmlnode_insert_tag(x, "x"); | |
2087 xmlnode_put_attrib(y, "xmlns", "jabber:x:conference"); | |
2088 subject = g_strdup_printf("%s@%s", jc->Jid->user, jc->Jid->server); | |
2089 xmlnode_put_attrib(y, "jid", subject); | |
2090 g_free(subject); | |
2091 | |
2092 if (message && strlen(message)) { | |
2093 char *utf8 = str_to_utf8(message); | |
2094 y = xmlnode_insert_tag(x, "body"); | |
2095 xmlnode_insert_cdata(y, utf8, -1); | |
2096 g_free(utf8); | |
2097 } | |
2098 | |
2956 | 2099 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
2086 | 2100 xmlnode_free(x); |
2101 } | |
2102 | |
2103 static void jabber_chat_leave(struct gaim_connection *gc, int id) | |
2104 { | |
2105 struct jabber_data *jd = gc->proto_data; | |
2956 | 2106 gjconn gjc = jd->gjc; |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2107 struct jabber_chat *jc = NULL; |
2086 | 2108 char *realwho; |
2109 xmlnode x; | |
2110 | |
2956 | 2111 /* Find out which chat we're leaving */ |
2112 if(jabber_find_chat_by_convo_id(gc, id, &jc) != 0) | |
2086 | 2113 return; |
2114 | |
2115 realwho = g_strdup_printf("%s@%s", jc->Jid->user, jc->Jid->server); | |
2116 x = jutil_presnew(0, realwho, NULL); | |
2117 g_free(realwho); | |
2118 xmlnode_put_attrib(x, "type", "unavailable"); | |
2956 | 2119 gjab_send(gjc, x); |
2086 | 2120 xmlnode_free(x); |
2121 jc->b = NULL; | |
2122 } | |
2123 | |
2167
edf8c5a70e5b
[gaim-migrate @ 2177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2162
diff
changeset
|
2124 static int jabber_chat_send(struct gaim_connection *gc, int id, char *message) |
2086 | 2125 { |
2126 xmlnode x, y; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2127 struct jabber_chat *jc = NULL; |
2086 | 2128 char *chatname; |
2956 | 2129 int retval = 0; |
2130 | |
2131 /* Find out which chat we're sending to */ | |
2132 if((retval = jabber_find_chat_by_convo_id(gc, id, &jc)) != 0) | |
2133 return(retval); | |
2086 | 2134 |
2135 x = xmlnode_new_tag("message"); | |
2136 xmlnode_put_attrib(x, "from", jid_full(jc->Jid)); | |
2137 chatname = g_strdup_printf("%s@%s", jc->Jid->user, jc->Jid->server); | |
2138 xmlnode_put_attrib(x, "to", chatname); | |
2139 g_free(chatname); | |
2140 xmlnode_put_attrib(x, "type", "groupchat"); | |
2141 | |
2289
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2142 if (message && strlen(message) > strlen("/topic ") && |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2143 !g_strncasecmp(message, "/topic ", strlen("/topic "))) { |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2144 char buf[8192]; |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2145 char *utf8 = str_to_utf8(message + strlen("/topic ")); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2146 y = xmlnode_insert_tag(x, "subject"); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2147 xmlnode_insert_cdata(y, utf8, -1); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2148 y = xmlnode_insert_tag(x, "body"); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2149 g_snprintf(buf, sizeof(buf), "/me has changed the subject to: %s", utf8); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2150 xmlnode_insert_cdata(y, buf, -1); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2151 g_free(utf8); |
38e156136896
[gaim-migrate @ 2299]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2278
diff
changeset
|
2152 } else if (message && strlen(message)) { |
2086 | 2153 char *utf8 = str_to_utf8(message); |
2154 y = xmlnode_insert_tag(x, "body"); | |
2155 xmlnode_insert_cdata(y, utf8, -1); | |
2156 g_free(utf8); | |
2157 } | |
2158 | |
2956 | 2159 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
2086 | 2160 xmlnode_free(x); |
2167
edf8c5a70e5b
[gaim-migrate @ 2177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2162
diff
changeset
|
2161 return 0; |
2086 | 2162 } |
2163 | |
2164 static void jabber_chat_whisper(struct gaim_connection *gc, int id, char *who, char *message) | |
2165 { | |
2166 xmlnode x, y; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
2167 struct jabber_chat *jc = NULL; |
2086 | 2168 char *chatname; |
2169 | |
2956 | 2170 /* Find out which chat we're whispering to */ |
2171 if(jabber_find_chat_by_convo_id(gc, id, &jc) != 0) | |
2086 | 2172 return; |
2173 | |
2174 x = xmlnode_new_tag("message"); | |
2175 xmlnode_put_attrib(x, "from", jid_full(jc->Jid)); | |
2176 chatname = g_strdup_printf("%s@%s/%s", jc->Jid->user, jc->Jid->server, who); | |
2177 xmlnode_put_attrib(x, "to", chatname); | |
2178 g_free(chatname); | |
2179 xmlnode_put_attrib(x, "type", "normal"); | |
2180 | |
2181 if (message && strlen(message)) { | |
2182 char *utf8 = str_to_utf8(message); | |
2183 y = xmlnode_insert_tag(x, "body"); | |
2184 xmlnode_insert_cdata(y, utf8, -1); | |
2185 g_free(utf8); | |
2186 } | |
2187 | |
2956 | 2188 gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); |
2086 | 2189 xmlnode_free(x); |
2190 } | |
2191 | |
2192 static char *jabber_normalize(const char *s) | |
2193 { | |
2194 static char buf[BUF_LEN]; | |
2195 char *t, *u; | |
2196 int x = 0; | |
2197 | |
2198 g_return_val_if_fail((s != NULL), NULL); | |
2199 | |
2956 | 2200 /* Somebody called us with s == NULL once... */ |
2201 if(s == NULL) { | |
2202 return(NULL); | |
2203 } else { | |
2204 u = t = g_strdup(s); | |
2205 | |
2206 g_strdown(t); | |
2207 | |
2208 while (*t && (x < BUF_LEN - 1)) { | |
2209 if (*t != ' ') | |
2210 buf[x++] = *t; | |
2211 t++; | |
2212 } | |
2213 buf[x] = '\0'; | |
2214 g_free(u); | |
2215 | |
2216 if (!strchr(buf, '@')) { | |
2217 strcat(buf, "@jabber.org"); /* this isn't always right, but eh */ | |
2218 } else if ((u = strchr(strchr(buf, '@'), '/')) != NULL) { | |
2219 *u = '\0'; | |
2220 } | |
2221 | |
2222 return buf; | |
2086 | 2223 } |
2224 } | |
2225 | |
2226 static void jabber_get_info(struct gaim_connection *gc, char *who) { | |
2227 xmlnode x; | |
2228 char *id; | |
2956 | 2229 char *realwho; |
2086 | 2230 struct jabber_data *jd = gc->proto_data; |
2956 | 2231 gjconn gjc = jd->gjc; |
2086 | 2232 |
2233 x = jutil_iqnew(JPACKET__GET, NS_VCARD); | |
2956 | 2234 /* Bare username? */ |
2235 if (!strchr(who, '@')) { | |
2236 realwho = g_strdup_printf("%s@%s", who, gjc->user->server); | |
2237 } else { | |
2238 realwho = g_strdup(who); | |
2239 } | |
2240 xmlnode_put_attrib(x, "to", realwho); | |
2241 g_free(realwho); | |
2242 | |
2243 id = gjab_getid(gjc); | |
2086 | 2244 xmlnode_put_attrib(x, "id", id); |
2245 | |
2956 | 2246 g_hash_table_insert(jd->gjc->queries, g_strdup(id), g_strdup("vCard")); |
2247 | |
2248 gjab_send(gjc, x); | |
2086 | 2249 |
2250 xmlnode_free(x); | |
2251 | |
2252 } | |
2253 | |
2956 | 2254 static void jabber_get_away_msg(struct gaim_connection *gc, char *who) { |
2255 struct jabber_data *jd = gc->proto_data; | |
2256 gjconn gjc = jd->gjc; | |
2257 char *status; | |
2258 | |
2259 /* space for all elements: Jabber I.D. + "status" + NULL (list terminator) */ | |
2260 gchar **str_arr = (gchar **) g_new(gpointer, 3); | |
2261 gchar **ap = str_arr; | |
2262 gchar *realwho, *final; | |
2263 | |
2264 /* Bare username? */ | |
2265 if (!strchr(who, '@')) { | |
2266 realwho = g_strdup_printf("%s@%s", who, gjc->user->server); | |
2267 } else { | |
2268 realwho = g_strdup(who); | |
2269 } | |
2270 *ap++ = g_strdup_printf("<B>Jabber ID:</B> %s<BR>\n", realwho); | |
2271 | |
2272 if((status = g_hash_table_lookup(jd->hash, realwho)) == NULL) { | |
3105 | 2273 status = _("Unknown"); |
2956 | 2274 } |
2275 *ap++ = g_strdup_printf("<B>Status:</B> %s<BR>\n", status); | |
2276 | |
2277 *ap = NULL; | |
2278 | |
2279 final= g_strjoinv(NULL, str_arr); | |
2280 g_strfreev(str_arr); | |
2281 | |
2282 g_show_info_text(gc, realwho, 2, final, NULL); | |
2283 g_free(realwho); | |
2284 g_free(final); | |
2285 | |
2286 } | |
2287 | |
2288 static void jabber_get_cb_info(struct gaim_connection *gc, int cid, char *who) { | |
2289 struct jabber_chat *jc = NULL; | |
2290 char *realwho; | |
2291 | |
2292 /* Find out which chat */ | |
2293 if(jabber_find_chat_by_convo_id(gc, cid, &jc) != 0) | |
2294 return; | |
2295 | |
2296 realwho = g_strdup_printf("%s@%s/%s", jc->Jid->user, jc->Jid->server, who); | |
2297 | |
2298 jabber_get_info(gc, realwho); | |
2299 g_free(realwho); | |
2300 } | |
2301 | |
2302 static void jabber_get_cb_away_msg(struct gaim_connection *gc, int cid, char *who) { | |
2303 struct jabber_chat *jc = NULL; | |
2304 char *realwho; | |
2305 | |
2306 /* Find out which chat */ | |
2307 if(jabber_find_chat_by_convo_id(gc, cid, &jc) != 0) | |
2308 return; | |
2309 | |
2310 realwho = g_strdup_printf("%s@%s/%s", jc->Jid->user, jc->Jid->server, who); | |
2311 | |
2312 jabber_get_away_msg(gc, realwho); | |
2313 | |
2314 g_free(realwho); | |
2315 | |
2316 } | |
2317 | |
2170
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2318 static GList *jabber_buddy_menu(struct gaim_connection *gc, char *who) { |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2319 GList *m = NULL; |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2320 struct proto_buddy_menu *pbm; |
2086 | 2321 |
2170
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2322 pbm = g_new0(struct proto_buddy_menu, 1); |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2323 pbm->label = _("Get Info"); |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2324 pbm->callback = jabber_get_info; |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2325 pbm->gc = gc; |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2326 m = g_list_append(m, pbm); |
2956 | 2327 pbm = g_new0(struct proto_buddy_menu, 1); |
2328 pbm->label = _("Get Away Msg"); | |
2329 pbm->callback = jabber_get_away_msg; | |
2330 pbm->gc = gc; | |
2331 m = g_list_append(m, pbm); | |
2170
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2332 |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
2333 return m; |
2086 | 2334 } |
2335 | |
2501
227cc42ffa6e
[gaim-migrate @ 2514]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2382
diff
changeset
|
2336 static GList *jabber_away_states(struct gaim_connection *gc) { |
2086 | 2337 GList *m = NULL; |
2338 | |
2339 m = g_list_append(m, "Online"); | |
2340 m = g_list_append(m, "Chatty"); | |
2341 m = g_list_append(m, "Away"); | |
2342 m = g_list_append(m, "Extended Away"); | |
2343 m = g_list_append(m, "Do Not Disturb"); | |
2344 | |
2345 return m; | |
2346 } | |
2347 | |
2348 static void jabber_set_away(struct gaim_connection *gc, char *state, char *message) | |
2349 { | |
2350 xmlnode x, y; | |
2351 struct jabber_data *jd = gc->proto_data; | |
2956 | 2352 gjconn gjc = jd->gjc; |
2086 | 2353 |
2354 gc->away = NULL; /* never send an auto-response */ | |
2355 | |
2356 x = xmlnode_new_tag("presence"); | |
2357 | |
2358 if (!strcmp(state, GAIM_AWAY_CUSTOM)) { | |
2359 /* oh goody. Gaim is telling us what to do. */ | |
2360 if (message) { | |
2361 /* Gaim wants us to be away */ | |
2362 y = xmlnode_insert_tag(x, "show"); | |
2363 xmlnode_insert_cdata(y, "away", -1); | |
2364 y = xmlnode_insert_tag(x, "status"); | |
2365 xmlnode_insert_cdata(y, message, -1); | |
2366 gc->away = ""; | |
2367 } else { | |
2368 /* Gaim wants us to not be away */ | |
2369 /* but for Jabber, we can just send presence with no other information. */ | |
2370 } | |
2371 } else { | |
2372 /* state is one of our own strings. it won't be NULL. */ | |
2373 if (!strcmp(state, "Online")) { | |
2374 /* once again, we don't have to put anything here */ | |
2375 } else if (!strcmp(state, "Chatty")) { | |
2376 y = xmlnode_insert_tag(x, "show"); | |
2377 xmlnode_insert_cdata(y, "chat", -1); | |
2378 } else if (!strcmp(state, "Away")) { | |
2379 y = xmlnode_insert_tag(x, "show"); | |
2380 xmlnode_insert_cdata(y, "away", -1); | |
2381 gc->away = ""; | |
2382 } else if (!strcmp(state, "Extended Away")) { | |
2383 y = xmlnode_insert_tag(x, "show"); | |
2384 xmlnode_insert_cdata(y, "xa", -1); | |
2385 gc->away = ""; | |
2386 } else if (!strcmp(state, "Do Not Disturb")) { | |
2387 y = xmlnode_insert_tag(x, "show"); | |
2388 xmlnode_insert_cdata(y, "dnd", -1); | |
2389 gc->away = ""; | |
2390 } | |
2391 } | |
2392 | |
2956 | 2393 gjab_send(gjc, x); |
2086 | 2394 xmlnode_free(x); |
2395 } | |
2396 | |
2397 static void jabber_set_idle(struct gaim_connection *gc, int idle) { | |
2398 struct jabber_data *jd = (struct jabber_data *)gc->proto_data; | |
2399 debug_printf("jabber_set_idle: setting idle %i\n", idle); | |
2400 jd->idle = idle ? time(NULL) - idle : idle; | |
2401 } | |
2402 | |
2403 static void jabber_keepalive(struct gaim_connection *gc) { | |
2404 struct jabber_data *jd = (struct jabber_data *)gc->proto_data; | |
2956 | 2405 gjab_send_raw(jd->gjc, " \t "); |
2086 | 2406 } |
2407 | |
2154
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2408 static GList *jabber_user_opts() |
2086 | 2409 { |
2154
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2410 GList *m = NULL; |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2411 struct proto_user_opt *puo; |
2086 | 2412 |
2154
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2413 puo = g_new0(struct proto_user_opt, 1); |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2414 puo->label = "Port:"; |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2415 puo->def = "5222"; |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2416 puo->pos = USEROPT_PORT; |
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2417 m = g_list_append(m, puo); |
2086 | 2418 |
2154
cff133e0ec0c
[gaim-migrate @ 2164]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
2419 return m; |
2086 | 2420 } |
2421 | |
2607
e0d4a23aac89
[gaim-migrate @ 2620]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2501
diff
changeset
|
2422 static void jabber_buddy_free(struct buddy *b) |
e0d4a23aac89
[gaim-migrate @ 2620]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2501
diff
changeset
|
2423 { |
e0d4a23aac89
[gaim-migrate @ 2620]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2501
diff
changeset
|
2424 while (b->proto_data) { |
e0d4a23aac89
[gaim-migrate @ 2620]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2501
diff
changeset
|
2425 g_free(((GSList *)b->proto_data)->data); |
e0d4a23aac89
[gaim-migrate @ 2620]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2501
diff
changeset
|
2426 b->proto_data = g_slist_remove(b->proto_data, ((GSList *)b->proto_data)->data); |
e0d4a23aac89
[gaim-migrate @ 2620]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2501
diff
changeset
|
2427 } |
e0d4a23aac89
[gaim-migrate @ 2620]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2501
diff
changeset
|
2428 } |
e0d4a23aac89
[gaim-migrate @ 2620]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2501
diff
changeset
|
2429 |
2956 | 2430 /*---------------------------------------*/ |
2431 /* Jabber "set info" (vCard) support */ | |
2432 /*---------------------------------------*/ | |
2433 | |
2434 /* | |
2435 * V-Card format: | |
2436 * | |
2437 * <vCard prodid='' version='' xmlns=''> | |
2438 * <FN></FN> | |
2439 * <N> | |
2440 * <FAMILY/> | |
2441 * <GIVEN/> | |
2442 * </N> | |
2443 * <NICKNAME/> | |
2444 * <URL/> | |
2445 * <ADR> | |
2446 * <STREET/> | |
2447 * <EXTADD/> | |
2448 * <LOCALITY/> | |
2449 * <REGION/> | |
2450 * <PCODE/> | |
2451 * <COUNTRY/> | |
2452 * </ADR> | |
2453 * <TEL/> | |
2454 * <EMAIL/> | |
2455 * <ORG> | |
2456 * <ORGNAME/> | |
2457 * <ORGUNIT/> | |
2458 * </ORG> | |
2459 * <TITLE/> | |
2460 * <ROLE/> | |
2461 * <DESC/> | |
2462 * <BDAY/> | |
2463 * </vCard> | |
2464 * | |
2465 * See also: | |
2466 * | |
2467 * http://docs.jabber.org/proto/html/vcard-temp.html | |
2468 * http://www.vcard-xml.org/dtd/vCard-XML-v2-20010520.dtd | |
2469 */ | |
2470 | |
2471 /* | |
2472 * Cross-reference user-friendly V-Card entry labels to vCard XML tags | |
2473 * and attributes. | |
2474 * | |
2475 * Order is (or should be) unimportant. For example: we have no way of | |
2476 * knowing in what order real data will arrive. | |
2477 * | |
2478 * Format: Label, Pre-set text, "visible" flag, "editable" flag, XML tag | |
2479 * name, XML tag's parent tag "path" (relative to vCard node). | |
2480 * | |
2481 * List is terminated by a NULL label pointer. | |
2482 * | |
2483 * Entries with no label text, but with XML tag and parent tag | |
2484 * entries, are used by V-Card XML construction routines to | |
2485 * "automagically" construct the appropriate XML node tree. | |
2486 * | |
2487 * Thoughts on future direction/expansion | |
2488 * | |
2489 * This is a "simple" vCard. | |
2490 * | |
2491 * It is possible for nodes other than the "vCard" node to have | |
2492 * attributes. Should that prove necessary/desirable, add an | |
2493 * "attributes" pointer to the vcard_template struct, create the | |
2494 * necessary tag_attr structs, and add 'em to the vcard_dflt_data | |
2495 * array. | |
2496 * | |
2497 * The above changes will (obviously) require changes to the vCard | |
2498 * construction routines. | |
2499 */ | |
2500 | |
2501 struct vcard_template { | |
2502 char *label; /* label text pointer */ | |
2503 char *text; /* entry text pointer */ | |
2504 int visible; /* should entry field be "visible?" */ | |
2505 int editable; /* should entry field be editable? */ | |
2506 char *tag; /* tag text */ | |
2507 char *ptag; /* parent tag "path" text */ | |
2508 char *url; /* vCard display format if URL */ | |
2509 } vcard_template_data[] = { | |
2975 | 2510 {N_("Full Name"), NULL, TRUE, TRUE, "FN", NULL, NULL}, |
2511 {N_("Family Name"), NULL, TRUE, TRUE, "FAMILY", "N", NULL}, | |
2512 {N_("Given Name"), NULL, TRUE, TRUE, "GIVEN", "N", NULL}, | |
2513 {N_("Nickname"), NULL, TRUE, TRUE, "NICKNAME", NULL, NULL}, | |
2514 {N_("URL"), NULL, TRUE, TRUE, "URL", NULL, "<A HREF=\"%s\">%s</A>"}, | |
2515 {N_("Street Address"), NULL, TRUE, TRUE, "STREET", "ADR", NULL}, | |
2516 {N_("Extended Address"), NULL, TRUE, TRUE, "EXTADD", "ADR", NULL}, | |
2517 {N_("Locality"), NULL, TRUE, TRUE, "LOCALITY", "ADR", NULL}, | |
2518 {N_("Region"), NULL, TRUE, TRUE, "REGION", "ADR", NULL}, | |
2519 {N_("Postal Code"), NULL, TRUE, TRUE, "PCODE", "ADR", NULL}, | |
2520 {N_("Country"), NULL, TRUE, TRUE, "COUNTRY", "ADR", NULL}, | |
2521 {N_("Telephone"), NULL, TRUE, TRUE, "TELEPHONE", NULL, NULL}, | |
2522 {N_("Email"), NULL, TRUE, TRUE, "EMAIL", NULL, "<A HREF=\"mailto:%s\">%s</A>"}, | |
2523 {N_("Organization Name"), NULL, TRUE, TRUE, "ORGNAME", "ORG", NULL}, | |
2524 {N_("Organization Unit"), NULL, TRUE, TRUE, "ORGUNIT", "ORG", NULL}, | |
2525 {N_("Title"), NULL, TRUE, TRUE, "TITLE", NULL, NULL}, | |
2526 {N_("Role"), NULL, TRUE, TRUE, "ROLE", NULL, NULL}, | |
2527 {N_("Birthday"), NULL, TRUE, TRUE, "BDAY", NULL, NULL}, | |
2528 {N_("Description"), NULL, TRUE, TRUE, "DESC", NULL, NULL}, | |
2956 | 2529 {"", NULL, TRUE, TRUE, "N", NULL, NULL}, |
2530 {"", NULL, TRUE, TRUE, "ADR", NULL, NULL}, | |
2531 {"", NULL, TRUE, TRUE, "ORG", NULL, NULL}, | |
2532 {NULL, NULL, 0, 0, NULL, NULL, NULL} | |
2533 }; | |
2534 | |
2535 /* | |
2536 * The "vCard" tag's attibute list... | |
2537 */ | |
2538 struct tag_attr { | |
2539 char *attr; | |
2540 char *value; | |
2541 } vcard_tag_attr_list[] = { | |
2542 {"prodid", "-//HandGen//NONSGML vGen v1.0//EN"}, | |
2543 {"version", "2.0", }, | |
2544 {"xmlns", "vcard-temp", }, | |
2545 {NULL, NULL}, | |
2546 }; | |
2547 | |
2548 | |
2549 /* | |
2550 * V-Card user instructions | |
2551 */ | |
2552 static char *multi_entry_instructions = | |
2975 | 2553 N_("All items below are optional. Enter only the information with which you feel comfortable"); |
2554 static char *entries_title = N_("User Identity"); | |
2956 | 2555 |
2556 /* | |
2557 * Used by routines to parse an XML-encoded string into an xmlnode tree | |
2558 */ | |
2559 typedef struct { | |
2560 XML_Parser parser; | |
2561 xmlnode current; | |
2562 } *xmlstr2xmlnode_parser, xmlstr2xmlnode_parser_struct; | |
2563 | |
2564 | |
2565 /* | |
2566 * Display a Jabber vCard | |
2567 */ | |
2568 static void jabber_handlevcard(gjconn gjc, xmlnode querynode, char *from) | |
2569 { | |
2570 struct gaim_connection *gc = GJ_GC(gjc); | |
2571 struct jabber_data *jd = GJ_GC(gjc)->proto_data; | |
2572 jid who = jid_new(gjc->p, from); | |
2573 char *cdata, *status; | |
2574 | |
2575 struct vcard_template *vc_tp = vcard_template_data; | |
2576 | |
2577 /* space for all vCard elements + Jabber I.D. + "status" + NULL (list terminator) */ | |
2578 gchar **str_arr = (gchar **) g_new(gpointer, | |
2579 (sizeof(vcard_template_data)/sizeof(struct vcard_template)) + 3); | |
2580 gchar **ap = str_arr; | |
2581 gchar *buddy, *final; | |
2582 | |
2583 if(who->resource != NULL && (who->resource)[0] != '\0') { | |
2584 buddy = g_strdup_printf("%s@%s/%s", who->user, who->server, who->resource); | |
2585 } else { | |
2586 buddy = g_strdup_printf("%s@%s", who->user, who->server); | |
2587 } | |
2588 *ap++ = g_strdup_printf("<B>Jabber ID:</B> %s<BR>\n", buddy); | |
2589 | |
2590 for(vc_tp = vcard_template_data; vc_tp->label != NULL; ++vc_tp) { | |
2591 if(strcmp(vc_tp->tag, "DESC") == 0) | |
2592 continue; /* special handling later */ | |
2593 if(vc_tp->ptag == NULL) { | |
2594 cdata = xmlnode_get_tag_data(querynode, vc_tp->tag); | |
2595 } else { | |
2596 gchar *tag = g_strdup_printf("%s/%s", vc_tp->ptag, vc_tp->tag); | |
2597 cdata = xmlnode_get_tag_data(querynode, tag); | |
2598 g_free(tag); | |
2599 } | |
2600 if(cdata != NULL) { | |
2601 if(vc_tp->url == NULL) { | |
2602 *ap++ = g_strdup_printf("<B>%s:</B> %s<BR>\n", vc_tp->label, cdata); | |
2603 } else { | |
2604 gchar *fmt = g_strdup_printf("<B>%%s:</B> %s<BR>\n", vc_tp->url); | |
2605 *ap++ = g_strdup_printf(fmt, vc_tp->label, cdata, cdata); | |
2606 g_free(fmt); | |
2607 } | |
2608 } | |
2609 } | |
2610 | |
2611 if((status = g_hash_table_lookup(jd->hash, buddy)) == NULL) { | |
3105 | 2612 status = _("Unknown"); |
2956 | 2613 } |
2614 *ap++ = g_strdup_printf("<B>Status:</B> %s<BR>\n", status); | |
2615 | |
2616 /* | |
2617 * "Description" handled as a special case: get a copy of the | |
2618 * string and HTML-ize. | |
2619 */ | |
2620 if((cdata = xmlnode_get_tag_data(querynode, "DESC")) != NULL) { | |
2621 gchar *tmp = g_strdup_printf("<HR>%s<BR>", cdata); | |
2622 *ap++ = strdup_withhtml(tmp); | |
2623 g_free(tmp); | |
2624 } | |
2625 | |
2626 *ap = NULL; | |
2627 | |
2628 final= g_strjoinv(NULL, str_arr); | |
2629 g_strfreev(str_arr); | |
2630 | |
2631 g_show_info_text(gc, buddy, 2, final, NULL); | |
2632 g_free(buddy); | |
2633 g_free(final); | |
2634 } | |
2635 | |
2636 /* | |
2637 * Used by XML_Parse on parsing CDATA | |
2638 */ | |
2639 static void xmlstr2xmlnode_charData(void *userdata, const char *s, int slen) | |
2640 { | |
2641 xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata; | |
2642 | |
2643 if (xmlp->current) | |
2644 xmlnode_insert_cdata(xmlp->current, s, slen); | |
2645 } | |
2646 | |
2647 /* | |
2648 * Used by XML_Parse to start or append to an xmlnode | |
2649 */ | |
2650 static void xmlstr2xmlnode_startElement(void *userdata, const char *name, const char **attribs) | |
2651 { | |
2652 xmlnode x; | |
2653 xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata; | |
2654 | |
2655 if (xmlp->current) { | |
2656 /* Append the node to the current one */ | |
2657 x = xmlnode_insert_tag(xmlp->current, name); | |
2658 xmlnode_put_expat_attribs(x, attribs); | |
2659 | |
2660 xmlp->current = x; | |
2661 } else { | |
2662 x = xmlnode_new_tag(name); | |
2663 xmlnode_put_expat_attribs(x, attribs); | |
2664 xmlp->current = x; | |
2665 } | |
2666 } | |
2667 | |
2668 /* | |
2669 * Used by XML_Parse to end an xmlnode | |
2670 */ | |
2671 static void xmlstr2xmlnode_endElement(void *userdata, const char *name) | |
2672 { | |
2673 xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata; | |
2674 xmlnode x; | |
2675 | |
2676 if (xmlp->current != NULL && (x = xmlnode_get_parent(xmlp->current)) != NULL) { | |
2677 xmlp->current = x; | |
2678 } | |
2679 } | |
2680 | |
2681 /* | |
2682 * Parse an XML-encoded string into an xmlnode tree | |
2683 * | |
2684 * Caller is responsible for freeing the returned xmlnode | |
2685 */ | |
2686 static xmlnode xmlstr2xmlnode(char *xmlstring) | |
2687 { | |
2688 xmlstr2xmlnode_parser my_parser = g_new(xmlstr2xmlnode_parser_struct, 1); | |
2689 xmlnode x = NULL; | |
2690 | |
2691 my_parser->parser = XML_ParserCreate(NULL); | |
2692 my_parser->current = NULL; | |
2693 | |
2694 XML_SetUserData(my_parser->parser, (void *)my_parser); | |
2695 XML_SetElementHandler(my_parser->parser, xmlstr2xmlnode_startElement, xmlstr2xmlnode_endElement); | |
2696 XML_SetCharacterDataHandler(my_parser->parser, xmlstr2xmlnode_charData); | |
2697 XML_Parse(my_parser->parser, xmlstring, strlen(xmlstring), 0); | |
2698 | |
2699 x = my_parser->current; | |
2700 | |
2701 XML_ParserFree(my_parser->parser); | |
2702 g_free(my_parser); | |
2703 | |
2704 return(x); | |
2705 } | |
2706 | |
2707 /* | |
2708 * Insert a tag node into an xmlnode tree, recursively inserting parent tag | |
2709 * nodes as necessary | |
2710 * | |
2711 * Returns pointer to inserted node | |
2712 * | |
2713 * Note to hackers: this code is designed to be re-entrant (it's recursive--it | |
2714 * calls itself), so don't put any "static"s in here! | |
2715 */ | |
2716 static xmlnode insert_tag_to_parent_tag(xmlnode start, const char *parent_tag, const char *new_tag) | |
2717 { | |
2718 xmlnode x = NULL; | |
2719 | |
2720 /* | |
2721 * If the parent tag wasn't specified, see if we can get it | |
2722 * from the vCard template struct. | |
2723 */ | |
2724 if(parent_tag == NULL) { | |
2725 struct vcard_template *vc_tp = vcard_template_data; | |
2726 | |
2727 while(vc_tp->label != NULL) { | |
2728 if(strcmp(vc_tp->tag, new_tag) == 0) { | |
2729 parent_tag = vc_tp->ptag; | |
2730 break; | |
2731 } | |
2732 ++vc_tp; | |
2733 } | |
2734 } | |
2735 | |
2736 /* | |
2737 * If we have a parent tag... | |
2738 */ | |
2739 if(parent_tag != NULL ) { | |
2740 /* | |
2741 * Try to get the parent node for a tag | |
2742 */ | |
2743 if((x = xmlnode_get_tag(start, parent_tag)) == NULL) { | |
2744 /* | |
2745 * Descend? | |
2746 */ | |
2747 char *grand_parent = strcpy(g_malloc(strlen(parent_tag) + 1), parent_tag); | |
2748 char *parent; | |
2749 | |
2750 if((parent = strrchr(grand_parent, '/')) != NULL) { | |
2751 *(parent++) = '\0'; | |
2752 x = insert_tag_to_parent_tag(start, grand_parent, parent); | |
2753 } else { | |
2754 x = xmlnode_insert_tag(start, grand_parent); | |
2755 } | |
2756 g_free(grand_parent); | |
2757 } else { | |
2758 /* | |
2759 * We found *something* to be the parent node. | |
2760 * Note: may be the "root" node! | |
2761 */ | |
2762 xmlnode y; | |
2763 if((y = xmlnode_get_tag(x, new_tag)) != NULL) { | |
2764 return(y); | |
2765 } | |
2766 } | |
2767 } | |
2768 | |
2769 /* | |
2770 * insert the new tag into its parent node | |
2771 */ | |
2772 return(xmlnode_insert_tag((x == NULL? start : x), new_tag)); | |
2773 } | |
2774 | |
2775 /* | |
2776 * Find the tag name for a label | |
2777 * | |
2778 * Returns NULL on not found | |
2779 */ | |
2780 static char *tag_for_label(const char *label) | |
2781 { | |
2782 struct vcard_template *vc_tp = vcard_template_data; | |
2783 char *p = NULL; | |
2784 | |
2785 for(vc_tp = vcard_template_data; vc_tp->label != NULL; ++vc_tp) { | |
2786 if(strcmp(label, vc_tp->label) == 0) { | |
2787 p = vc_tp->tag; | |
2788 break; | |
2789 } | |
2790 } | |
2791 | |
2792 return(p); | |
2793 } | |
2794 | |
2795 /* | |
2796 * Send vCard info to Jabber server | |
2797 */ | |
2798 static void jabber_set_info(struct gaim_connection *gc, char *info) | |
2799 { | |
2800 xmlnode x, vc_node; | |
2801 char *id; | |
2802 struct jabber_data *jd = gc->proto_data; | |
2803 gjconn gjc = jd->gjc; | |
2804 | |
2805 x = xmlnode_new_tag("iq"); | |
2806 xmlnode_put_attrib(x,"type","set"); | |
2807 | |
2808 id = gjab_getid(gjc); | |
2809 | |
2810 xmlnode_put_attrib(x, "id", id); | |
2811 | |
2812 /* | |
2813 * Send only if there's actually any *information* to send | |
2814 */ | |
2815 if((vc_node = xmlstr2xmlnode(info)) != NULL && xmlnode_get_name(vc_node) != NULL && | |
2816 g_strncasecmp(xmlnode_get_name(vc_node), "vcard", 5) == 0) { | |
2817 xmlnode_insert_tag_node(x, vc_node); | |
2818 debug_printf("jabber: vCard packet: %s\n", xmlnode2str(x)); | |
2819 gjab_send(gjc, x); | |
2820 } | |
2821 | |
2822 xmlnode_free(x); | |
2823 } | |
2824 | |
2825 /* | |
2826 * This is the callback from the "ok clicked" for "set vCard" | |
2827 * | |
2828 * Formats GSList data into XML-encoded string and returns a pointer | |
2829 * to said string. | |
2830 * | |
2831 * g_free()'ing the returned string space is the responsibility of | |
2832 * the caller. | |
2833 */ | |
2834 static gchar *jabber_format_info(MultiEntryDlg *b) | |
2835 { | |
2836 xmlnode vc_node; | |
2837 GSList *list; | |
2838 MultiEntryData *med; | |
2839 MultiTextData *mtd; | |
2840 char *p; | |
2841 | |
2842 struct tag_attr *tag_attr; | |
2843 | |
2844 vc_node = xmlnode_new_tag("vCard"); | |
2845 | |
2846 for(tag_attr = vcard_tag_attr_list; tag_attr->attr != NULL; ++tag_attr) | |
2847 xmlnode_put_attrib(vc_node, tag_attr->attr, tag_attr->value); | |
2848 | |
2849 for(list = b->multi_entry_items; list != NULL; list = list->next) { | |
2850 med = (MultiEntryData *) list->data; | |
2851 if(med->label != NULL && med->text != NULL && (med->text)[0] != '\0') { | |
2852 if((p = tag_for_label(med->label)) != NULL) { | |
2853 xmlnode xp; | |
2854 if((xp = insert_tag_to_parent_tag(vc_node, NULL, p)) != NULL) { | |
2855 xmlnode_insert_cdata(xp, med->text, -1); | |
2856 } | |
2857 } | |
2858 } | |
2859 } | |
2860 | |
2861 for(list = b->multi_text_items; list != NULL; list = list->next) { | |
2862 mtd = (MultiTextData *) list->data; | |
2863 if(mtd->label != NULL && mtd->text != NULL && (mtd->text)[0] != '\0') { | |
2864 if((p = tag_for_label(mtd->label)) != NULL) { | |
2865 xmlnode xp; | |
2866 if((xp = insert_tag_to_parent_tag(vc_node, NULL, p)) != NULL) { | |
2867 xmlnode_insert_cdata(xp, mtd->text, -1); | |
2868 } | |
2869 } | |
2870 } | |
2871 } | |
2872 | |
2873 | |
2874 p = g_strdup(xmlnode2str(vc_node)); | |
2875 xmlnode_free(vc_node); | |
2876 | |
2877 return(p); | |
2878 } | |
2879 | |
2880 /* | |
2881 * This gets executed by the proto action | |
2882 * | |
2883 * Creates a new MultiEntryDlg struct, gets the XML-formatted user_info | |
2884 * string (if any) into GSLists for the (multi-entry) edit dialog and | |
2885 * calls the set_vcard dialog. | |
2886 */ | |
2887 static void jabber_setup_set_info(struct gaim_connection *gc) | |
2888 { | |
2889 MultiEntryData *data; | |
2890 const struct vcard_template *vc_tp; | |
2891 char *user_info; | |
2892 MultiEntryDlg *b = multi_entry_dialog_new(); | |
2893 char *cdata; | |
2894 xmlnode x_vc_data = NULL; | |
2895 struct aim_user *tmp = gc->user; | |
2896 b->user = tmp; | |
2897 | |
2898 | |
2899 /* | |
2900 * Get existing, XML-formatted, user info | |
2901 */ | |
2902 if((user_info = g_malloc(strlen(tmp->user_info) + 1)) != NULL) { | |
2903 strcpy(user_info, tmp->user_info); | |
2904 x_vc_data = xmlstr2xmlnode(user_info); | |
2905 } | |
2906 | |
2907 /* | |
2908 * Set up GSLists for edit with labels from "template," data from user info | |
2909 */ | |
2910 for(vc_tp = vcard_template_data; vc_tp->label != NULL; ++vc_tp) { | |
2911 if((vc_tp->label)[0] == '\0') | |
2912 continue; | |
2913 if(vc_tp->ptag == NULL) { | |
2914 cdata = xmlnode_get_tag_data(x_vc_data, vc_tp->tag); | |
2915 } else { | |
2916 gchar *tag = g_strdup_printf("%s/%s", vc_tp->ptag, vc_tp->tag); | |
2917 cdata = xmlnode_get_tag_data(x_vc_data, tag); | |
2918 g_free(tag); | |
2919 } | |
2920 if(strcmp(vc_tp->tag, "DESC") == 0) { | |
2921 multi_text_list_update(&(b->multi_text_items), | |
2922 vc_tp->label, cdata, TRUE); | |
2923 } else { | |
2924 data = multi_entry_list_update(&(b->multi_entry_items), | |
2925 vc_tp->label, cdata, TRUE); | |
2926 data->visible = vc_tp->visible; | |
2927 data->editable = vc_tp->editable; | |
2928 } | |
2929 } | |
2930 | |
2931 | |
2932 if(x_vc_data != NULL) { | |
2933 xmlnode_free(x_vc_data); | |
2934 } else { | |
2935 /* | |
2936 * Early Beta versions had a different user_info storage format--let's | |
2937 * see if that works. | |
2938 * | |
2939 * This goes away RSN. | |
2940 */ | |
2941 const char *record_separator = "<BR>"; | |
2942 const char *field_separator = ": "; | |
2943 gchar **str_list, **str_list_ptr, **str_list2; | |
2944 | |
2945 if((str_list = g_strsplit(user_info, record_separator, 0)) != NULL) { | |
2946 for(str_list_ptr = str_list; *str_list_ptr != NULL; ++str_list_ptr) { | |
2947 str_list2 = g_strsplit(*str_list_ptr, field_separator, 2); | |
2948 if(str_list2[0] != NULL && str_list2[1] != NULL) { | |
2949 g_strstrip(str_list2[0]); | |
2950 g_strstrip(str_list2[1]); | |
2951 /* this is ugly--so far */ | |
2952 if(strcmp(str_list2[0], "Description") == 0) { | |
2953 multi_text_list_update(&(b->multi_text_items), | |
2954 str_list2[0], str_list2[1], FALSE); | |
2955 } else { | |
2956 multi_entry_list_update(&(b->multi_entry_items), | |
2957 str_list2[0], str_list2[1], FALSE); | |
2958 } | |
2959 } | |
2960 g_strfreev(str_list2); | |
2961 } | |
2962 g_strfreev(str_list); | |
2963 } | |
2964 } | |
2965 | |
2966 if(user_info != NULL) { | |
2967 g_free(user_info); | |
2968 } | |
2969 | |
2975 | 2970 b->title = _("Gaim - Edit Jabber vCard"); |
2956 | 2971 b->wmclass_name = "set_info"; |
2972 b->wmclass_class = "Gaim"; | |
2973 b->instructions->text = g_strdup(multi_entry_instructions); | |
2975 | 2974 b->entries_title = g_strdup(entries_title); |
2956 | 2975 |
2976 b->custom = (void *) jabber_format_info; | |
2977 | |
2978 show_set_vcard(b); | |
2979 } | |
2980 | |
2981 /*---------------------------------------*/ | |
2982 /* End Jabber "set info" (vCard) support */ | |
2983 /*---------------------------------------*/ | |
2984 | |
2985 /*----------------------------------------*/ | |
2986 /* Jabber "user registration" support */ | |
2987 /*----------------------------------------*/ | |
2988 | |
2989 /* | |
2990 * Three of the following four functions duplicate much of what | |
2991 * exists elsewhere: | |
2992 * | |
2993 * jabber_handleregresp() | |
2994 * gjab_reqreg() | |
2995 * jabber_handle_registration_state() | |
2996 * | |
2997 * It may be that an additional flag could be added to one of | |
2998 * the "local" structs and the duplicated code modified to | |
2999 * account for it--thus eliminating the duplication. Then again: | |
3000 * doing it the way it is may be much cleaner. | |
3001 * | |
3002 * TBD: Code to support requesting additional information server | |
3003 * wants at registration--incl. dialog. | |
3004 */ | |
3005 | |
3006 /* | |
3007 * Like jabber_handlepacket(), only different | |
3008 */ | |
3009 static void jabber_handleregresp(gjconn gjc, jpacket p) | |
3010 { | |
3011 if (jpacket_subtype(p) == JPACKET__RESULT) { | |
3012 xmlnode querynode; | |
3013 | |
3014 if((querynode = xmlnode_get_tag(p->x, "query")) != NULL) { | |
3015 char *xmlns; | |
3016 | |
3017 /* we damn well *better* have this! */ | |
3018 if((xmlns = xmlnode_get_attrib(querynode, "xmlns")) != NULL && | |
3019 strcmp(xmlns, NS_REGISTER) == 0) { | |
3020 | |
3021 char *tag; | |
3022 xmlnode child = xmlnode_get_firstchild(querynode); | |
3023 | |
3024 debug_printf("got registration requirments response!\n"); | |
3025 | |
3026 while(child != NULL) { | |
3027 if((tag = xmlnode_get_name(child)) != NULL) { | |
3028 char *data; | |
3029 | |
3030 fprintf(stderr, "DBG: got node: \"%s\"\n", tag); | |
3031 fflush(stderr); | |
3032 | |
3033 if((data = xmlnode_get_data(child)) != NULL) { | |
3034 fprintf(stderr, "DBG: got data: \"%s\"\n", data); | |
3035 fflush(stderr); | |
3036 } | |
3037 } | |
3038 child = xmlnode_get_nextsibling(child); | |
3039 } | |
3040 } | |
3041 } else { | |
3042 debug_printf("registration successful!\n"); | |
3043 | |
2975 | 3044 hide_login_progress_notice(GJ_GC(gjc), _("Server Registration successful!")); |
2956 | 3045 /* |
3046 * TBD: is this the correct way to do away with a | |
3047 * gaim_connection and all its associated memory | |
3048 * allocs, etc.? | |
3049 */ | |
3050 signoff(GJ_GC(gjc)); | |
3051 } | |
3052 | |
3053 } else { | |
3054 xmlnode xerr; | |
3055 char *errmsg = NULL; | |
3056 int errcode = 0; | |
3057 struct jabber_data *jd = GJ_GC(gjc)->proto_data; | |
3058 | |
3059 debug_printf("registration failed\n"); | |
3060 xerr = xmlnode_get_tag(p->x, "error"); | |
3061 if (xerr) { | |
3062 char msg[BUF_LONG]; | |
3063 errmsg = xmlnode_get_data(xerr); | |
3064 if (xmlnode_get_attrib(xerr, "code")) { | |
3065 errcode = atoi(xmlnode_get_attrib(xerr, "code")); | |
3066 g_snprintf(msg, sizeof(msg), "Error %d: %s", errcode, errmsg); | |
3067 } else | |
3068 g_snprintf(msg, sizeof(msg), "%s", errmsg); | |
3069 hide_login_progress(GJ_GC(gjc), msg); | |
3070 } else { | |
2975 | 3071 hide_login_progress(GJ_GC(gjc), _("Unknown registration error")); |
2956 | 3072 } |
3073 | |
3074 jd->die = TRUE; | |
3075 } | |
3076 } | |
3077 | |
3078 /* | |
3079 * Like gjab_reqauth(), only different | |
3080 */ | |
3081 static void gjab_reqreg(gjconn gjc) | |
3082 { | |
3083 xmlnode x, y, z; | |
3084 char *user; | |
3085 | |
3086 if (!gjc) | |
3087 return; | |
3088 | |
3089 x = jutil_iqnew(JPACKET__SET, NS_REGISTER); | |
3090 y = xmlnode_get_tag(x, "query"); | |
3091 | |
3092 user = gjc->user->user; | |
3093 | |
3094 if (user) { | |
3095 z = xmlnode_insert_tag(y, "username"); | |
3096 xmlnode_insert_cdata(z, user, -1); | |
3097 } | |
3098 z = xmlnode_insert_tag(y, "password"); | |
3099 xmlnode_insert_cdata(z, gjc->pass, -1); | |
3100 | |
3101 debug_printf("jabber: registration packet: %s\n", xmlnode2str(x)); | |
3102 gjab_send(gjc, x); | |
3103 xmlnode_free(x); | |
3104 } | |
3105 | |
3106 /* | |
3107 * Like jabber_handlestate(), only different | |
3108 */ | |
3109 static void jabber_handle_registration_state(gjconn gjc, int state) | |
3110 { | |
3111 switch (state) { | |
3112 case JCONN_STATE_OFF: | |
3074 | 3113 if(gjc->was_connected) { |
3114 hide_login_progress_error(GJ_GC(gjc), _("Connection lost")); | |
3115 } else { | |
3116 hide_login_progress(GJ_GC(gjc), _("Unable to connect")); | |
3117 } | |
2956 | 3118 signoff(GJ_GC(gjc)); |
3119 break; | |
3120 case JCONN_STATE_CONNECTED: | |
3074 | 3121 gjc->was_connected = 1; |
2956 | 3122 /* |
3123 * TBD? | |
2975 | 3124 set_login_progress(GJ_GC(gjc), 2, _("Connected")); |
2956 | 3125 */ |
3126 break; | |
3127 case JCONN_STATE_ON: | |
3128 /* | |
3129 * TBD? | |
2975 | 3130 set_login_progress(GJ_GC(gjc), 3, _("Requesting Authentication Method")); |
2956 | 3131 */ |
3132 gjab_reqreg(gjc); | |
3133 /* | |
3134 * TBD: A work-in-progress | |
3135 gjab_reqregreqs(gjc); | |
3136 */ | |
3137 break; | |
3138 default: | |
3139 debug_printf("state change: %d\n", state); | |
3140 } | |
3141 return; | |
3142 } | |
3143 | |
3144 /* | |
3145 * Like jabber_login(), only different | |
3146 */ | |
3147 void jabber_register_user(struct aim_user *au) | |
3148 { | |
3149 struct gaim_connection *gc = new_gaim_conn(au); | |
3150 struct jabber_data *jd = gc->proto_data = g_new0(struct jabber_data, 1); | |
3151 char *loginname = create_valid_jid(au->username, DEFAULT_SERVER, "GAIM"); | |
3152 | |
3153 /* | |
3154 * These do nothing during registration | |
3155 */ | |
3156 jd->hash = NULL; | |
3157 jd->chats = NULL; | |
3158 | |
3159 if ((jd->gjc = gjab_new(loginname, au->password, gc)) == NULL) { | |
3160 g_free(loginname); | |
3161 debug_printf("jabber: unable to connect (jab_new failed)\n"); | |
2975 | 3162 hide_login_progress(gc, _("Unable to connect")); |
2956 | 3163 signoff(gc); |
3164 } else { | |
3165 gjab_state_handler(jd->gjc, jabber_handle_registration_state); | |
3166 gjab_packet_handler(jd->gjc, jabber_handleregresp); | |
3167 jd->gjc->queries = NULL; | |
3168 gjab_start(jd->gjc); | |
3169 } | |
3170 | |
3171 g_free(loginname); | |
3172 } | |
3173 | |
3174 /*----------------------------------------*/ | |
3175 /* End Jabber "user registration" support */ | |
3176 /*----------------------------------------*/ | |
3177 | |
3178 static void jabber_do_action(struct gaim_connection *gc, char *act) | |
3179 { | |
2975 | 3180 if (!strcmp(act, _("Set User Info"))) { |
2956 | 3181 jabber_setup_set_info(gc); |
3182 /* | |
2975 | 3183 } else if (!strcmp(act, _("Set Dir Info"))) { |
2956 | 3184 show_set_dir(gc); |
3257 | 3185 */ |
2975 | 3186 } else if (!strcmp(act, _("Change Password"))) { |
2956 | 3187 show_change_passwd(gc); |
3188 } | |
3189 } | |
3190 | |
3191 static GList *jabber_actions() | |
3192 { | |
3193 GList *m = NULL; | |
3194 | |
2975 | 3195 m = g_list_append(m, _("Set User Info")); |
2956 | 3196 /* |
2975 | 3197 m = g_list_append(m, _("Set Dir Info")); |
3257 | 3198 */ |
2975 | 3199 m = g_list_append(m, _("Change Password")); |
2956 | 3200 |
3201 return m; | |
3202 } | |
3203 | |
2086 | 3204 static struct prpl *my_protocol = NULL; |
3205 | |
3206 void jabber_init(struct prpl *ret) | |
3207 { | |
3208 /* the NULL's aren't required but they're nice to have */ | |
3209 ret->protocol = PROTO_JABBER; | |
3210 ret->options = OPT_PROTO_UNIQUE_CHATNAME | OPT_PROTO_CHAT_TOPIC; | |
3211 ret->name = jabber_name; | |
3212 ret->list_icon = jabber_list_icon; | |
3213 ret->away_states = jabber_away_states; | |
2956 | 3214 ret->actions = jabber_actions; |
3215 ret->do_action = jabber_do_action; | |
2086 | 3216 ret->buddy_menu = jabber_buddy_menu; |
3217 ret->user_opts = jabber_user_opts; | |
3218 ret->login = jabber_login; | |
3219 ret->close = jabber_close; | |
3220 ret->send_im = jabber_send_im; | |
2956 | 3221 ret->set_info = jabber_set_info; |
2086 | 3222 ret->get_info = jabber_get_info; |
2956 | 3223 ret->get_cb_info = jabber_get_cb_info; |
3224 ret->get_cb_away = jabber_get_cb_away_msg; | |
2086 | 3225 ret->set_away = jabber_set_away; |
3226 ret->set_dir = NULL; | |
3227 ret->get_dir = NULL; | |
3228 ret->dir_search = NULL; | |
3229 ret->set_idle = jabber_set_idle; | |
3257 | 3230 ret->change_passwd = jabber_change_passwd; |
2086 | 3231 ret->add_buddy = jabber_add_buddy; |
3232 ret->add_buddies = NULL; | |
3233 ret->remove_buddy = jabber_remove_buddy; | |
3234 ret->add_permit = NULL; | |
3235 ret->add_deny = NULL; | |
3236 ret->rem_permit = NULL; | |
3237 ret->rem_deny = NULL; | |
3238 ret->set_permit_deny = NULL; | |
3239 ret->warn = NULL; | |
2205
cff4fbe01c7b
[gaim-migrate @ 2215]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2170
diff
changeset
|
3240 ret->chat_info = jabber_chat_info; |
2086 | 3241 ret->join_chat = jabber_join_chat; |
3242 ret->chat_invite = jabber_chat_invite; | |
3243 ret->chat_leave = jabber_chat_leave; | |
3244 ret->chat_whisper = jabber_chat_whisper; | |
3245 ret->chat_send = jabber_chat_send; | |
3246 ret->keepalive = jabber_keepalive; | |
3247 ret->normalize = jabber_normalize; | |
2607
e0d4a23aac89
[gaim-migrate @ 2620]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2501
diff
changeset
|
3248 ret->buddy_free = jabber_buddy_free; |
2956 | 3249 ret->register_user = jabber_register_user; |
3136 | 3250 ret->alias_buddy = jabber_roster_update; |
3251 ret->group_buddy = jabber_group_change; | |
2086 | 3252 |
3253 my_protocol = ret; | |
3254 } | |
3255 | |
3256 #ifndef STATIC | |
3257 | |
3258 char *gaim_plugin_init(GModule *handle) | |
3259 { | |
3260 load_protocol(jabber_init, sizeof(struct prpl)); | |
3261 return NULL; | |
3262 } | |
3263 | |
3264 void gaim_plugin_remove() | |
3265 { | |
3266 struct prpl *p = find_prpl(PROTO_JABBER); | |
3267 if (p == my_protocol) | |
3268 unload_protocol(p); | |
3269 } | |
3270 | |
3271 char *name() | |
3272 { | |
3273 return "Jabber"; | |
3274 } | |
3275 | |
3276 char *description() | |
3277 { | |
2162
a464da684307
[gaim-migrate @ 2172]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2154
diff
changeset
|
3278 return PRPL_DESC("Jabber"); |
2086 | 3279 } |
3280 | |
3281 #endif |