All pastes #504778 Raw Edit

Someone

public c v1 · immutable
·published 2007-05-23 08:09 UTC
rendered paste body
/*********************************************************************************		File	 : bbirc.c*		Author	 : ceeg*		Created  : Tue May 22 05:46:13 2007*		Modified : Wed May 23 04:05:49 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 <stdarg.h>  	/* for variable argument lists */#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 1024 			  /* 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() */int IrcSend( IrcConnection *, char *format, ...); /* See IrcSend() *//* 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 (IrcSend(irc, "NICK %s\nUSER %s . . :%sv%.1f\n", irc->nickname, irc->nickname, irc->nickname, VERSION) < 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);				/* 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.			 */			IrcSend( irc, "PONG :%s\n", match+6 );			printf("PONG!\n");			pongedOnce = 1; /* so we can join a channel (gotta send pong first) */		}				if((pongedOnce == 1) && (inChannel == 0))		{			IrcSend(irc, "JOIN #%s\n", irc->channel);			inChannel++;			IrcSend(irc, "PRIVMSG #%s :%s\n", irc->channel, "Hello.");		}				}	return 0;}/* DieWithError  */void DieWithError( char *errorMsg ){	perror(errorMsg);	exit(1);}/* IrcSend *  = treat it like sprintf on an IrcConnection */int IrcSend( IrcConnection * ircon, char *format, ...){	va_list args;	char *buffer = malloc(1024);	unsigned int bufLen;	int retn = 0;		va_start(args, format);	vsprintf(buffer, format, args);	va_end(args);		bufLen = strlen(buffer);	if (send(ircon->sock, buffer, bufLen, 0) == bufLen) 		goto out;	else 	{		retn = -1;		goto out;	}		out:		free(buffer);		return retn;}