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