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