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