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