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