All pastes #501758 Raw Edit

Anonymous

public c v1 · immutable
·published 2007-05-22 06:07 UTC
rendered paste body
/* Barebones IRC Skeleton */#include <stdio.h> 	/* printf() */#include <sys/socket.h> /* socket(), connect(), send(), recv() */#include <arpa/inet.h>  /* sockaddr_in, inet_addr() */#include <stdlib.h>	/* atoi() */#include <string.h>	/* memset() */#include <unistd.h>	/* close() */void DieWithError(char *errorMsg); /* prints error msg and exits */int main( int argc, char *argv[] ){	int sock;				/* the socket */	int inCommand;				/* internal commands *not sent to server* */	struct sockaddr_in ircServerAddr;	/* irc server address */	unsigned short ircServerPort;		/* irc server port */	char *serverIp;				/* x.x.x.x */	char *sendMsg;				/* string to send to irc server */	unsigned int sendMsgLen;		/* len of send message */		if ((argc < 2) || (argc > 3))	{		fprintf(stderr, "Usage: %s <IRC Server IP> [port]\n", argv[0]);		exit(1);	}		serverIp = argv[1]; 			/* first arg; required server ip */		if (argc == 3)		ircServerPort = atoi(argv[2]);	/* use provided port */	else		ircServerPort = 6667;		/* or use standard irc port */		/* create socket */	if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)		DieWithError("couldn't make socket");			/* fill ircServerAddr struct */	memset(&ircServerAddr, 0, sizeof(ircServerAddr)); 	/* zero the struct */	ircServerAddr.sin_family	= AF_INET;		/* internet address family */	ircServerAddr.sin_addr.s_addr	= inet_addr(serverIp);  /* server ip */	ircServerAddr.sin_port		= htons(ircServerPort); /* http://retran.com/beej/htonsman.html */		/* connect to the IRC server */	if (connect(sock, (struct sockaddr *) &ircServerAddr, sizeof(ircServerAddr)) < 0)		DieWithError("couldn't connect to serv");						   	sendMsg = "USER CeegBot CeegBot CeegBot :CeegBot\r\nNICK CeegBot\r\nJOIN #LLJKLLJK\r\n";	sendMsgLen = strlen(sendMsg);				/* size up the send message */		/* send the string to the server */	if (send(sock, sendMsg, sendMsgLen, 0) != sendMsgLen)		DieWithError("unexpected number of bytes sent");			/* === at this point the bot is connected to the irc channel ===	       and sitting quietly in a channel doing nothing	       assuming the nick wasn't taken	       	       you'll get kicked at the first PING though. 	       i'm just trying to learn some basic sockets here ;)	*/		if ((inCommand = getchar()) == 'q')	{		sendMsg = "QUIT :byebye";		close(sock);		exit(0);	}}void DieWithError(char *errorMsg){    perror(errorMsg);    exit(1);}