immibis

IRC coffee bot written when I was 12

Oct 22nd, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.40 KB | None | 0 0
  1. IRC.H BEGINS HERE
  2.  
  3. #ifndef _IRC_H_
  4. #define _IRC_H_
  5.  
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <winsock2.h>
  9. #include <windows.h>
  10. #include <ws2tcpip.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13.  
  14. class IrcCantConnectException
  15. {
  16.     char message[1024];
  17. public:
  18.     IrcCantConnectException(const char *hostname)
  19.     {
  20.         strcpy(message,"IRC failed to connect to ");
  21.         strcat(message,hostname);
  22.     }
  23.     const char *getMessage() {return message;}
  24. };
  25.  
  26. class IrcConnection
  27. {
  28.     SOCKET sock;
  29.     char recvbuf[4096];
  30.     char recvbuf2[4096];
  31.     char sendbuf[4096];
  32.     int receiving;
  33.     int receiving2;
  34. protected:
  35.     bool operational;
  36.     char mynick[256];
  37.     const char *server_;
  38.     const char *user_;
  39.     const char *nick_;
  40.     const char *password_;
  41.     const char *channel_;
  42. public:
  43.     IrcConnection() {sock=NULL;*recvbuf='\0';receiving=0;}
  44.     void IrcConnectTo(const char *server,const char *user,const char *nick,const char *password,const char *channel)
  45.     {
  46.         int reply;
  47.         printf("\nNICK is %s\n\n",nick);
  48.         hostent *h=gethostbyname(server);
  49.         int addrlen=sizeof(sockaddr_in);
  50.         sockaddr_in addr;
  51.         sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  52.         if(sock==INVALID_SOCKET) throw "Failed to create socket";
  53.         if(!h) throw "gethostbyname failed";
  54.         //WSAStringToAddress(*h->h_addr_list,AF_INET,NULL,(sockaddr*)&addr,&addrlen);
  55.         addr.sin_addr.S_un.S_addr=*(unsigned long*)*h->h_addr_list;
  56.         addr.sin_family=AF_INET;
  57.         addr.sin_port=htons(IRCPORT);
  58.         if(connect(sock,(sockaddr*)&addr,sizeof addr)==SOCKET_ERROR)
  59.             throw "Failed to connect to server";
  60.         operational=true;
  61.         if(nick)
  62.         {
  63.             sprintf(recvbuf,"NICK %s",nick);
  64.             Send(recvbuf);
  65.             reply=GetReply();
  66.             strcpy(mynick,nick);
  67.         }
  68.         if(!user) user=nick;
  69.         {
  70.             sprintf(recvbuf,"USER %s 127.0.0.1 %s :IrcTelnetBot",user,server);
  71.             Send(recvbuf);
  72.             reply=GetReply();
  73.         }
  74.         if(password)
  75.         {
  76.             sprintf(recvbuf,"PRIVMSG NickServ :IDENTIFY %s",password);
  77.             Send(recvbuf);
  78.             Recv(false);
  79.             reply=GetReply();
  80.         }
  81.         if(channel)
  82.         {
  83.             while(reply!=375) reply=GetReply();
  84.             sprintf(recvbuf,"JOIN %s",channel);
  85.             Send(recvbuf);
  86.             reply=GetReply();
  87.         }
  88.         Send("PRIVMSG NickServ :SET UNFILTERED ON");
  89.         receiving=0;
  90.         Recv(false);
  91.         channel_=channel;
  92.         nick_=nick;
  93.         user_=user;
  94.         password_=password;
  95.         server_=server;
  96.     }
  97.     void Send(const char *message,SOCKET s=NULL)
  98.     {
  99.         int result;
  100.         if(s==NULL) s=sock;
  101.         if(s==sock && !operational) throw "Send on unoperational connection";
  102.         if(s)
  103.         {
  104.             sprintf(sendbuf,"%s\n",message);
  105.             if(s==sock)
  106.             {
  107.                 if((result=send(s,sendbuf,strlen(sendbuf),0))==SOCKET_ERROR || result==0)
  108.                 {
  109.                     sock=NULL;
  110.                     receiving=0;
  111.                     *recvbuf=*sendbuf='\0';
  112.                     printf("%s was disconnected: %s\n",mynick,message);
  113.                     fflush(stdout);
  114.                 }
  115.                 else
  116.                     printf("%s: %s\n",mynick,message),fflush(stdout);
  117.             }
  118.             else
  119.             {
  120.                 send(s,sendbuf,strlen(sendbuf),0);
  121.                 printf("(2) %s: %s\n",mynick,message),fflush(stdout);
  122.             }
  123.         }
  124.         else
  125.         {
  126.             operational=false;
  127.             throw "Send without a connection";
  128.         }
  129.     }
  130.     int GetReply()
  131.     {
  132. begin:
  133.         char *str=(char*)Recv(true);
  134.         char *pStr;
  135.         if(str && *str==':') str=strchr(str,' ')+1;
  136.         if(str==NULL) return -1;
  137.         for(pStr=str;*pStr;++pStr)
  138.             *pStr=tolower(*pStr);
  139.         if(strstr(str,"ping ")==str && *mynick)
  140.         {
  141.             str+=5;
  142.             if(!*str || !*(str-1))
  143.             {
  144.                 sprintf(sendbuf,"PONG %s",mynick);
  145.                 Send(sendbuf);
  146.             }
  147.             else
  148.             {
  149.                 //sprintf(sendbuf,"PONG %s %s",mynick,str);
  150.                 sprintf(sendbuf, "PONG %s",str);
  151.                 Send(sendbuf);
  152.             }
  153.             goto begin;
  154.         }
  155.         pStr=strchr(str,' ');
  156.         if(pStr && strstr(pStr,"kick ")==pStr && strstr(pStr+5,mynick)==pStr+5)
  157.         {
  158.             closesocket(sock);
  159.             sock=NULL;
  160.             IrcConnectTo(server_,user_,nick_,password_,channel_);
  161.             return 0;
  162.         }
  163.         if(*str<'0' || *str>'9')
  164.             return -1;
  165.         return atoi(str);
  166.     }
  167.     const char *Recv(bool wait,SOCKET s=NULL)
  168.     {
  169.         if(s==NULL) s=sock;
  170.         if(s==sock && !operational) return NULL;
  171.         if(!s) return NULL;
  172.         if(!wait)
  173.         {
  174.             fd_set fds;
  175.             timeval tv={0,0};
  176.             FD_ZERO(&fds);
  177.             FD_SET(s,&fds);
  178.             select(s+1,&fds,NULL,NULL,&tv);
  179.             if(!FD_ISSET(s,&fds))
  180.                 return NULL;
  181.         }
  182.         char ch;
  183.         char *buf=(s==sock?recvbuf:recvbuf2);
  184.         int result;
  185.         int *receivingvar=(s==sock?&receiving:&receiving2);
  186.         while(true) // Not really
  187.         {
  188.             if(s!=sock)
  189.             {
  190.                 fd_set fds;
  191.                 timeval tv={0,0};
  192.                 FD_ZERO(&fds);
  193.                 FD_SET(s,&fds);
  194.                 select(s+1,&fds,NULL,NULL,&tv);
  195.                 if(!FD_ISSET(s,&fds))
  196.                 {
  197.                     buf[(*receivingvar)++]='\0';
  198.                     break;
  199.                 }
  200.             }
  201.             if((result=recv(s,&ch,1,0))==SOCKET_ERROR || result==0)
  202.             {
  203.                 if(s==sock)
  204.                 {
  205.                     Send("QUIT :Socket error");
  206.                     sock=NULL;
  207.                 }
  208.                 return NULL;
  209.             }
  210.             if(s!=sock && ch=='\x1B')
  211.             {
  212.                 while(ch!='m' && ch!='\r' && ch!='\n')
  213.                 {
  214.                     if((result=recv(s,&ch,1,0))==SOCKET_ERROR || result==0)
  215.                     {
  216.                         if(s==sock)
  217.                         {
  218.                             Send("QUIT :Socket error");
  219.                             sock=NULL;
  220.                         }
  221.                         return NULL;
  222.                     }
  223.                 }
  224.                 if(ch=='m')
  225.                 {
  226.                     if((result=recv(s,&ch,1,0))==SOCKET_ERROR || result==0)
  227.                     {
  228.                         if(s==sock)
  229.                         {
  230.                             Send("QUIT :Socket error");
  231.                             sock=NULL;
  232.                         }
  233.                         return NULL;
  234.                     }
  235.                 }
  236.             }
  237.             if(ch=='\r' || ch=='\0')
  238.                 ;
  239.             else if(ch=='\n')
  240.             {
  241.                 buf[(*receivingvar)++]='\0';
  242.                 break;
  243.             }
  244.             else
  245.                 buf[(*receivingvar)++]=ch;
  246.         }
  247.         *receivingvar=0;
  248.         if(!*buf)
  249.             return NULL;
  250.         printf("(R%s) %s: %s\n",(s==sock?"":"2"),mynick,buf);
  251.         fflush(stdout);
  252.         return buf;
  253.     }
  254.     ~IrcConnection()
  255.     {
  256.         if(sock)
  257.         {
  258.             Send("QUIT :IRC bot shutting down");
  259.             closesocket(sock);
  260.             sock=NULL;
  261.         }
  262.     }
  263.     bool IsConnected() {return (sock!=NULL);}
  264.     SOCKET GetSocket() {return sock;}
  265. };
  266.  
  267. class IrcUserBot : public IrcConnection
  268. {
  269.     char nick[64];
  270.     char password[64];
  271.     char sendbuf[4096];
  272.     char recvbuf[4096];
  273.     SOCKET connected_to;
  274.     char bound_to[256];
  275.     static int cur_number;
  276.     int nSentMessage;
  277.     bool being_stupid;
  278. public:
  279.     IrcUserBot() {*nick=*password=*bound_to=*sendbuf=*recvbuf='\0';connected_to=NULL;nSentMessage=0;being_stupid=false;}
  280.     void Construct()
  281.     {
  282.         *sendbuf=*recvbuf=*bound_to='\0';connected_to=NULL;
  283.         if(cur_number>=NNICKS)
  284.         {
  285.             sprintf(nick,"%s%i",BOTPREFIX,cur_number);
  286.             sprintf(password,"%s%i",PASSPREFIX,cur_number);
  287.             IrcConnectTo(SERVER,nick,nick,password,CHANNEL);
  288.         }
  289.         else
  290.         {
  291.             strcpy(nick,Nicks[cur_number]);
  292.             sprintf(password,"%s%i",PASSPREFIX,cur_number);
  293.             IrcConnectTo(SERVER,nick,nick,password,CHANNEL);
  294.         }
  295.         ++cur_number;
  296.         nSentMessage=0;
  297.     }
  298.     void DebugMessage(const char *message)
  299.     {
  300.         sprintf(sendbuf,"PRIVMSG %s :%s",CHANNEL,message);
  301.         Send(sendbuf);
  302.     }
  303.     void CheckBot()
  304.     {
  305.         char *str; // str is the complete message
  306.         char *pStr; // pStr is the message not including the source.
  307.         char *pUserFrom;
  308.         char *pUserTo;
  309.         char *pMessage;
  310.         if(!IsConnected()) IrcConnectTo(SERVER,nick,nick,password,CHANNEL);
  311.         if(!(str=(char*)Recv(false)))
  312.             return;
  313.         pStr=strchr(str,' ');
  314.         if(pStr && strstr(pStr,"404 ")==pStr)
  315.         {
  316.             closesocket(GetSocket());
  317.             if(cur_number>=NNICKS)
  318.             {
  319.                 sprintf(nick,"%s%i",BOTPREFIX,cur_number);
  320.                 sprintf(password,"%s%i",PASSPREFIX,cur_number);
  321.                 IrcConnectTo(SERVER,nick,nick,password,CHANNEL);
  322.             }
  323.             else
  324.             {
  325.                 strcpy(nick,Nicks[cur_number]);
  326.                 sprintf(password,"pass%i",PASSPREFIX,cur_number);
  327.                 IrcConnectTo(SERVER,nick,nick,password,CHANNEL);
  328.             }
  329.             cur_number++;
  330.             return;
  331.         }
  332.         {
  333.             char *p;
  334.             for(p=pStr;*p && *p!=' ';++p)
  335.                 *p=tolower(*p);
  336.         }
  337.         if(pStr && strstr(pStr,"kick ")==pStr && strstr(pStr+5,mynick)==pStr+5)
  338.         {
  339.             closesocket(GetSocket());
  340.             IrcConnectTo(server_,user_,nick_,password_,channel_);
  341.             return;
  342.         }
  343.         if((pStr && strstr(pStr,"ping")==pStr) || strstr(str,"ping")==str)
  344.         {
  345.             if(!pStr)
  346.             {
  347.                 sprintf(sendbuf,"PONG %s",nick);
  348.                 Send(sendbuf);
  349.                 return;
  350.             }
  351.             pUserFrom=pStr+1;
  352.             if(strchr(pUserFrom,' '))
  353.                 *strchr(pUserFrom,' ')='\0';
  354.             sprintf(sendbuf,"PONG %s %s",nick,pUserFrom);
  355.             Send(sendbuf);
  356.             return;
  357.         }
  358.         pStr=strchr(str,' ');
  359.         {
  360.             char *p;
  361.             for(p=pStr+1;*p && *p!=' ';++p)
  362.                 *p=tolower(*p);
  363.         }
  364.         if(!pStr || (strstr(pStr+1,"privmsg")!=pStr+1)) return;
  365.         char *pCommand=pStr+1;
  366.         ++pStr;
  367.         if(*str!=':') return;
  368.         pUserFrom=str+1;
  369.         *pStr='\0';
  370.         pUserTo=strchr(pStr+1,' ')+1;
  371.         if(pUserTo-1==NULL) return;
  372.         pStr=strchr(pUserTo,' ');
  373.         if(!pStr) return;
  374.         else (*pStr='\0'),(pMessage=pStr+1);
  375.         if(*pMessage==':') ++pMessage;
  376.         pStr=strchr(pUserFrom,'!');
  377.         if(pStr) *pStr='\0';
  378.  
  379.         if((pMessage[1]=='j' || pMessage[1]=='J'
  380.             || pMessage[1]=='p' || pMessage[1]=='P' || pMessage[1]=='?') && *pMessage==CMDPREFIX && pMessage[2]==' ')
  381.         {
  382.             if(strcmp(pUserFrom,BOTMASTER))
  383.             {
  384.                 sprintf(sendbuf,"PRIVMSG %s :Only %s can make me join or part channels or be stupid",(*pUserTo=='#'?pUserTo:pUserFrom),BOTMASTER);
  385.                 Send(sendbuf);
  386.             }
  387.             else if(pMessage[1]=='?')
  388.             {
  389.                 being_stupid=!being_stupid;
  390.                 sprintf(sendbuf,"PRIVMSG %s :being_stupid is now %s",BOTMASTER,(being_stupid?"true":"false"));
  391.                 Send(sendbuf);
  392.             }
  393.             else if(pMessage[1]=='j' || pMessage[1]=='J')
  394.             {
  395.                 sprintf(sendbuf,"JOIN %s",pMessage+3);
  396.                 Send(sendbuf);
  397.             }
  398.             else //it must be p or P
  399.             {
  400.                 sprintf(sendbuf,"PART %s",pMessage+3);
  401.                 Send(sendbuf);
  402.             }
  403.         }
  404.         else if((pMessage[1]=='R' || pMessage[1]=='r') && *pMessage==CMDPREFIX && pMessage[2]==' ')
  405.         {
  406.             if(strcmp(pUserFrom,BOTMASTER))
  407.             {
  408.                 sprintf(sendbuf,"PRIVMSG %s :Only %s can use !r",(*pUserTo=='#'?pUserTo:pUserFrom),BOTMASTER);
  409.                 Send(sendbuf);
  410.             }
  411.             else
  412.             {
  413.                 Send(pMessage+3);
  414.             }
  415.         }
  416.         else if((pMessage[1]=='m' || pMessage[1]=='M') && pMessage[0]==CMDPREFIX && pMessage[2]==' ')
  417.         {
  418.             char *pSpace=strchr(pMessage+3,' ');
  419.             if(pSpace==NULL)
  420.                 return;
  421.             pMessage+=3;
  422.             *pSpace++='\0';
  423.             if(*pMessage=='#' && strcmp(pMessage,CHANNEL))
  424.             {
  425.                 sprintf(sendbuf,"JOIN %s",pMessage);
  426.                 Send(sendbuf);
  427.             }
  428.             if(!strcmpi(pMessage,mynick))
  429.             {
  430.                 sprintf(sendbuf,"PRIVMSG %s :%s has attempted to crash me. I will now either crash or be kicked off for excess flood. LOL.",CHANNEL,pUserFrom);
  431.                 Send(sendbuf);
  432.             }
  433.             sprintf(sendbuf,"PRIVMSG %s :%s",pMessage,pSpace);
  434.             Send(sendbuf);
  435.             if(*pMessage=='#' && strcmp(pMessage,CHANNEL))
  436.             {
  437.                 sprintf(sendbuf,"PART %s",pMessage);
  438.                 Send(sendbuf);
  439.             }
  440.         }
  441.         else if((pMessage[1]=='t' || pMessage[1]=='T') && pMessage[0]==CMDPREFIX && pMessage[2]==' ')
  442.         {
  443.             char *pSpace=strchr(pMessage+3,' ');
  444.             if(pSpace==NULL)
  445.                 return;
  446.             pMessage+=3;
  447.             *pSpace++='\0';
  448.             if(*pMessage=='#' && strcmp(pMessage,CHANNEL))
  449.             {
  450.                 sprintf(sendbuf,"JOIN %s",pMessage);
  451.                 Send(sendbuf);
  452.             }
  453.             if(strcmpi(pMessage,mynick))
  454.             {
  455.                 sprintf(sendbuf,"PRIVMSG %s :%s",pMessage,pSpace);
  456.                 Send(sendbuf);
  457.             }
  458.         }
  459.         else if(*pMessage=='\001')
  460.         {
  461.             if(pMessage[1]=='P' && pMessage[2]=='I' && pMessage[3]=='N' && pMessage[4]=='G')
  462.             {
  463.                 sprintf(sendbuf,"PRIVMSG %s :\001PING 1123\001",pUserFrom);
  464.                 Send(sendbuf);
  465.                 sprintf(sendbuf,"PRIVMSG %s :\001VERSION\001",pUserFrom);
  466.                 Send(sendbuf);
  467.                 sprintf(sendbuf,"NOTICE %s :%s",pUserFrom,pMessage);
  468.                 Send(sendbuf);
  469.             }
  470.             else if(pMessage[1]=='V' && pMessage[2]=='E' && pMessage[3]=='R' && pMessage[4]=='S')
  471.             {
  472.                 sprintf(sendbuf,"NOTICE %s :\001VERSION None of your business!\001",pUserFrom);
  473.                 Send(sendbuf);
  474.             }
  475.         }
  476.         else if(pMessage[1]==CMDPREFIX && pMessage[0]==CMDPREFIX && pMessage[2]==' ')
  477.         {
  478.             if(!strcmpi(pMessage+3,mynick))
  479.             {
  480.                 sprintf(sendbuf,"PRIVMSG %s :%s has attempted to crash me. I will now either crash or be kicked off for excess flood. LOL.",CHANNEL,pUserFrom);
  481.                 Send(sendbuf);
  482.                 sprintf(sendbuf,"PRIVMSG %s :Sending endless string of pings to myself...",CHANNEL);
  483.                 Send(sendbuf);
  484.                 sprintf(sendbuf,"PRIVMSG %s :\001PING 1123\001",mynick);
  485.                 Send(sendbuf);
  486.             }
  487.             else
  488.             {
  489.                 sprintf(sendbuf,"PRIVMSG %s :%s",pMessage+3,pMessage);
  490.                 Send(sendbuf);
  491.             }
  492.         }
  493.         else if(isdigit(pMessage[1]) && pMessage[0]==CMDPREFIX && pMessage[2]==' ')
  494.         {
  495.             if(*(pMessage+3))
  496.             {
  497.                 pUserTo=pMessage+3;
  498.                 if(strcmp(pUserTo,CHANNEL))
  499.                 {
  500.                     sprintf(sendbuf,"JOIN %s",pUserTo);
  501.                     Send(sendbuf);
  502.                 }
  503.             }
  504.             Send("NICK SpammingCoffeeBot");
  505.             for(int k=0;k<pMessage[1]-'0';k++)
  506.             {
  507.                 try
  508.                 {
  509.                     if(GetSocket()==NULL)
  510.                         break;
  511.                     sprintf(sendbuf,"PRIVMSG %s :spamming. Buy my iPod!",pUserTo);
  512.                     Send(sendbuf);
  513.                     sprintf(sendbuf,"PRIVMSG %s :spamming. Buy my iPhone!",pUserTo);
  514.                     Send(sendbuf);
  515.                     sprintf(sendbuf,"PRIVMSG %s :spamming. Get the latest offer on all chocolate bar accessories!",pUserTo);
  516.                     Send(sendbuf);
  517.                     sprintf(sendbuf,"PRIVMSG %s :spamming. All the latest travel insurance stuff and nonsense!",pUserTo);
  518.                     Send(sendbuf);
  519.                     sprintf(sendbuf,"PRIVMSG %s :spamming. Go to #arianne!!",pUserTo);
  520.                     Send(sendbuf);
  521.                 } catch(...) {break;}
  522.             }
  523.             sprintf(sendbuf,"NICK %s",mynick);
  524.             Send(sendbuf);
  525.             if(*(pMessage+3) && strcmp(pUserTo,CHANNEL))
  526.             {
  527.                 sprintf(sendbuf,"JOIN %s",pUserTo);
  528.                 Send(sendbuf);
  529.             }
  530.         }
  531.         else if((pMessage[1]=='c' || pMessage[1]=='C') && pMessage[0]==CMDPREFIX && pMessage[2]==' ')
  532.         {
  533.             if(*pUserTo!='#')
  534.                 pUserTo=pUserFrom;
  535.  
  536.             char *tok=strtok(pMessage," ");
  537.             int sugars=0;
  538.             int milk=1;
  539.             int type=0;
  540.             int size=4;
  541.             char *drink="coffee";
  542.             bool magnetic=false;
  543.             bool decaf=false;
  544.             char *other_milk="";
  545.             char *other_size="";
  546.             char *target=pUserFrom;
  547.             char message[1024]="";
  548. #define MILK_COLD 1
  549. #define MILK_GOAT 5
  550. #define MILK_HOT 3
  551. #define MILK_NONE 0
  552. #define MILK_FROTHED 2
  553. #define MILK_OTHER -1
  554. #define TYPE_NORMAL 0
  555. #define TYPE_ESPRESSO 1
  556. #define TYPE_TURKISH 2
  557. #define TYPE_ICED 3
  558.             tok=strtok(NULL," ");
  559.             while(tok)
  560.             {
  561.                 if(*tok!='-')
  562.                 {
  563.                     strcat(message," ");
  564.                     strcat(message,tok);
  565.                     tok=strtok(NULL," ");
  566.                     continue;
  567.                 }
  568.                 tok++;
  569.                 switch(*tok)
  570.                 {
  571.                 case 'e':type=TYPE_ESPRESSO;break;
  572.                 case 'T':type=TYPE_TURKISH;break;
  573.                 case 'i':type=TYPE_ICED;break;
  574.                 case 'm':
  575.                     {
  576.                         char *p;
  577.                         for(p=tok;*p;++p)
  578.                             *p=tolower(*p);
  579.                     }
  580.                     tok++;
  581.                     if(!strcmp(tok,"hot"))
  582.                         milk=MILK_HOT;
  583.                     else if(!strcmp(tok,"cold"))
  584.                         milk=MILK_COLD;
  585.                     else if(!strcmp(tok,"none"))
  586.                         milk=MILK_NONE;
  587.                     else if(!strcmp(tok,"frth"))
  588.                         milk=MILK_FROTHED;
  589.                     else if(!strcmp(tok,"goat"))
  590.                         milk=MILK_GOAT;
  591.                     else if(!strcmp(tok,"agnet") || !strcmp(tok,"agnetic") || !strcmp(tok,"chocolate"))
  592.                         magnetic=true;
  593.                     else
  594.                     {
  595.                         milk=MILK_OTHER;
  596.                         other_milk=tok;
  597.                         for(char *pStr=other_milk;*pStr;pStr++)
  598.                             if(*pStr=='_') *pStr=' ';
  599.                     }
  600.                     break;
  601.                 case 's':
  602.                     tok++;
  603.                     sugars=atoi(tok);
  604.                     break;
  605.                 case 'M':
  606.                     drink="message";
  607.                     size=-1;
  608.                     other_size="bottle";
  609.                     milk=MILK_NONE;
  610.                     break;
  611.                 case 'z':
  612.                 case 'S':
  613.                     tok++;
  614.                     if(*tok<'0' || *tok>'9')
  615.                     {
  616.                         size=-1;
  617.                         other_size=tok;
  618.                         for(char *pStr=other_size;*pStr;pStr++)
  619.                             if(*pStr=='_') *pStr=' ';
  620.                     }
  621.                     else
  622.                         size=atoi(tok);
  623.                     break;
  624.                 case '-':
  625.                     tok++;
  626.                     if(strstr(tok,"other=")==tok)
  627.                     {
  628.                         tok+=6;
  629.                         drink=tok;
  630.                         for(char *pStr=drink;*pStr;pStr++)
  631.                             if(*pStr=='_') *pStr=' ';
  632.                     }
  633.                     else if(strstr(tok,"target=")==tok)
  634.                     {
  635.                         tok+=7;
  636.                         target=tok;
  637.                     }
  638.                     else if(!strcmp(tok,"help"))
  639.                     {
  640.                         sprintf(sendbuf,"PRIVMSG %s :Usage: !c [--target={NICK|CHANNEL}] [-T] [-d] [-e] [--other=DRINKTYPE] [-mMILKTYPE] [-sNUMBER_OF_SUGARS] [-zSIZE]",pUserTo);Send(sendbuf);
  641.                         sprintf(sendbuf,"PRIVMSG %s :-T: Turkish coffee    -d: Decaf coffee      -e: Espresso coffee",pUserTo);Send(sendbuf);
  642.                         sprintf(sendbuf,"PRIVMSG %s :--other=DRINKTYPE: Make a non-coffee drink",pUserTo);Send(sendbuf);
  643.                         sprintf(sendbuf,"PRIVMSG %s :-mMILKTYPE  milktype can be none, hot, cold, frth, agnet, agnetic, or chocolate or a user defined string",pUserTo);Send(sendbuf);
  644.                         sprintf(sendbuf,"PRIVMSG %s :-zSIZE size can be 1, 2, 3, 4, 5, 6, or a user defined string",pUserTo);Send(sendbuf);
  645.                         sprintf(sendbuf,"PRIVMSG %s :--target={NICK|CHANNEL}   Give the coffee to the specified nick/channel",pUserTo);Send(sendbuf);
  646.                         sprintf(sendbuf,"PRIVMSG %s :-sNUMBER_OF_SUGARS   Give a negative value for an infinite number",pUserTo);Send(sendbuf);
  647.                         Sleep(5000);
  648.                         sprintf(sendbuf,"PRIVMSG %s :Usage: !t NICK|CHANNEL MESSAGE",pUserTo);Send(sendbuf);
  649.                         sprintf(sendbuf,"PRIVMSG %s :Send the specified MESSAGE to the specified NICK or CHANNEL (if a channel, the bot must be in that channel)",pUserTo);Send(sendbuf);
  650.                         return;
  651.                     }
  652.                     break;
  653.                 case 'd':decaf=true;break;
  654.                 }
  655.                 tok=strtok(NULL," ");
  656.             }
  657.             char coffeebuf[2048];
  658.             sprintf(coffeebuf,"a%s%s %s",(decaf?" decaf":""),(type==TYPE_ESPRESSO?" espresso":(type==TYPE_TURKISH?" Turkish":(type==TYPE_ICED?" iced":""))),drink);
  659.             char buf2[256];
  660.             if(sugars>0)
  661.                 sprintf(buf2," with %i sugars",sugars);
  662.             else if(sugars<0)
  663.                 strcpy(buf2," with an infinite number of sugars");
  664.             else
  665.                 *buf2='\0';
  666.             strcat(coffeebuf,buf2);
  667.             strcat(coffeebuf," ");
  668.             switch(size)
  669.             {
  670.             case 1:strcpy(buf2,"in a thimble");break;
  671.             case 2:strcpy(buf2,"in an espresso cup");break;
  672.             case 3:strcpy(buf2,"in a cafe-style coffee cup");break;
  673.             case 4:strcpy(buf2,"in an office mug");break;
  674.             case 5:strcpy(buf2,"in a sysadmin mug");break;
  675.             case 6:strcpy(buf2,"in a bucket");break;
  676.             case -1:sprintf(buf2,"in a %s",other_size);break;
  677.             default:sprintf(buf2,"in an container of size %i",size);
  678.             }
  679.             strcat(coffeebuf,buf2);
  680.             switch(milk)
  681.             {
  682.             case MILK_GOAT:strcpy(buf2," with a goat in it");break;
  683.             case MILK_COLD:strcpy(buf2," with cold milk");break;
  684.             case MILK_HOT:strcpy(buf2," with hot milk");break;
  685.             case MILK_NONE:strcpy(buf2," with no milk");break;
  686.             case MILK_FROTHED:strcpy(buf2," with frothed milk");break;
  687.             case MILK_OTHER:sprintf(buf2," with %s milk",other_milk);break;
  688.             default:sprintf(buf2," with a milk value of %i",milk);
  689.             }
  690.             strcat(coffeebuf,buf2);
  691.             if(*message)
  692.             {
  693.                 sprintf(buf2," %s",message);
  694.                 strcat(coffeebuf,buf2);
  695.             }
  696.             if(*target=='#')
  697.             {
  698.                 if(strcmp(target,CHANNEL))
  699.                 {
  700.                     sprintf(sendbuf,"JOIN %s",target);
  701.                     Send(sendbuf);
  702.                 }
  703.                 sprintf(sendbuf,"PRIVMSG %s :\001ACTION is making %s for this channel\001",target,coffeebuf);
  704.                 Send(sendbuf);
  705.             }
  706.             else if(strcmp(pUserFrom,target))
  707.             {
  708.                 sprintf(sendbuf,"PRIVMSG %s :\001ACTION is making %s for you\001",target,coffeebuf);
  709.                 Send(sendbuf);
  710.             }
  711.             sprintf(sendbuf,"PRIVMSG %s :\001ACTION is making %s for %s\001",pUserTo,coffeebuf,target);
  712.             Send(sendbuf);
  713. #ifdef REPORT_TO_CHANNEL
  714.             if(strcmp(pUserTo,CHANNEL))
  715.             {
  716.                 sprintf(sendbuf,"PRIVMSG %s :\001ACTION is making %s for %s\001",CHANNEL,coffeebuf,target);
  717.                 Send(sendbuf);
  718.             }
  719. #endif
  720.             if(milk>0 && milk<6)
  721.                 Sleep(milk*1000);
  722.             if(magnetic)
  723.             {
  724.                 sprintf(sendbuf,"PRIVMSG %s :\001ACTION spills %s's coffee into a Magnetic Laser Device\001",pUserTo,target);
  725.                 Send(sendbuf);
  726.                 if(*target=='#')
  727.                 {
  728.                     sprintf(sendbuf,"PRIVMSG %s :\001ACTION spills the channel's coffee into a Magnetic Laser Device\001",target,coffeebuf);
  729.                     Send(sendbuf);
  730.                 }
  731.                 else if(strcmp(pUserFrom,target))
  732.                 {
  733.                     sprintf(sendbuf,"PRIVMSG %s :\001ACTION spills your coffee into a Magnetic Laser Device\001",target);
  734.                     Send(sendbuf);
  735.                 }
  736. #ifdef REPORT_TO_CHANNEL
  737.                 if(strcmp(pUserTo,CHANNEL))
  738.                 {
  739.                     sprintf(sendbuf,"PRIVMSG %s :\001ACTION spills %s's coffee into a Magnetic Laser Device\001",CHANNEL,target);
  740.                     Send(sendbuf);
  741.                 }
  742. #endif
  743.                 char buffer[2048];
  744.                 strcpy(buffer,coffeebuf+2);
  745.                 sprintf(coffeebuf,"a magnetic %s which is emitting lots of blue light and a barely audible hum",buffer);
  746.             }
  747.             sprintf(sendbuf,"PRIVMSG %s :\001ACTION gives %s %s\001",pUserTo,target,coffeebuf);
  748.             Send(sendbuf);
  749.             if(*target=='#')
  750.             {
  751.                 sprintf(sendbuf,"PRIVMSG %s :\001ACTION gives everyone in this channel %s\001",target,coffeebuf);
  752.                 Send(sendbuf);
  753.                 if(strcmp(target,CHANNEL))
  754.                 {
  755.                     sprintf(sendbuf,"PART %s",target);
  756.                     Send(sendbuf);
  757.                 }
  758.             }
  759.             else if(strcmp(pUserFrom,target))
  760.             {
  761.                 sprintf(sendbuf,"PRIVMSG %s :\001ACTION gives you %s\001",target,coffeebuf);
  762.                 Send(sendbuf);
  763.             }
  764. #ifdef REPORT_TO_CHANNEL
  765.             if(strcmp(pUserTo,CHANNEL))
  766.             {
  767.                 sprintf(sendbuf,"PRIVMSG %s :\001ACTION gives %s %s\001",CHANNEL,target,coffeebuf);
  768.                 Send(sendbuf);
  769.             }
  770. #endif
  771.         }
  772.         else
  773.         {
  774.             if((*pMessage!='!' || pMessage[2]!=' ') && *pMessage!='\001')
  775.             {
  776.                 //if(*pUserTo=='#' && strcmp(pUserTo,CHANNEL))
  777.                 //{
  778.                     //sprintf(sendbuf,"PRIVMSG %s :%s: <%s> %s",CHANNEL,pUserTo,pUserFrom,pMessage);
  779.                     //Send(sendbuf);
  780.                 //}
  781.                 return;
  782.             }
  783.             if(being_stupid)
  784.             {
  785.                 char *target=(*pUserTo=='#'?pUserTo:pUserFrom);
  786.                 sprintf(sendbuf,"PRIVMSG %s :%s",target,pMessage);
  787.             }
  788.         }
  789.     }
  790.     bool IsBound() {return (connected_to!=NULL);}
  791.     ~IrcUserBot()
  792.     {
  793.         if(connected_to)
  794.         {
  795.             sprintf(sendbuf,"QUIT :IRC bot shutting down",CHANNEL);
  796.             Send(sendbuf);
  797.             closesocket(connected_to);
  798.         }
  799.     }
  800. };
  801.  
  802. extern IrcUserBot bots[NBOTS];
  803. extern IrcConnection botmain;
  804.  
  805. #endif
  806.  
  807. IRC.H ENDS HERE
  808. IRC.CPP BEGINS HERE
  809.  
  810. #define SERVER "irc.esper.net"
  811. #define NBOTS 1
  812. #define CHANNEL "#coffeebot-test"
  813. #define BOTPREFIX "Coffee"
  814. #define PASSPREFIX "WaiterPass"
  815. #define CMDPREFIX '%'
  816. #define IRCPORT 6667
  817. //#define MESSAGESUNTILPAUSE 1
  818. //#define PAUSELEN 2000
  819. #define REPORT_TO_CHANNEL
  820. #define NICKFORMAT "%s%x"7
  821. #define BOTMAIN "immibis"
  822. #define BOTMASTER BOTMAIN
  823. #define NNICKS 1
  824.  
  825. const char *const Nicks[]={
  826.     "CoffeeBot"
  827. };
  828.  
  829. #include <process.h>
  830. #include "irc.h"
  831.  
  832. int IrcUserBot::cur_number=0;
  833. IrcUserBot bots[NBOTS];
  834. IrcConnection botmain;
  835. bool online[NBOTS];
  836. bool is_online=false;
  837.  
  838. void run_botcheck(void *bot)
  839. {
  840.     IrcUserBot *botptr=(IrcUserBot*)bot;
  841.     int id=botptr-bots;
  842.     int nBots=0;
  843.     int k;
  844.     for(k=0;k<NBOTS;k++)
  845.         if(online[k])
  846.             nBots++;
  847.     while(true)
  848.     {
  849.         try
  850.         {
  851.             botptr->CheckBot();
  852.             Sleep(nBots*2);
  853.         }
  854.         catch(char *message)
  855.         {
  856.             fprintf(stderr,"%i: %s\n",id,message);
  857.         }
  858.     }
  859. }
  860.  
  861. void main()
  862. {
  863.     //freopen("bots.log","w",stdout);
  864.  
  865.     int num=0;
  866.     IrcUserBot *bot=bots;
  867.     WSADATA wsa;
  868.     SetPriorityClass(GetCurrentProcess(),IDLE_PRIORITY_CLASS);
  869.     if(WSAStartup(MAKEWORD(2,0),&wsa)==SOCKET_ERROR)
  870.     {
  871.         fprintf(stderr,"Failed to call WSAStartup for version 2.0\n");
  872.         return;
  873.     }
  874.     printf("WSA initialized\n");
  875.     printf("iMaxSockets: %i\n",wsa.iMaxSockets);
  876.     printf("iMaxUdpDg: %i\n",wsa.iMaxUdpDg);
  877.     printf("lpVendorInfo: %p\n",wsa.lpVendorInfo);
  878.     printf("szDescription: %s\n",wsa.szDescription);
  879.     printf("szSystemStatus: %s\n",wsa.szSystemStatus);
  880.     printf("wHighVersion: %i.%i\n",LOBYTE(wsa.wHighVersion),HIBYTE(wsa.wHighVersion));
  881.     printf("wVersion: %i.%i\n",LOBYTE(wsa.wVersion),HIBYTE(wsa.wVersion));
  882.     printf("\n");
  883.     for(num=0;num<NBOTS;num++)
  884.     {
  885.         printf("Initialising bot %i...",num);
  886.         try
  887.         {
  888.             bots[num].Construct();
  889.             online[num]=true;
  890.             is_online=true;
  891.             printf("Complete\n");
  892.         }
  893.         catch(char *message)
  894.         {
  895.             printf("Failed: %s\n",message);
  896.             online[num]=false;
  897.         }
  898.     }
  899.     for(num=0;num<NBOTS;num++)
  900.         if(online[num])
  901.             _beginthread(run_botcheck,0,(void*)(bots+num));
  902.     if(!is_online)
  903.     {
  904.         fprintf(stderr,"\nFatal error: no bots are available.\n");
  905.         return;
  906.     }
  907.     num=0;
  908.     printf("\n");
  909.     while(true) {Sleep(1);}
  910. }
  911.  
  912. IRC.CPP ENDS HERE
Advertisement
Add Comment
Please, Sign In to add comment