29
|
1 |
/* |
|
2 |
* jabglue.c -- Jabber protocol handling |
|
3 |
* |
|
4 |
* Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net> |
|
5 |
* Parts come from the centericq project: |
|
6 |
* Copyright (C) 2002-2005 by Konstantin Klyagin <konst@konst.org.ua> |
|
7 |
* |
|
8 |
* This program is free software; you can redistribute it and/or modify |
|
9 |
* it under the terms of the GNU General Public License as published by |
|
10 |
* the Free Software Foundation; either version 2 of the License, or (at |
|
11 |
* your option) any later version. |
|
12 |
* |
|
13 |
* This program is distributed in the hope that it will be useful, but |
|
14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 |
* General Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License |
|
19 |
* along with this program; if not, write to the Free Software |
|
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
21 |
* USA |
|
22 |
*/ |
|
23 |
|
|
24 |
#include "../libjabber/jabber.h" |
|
25 |
#include "jabglue.h" |
81
|
26 |
#include "roster.h" |
29
|
27 |
#include "screen.h" |
113
|
28 |
#include "hooks.h" |
29
|
29 |
#include "utils.h" |
|
30 |
|
|
31 |
#define JABBERPORT 5222 |
|
32 |
#define JABBERSSLPORT 5223 |
|
33 |
|
|
34 |
jconn jc; |
112
|
35 |
time_t LastPingTime; |
|
36 |
unsigned int KeepaliveDelay; |
31
|
37 |
static int s_id = 1; // FIXME which use?? |
|
38 |
static int regmode, regdone; |
116
|
39 |
static enum imstatus mystatus = offline; |
112
|
40 |
unsigned char online; |
29
|
41 |
|
46
|
42 |
char imstatus2char[imstatus_size] = { |
|
43 |
'_', 'o', 'i', 'f', 'd', 'c', 'n', 'a' |
|
44 |
}; |
|
45 |
|
31
|
46 |
static enum { |
29
|
47 |
STATE_CONNECTING, |
|
48 |
STATE_GETAUTH, |
|
49 |
STATE_SENDAUTH, |
|
50 |
STATE_LOGGED |
|
51 |
} jstate; |
|
52 |
|
|
53 |
|
31
|
54 |
void statehandler(jconn, int); |
|
55 |
void packethandler(jconn, jpacket); |
|
56 |
|
|
57 |
void screen_logger(jconn j, int io, const char *buf) |
|
58 |
{ |
36
|
59 |
scr_LogPrint("%03s: %s", ((io == 0) ? "OUT" : "IN"), buf); |
31
|
60 |
} |
|
61 |
|
|
62 |
void file_logger(jconn j, int io, const char *buf) |
|
63 |
{ |
36
|
64 |
ut_WriteLog("%03s: %s\n", ((io == 0) ? "OUT" : "IN"), buf); |
31
|
65 |
} |
|
66 |
|
|
67 |
void big_logger(jconn j, int io, const char *buf) |
|
68 |
{ |
|
69 |
screen_logger(j, io, buf); |
|
70 |
file_logger(j, io, buf); |
|
71 |
} |
|
72 |
|
113
|
73 |
/* |
29
|
74 |
static void jidsplit(const char *jid, char **user, char **host, |
|
75 |
char **res) |
|
76 |
{ |
|
77 |
char *tmp, *ptr; |
|
78 |
tmp = strdup(jid); |
|
79 |
|
|
80 |
if ((ptr = strchr(tmp, '/')) != NULL) { |
|
81 |
*res = strdup(ptr+1); |
|
82 |
*ptr = 0; |
|
83 |
} else |
|
84 |
*res = NULL; |
|
85 |
|
|
86 |
if ((ptr = strchr(tmp, '@')) != NULL) { |
|
87 |
*host = strdup(ptr+1); |
|
88 |
*ptr = 0; |
|
89 |
} else |
|
90 |
*host = NULL; |
|
91 |
|
|
92 |
*user = strdup(tmp); |
|
93 |
free(tmp); |
|
94 |
} |
113
|
95 |
*/ |
29
|
96 |
|
|
97 |
char *jidtodisp(const char *jid) |
|
98 |
{ |
|
99 |
char *ptr; |
|
100 |
char *alias = strdup(jid); |
|
101 |
if ((ptr = strchr(alias, '/')) != NULL) { |
|
102 |
*ptr = 0; |
|
103 |
} |
|
104 |
return alias; |
|
105 |
} |
|
106 |
|
35
|
107 |
jconn jb_connect(const char *jid, unsigned int port, int ssl, const char *pass) |
29
|
108 |
{ |
|
109 |
if (!port) { |
|
110 |
if (ssl) |
|
111 |
port = JABBERSSLPORT; |
|
112 |
else |
|
113 |
port = JABBERPORT; |
|
114 |
} |
|
115 |
|
|
116 |
if (jc) |
|
117 |
free(jc); |
|
118 |
|
51
|
119 |
jc = jab_new((char*)jid, (char*)pass, port, ssl); |
29
|
120 |
|
35
|
121 |
jab_logger(jc, file_logger); |
29
|
122 |
jab_packet_handler(jc, &packethandler); |
|
123 |
jab_state_handler(jc, &statehandler); |
|
124 |
|
|
125 |
if (jc->user) { |
112
|
126 |
online = TRUE; |
29
|
127 |
jstate = STATE_CONNECTING; |
|
128 |
statehandler(0, -1); |
|
129 |
jab_start(jc); |
|
130 |
} |
|
131 |
|
|
132 |
return jc; |
|
133 |
} |
|
134 |
|
|
135 |
void jb_disconnect(void) |
|
136 |
{ |
|
137 |
statehandler(jc, JCONN_STATE_OFF); |
|
138 |
} |
|
139 |
|
112
|
140 |
inline void jb_reset_keepalive() |
|
141 |
{ |
|
142 |
time(&LastPingTime); |
|
143 |
} |
|
144 |
|
29
|
145 |
void jb_keepalive() |
|
146 |
{ |
113
|
147 |
if (jc && online) |
112
|
148 |
jab_send_raw(jc, " \t "); |
|
149 |
jb_reset_keepalive(); |
|
150 |
} |
|
151 |
|
|
152 |
void jb_set_keepalive_delay(unsigned int delay) |
|
153 |
{ |
|
154 |
KeepaliveDelay = delay; |
29
|
155 |
} |
|
156 |
|
|
157 |
void jb_main() |
|
158 |
{ |
|
159 |
xmlnode x, z; |
|
160 |
char *cid; |
|
161 |
|
112
|
162 |
if (!online) |
|
163 |
return; |
29
|
164 |
if (jc && jc->state == JCONN_STATE_CONNECTING) { |
|
165 |
jab_start(jc); |
|
166 |
return; |
|
167 |
} |
|
168 |
|
31
|
169 |
jab_poll(jc, 50); |
29
|
170 |
|
|
171 |
if (jstate == STATE_CONNECTING) { |
|
172 |
if (jc) { |
|
173 |
x = jutil_iqnew(JPACKET__GET, NS_AUTH); |
|
174 |
cid = jab_getid(jc); |
|
175 |
xmlnode_put_attrib(x, "id", cid); |
|
176 |
// id = atoi(cid); |
|
177 |
|
|
178 |
z = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "username"); |
|
179 |
xmlnode_insert_cdata(z, jc->user->user, (unsigned) -1); |
|
180 |
jab_send(jc, x); |
|
181 |
xmlnode_free(x); |
|
182 |
|
|
183 |
jstate = STATE_GETAUTH; |
|
184 |
} |
|
185 |
|
|
186 |
if (!jc || jc->state == JCONN_STATE_OFF) { |
|
187 |
scr_LogPrint("Unable to connect to the server"); |
112
|
188 |
online = FALSE; |
29
|
189 |
} |
|
190 |
} |
|
191 |
|
|
192 |
if (!jc) { |
|
193 |
statehandler(jc, JCONN_STATE_OFF); |
|
194 |
} else if (jc->state == JCONN_STATE_OFF || jc->fd == -1) { |
|
195 |
statehandler(jc, JCONN_STATE_OFF); |
|
196 |
} |
112
|
197 |
|
|
198 |
// Keepalive |
|
199 |
if (KeepaliveDelay) { |
|
200 |
time_t now; |
|
201 |
time(&now); |
|
202 |
if (now > LastPingTime + KeepaliveDelay) |
|
203 |
jb_keepalive(); |
|
204 |
} |
29
|
205 |
} |
|
206 |
|
116
|
207 |
inline enum imstatus jb_getstatus() |
|
208 |
{ |
|
209 |
return mystatus; |
|
210 |
} |
|
211 |
|
|
212 |
void jb_setstatus(enum imstatus st, char *msg) |
29
|
213 |
{ |
116
|
214 |
xmlnode x; |
|
215 |
|
|
216 |
if (!online) |
|
217 |
return; |
|
218 |
|
|
219 |
x = jutil_presnew(JPACKET__UNKNOWN, 0, 0); |
29
|
220 |
|
|
221 |
switch(st) { |
|
222 |
case away: |
|
223 |
xmlnode_insert_cdata(xmlnode_insert_tag(x, "show"), "away", |
|
224 |
(unsigned) -1); |
|
225 |
break; |
|
226 |
|
|
227 |
case occupied: |
|
228 |
case dontdisturb: |
|
229 |
xmlnode_insert_cdata(xmlnode_insert_tag(x, "show"), "dnd", |
|
230 |
(unsigned) -1); |
|
231 |
break; |
|
232 |
|
|
233 |
case freeforchat: |
|
234 |
xmlnode_insert_cdata(xmlnode_insert_tag(x, "show"), "chat", |
|
235 |
(unsigned) -1); |
|
236 |
break; |
|
237 |
|
|
238 |
case notavail: |
|
239 |
xmlnode_insert_cdata(xmlnode_insert_tag(x, "show"), "xa", |
|
240 |
(unsigned) -1); |
|
241 |
break; |
|
242 |
|
|
243 |
case invisible: |
|
244 |
xmlnode_put_attrib(x, "type", "invisible"); |
|
245 |
break; |
51
|
246 |
|
|
247 |
default: |
|
248 |
break; |
29
|
249 |
} |
|
250 |
|
31
|
251 |
/* TODO |
|
252 |
if (!add["prio"].empty()) |
29
|
253 |
xmlnode_insert_cdata(xmlnode_insert_tag(x, "priority"), |
|
254 |
add["prio"].c_str(), (unsigned) -1); |
|
255 |
*/ |
|
256 |
|
116
|
257 |
if (!msg) { |
51
|
258 |
msg = ""; // FIXME |
29
|
259 |
//msg = imstatus2str(st); |
|
260 |
} |
|
261 |
|
|
262 |
xmlnode_insert_cdata(xmlnode_insert_tag(x, "status"), msg, |
|
263 |
(unsigned) -1); |
|
264 |
|
|
265 |
jab_send(jc, x); |
|
266 |
xmlnode_free(x); |
|
267 |
|
116
|
268 |
//sendvisibility(); ??? |
29
|
269 |
|
116
|
270 |
hk_mystatuschange(0, mystatus, st); |
|
271 |
mystatus = st; |
29
|
272 |
} |
|
273 |
|
35
|
274 |
void jb_send_msg(const char *jid, const char *text) |
|
275 |
{ |
51
|
276 |
xmlnode x = jutil_msgnew(TMSG_CHAT, (char*)jid, 0, (char*)text); |
35
|
277 |
jab_send(jc, x); |
|
278 |
xmlnode_free(x); |
114
|
279 |
jb_reset_keepalive(); |
35
|
280 |
} |
|
281 |
|
31
|
282 |
void postlogin() |
|
283 |
{ |
|
284 |
//int i; |
|
285 |
|
|
286 |
//flogged = TRUE; |
|
287 |
//ourstatus = available; |
|
288 |
|
|
289 |
//setautostatus(jhook.manualstatus); |
|
290 |
|
116
|
291 |
jb_setstatus(available, "I'm here!"); // XXX not always "available"... |
81
|
292 |
buddylist_build(); |
31
|
293 |
/* |
|
294 |
for (i = 0; i < clist.count; i++) { |
|
295 |
c = (icqcontact *) clist.at(i); |
|
296 |
|
|
297 |
if (c->getdesc().pname == proto) |
|
298 |
if (ischannel(c)) |
|
299 |
if (c->getbasicinfo().requiresauth) |
|
300 |
c->setstatus(available); |
|
301 |
} |
|
302 |
*/ |
|
303 |
|
|
304 |
/* |
|
305 |
agents.insert(agents.begin(), agent("vcard", "Jabber VCard", "", agent::atStandard)); |
|
306 |
agents.begin()->params[agent::ptRegister].enabled = TRUE; |
|
307 |
|
|
308 |
string buf; |
|
309 |
ifstream f(conf.getconfigfname("jabber-infoset").c_str()); |
|
310 |
|
|
311 |
if (f.is_open()) { |
|
312 |
icqcontact *c = clist.get(contactroot); |
|
313 |
|
|
314 |
c->clear(); |
|
315 |
icqcontact::basicinfo bi = c->getbasicinfo(); |
|
316 |
icqcontact::reginfo ri = c->getreginfo(); |
|
317 |
|
|
318 |
ri.service = agents.begin()->name; |
|
319 |
getstring(f, buf); c->setnick(buf); |
|
320 |
getstring(f, buf); bi.email = buf; |
|
321 |
getstring(f, buf); bi.fname = buf; |
|
322 |
getstring(f, buf); bi.lname = buf; |
|
323 |
f.close(); |
|
324 |
|
|
325 |
c->setbasicinfo(bi); |
|
326 |
c->setreginfo(ri); |
|
327 |
|
|
328 |
sendupdateuserinfo(*c); |
|
329 |
unlink(conf.getconfigfname("jabber-infoset").c_str()); |
|
330 |
} |
|
331 |
*/ |
|
332 |
} |
|
333 |
|
29
|
334 |
void gotloggedin(void) |
|
335 |
{ |
|
336 |
xmlnode x; |
|
337 |
|
|
338 |
x = jutil_iqnew(JPACKET__GET, NS_AGENTS); |
|
339 |
xmlnode_put_attrib(x, "id", "Agent List"); |
|
340 |
jab_send(jc, x); |
|
341 |
xmlnode_free(x); |
|
342 |
|
|
343 |
x = jutil_iqnew(JPACKET__GET, NS_ROSTER); |
|
344 |
xmlnode_put_attrib(x, "id", "Roster"); |
|
345 |
jab_send(jc, x); |
|
346 |
xmlnode_free(x); |
|
347 |
} |
|
348 |
|
|
349 |
void gotroster(xmlnode x) |
|
350 |
{ |
33
|
351 |
xmlnode y, z; |
29
|
352 |
|
|
353 |
for (y = xmlnode_get_tag(x, "item"); y; y = xmlnode_get_nextsibling(y)) { |
|
354 |
const char *alias = xmlnode_get_attrib(y, "jid"); |
51
|
355 |
//const char *sub = xmlnode_get_attrib(y, "subscription"); // TODO Not used |
29
|
356 |
const char *name = xmlnode_get_attrib(y, "name"); |
81
|
357 |
const char *group = NULL; |
29
|
358 |
|
33
|
359 |
z = xmlnode_get_tag(y, "group"); |
|
360 |
if (z) group = xmlnode_get_data(z); |
29
|
361 |
|
|
362 |
if (alias) { |
35
|
363 |
char *buddyname; |
|
364 |
if (name) |
51
|
365 |
buddyname = (char*)name; |
35
|
366 |
else |
|
367 |
buddyname = jidtodisp(alias); |
|
368 |
|
81
|
369 |
roster_add_user(alias, buddyname, group, ROSTER_TYPE_USER); |
35
|
370 |
if (!name) |
29
|
371 |
free(buddyname); |
|
372 |
} |
|
373 |
} |
|
374 |
|
|
375 |
postlogin(); |
|
376 |
} |
|
377 |
|
|
378 |
void gotmessage(char *type, const char *from, const char *body, |
|
379 |
const char *enc) |
|
380 |
{ |
113
|
381 |
char *jid; |
29
|
382 |
|
35
|
383 |
/* |
113
|
384 |
//char *u, *h, *r; |
|
385 |
//jidsplit(from, &u, &h, &r); |
35
|
386 |
// Maybe we should remember the resource? |
|
387 |
if (r) |
|
388 |
scr_LogPrint("There is an extra part in message (resource?): %s", r); |
113
|
389 |
//scr_LogPrint("Msg from <%s>, type=%s", jidtodisp(from), type); |
35
|
390 |
*/ |
|
391 |
|
113
|
392 |
jid = jidtodisp(from); |
|
393 |
hk_message_in(jid, 0, body); |
|
394 |
free(jid); |
29
|
395 |
} |
|
396 |
|
31
|
397 |
void statehandler(jconn conn, int state) |
|
398 |
{ |
|
399 |
static int previous_state = -1; |
|
400 |
|
35
|
401 |
ut_WriteLog("StateHandler called (state=%d).\n", state); |
31
|
402 |
|
|
403 |
switch(state) { |
|
404 |
case JCONN_STATE_OFF: |
37
|
405 |
|
112
|
406 |
online = FALSE; |
31
|
407 |
|
37
|
408 |
if (previous_state != JCONN_STATE_OFF) { |
|
409 |
scr_LogPrint("+ JCONN_STATE_OFF"); |
|
410 |
/* |
31
|
411 |
jhook.roster.clear(); |
|
412 |
jhook.agents.clear(); |
37
|
413 |
*/ |
|
414 |
} |
31
|
415 |
break; |
|
416 |
|
|
417 |
case JCONN_STATE_CONNECTED: |
35
|
418 |
scr_LogPrint("+ JCONN_STATE_CONNECTED"); |
31
|
419 |
break; |
|
420 |
|
|
421 |
case JCONN_STATE_AUTH: |
35
|
422 |
scr_LogPrint("+ JCONN_STATE_AUTH"); |
31
|
423 |
break; |
|
424 |
|
|
425 |
case JCONN_STATE_ON: |
35
|
426 |
scr_LogPrint("+ JCONN_STATE_ON"); |
112
|
427 |
online = TRUE; |
31
|
428 |
break; |
|
429 |
|
35
|
430 |
case JCONN_STATE_CONNECTING: |
|
431 |
scr_LogPrint("+ JCONN_STATE_CONNECTING"); |
|
432 |
break; |
|
433 |
|
31
|
434 |
default: |
|
435 |
break; |
|
436 |
} |
|
437 |
previous_state = state; |
|
438 |
} |
|
439 |
|
|
440 |
void packethandler(jconn conn, jpacket packet) |
|
441 |
{ |
91
|
442 |
char *p, *r; |
37
|
443 |
xmlnode x, y; |
31
|
444 |
// string from, type, body, enc, ns, id, u, h, s; |
|
445 |
char *from=NULL, *type=NULL, *body=NULL, *enc=NULL; |
|
446 |
char *ns=NULL; |
|
447 |
char *id=NULL; |
35
|
448 |
enum imstatus ust; |
31
|
449 |
// int npos; |
|
450 |
|
112
|
451 |
jb_reset_keepalive(); // reset keepalive delay |
31
|
452 |
jpacket_reset(packet); |
|
453 |
|
|
454 |
p = xmlnode_get_attrib(packet->x, "from"); if (p) from = p; |
|
455 |
p = xmlnode_get_attrib(packet->x, "type"); if (p) type = p; |
|
456 |
|
|
457 |
switch (packet->type) { |
|
458 |
case JPACKET_MESSAGE: |
52
|
459 |
{ |
|
460 |
char *tmp = NULL; |
|
461 |
x = xmlnode_get_tag(packet->x, "body"); |
|
462 |
p = xmlnode_get_data(x); if (p) body = p; |
31
|
463 |
|
52
|
464 |
if ((x = xmlnode_get_tag(packet->x, "subject")) != NULL) |
|
465 |
if ((p = xmlnode_get_data(x)) != NULL) { |
|
466 |
tmp = malloc(strlen(body)+strlen(p)+3); |
|
467 |
*tmp = '['; |
|
468 |
strcpy(tmp+1, p); |
|
469 |
strcat(tmp, "]\n"); |
|
470 |
strcat(tmp, body); |
|
471 |
body = tmp; |
|
472 |
} |
|
473 |
|
|
474 |
/* there can be multiple <x> tags. we're looking for one with |
|
475 |
xmlns = jabber:x:encrypted */ |
|
476 |
|
|
477 |
for (x = xmlnode_get_firstchild(packet->x); x; x = xmlnode_get_nextsibling(x)) { |
|
478 |
if ((p = xmlnode_get_name(x)) && !strcmp(p, "x")) |
|
479 |
if ((p = xmlnode_get_attrib(x, "xmlns")) && |
|
480 |
!strcasecmp(p, "jabber:x:encrypted")) |
|
481 |
if ((p = xmlnode_get_data(x)) != NULL) { |
|
482 |
enc = p; |
|
483 |
break; |
|
484 |
} |
31
|
485 |
} |
|
486 |
|
52
|
487 |
if (body) |
|
488 |
gotmessage(type, from, body, enc); |
|
489 |
if (tmp) |
|
490 |
free(tmp); |
31
|
491 |
} |
|
492 |
break; |
|
493 |
|
|
494 |
case JPACKET_IQ: |
|
495 |
if (!strcmp(type, "result")) { |
|
496 |
|
51
|
497 |
if ((p = xmlnode_get_attrib(packet->x, "id")) != NULL) { |
31
|
498 |
int iid = atoi(p); |
|
499 |
|
|
500 |
ut_WriteLog("iid = %d\n", iid); |
|
501 |
if (iid == s_id) { |
|
502 |
if (!regmode) { |
|
503 |
if (jstate == STATE_GETAUTH) { |
51
|
504 |
if ((x = xmlnode_get_tag(packet->x, "query")) != NULL) |
31
|
505 |
if (!xmlnode_get_tag(x, "digest")) { |
|
506 |
jc->sid = 0; |
|
507 |
} |
|
508 |
|
|
509 |
s_id = atoi(jab_auth(jc)); |
|
510 |
jstate = STATE_SENDAUTH; |
|
511 |
} else { |
|
512 |
gotloggedin(); |
|
513 |
jstate = STATE_LOGGED; |
|
514 |
} |
|
515 |
} else { |
|
516 |
regdone = TRUE; |
|
517 |
} |
|
518 |
return; |
|
519 |
} |
|
520 |
|
|
521 |
if (!strcmp(p, "VCARDreq")) { |
|
522 |
x = xmlnode_get_firstchild(packet->x); |
|
523 |
if (!x) x = packet->x; |
|
524 |
|
|
525 |
//jhook.gotvcard(ic, x); TODO |
|
526 |
scr_LogPrint("Got VCARD"); |
|
527 |
return; |
|
528 |
} else if (!strcmp(p, "versionreq")) { |
|
529 |
// jhook.gotversion(ic, packet->x); TODO |
|
530 |
scr_LogPrint("Got version"); |
|
531 |
return; |
|
532 |
} |
|
533 |
} |
|
534 |
|
51
|
535 |
if ((x = xmlnode_get_tag(packet->x, "query")) != NULL) { |
31
|
536 |
p = xmlnode_get_attrib(x, "xmlns"); if (p) ns = p; |
|
537 |
|
|
538 |
if (!strcmp(ns, NS_ROSTER)) { |
|
539 |
gotroster(x); |
|
540 |
} else if (!strcmp(ns, NS_AGENTS)) { |
|
541 |
for (y = xmlnode_get_tag(x, "agent"); y; y = xmlnode_get_nextsibling(y)) { |
|
542 |
const char *alias = xmlnode_get_attrib(y, "jid"); |
|
543 |
|
|
544 |
if (alias) { |
|
545 |
const char *name = xmlnode_get_tag_data(y, "name"); |
|
546 |
const char *desc = xmlnode_get_tag_data(y, "description"); |
51
|
547 |
// const char *service = xmlnode_get_tag_data(y, "service"); TODO |
37
|
548 |
enum agtype atype = unknown; |
31
|
549 |
|
37
|
550 |
if (xmlnode_get_tag(y, "groupchat")) atype = groupchat; else |
|
551 |
if (xmlnode_get_tag(y, "transport")) atype = transport; else |
|
552 |
if (xmlnode_get_tag(y, "search")) atype = search; |
31
|
553 |
|
|
554 |
if (alias && name && desc) { |
37
|
555 |
scr_LogPrint("Agent: %s / %s / %s / type=%d", |
|
556 |
alias, name, desc, atype); |
31
|
557 |
|
37
|
558 |
if (atype == search) { |
31
|
559 |
x = jutil_iqnew (JPACKET__GET, NS_SEARCH); |
|
560 |
xmlnode_put_attrib(x, "to", alias); |
|
561 |
xmlnode_put_attrib(x, "id", "Agent info"); |
|
562 |
jab_send(conn, x); |
|
563 |
xmlnode_free(x); |
|
564 |
} |
|
565 |
|
|
566 |
if (xmlnode_get_tag(y, "register")) { |
|
567 |
x = jutil_iqnew (JPACKET__GET, NS_REGISTER); |
|
568 |
xmlnode_put_attrib(x, "to", alias); |
|
569 |
xmlnode_put_attrib(x, "id", "Agent info"); |
|
570 |
jab_send(conn, x); |
|
571 |
xmlnode_free(x); |
|
572 |
} |
|
573 |
} |
|
574 |
} |
|
575 |
} |
|
576 |
|
37
|
577 |
/* |
31
|
578 |
if (find(jhook.agents.begin(), jhook.agents.end(), DEFAULT_CONFSERV) == jhook.agents.end()) |
|
579 |
jhook.agents.insert(jhook.agents.begin(), agent(DEFAULT_CONFSERV, DEFAULT_CONFSERV, |
|
580 |
_("Default Jabber conference server"), agent::atGroupchat)); |
|
581 |
|
|
582 |
*/ |
|
583 |
} else if (!strcmp(ns, NS_SEARCH) || !strcmp(ns, NS_REGISTER)) { |
|
584 |
p = xmlnode_get_attrib(packet->x, "id"); id = p ? p : (char*)""; |
|
585 |
|
|
586 |
if (!strcmp(id, "Agent info")) { |
|
587 |
// jhook.gotagentinfo(packet->x); TODO |
|
588 |
scr_LogPrint("Got agent info"); |
|
589 |
} else if (!strcmp(id, "Lookup")) { |
|
590 |
// jhook.gotsearchresults(packet->x); TODO |
|
591 |
scr_LogPrint("Got search results"); |
|
592 |
} else if (!strcmp(id, "Register")) { |
|
593 |
x = jutil_iqnew(JPACKET__GET, NS_REGISTER); |
|
594 |
xmlnode_put_attrib(x, "to", from); |
|
595 |
xmlnode_put_attrib(x, "id", "Agent info"); |
|
596 |
jab_send(conn, x); |
|
597 |
xmlnode_free(x); |
|
598 |
} |
|
599 |
|
|
600 |
} |
|
601 |
} |
|
602 |
} else if (!strcmp(type, "set")) { |
|
603 |
} else if (!strcmp(type, "error")) { |
|
604 |
char *name=NULL, *desc=NULL; |
52
|
605 |
int code = 0; |
31
|
606 |
|
|
607 |
x = xmlnode_get_tag(packet->x, "error"); |
|
608 |
p = xmlnode_get_attrib(x, "code"); if (p) code = atoi(p); |
|
609 |
p = xmlnode_get_attrib(x, "id"); if (p) name = p; |
|
610 |
p = xmlnode_get_tag_data(packet->x, "error"); if (p) desc = p; |
|
611 |
|
|
612 |
switch(code) { |
|
613 |
case 401: /* Unauthorized */ |
|
614 |
case 302: /* Redirect */ |
|
615 |
case 400: /* Bad request */ |
|
616 |
case 402: /* Payment Required */ |
|
617 |
case 403: /* Forbidden */ |
|
618 |
case 404: /* Not Found */ |
|
619 |
case 405: /* Not Allowed */ |
|
620 |
case 406: /* Not Acceptable */ |
|
621 |
case 407: /* Registration Required */ |
|
622 |
case 408: /* Request Timeout */ |
|
623 |
case 409: /* Conflict */ |
|
624 |
case 500: /* Internal Server Error */ |
|
625 |
case 501: /* Not Implemented */ |
|
626 |
case 502: /* Remote Server Error */ |
|
627 |
case 503: /* Service Unavailable */ |
|
628 |
case 504: /* Remote Server Timeout */ |
|
629 |
default: |
|
630 |
/* |
|
631 |
if (!regmode) { |
|
632 |
face.log(desc.empty() ? |
|
633 |
_("+ [jab] error %d") : |
|
634 |
_("+ [jab] error %d: %s"), |
|
635 |
code, desc.c_str()); |
|
636 |
|
|
637 |
if (!jhook.flogged && code != 501) { |
|
638 |
close(jc->fd); |
|
639 |
jc->fd = -1; |
|
640 |
} |
|
641 |
|
|
642 |
} else { |
|
643 |
jhook.regerr = desc; |
|
644 |
|
|
645 |
} |
|
646 |
*/ |
|
647 |
} |
|
648 |
scr_LogPrint("Error code from server (%d)", code); |
|
649 |
|
|
650 |
} |
|
651 |
break; |
|
652 |
|
|
653 |
case JPACKET_PRESENCE: |
|
654 |
x = xmlnode_get_tag(packet->x, "show"); |
35
|
655 |
ust = available; |
31
|
656 |
|
|
657 |
if (x) { |
|
658 |
p = xmlnode_get_data(x); if (p) ns = p; |
|
659 |
|
|
660 |
if (ns) { |
35
|
661 |
if (!strcmp(ns, "away")) ust = away; |
|
662 |
else if (!strcmp(ns, "dnd")) ust = dontdisturb; |
|
663 |
else if (!strcmp(ns, "xa")) ust = notavail; |
|
664 |
else if (!strcmp(ns, "chat")) ust = freeforchat; |
31
|
665 |
} |
|
666 |
} |
|
667 |
|
35
|
668 |
if (type && !strcmp(type, "unavailable")) { |
|
669 |
ust = offline; |
31
|
670 |
} |
|
671 |
|
91
|
672 |
r = jidtodisp(from); |
113
|
673 |
/* |
91
|
674 |
if (ust != roster_getstatus(r)) |
|
675 |
scr_LogPrint("Buddy status has changed: [%c>%c] <%s>", |
|
676 |
imstatus2char[roster_getstatus(r)], imstatus2char[ust], r); |
|
677 |
roster_setstatus(r, ust); |
113
|
678 |
*/ |
|
679 |
if (ust != roster_getstatus(r)) |
|
680 |
hk_statuschange(r, 0, ust); |
91
|
681 |
free(r); |
81
|
682 |
buddylist_build(); |
|
683 |
scr_DrawRoster(); |
31
|
684 |
/* |
35
|
685 |
if (x = xmlnode_get_tag(packet->x, "status")) |
|
686 |
if (p = xmlnode_get_data(x)) |
|
687 |
scr_LogPrint("Away msg: %s", p); |
31
|
688 |
*/ |
|
689 |
break; |
|
690 |
|
|
691 |
case JPACKET_S10N: |
|
692 |
scr_LogPrint("Received subscription packet"); |
43
|
693 |
if (type) scr_LogPrint("Type=%s", type); |
31
|
694 |
|
43
|
695 |
if (!strcmp(type, "subscribe")) { |
91
|
696 |
int isagent; |
|
697 |
r = jidtodisp(from); |
|
698 |
isagent = (roster_gettype(r) & ROSTER_TYPE_AGENT) != 0; |
|
699 |
free(r); |
|
700 |
scr_LogPrint("isagent=%d", isagent); // XXX DBG |
|
701 |
if (!isagent) { |
43
|
702 |
scr_LogPrint("<%s> wants to subscribe " |
|
703 |
"to your network presence updates", from); |
91
|
704 |
} else { |
|
705 |
x = jutil_presnew(JPACKET__SUBSCRIBED, from, 0); |
31
|
706 |
jab_send(jc, x); |
|
707 |
xmlnode_free(x); |
91
|
708 |
} |
43
|
709 |
} else if (!strcmp(type, "unsubscribe")) { |
|
710 |
x = jutil_presnew(JPACKET__UNSUBSCRIBED, from, 0); |
31
|
711 |
jab_send(jc, x); |
|
712 |
xmlnode_free(x); |
43
|
713 |
scr_LogPrint("<%s> has unsubscribed to your presence updates", from); |
31
|
714 |
} |
|
715 |
break; |
|
716 |
|
|
717 |
default: |
|
718 |
break; |
|
719 |
} |
|
720 |
} |
|
721 |
|