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