#include #include #include #include #include #include #define PROTOPORT 6667 /* default protocol port number */ #define SOCK_FROM "<-- " #define SOCK_TO "--> " #ifdef __dietlibc__ #include #else #include static inline int __write1(const char* s) { write(1,s,strlen(s)); } static inline int __write2(const char* s) { write(2,s,strlen(s)); } #endif extern int errno; char localhost[] = "localhost"; /* default host name */ /* write to socket */ void __writesd(int sd, char* msg){ write(sd,msg,strlen(msg)); write(sd,"\r\n",strlen("\r\n")); __write1(SOCK_TO); __write1(msg); __write1("\n"); } /* append SOCK_FROM to a message -- mostly read from sd */ void __writerd(char *msg){ __write1(SOCK_FROM); __write1(msg); } /* read from socket */ int __readsd(int sd, char buf[512]){ int n,i; char chr[1]; i=0; while ( strncmp(chr,"\n",1) ) { n = read (sd,chr,1); if ( n <= 0 ) return -1; strncpy(buf+i,chr,1); strncpy(buf+i+1,"\0",1); //write(1,chr,1); i++; } chr[0]='\0'; return 0; } /* void sockreadwrite(int sd, char* msg){ int n; char* buf; write(sd,msg,strlen(msg)); n = read(sd, buf, sizeof(buf)); write (1,buf,n); } */ /* parse irc messages and respond, if necessary (PING...) */ int parseCMD(int sd, char buf[512], char nickname[30]){ char tmpchar[512], command[512]; int loggedin=0; if ( strncmp(buf,"PING",4) == 0) { strncpy(tmpchar,buf+5,strlen(buf)-2); strcpy(command,"PONG "); strcpy(command+strlen(command),tmpchar); __writesd(sd,command); } } main(int argc, char *argv[]) { struct hostent *ptrh; /* pointer to a host table entry */ struct protoent *ptrp; /* pointer to a protocol table entry */ struct sockaddr_in sad; /* structure to hold an IP address */ int sd; /* socket descriptor */ int port; /* protocol port number */ char *host; /* pointer to host name */ int n; /* number of characters read */ char buf[512]; /* buffer for data from the server */ char nickname[30]; memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ /* Check command-line argument for protocol port and extract */ /* port number if one is specified. Otherwise, use the default */ /* port value given by constant PROTOPORT */ if (argc > 2) { /* if protocol port specified */ port = atoi(argv[2]); /* convert to binary */ } else { port = PROTOPORT; /* use default port number */ } if (port > 0) /* test for legal value */ sad.sin_port = htons((unsigned short)port); else { /* print error message and exit */ fprintf(stderr,"bad port number %s\n",argv[2]); exit(1); } /* Check host argument and assign host name. */ if (argc > 1) { host = argv[1]; /* if host argument specified */ } else { host = localhost; } /* Convert host name to equivalent IP address and copy to sad. */ ptrh = gethostbyname(host); if ( ((char *)ptrh) == NULL ) { fprintf(stderr,"invalid host: %s\n", host); exit(1); } memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length); /* Map TCP transport protocol name to protocol number. */ if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) { perror("cannot map \"tcp\" to protocol number"); exit(1); } /* Create a socket. */ sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto); if (sd < 0) { perror("socket creation failed"); exit(1); } /* Connect the socket to the specified server. */ if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) { perror("connect failed"); exit(1); } strncpy(nickname,"lobotomi",strlen("lobotomi")+1); __writesd(sd,"USER lobotomi lart.info euirc :Aardvarks Bot"); __writesd(sd,"NICK lobotomi"); __writesd(sd,"PRIVMSG nickserv :IDENTIFY password"); __writesd(sd,"JOIN #dafk"); n = 1; while (n >= 0) { n = __readsd(sd, buf); __writerd(buf); parseCMD(sd,buf,nickname); buf[0]='\0'; } /* Close the socket. */ close(sd); /* Terminate the client program gracefully. */ exit(0); }