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