Facebook
From Nie Ja, 8 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 419
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <netdb.h>
  5. #include <stdarg.h>
  6.  
  7. int conn;
  8. char sbuf[512];
  9.  
  10. void raw(char *fmt, ...) {
  11.     va_list ap;
  12.     va_start(ap, fmt);
  13.     vsnprintf(sbuf, 512, fmt, ap);
  14.     va_end(ap);
  15.     printf("<< %s", sbuf);
  16.     write(conn, sbuf, strlen(sbuf));
  17. }
  18.  
  19. int main() {
  20.  
  21.     char *nick = "testBot";
  22.     char *channel = "#e-sim.pfa";
  23.     char *host = "irc.quakenet.org";
  24.     char *port = "6667";
  25.  
  26.     char *user, *command, *where, *message, *sep, *target;
  27.     int i, j, l, sl, o = -1, start, wordcount;
  28.     char buf[513];
  29.     struct addrinfo hints, *res;
  30.  
  31.     char wiadomosc[513];
  32.  
  33.     memset(&hints, 0, sizeof hints);
  34.     hints.ai_family = AF_INET;
  35.     hints.ai_socktype = SOCK_STREAM;
  36.     getaddrinfo(host, port, &hints, &res);
  37.     conn = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  38.     connect(conn, res->ai_addr, res->ai_addrlen);
  39.  
  40.     raw("USER %s 0 0 :%s\r\n", nick, nick);
  41.     raw("NICK %s\r\n", nick);
  42.  
  43.     while ((sl = read(conn, sbuf, 512))) {
  44.         for (i = 0; i < sl; i++) {
  45.             o++;
  46.             buf[o] = sbuf[i];
  47.             if ((i > 0 && sbuf[i] == '\n' && sbuf[i - 1] == '\r') || o == 512) {
  48.                 buf[o + 1] = '\0';
  49.                 l = o;
  50.                 o = -1;
  51.  
  52.                 printf(">> %s", buf);
  53.                 //printf("BUF: \"%s\".\n", buf);
  54.                 if (!strncmp(buf, "PING", 4)) {
  55.                     buf[1] = 'O';
  56.                     raw(buf);
  57.                 } else if (buf[0] == ':') {
  58.                     wordcount = 0;
  59.                     user = command = where = message = NULL;
  60.                     for (j = 1; j < l; j++) {
  61.                         if (buf[j] == ' ') {
  62.                             buf[j] = '\0';
  63.                             wordcount++;
  64.                             switch(wordcount) {
  65.                                 case 1: user = buf + 1; break;
  66.                                 case 2: command = buf + start; break;
  67.                                 case 3: where = buf + start; break;
  68.                             }
  69.                             if (j == l - 1) continue;
  70.                             start = j + 1;
  71.                         } else if (buf[j] == ':' && wordcount == 3) {
  72.                             if (j < l - 1) message = buf + j + 1;
  73.                             break;
  74.                         }
  75.                     }
  76.  
  77.                     if (wordcount < 2) continue;
  78.  
  79.                     if (!strncmp(command, "001", 3) && channel != NULL) {
  80.                         raw("JOIN %s\r\n", channel);
  81.                     } else if (!strncmp(command, "PRIVMSG", 7) || !strncmp(command, "NOTICE", 6)) {
  82.                         if (where == NULL || message == NULL) continue;
  83.                         if ((sep = strchr(user, '!')) != NULL) user[sep - user] = '\0';
  84.                         if (where[0] == '#' || where[0] == '&' || where[0] == '+' || where[0] == '!') target = where; else target = user;
  85.                         /*
  86.                         if(!strncmp(message, "-a", 2))
  87.                         {
  88.                             strcpy(wiadomosc, "PRIVMSG #e-sim.pfa :piszemy coś");
  89.                             printf("WIADOMOSC: %s\n", wiadomosc);
  90.                             write(conn, wiadomosc, strlen(wiadomosc));
  91.                         }
  92.                         */
  93.                         printf("[from: %s] [reply-with: %s] [where: %s] [reply-to: %s] %s", user, command, where, target, message);
  94.                         //raw("%s %s :%s", command, target, message); // If you enable this the IRCd will get its "*** Looking up your hostname..." messages thrown back at it but it works...
  95.                     }
  96.                 }
  97.  
  98.             }
  99.         }
  100.  
  101.     }
  102.  
  103.     return 0;
  104.  
  105. }
  106.