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