ipconverting.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 "ipconverting.h"
00030
00034 IpConverting::IpConverting(BotKernel*b)
00035 {
00036 this->author = "eponyme";
00037 this->description = "host <=> IP converting";
00038 this->version = VERSION;
00039 this->name = "ipconverting";
00040 this->bindFunction("host2ip",IN_COMMAND_HANDLER,"host2ip",0,10);
00041 this->bindFunction("ip2host",IN_COMMAND_HANDLER,"ip2host",0,10);
00042 }
00043
00044 extern "C"
00045 {
00046 Plugin *contruct_ipconverting(BotKernel*b)
00047 {
00048 return new IpConverting(b);
00049 }
00050 void destroy_ipconverting(Plugin*p)
00051 {
00052 delete p;
00053 }
00054 bool host2ip (Message*m,Plugin*p,BotKernel*b)
00055 {
00056 int i = 0 ;
00057 vector<string> msgs;
00058 if ( m->isPublic() ) {
00059 if ( m->getSplit().size() == 5 ) {
00060 hostent * h = NULL;
00061 string hostname = m->getPart(4);
00062 h = gethostbyname(hostname.c_str());
00063 if(h!=NULL) {
00064 while ( h->h_addr_list[i] != NULL ) {
00065 msgs.push_back("* Host:"+hostname+" IP:"+string(inet_ntoa(*(reinterpret_cast<in_addr*>(h->h_addr_list[i]))))+" *");
00066 i ++ ;
00067 }
00068 }
00069 else {
00070 msgs.push_back("* Could not resolve host *");
00071 }
00072 b->send(IRCProtocol::sendMsg(m->getSource(),msgs)) ;
00073 }
00074 }
00075 return true;
00076 }
00077 bool ip2host (Message*m,Plugin*p,BotKernel*b)
00078 {
00079 int i = 0 ;
00080 vector<string> msgs;
00081 if ( m->isPublic() ) {
00082 if ( m->getSplit().size() == 5 ) {
00083 hostent *h = NULL;
00084 string ip = m->getPart(4);
00085 unsigned int addr;
00086 addr = inet_addr(ip.c_str());
00087 h = gethostbyaddr((char*)(&addr), 4, AF_INET);
00088 if(h!=NULL) {
00089 msgs.push_back("* Ip:"+ip+" Host:"+string(h->h_name)+" *");
00090 while(h->h_aliases[i] != NULL) {
00091 msgs.push_back("* Ip:"+ip+" Host:"+string(h->h_aliases[i])+" *");
00092 i++;
00093 }
00094 }
00095 else {
00096 msgs.push_back("* Could not resolve address *");
00097 }
00098 b->send(IRCProtocol::sendMsg(m->getSource(),msgs)) ;
00099 }
00100 }
00101 return true;
00102 }
00103 }