trad.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 "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 }