comparison mcabber/src/jabglue.c @ 29:86837ff0554c

[/trunk] Changeset 45 by mikael * Switch to libjabber. (Does NOT work at all yet) jabglue.c is a wrapper around the libjabber library.
author mikael
date Mon, 28 Mar 2005 20:12:48 +0000
parents
children 0f0fbd0c4a7f
comparison
equal deleted inserted replaced
28:0cd8025eebee 29:86837ff0554c
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
24 #include "../libjabber/jabber.h"
25 #include "jabglue.h"
26 #include "screen.h"
27 #include "utils.h"
28
29 #define JABBERPORT 5222
30 #define JABBERSSLPORT 5223
31
32 jconn jc;
33
34 enum {
35 STATE_CONNECTING,
36 STATE_GETAUTH,
37 STATE_SENDAUTH,
38 STATE_LOGGED
39 } jstate;
40
41
42 static void jidsplit(const char *jid, char **user, char **host,
43 char **res)
44 {
45 char *tmp, *ptr;
46 tmp = strdup(jid);
47
48 if ((ptr = strchr(tmp, '/')) != NULL) {
49 *res = strdup(ptr+1);
50 *ptr = 0;
51 } else
52 *res = NULL;
53
54 if ((ptr = strchr(tmp, '@')) != NULL) {
55 *host = strdup(ptr+1);
56 *ptr = 0;
57 } else
58 *host = NULL;
59
60 *user = strdup(tmp);
61 free(tmp);
62 }
63
64 char *jidtodisp(const char *jid)
65 {
66 char *ptr;
67 char *alias = strdup(jid);
68 if ((ptr = strchr(alias, '/')) != NULL) {
69 *ptr = 0;
70 }
71 return alias;
72 }
73
74 void statehandler(jconn conn, int state)
75 {
76 static int previous_state = -1;
77
78 switch(state) {
79 case JCONN_STATE_OFF:
80 /*
81 jhook.flogged = jhook.fonline = false;
82
83 if(previous_state != JCONN_STATE_OFF) {
84 logger.putourstatus(jhook.proto, jhook.getstatus(), jhook.ourstatus = offline);
85 jhook.log(logDisconnected);
86 jhook.roster.clear();
87 jhook.agents.clear();
88 clist.setoffline(jhook.proto);
89 face.update();
90 }
91 */
92 break;
93
94 case JCONN_STATE_CONNECTED:
95 break;
96
97 case JCONN_STATE_AUTH:
98 break;
99
100 case JCONN_STATE_ON:
101 // if(jhook.regmode) jhook.fonline = true;
102 break;
103
104 default:
105 break;
106 }
107 previous_state = state;
108 }
109
110 void packethandler(jconn conn, jpacket packet)
111 {
112 char *p;
113 xmlnode x, y;
114 // string from, type, body, enc, ns, id, u, h, s;
115 char *from=NULL, *type=NULL, *body=NULL, *enc=NULL;
116 char *ns=NULL;
117 //char *id=NULL;
118 // imstatus ust;
119 // int npos;
120 // bool isagent;
121
122 jpacket_reset(packet);
123
124 p = xmlnode_get_attrib(packet->x, "from"); if(p) from = p;
125 p = xmlnode_get_attrib(packet->x, "type"); if(p) type = p;
126 //imcontact ic(jidtodisp(from), jhook.proto);
127
128 switch (packet->type) {
129 case JPACKET_MESSAGE:
130 x = xmlnode_get_tag(packet->x, "body");
131 p = xmlnode_get_data(x); if(p) body = p;
132
133 if ((x = xmlnode_get_tag(packet->x, "subject")) != NULL)
134 if ((p = xmlnode_get_data(x)) != NULL) {
135 char *tmp = malloc(strlen(body)+strlen(p)+3);
136 strcpy(tmp, p);
137 strcat(tmp, ": ");
138 strcat(tmp, body);
139 body = tmp; // XXX check it is free'd later...
140 }
141
142 /* there can be multiple <x> tags. we're looking for one with
143 xmlns = jabber:x:encrypted */
144
145 for (x = xmlnode_get_firstchild(packet->x); x; x = xmlnode_get_nextsibling(x)) {
146 if ((p = xmlnode_get_name(x)) && !strcmp(p, "x"))
147 if ((p = xmlnode_get_attrib(x, "xmlns")) && !strcasecmp(p, "jabber:x:encrypted"))
148 if ((p = xmlnode_get_data(x)) != NULL) {
149 enc = p;
150 break;
151 }
152 }
153
154 // FIXME:
155 if (body) {
156 scr_LogPrint("Message received");
157 scr_LogPrint("Type: %s", type);
158 gotmessage(type, from, body, enc);
159 }
160
161 break;
162
163 case JPACKET_IQ:
164 if (!strcmp(type, "result")) {
165 scr_LogPrint("Received a result packet");
166 /*
167 if (p = xmlnode_get_attrib(packet->x, "id")) {
168 int iid = atoi(p);
169
170 if (iid == jhook.id) {
171 if (!jhook.regmode) {
172 if (jhook.jstate == STATE_GETAUTH) {
173 if (x = xmlnode_get_tag(packet->x, "query"))
174 if (!xmlnode_get_tag(x, "digest")) {
175 jhook.jc->sid = 0;
176 }
177
178 jhook.id = atoi(jab_auth(jhook.jc));
179 jhook.jstate = STATE_SENDAUTH;
180
181 } else {
182 jhook.gotloggedin();
183 jhook.jstate = STATE_LOGGED;
184 }
185
186 } else {
187 jhook.regdone = true;
188
189 }
190 return;
191 }
192
193 if(!strcmp(p, "VCARDreq")) {
194 x = xmlnode_get_firstchild(packet->x);
195 if(!x) x = packet->x;
196
197 jhook.gotvcard(ic, x);
198 return;
199
200 } else if(!strcmp(p, "versionreq")) {
201 jhook.gotversion(ic, packet->x);
202 return;
203
204 }
205 }
206
207 if(x = xmlnode_get_tag(packet->x, "query")) {
208 p = xmlnode_get_attrib(x, "xmlns"); if(p) ns = p;
209
210 if(ns == NS_ROSTER) {
211 jhook.gotroster(x);
212
213 } else if(ns == NS_AGENTS) {
214 for(y = xmlnode_get_tag(x, "agent"); y; y = xmlnode_get_nextsibling(y)) {
215 const char *alias = xmlnode_get_attrib(y, "jid");
216
217 if(alias) {
218 const char *name = xmlnode_get_tag_data(y, "name");
219 const char *desc = xmlnode_get_tag_data(y, "description");
220 const char *service = xmlnode_get_tag_data(y, "service");
221 agent::agent_type atype = agent::atUnknown;
222
223 if(xmlnode_get_tag(y, "groupchat")) atype = agent::atGroupchat; else
224 if(xmlnode_get_tag(y, "transport")) atype = agent::atTransport; else
225 if(xmlnode_get_tag(y, "search")) atype = agent::atSearch;
226
227 if(alias && name && desc) {
228 jhook.agents.push_back(agent(alias, name, desc, atype));
229
230 if(atype == agent::atSearch) {
231 x = jutil_iqnew (JPACKET__GET, NS_SEARCH);
232 xmlnode_put_attrib(x, "to", alias);
233 xmlnode_put_attrib(x, "id", "Agent info");
234 jab_send(conn, x);
235 xmlnode_free(x);
236 }
237
238 if(xmlnode_get_tag(y, "register")) {
239 x = jutil_iqnew (JPACKET__GET, NS_REGISTER);
240 xmlnode_put_attrib(x, "to", alias);
241 xmlnode_put_attrib(x, "id", "Agent info");
242 jab_send(conn, x);
243 xmlnode_free(x);
244 }
245 }
246 }
247 }
248
249 if(find(jhook.agents.begin(), jhook.agents.end(), DEFAULT_CONFSERV) == jhook.agents.end())
250 jhook.agents.insert(jhook.agents.begin(), agent(DEFAULT_CONFSERV, DEFAULT_CONFSERV,
251 _("Default Jabber conference server"), agent::atGroupchat));
252
253 } else if(ns == NS_SEARCH || ns == NS_REGISTER) {
254 p = xmlnode_get_attrib(packet->x, "id"); id = p ? p : "";
255
256 if(id == "Agent info") {
257 jhook.gotagentinfo(packet->x);
258 } else if(id == "Lookup") {
259 jhook.gotsearchresults(packet->x);
260 } else if(id == "Register") {
261 x = jutil_iqnew(JPACKET__GET, NS_REGISTER);
262 xmlnode_put_attrib(x, "to", from.c_str());
263 xmlnode_put_attrib(x, "id", "Agent info");
264 jab_send(conn, x);
265 xmlnode_free(x);
266 }
267
268 }
269 }
270 */
271
272 } else if (!strcmp(type, "set")) {
273 } else if (!strcmp(type, "error")) {
274 char *name=NULL, *desc=NULL;
275 int code;
276
277 x = xmlnode_get_tag(packet->x, "error");
278 p = xmlnode_get_attrib(x, "code"); if(p) code = atoi(p);
279 p = xmlnode_get_attrib(x, "id"); if(p) name = p;
280 p = xmlnode_get_tag_data(packet->x, "error"); if(p) desc = p;
281
282 switch(code) {
283 case 401: /* Unauthorized */
284 case 302: /* Redirect */
285 case 400: /* Bad request */
286 case 402: /* Payment Required */
287 case 403: /* Forbidden */
288 case 404: /* Not Found */
289 case 405: /* Not Allowed */
290 case 406: /* Not Acceptable */
291 case 407: /* Registration Required */
292 case 408: /* Request Timeout */
293 case 409: /* Conflict */
294 case 500: /* Internal Server Error */
295 case 501: /* Not Implemented */
296 case 502: /* Remote Server Error */
297 case 503: /* Service Unavailable */
298 case 504: /* Remote Server Timeout */
299 default:
300 /*
301 if(!jhook.regmode) {
302 face.log(desc.empty() ?
303 _("+ [jab] error %d") :
304 _("+ [jab] error %d: %s"),
305 code, desc.c_str());
306
307 if(!jhook.flogged && code != 501) {
308 close(jhook.jc->fd);
309 jhook.jc->fd = -1;
310 }
311
312 } else {
313 jhook.regerr = desc;
314
315 }
316 */
317 }
318 scr_LogPrint("Error code from server (%d)", code);
319
320 }
321 break;
322
323 case JPACKET_PRESENCE:
324 x = xmlnode_get_tag(packet->x, "show");
325 //ust = available;
326
327 if (x) {
328 p = xmlnode_get_data(x); if(p) ns = p;
329
330 if (ns) {
331 scr_LogPrint("New status: %s", ns);
332 /*
333 if (ns == "away") ust = away; else
334 if (ns == "dnd") ust = dontdisturb; else
335 if (ns == "xa") ust = notavail; else
336 if (ns == "chat") ust = freeforchat;
337 */
338 }
339 }
340
341 if (!strcmp(type, "unavailable")) {
342 scr_LogPrint("New status: unavailable/offline");
343 // XXX
344 // ust = offline;
345 }
346
347 /*
348 jidsplit(from, u, h, s);
349 id = u + "@" + h;
350
351 if(clist.get(imcontact((string) "#" + id, jhook.proto))) {
352 if(ust == offline) {
353 vector<string>::iterator im = find(jhook.chatmembers[id].begin(), jhook.chatmembers[id].end(), s);
354 if(im != jhook.chatmembers[id].end())
355 jhook.chatmembers[id].erase(im);
356
357 } else {
358 jhook.chatmembers[id].push_back(s);
359
360 }
361
362 } else {
363 icqcontact *c = clist.get(ic);
364
365 if(c)
366 if(c->getstatus() != ust) {
367 if(c->getstatus() == offline)
368 jhook.awaymsgs[ic.nickname] = "";
369
370 logger.putonline(c, c->getstatus(), ust);
371 c->setstatus(ust);
372
373 if(x = xmlnode_get_tag(packet->x, "status"))
374 if(p = xmlnode_get_data(x))
375 jhook.awaymsgs[ic.nickname] = p;
376
377 #ifdef HAVE_GPGME
378 if(x = xmlnode_get_tag(packet->x, "x"))
379 if(p = xmlnode_get_attrib(x, "xmlns"))
380 if((string) p == "jabber:x:signed")
381 if(p = xmlnode_get_data(x))
382 c->setpgpkey(pgp.verify(p, jhook.awaymsgs[ic.nickname]));
383 #endif
384
385 }
386 }
387 */
388 break;
389
390 case JPACKET_S10N:
391 scr_LogPrint("Received subscription packet");
392 /*
393 isagent = find(jhook.agents.begin(), jhook.agents.end(), from) != jhook.agents.end();
394
395 if(type == "subscribe") {
396 if(!isagent) {
397 em.store(imauthorization(ic, imevent::incoming,
398 imauthorization::Request, _("The user wants to subscribe to your network presence updates")));
399
400 } else {
401 auto_ptr<char> cfrom(strdup(from.c_str()));
402 x = jutil_presnew(JPACKET__SUBSCRIBED, cfrom.get(), 0);
403 jab_send(jhook.jc, x);
404 xmlnode_free(x);
405 }
406
407 } else if(type == "unsubscribe") {
408 auto_ptr<char> cfrom(strdup(from.c_str()));
409 x = jutil_presnew(JPACKET__UNSUBSCRIBED, cfrom.get(), 0);
410 jab_send(jhook.jc, x);
411 xmlnode_free(x);
412 em.store(imnotification(ic, _("The user has removed you from his contact list (unsubscribed you, using the Jabber language)")));
413
414 }
415 */
416
417 break;
418
419 default:
420 break;
421 }
422 }
423
424 jconn jb_connect(const char *servername, unsigned int port, int ssl,
425 const char *jid, const char *pass,
426 const char *resource)
427 {
428 if (!port) {
429 if (ssl)
430 port = JABBERSSLPORT;
431 else
432 port = JABBERPORT;
433 }
434
435 if (jc)
436 free(jc);
437
438 //jc = jab_new(jid, pass, port, ssl);
439 jc = jab_new("mctest@lilotux.net/mcabber", (char*)pass, (int)port, ssl);
440
441 jab_packet_handler(jc, &packethandler);
442 jab_state_handler(jc, &statehandler);
443
444 if (jc->user) {
445 //fonline = true;
446 scr_LogPrint("+ State_Connecting");
447 jstate = STATE_CONNECTING;
448 statehandler(0, -1);
449 jab_start(jc);
450 }
451
452 return jc;
453 }
454
455 void jb_disconnect(void)
456 {
457 statehandler(jc, JCONN_STATE_OFF);
458 }
459
460 void jb_keepalive()
461 {
462 if (jc) {
463 // XXX Only if connected...
464 jab_send_raw(jc, " ");
465 }
466 }
467
468 void jb_main()
469 {
470 xmlnode x, z;
471 char *cid;
472
473 if (jc && jc->state == JCONN_STATE_CONNECTING) {
474 jab_start(jc);
475 return;
476 }
477
478 jab_poll(jc, 0);
479
480 if (jstate == STATE_CONNECTING) {
481 if (jc) {
482 x = jutil_iqnew(JPACKET__GET, NS_AUTH);
483 cid = jab_getid(jc);
484 xmlnode_put_attrib(x, "id", cid);
485 // id = atoi(cid);
486
487 z = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "username");
488 xmlnode_insert_cdata(z, jc->user->user, (unsigned) -1);
489 jab_send(jc, x);
490 xmlnode_free(x);
491
492 jstate = STATE_GETAUTH;
493 }
494
495 if (!jc || jc->state == JCONN_STATE_OFF) {
496 scr_LogPrint("Unable to connect to the server");
497 // fonline = false;
498 }
499 }
500
501 if (!jc) {
502 statehandler(jc, JCONN_STATE_OFF);
503 } else if (jc->state == JCONN_STATE_OFF || jc->fd == -1) {
504 statehandler(jc, JCONN_STATE_OFF);
505 }
506 }
507
508 void setjabberstatus(enum imstatus st, char *msg)
509 {
510 xmlnode x = jutil_presnew(JPACKET__UNKNOWN, 0, 0);
511
512 switch(st) {
513 case away:
514 xmlnode_insert_cdata(xmlnode_insert_tag(x, "show"), "away",
515 (unsigned) -1);
516 break;
517
518 case occupied:
519 case dontdisturb:
520 xmlnode_insert_cdata(xmlnode_insert_tag(x, "show"), "dnd",
521 (unsigned) -1);
522 break;
523
524 case freeforchat:
525 xmlnode_insert_cdata(xmlnode_insert_tag(x, "show"), "chat",
526 (unsigned) -1);
527 break;
528
529 case notavail:
530 xmlnode_insert_cdata(xmlnode_insert_tag(x, "show"), "xa",
531 (unsigned) -1);
532 break;
533
534 case invisible:
535 xmlnode_put_attrib(x, "type", "invisible");
536 break;
537 }
538
539 /*
540 if(!add["prio"].empty())
541 xmlnode_insert_cdata(xmlnode_insert_tag(x, "priority"),
542 add["prio"].c_str(), (unsigned) -1);
543 */
544
545 if (!msg || !*msg) {
546 msg = "unknownStatus";
547 //msg = imstatus2str(st);
548 }
549
550 xmlnode_insert_cdata(xmlnode_insert_tag(x, "status"), msg,
551 (unsigned) -1);
552
553 jab_send(jc, x);
554 xmlnode_free(x);
555
556 //sendvisibility();
557
558 // XXX logger.putourstatus(proto, getstatus(), ourstatus = st);
559 }
560
561 void gotloggedin(void)
562 {
563 xmlnode x;
564
565 /*
566 x = jutil_iqnew(JPACKET__GET, NS_AGENTS);
567 xmlnode_put_attrib(x, "id", "Agent List");
568 jab_send(jc, x);
569 xmlnode_free(x);
570 */
571
572 x = jutil_iqnew(JPACKET__GET, NS_ROSTER);
573 xmlnode_put_attrib(x, "id", "Roster");
574 jab_send(jc, x);
575 xmlnode_free(x);
576 }
577
578 void gotroster(xmlnode x)
579 {
580 xmlnode y; // z;
581
582 for (y = xmlnode_get_tag(x, "item"); y; y = xmlnode_get_nextsibling(y)) {
583 const char *alias = xmlnode_get_attrib(y, "jid");
584 const char *sub = xmlnode_get_attrib(y, "subscription");
585 const char *name = xmlnode_get_attrib(y, "name");
586 //const char *group = 0;
587
588 //z = xmlnode_get_tag(y, "group");
589 //if(z) group = xmlnode_get_data(z);
590
591 if (alias) {
592 char *buddyname = jidtodisp(alias);
593 if (buddyname) {
594 scr_LogPrint("New buddy: %s", buddyname);
595 free(buddyname);
596 }
597 }
598 }
599
600 postlogin();
601 }
602
603 void postlogin()
604 {
605 //int i;
606
607 //flogged = true;
608 //ourstatus = available;
609
610 //setautostatus(jhook.manualstatus);
611
612 /*
613 for (i = 0; i < clist.count; i++) {
614 c = (icqcontact *) clist.at(i);
615
616 if (c->getdesc().pname == proto)
617 if (ischannel(c))
618 if (c->getbasicinfo().requiresauth)
619 c->setstatus(available);
620 }
621 */
622
623 /*
624 agents.insert(agents.begin(), agent("vcard", "Jabber VCard", "", agent::atStandard));
625 agents.begin()->params[agent::ptRegister].enabled = true;
626
627 string buf;
628 ifstream f(conf.getconfigfname("jabber-infoset").c_str());
629
630 if (f.is_open()) {
631 icqcontact *c = clist.get(contactroot);
632
633 c->clear();
634 icqcontact::basicinfo bi = c->getbasicinfo();
635 icqcontact::reginfo ri = c->getreginfo();
636
637 ri.service = agents.begin()->name;
638 getstring(f, buf); c->setnick(buf);
639 getstring(f, buf); bi.email = buf;
640 getstring(f, buf); bi.fname = buf;
641 getstring(f, buf); bi.lname = buf;
642 f.close();
643
644 c->setbasicinfo(bi);
645 c->setreginfo(ri);
646
647 sendupdateuserinfo(*c);
648 unlink(conf.getconfigfname("jabber-infoset").c_str());
649 }
650 */
651 }
652
653 void gotmessage(char *type, const char *from, const char *body,
654 const char *enc)
655 {
656 char *u, *h, *r;
657
658 jidsplit(from, &u, &h, &r);
659 if (*r)
660 scr_LogPrint("There is an extra part in message: %s", *r);
661 scr_WriteIncomingMessage(from, body);
662 }
663