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