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

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