All pastes #273204 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-12-09 18:32 UTC
rendered paste body
//  Kyriakos aka kir    koulisathens@gmail.com
//  Tiny IRC client version 0.4
//  9/12/2006
//  usage: ircclient <servername>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define  DEFAULT_SERVER      "city.irc.gr"

#define  NICKNAME            "kasparof"
#define  CHANNEL             "#playchess"

#define  NICKNAME_IDENTIFY   "deepblue"
#define  CHANNEL_IDENTIFY    "12346"

void connectMe();
void sendMe();
void recvMe();

char server[20], lineout[100], buffer[1000];
int s;

int main(int argc, char **argv)
{
  char *a, user[50];

  if (argc!=2)
     strcpy(server, DEFAULT_SERVER);
  else
     strcpy(server,argv[1]);
 while (CHANNEL == NULL)
 {
  connectMe();

  sprintf(lineout, "USER kasparof none@nowhere.gr: anonymos xrhsths\n\r");
  sendMe();
  
  sprintf(lineout, "NICK %s\n\r", NICKNAME);
  sendMe();

  sprintf(lineout, "IDENTIFY %s\n\r", NICKNAME_IDENTIFY);
  sendMe();

  sprintf(lineout, "ChanServ IDENTIFY %s %s\n\r", CHANNEL, CHANNEL_IDENTIFY);
  sendMe();

  sprintf(lineout, "JOIN %s\n\r", CHANNEL);
  sendMe();

  while(1)
  {
    recvMe();

    if ( a = strstr(buffer, "PING :") )
    {
       sprintf(lineout, "PONG :%s\n\r", NICKNAME);
       sendMe();
    }

    if (buffer[0] == ':')
    {
       a = buffer + 1;

       strncpy(user, a, sizeof(user) );

       if (a  = strchr(user, '!') )
          *a = '\0';

       if (a = strstr(buffer, "PRIVMSG") )
         printf("%s:\t%s", user, a+strlen(CHANNEL)+10);
       else
         printf("%s",buffer);
    }


  }

}

}


void sendMe()
{
  if (send(s,lineout,strlen(lineout),0) < 0)
     exit(1);
}


void connectMe()
{
  struct sockaddr_in addr;
  struct hostent *h;

  if ( (h = gethostbyname(server)) == NULL)
  {
   perror("gethostbyname");
   exit(1);
  }

  if ( (s = socket(AF_INET, SOCK_STREAM, 0) ) < 0 )
     exit(1);

  addr.sin_family = AF_INET;
  addr.sin_port = htons(6667);
  addr.sin_addr = *( (struct in_addr *) h->h_addr );

  memset(&(addr.sin_zero), 0, sizeof(addr.sin_zero) );

  if ( (connect(s, (struct sockaddr *) &addr, sizeof(struct sockaddr) )) < 0 )
  {
   perror("connect");
   close(s);
   exit(1);
  }
}

void recvMe()
{
  memset(buffer,0,sizeof(buffer));

  if ( recv(s,buffer,sizeof(buffer),0) < 0 )
     exit(1);

}