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