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