3 * centerim protocol specific user interface related routines
4 * $Id: imcontroller.cc,v 1.56 2004/07/20 22:16:40 konst Exp $
6 * Copyright (C) 2001-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
25 #include "imcontroller.h"
28 #include "yahoohook.h"
31 #include "jabberhook.h"
34 #include "icqcontacts.h"
35 #include "eventmanager.h"
37 #define clr(c) conf->getcolor(c)
39 imcontroller imcontrol
;
41 imcontroller::imcontroller() {
44 imcontroller::~imcontroller() {
47 bool imcontroller::regdialog(protocolname pname
) {
48 bool finished
, success
;
51 string prserv
= conf
->getsockshost() + ":" + i2str(conf
->getsocksport());
55 finished
= success
= false;
56 rnick
= rfname
= rlname
= remail
= rpasswd
= "";
58 rserver
= icqconf::defservers
[pname
].server
+ ":" +
59 i2str(icqconf::defservers
[pname
].port
);
61 db
.setwindow(new textwindow(0, 0, face
.sizeDlg
.width
, face
.sizeDlg
.height
,
62 clr(cp_dialog_frame
), TW_CENTERED
));
64 db
.getwindow()->set_titlef(clr(cp_dialog_highlight
),
65 _(" Register on the %s network "),
66 conf
->getprotocolname(pname
).c_str());
68 db
.settree(new treeview(clr(cp_dialog_text
), clr(cp_dialog_selected
),
69 clr(cp_dialog_highlight
), clr(cp_dialog_text
)));
71 db
.setbar(new horizontalbar(clr(cp_dialog_text
), clr(cp_dialog_selected
),
72 _("Change"), _("Go ahead"), 0));
75 treeview
&t
= *db
.gettree();
80 i
= db
.gettree()->addnode(_(" Details "));
81 t
.addleaff(i
, 0, 4, _(" Nickname : %s "), rnick
.c_str());
83 t
.addleaff(i
, 0, 5, _(" E-Mail : %s "), remail
.c_str());
84 t
.addleaff(i
, 0, 6, _(" First name : %s "), rfname
.c_str());
85 t
.addleaff(i
, 0, 7, _(" Last name : %s "), rlname
.c_str());
87 i
= db
.gettree()->addnode(_(" Password "));
88 t
.addleaff(i
, 0, 2, _(" Password to set : %s "), string(rpasswd
.size(), '*').c_str());
89 t
.addleaff(i
, 0, 3, _(" Check the password : %s "), string(pcheck
.size(), '*').c_str());
92 i
= db
.gettree()->addnode(_(" Details "));
93 t
.addleaff(i
, 0, 8, _(" Server to use : %s "), rserver
.c_str());
96 finished
= !db
.open(n
, b
, (void **) &i
);
102 case 2: rpasswd
= face
.inputstr(_("Password: "), rpasswd
, '*'); break;
103 case 3: pcheck
= face
.inputstr(_("Check the password you entered: "), pcheck
, '*'); break;
104 case 4: rnick
= face
.inputstr(_("Nickname to set: "), rnick
); break;
105 case 5: remail
= face
.inputstr(_("E-Mail: "), remail
); break;
106 case 6: rfname
= face
.inputstr(_("First name: "), rfname
); break;
107 case 7: rlname
= face
.inputstr(_("Last name: "), rlname
); break;
108 case 8: rserver
= face
.inputstr(_("Server: "), rserver
); break;
113 if(rpasswd
!= pcheck
) {
114 face
.status(_("Passwords do not match"));
115 } else if(rpasswd
.empty()) {
116 face
.status(_("Password must be entered"));
117 } else if(pname
== icq
&& rpasswd
.size() < 6) {
118 face
.status(_("Password must be at least 6 chars long"));
120 finished
= success
= true;
131 bool imcontroller::uinreg(icqconf::imaccount
&account
) {
133 abstracthook
&hook
= gethook(account
.pname
);
135 if(success
= regdialog(account
.pname
)) {
136 unlink((conf
->getdirname() + conf
->getprotocolname(account
.pname
) + "-infoset").c_str());
137 face
.progress
.show(_(" Registration progress "));
139 for(fin
= false; !fin
; ) {
140 face
.progress
.log(_("Connecting to the server %s ..."), rserver
.c_str());
142 if(hook
.regconnect(rserver
)) {
144 face
.progress
.log(_("Sending request"));
146 while(!ruin
&& !fin
) {
147 fin
= hook
.regattempt(ruin
, rpasswd
, remail
);
150 if(fin
= (face
.ask(_("Timed out waiting for a new uin. Retry?"),
151 ASK_YES
| ASK_NO
, ASK_YES
) == ASK_NO
)) {
154 face
.progress
.log(_("Retrying.."));
160 face
.progress
.log(_("New UIN received, %lu"), ruin
);
162 account
.password
= rpasswd
;
165 ofstream
f((conf
->getdirname() + conf
->getprotocolname(account
.pname
) + "-infoset").c_str());
167 f
<< rnick
<< endl
<<
176 face
.progress
.log(_("Disconnected"));
179 fin
= (face
.ask(_("Unable to connect to icq server. Retry?"),
180 ASK_YES
| ASK_NO
) == ASK_NO
);
184 face
.progress
.hide();
190 bool imcontroller::jabberregistration(icqconf::imaccount
&account
) {
196 if(success
= regdialog(account
.pname
)) {
197 unlink((conf
->getdirname() + "jabber-infoset").c_str());
198 face
.progress
.show(_(" Registration progress "));
200 face
.progress
.log(_("Trying to register %s at %s ..."),
201 rnick
.c_str(), rserver
.c_str());
203 if(success
= jhook
.regnick(rnick
, rpasswd
, rserver
, err
)) {
204 account
.nickname
= rnick
;
205 account
.password
= rpasswd
;
206 account
.server
= rserver
;
208 if((pos
= account
.server
.find(":")) != -1) {
209 account
.port
= atoi(account
.server
.substr(pos
+1).c_str());
210 account
.server
.erase(pos
);
212 account
.port
= icqconf::defservers
[jabber
].port
;
215 face
.progress
.log(_("The Jabber ID was successfully registered"));
218 ofstream
f((conf
->getdirname() + "jabber-infoset").c_str());
220 f
<< rnick
<< endl
<<
228 string msgerr
= _("Failed");
229 if(!err
.empty()) msgerr
+= (string
) ": " + err
;
230 face
.progress
.log("%s", msgerr
.c_str());
234 face
.progress
.log(_("Disconnected"));
236 face
.progress
.hide();
245 void imcontroller::icqupdatedetails() {
247 icqcontact
*c
= clist
.get(contactroot
);
251 ihook
.requestinfo(imcontact(conf
->getourid(icq
).uin
, icq
));
253 if(face
.updatedetails()) {
254 ihook
.sendupdateuserinfo(*c
);
257 face
.status(_("You must be logged to the ICQ network to update the details"));
262 void imcontroller::aimupdateprofile() {
264 icqcontact
*c
= clist
.get(contactroot
);
267 ahook
.requestinfo(imcontact(conf
->getourid(aim
).nickname
, aim
));
268 if(face
.updatedetails(0, aim
)) ahook
.sendupdateuserinfo(*c
);
272 void imcontroller::msnupdateprofile() {
275 mhook
.requestinfo(imcontact(conf
->getourid(msn
).nickname
, msn
));
276 string tmp
= face
.inputstr(_("new MSN friendly nick: "), clist
.get(contactroot
)->getnick());
278 if(face
.getlastinputkey() != KEY_ESC
&& !tmp
.empty()) {
279 icqcontact
*c
= clist
.get(contactroot
);
281 mhook
.sendupdateuserinfo(*c
);
284 face
.status(_("You must be logged to the MSN network to update the friendly nick"));
289 void imcontroller::jabberupdateprofile() {
294 (c
= clist
.get(contactroot
))->clear();
295 jhook
.requestinfo(imcontact(conf
->getourid(jabber
).nickname
, jabber
));
297 if(face
.updatedetails(0, jabber
))
298 jhook
.sendupdateuserinfo(*c
);
301 face
.status(_("You must be logged to the Jabber network to update your details"));
306 void imcontroller::gaduupdateprofile() {
311 (c
= clist
.get(contactroot
))->clear();
312 ghook
.requestinfo(imcontact(conf
->getourid(gadu
).uin
, jabber
));
314 if(face
.updatedetails(0, gadu
))
315 ghook
.sendupdateuserinfo(*c
);
318 face
.status(_("You must be logged to the Gadu-Gadu network to update your details"));
323 void imcontroller::registration(icqconf::imaccount
&account
) {
324 switch(account
.pname
) {
332 jabberregistration(account
);
335 face
.status("[" + conf
->getprotocolname(account
.pname
) + "] " +
336 _("registration is not supported"));
341 void imcontroller::updateinfo(icqconf::imaccount
&account
) {
342 switch(account
.pname
) {
343 case icq
: icqupdatedetails(); break;
344 case aim
: aimupdateprofile(); break;
345 case msn
: msnupdateprofile(); break;
346 case jabber
: jabberupdateprofile(); break;
347 case gadu
: gaduupdateprofile(); break;
351 // ----------------------------------------------------------------------------
353 void imsearchparams::save(const string
&prname
) const {
354 string fname
= conf
->getconfigfname("search-profiles"), buf
, orig
;
359 of
.open((fname
+ ".tmp").c_str());
362 rf
.open(fname
.c_str());
367 if(getword(buf
= orig
, "\t") != prname
)
378 rename((fname
+ ".tmp").c_str(), fname
.c_str());
381 of
.open(fname
.c_str(), ios::app
);
385 << (int) pname
<< "\t"
386 << (onlineonly
? "1" : "0") << "\t"
392 << (int) agerange
<< "\t"
393 << (int) gender
<< "\t"
400 << department
<< "\t"
404 << (sincelast
? "1" : "0") << "\t"
405 << (reverse
? "1" : "0") << "\t"
407 << (photo
? "1" : "0") << "\t"
411 vector
<pair
<string
, string
> >::const_iterator
412 ifp
= flexparams
.begin();
414 while(ifp
!= flexparams
.end()) {
415 of
<< ifp
->first
<< "\t" << ifp
->second
<< "\t";
423 static string
getfield(string
&buf
) {
427 if((pos
= buf
.find("\t")) == -1)
430 r
= buf
.substr(0, pos
);
436 bool imsearchparams::load(const string
&prname
) {
437 ifstream
f(conf
->getconfigfname("search-profiles").c_str());
438 string buf
, flexparam
;
445 if(r
= (getword(buf
, "\t") == prname
)) {
446 pname
= (protocolname
) strtoul(getfield(buf
).c_str(), 0, 0);
447 onlineonly
= getfield(buf
) == "1";
448 uin
= strtoul(getfield(buf
).c_str(), 0, 0);
449 minage
= strtoul(getfield(buf
).c_str(), 0, 0);
450 maxage
= strtoul(getfield(buf
).c_str(), 0, 0);
451 country
= strtoul(getfield(buf
).c_str(), 0, 0);
452 language
= strtoul(getfield(buf
).c_str(), 0, 0);
453 agerange
= (ICQ2000::AgeRange
) strtoul(getfield(buf
).c_str(), 0, 0);
454 gender
= (imgender
) strtoul(getfield(buf
).c_str(), 0, 0);
455 firstname
= getfield(buf
);
456 lastname
= getfield(buf
);
457 nick
= getfield(buf
);
458 city
= getfield(buf
);
459 state
= getfield(buf
);
460 company
= getfield(buf
);
461 department
= getfield(buf
);
462 position
= getfield(buf
);
463 email
= getfield(buf
);
464 room
= getfield(buf
);
465 sincelast
= getfield(buf
) == "1";
466 reverse
= getfield(buf
) == "1";
467 service
= getfield(buf
);
468 photo
= getfield(buf
) == "1";
469 kwords
= getfield(buf
);
470 randomgroup
= strtoul(getfield(buf
).c_str(), 0, 0);
473 if(!(flexparam
= getfield(buf
)).empty())
474 flexparams
.push_back(make_pair(flexparam
, getfield(buf
)));