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