Advertisement
Julienl

BOT IRC C Windows

Aug 31st, 2011
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.22 KB | None | 0 0
  1. /*
  2.  BOT IRC C
  3.  @author Julienl
  4. */
  5.  
  6. #include <winsock2.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <regex.h>
  10. #include <time.h>
  11.  
  12.  
  13. //Fonction initialisation
  14. void init()
  15. {
  16.     WSADATA WSAData;
  17.  
  18.     WSAStartup(MAKEWORD(2, 2), &WSAData);
  19. }
  20.  
  21. //Fonction cleanup de WSA
  22. int CleanupWSA()
  23. {
  24.     int WSA = WSACleanup();
  25.  
  26.     return WSA;
  27. }
  28.  
  29. //Fermeture du socket
  30. void clean(SOCKET mysock)
  31. {
  32.     closesocket(mysock);
  33.  
  34.     CleanupWSA();
  35. }
  36.  
  37. //Comparer une string à une expression réguliére
  38. int regexcheck(char *str_request, char *str_regex)
  39. {
  40.     int err;
  41.     regex_t preg;
  42.     //const char *str_regex = ":coucou";
  43.  
  44.     err = regcomp(&preg, str_regex, REG_NOSUB | REG_EXTENDED);
  45.  
  46.     if(err == 0)
  47.     {
  48.         int match;
  49.  
  50.         match = regexec(&preg, str_request, 0, NULL, 0);
  51.  
  52.         regfree(&preg);
  53.  
  54.         return match;
  55.     }
  56.  
  57. }
  58.  
  59.  
  60. char* returndate(void)
  61. {
  62.     char valdate[100];
  63.  
  64.     SYSTEMTIME Time;
  65.  
  66.     GetLocalTime(&Time);
  67.  
  68.     sprintf(valdate, "PRIVMSG #botjulienl %02d/%02d/%04d\r\n", Time.wDay, Time.wMonth, Time.wYear);
  69.  
  70.     return valdate;
  71. }
  72.  
  73. char* returntime(void)
  74. {
  75.     char valtime[100];
  76.  
  77.     SYSTEMTIME Time;
  78.  
  79.     GetLocalTime(&Time);
  80.  
  81.     sprintf(valtime, "PRIVMSG #botjulienl %02dh:%02dmn:%02ds\r\n", Time.wHour, Time.wMinute, Time.wSecond);
  82.  
  83.     return valtime;
  84. }
  85.  
  86.  
  87. //fonction main
  88. int main(int argc, char **argv)
  89. {
  90.     init();
  91.  
  92.     char *nick = "NICK jultaro\r\n";
  93.     char *user = "USER guest \"localhost\" \"irc_server\" :julbot \r\n";
  94.     char *join = "JOIN #botjulienl\r\n";
  95.     char *chan = "#botjulienl";
  96.  
  97.     int i = 0;
  98.  
  99.     //Definition de la socket
  100.     SOCKET mysock;
  101.     SOCKADDR_IN sin;
  102.  
  103.     //Défiition des informations de la socket
  104.     sin.sin_addr.s_addr = inet_addr("213.179.58.83");
  105.     sin.sin_family      = AF_INET;
  106.     sin.sin_port        = htons(6667);
  107.     mysock = socket(AF_INET,SOCK_STREAM,0);
  108.  
  109.     //Connexion de la socket
  110.     if(connect(mysock, (SOCKADDR *)&sin, sizeof(sin)) == -1)
  111.     {
  112.         printf("%u", WSAGetLastError());
  113.         return -1;
  114.     }
  115.  
  116.     //Transmission des données d'authentification
  117.     if(send(mysock, nick, strlen(nick), 0) == -1)
  118.     {
  119.         printf("Error for Nick \n");
  120.     }
  121.  
  122.     if(send(mysock, user, strlen(user), 0) == -1)
  123.     {
  124.         printf("Error for User\n");
  125.     }
  126.  
  127.     if(send(mysock, join, strlen(join), 0) == -1)
  128.     {
  129.         printf("Error for JOIN\n");
  130.     }
  131.  
  132.     send(mysock, "PRIVMSG #botjulienl Plop\r\n", strlen("PRIVMSG #botjulienl Plop\r\n"), 0);
  133.  
  134.     while(i == 0)
  135.     {
  136.         char * buffer;
  137.         char buf[512];
  138.  
  139.         if(recv(mysock, buf, sizeof(buf), 0) == -1)
  140.         {
  141.             printf("Error : buffer");
  142.         }
  143.  
  144.         buffer = malloc(sizeof(buf));
  145.  
  146.         buffer = buf;
  147.  
  148.         //Répondre au Ping
  149.         if (strstr(buffer, "PING :") != NULL && buffer[0] == 'P')
  150.         {
  151.             buffer[1] = 'O';
  152.  
  153.             if ((send(mysock, buffer, strlen(buffer), 0)) == -1)
  154.             {
  155.                 printf("Error : PING");
  156.             }
  157.  
  158.         }
  159.  
  160.         if(regexcheck(buffer,"PRIVMSG #botjulienl :!time") == 0 && strstr(buffer, ":julienl!") != NULL)
  161.         {
  162.             char *time = returntime();
  163.  
  164.             if(send(mysock, time, strlen(time), 0) == -1)
  165.             {
  166.                 printf("ERROR : Time");
  167.             }
  168.         }
  169.  
  170.         if(regexcheck(buffer, "PRIVMSG #botjulienl :!date") == 0 && strstr(buffer, ":julienl!") != NULL)
  171.         {
  172.             char *date = returndate();
  173.  
  174.             if(send(mysock, date, strlen(date), 0) == -1)
  175.             {
  176.                 printf("ERROR : Date");
  177.             }
  178.         }
  179.  
  180.         if(regexcheck(buffer, "PRIVMSG #botjulienl :test") == 0 && strstr(buffer, ":julienl!") != NULL)
  181.         {
  182.             send(mysock, "PRIVMSG #botjulienl Yeah !\r\n", strlen("PRIVMSG #botjulienl Yeah !\r\n"), 0);
  183.         }
  184.  
  185.         if(regexcheck(buffer, "PRIVMSG #botjulienl :!quit") == 0 && strstr(buffer, ":julienl!") != NULL)
  186.         {
  187.             send(mysock, "QUIT\r\n", strlen("QUIT\r\n"), 0);
  188.  
  189.             i = 1;
  190.         }
  191.  
  192.         printf("%s", buffer);
  193.  
  194.         //Vider le buffer
  195.         free(buffer);
  196.         buffer = NULL;
  197.     }
  198.  
  199.     clean(mysock);
  200.  
  201.     return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement