Updating ChangeLog for 4.22.10
[centerim.git] / src / hooks / yahoohook.cc
blobfc8dd4a2af7f4492ae60544c779d842cd0e35af1
1 /*
3 * centerim yahoo! protocol handling class
4 * $Id: yahoohook.cc,v 1.112 2004/12/20 00:54:02 konst Exp $
6 * Copyright (C) 2003-2004 by Konstantin Klyagin <k@thekonst.net>
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.
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.
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
25 #include "icqcommon.h"
27 #ifdef BUILD_YAHOO
29 #include "yahoohook.h"
30 #include "icqmlist.h"
31 #include "icqface.h"
32 #include "imlogger.h"
34 #include "icqcontacts.h"
35 #include "icqgroups.h"
37 #include "accountmanager.h"
38 #include "eventmanager.h"
40 #include "yahoo2.h"
41 #include "connwrap.h"
43 #include <netdb.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #define PERIOD_REFRESH 60
48 #define PERIOD_CLOSE 6
50 #define NOTIFBUF 512
52 int yahoohook::yfd::connection_tags = 0;
54 char pager_host[255], pager_port[255], filetransfer_host[255],
55 filetransfer_port[255], webcam_host[255], webcam_port[255],
56 local_host[255], webcam_description[255];
58 int conn_type;
60 yahoohook yhook;
62 static int stat2int[imstatus_size] = {
63 YAHOO_STATUS_OFFLINE,
64 YAHOO_STATUS_AVAILABLE,
65 YAHOO_STATUS_INVISIBLE,
66 YAHOO_STATUS_CUSTOM,
67 YAHOO_STATUS_BUSY,
68 YAHOO_STATUS_NOTATDESK,
69 YAHOO_STATUS_NOTATHOME,
70 YAHOO_STATUS_IDLE,
71 YAHOO_STATUS_OUTTOLUNCH,
74 yahoohook::yahoohook() : abstracthook(yahoo), fonline(false), cid(0) {
75 fcapabs.insert(hookcapab::setaway);
76 fcapabs.insert(hookcapab::fetchaway);
77 fcapabs.insert(hookcapab::directadd);
78 // fcapabs.insert(hookcapab::files);
79 // fcapabs.insert(hookcapab::conferencing);
80 // fcapabs.insert(hookcapab::conferencesaretemporary);
81 fcapabs.insert(hookcapab::ssl);
82 fcapabs.insert(hookcapab::authrequests);
84 pager_host[0] = pager_port[0] = filetransfer_host[0] =
85 filetransfer_port[0] = webcam_host[0] = webcam_port[0] =
86 local_host[0] = 0;
89 yahoohook::~yahoohook() {
92 void yahoohook::init() {
93 manualstatus = conf->getstatus(proto);
95 static struct yahoo_callbacks c;
97 memset(&c, 0, sizeof(c));
99 c.ext_yahoo_login_response = &login_response;
100 c.ext_yahoo_got_buddies = &got_buddies;
101 c.ext_yahoo_status_changed = &status_changed;
102 c.ext_yahoo_got_im = &got_im;
103 c.ext_yahoo_got_conf_invite = &got_conf_invite;
104 c.ext_yahoo_conf_userdecline = &conf_userdecline;
105 c.ext_yahoo_conf_userjoin = &conf_userjoin;
106 c.ext_yahoo_conf_userleave = &conf_userleave;
107 c.ext_yahoo_conf_message = &conf_message;
108 c.ext_yahoo_got_file = &got_file;
109 c.ext_yahoo_contact_added = &contact_added;
110 c.ext_yahoo_game_notify = &game_notify;
111 c.ext_yahoo_mail_notify = &mail_notify;
112 c.ext_yahoo_system_message = &system_message;
113 c.ext_yahoo_error = &error;
114 c.ext_yahoo_add_handler = &add_handler;
115 c.ext_yahoo_remove_handler = &remove_handler;
116 c.ext_yahoo_connect_async = &connect_async;
117 c.ext_yahoo_read = &y_read;
118 c.ext_yahoo_write = &y_write;
119 c.ext_yahoo_close = &y_close;
120 c.ext_yahoo_got_identities = &got_identities;
121 c.ext_yahoo_got_ignore = &got_ignore;
122 c.ext_yahoo_got_cookies = &got_cookies;
123 c.ext_yahoo_chat_cat_xml = &chat_cat_xml;
124 c.ext_yahoo_chat_join = &chat_join;
125 c.ext_yahoo_chat_userjoin = &chat_userjoin;
126 c.ext_yahoo_chat_userleave = &chat_userleave;
127 c.ext_yahoo_chat_message = &chat_message;
128 c.ext_yahoo_rejected = &rejected;
129 c.ext_yahoo_typing_notify = &typing_notify;
130 c.ext_yahoo_got_webcam_image = &got_webcam_image;
131 c.ext_yahoo_webcam_invite = &webcam_invite;
132 c.ext_yahoo_webcam_invite_reply = &webcam_invite_reply;
133 c.ext_yahoo_webcam_closed = &webcam_closed;
134 c.ext_yahoo_webcam_viewer = &webcam_viewer;
135 c.ext_yahoo_webcam_data_request = &webcam_data_request;
136 c.ext_yahoo_got_search_result = &got_search_result;
137 c.ext_yahoo_got_ping = &got_ping;
138 c.ext_yahoo_log = &ylog;
139 c.ext_yahoo_got_buddyicon_request = &got_buddyicon_request;
140 c.ext_yahoo_got_buddyicon = &got_buddyicon;
141 c.ext_yahoo_buddyicon_uploaded = &buddyicon_uploaded;
142 c.ext_yahoo_chat_yahooerror = &chat_yahooerror;
143 c.ext_yahoo_chat_yahoologout = &chat_yahoologout;
144 c.ext_yahoo_connect = &yahoo_connect;
145 c.ext_yahoo_file_transfer_done = &file_transfer_done;
146 c.ext_yahoo_get_ip_addr = &get_ip_addr;
147 c.ext_yahoo_got_buddy_change_group = &got_buddy_change_group;
148 c.ext_yahoo_got_buddyicon_checksum = &got_buddyicon_checksum;
149 c.ext_yahoo_got_buzz = &got_buzz;
150 c.ext_yahoo_got_ft_data = &got_ft_data;
151 c.ext_yahoo_auth_request = &auth_request;
153 yahoo_register_callbacks(&c);
156 void yahoohook::connect() {
157 icqconf::imaccount acc = conf->getourid(proto);
158 int r;
160 log(logConnecting);
162 if(cid < 0) {
163 yahoo_logoff(cid);
164 yahoo_close(cid);
167 cid = yahoo_init_with_attributes(acc.nickname.c_str(), acc.password.c_str(),
168 "pager_host", acc.server.c_str(),
169 "pager_port", acc.port,
170 (char *) NULL);
172 yahoo_login(cid, stat2int[manualstatus]);
174 if(cid < 0) {
175 string msg = _("+ [yahoo] cannot connect: ");
177 switch(cid) {
178 case -1: msg += _("could not resolve hostname"); break;
179 case -2: msg += _("could not create socket"); break;
180 default: msg += _("verify the pager host and port entered"); break;
183 face.log(msg);
184 } else {
185 fonline = true;
186 flogged = false;
190 void yahoohook::main() {
191 vector<yfd>::iterator i;
192 struct timeval tv;
193 int hsock;
194 fd_set rs, ws, es;
196 FD_ZERO(&rs);
197 FD_ZERO(&ws);
198 FD_ZERO(&es);
200 tv.tv_sec = tv.tv_usec = 0;
201 hsock = 0;
203 for(i = fds.begin(); i != fds.end(); ++i) {
204 struct y_c ic = i->con;
205 if (i->cond & YAHOO_INPUT_READ)
206 FD_SET(ic.fd, &rs);
207 if (i->cond & YAHOO_INPUT_WRITE)
208 FD_SET(ic.fd, &ws);
209 hsock = max(hsock, ic.fd);
212 if(select(hsock+1, &rs, &ws, 0, &tv) > 0) {
213 for(i = fds.begin(); i != fds.end(); ++i) {
214 if(i->isconnect) {
215 if (!cw_nb_connect(i->con.fd, NULL, 0, i->con.ssl, &i->con.state))
217 if (i->con.state & CW_CONNECT_WANT_SOMETHING)
218 continue;
220 else
222 cw_close(i->con.fd);
223 break;
225 struct y_c tc = i->con;
226 connect_complete(i->data, &tc);
227 break;
230 if(FD_ISSET(i->con.fd, &rs)) {
231 yahoo_read_ready(cid, &i->con, i->data);
232 break;
234 if(FD_ISSET(i->con.fd, &ws)) {
235 yahoo_write_ready(cid, &i->con, i->data);
236 break;
242 void yahoohook::getsockets(fd_set &rs, fd_set &ws, fd_set &es, int &hsocket) const {
243 if(online()) {
244 vector<yfd>::const_iterator i;
246 for(i = fds.begin(); i != fds.end(); ++i) {
247 if (i->cond & YAHOO_INPUT_READ)
248 FD_SET(i->con.fd, &rs);
249 if (i->cond & YAHOO_INPUT_WRITE)
250 FD_SET(i->con.fd, &ws);
251 hsocket = max(hsocket, i->con.fd);
256 bool yahoohook::isoursocket(fd_set &rs, fd_set &ws, fd_set &es) const {
257 vector<yfd>::const_iterator i;
259 for(i = fds.begin(); i != fds.end(); ++i)
261 if(i->cond & YAHOO_INPUT_READ && FD_ISSET(i->con.fd, &rs))
262 return true;
264 if(i->cond & YAHOO_INPUT_WRITE && FD_ISSET(i->con.fd, &ws))
265 return true;
267 return false;
270 void yahoohook::disconnect() {
271 if(online()) {
272 yahoo_logoff(cid);
273 time(&timer_close);
277 void yahoohook::disconnected() {
278 if(logged()) {
279 vector<yfd>::const_iterator i;
280 for(i = fds.begin(); i != fds.end(); ++i)
282 cw_close(i->con.fd);
285 fds.clear();
287 logger.putourstatus(proto, getstatus(), ourstatus = offline);
288 clist.setoffline(proto);
289 fonline = false;
290 log(logDisconnected);
291 face.update();
295 void yahoohook::exectimers() {
296 vector<pair<Action, string> >::iterator it;
298 for(it = tobedone.begin(); it != tobedone.end(); ++it) {
299 switch(it->first) {
300 case tbdConfLogon:
301 /* TODO: investigate if copy is really needed here */
302 char *room = strdup(it->second.c_str());
303 yahoo_conference_logon(cid, 0, getmembers(it->second), room);
304 free (room);
305 break;
309 tobedone.clear();
311 if(logged()) {
312 if(timer_current-timer_refresh > PERIOD_REFRESH) {
313 yahoo_refresh(cid);
314 yahoo_keepalive(cid);
315 timer_refresh = timer_current;
317 } else if(timer_close && timer_current-timer_close > PERIOD_CLOSE) {
318 yahoo_close(cid);
319 disconnected();
325 struct tm *yahoohook::timestamp() {
326 return localtime(&timer_current);
329 bool yahoohook::send(const imevent &ev) {
330 icqcontact *c = clist.get(ev.getcontact());
331 string text;
333 if(c) {
334 if(ev.gettype() == imevent::message) {
335 const immessage *m = static_cast<const immessage *>(&ev);
336 if(m) text = rushtmlconv("ku", m->gettext());
338 } else if(ev.gettype() == imevent::url) {
339 const imurl *m = static_cast<const imurl *>(&ev);
340 if(m) text = rushtmlconv("ku", m->geturl()) + "\n\n" + rusconv("kw", m->getdescription());
342 } else if(ev.gettype() == imevent::file) {
343 const imfile *m = static_cast<const imfile *>(&ev);
344 vector<imfile::record> files = m->getfiles();
345 vector<imfile::record>::const_iterator ir;
347 for(ir = files.begin(); ir != files.end(); ++ir) {
348 sfiles.push_back(strdup(ir->fname.c_str()));
349 srfiles[sfiles.back()] = *m;
351 yahoo_send_file(cid, ev.getcontact().nickname.c_str(),
352 m->getmessage().c_str(), justfname(ir->fname).c_str(),
353 ir->size, &get_fd, sfiles.back());
356 return true;
357 } else if (ev.gettype() == imevent::authorization) {
358 const imauthorization *m = static_cast<const imauthorization *> (&ev);
360 switch(m->getauthtype()) {
361 case imauthorization::Granted:
362 yahoo_confirm_buddy(cid, ev.getcontact().nickname.c_str(), 0, 0);
363 break;
365 case imauthorization::Rejected:
366 yahoo_confirm_buddy(cid, ev.getcontact().nickname.c_str(), 1, 0);
367 break;
370 return true;
373 if(!ischannel(c)) {
374 yahoo_send_im(cid, 0, ev.getcontact().nickname.c_str(), text.c_str(), 0, 0);
375 } else {
376 yahoo_conference_message(cid, 0, getmembers(ev.getcontact().nickname.substr(1)),
377 ev.getcontact().nickname.c_str()+1, text.c_str(), 0);
380 return true;
383 return false;
386 bool yahoohook::online() const {
387 return fonline;
390 bool yahoohook::logged() const {
391 return fonline && flogged;
394 bool yahoohook::isconnecting() const {
395 return fonline && !flogged;
398 void yahoohook::sendnewuser(const imcontact &ic) {
399 sendnewuser(ic, true);
402 void yahoohook::sendnewuser(const imcontact &ic, bool report) {
403 if(online() && !ischannel(ic)) {
404 if(logged()) {
405 bool found = false;
406 const YList *buddies = yahoo_get_buddylist(cid);
407 const YList *bud = 0;
409 for(bud = buddies; bud && !found; bud = y_list_next(bud))
410 found = ic.nickname == static_cast<yahoo_buddy *>(bud->data)->id;
412 if(!found) {
413 if(report) log(logContactAdd, ic.nickname.c_str());
414 if (groups.size()>0)
416 vector<icqgroup>::const_iterator ig = find(groups.begin(), groups.end(), clist.get(ic)->getgroupid());
417 if (ig == groups.end())
418 ig = groups.begin();
419 yahoo_add_buddy(cid, ic.nickname.c_str(), ig->getname().c_str(), "");
425 requestinfo(ic);
428 void yahoohook::removeuser(const imcontact &ic) {
429 removeuser(ic, true);
432 void yahoohook::removeuser(const imcontact &ic, bool report) {
433 if(logged()) {
434 if(!ischannel(ic)) {
435 if(report) log(logContactRemove, ic.nickname.c_str());
437 const YList *buddies = yahoo_get_buddylist(cid);
438 const YList *bud = 0;
440 for(bud = buddies; bud; bud = y_list_next(bud)) {
441 if(ic.nickname == ((yahoo_buddy *) bud->data)->id)
442 yahoo_remove_buddy(cid,
443 ((yahoo_buddy *) bud->data)->id,
444 ((yahoo_buddy *) bud->data)->group);
446 } else {
447 if(report)
448 face.log(_("+ [yahoo] leaving the %s conference"),
449 ic.nickname.c_str());
451 yahoo_conference_logoff(cid, 0, getmembers(ic.nickname.substr(1)), ic.nickname.c_str()+1);
456 imstatus yahoohook::yahoo2imstatus(int status) {
457 imstatus st = offline;
459 switch(status) {
460 case YAHOO_STATUS_AVAILABLE:
461 st = available;
462 break;
463 case YAHOO_STATUS_BUSY:
464 st = dontdisturb;
465 break;
466 case YAHOO_STATUS_CUSTOM:
467 case YAHOO_STATUS_BRB:
468 case YAHOO_STATUS_IDLE:
469 case YAHOO_STATUS_ONPHONE:
470 st = away;
471 break;
472 case YAHOO_STATUS_NOTATDESK:
473 st = occupied;
474 break;
475 case YAHOO_STATUS_NOTATHOME:
476 case YAHOO_STATUS_NOTINOFFICE:
477 case YAHOO_STATUS_ONVACATION:
478 case YAHOO_STATUS_STEPPEDOUT:
479 st = notavail;
480 break;
481 case YAHOO_STATUS_OUTTOLUNCH:
482 st = outforlunch;
483 break;
484 case YAHOO_STATUS_INVISIBLE:
485 st = invisible;
486 break;
487 case -1:
488 st = offline;
489 break;
492 return st;
495 bool yahoohook::enabled() const {
496 return true;
499 void yahoohook::setautostatus(imstatus st) {
500 if(st == offline) {
501 if(getstatus() != offline) {
502 disconnect();
504 } else {
505 if(getstatus() == offline) {
506 connect();
508 } else {
509 logger.putourstatus(proto, getstatus(), ourstatus = st);
511 if(st == freeforchat) {
512 /* TODO copy should not be needed here ?*/
513 char *msg = strdup("free for chat");
514 yahoo_set_away(cid, (yahoo_status) stat2int[st], msg, 0);
515 free (msg);
517 } else if(st != away) {
518 yahoo_set_away(cid, (yahoo_status) stat2int[st], 0, 0);
520 } else {
521 char *msg = strdup(rusconv("ku", conf->getawaymsg(proto)).c_str());
522 yahoo_set_away(cid, (yahoo_status) stat2int[st], msg, 1);
523 free (msg);
530 imstatus yahoohook::getstatus() const {
531 return online() ? ourstatus : offline;
534 void yahoohook::requestinfo(const imcontact &ic) {
535 requestfromfound(ic);
537 icqcontact *c = clist.get(ic);
538 if(!c) c = clist.get(contactroot);
540 icqcontact::moreinfo m = c->getmoreinfo();
541 icqcontact::basicinfo b = c->getbasicinfo();
543 m.homepage = "http://profiles.yahoo.com/" + ic.nickname;
544 b.email = ic.nickname + "@yahoo.com";
546 c->setmoreinfo(m);
547 c->setbasicinfo(b);
550 void yahoohook::userstatus(const string &nick, int st, const string &message, bool away) {
551 imcontact ic(nick, proto);
552 icqcontact *c = clist.get(ic);
554 awaymessages[nick] = rusconv("wk", message);
556 if(!c) {
557 c = clist.addnew(ic, false);
560 if(c) {
561 logger.putonline(ic, c->getstatus(), yahoo2imstatus(st));
562 c->setstatus(yahoo2imstatus(st));
566 YList *yahoohook::getmembers(const string &room) {
567 int i;
568 static YList *smemb = 0;
569 vector<string>::iterator ic;
570 map<string, vector<string> >::iterator im;
572 if(smemb) {
573 for(YList *n = smemb; n; n = y_list_next(n))
574 free(n->data);
576 y_list_free(smemb);
577 smemb = 0;
580 if((im = confmembers.find(room)) != confmembers.end())
581 for(ic = im->second.begin(); ic != im->second.end(); ++ic)
582 smemb = y_list_append(smemb, strdup(ic->c_str()));
584 return smemb;
587 string yahoohook::decode(string text, bool utf) {
588 int npos, mpos;
590 text = rushtmlconv(utf ? "uk" : "wk", text);
592 while((npos = text.find("\e[")) != -1) {
593 if((mpos = text.substr(npos).find("m")) == -1)
594 mpos = text.size()-npos-1;
596 text.erase(npos, mpos+1);
599 return text;
602 bool yahoohook::knowntransfer(const imfile &fr) const {
603 return fvalid.find(fr) != fvalid.end();
606 void yahoohook::replytransfer(const imfile &fr, bool accept, const string &localpath) {
607 /* if(accept) {
608 string localname = localpath + "/";
609 localname += fr.getfiles()[0].fname;
610 sfiles.push_back(strdup(localname.c_str()));
611 srfiles[sfiles.back()] = fr;
612 yahoo_get_url_handle(cid, fvalid[fr].c_str(), &get_url, sfiles.back());
614 } else {
615 fvalid.erase(fr);
620 void yahoohook::aborttransfer(const imfile &fr) {
621 face.transferupdate(fr.getfiles().begin()->fname, fr, icqface::tsCancel, 0, 0);
622 fvalid.erase(fr);
625 void yahoohook::lookup(const imsearchparams &params, verticalmenu &dest) {
626 string room;
627 icqcontact *c;
628 vector<string>::const_iterator i;
630 while(!foundguys.empty()) {
631 delete foundguys.back();
632 foundguys.pop_back();
635 yahoo_search_gender gender = YAHOO_GENDER_NONE;
637 switch(params.gender) {
638 case genderMale: gender = YAHOO_GENDER_MALE; break;
639 case genderFemale: gender = YAHOO_GENDER_FEMALE; break;
642 searchonlineonly = params.onlineonly;
643 searchdest = &dest;
645 if(!params.kwords.empty()) {
646 yahoo_search(cid, YAHOO_SEARCH_KEYWORD, decode(params.kwords, false).c_str(),
647 gender, YAHOO_AGERANGE_NONE, params.photo ? 1 : 0, 1);
649 } else if(!params.firstname.empty()) {
650 yahoo_search(cid, YAHOO_SEARCH_NAME, decode(params.firstname, false).c_str(),
651 gender, YAHOO_AGERANGE_NONE, params.photo ? 1 : 0, 1);
653 } else if(!params.room.empty()) {
654 room = params.room.substr(1);
655 i = confmembers[room].begin();
657 while(i != confmembers[room].end()) {
658 if(c = clist.get(imcontact(*i, proto)))
659 searchdest->additem(conf->getcolor(cp_clist_yahoo),
660 c, (string) " " + *i);
662 ++i;
665 face.findready();
666 log(logConfMembers, searchdest->getcount());
668 searchdest->redraw();
669 searchdest = 0;
674 void yahoohook::conferencecreate(const imcontact &confid, const vector<imcontact> &lst) {
675 int i;
676 string room = confid.nickname.substr(1);
678 YList *who = 0;
680 vector<imcontact>::const_iterator il = lst.begin();
681 while(il != lst.end()) {
682 who = y_list_append(who, strdup(il->nickname.c_str()));
683 ++il;
686 yahoo_conference_invite(cid, 0, who, room.c_str(), _("Please join my conference."));
688 for(YList *w = who; w; w = y_list_next(w))
689 free(w->data);
691 y_list_free(who);
694 void yahoohook::requestawaymsg(const imcontact &ic) {
695 icqcontact *c = clist.get(ic);
697 if(c) {
698 if(awaymessages.find(ic.nickname) != awaymessages.end()) {
699 em.store(imnotification(ic, string() + _("Custom status message:") + "\n\n" +
700 awaymessages[ic.nickname]));
702 } else {
703 face.log(_("+ [yahoo] cannot fetch away msg from %s, %s (maybe no away msg set)"),
704 c->getdispnick().c_str(), ic.totext().c_str());
710 void yahoohook::checkinlist(imcontact ic) {
711 bool found = false;
712 icqcontact *c = clist.get(ic);
714 if(c)
715 if(c->inlist()) {
716 const YList *buddies = yahoo_get_buddylist(cid);
718 for(const YList *bud = buddies; bud && !found; ) {
719 yahoo_buddy *yb = (yahoo_buddy *) bud->data;
720 found = (c->getdesc().nickname == yb->id);
721 bud = y_list_next(bud);
724 if(!found) sendnewuser(ic, false);
728 void yahoohook::updatecontact(icqcontact *c) {
729 if(logged() && conf->getgroupmode() != icqconf::nogroups) {
730 bool found = false;
731 const YList *buddies = yahoo_get_buddylist(cid);
732 string newgroupname = groups.getname(c->getgroupid());
734 for(const YList *bud = buddies; bud; ) {
735 yahoo_buddy *yb = (yahoo_buddy *) bud->data;
737 if(c->getdesc().nickname == yb->id) {
738 if(!found) {
739 if(newgroupname != yb->group)
740 yahoo_change_buddy_group(cid, yb->id, yb->group, newgroupname.c_str());
742 found = true;
743 } else {
744 yahoo_remove_buddy(cid, yb->id, yb->group);
748 bud = y_list_next(bud);
753 void yahoohook::renamegroup(const string &oldname, const string &newname) {
754 if(logged()) {
755 string tempname = oldname + "___" + i2str(getpid()) + "_temp";
756 yahoo_group_rename(cid, oldname.c_str(), tempname.c_str());
757 yahoo_group_rename(cid, tempname.c_str(), newname.c_str());
761 // ----------------------------------------------------------------------------
763 void yahoohook::login_response(int id, int succ, const char *url) {
764 vector<string>::iterator in;
766 switch(succ) {
767 case YAHOO_LOGIN_OK:
768 yhook.flogged = true;
769 logger.putourstatus(yahoo, offline, yhook.ourstatus = yhook.manualstatus);
770 yhook.log(logLogged);
771 time(&yhook.timer_refresh);
772 yhook.setautostatus(yhook.manualstatus);
773 yhook.timer_close = 0;
774 break;
776 case YAHOO_LOGIN_LOGOFF:
777 yhook.fonline = yhook.fonline = false;
778 yahoo_close(yhook.cid);
779 face.log(_("+ [yahoo] cannot login"));
780 break;
782 case YAHOO_LOGIN_PASSWD:
783 yhook.fonline = yhook.fonline = false;
784 yahoo_close(yhook.cid);
785 face.log(_("+ [yahoo] cannot login: username and password mismatch"));
786 break;
788 case YAHOO_LOGIN_UNAME:
789 yhook.fonline = yhook.fonline = false;
790 yahoo_close(yhook.cid);
791 face.log(_("+ [yahoo] cannot login: username doesn't exist"));
792 break;
794 case YAHOO_LOGIN_LOCK:
795 yhook.fonline = yhook.fonline = false;
796 yahoo_close(yhook.cid);
797 face.log(_("+ [yahoo] cannot login: the account has been blocked"));
798 face.log(_("+ to reactivate visit %s"), url);
799 break;
801 case YAHOO_LOGIN_DUPL:
802 face.log(_("+ [yahoo] another logon detected"));
803 yahoo_logoff(yhook.cid);
804 yhook.manualstatus = offline;
805 break;
807 case YAHOO_LOGIN_UNKNOWN:
808 case YAHOO_LOGIN_SOCK:
809 face.log(_("+ [yahoo] server closed socket"));
810 yhook.disconnected();
811 break;
814 face.update();
817 void yahoohook::got_buddies(int id, YList *buds) {
818 const YList *buddy;
820 for(buddy = buds; buddy; buddy = y_list_next(buddy)) {
821 yahoo_buddy *bud = static_cast<yahoo_buddy *>(buddy->data);
823 clist.updateEntry(imcontact(bud->id, yahoo), bud->group ? bud->group : "");
827 void yahoohook::got_identities(int id, YList *buds) {
830 void yahoohook::status_changed(int id, const char *who, int stat, const char *msg, int away, int idle, int mobile) {
831 yhook.userstatus(who, stat, msg ? msg : "", (bool) away);
834 void yahoohook::got_im(int id, const char *me, const char *who, const char *msg, long tm, int stat, int utf8) {
835 imcontact ic(who, yahoo);
836 string text = cuthtml(msg, chCutBR | chLeaveLinks);
838 yhook.checkinlist(ic);
839 text = yhook.decode(text, utf8);
841 if(!text.empty()) {
842 em.store(immessage(ic, imevent::incoming, text, tm));
846 void yahoohook::got_search_result(int id, int found, int start, int total, YList *contacts) {
847 yahoo_found_contact *fc;
848 icqcontact *c;
849 YList *ir;
850 string sg, line;
852 if(!yhook.searchdest)
853 return;
855 for(ir = contacts; ir; ir = ir->next) {
856 fc = (yahoo_found_contact *) ir->data;
858 if(yhook.searchonlineonly && !fc->online)
859 continue;
861 c = new icqcontact(imcontact(fc->id, yahoo));
863 icqcontact::basicinfo binfo = c->getbasicinfo();
864 icqcontact::moreinfo minfo = c->getmoreinfo();
866 if(!fc->id || !fc->gender)
867 continue;
869 c->setnick(fc->id);
870 c->setdispnick(c->getnick());
872 sg = up(fc->gender);
873 if(sg == "MALE") minfo.gender = genderMale;
874 else if(sg == "FEMALE") minfo.gender = genderFemale;
876 minfo.age = fc->age;
877 if(fc->location) binfo.street = fc->location;
879 line = (fc->online ? "o " : " ") + c->getnick();
881 if(fc->age || fc->location || strlen(fc->gender) >= 4) {
882 line += " <";
883 if(fc->age) line += i2str(fc->age);
885 if(strlen(fc->gender) >= 4) {
886 if(fc->age) line += ", ";
887 line += fc->gender;
890 if(fc->location && strlen(fc->location)) {
891 if(fc->age || strlen(fc->gender) >= 4) line += ", ";
892 line += fc->location;
895 line += ">";
898 c->setbasicinfo(binfo);
899 c->setmoreinfo(minfo);
901 yhook.foundguys.push_back(c);
902 yhook.searchdest->additem(conf->getcolor(cp_clist_yahoo), c, line);
905 yhook.searchdest->redraw();
907 if(start + found >= total) {
908 face.findready();
909 yhook.log(logSearchFinished, yhook.foundguys.size());
910 yhook.searchdest = 0;
911 } else {
912 yahoo_search_again(yhook.cid, -1);
916 void yahoohook::got_conf_invite(int id, const char *me, const char *who, const char *room, const char *msg, YList *members) {
917 icqconf::imaccount acc = conf->getourid(yahoo);
918 string confname = (string) "#" + room, inviter, text;
919 vector<string>::iterator ic;
920 char buf[NOTIFBUF];
921 int i;
923 imcontact cont(confname, yahoo);
924 icqcontact *c = clist.get(cont);
925 if(!c) c = clist.addnew(cont);
927 inviter = confname.substr(1);
928 if((i = inviter.rfind("-")) != -1) {
929 inviter.erase(i);
932 snprintf(buf, NOTIFBUF, _("The user %s has invited you to the %s conference, the topic there is: %s"),
933 yhook.rusconv("wk", inviter).c_str(),
934 yhook.rusconv("wk", room).c_str(),
935 yhook.rusconv("wk", msg).c_str());
937 text = (string) buf + "\n\n" + _("Current conference members are: ");
938 yhook.confmembers[room].push_back(inviter);
940 for(YList *m = members; m; m = y_list_next(m)) {
941 string id = (char *) m->data;
943 if(id != acc.nickname)
944 if(find(yhook.confmembers[room].begin(), yhook.confmembers[room].end(), id) == yhook.confmembers[room].end()) {
945 yhook.confmembers[room].push_back(id);
949 for(ic = yhook.confmembers[room].begin(); ic != yhook.confmembers[room].end(); ) {
950 text += *ic;
951 if(++ic != yhook.confmembers[room].end())
952 text += ", ";
955 c->setstatus(available);
956 em.store(imnotification(cont, text));
957 em.store(imnotification(cont, _("Auto-joined the conference")));
959 yhook.tobedone.push_back(make_pair(tbdConfLogon, room));
962 void yahoohook::conf_userdecline(int id, const char *me, const char *who, const char *room, const char *msg) {
963 icqcontact *c = clist.get(imcontact((string) "#" + room, yahoo));
964 char buf[NOTIFBUF];
966 if(c) {
967 snprintf(buf, NOTIFBUF, _("The user %s has declined your invitation to join the conference"), who);
968 em.store(imnotification(c, buf));
972 void yahoohook::conf_userjoin(int id, const char *me, const char *who, const char *room) {
973 icqcontact *c = clist.get(imcontact((string) "#" + room, yahoo));
974 char buf[NOTIFBUF];
976 if(c) {
977 snprintf(buf, NOTIFBUF, _("The user %s has joined the conference"), who);
979 if(find(yhook.confmembers[room].begin(), yhook.confmembers[room].end(), who) == yhook.confmembers[room].end())
980 yhook.confmembers[room].push_back(who);
982 em.store(imnotification(c, buf));
986 void yahoohook::conf_userleave(int id, const char *me, const char *who, const char *room) {
987 icqcontact *c = clist.get(imcontact((string) "#" + room, yahoo));
988 char buf[NOTIFBUF];
989 vector<string>::iterator im;
991 if(c) {
992 snprintf(buf, NOTIFBUF, _("The user %s has left the conference"), who);
993 em.store(imnotification(c, buf));
995 im = find(yhook.confmembers[room].begin(), yhook.confmembers[room].end(), who);
996 if(im != yhook.confmembers[room].end()) yhook.confmembers[room].erase(im);
1000 void yahoohook::conf_message(int id, const char *me, const char *who, const char *room, const char *msg, int utf8) {
1001 icqcontact *c = clist.get(imcontact((string) "#" + room, yahoo));
1003 string text = (string) who + ": " + cuthtml(msg, chCutBR | chLeaveLinks);
1004 text = yhook.decode(text, utf8);
1006 if(c) em.store(immessage(c, imevent::incoming, text));
1009 void yahoohook::got_file(int id, const char *me, const char *who, const char *msg, const char *fname, unsigned long fesize, char *trid) {
1010 if(!who || !fname || !trid)
1011 return;
1013 int pos;
1014 imfile::record r;
1015 r.fname = fname;
1016 r.size = fesize;
1017 if(!fesize) r.size = -1;
1019 if((pos = r.fname.find('?')) != -1)
1020 r.fname.erase(pos);
1022 imfile fr(imcontact(who, yahoo), imevent::incoming, "",
1023 vector<imfile::record>(1, r));
1025 yhook.fvalid[fr] = trid;
1026 em.store(fr);
1028 face.transferupdate(fname, fr, icqface::tsInit, fesize, 0);
1031 void yahoohook::contact_added(int id, const char *myid, const char *who, const char *msg) {
1032 string text = _("The user has added you to his/her contact list");
1034 if(msg)
1035 if(strlen(msg)) {
1036 text += (string) ", " + _("the message was: ") + msg;
1039 em.store(imnotification(imcontact(who, yahoo), text));
1042 void yahoohook::typing_notify(int id, const char *me, const char *who, int stat) {
1043 icqcontact *c = clist.get(imcontact(who, yahoo));
1044 if(c) c->setlasttyping(stat ? timer_current : 0);
1047 void yahoohook::game_notify(int id, const char *me, const char *who, int stat, const char *msg) {
1050 void yahoohook::mail_notify(int id, const char *from, const char *subj, int cnt) {
1051 char buf[NOTIFBUF];
1053 if(from && subj) {
1054 snprintf(buf, NOTIFBUF, _("+ [yahoo] e-mail from %s, %s"), from, subj);
1055 face.log(buf);
1056 clist.get(contactroot)->playsound(imevent::email);
1060 void yahoohook::system_message(int id, const char *me, const char *who, const char *msg) {
1061 face.log(_("+ [yahoo] system (%s): %s"), who, msg);
1064 void yahoohook::got_ping(int id, const char *msg) {
1065 // face.log(_("+ [yahoo] ping: %s"), msg);
1068 void yahoohook::error(int id, const char *err, int fatal, int num) {
1069 if(fatal) {
1070 face.log(_("+ [yahoo] fatal error: %s"), err);
1071 yhook.disconnected();
1073 else {
1074 face.log(_("[yahoo] error %s"), err);
1078 int yahoohook::add_handler(int id, void *fd, yahoo_input_condition cond, void *data) {
1079 int tag = -1;
1080 struct y_c *con = (struct y_c *)fd;
1082 yhook.fds.push_back(yfd(con->fd, data, con->ssl, cond));
1083 tag = yhook.fds.back().tag;
1085 return tag;
1088 void yahoohook::remove_handler(int id, int tag) {
1089 vector<yfd>::iterator i;
1091 i = find(yhook.fds.begin(), yhook.fds.end(), tag);
1092 if(i != yhook.fds.end())
1093 yhook.fds.erase(i);
1096 int yahoohook::connect_async(int id, const char *host, int port, yahoo_connect_callback callback, void *data, int use_ssl) {
1097 struct sockaddr_in serv_addr;
1098 static struct hostent *server;
1099 int servfd;
1100 struct connect_callback_data * ccd;
1101 int error;
1103 if(!(server = gethostbyname(host))) {
1104 errno = h_errno;
1105 return -1;
1108 if((servfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1109 return -1;
1112 memset(&serv_addr, 0, sizeof(serv_addr));
1113 serv_addr.sin_family = AF_INET;
1114 memcpy(&serv_addr.sin_addr.s_addr, *server->h_addr_list, server->h_length);
1115 serv_addr.sin_port = htons(port);
1118 int state = 0;
1119 struct y_c *con = new struct y_c(servfd, use_ssl, state);
1120 error = cw_nb_connect(servfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr), use_ssl, &state);
1121 con->state = state;
1123 if(!error && !state) {
1124 int f = fcntl(servfd, F_GETFL);
1125 fcntl(servfd, F_SETFL, f | O_NONBLOCK);
1126 callback(con, 0, data);
1127 return 0;
1128 } else if((error == -1 && errno == EINPROGRESS) || (state & CW_CONNECT_STARTED)){
1129 ccd = new connect_callback_data;
1130 ccd->callback = callback;
1131 ccd->callback_data = data;
1132 ccd->id = id;
1134 ccd->tag = add_handler(id, con, YAHOO_INPUT_WRITE, ccd);
1135 delete con;
1136 return ccd->tag;
1137 } else {
1138 y_close(con);
1139 callback(NULL, 0, data);
1141 close(servfd);
1142 return -1;
1146 void yahoohook::connect_complete(void *data, struct y_c *con) {
1147 connect_callback_data *ccd = (connect_callback_data *) data;
1148 int so_error;
1149 socklen_t err_size = sizeof(so_error);
1151 remove_handler(0, ccd->tag);
1152 if(getsockopt(con->fd, SOL_SOCKET, SO_ERROR, (char *) &so_error, &err_size) == -1 || so_error != 0) {
1153 if(yhook.logged())
1154 face.log(_("+ [yahoo] direct connection failed"));
1156 cw_close(con->fd);
1157 } else {
1158 ccd->callback(con, so_error, ccd->callback_data);
1161 delete ccd;
1164 int yahoohook::y_write(void *fd, char *buf, int len)
1166 if (!fd)
1167 return -1;
1168 struct y_c *con = (struct y_c *)fd;
1169 return cw_write(con->fd, buf, len, con->ssl);
1172 int yahoohook::y_read(void *fd, char *buf, int len)
1174 if (!fd)
1175 return -1;
1176 struct y_c *con = (struct y_c *)fd;
1177 return cw_read(con->fd, buf, len, con->ssl);
1181 void yahoohook::y_close(void *fd)
1183 struct y_c *con = (struct y_c *)fd;
1184 cw_close(con->fd);
1185 delete con;
1188 void yahoohook::got_ignore(int id, YList * igns) {
1191 void yahoohook::got_cookies(int id) {
1194 void yahoohook::chat_cat_xml(int id, const char *xml) {
1195 face.log(_("+ [yahoo] chat_cat_xml"));
1198 void yahoohook::chat_join(int id, const char *me, const char *room, const char *topic, YList *members, void *fd) {
1199 face.log(_("+ [yahoo] chat_join"));
1202 void yahoohook::chat_userjoin(int id, const char *me, const char *room, struct yahoo_chat_member *who) {
1203 face.log(_("+ [yahoo] chat_userjoin"));
1206 void yahoohook::chat_userleave(int id, const char *me, const char *room, const char *who) {
1207 face.log(_("+ [yahoo] chat_userleave"));
1210 void yahoohook::chat_message(int id, const char *me, const char *who, const char *room, const char *msg, int msgtype, int utf8) {
1211 face.log(_("+ [yahoo] chat_message from %s"), who);
1214 void yahoohook::rejected(int id, const char *who, const char *msg) {
1215 imcontact ic(who, yahoo);
1216 string text = cuthtml(msg?msg: "", chCutBR | chLeaveLinks);
1217 string message = "The user has rejected your authorization request";
1219 if (!text.empty())
1221 message += " (";
1222 message += text;
1223 message += ")";
1226 em.store(imnotification(ic, message));
1229 void yahoohook::got_webcam_image(int id, const char * who, const unsigned char *image, unsigned int image_size, unsigned int real_size, unsigned int timestamp) {
1232 void yahoohook::webcam_invite(int id, const char *me, const char *from) {
1235 void yahoohook::webcam_invite_reply(int id, const char *me, const char *from, int accept) {
1238 void yahoohook::webcam_closed(int id, const char *who, int reason) {
1241 void yahoohook::webcam_viewer(int id, const char *who, int connect) {
1244 void yahoohook::webcam_data_request(int id, int send) {
1247 void yahoohook::got_buddyicon_request(int id, const char *me, const char *who) {
1250 void yahoohook::got_buddyicon(int id, const char *me, const char *who, const char *url, int checksum) {
1253 void yahoohook::buddyicon_uploaded(int id, const char *url) {
1256 void yahoohook::chat_yahooerror(int id, const char *me) {
1257 face.log(_("+ [yahoo] chat_yahooerror"));
1260 void yahoohook::chat_yahoologout(int id, const char *me) {
1261 face.log(_("+ [yahoo] chat_yahoologout"));
1264 int yahoohook::yahoo_connect(const char *host, int port) {
1265 face.log(_("+ [yahoo] yahoo connect"));
1266 return -1;
1269 void yahoohook::file_transfer_done(int id, int result, void *data) {
1272 char *yahoohook::get_ip_addr(const char *domain) {
1273 face.log(_("+ [yahoo] get_ip_addr %s"), domain);
1274 return NULL;
1277 void yahoohook::got_buddy_change_group(int id, const char *me, const char *who, const char *old_group, const char *new_group) {
1278 face.log(_("+ [yahoo] yahoo buddy_change_group %s from %s to %s"), who, old_group, new_group);
1281 void yahoohook::got_buddyicon_checksum(int id, char const *me, const char *who, int checksum) {
1284 void yahoohook::got_buzz(int id, const char *me, const char *who, long tm) {
1285 imcontact ic(who, yahoo);
1287 em.store(imnotification(ic, (string)
1288 _("The user has buzzed you")));
1291 void yahoohook::got_ft_data(int id, const unsigned char *in, int len, void *data) {
1294 void yahoohook::auth_request(int id, const char *who, const char *msg) {
1295 imcontact ic(who, yahoo);
1296 string text = cuthtml(msg?msg: "", chCutBR | chLeaveLinks);
1298 yhook.checkinlist(ic);
1299 text = yhook.decode(text, true);
1301 em.store(imauthorization(ic, imevent::incoming, imauthorization::Request, text));
1304 int yahoohook::ylog(const char *fmt, ...) {
1305 if(conf->getdebug()) {
1306 char buf[NOTIFBUF];
1307 va_list ap;
1309 va_start(ap, fmt);
1310 vsnprintf(buf, NOTIFBUF, fmt, ap);
1311 va_end(ap);
1313 face.log(buf);
1316 return 0;
1319 void yahoohook::get_fd(int id, void *fd, int error, void *data) {
1320 const char *fname = (const char *) data;
1321 struct y_c *con = (struct y_c *)fd;
1322 char buf[1024];
1323 int size = 0;
1325 if(!error) {
1326 ifstream f(fname);
1328 if(f.is_open()) {
1329 while(!f.eof()) {
1330 f.read(buf, 1024);
1331 cw_write(con->fd, buf, f.tellg(), con->ssl);
1332 size += f.tellg();
1334 f.close();
1338 map<const char *, imfile>::iterator ir = yhook.srfiles.find(fname);
1339 if(ir != yhook.srfiles.end()) {
1340 face.transferupdate(fname, ir->second,
1341 error ? icqface::tsError : icqface::tsFinish,
1342 size, size);
1344 yhook.srfiles.erase(ir);
1347 vector<char *>::iterator i = find(yhook.sfiles.begin(), yhook.sfiles.end(), fname);
1348 if(i != yhook.sfiles.end()) {
1349 delete *i;
1350 yhook.sfiles.erase(i);
1354 void yahoohook::get_url(int id, int fd, int error, const char *filename, unsigned long size, void *data) {
1355 int rsize = 0;
1356 const char *localname = (const char *) data;
1358 if(!error) {
1359 vector<char *>::iterator i = find(yhook.sfiles.begin(), yhook.sfiles.end(), localname);
1360 if(i != yhook.sfiles.end()) {
1361 ofstream f(localname);
1363 if(f.is_open()) {
1364 int r;
1365 char buf[1024];
1366 FILE *fp = fdopen(fd, "r");
1368 while(!feof(fp)) {
1369 r = fread(buf, 1, 1024, fp);
1370 if(r > 0) f.write(buf, r);
1371 rsize += r;
1374 fclose(fp);
1375 f.close();
1376 } else {
1377 error = -1;
1380 delete *i;
1381 yhook.sfiles.erase(i);
1385 map<const char *, imfile>::iterator ir = yhook.srfiles.find(localname);
1386 if(ir != yhook.srfiles.end()) {
1387 face.transferupdate(justfname(localname), ir->second,
1388 error ? icqface::tsError : icqface::tsFinish,
1389 size, rsize);
1391 yhook.srfiles.erase(ir);
1396 #endif