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