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