bashfr.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 "bashfr.h"
00030
00034 Bashfr::Bashfr(BotKernel*b)
00035 {
00036 this->author = "eponyme";
00037 this->description = "Display quotes from bashfr.org";
00038 this->version = VERSION;
00039 this->name = "bashfr";
00040 this->bindFunction("bashfr",IN_COMMAND_HANDLER,"bashfr",0,10);
00041 }
00042
00043 extern "C"
00044 {
00045 Plugin *contruct_bashfr(BotKernel*b)
00046 {
00047 return new Bashfr(b);
00048 }
00049 void destroy_bashfr(Plugin*p)
00050 {
00051 delete p;
00052 }
00053 bool bashfr(Message*m,Plugin*p,BotKernel*b)
00054 {
00055 string QUOTE_BEGIN = "<div class=\"quote1\">";
00056 string QUOTE_TEXT_BEGIN = "</span><br />";
00057 string QUOTE_TEXT_END = "</div>";
00058 string ADSENSE = "</div><div class=\"adsense\">";
00059 string buffer;
00060 string::size_type pos ;
00061 Socket sock = Socket();
00062 string query;
00063 if (m->isPublic())
00064 {
00065 if (!sock.connectSock(80,"www.bashfr.org","")) {
00066 b->send(IRCProtocol::sendMsg(m->getSource(),"* Unable to connect to www.bashfr.org *"));
00067 return true;
00068 }
00069 if (m->nbParts() == 5) {
00070 query = "GET /?"+m->getPart(4)+" HTTP/1.1\nHost: www.bashfr.org\n\n";
00071 }
00072 else if (m->nbParts() == 4 ) {
00073 query = "GET /?sort=random HTTP/1.1\nHost: www.bashfr.org\n\n" ;
00074 }
00075 else {
00076 return true;
00077 }
00078 sock.sendStr(query) ;
00079 while ((buffer.find(ADSENSE)==string::npos)&&(buffer.find("</body>")==string::npos)) {
00080 buffer += sock.getOldestMessage();
00081 }
00082 pos = buffer.find(QUOTE_BEGIN);
00083 if ( pos == string::npos) {
00084 b->send(IRCProtocol::sendMsg(m->getSource(),"* Quote non trouvee *"));
00085 return true;
00086 }
00087 buffer = buffer.substr(pos+QUOTE_BEGIN.length()) ;
00088 pos = buffer.find(QUOTE_TEXT_BEGIN);
00089 if ( pos == string::npos) {
00090 b->send(IRCProtocol::sendMsg(m->getSource(),"* Parse error *"));
00091 return true;
00092 }
00093 buffer = buffer.substr(pos+QUOTE_TEXT_BEGIN.length()) ;
00094 pos = buffer.find(QUOTE_TEXT_END);
00095 if ( pos == string::npos) {
00096 b->send(IRCProtocol::sendMsg(m->getSource(),"* Parse error *"));
00097 return true;
00098 }
00099 buffer = buffer.substr(0,pos) ;
00100 vector<string> lines = Tools::stringToVector(buffer,"<br />\r\n",0);
00101 if ( lines.size() <= Tools::strToUnsignedInt(b->getCONFF()->getValue(p->getName()+".maxlines")) ) {
00102 for (unsigned int i = 0 ; i < lines.size() ; i ++ ) {
00103 b->send(IRCProtocol::sendMsg(m->getSource(),Tools::clearAccents(Tools::cleanHTML(lines[i]))));
00104 }
00105 }
00106 else {
00107 b->send(IRCProtocol::sendMsg(m->getSource(),"* Too many lines. Sorry...*"));
00108 }
00109 }
00110 return true;
00111 }
00112 }