comparison mcabber/src/jabglue.c @ 31:0f0fbd0c4a7f

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