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