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