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,
|
|
519 const char *enc)
|
|
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);
|
213
|
534 hk_message_in(jid, 0, 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;
|
|
599 x = xmlnode_get_tag(packet->x, "body");
|
|
600 p = xmlnode_get_data(x); if (p) body = p;
|
31
|
601
|
52
|
602 if ((x = xmlnode_get_tag(packet->x, "subject")) != NULL)
|
|
603 if ((p = xmlnode_get_data(x)) != NULL) {
|
165
|
604 tmp = g_new(char, strlen(body)+strlen(p)+4);
|
52
|
605 *tmp = '[';
|
|
606 strcpy(tmp+1, p);
|
|
607 strcat(tmp, "]\n");
|
|
608 strcat(tmp, body);
|
|
609 body = tmp;
|
|
610 }
|
|
611
|
|
612 /* there can be multiple <x> tags. we're looking for one with
|
|
613 xmlns = jabber:x:encrypted */
|
|
614
|
|
615 for (x = xmlnode_get_firstchild(packet->x); x; x = xmlnode_get_nextsibling(x)) {
|
|
616 if ((p = xmlnode_get_name(x)) && !strcmp(p, "x"))
|
|
617 if ((p = xmlnode_get_attrib(x, "xmlns")) &&
|
|
618 !strcasecmp(p, "jabber:x:encrypted"))
|
|
619 if ((p = xmlnode_get_data(x)) != NULL) {
|
|
620 enc = p;
|
|
621 break;
|
|
622 }
|
31
|
623 }
|
|
624
|
165
|
625 if (from && body)
|
52
|
626 gotmessage(type, from, body, enc);
|
|
627 if (tmp)
|
159
|
628 g_free(tmp);
|
31
|
629 }
|
|
630 break;
|
|
631
|
|
632 case JPACKET_IQ:
|
|
633 if (!strcmp(type, "result")) {
|
|
634
|
51
|
635 if ((p = xmlnode_get_attrib(packet->x, "id")) != NULL) {
|
31
|
636 int iid = atoi(p);
|
|
637
|
|
638 ut_WriteLog("iid = %d\n", iid);
|
|
639 if (iid == s_id) {
|
|
640 if (!regmode) {
|
|
641 if (jstate == STATE_GETAUTH) {
|
51
|
642 if ((x = xmlnode_get_tag(packet->x, "query")) != NULL)
|
31
|
643 if (!xmlnode_get_tag(x, "digest")) {
|
|
644 jc->sid = 0;
|
|
645 }
|
|
646
|
|
647 s_id = atoi(jab_auth(jc));
|
|
648 jstate = STATE_SENDAUTH;
|
|
649 } else {
|
|
650 gotloggedin();
|
|
651 jstate = STATE_LOGGED;
|
|
652 }
|
|
653 } else {
|
|
654 regdone = TRUE;
|
|
655 }
|
|
656 return;
|
|
657 }
|
|
658
|
|
659 if (!strcmp(p, "VCARDreq")) {
|
|
660 x = xmlnode_get_firstchild(packet->x);
|
|
661 if (!x) x = packet->x;
|
|
662
|
|
663 //jhook.gotvcard(ic, x); TODO
|
|
664 scr_LogPrint("Got VCARD");
|
|
665 return;
|
|
666 } else if (!strcmp(p, "versionreq")) {
|
|
667 // jhook.gotversion(ic, packet->x); TODO
|
|
668 scr_LogPrint("Got version");
|
|
669 return;
|
|
670 }
|
|
671 }
|
|
672
|
51
|
673 if ((x = xmlnode_get_tag(packet->x, "query")) != NULL) {
|
31
|
674 p = xmlnode_get_attrib(x, "xmlns"); if (p) ns = p;
|
|
675
|
|
676 if (!strcmp(ns, NS_ROSTER)) {
|
|
677 gotroster(x);
|
|
678 } else if (!strcmp(ns, NS_AGENTS)) {
|
|
679 for (y = xmlnode_get_tag(x, "agent"); y; y = xmlnode_get_nextsibling(y)) {
|
|
680 const char *alias = xmlnode_get_attrib(y, "jid");
|
|
681
|
|
682 if (alias) {
|
|
683 const char *name = xmlnode_get_tag_data(y, "name");
|
|
684 const char *desc = xmlnode_get_tag_data(y, "description");
|
51
|
685 // const char *service = xmlnode_get_tag_data(y, "service"); TODO
|
37
|
686 enum agtype atype = unknown;
|
31
|
687
|
37
|
688 if (xmlnode_get_tag(y, "groupchat")) atype = groupchat; else
|
|
689 if (xmlnode_get_tag(y, "transport")) atype = transport; else
|
|
690 if (xmlnode_get_tag(y, "search")) atype = search;
|
31
|
691
|
192
|
692 if (atype == transport) {
|
|
693 char *cleanjid = jidtodisp(alias);
|
194
|
694 roster_add_user(cleanjid, NULL, JABBER_AGENT_GROUP,
|
|
695 ROSTER_TYPE_AGENT);
|
192
|
696 g_free(cleanjid);
|
|
697 }
|
31
|
698 if (alias && name && desc) {
|
37
|
699 scr_LogPrint("Agent: %s / %s / %s / type=%d",
|
|
700 alias, name, desc, atype);
|
31
|
701
|
37
|
702 if (atype == search) {
|
31
|
703 x = jutil_iqnew (JPACKET__GET, NS_SEARCH);
|
|
704 xmlnode_put_attrib(x, "to", alias);
|
|
705 xmlnode_put_attrib(x, "id", "Agent info");
|
|
706 jab_send(conn, x);
|
|
707 xmlnode_free(x);
|
|
708 }
|
|
709
|
|
710 if (xmlnode_get_tag(y, "register")) {
|
|
711 x = jutil_iqnew (JPACKET__GET, NS_REGISTER);
|
|
712 xmlnode_put_attrib(x, "to", alias);
|
|
713 xmlnode_put_attrib(x, "id", "Agent info");
|
|
714 jab_send(conn, x);
|
|
715 xmlnode_free(x);
|
|
716 }
|
|
717 }
|
|
718 }
|
|
719 }
|
|
720
|
37
|
721 /*
|
31
|
722 if (find(jhook.agents.begin(), jhook.agents.end(), DEFAULT_CONFSERV) == jhook.agents.end())
|
|
723 jhook.agents.insert(jhook.agents.begin(), agent(DEFAULT_CONFSERV, DEFAULT_CONFSERV,
|
|
724 _("Default Jabber conference server"), agent::atGroupchat));
|
|
725
|
|
726 */
|
|
727 } else if (!strcmp(ns, NS_SEARCH) || !strcmp(ns, NS_REGISTER)) {
|
|
728 p = xmlnode_get_attrib(packet->x, "id"); id = p ? p : (char*)"";
|
|
729
|
|
730 if (!strcmp(id, "Agent info")) {
|
|
731 // jhook.gotagentinfo(packet->x); TODO
|
|
732 scr_LogPrint("Got agent info");
|
|
733 } else if (!strcmp(id, "Lookup")) {
|
|
734 // jhook.gotsearchresults(packet->x); TODO
|
|
735 scr_LogPrint("Got search results");
|
|
736 } else if (!strcmp(id, "Register")) {
|
|
737 x = jutil_iqnew(JPACKET__GET, NS_REGISTER);
|
|
738 xmlnode_put_attrib(x, "to", from);
|
|
739 xmlnode_put_attrib(x, "id", "Agent info");
|
|
740 jab_send(conn, x);
|
|
741 xmlnode_free(x);
|
|
742 }
|
|
743
|
|
744 }
|
|
745 }
|
|
746 } else if (!strcmp(type, "set")) {
|
|
747 } else if (!strcmp(type, "error")) {
|
|
748 char *name=NULL, *desc=NULL;
|
52
|
749 int code = 0;
|
31
|
750
|
|
751 x = xmlnode_get_tag(packet->x, "error");
|
|
752 p = xmlnode_get_attrib(x, "code"); if (p) code = atoi(p);
|
|
753 p = xmlnode_get_attrib(x, "id"); if (p) name = p;
|
|
754 p = xmlnode_get_tag_data(packet->x, "error"); if (p) desc = p;
|
|
755
|
|
756 switch(code) {
|
|
757 case 401: /* Unauthorized */
|
|
758 case 302: /* Redirect */
|
|
759 case 400: /* Bad request */
|
|
760 case 402: /* Payment Required */
|
|
761 case 403: /* Forbidden */
|
|
762 case 404: /* Not Found */
|
|
763 case 405: /* Not Allowed */
|
|
764 case 406: /* Not Acceptable */
|
|
765 case 407: /* Registration Required */
|
|
766 case 408: /* Request Timeout */
|
|
767 case 409: /* Conflict */
|
|
768 case 500: /* Internal Server Error */
|
|
769 case 501: /* Not Implemented */
|
|
770 case 502: /* Remote Server Error */
|
|
771 case 503: /* Service Unavailable */
|
|
772 case 504: /* Remote Server Timeout */
|
|
773 default:
|
|
774 /*
|
|
775 if (!regmode) {
|
|
776 face.log(desc.empty() ?
|
|
777 _("+ [jab] error %d") :
|
|
778 _("+ [jab] error %d: %s"),
|
|
779 code, desc.c_str());
|
|
780
|
|
781 if (!jhook.flogged && code != 501) {
|
|
782 close(jc->fd);
|
|
783 jc->fd = -1;
|
|
784 }
|
|
785
|
|
786 } else {
|
|
787 jhook.regerr = desc;
|
|
788
|
|
789 }
|
|
790 */
|
|
791 }
|
|
792 scr_LogPrint("Error code from server (%d)", code);
|
|
793
|
|
794 }
|
|
795 break;
|
|
796
|
|
797 case JPACKET_PRESENCE:
|
|
798 x = xmlnode_get_tag(packet->x, "show");
|
35
|
799 ust = available;
|
31
|
800
|
|
801 if (x) {
|
|
802 p = xmlnode_get_data(x); if (p) ns = p;
|
|
803
|
|
804 if (ns) {
|
35
|
805 if (!strcmp(ns, "away")) ust = away;
|
|
806 else if (!strcmp(ns, "dnd")) ust = dontdisturb;
|
|
807 else if (!strcmp(ns, "xa")) ust = notavail;
|
|
808 else if (!strcmp(ns, "chat")) ust = freeforchat;
|
31
|
809 }
|
|
810 }
|
|
811
|
35
|
812 if (type && !strcmp(type, "unavailable")) {
|
|
813 ust = offline;
|
31
|
814 }
|
|
815
|
91
|
816 r = jidtodisp(from);
|
113
|
817 if (ust != roster_getstatus(r))
|
|
818 hk_statuschange(r, 0, ust);
|
159
|
819 g_free(r);
|
31
|
820 /*
|
35
|
821 if (x = xmlnode_get_tag(packet->x, "status"))
|
|
822 if (p = xmlnode_get_data(x))
|
|
823 scr_LogPrint("Away msg: %s", p);
|
31
|
824 */
|
|
825 break;
|
|
826
|
|
827 case JPACKET_S10N:
|
205
|
828 scr_LogPrint("Received (un)subscription packet (type=%s)",
|
|
829 ((type) ? type : ""));
|
31
|
830
|
43
|
831 if (!strcmp(type, "subscribe")) {
|
91
|
832 int isagent;
|
|
833 r = jidtodisp(from);
|
|
834 isagent = (roster_gettype(r) & ROSTER_TYPE_AGENT) != 0;
|
159
|
835 g_free(r);
|
91
|
836 scr_LogPrint("isagent=%d", isagent); // XXX DBG
|
|
837 if (!isagent) {
|
43
|
838 scr_LogPrint("<%s> wants to subscribe "
|
|
839 "to your network presence updates", from);
|
125
|
840 // FIXME we accept everybody...
|
|
841 x = jutil_presnew(JPACKET__SUBSCRIBED, from, 0);
|
|
842 jab_send(jc, x);
|
|
843 xmlnode_free(x);
|
91
|
844 } else {
|
|
845 x = jutil_presnew(JPACKET__SUBSCRIBED, from, 0);
|
31
|
846 jab_send(jc, x);
|
|
847 xmlnode_free(x);
|
91
|
848 }
|
43
|
849 } else if (!strcmp(type, "unsubscribe")) {
|
|
850 x = jutil_presnew(JPACKET__UNSUBSCRIBED, from, 0);
|
31
|
851 jab_send(jc, x);
|
|
852 xmlnode_free(x);
|
43
|
853 scr_LogPrint("<%s> has unsubscribed to your presence updates", from);
|
31
|
854 }
|
|
855 break;
|
|
856
|
|
857 default:
|
|
858 break;
|
|
859 }
|
|
860 }
|
|
861
|