Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- BOT IRC C
- @author Julienl
- */
- #include <winsock2.h>
- #include <string.h>
- #include <stdio.h>
- #include <regex.h>
- #include <time.h>
- //Fonction initialisation
- void init()
- {
- WSADATA WSAData;
- WSAStartup(MAKEWORD(2, 2), &WSAData);
- }
- //Fonction cleanup de WSA
- int CleanupWSA()
- {
- int WSA = WSACleanup();
- return WSA;
- }
- //Fermeture du socket
- void clean(SOCKET mysock)
- {
- closesocket(mysock);
- CleanupWSA();
- }
- //Comparer une string à une expression réguliére
- int regexcheck(char *str_request, char *str_regex)
- {
- int err;
- regex_t preg;
- //const char *str_regex = ":coucou";
- err = regcomp(&preg, str_regex, REG_NOSUB | REG_EXTENDED);
- if(err == 0)
- {
- int match;
- match = regexec(&preg, str_request, 0, NULL, 0);
- regfree(&preg);
- return match;
- }
- }
- char* returndate(void)
- {
- char valdate[100];
- SYSTEMTIME Time;
- GetLocalTime(&Time);
- sprintf(valdate, "PRIVMSG #botjulienl %02d/%02d/%04d\r\n", Time.wDay, Time.wMonth, Time.wYear);
- return valdate;
- }
- char* returntime(void)
- {
- char valtime[100];
- SYSTEMTIME Time;
- GetLocalTime(&Time);
- sprintf(valtime, "PRIVMSG #botjulienl %02dh:%02dmn:%02ds\r\n", Time.wHour, Time.wMinute, Time.wSecond);
- return valtime;
- }
- //fonction main
- int main(int argc, char **argv)
- {
- init();
- char *nick = "NICK jultaro\r\n";
- char *user = "USER guest \"localhost\" \"irc_server\" :julbot \r\n";
- char *join = "JOIN #botjulienl\r\n";
- char *chan = "#botjulienl";
- int i = 0;
- //Definition de la socket
- SOCKET mysock;
- SOCKADDR_IN sin;
- //Défiition des informations de la socket
- sin.sin_addr.s_addr = inet_addr("213.179.58.83");
- sin.sin_family = AF_INET;
- sin.sin_port = htons(6667);
- mysock = socket(AF_INET,SOCK_STREAM,0);
- //Connexion de la socket
- if(connect(mysock, (SOCKADDR *)&sin, sizeof(sin)) == -1)
- {
- printf("%u", WSAGetLastError());
- return -1;
- }
- //Transmission des données d'authentification
- if(send(mysock, nick, strlen(nick), 0) == -1)
- {
- printf("Error for Nick \n");
- }
- if(send(mysock, user, strlen(user), 0) == -1)
- {
- printf("Error for User\n");
- }
- if(send(mysock, join, strlen(join), 0) == -1)
- {
- printf("Error for JOIN\n");
- }
- send(mysock, "PRIVMSG #botjulienl Plop\r\n", strlen("PRIVMSG #botjulienl Plop\r\n"), 0);
- while(i == 0)
- {
- char * buffer;
- char buf[512];
- if(recv(mysock, buf, sizeof(buf), 0) == -1)
- {
- printf("Error : buffer");
- }
- buffer = malloc(sizeof(buf));
- buffer = buf;
- //Répondre au Ping
- if (strstr(buffer, "PING :") != NULL && buffer[0] == 'P')
- {
- buffer[1] = 'O';
- if ((send(mysock, buffer, strlen(buffer), 0)) == -1)
- {
- printf("Error : PING");
- }
- }
- if(regexcheck(buffer,"PRIVMSG #botjulienl :!time") == 0 && strstr(buffer, ":julienl!") != NULL)
- {
- char *time = returntime();
- if(send(mysock, time, strlen(time), 0) == -1)
- {
- printf("ERROR : Time");
- }
- }
- if(regexcheck(buffer, "PRIVMSG #botjulienl :!date") == 0 && strstr(buffer, ":julienl!") != NULL)
- {
- char *date = returndate();
- if(send(mysock, date, strlen(date), 0) == -1)
- {
- printf("ERROR : Date");
- }
- }
- if(regexcheck(buffer, "PRIVMSG #botjulienl :test") == 0 && strstr(buffer, ":julienl!") != NULL)
- {
- send(mysock, "PRIVMSG #botjulienl Yeah !\r\n", strlen("PRIVMSG #botjulienl Yeah !\r\n"), 0);
- }
- if(regexcheck(buffer, "PRIVMSG #botjulienl :!quit") == 0 && strstr(buffer, ":julienl!") != NULL)
- {
- send(mysock, "QUIT\r\n", strlen("QUIT\r\n"), 0);
- i = 1;
- }
- printf("%s", buffer);
- //Vider le buffer
- free(buffer);
- buffer = NULL;
- }
- clean(mysock);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement