/* Uberbot: Another bot for IRC daemons 
 * Author: The Uberuser 
 * Initial Date: July 2004 
 * Version: 1.0 Alpha 
 * 
 * 
 * Main module for the uberbot to connect with IRC daemons. */ 
 
#include <stdio.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
 
/* global variables to be accessed 
   by all functions */ 
 
char product[] = "UberBot"; /* product name */ 
char author[] = "The Uberuser"; /* Person who wrote code */ 
char date[] = "July 2004"; /* Date project started */ 
float ver = 1.0; /* version number */ 
 
int main(int argc, char **argv) 
{ 
  int status; /* status return code */ 
  int s; /* socket */ 
  int bytes; /* bytes returned */ 
  int i; /* index number (counter) */ 
  int c; /* character returned from getchar() */ 
  int len; /* length of string */ 
  struct sockaddr_in serv_addr; /* structure containing the server information (IP address, etc)  */ 
  char buffer[BUFSIZ]; /* user input */ 
  char *server = NULL; 
  char *chancom[] = { /* commands to send to channel once connected */ 
    "JOIN #netwhores\r\n", 
    "PRIVMSG #netwhores ", 
    "QUIT\r\n",}; 
 
  if (argc >= 2) 
    server = argv[1]; 
  else 
    server = "205.207.137.2"; 
 
  if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) 
    perror("socket()"); 
 
  memset(&serv_addr, 0, sizeof serv_addr); 
  serv_addr.sin_family = AF_INET; 
  serv_addr.sin_port = htons(6667); 
  serv_addr.sin_addr.s_addr = inet_addr(server); 
 
  len = sizeof serv_addr; 
 
  printf("Welcome to %s v%.1f\nDeveloped by: %s\n\n", product, ver, author); 
  if (connect(s, (struct sockaddr *) &serv_addr, len) == -1) { 
    perror("connect()"); 
    exit(1); 
  } 
  IRCinit(&s); 
 
  for (i=0; i <= 2; i++) { 
    if ((status = write(s, chancom[i], strlen(chancom[i]))) == -1) { 
      perror("JOIN"); 
      exit(1); 
    } 
 
    if ((bytes = read(s, &buffer, sizeof(buffer))) == -1) { 
      perror("SOME READ ERROR"); 
      exit(1); 
    } 
      buffer[bytes] = '\0'; 
    printf("%s\r\n", buffer); 
  } 
  close(s); 
  return 0; 
} 
 
/* Module to establish a connection with the specified IRC daemon */ 
 
/* uberbot: IRC Bot client 
 * Author: The Uberuser July 2004 */ 
 
#include <stdio.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
 
void IRCinit(int *sock) /* Establish connection with IRC server */ 
{ 
  int n; /* bytes read from socket */ 
  int i; /* index number (counter) */ 
  char buf[512]; /* read buffer */ 
  char pong[17]; /* PING?, PONG! */ 
  char pid[12]; /* string returned from PING */ 
 
  char *commands[] = { 
    "NICK ubbies\r\n", 
    "USER uberuser m4dl33tb0x m4dl33tb0x :GNU is Not Unix\r\n", 
  }; 
 
  for (i=0; i <= 1; i++) { 
    if ((n = read(*sock, &buf, sizeof(buf))) == -1) { 
      perror("CONNECTION OUTPUT"); 
      exit(1); 
    } 
    buf[n] = '\0'; 
    printf("%s\r", buf); 
 
    if (write(*sock, commands[i], strlen(commands[i])) == -1) { 
      perror("WRITE ERROR"); 
      exit(1); 
    } 
  } 
  sscanf(buf, "PING :%s", pid); 
  printf("SEQUENCE NUMBER: %s\n", pid); 
  sprintf(pong, "PONG :%s", pid); 
 
  if (write(*sock, pong, strlen(pong)) == -1) { 
    perror("PONG write()"); 
    exit(1); 
  } 
  if ((n = read(*sock, &buf, sizeof(buf))) == -1) { 
    perror("PONG read()"); 
    exit(1); 
  } 
 
  buf[n] = '\0'; 
  printf("%s\r\n", buf); 
 
 
  if ((n = read(*sock, &buf, sizeof(buf))) == -1) { 
    perror("PONG read() 2"); 
    exit(1); 
  } 
  buf[n] = '\0'; 
  printf("%s\r\n", buf); 
  sscanf(buf, "PING :%s", pid); 
  printf("SEQUENCE NUMBER: %s\n", pid); 
  sprintf(pong, "PONG :%s", pid); 
  printf("IRC CONNECTION: INITIALIZED\n"); 
}

