fedorafr.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 "fedorafr.h"
00030 
00034 Fedorafr::Fedorafr(BotKernel*b)
00035 {
00036         this->author = "eponyme";
00037         this->description = "Plugins that allow people to search on Fedora-fr.org wiki and planet";
00038         this->version = VERSION;
00039         this->name = "fedorafr";
00040         this->bindFunction("wiki",IN_COMMAND_HANDLER,"wiki",0,10);
00041         this->bindFunction("planet",IN_COMMAND_HANDLER,"planet",0,10);
00042         this->bindFunction("paste",IN_COMMAND_HANDLER,"displayPaste",0,10);
00043 }
00044 
00045 
00046 
00052 vector<string> Fedorafr::getWikiLinks(string datas) {
00053    string result;
00054    vector<string> results;
00055    const string BASE_URL = "http://doc.fedora-fr.org" ;
00056    string::size_type pos;
00057    vector<string> split = Tools::stringToVector(datas,"\n",0) ;  
00058    for (unsigned int i = 0 ; i < split.size() ; i ++ ) {
00059       pos = split[i].find("href=\"");
00060       if ( pos != string::npos ) {
00061          result = BASE_URL+split[i].substr(pos+string("href=\"").size(),split[i].find(" title=\"")-(pos+string("href=\"").size()+1)) ;
00062          if ( result.find("Discuter:") == string::npos) {
00063             results.push_back(result);
00064          }
00065       }
00066    }
00067    return results;
00068 }
00069 
00070 extern "C"
00071 {
00072         Plugin *contruct_fedorafr(BotKernel*b)
00073         {
00074                 return new Fedorafr(b);
00075         }
00076         void destroy_fedorafr(Plugin*p)
00077         {
00078                 delete p;
00079         }
00080         bool wiki (Message*m,Plugin*p,BotKernel*b)
00081         {
00082       const string NO_RESULT_TEXT = "Aucun texte d’article ne correspond à la recherche.";
00083       const string RESULTS = "mw-search-results";
00084       const string POWERSEARCH = "<form id=\"powersearch\"" ;
00085       const string END_TITLE = "</span></h2>";
00086       const string SEARCH_BOTTOM = "mw-search-pager-bottom" ;
00087       string buffer = "";
00088       string title = "";
00089       string::size_type pos=0;
00090       unsigned int startFrom=5;
00091       unsigned int nbResults ;
00092       vector<string> results;
00093       unsigned int maxResults = Tools::strToInt(b->getCONFF()->getValue(p->getName()+".wiki_max_results"));
00094       Fedorafr*ffr = (Fedorafr*)p;
00095       Socket sock = Socket();
00096       if ( m->isPublic() && (m->nbParts() >= 5) ) {
00097          if (!sock.connectSock(80,"doc.fedora-fr.org","")) {
00098             b->send(IRCProtocol::sendMsg(m->getSource(),"* Unable to connect to doc.fedora-fr.org *"));
00099             return true;
00100          }
00101          nbResults = Tools::strToInt(m->getPart(4));
00102          if (nbResults == 0 ) {
00103             nbResults = Tools::strToInt(b->getCONFF()->getValue(p->getName()+".wiki_default_results"));  
00104             startFrom = 4;
00105          }
00106          else  if (nbResults > maxResults ) {
00107             nbResults = maxResults ;
00108          }
00109          sock.sendStr("GET /wiki/Special:Search?search="+Tools::urlencode(Tools::vectorToString(m->getSplit()," ",startFrom) )+"&fulltext=Rechercher HTTP/1.1\nHost: doc.fedora-fr.org\n\n");
00110          while (buffer.find(POWERSEARCH)==string::npos) {
00111             buffer += sock.receive() ;
00112          }
00113          pos = buffer.find(RESULTS);
00114          if ( pos == string::npos) {
00115                  b->send(IRCProtocol::sendMsg(m->getSource(),"Aucun resultat."));
00116             return true;
00117          }
00118          buffer = buffer.substr(pos+string(RESULTS).length()+2);
00119          pos = buffer.find(SEARCH_BOTTOM);
00120          if ( pos == string::npos) {
00121                  b->send(IRCProtocol::sendMsg(m->getSource(),"* Error while parsing result * (1)"));
00122             return 0;
00123          }
00124          results = ffr->getWikiLinks(buffer.substr(0,pos)) ;
00125          if (nbResults > results.size() ) {
00126             nbResults = results.size();
00127          } 
00128          for ( unsigned int i = 0 ; i  < nbResults ; i ++ ) {
00129             b->send(IRCProtocol::sendMsg(m->getSource(),results[i]));
00130          }
00131       }
00132                 return true;
00133         }
00134         bool planet (Message*m,Plugin*p,BotKernel*b)
00135         {
00136       const string BEGIN_HREF = "<h2><a href=\"";
00137       const string END_HREF = "\" title=";
00138       string buffer = "";
00139       string::size_type pos=0;
00140       unsigned int startFrom=5;
00141       unsigned int nbResults ;
00142       vector<string> results;
00143       unsigned int maxResults = Tools::strToInt(b->getCONFF()->getValue(p->getName()+".planet_max_results"));
00144       Socket sock = Socket();
00145       if ( m->isPublic() && (m->nbParts() >= 5) ) {
00146          if (!sock.connectSock(80,"planet.fedora-fr.org","")) {
00147             b->send(IRCProtocol::sendMsg(m->getSource(),"* Unable to connect to planet.fedora-fr.org *"));
00148             return true;
00149          }
00150          nbResults = Tools::strToInt(m->getPart(4));
00151          if (nbResults == 0 ) {
00152             nbResults = Tools::strToInt(b->getCONFF()->getValue(p->getName()+".planet_default_results"));  
00153             startFrom = 4;
00154          }
00155          else  if (nbResults > maxResults ) {
00156             nbResults = maxResults ;
00157          }
00158          sock.sendStr("GET content/advancedsearch?SearchPageLimit=2&SearchText="+Tools::urlencode(Tools::vectorToString(m->getSplit()," ",startFrom) )+" HTTP/1.1\nHost: planet.fedora-fr.org\n\n");
00159          while (buffer.find("</body>")==string::npos) {
00160             buffer += sock.receive() ;
00161          } 
00162          do {
00163             pos = buffer.find(BEGIN_HREF);
00164             if (pos!=string::npos) {
00165                buffer = buffer.substr(pos+BEGIN_HREF.length());
00166                results.push_back(buffer.substr(0,buffer.find(END_HREF) ));
00167             }
00168          }
00169          while (pos !=string::npos); 
00170          if ( results.size() > 0 ) {
00171             if ( results.size() < nbResults ) {
00172                nbResults = results.size() ;
00173             }
00174             for (unsigned int i = 0 ; i < nbResults ; i ++ ) {
00175                b->send(IRCProtocol::sendMsg(m->getSource(),results[i]));
00176             }
00177          }
00178          else {
00179             b->send(IRCProtocol::sendMsg(m->getSource(),"Aucun resultat"));
00180          }
00181       }
00182                 return true;
00183         }
00184         bool displayPaste (Message*m,Plugin*p,BotKernel*b)
00185         {
00186       if (m->isPublic() )
00187          b->send(IRCProtocol::sendMsg(m->getSource(),"http://fedora-fr.pastebin.com ou http://rafb.net/paste/"));
00188       return true;
00189    }
00190 }

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