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