Advertisement
oaktree

IRC bot still in works

Apr 10th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <netinet/in.h>
  7. #include <netdb.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <stdarg.h>
  11.  
  12. #define IRC_PORT 6667
  13. #define BUFFER_SIZE 2048
  14.  
  15. int connect_to_irc(int* sockfd, char* irc_channel, int port);
  16. int get_on_channel(int* sockfd, char* channel, char* nick, char* buf);
  17. int write_to_socket(int* sockfd, char* buf, char* fmt, ...);
  18.  
  19. int main(int argc, char** argv) {
  20.  
  21.     int sockfd;
  22.     connect_to_irc(&sockfd, "irc.freenode.net", 6667);
  23.  
  24.     char buf[BUFFER_SIZE+1]; char out[BUFFER_SIZE+1];
  25.     int n;
  26.  
  27.     get_on_channel(&sockfd, "#nullbyte", "PineTree", out);
  28.    
  29.     while( (n = read(sockfd, buf, BUFFER_SIZE)) ) {
  30.         if (n <= 0) continue;
  31.  
  32.         buf[n] = 0;
  33.  
  34.         if (strncmp(buf, "PING", 4) {
  35.             printf("We got PING'd\n");
  36.            
  37.             buf[1] = 'O';
  38.            
  39.             send(sockfd, out, strlen(out), 0);
  40.         }
  41.  
  42.         if (strstr(buf, "@slap") != NULL) {
  43.             out[0] = 0;
  44.  
  45.             char* pos = strstr(buf, "@slap ") + 6;
  46.             sprintf(out, "\rPRIVMSG #nullbyte :ACTION slapped the hell outta %s\r\n", pos);
  47.  
  48.             send(sockfd, out, strlen(out), 0);
  49.         }
  50.  
  51.         if (strstr(buf, "@google ") != NULL) {
  52.             char* google_url = "http://google.com/#q=";
  53.             char* pos = strstr(buf, "@google ") + 8;
  54.  
  55.             char* result = malloc(strlen(google_url)+strlen(pos));
  56.             if (result == NULL) continue;
  57.  
  58.             strcpy(result, google_url);
  59.             int x = strlen(google_url);
  60.             for (int i = 0, n = strlen(pos); i < n; i++) {
  61.                 if ( isalnum(pos[i]) )
  62.                     result[x] = pos[i];
  63.                 else if ( isspace(pos[i]) )
  64.                     result[x] = '+';
  65.  
  66.                 x++;
  67.                
  68.             }
  69.  
  70.             out[0] = 0;
  71.             sprintf(out, "\rPRIVMSG #nullbyte :%s\r\n", result);
  72.  
  73.             send(sockfd, out, strlen(out),0);
  74.             memset(result, 0, strlen(result));
  75.             free(result);
  76.         }
  77.  
  78.         printf("%s\n", buf);
  79.     }
  80.  
  81.     printf("click enter to exit");
  82.     scanf("%c",buf);
  83.  
  84.     close(sockfd);
  85.  
  86.     return 0;
  87. }
  88. int connect_to_irc(int* sockfd, char* irc_channel, int port) {
  89.     struct sockaddr_in serv_addr;
  90.     struct hostent *server;
  91.  
  92.     *sockfd = socket(AF_INET, SOCK_STREAM, 0);
  93.     if (*sockfd < 0) {
  94.         perror("socket could not be opened");
  95.         exit(1);
  96.     }
  97.  
  98.     server = gethostbyname("irc.freenode.net");
  99.     if (server == NULL) {
  100.         perror("host not found");
  101.         close(*sockfd);
  102.         exit(2);
  103.     }
  104.  
  105.     memset(&serv_addr, 0, sizeof(serv_addr));
  106.  
  107.     bcopy(
  108.         (char*)server->h_addr,
  109.         (char*)&serv_addr.sin_addr.s_addr,
  110.         server->h_length
  111.         );
  112.  
  113.     serv_addr.sin_family = AF_INET;
  114.     serv_addr.sin_port = htons(IRC_PORT);
  115.  
  116.     if ( connect(*sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0 ) {
  117.         perror("error connecting...");
  118.         close(*sockfd);
  119.         exit(3);
  120.     }
  121.  
  122.     return *sockfd;
  123. }
  124. int get_on_channel(int* sockfd, char* channel, char* nick, char* buf) {
  125.  
  126.     write_to_socket(sockfd, buf, "\rUSER %s 0 * :%s\r\n", nick, nick);
  127.     write_to_socket(sockfd, buf, "\rNICK %s\r\n", nick);
  128.     write_to_socket(sockfd, buf, "\rJOIN %s\r\n", channel);
  129.  
  130.     return 0;
  131. }
  132. int write_to_socket(int* sockfd, char* buf, char* fmt, ...) {
  133.     memset(buf, 0, BUFFER_SIZE);
  134.  
  135.     va_list ap;
  136.     va_start(ap,fmt);
  137.  
  138.     vsnprintf(buf, BUFFER_SIZE, fmt, ap);
  139.  
  140.     va_end(ap);
  141.  
  142.     write(*sockfd, buf, strlen(buf));
  143.  
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement