All pastes #504546 Raw Edit

Miscellany

public c v1 · immutable
·published 2007-05-23 04:17 UTC
rendered paste body
/*********************************************************************************		File	 : bbirc.c*		Author	 : ceeg*		Created  : Tue May 22 05:46:13 2007*		Modified : Wed May 23 00:05:04 2007*		Website  : www.chrisgillis.net**	=== bbIRC is a barebonesIRC bot. I'll add stuff when I feel like it. My code* 		is probably crap and will destroy your PC so don't run it.**  This program is free software; you can redistribute it and/or modify*  it under the terms of the GNU General Public License as published by*  the Free Software Foundation; either version 2 of the License, or*  (at your option) any later version.**  This program is distributed in the hope that it will be useful,*  but WITHOUT ANY WARRANTY; without even the implied warranty of*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*  GNU General Public License for more details.**  You should have received a copy of the GNU General Public License*  along with this program; if not, write to the Free Software*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.********************************************************************************//* First things first, we must include all necessary header files * Note I have commented each line with the functions the header * provides access to *//* The only headers included that are related to sockets are inet.h * and socket.h */ #include <stdio.h> 		/* printf() */#include <stdlib.h>		/* atoi() */#include <string.h>		/* we'll use some string funcs, also memset() */#include <arpa/inet.h>  /* sockaddr_in, inet_addr() */#include <sys/socket.h> /* socket(), connect(), send(), recv() */#include <unistd.h>		/* close() *//* I've defined a datatype called IrcConnection that will hold * all of the variables related to connecting to an IRC server * *** you will want to move this struct to its own file in your *     own projects. */	 typedef struct {	/* The socket our server will transmit data on */	int sock;	/* The server address */	struct sockaddr_in ircServerAddr;	/* The port we're going to connect on */	unsigned short ircServerPort;	/* A pointer to char for storing the IP */	char *serverIp;	/* A pointer to char for storing the bots nick */	char *nickname;	/* A pointer to char for storing the channel to join */	char *channel;} IrcConnection;	/* Next, we create all of our #define directives. */#define RECVBUFSIZE 512 /* The size alloted to the buffer we're going to use						 * for the recv() buffer */						 #define VERSION 0.1		/* Version number *//* Now we'll declare any functions we've written for the program */void DieWithError( char *errorMessage ); /* See DieWithError() */int SendUserNick( IrcConnection * );	 /* See SendUserNick() */int Pong( IrcConnection *, char *match );/* See Pong() */int JoinChannel( IrcConnection * );		 /* See JoinChannel() *//* Now we declare the main() funtion. It's here where our program will * loop until it is terminated. I've tried to cut the bot up into smaller * functions though it's possible I'll get lazy. */ int main ( int argc, char *argv[] ){	/* Declare variables */	char recvBuf[RECVBUFSIZE];	unsigned short bytesRcvd = 0;	char pongedOnce = 0; /* bot sent pong? */	char inChannel  = 0; /* bot joined a channel? */		/* Create a pointer to IrcConnection */	IrcConnection con, *irc = &con; 		/* We need to check to see if the arguments provided to the 	 * program are correct before we assign any values to IrcConnection	 */		if ( (argc < 4) || (argc > 5) )	{		fprintf(stderr, "Usage: %s <Server IP> <Nick> <Channel> [port]\n", argv[0]);		exit(1);	}		/* Now we need to see if the program has been given an explicit port	 * to use, as well as set some IrcConnection members to their values	 */		irc->serverIp = argv[1];	irc->nickname = argv[2];	irc->channel  = argv[3];		if ( argc == 5 )		irc->ircServerPort = atoi(argv[4]); /* set port to specified */	else		irc->ircServerPort = 6667;			/* or use standard */		/* Oh boy, we get to initialize the socket! */		if ((irc->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)		DieWithError("Error: socket() failed.");		/* Go ahead and initialize the ircServerAddr struct so we can connect	 * on the socket	 */		/* To my knowledge, it is required on some systems to zero the structure	 * Please correct me if i'm wrong/out of date on this	 */		memset(&(irc->ircServerAddr), 0, sizeof(irc->ircServerAddr));	/* internet address family */	irc->ircServerAddr.sin_family		= AF_INET;	/* server ip */		irc->ircServerAddr.sin_addr.s_addr	= inet_addr(irc->serverIp);  	/* http://retran.com/beej/htonsman.html */	irc->ircServerAddr.sin_port			= htons(irc->ircServerPort); 		/* It's about time we got to connect() right? */		if (connect(irc->sock, (struct sockaddr *) &(irc->ircServerAddr), sizeof(irc->ircServerAddr)) < 0)		DieWithError("Error: connect() failed.");		/* Well shoot, if it's gotten this far you're connected to an IRC server.	 * Not for long though ;)	 * We need to send a few strings to the server so it doesnt boot us off!	 * NICK <nickname>	 * USER nick nick nick :nick	 */		if (SendUserNick(irc) < 0)		DieWithError("Error: SendUserNick() failed.");				/* After you send a USER and a NICK you HAVE to start receiving. The server	 * will likely send a "PING :<string>" message and you MUST reply to it with	 * "PONG :<string>" where <string> == <string>, this is so the server can 	 * remove dropped connections 	 */		/* ============= Main Loop ============= */	while (1) /* repeat until terminated */	{		char *match;				if ((bytesRcvd = recv(irc->sock, recvBuf, RECVBUFSIZE-1, 0)) <= 0)			DieWithError("recv failed\n");					recvBuf[bytesRcvd] = '\0';		printf(recvBuf);		printf("\n");				/* We now effectively have a message stored in our recv buffer 		 * It would be good if we could search it for PING		 * y hullo thar, string.h		 */		if ((match = strstr(recvBuf, "PING")) != NULL)		{			/* A pong message is in the format PONG :<string>			 * "PING :" is 6chars. match+6 is the start of the string we have		 	 * to reply back with.			 */			Pong( irc, match+6 );			printf("PONG!\n");			pongedOnce = 1; /* so we can join a channel (gotta send pong first) */		}				if((pongedOnce == 1) && (inChannel == 0))		{			JoinChannel(irc);			inChannel++;		}				}	return 0;} /* Just a quick note while you're in this section, it'd be better to write a  * single function that sends a message to the server. I'll probably do that * ...sometime  *//* DieWithError  */void DieWithError( char *errorMsg ){	perror(errorMsg);	exit(1);}/* SendUserNick * Takes a pointer to IrcConnection * sends messages to server to register NICK/USER * returns -1 if unsuccessful */int SendUserNick( IrcConnection *ircon ){	char *usnbuf = malloc(256); /* we need a buffer for sprint, so we 	                             * can format the string we'll send */	unsigned int bufLen;        /* stores how wide usnbuf is */	 sprintf(usnbuf, "USER %s . . :%s%f\r\nNICK %sr\r\n", ircon->nickname, 														  ircon->nickname, 														  VERSION, 														  ircon->nickname);	 bufLen = strlen(usnbuf);	if (send(ircon->sock, usnbuf, bufLen, 0) != bufLen)		return -1;	else		return 0; }/* Pong * takes pointer to char n where n...'\0' is the PING string and  * sends a pong reply to the server  */int Pong( IrcConnection *ircon, char *match ){	char *buf = malloc(128);	unsigned int bufLen;		sprintf(buf, "PONG :%s", match);	bufLen = strlen(buf);		if (send(ircon->sock, buf, bufLen, 0) != bufLen)		return -1;	else		return 0; }/* JoinChannel * joins the channel specified in the IrcConnection structure */int JoinChannel( IrcConnection *ircon ){	char *cbuf = malloc(128);	unsigned int bufLen;		sprintf(cbuf, "JOIN #%s", ircon->channel);	bufLen = strlen(cbuf);		if (send(ircon->sock, cbuf, bufLen, 0) != bufLen)		return -1;	else		return 0; }