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