manager.cpp

00001 /* 00002 $Id: manager_8cpp-source.html,v 1.1 2004/10/05 21:12:02 mentat Exp $ 00003 00004 GNU Messenger - The secure instant messenger 00005 Copyright (C) 1999-2003 Henrik Abelsson <henrik@abelsson.com> 00006 Copyright (C) 2002-2004 Jesse Lovelace <jesse at aslogicsys dot com> 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 */ 00022 00023 #include <iostream> 00024 #include <map> 00025 00026 #include "gm/manager.h" 00027 #include "gm/tocprotocol.h" 00028 #include "gm/settings_manager.h" 00029 #include "gm/debug.h" 00030 #include "gm/contact_manager.h" 00031 00032 namespace GNUMessenger { 00033 00034 using namespace std; 00035 00036 ProtocolManager::~ProtocolManager() 00037 { 00038 if (!m_shutdown) 00039 shutdown(); 00040 } 00041 00042 void ProtocolManager::shutdown() 00043 { 00044 LOG("Shutting down Protocol Manager..."); 00045 for (map<string, Protocol *>::iterator it = m_provider.begin(); 00046 it != m_provider.end(); it++) 00047 { 00048 if (it->second) { 00049 it->second->shutdown(); 00050 delete it->second; 00051 it->second = 0; 00052 } 00053 } 00054 m_shutdown = true; 00055 00056 } 00057 00058 bool ProtocolManager::removeProtocol(const string &proto) 00059 { 00060 LOG_DEBUG("ProtocolManager::removeProtocol:" << proto); 00061 00062 logout(proto); 00063 Protocol * myProto = protocol(proto); 00064 00065 if (!myProto) { 00066 LOG_ERROR("Can't find protocol!"); 00067 return false; 00068 } 00069 00070 m_provider.erase(proto); 00071 00072 delete myProto; 00073 00074 return true; 00075 } 00076 00077 bool ProtocolManager::loadProtocol(const string& name) 00078 { 00079 LOG_DEBUG("ProtocolManager::loadProtocol:" << name); 00080 XMLNode config; 00081 00082 if (m_auth.settings().getNet("toc", config)) { 00083 00084 new TocProtocol(config, this); 00085 00086 return true; 00087 } 00088 00089 LOG_ERROR("Could not find protocol settings."); 00090 00091 return false; 00092 00093 } 00094 00095 bool ProtocolManager::addProtocol(Protocol *proto) 00096 { 00097 LOG_DEBUG("ProtocolManager::addProtocol: " << proto->getProtocol()); 00098 00099 if (m_provider.find(proto->getProtocol()) == m_provider.end()) { 00100 00101 LOG_DEBUG( "Adding " << proto->getProtocol() << " to protocol manager"); 00102 m_provider[proto->getProtocol()]=proto; 00104 vector<Contact> contacts; 00105 00106 LOG_DEBUG("Loading in contacts to protocol..."); 00107 m_auth.contacts().getContactsOfProtocol(proto->getProtocol(), contacts); 00108 00109 proto->setContacts(contacts); 00110 00111 } else { 00112 LOG_ERROR("Cannot add duplicate protocols to the provider list, deleting: " << proto->getProtocol()); 00113 00114 delete proto; 00115 return false; 00116 } 00117 return true; 00118 00119 } 00120 00121 bool ProtocolManager::hasProtocol(const string& name) 00122 { 00123 if (m_provider.find(name) != m_provider.end()) { 00124 return true; 00125 } 00126 return false; 00127 } 00128 00129 bool ProtocolManager::changeSettings(const string& proto, XMLNode& conf) 00130 { 00131 LOG_DEBUG("ProtocolManager::changeSetting: " << proto); 00132 00133 if (m_provider.find(proto) != m_provider.end()) { 00134 LOG_DEBUG("Updating setings"); 00135 m_provider[proto]->changeSettings(conf); 00136 return true; 00137 } else // add the protocol (this implies there were no settings before)! 00138 LOG_DEBUG("Adding new protocol (with new settings)"); 00139 if (proto == "toc") { 00140 new TocProtocol(conf, this); 00141 return true; 00142 } 00143 #if 0 00144 else 00145 if (proto == "icq") 00146 new IcqProtocol(conf, this); 00147 else 00148 if (proto == "yahoo") 00149 new YahooProtocol(conf, this); 00150 else 00151 if (proto == "msn") 00152 new MsnProtocol(conf, this); 00153 else 00154 if (proto == "kit") 00155 new KitProtocol(conf, this); 00156 #endif 00157 return false; 00158 } 00159 00160 int ProtocolManager::getState(const string &proto) 00161 { 00162 if (m_provider.count(proto)) 00163 return m_provider[proto]->getState(); // needs to return 00164 00165 return Protocol::S_invalid; 00166 } 00167 00168 vector<string> ProtocolManager::protocols() const 00169 { 00170 vector<string> tmp; 00171 for (map<string, Protocol*>::const_iterator i=m_provider.begin();i!=m_provider.end();i++) 00172 tmp.push_back(i->first); 00173 00174 return tmp; 00175 } 00176 00177 Protocol *ProtocolManager::protocol(const string &proto) 00178 { 00179 map<string, Protocol *>::iterator it = m_provider.find(proto); 00180 if (it != m_provider.end()) 00181 return it->second; 00182 else 00183 return NULL; 00184 } 00185 00186 const Protocol *ProtocolManager::protocol(const string &proto) const 00187 { 00188 map<string, Protocol *>::const_iterator cit = m_provider.find(proto); 00189 if (cit != m_provider.end()) 00190 return cit->second; 00191 else 00192 return NULL; 00193 } 00194 00195 string ProtocolManager::screenName(const string &proto) 00196 { 00197 if (m_provider.count(proto)) 00198 return m_provider[proto]->getLoginName(); 00199 else 00200 return ""; 00201 } 00202 00203 bool ProtocolManager::update(const string &proto) 00204 { 00205 if (m_provider.find(proto) != m_provider.end()) 00206 { 00207 m_provider[proto]->update(); 00208 return true; 00209 } 00210 return false; 00211 } 00212 00213 void ProtocolManager::updateAll() 00214 { 00215 LOG_DEBUG("ProtocolManager::updateAll"); 00216 for (map<string, Protocol*>::iterator i=m_provider.begin();i!=m_provider.end();i++) 00217 i->second->update(); 00218 } 00219 00220 bool ProtocolManager::setAway(const string& proto, const string& msg) 00221 { 00222 LOG_DEBUG("ProtocolManager::setAway: " << proto); 00223 if (m_provider.count(proto)) { 00224 m_provider[proto]->setAway(msg); 00225 return true; 00226 } 00227 00228 return false; 00229 } 00230 00231 void ProtocolManager::setAllAway(const string& msg) 00232 { 00233 for (map<string, Protocol*>::iterator i=m_provider.begin();i!=m_provider.end();i++) 00234 i->second->setAway(msg); 00235 } 00236 00237 bool ProtocolManager::setInfo(const string& proto, const string& info) 00238 { 00239 if (m_provider.count(proto)) { 00240 m_provider[proto]->setInfo(info); 00241 return true; 00242 } 00243 00244 return false; 00245 } 00246 00247 void ProtocolManager::setAllInfo(const string& info) 00248 { 00249 for (map<string, Protocol*>::iterator i=m_provider.begin();i!=m_provider.end();i++) 00250 i->second->setInfo(info); 00251 } 00252 00253 bool ProtocolManager::login(const string &proto) 00254 { 00255 LOG_DEBUG("ProtocolManager::login: " << proto); 00256 if (m_provider.count(proto)) { 00257 if (!isOnline(proto)) 00258 m_provider[proto]->login(); 00259 return true; 00260 } 00261 return false; 00262 } 00263 00264 bool ProtocolManager::logout(const string &proto) 00265 { 00266 LOG_DEBUG("ProtocolManager::logout: " << proto); 00267 if (m_provider.count(proto)) { 00268 LOG_DEBUG("Proto not found."); 00269 if (isOnline(proto)) 00270 m_provider[proto]->logout(); 00271 return true; 00272 } 00273 return false; 00274 00275 } 00276 00277 bool ProtocolManager::sendMessage(const string &proto,const Contact &recipient, const string &message) 00278 { 00279 LOG_DEBUG("ProtocolManager::sendMessage"); 00280 if (isOnline(proto)) { 00281 m_provider[proto]->sendMessage(recipient, message); 00282 return true; 00283 } 00284 LOG_DEBUG("That protocol is not online."); 00285 return false; 00286 } 00287 00288 bool ProtocolManager::sendMessage(const string &proto,const Contact &recipient, const VBuffer& data) 00289 { 00290 if (isOnline(proto)) { 00291 m_provider[proto]->sendMessage(recipient, data); 00292 return true; 00293 } 00294 return false; 00295 } 00296 00297 bool ProtocolManager::sendMessage(const Contact& c, const string& message) 00298 { 00299 LOG_DEBUG("ProtocolManager::sendMessage: " << c.getProtocol()); 00300 00301 if (c.getProtocol().length() == 0) { 00302 LOG_DEBUG("Cannot send, no protocol online"); 00303 return false; 00304 } 00305 00306 return sendMessage(c.getProtocol(), c, message); 00307 00308 } 00309 00310 bool ProtocolManager::sendMessage(const Contact& c, const VBuffer& data) 00311 { 00312 LOG_DEBUG("Send message (data) with protocol " << c.getProtocol()); 00313 00314 if (c.getProtocol().length() == 0) { 00315 LOG_DEBUG("Cannot send, no protocol online"); 00316 return false; 00317 } 00318 00319 return sendMessage(c.getProtocol(), c, data); 00320 00321 } 00322 00323 void ProtocolManager::addContact(const Contact& c) 00324 { 00325 for (map<string, Protocol*>::iterator it = m_provider.begin(); 00326 it != m_provider.end(); 00327 it++) { 00328 LOG_DEBUG("Updating " << c.name() << " in proto " << it->first); 00329 if (c.hasProtocol(it->first)) 00330 (*it->second).addContact(c); 00331 else // if the protocol doesn't have the contact, make sure it isnt there 00332 (*it->second).delContact(c); 00333 } 00334 } 00335 00336 00337 bool ProtocolManager::delContact(const string& str, const Contact& c) 00338 { 00339 LOG_ERROR("ProtocolManager::delBuddy: I dont do anything"); 00340 return false; 00341 } 00342 00343 #if 0 00344 bool ProtocolManager::customRequest(const string &proto,const XMLNode &n) 00345 { 00346 if (m_provider.count(proto)) { 00347 m_provider[proto]->customRequest(n); 00348 return true; 00349 } 00350 return false; 00351 } 00352 #endif 00353 00354 void ProtocolManager::protoGotContacts(const string& proto, const vector<buddy>& c) 00355 { 00356 LOG_DEBUG("ProtocolManager::protoGotContacts: " << proto); 00357 c_gotContacts(proto, c); 00358 } 00359 00360 void ProtocolManager::protoLoggedIn(const string& proto) 00361 { 00362 LOG_DEBUG("ProtocolManager::protoLoggedIn: " << proto); 00363 00364 m_online[proto] = true; 00365 00366 c_loggedIn(proto); 00367 } 00368 00369 void ProtocolManager::protoLoggedOut(const string& proto) 00370 { 00371 LOG_DEBUG("ProtocolManager::protoLoggedOut: " << proto); 00372 00373 m_online[proto] = false; 00374 00375 c_loggedOut(proto); 00376 } 00377 00378 void ProtocolManager::protoRecvdMessage(const string& proto, const Contact& c, const string& message) 00379 { 00380 LOG_DEBUG("Received message: from: " << c.name() << " on: " << proto); 00381 c_recvdMessage(proto, c, message); 00382 } 00383 00384 void ProtocolManager::protoRecvdMessageAnony(const string& proto, const Contact& c, const string& message) 00385 { 00386 LOG_DEBUG("Recieved Anonymous message from " << c.name() << " on: " << proto); 00387 c_recvdMessageAnony(proto, c, message); 00388 } 00389 00390 void ProtocolManager::protoStatusChange(const string& proto, const Contact& c) 00391 { 00392 LOG_DEBUG("Status change for: " << c.name()); 00393 // need to update active list here 00394 c_statusChange(proto, c); 00395 } 00396 00397 void ProtocolManager::protoError(const string& proto, int err_no, const string& error) 00398 { 00399 LOG_DEBUG("Error. Protocol: " << proto << " Error: " << error); 00400 c_error(proto, err_no, error); 00401 } 00402 00403 void ProtocolManager::protoStateChange(const string &proto, Protocol::State st) 00404 { 00405 LOG_DEBUG("Protocol State Change: " << proto); 00406 00407 switch (st) { 00408 case (Protocol::S_online): 00409 LOG_DEBUG("Protocol Online"); break; 00410 case (Protocol::S_invalid): 00411 LOG_DEBUG("Invalid State"); break; 00412 case (Protocol::S_connecting): 00413 LOG_DEBUG("Protocol Connecting"); break; 00414 case (Protocol::S_offline): 00415 LOG_DEBUG("Protocol Offline"); break; 00416 case (Protocol::S_disconnecting): 00417 LOG_DEBUG("Protocol Disconnecting"); break; 00418 default: LOG_DEBUG("Unknown state"); 00419 } 00420 00421 c_stateChange(proto, st); 00422 } 00423 00424 void ProtocolManager::protoGotPubkey(const Contact& c, const string& key) 00425 { 00426 LOG_DEBUG("Got public key from: " << c.name()); 00427 c_gotPubkey(c, key); 00428 } 00429 /* 00430 void ProtocolManager::protoGotBuddy(const string& proto, const Contact& c) 00431 { 00432 c_gotBuddy(proto, c); 00433 }*/ 00434 00435 void ProtocolManager::protoCustom(const string& proto, const XMLNode &n) 00436 { 00437 LOG_DEBUG("Got custom: " << proto); 00438 c_custom(proto, n); 00439 } 00440 00441 list<Network*> ProtocolManager::getNets(const string& proto) 00442 { 00443 LOG_DEBUG("ProtocolManager::getNets: " << proto); 00444 if (m_provider.count(proto)) 00445 return m_provider[proto]->getNetworks(); 00446 00447 // temp fix for "not all paths return a value 00448 list<Network *> dummy; 00449 return dummy; 00450 } 00451 00452 list<Network*> ProtocolManager::getNetsAll() 00453 { 00454 LOG_DEBUG("ProtocolManager::getNetsAll"); 00455 list<Network *> tmp; 00456 for (map<string, Protocol*>::iterator i = m_provider.begin(); 00457 i != m_provider.end(); 00458 i++) 00459 { 00460 list<Network *> nets = i->second->getNetworks(); 00461 00462 for (list<Network*>::iterator j = nets.begin(); 00463 j != nets.end(); 00464 j++) { 00465 tmp.push_back(*j); 00466 } 00467 } 00468 return tmp; 00469 } 00470 00473 bool ProtocolManager::isGM(const Contact& c) 00474 { 00475 return false; 00476 } 00477 00479 void ProtocolManager::forceKeyX(const Contact& c) 00480 { 00481 // this must interface ssh2 code 00482 } 00483 00486 void ProtocolManager::changeCipher(const Contact& c, int cipherType, int keysize , int blocksize) 00487 { 00488 // this must interface ssh2 code 00489 } 00490 00491 void ProtocolManager::getInfo(const Contact& contact) 00492 { 00493 for (map<string, Protocol*>::iterator it = m_provider.begin(); 00494 it != m_provider.end(); 00495 it++) { 00496 if (contact.hasProtocol(it->first)) 00497 (it->second)->getInfo(contact.getServerId(it->first)); 00498 } 00499 00500 } 00501 00502 void ProtocolManager::protoGotInfo(const Contact& c, const string& info, const string& proto) 00503 { 00504 c_gotInfo(c, info, proto); 00505 } 00506 00507 bool ProtocolManager::isOnline(const string& proto) const 00508 { 00509 map<string, bool>::const_iterator it = m_online.find(proto); 00510 if (m_online.find(proto) == m_online.end()) { 00511 return false; 00512 } 00513 return it->second; 00514 00515 } 00516 00518 unsigned long ProtocolManager::getSentBytes(const string& proto) const 00519 { 00520 const Protocol * protop = protocol(proto); 00521 if (protop) 00522 return protop->getSentBytes(); 00523 else 00524 return 0; 00525 } 00526 unsigned long ProtocolManager::getSentPackets(const string& proto) const 00527 { 00528 const Protocol * protop = protocol(proto); 00529 if (protop) 00530 return protop->getSentPackets(); 00531 else 00532 return 0; 00533 } 00534 00535 unsigned long ProtocolManager::getRecvBytes(const string& proto) const 00536 { 00537 const Protocol * protop = protocol(proto); 00538 if (protop) 00539 return protop->getRecvBytes(); 00540 else 00541 return 0; 00542 } 00543 00544 unsigned long ProtocolManager::getRecvPackets(const string& proto) const 00545 { 00546 const Protocol * protop = protocol(proto); 00547 if (protop) 00548 return protop->getRecvPackets(); 00549 else 00550 return 0; 00551 } 00552 00553 vector<string> ProtocolManager::getRemoteIPs(const string& proto) const 00554 { 00555 vector<string> ips; 00556 const Protocol * protop = protocol(proto); 00557 if (protop) 00558 return protop->getRemoteIPs(); 00559 else 00560 return ips; 00561 } 00562 00563 } 00564 /* 00565 ----- 00566 $Log: manager.cpp,v $ 00567 Revision 1.5 2004/10/04 23:28:44 mentat 00568 Adding VC 2003 project file and compiles on Win32 00569 00570 Revision 1.4 2004/10/04 05:14:20 mentat 00571 Adding info get support and HTTP. 00572 00573 Revision 1.3 2004/10/03 18:39:40 mentat 00574 Merging in hash_map changes to TOC and getInfo methods. 00575 00576 Revision 1.2.2.1 2004/10/03 18:36:03 mentat 00577 Added hash_map to toc protocol and working in getInfo methods. 00578 00579 Revision 1.2 2004/10/03 15:22:57 mentat 00580 Fixed a pure virtual function error and improved TOC. 00581 00582 Revision 1.1.1.1 2004/10/03 06:17:37 mentat 00583 Initial re-import. 00584 00585 */

Generated on Tue Oct 5 14:41:47 2004 for GNU Messenger by doxygen 1.3.8