fedorafr.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 }