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