fedoraproject.cpp

Go to the documentation of this file.
00001 /*
00002 #########################################################################
00003 #
00004 #  This file is part of trustyRC.
00005 #
00006 #  trustyRC, fully modular IRC robot 
00007 #  Copyright (C) 2006-2008 Nicoleau Fabien 
00008 #
00009 #  trustyRC is free software: you can redistribute it and/or modify
00010 #  it under the terms of the GNU General Public License as published by
00011 #  the Free Software Foundation, either version 3 of the License, or
00012 #  (at your option) any later version.
00013 #
00014 #  trustyRC is distributed in the hope that it will be useful,
00015 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 #  GNU General Public License for more details.
00018 #
00019 #  You should have received a copy of the GNU General Public License
00020 #  along with trustyRC.  If not, see <http://www.gnu.org/licenses/>.
00021 #
00022 #########################################################################
00023 */
00024 
00029 #include "fedoraproject.h"
00030 
00034 FedoraProject::FedoraProject(BotKernel*b)
00035 {
00036         this->author = "eponyme";
00037         this->description = "Plugin in connection with fedora project";
00038         this->version = VERSION;
00039         this->name = "fedoraproject";
00040    this->bindFunction("whoowns",IN_COMMAND_HANDLER,"whoowns",0,60);
00041    this->bindFunction("fas",IN_COMMAND_HANDLER,"fas",0,60);
00042         this->bindFunction("reloadfas",IN_COMMAND_HANDLER,"reloadfas",0,60);
00043    this->addRequirement("admin");
00044 
00045         if (!this->loadFasFile(b->getDatasDir()+"fas.txt")) {
00046                 b->getSysLog()->log("Unable to load fas file",WARNING);
00047         }
00048 }
00049 
00055 string FedoraProject::whoowns(string name) 
00056 {
00057    const string URL = "https://admin.fedoraproject.org/pkgdb/packages/name/"+Tools::urlencode(name);
00058    const string LAST_OWNER = "<span class=\"ownerName\"><a href=\"";
00059    const string END_OWNER = "</a>";
00060    CURL *curl;  
00061    CURLcode result;  
00062    string::size_type pos;
00063    char errorBuffer[CURL_ERROR_SIZE];  
00064    string buffer;  
00065    curl = curl_easy_init();  
00066    if (curl) {  
00067       curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);  
00068       curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());  
00069       curl_easy_setopt(curl, CURLOPT_HEADER, 0);  
00070       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);  
00071       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, FedoraProject::writer);  
00072       curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); 
00073       result = curl_easy_perform(curl);  
00074       curl_easy_cleanup(curl);  
00075       if (result == CURLE_OK) {  
00076          pos = buffer.rfind(LAST_OWNER);
00077          if ( pos == string::npos ){
00078             return (name+" : package not found");
00079          }
00080          else {
00081             buffer = buffer.substr(pos+LAST_OWNER.length());
00082             buffer = buffer.substr(buffer.find(">")+1);
00083             return (name+"'s owner : "+buffer.substr(0,buffer.find(END_OWNER)));
00084          }
00085       }  
00086       else {  
00087          return (name+" : package not found");
00088       }  
00089    } 
00090    return "curl init error";
00091 }
00092 
00097 bool FedoraProject::loadFasFile(string file) {
00098         this->usersInfos.clear();
00099         vector<string> temp,tempinfos;
00100         ifstream ifs(file.c_str());
00101    if ( ifs ) {
00102         string line;
00103       while ( getline( ifs, line ) ) {
00104                         temp = Tools::stringToVector(line,",",0) ;
00105                         tempinfos.clear();
00106                         tempinfos.push_back(temp[1]);
00107                         tempinfos.push_back(temp[2]);
00108                         tempinfos.push_back(temp[3]);
00109                         this->usersInfos[temp[0]] = tempinfos;
00110       }
00111                 ifs.close();
00112                 return true;
00113         }
00114         else {
00115                 return false;
00116         }
00117 }
00118 
00127 vector<string> FedoraProject::getFasUserInfos(string nick) {
00128         vector<string> notFound;
00129         notFound.push_back("user not found");
00130         map<string,vector<string> >::iterator fter = this->usersInfos.find(nick);
00131         if ( fter != this->usersInfos.end() ) {
00132                 return fter->second ;
00133         }
00134         else {
00135                 return notFound;
00136         }
00137 }
00138 
00139 int FedoraProject::writer(char *data, size_t size, size_t nmemb,string *buffer)  
00140 {  
00141    int result = 0;  
00142    if (buffer != NULL) {  
00143       buffer->append(data, size * nmemb);  
00144       result = size * nmemb;  
00145    }   
00146    return result;  
00147 }
00148 
00149 extern "C"
00150 {
00151         Plugin *contruct_fedoraproject(BotKernel*b)
00152         {
00153                 return new FedoraProject(b);
00154         }
00155         void destroy_fedoraproject(Plugin*p)
00156         {
00157                 delete p;
00158         }
00159         bool whoowns (Message*m,Plugin*p,BotKernel*b)
00160         {
00161       FedoraProject * fp = (FedoraProject*)p;
00162       if ( m->isPublic() && (m->nbParts() == 5) ) {
00163          b->send(IRCProtocol::sendMsg(m->getSource(),fp->whoowns(m->getPart(4))));
00164       }
00165                 return true;
00166         }
00167         bool fas (Message*m,Plugin*p,BotKernel*b)
00168         {
00169       FedoraProject * fp = (FedoraProject*)p;
00170       if ( m->isPublic() && (m->nbParts() == 5) ) {
00171          b->send(IRCProtocol::sendMsg(m->getSource(),Tools::vectorToString(fp->getFasUserInfos(m->getPart(4)),",",0)));
00172       }
00173                 return true;
00174         }
00175         bool reloadfas(Message*m,Plugin*p,BotKernel*b) 
00176         {
00177       pPlugin * ppAdm = b->getPlugin("admin");
00178       Admin*adm = NULL;
00179                 FedoraProject * fp = (FedoraProject*)p;
00180       if (ppAdm!=NULL) {
00181                         adm = (Admin*)ppAdm->object ;
00182         if ( adm->isSuperAdmin( m->getSender()) && m->isPrivate()) {
00183                                 if (fp->loadFasFile(b->getDatasDir()+"fas.txt")) {
00184                                         b->getSysLog()->log("FAS file reloaded by "+m->getSender(),INFO);
00185                                         b->send(IRCProtocol::sendNotice(m->getNickSender(),"FAS file reloaded"));
00186                                 }
00187                                 else {
00188                                         b->getSysLog()->log("Unable to load fas file (by "+m->getSender()+")",WARNING);
00189                                         b->send(IRCProtocol::sendNotice(m->getNickSender(),"Unable to load fas file"));
00190                                 }
00191         }
00192                 }
00193                 return true;
00194         }
00195 }

Generated on Sun Aug 16 15:28:27 2009 for trustyRC by  doxygen 1.5.8