29
|
1 /* |
|
2 * jabglue.c -- Jabber protocol handling |
|
3 * |
|
4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net> |
|
5 * Parts come from the centericq project: |
|
6 * Copyright (C) 2002-2005 by Konstantin Klyagin <konst@konst.org.ua> |
|
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 (at |
|
11 * your option) any later version. |
|
12 * |
|
13 * This program is distributed in the hope that it will be useful, but |
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 * 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 |
|
21 * USA |
|
22 */ |
|
23 |
237
|
24 #define _GNU_SOURCE /* We need glibc for strptime */ |
29
|
25 #include "../libjabber/jabber.h" |
|
26 #include "jabglue.h" |
81
|
27 #include "roster.h" |
29
|
28 #include "screen.h" |
113
|
29 #include "hooks.h" |
213
|
30 #include "utf8.h" |
29
|
31 #include "utils.h" |
|
32 |
|
33 #define JABBERPORT 5222 |
|
34 #define JABBERSSLPORT 5223 |
|
35 |
194
|
36 #define JABBER_AGENT_GROUP "Jabber Agents" |
|
37 |
29
|
38 jconn jc; |
112
|
39 time_t LastPingTime; |
|
40 unsigned int KeepaliveDelay; |
272
|
41 static unsigned int prio; |
31
|
42 static int s_id = 1; // FIXME which use?? |
|
43 static int regmode, regdone; |
116
|
44 static enum imstatus mystatus = offline; |
112
|
45 unsigned char online; |
29
|
46 |
46
|
47 char imstatus2char[imstatus_size] = { |
|
48 '_', 'o', 'i', 'f', 'd', 'c', 'n', 'a' |
|
49 }; |
|
50 |
31
|
51 static enum { |
29
|
52 STATE_CONNECTING, |
|
53 STATE_GETAUTH, |
|
54 STATE_SENDAUTH, |
|
55 STATE_LOGGED |
|
56 } jstate; |
|
57 |
|
58 |
31
|
59 void statehandler(jconn, int); |
|
60 void packethandler(jconn, jpacket); |
|
61 |
|
62 void screen_logger(jconn j, int io, const char *buf) |
|
63 { |
36
|
64 scr_LogPrint("%03s: %s", ((io == 0) ? "OUT" : "IN"), buf); |
31
|
65 } |
|
66 |
|
67 void file_logger(jconn j, int io, const char *buf) |
|
68 { |
36
|
69 ut_WriteLog("%03s: %s\n", ((io == 0) ? "OUT" : "IN"), buf); |
31
|
70 } |
|
71 |
|
72 void big_logger(jconn j, int io, const char *buf) |
|
73 { |
|
74 screen_logger(j, io, buf); |
|
75 file_logger(j, io, buf); |
|
76 } |
|
77 |
113
|
78 /* |
29
|
79 static void jidsplit(const char *jid, char **user, char **host, |
|
80 char **res) |
|
81 { |
|
82 char *tmp, *ptr; |
|
83 tmp = strdup(jid); |
|
84 |
|
85 if ((ptr = strchr(tmp, '/')) != NULL) { |
|
86 *res = strdup(ptr+1); |
|
87 *ptr = 0; |
|
88 } else |
|
89 *res = NULL; |
|
90 |
|
91 if ((ptr = strchr(tmp, '@')) != NULL) { |
|
92 *host = strdup(ptr+1); |
|
93 *ptr = 0; |
|
94 } else |
|
95 *host = NULL; |
|
96 |
|
97 *user = strdup(tmp); |
|
98 free(tmp); |
|
99 } |
113
|
100 */ |
29
|
101 |
192
|
102 // jidtodisp(jid) |
|
103 // Strips the resource part from the jid |
|
104 // The caller should g_free the result after use. |
29
|
105 char *jidtodisp(const char *jid) |
|
106 { |
|
107 char *ptr; |
165
|
108 char *alias; |
|
109 |
|
110 while ((alias = g_strdup(jid)) == NULL) |
|
111 usleep(100); |
|
112 |
29
|
113 if ((ptr = strchr(alias, '/')) != NULL) { |
|
114 *ptr = 0; |
|
115 } |
|
116 return alias; |
|
117 } |
|
118 |
35
|
119 jconn jb_connect(const char *jid, unsigned int port, int ssl, const char *pass) |
29
|
120 { |
|
121 if (!port) { |
|
122 if (ssl) |
|
123 port = JABBERSSLPORT; |
|
124 else |
|
125 port = JABBERPORT; |
|
126 } |
|
127 |
|
128 if (jc) |
|
129 free(jc); |
|
130 |
51
|
131 jc = jab_new((char*)jid, (char*)pass, port, ssl); |
29
|
132 |
35
|
133 jab_logger(jc, file_logger); |
29
|
134 jab_packet_handler(jc, &packethandler); |
|
135 jab_state_handler(jc, &statehandler); |
|
136 |
|
137 if (jc->user) { |
112
|
138 online = TRUE; |
29
|
139 jstate = STATE_CONNECTING; |
|
140 statehandler(0, -1); |
|
141 jab_start(jc); |
|
142 } |
|
143 |
|
144 return jc; |
|
145 } |
|
146 |
|
147 void jb_disconnect(void) |
|
148 { |
|
149 statehandler(jc, JCONN_STATE_OFF); |
|
150 } |
|
151 |
112
|
152 inline void jb_reset_keepalive() |
|
153 { |
|
154 time(&LastPingTime); |
|
155 } |
|
156 |
29
|
157 void jb_keepalive() |
|
158 { |
113
|
159 if (jc && online) |
112
|
160 jab_send_raw(jc, " \t "); |
|
161 jb_reset_keepalive(); |
|
162 } |
|
163 |
|
164 void jb_set_keepalive_delay(unsigned int delay) |
|
165 { |
|
166 KeepaliveDelay = delay; |
29
|
167 } |
|
168 |
272
|
169 inline void jb_set_priority(unsigned int priority) |
|
170 { |
|
171 prio = priority; |
|
172 } |
|
173 |
29
|
174 void jb_main() |
|
175 { |
|
176 xmlnode x, z; |
|
177 char *cid; |
|
178 |
164
|
179 if (!online) return; |
|
180 |
29
|
181 if (jc && jc->state == JCONN_STATE_CONNECTING) { |
|
182 jab_start(jc); |
|
183 return; |
|
184 } |
|
185 |
31
|
186 jab_poll(jc, 50); |
29
|
187 |
|
188 if (jstate == STATE_CONNECTING) { |
|
189 if (jc) { |
|
190 x = jutil_iqnew(JPACKET__GET, NS_AUTH); |
|
191 cid = jab_getid(jc); |
|
192 xmlnode_put_attrib(x, "id", cid); |
|
193 // id = atoi(cid); |
|
194 |
|
195 z = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "username"); |
|
196 xmlnode_insert_cdata(z, jc->user->user, (unsigned) -1); |
|
197 jab_send(jc, x); |
|
198 xmlnode_free(x); |
|
199 |
|
200 jstate = STATE_GETAUTH; |
|
201 } |
|
202 |
|
203 if (!jc || jc->state == JCONN_STATE_OFF) { |
|
204 scr_LogPrint("Unable to connect to the server"); |
112
|
205 online = FALSE; |
29
|
206 } |
|
207 } |
|
208 |
|
209 if (!jc) { |
|
210 statehandler(jc, JCONN_STATE_OFF); |
|
211 } else if (jc->state == JCONN_STATE_OFF || jc->fd == -1) { |
|
212 statehandler(jc, JCONN_STATE_OFF); |
|
213 } |
112
|
214 |
|
215 // Keepalive |
|
216 if (KeepaliveDelay) { |
|
217 time_t now; |
|
218 time(&now); |
|
219 if (now > LastPingTime + KeepaliveDelay) |
|
220 jb_keepalive(); |
|
221 } |
29
|
222 } |
|
223 |
116
|
224 inline enum imstatus jb_getstatus() |
|
225 { |
|
226 return mystatus; |
|
227 } |
|
228 |
|
229 void jb_setstatus(enum imstatus st, char *msg) |
29
|
230 { |
116
|
231 xmlnode x; |
|
232 |
164
|
233 if (!online) return; |
116
|
234 |
|
235 x = jutil_presnew(JPACKET__UNKNOWN, 0, 0); |
29
|
236 |
|
237 switch(st) { |
|
238 case away: |
|
239 xmlnode_insert_cdata(xmlnode_insert_tag(x, "show"), "away", |
|
240 (unsigned) -1); |
|
241 break; |
|
242 |
|
243 case occupied: |
|
244 case dontdisturb: |
|
245 xmlnode_insert_cdata(xmlnode_insert_tag(x, "show"), "dnd", |
|
246 (unsigned) -1); |
|
247 break; |
|
248 |
|
249 case freeforchat: |
|
250 xmlnode_insert_cdata(xmlnode_insert_tag(x, "show"), "chat", |
|
251 (unsigned) -1); |
|
252 break; |
|
253 |
|
254 case notavail: |
|
255 xmlnode_insert_cdata(xmlnode_insert_tag(x, "show"), "xa", |
|
256 (unsigned) -1); |
|
257 break; |
|
258 |
|
259 case invisible: |
|
260 xmlnode_put_attrib(x, "type", "invisible"); |
|
261 break; |
51
|
262 |
118
|
263 case offline: |
|
264 xmlnode_put_attrib(x, "type", "unavailable"); |
|
265 break; |
|
266 |
51
|
267 default: |
|
268 break; |
29
|
269 } |
|
270 |
272
|
271 if (prio) { |
|
272 char strprio[8]; |
|
273 snprintf(strprio, 8, "%u", prio); |
29
|
274 xmlnode_insert_cdata(xmlnode_insert_tag(x, "priority"), |
272
|
275 strprio, (unsigned) -1); |
|
276 } |
29
|
277 |
116
|
278 if (!msg) { |
51
|
279 msg = ""; // FIXME |
29
|
280 //msg = imstatus2str(st); |
|
281 } |
|
282 |
|
283 xmlnode_insert_cdata(xmlnode_insert_tag(x, "status"), msg, |
|
284 (unsigned) -1); |
|
285 |
|
286 jab_send(jc, x); |
|
287 xmlnode_free(x); |
|
288 |
116
|
289 //sendvisibility(); ??? |
29
|
290 |
164
|
291 // We'll need to update the roster if we switch to/from offline because |
|
292 // we don't know the presences of buddies when offline... |
|
293 if (mystatus == offline || st == offline) |
|
294 update_roster = TRUE; |
|
295 |
116
|
296 hk_mystatuschange(0, mystatus, st); |
|
297 mystatus = st; |
29
|
298 } |
|
299 |
35
|
300 void jb_send_msg(const char *jid, const char *text) |
|
301 { |
213
|
302 char *buffer = utf8_encode(text); |
|
303 xmlnode x = jutil_msgnew(TMSG_CHAT, (char*)jid, 0, (char*)buffer); |
35
|
304 jab_send(jc, x); |
|
305 xmlnode_free(x); |
213
|
306 free(buffer); |
114
|
307 jb_reset_keepalive(); |
35
|
308 } |
|
309 |
213
|
310 // Note: the caller should check the jid is correct |
244
|
311 void jb_addbuddy(const char *jid, const char *name, const char *group) |
124
|
312 { |
|
313 xmlnode x, y, z; |
192
|
314 char *cleanjid; |
124
|
315 |
208
|
316 if (!online) return; |
124
|
317 |
|
318 // We don't check if the jabber user already exists in the roster, |
|
319 // because it allows to re-ask for notification. |
|
320 |
|
321 //x = jutil_presnew(JPACKET__SUBSCRIBE, jid, NULL); |
125
|
322 x = jutil_presnew(JPACKET__SUBSCRIBE, (char*)jid, "online"); |
124
|
323 jab_send(jc, x); |
|
324 xmlnode_free(x); |
|
325 |
|
326 x = jutil_iqnew(JPACKET__SET, NS_ROSTER); |
|
327 y = xmlnode_get_tag(x, "query"); |
|
328 z = xmlnode_insert_tag(y, "item"); |
|
329 xmlnode_put_attrib(z, "jid", jid); |
|
330 |
244
|
331 if (name) { |
|
332 char *name_utf8 = utf8_encode(name); |
|
333 z = xmlnode_insert_tag(z, "name"); |
|
334 xmlnode_insert_cdata(z, name_utf8, (unsigned) -1); |
|
335 free(name_utf8); |
|
336 } |
|
337 |
124
|
338 if (group) { |
213
|
339 char *group_utf8 = utf8_encode(group); |
124
|
340 z = xmlnode_insert_tag(z, "group"); |
213
|
341 xmlnode_insert_cdata(z, group_utf8, (unsigned) -1); |
|
342 free(group_utf8); |
124
|
343 } |
|
344 |
|
345 jab_send(jc, x); |
|
346 xmlnode_free(x); |
|
347 |
192
|
348 cleanjid = jidtodisp(jid); |
244
|
349 roster_add_user(cleanjid, name, group, ROSTER_TYPE_USER); |
192
|
350 g_free(cleanjid); |
124
|
351 buddylist_build(); |
|
352 |
244
|
353 update_roster = TRUE; |
124
|
354 } |
|
355 |
205
|
356 void jb_delbuddy(const char *jid) |
|
357 { |
|
358 xmlnode x, y, z; |
|
359 char *cleanjid; |
|
360 |
|
361 if (!online) return; |
|
362 |
|
363 cleanjid = jidtodisp(jid); |
|
364 |
|
365 // If the current buddy is an agent, unsubscribe from it |
|
366 if (roster_gettype(cleanjid) == ROSTER_TYPE_AGENT) { |
|
367 scr_LogPrint("Unregistering from the %s agent", cleanjid); |
|
368 |
|
369 x = jutil_iqnew(JPACKET__SET, NS_REGISTER); |
|
370 xmlnode_put_attrib(x, "to", cleanjid); |
|
371 y = xmlnode_get_tag(x, "query"); |
|
372 xmlnode_insert_tag(y, "remove"); |
|
373 jab_send(jc, x); |
|
374 xmlnode_free(x); |
|
375 } |
|
376 |
|
377 // Unsubscribe this buddy from our presence notification |
|
378 x = jutil_presnew(JPACKET__UNSUBSCRIBE, cleanjid, 0); |
|
379 jab_send(jc, x); |
|
380 xmlnode_free(x); |
|
381 |
|
382 // Ask for removal from roster |
|
383 x = jutil_iqnew(JPACKET__SET, NS_ROSTER); |
|
384 y = xmlnode_get_tag(x, "query"); |
|
385 z = xmlnode_insert_tag(y, "item"); |
|
386 xmlnode_put_attrib(z, "jid", cleanjid); |
|
387 xmlnode_put_attrib(z, "subscription", "remove"); |
|
388 jab_send(jc, x); |
|
389 xmlnode_free(x); |
|
390 |
|
391 roster_del_user(cleanjid); |
|
392 g_free(cleanjid); |
|
393 buddylist_build(); |
|
394 |
|
395 update_roster = TRUE; |
|
396 } |
|
397 |
208
|
398 void jb_updatebuddy(const char *jid, const char *name, const char *group) |
|
399 { |
|
400 xmlnode x, y; |
|
401 char *cleanjid; |
213
|
402 char *name_utf8; |
208
|
403 |
|
404 if (!online) return; |
|
405 |
|
406 // XXX We should check name's and group's correctness |
|
407 |
|
408 cleanjid = jidtodisp(jid); |
213
|
409 name_utf8 = utf8_encode(name); |
208
|
410 |
|
411 x = jutil_iqnew(JPACKET__SET, NS_ROSTER); |
|
412 y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item"); |
|
413 xmlnode_put_attrib(y, "jid", cleanjid); |
213
|
414 xmlnode_put_attrib(y, "name", name_utf8); |
208
|
415 |
|
416 if (group) { |
213
|
417 char *group_utf8 = utf8_encode(group); |
208
|
418 y = xmlnode_insert_tag(y, "group"); |
213
|
419 xmlnode_insert_cdata(y, group_utf8, (unsigned) -1); |
|
420 free(group_utf8); |
208
|
421 } |
|
422 |
|
423 jab_send(jc, x); |
|
424 xmlnode_free(x); |
213
|
425 free(name_utf8); |
208
|
426 g_free(cleanjid); |
|
427 } |
|
428 |
31
|
429 void postlogin() |
|
430 { |
|
431 //int i; |
|
432 |
|
433 //flogged = TRUE; |
|
434 //ourstatus = available; |
|
435 |
|
436 //setautostatus(jhook.manualstatus); |
|
437 |
116
|
438 jb_setstatus(available, "I'm here!"); // XXX not always "available"... |
81
|
439 buddylist_build(); |
31
|
440 /* |
|
441 for (i = 0; i < clist.count; i++) { |
|
442 c = (icqcontact *) clist.at(i); |
|
443 |
|
444 if (c->getdesc().pname == proto) |
|
445 if (ischannel(c)) |
|
446 if (c->getbasicinfo().requiresauth) |
|
447 c->setstatus(available); |
|
448 } |
|
449 */ |
|
450 |
|
451 /* |
|
452 agents.insert(agents.begin(), agent("vcard", "Jabber VCard", "", agent::atStandard)); |
|
453 agents.begin()->params[agent::ptRegister].enabled = TRUE; |
|
454 |
|
455 string buf; |
|
456 ifstream f(conf.getconfigfname("jabber-infoset").c_str()); |
|
457 |
|
458 if (f.is_open()) { |
|
459 icqcontact *c = clist.get(contactroot); |
|
460 |
|
461 c->clear(); |
|
462 icqcontact::basicinfo bi = c->getbasicinfo(); |
|
463 icqcontact::reginfo ri = c->getreginfo(); |
|
464 |
|
465 ri.service = agents.begin()->name; |
|
466 getstring(f, buf); c->setnick(buf); |
|
467 getstring(f, buf); bi.email = buf; |
|
468 getstring(f, buf); bi.fname = buf; |
|
469 getstring(f, buf); bi.lname = buf; |
|
470 f.close(); |
|
471 |
|
472 c->setbasicinfo(bi); |
|
473 c->setreginfo(ri); |
|
474 |
|
475 sendupdateuserinfo(*c); |
|
476 unlink(conf.getconfigfname("jabber-infoset").c_str()); |
|
477 } |
|
478 */ |
|
479 } |
|
480 |
29
|
481 void gotloggedin(void) |
|
482 { |
|
483 xmlnode x; |
|
484 |
|
485 x = jutil_iqnew(JPACKET__GET, NS_AGENTS); |
|
486 xmlnode_put_attrib(x, "id", "Agent List"); |
|
487 jab_send(jc, x); |
|
488 xmlnode_free(x); |
|
489 |
|
490 x = jutil_iqnew(JPACKET__GET, NS_ROSTER); |
|
491 xmlnode_put_attrib(x, "id", "Roster"); |
|
492 jab_send(jc, x); |
|
493 xmlnode_free(x); |
|
494 } |
|
495 |
|
496 void gotroster(xmlnode x) |
|
497 { |
33
|
498 xmlnode y, z; |
29
|
499 |
|
500 for (y = xmlnode_get_tag(x, "item"); y; y = xmlnode_get_nextsibling(y)) { |
|
501 const char *alias = xmlnode_get_attrib(y, "jid"); |
51
|
502 //const char *sub = xmlnode_get_attrib(y, "subscription"); // TODO Not used |
29
|
503 const char *name = xmlnode_get_attrib(y, "name"); |
213
|
504 char *group = NULL; |
29
|
505 |
33
|
506 z = xmlnode_get_tag(y, "group"); |
|
507 if (z) group = xmlnode_get_data(z); |
29
|
508 |
|
509 if (alias) { |
35
|
510 char *buddyname; |
192
|
511 char *cleanalias = jidtodisp(alias); |
213
|
512 char *name_noutf8 = NULL; |
|
513 char *group_noutf8 = NULL; |
|
514 |
|
515 if (name) { |
|
516 name_noutf8 = utf8_decode(name); |
|
517 buddyname = name_noutf8; |
|
518 } else |
192
|
519 buddyname = cleanalias; |
35
|
520 |
213
|
521 if (group) group_noutf8 = utf8_decode(group); |
|
522 roster_add_user(cleanalias, buddyname, group_noutf8, ROSTER_TYPE_USER); |
|
523 if (name_noutf8) free(name_noutf8); |
|
524 if (group_noutf8) free(group_noutf8); |
192
|
525 g_free(cleanalias); |
29
|
526 } |
|
527 } |
|
528 |
|
529 postlogin(); |
|
530 } |
|
531 |
|
532 void gotmessage(char *type, const char *from, const char *body, |
223
|
533 const char *enc, time_t timestamp) |
29
|
534 { |
113
|
535 char *jid; |
213
|
536 char *buffer = utf8_decode(body); |
29
|
537 |
35
|
538 /* |
113
|
539 //char *u, *h, *r; |
|
540 //jidsplit(from, &u, &h, &r); |
35
|
541 // Maybe we should remember the resource? |
|
542 if (r) |
|
543 scr_LogPrint("There is an extra part in message (resource?): %s", r); |
113
|
544 //scr_LogPrint("Msg from <%s>, type=%s", jidtodisp(from), type); |
35
|
545 */ |
|
546 |
113
|
547 jid = jidtodisp(from); |
223
|
548 hk_message_in(jid, timestamp, buffer); |
192
|
549 g_free(jid); |
213
|
550 free(buffer); |
29
|
551 } |
|
552 |
31
|
553 void statehandler(jconn conn, int state) |
|
554 { |
|
555 static int previous_state = -1; |
|
556 |
35
|
557 ut_WriteLog("StateHandler called (state=%d).\n", state); |
31
|
558 |
|
559 switch(state) { |
|
560 case JCONN_STATE_OFF: |
192
|
561 if (previous_state != JCONN_STATE_OFF) |
|
562 scr_LogPrint("+ JCONN_STATE_OFF"); |
37
|
563 |
112
|
564 online = FALSE; |
159
|
565 mystatus = offline; |
164
|
566 roster_free(); |
|
567 update_roster = TRUE; |
31
|
568 break; |
|
569 |
|
570 case JCONN_STATE_CONNECTED: |
35
|
571 scr_LogPrint("+ JCONN_STATE_CONNECTED"); |
31
|
572 break; |
|
573 |
|
574 case JCONN_STATE_AUTH: |
35
|
575 scr_LogPrint("+ JCONN_STATE_AUTH"); |
31
|
576 break; |
|
577 |
|
578 case JCONN_STATE_ON: |
35
|
579 scr_LogPrint("+ JCONN_STATE_ON"); |
112
|
580 online = TRUE; |
31
|
581 break; |
|
582 |
35
|
583 case JCONN_STATE_CONNECTING: |
|
584 scr_LogPrint("+ JCONN_STATE_CONNECTING"); |
|
585 break; |
|
586 |
31
|
587 default: |
|
588 break; |
|
589 } |
|
590 previous_state = state; |
|
591 } |
|
592 |
|
593 void packethandler(jconn conn, jpacket packet) |
|
594 { |
91
|
595 char *p, *r; |
37
|
596 xmlnode x, y; |
31
|
597 char *from=NULL, *type=NULL, *body=NULL, *enc=NULL; |
|
598 char *ns=NULL; |
|
599 char *id=NULL; |
35
|
600 enum imstatus ust; |
31
|
601 // int npos; |
|
602 |
112
|
603 jb_reset_keepalive(); // reset keepalive delay |
31
|
604 jpacket_reset(packet); |
|
605 |
|
606 p = xmlnode_get_attrib(packet->x, "from"); if (p) from = p; |
|
607 p = xmlnode_get_attrib(packet->x, "type"); if (p) type = p; |
|
608 |
|
609 switch (packet->type) { |
|
610 case JPACKET_MESSAGE: |
52
|
611 { |
|
612 char *tmp = NULL; |
223
|
613 time_t timestamp = 0; |
|
614 |
52
|
615 x = xmlnode_get_tag(packet->x, "body"); |
|
616 p = xmlnode_get_data(x); if (p) body = p; |
31
|
617 |
52
|
618 if ((x = xmlnode_get_tag(packet->x, "subject")) != NULL) |
|
619 if ((p = xmlnode_get_data(x)) != NULL) { |
165
|
620 tmp = g_new(char, strlen(body)+strlen(p)+4); |
52
|
621 *tmp = '['; |
|
622 strcpy(tmp+1, p); |
|
623 strcat(tmp, "]\n"); |
|
624 strcat(tmp, body); |
|
625 body = tmp; |
|
626 } |
|
627 |
|
628 /* there can be multiple <x> tags. we're looking for one with |
|
629 xmlns = jabber:x:encrypted */ |
|
630 |
|
631 for (x = xmlnode_get_firstchild(packet->x); x; x = xmlnode_get_nextsibling(x)) { |
|
632 if ((p = xmlnode_get_name(x)) && !strcmp(p, "x")) |
|
633 if ((p = xmlnode_get_attrib(x, "xmlns")) && |
|
634 !strcasecmp(p, "jabber:x:encrypted")) |
|
635 if ((p = xmlnode_get_data(x)) != NULL) { |
|
636 enc = p; |
|
637 break; |
|
638 } |
31
|
639 } |
|
640 |
223
|
641 // Timestamp? |
|
642 if ((x = xmlnode_get_tag(packet->x, "x")) != NULL) { |
241
|
643 if ((p = xmlnode_get_attrib(x, "stamp")) != NULL) |
|
644 timestamp = from_iso8601(p, 1); |
223
|
645 } |
|
646 |
165
|
647 if (from && body) |
223
|
648 gotmessage(type, from, body, enc, timestamp); |
52
|
649 if (tmp) |
159
|
650 g_free(tmp); |
31
|
651 } |
|
652 break; |
|
653 |
|
654 case JPACKET_IQ: |
|
655 if (!strcmp(type, "result")) { |
|
656 |
51
|
657 if ((p = xmlnode_get_attrib(packet->x, "id")) != NULL) { |
31
|
658 int iid = atoi(p); |
|
659 |
|
660 ut_WriteLog("iid = %d\n", iid); |
|
661 if (iid == s_id) { |
|
662 if (!regmode) { |
|
663 if (jstate == STATE_GETAUTH) { |
51
|
664 if ((x = xmlnode_get_tag(packet->x, "query")) != NULL) |
31
|
665 if (!xmlnode_get_tag(x, "digest")) { |
|
666 jc->sid = 0; |
|
667 } |
|
668 |
|
669 s_id = atoi(jab_auth(jc)); |
|
670 jstate = STATE_SENDAUTH; |
|
671 } else { |
|
672 gotloggedin(); |
|
673 jstate = STATE_LOGGED; |
|
674 } |
|
675 } else { |
|
676 regdone = TRUE; |
|
677 } |
|
678 return; |
|
679 } |
|
680 |
|
681 if (!strcmp(p, "VCARDreq")) { |
|
682 x = xmlnode_get_firstchild(packet->x); |
|
683 if (!x) x = packet->x; |
|
684 |
|
685 //jhook.gotvcard(ic, x); TODO |
|
686 scr_LogPrint("Got VCARD"); |
|
687 return; |
|
688 } else if (!strcmp(p, "versionreq")) { |
|
689 // jhook.gotversion(ic, packet->x); TODO |
|
690 scr_LogPrint("Got version"); |
|
691 return; |
|
692 } |
|
693 } |
|
694 |
51
|
695 if ((x = xmlnode_get_tag(packet->x, "query")) != NULL) { |
31
|
696 p = xmlnode_get_attrib(x, "xmlns"); if (p) ns = p; |
|
697 |
|
698 if (!strcmp(ns, NS_ROSTER)) { |
|
699 gotroster(x); |
|
700 } else if (!strcmp(ns, NS_AGENTS)) { |
|
701 for (y = xmlnode_get_tag(x, "agent"); y; y = xmlnode_get_nextsibling(y)) { |
|
702 const char *alias = xmlnode_get_attrib(y, "jid"); |
|
703 |
|
704 if (alias) { |
|
705 const char *name = xmlnode_get_tag_data(y, "name"); |
|
706 const char *desc = xmlnode_get_tag_data(y, "description"); |
51
|
707 // const char *service = xmlnode_get_tag_data(y, "service"); TODO |
37
|
708 enum agtype atype = unknown; |
31
|
709 |
37
|
710 if (xmlnode_get_tag(y, "groupchat")) atype = groupchat; else |
|
711 if (xmlnode_get_tag(y, "transport")) atype = transport; else |
|
712 if (xmlnode_get_tag(y, "search")) atype = search; |
31
|
713 |
192
|
714 if (atype == transport) { |
|
715 char *cleanjid = jidtodisp(alias); |
194
|
716 roster_add_user(cleanjid, NULL, JABBER_AGENT_GROUP, |
|
717 ROSTER_TYPE_AGENT); |
192
|
718 g_free(cleanjid); |
|
719 } |
31
|
720 if (alias && name && desc) { |
37
|
721 scr_LogPrint("Agent: %s / %s / %s / type=%d", |
|
722 alias, name, desc, atype); |
31
|
723 |
37
|
724 if (atype == search) { |
31
|
725 x = jutil_iqnew (JPACKET__GET, NS_SEARCH); |
|
726 xmlnode_put_attrib(x, "to", alias); |
|
727 xmlnode_put_attrib(x, "id", "Agent info"); |
|
728 jab_send(conn, x); |
|
729 xmlnode_free(x); |
|
730 } |
|
731 |
|
732 if (xmlnode_get_tag(y, "register")) { |
|
733 x = jutil_iqnew (JPACKET__GET, NS_REGISTER); |
|
734 xmlnode_put_attrib(x, "to", alias); |
|
735 xmlnode_put_attrib(x, "id", "Agent info"); |
|
736 jab_send(conn, x); |
|
737 xmlnode_free(x); |
|
738 } |
|
739 } |
|
740 } |
|
741 } |
|
742 |
37
|
743 /* |
31
|
744 if (find(jhook.agents.begin(), jhook.agents.end(), DEFAULT_CONFSERV) == jhook.agents.end()) |
|
745 jhook.agents.insert(jhook.agents.begin(), agent(DEFAULT_CONFSERV, DEFAULT_CONFSERV, |
|
746 _("Default Jabber conference server"), agent::atGroupchat)); |
|
747 |
|
748 */ |
|
749 } else if (!strcmp(ns, NS_SEARCH) || !strcmp(ns, NS_REGISTER)) { |
|
750 p = xmlnode_get_attrib(packet->x, "id"); id = p ? p : (char*)""; |
|
751 |
|
752 if (!strcmp(id, "Agent info")) { |
|
753 // jhook.gotagentinfo(packet->x); TODO |
|
754 scr_LogPrint("Got agent info"); |
|
755 } else if (!strcmp(id, "Lookup")) { |
|
756 // jhook.gotsearchresults(packet->x); TODO |
|
757 scr_LogPrint("Got search results"); |
|
758 } else if (!strcmp(id, "Register")) { |
|
759 x = jutil_iqnew(JPACKET__GET, NS_REGISTER); |
|
760 xmlnode_put_attrib(x, "to", from); |
|
761 xmlnode_put_attrib(x, "id", "Agent info"); |
|
762 jab_send(conn, x); |
|
763 xmlnode_free(x); |
|
764 } |
|
765 |
|
766 } |
|
767 } |
|
768 } else if (!strcmp(type, "set")) { |
|
769 } else if (!strcmp(type, "error")) { |
|
770 char *name=NULL, *desc=NULL; |
52
|
771 int code = 0; |
31
|
772 |
|
773 x = xmlnode_get_tag(packet->x, "error"); |
|
774 p = xmlnode_get_attrib(x, "code"); if (p) code = atoi(p); |
|
775 p = xmlnode_get_attrib(x, "id"); if (p) name = p; |
|
776 p = xmlnode_get_tag_data(packet->x, "error"); if (p) desc = p; |
|
777 |
231
|
778 #if 0 |
31
|
779 switch(code) { |
|
780 case 401: /* Unauthorized */ |
|
781 case 302: /* Redirect */ |
|
782 case 400: /* Bad request */ |
|
783 case 402: /* Payment Required */ |
|
784 case 403: /* Forbidden */ |
|
785 case 404: /* Not Found */ |
|
786 case 405: /* Not Allowed */ |
|
787 case 406: /* Not Acceptable */ |
|
788 case 407: /* Registration Required */ |
|
789 case 408: /* Request Timeout */ |
|
790 case 409: /* Conflict */ |
|
791 case 500: /* Internal Server Error */ |
|
792 case 501: /* Not Implemented */ |
|
793 case 502: /* Remote Server Error */ |
|
794 case 503: /* Service Unavailable */ |
|
795 case 504: /* Remote Server Timeout */ |
|
796 default: |
|
797 /* |
|
798 if (!regmode) { |
|
799 face.log(desc.empty() ? |
|
800 _("+ [jab] error %d") : |
|
801 _("+ [jab] error %d: %s"), |
|
802 code, desc.c_str()); |
|
803 |
|
804 if (!jhook.flogged && code != 501) { |
|
805 close(jc->fd); |
|
806 jc->fd = -1; |
|
807 } |
|
808 |
|
809 } else { |
|
810 jhook.regerr = desc; |
|
811 |
|
812 } |
|
813 */ |
|
814 } |
231
|
815 #endif |
31
|
816 scr_LogPrint("Error code from server (%d)", code); |
|
817 |
|
818 } |
|
819 break; |
|
820 |
|
821 case JPACKET_PRESENCE: |
|
822 x = xmlnode_get_tag(packet->x, "show"); |
35
|
823 ust = available; |
31
|
824 |
|
825 if (x) { |
|
826 p = xmlnode_get_data(x); if (p) ns = p; |
|
827 |
|
828 if (ns) { |
35
|
829 if (!strcmp(ns, "away")) ust = away; |
|
830 else if (!strcmp(ns, "dnd")) ust = dontdisturb; |
|
831 else if (!strcmp(ns, "xa")) ust = notavail; |
|
832 else if (!strcmp(ns, "chat")) ust = freeforchat; |
31
|
833 } |
|
834 } |
|
835 |
35
|
836 if (type && !strcmp(type, "unavailable")) { |
|
837 ust = offline; |
31
|
838 } |
|
839 |
221
|
840 if ((x = xmlnode_get_tag(packet->x, "status")) != NULL) |
|
841 p = xmlnode_get_data(x); |
|
842 else |
|
843 p = NULL; |
|
844 |
91
|
845 r = jidtodisp(from); |
113
|
846 if (ust != roster_getstatus(r)) |
221
|
847 hk_statuschange(r, 0, ust, p); |
159
|
848 g_free(r); |
31
|
849 break; |
|
850 |
|
851 case JPACKET_S10N: |
205
|
852 scr_LogPrint("Received (un)subscription packet (type=%s)", |
|
853 ((type) ? type : "")); |
31
|
854 |
43
|
855 if (!strcmp(type, "subscribe")) { |
91
|
856 int isagent; |
|
857 r = jidtodisp(from); |
|
858 isagent = (roster_gettype(r) & ROSTER_TYPE_AGENT) != 0; |
159
|
859 g_free(r); |
91
|
860 scr_LogPrint("isagent=%d", isagent); // XXX DBG |
|
861 if (!isagent) { |
43
|
862 scr_LogPrint("<%s> wants to subscribe " |
|
863 "to your network presence updates", from); |
125
|
864 // FIXME we accept everybody... |
|
865 x = jutil_presnew(JPACKET__SUBSCRIBED, from, 0); |
|
866 jab_send(jc, x); |
|
867 xmlnode_free(x); |
91
|
868 } else { |
|
869 x = jutil_presnew(JPACKET__SUBSCRIBED, from, 0); |
31
|
870 jab_send(jc, x); |
|
871 xmlnode_free(x); |
91
|
872 } |
43
|
873 } else if (!strcmp(type, "unsubscribe")) { |
|
874 x = jutil_presnew(JPACKET__UNSUBSCRIBED, from, 0); |
31
|
875 jab_send(jc, x); |
|
876 xmlnode_free(x); |
43
|
877 scr_LogPrint("<%s> has unsubscribed to your presence updates", from); |
31
|
878 } |
|
879 break; |
|
880 |
|
881 default: |
|
882 break; |
|
883 } |
|
884 } |
|
885 |