/******************************************************************************* * * File : bbirc.c * Author : ceeg * Created : Tue May 22 05:46:13 2007 * Modified : Wed May 23 15:59: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 /* printf() */ #include /* atoi() */ #include /* we'll use some string funcs, also memset() */ #include /* for variable argument lists */ #include /* sockaddr_in, inet_addr() */ #include /* socket(), connect(), send(), recv() */ #include /* close() */ /* I've defined a datatype called IrcConnection that will hold * all of the variables related to connecting to an IRC server */ 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; #define RECVBUFSIZE 1024 /* The size alloted to the buffer we're going to use * for the recv() buffer */ #define VERSION 0.1 /* Version number */ void DieWithError( char *errorMessage ); /* outputs errorMessage and quits */ int IrcSend( IrcConnection *, char *format, ...); /* sends a message over an IrcConnection */ int main ( int argc, char *argv[] ) { char recvBuf[RECVBUFSIZE]; /* buffer used for storing recv() messages */ unsigned short bytesRcvd = 0; /* used for indexing recvBuf after recv() */ char pongedOnce = 0; /* bot sent pong? */ char inChannel = 0; /* bot joined a channel? */ char *match; /* used for matching messages */ /* Create a pointer to IrcConnection */ IrcConnection con, *irc = &con; /* Check correct size of argc */ if ( (argc < 4) || (argc > 5) ) { fprintf(stderr, "Usage: %s [port]\n", argv[0]); exit(1); } /* Assign values to irc struct */ irc->serverIp = argv[1]; irc->nickname = argv[2]; irc->channel = argv[3]; /* explicit port? */ if ( argc == 5 ) irc->ircServerPort = atoi(argv[4]); /* set port to specified */ else irc->ircServerPort = 6667; /* or use standard */ /* init the socket */ if ((irc->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("Error: socket() failed."); /* 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 * 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: sending nick/usr failed."); /* After you send a USER and a NICK you HAVE to start receiving. The server * will likely send a "PING :" message and you MUST reply to it with * "PONG :" where == , this is so the server can * remove dropped connections */ /* ============= Main Loop ============= */ while (1) /* repeat until terminated */ { 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 : * "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, "WOMP WOMP WOMP."); } } 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; }