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