trad.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 "trad.h"
00030 
00034 Trad::Trad(BotKernel*b)
00035 {
00036         this->author = "eponyme";
00037         this->description = "Provides a command to translate a sentence from a language to an other using translate.google.com";
00038         this->version = VERSION;
00039         this->name = "trad";
00040         this->bindFunction("trad",IN_COMMAND_HANDLER,"trad",0,10);
00041 }
00042 
00043 extern "C"
00044 {
00045         Plugin *contruct_trad(BotKernel*b)
00046         {
00047                 return new Trad(b);
00048         }
00049         void destroy_trad(Plugin*p)
00050         {
00051                 delete p;
00052         }
00053         bool trad (Message*m,Plugin*p,BotKernel*b)
00054         {
00055       string buffer,text,from,to ="";
00056       const string flag = "<div id=result_box dir=\"ltr\">";
00057       string::size_type pos;
00058       Socket sock = Socket();
00059       if ((m->isPublic()) && (m->nbParts() >= 7) )
00060       {
00061          from = Tools::urlencode(m->getPart(4));
00062          to = Tools::urlencode(m->getPart(5));
00063          text = Tools::urlencode(Tools::vectorToString(m->getSplit()," ",6) );
00064          if (!sock.connectSock(80,"translate.google.com","")) {
00065             b->send(IRCProtocol::sendMsg(m->getSource(),"* Unable to connect to translate.google.com *")) ;
00066             return true;
00067          }
00068          sock.sendStr("GET /translate_t?text="+text+"&langpair="+from+"%7C"+to+"&hl=fr HTTP/1.1\nHost: translate.google.com\n\n");
00069          while (buffer.find("</html>")==string::npos) {
00070             buffer += sock.receive() ;
00071          }
00072          pos = buffer.find(flag);
00073          if ( pos == string::npos) {
00074                  b->send(IRCProtocol::sendMsg(m->getSource(),"* Error while parsing result *")) ;
00075             return true;
00076          }
00077          buffer = buffer.substr(pos,buffer.length()-pos);
00078          pos = buffer.find("</div>");
00079          if ( pos == string::npos) {
00080                  b->send(IRCProtocol::sendMsg(m->getSource(),"* Error while parsing result *")) ;
00081             return true;
00082          }
00083          buffer = buffer.substr(flag.length(),pos-flag.length());
00084          b->send(IRCProtocol::sendMsg(m->getSource(),"Translation : "+Tools::cleanHTML(buffer))) ;
00085       }
00086                 return true;
00087         }
00088 }

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