/******************************************************************************* * * 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 smile.gif * * 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. * *******************************************************************************/ #include "circus.h" #define SENDBUFSIZE 1024 /* size for send buf */ #define RECVBUFSIZE 1024 /* size for recv buf */ #define VERSION 0.1 /* Version number */ 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; /* Fill the IrcConnection with values from command line */ IrcLoadValues(irc, argc, argv); /* Create the socket and connect * i.e, sock() and connect() */ IrcSockConnect(irc); /* Need to send these two messages to register with the Irc Server * (not nickserv or anything, thats different) * NICK * USER nick nick nick :nick */ if (IrcSend(irc, SENDBUFSIZE, "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] = '\'; printf("%s\n", recvBuf); /* We now effectively have a message stored in our recv buffer. Get PING */ 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, SENDBUFSIZE,"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, SENDBUFSIZE, "JOIN #%s\n", irc->channels[0]); IrcJoinChannel(irc, argv[3]); IrcJoinChannel(irc, "kjll"); IrcJoinChannel(irc, "ikjl"); inChannel++; IrcSend(irc, SENDBUFSIZE, "PRIVMSG #%s :%s\n", irc->channels[0], "example"); } } return 0; } #include "circus.h" /* IrcSend * = treat it like snprintf on an IrcConnection */ int firstchan = 0; int IrcSend( IrcConnection *ircon, int bufsize, char *format, ...) { va_list args; char *buffer = malloc((size_t)bufsize); int retn = 0; unsigned int bufLen; va_start(args, format); vsnprintf(buffer, bufsize, format, args); va_end(args); bufLen = strlen(buffer); if (send(ircon->sock, buffer, bufLen, 0) != bufLen) retn = -1; free(buffer); return retn; } void IrcLoadValues( IrcConnection *ircon, int argc, char *argv[]) { /* 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 */ ircon->serverIp = argv[1]; ircon->nickname = argv[2]; /* explicit port? */ if ( argc == 5 ) ircon->ircServerPort = atoi(argv[4]); /* set port to specified */ else ircon->ircServerPort = 6667; /* or use standard */ /* zero it */ memset(&(ircon->ircServerAddr), 0, sizeof(ircon->ircServerAddr)); /* internet address family */ ircon->ircServerAddr.sin_family = AF_INET; /* server ip */ ircon->ircServerAddr.sin_addr.s_addr = inet_addr(ircon->serverIp); /* http://retran.com/beej/htonsman.html */ ircon->ircServerAddr.sin_port = htons(ircon->ircServerPort); /* set channel count to 0 */ ircon->cc = 0; } int IrcSockConnect( IrcConnection *ircon ) { /* init the socket */ if ((ircon->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("Error: socket() failed."); /* It's about time we got to connect() right? */ if (connect(ircon->sock, (struct sockaddr *) &(ircon->ircServerAddr), sizeof(ircon->ircServerAddr)) < 0) DieWithError("Error: connect() failed."); } int IrcJoinChannel( IrcConnection *ircon, char *chan) { if( firstchan == 0 ) { ircon->channels[ircon->cc] = chan; if(IrcSend(ircon, CHANSENDSIZ, "JOIN #%s\n", ircon->channels[ircon->cc]) < 0) DieWithError("Error: IrcSend() failed: "); firstchan++; } else { ircon->cc += 1; ircon->channels[ircon->cc] = chan; if(IrcSend(ircon, CHANSENDSIZ, "JOIN #%s\n", ircon->channels[ircon->cc]) < 0) DieWithError("Error: IrcSend() failed: "); } return ircon->cc; } /* DieWithError */ void DieWithError( char *errorMsg ) { perror(errorMsg); exit(1); } #include /* printf() */ #include /* atoi() */ #include /* we'll use some string funcs, also memset() */ #include /* for va list */ #include /* sockaddr_in, inet_addr() */ #include /* socket(), connect(), send(), recv() */ #include /* close() */ #define MAXCHANNELS 31 /* max num of channels one can join */ #define CHANSENDSIZ 100 /* send buffer for JoinChannel */ 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; /* An array of channels */ char **channels ; /* how many channels joined? */ int cc; /* channel count */ } IrcConnection; void DieWithError( char *errorMessage ); /* outputs errorMessage and quits */ int IrcSend( IrcConnection *, int bufsize, char *format, ...); /* sends a message over an IrcConnection */ void IrcLoadValues( IrcConnection *, int argc, char *argv[]); /* loads command line options into IrcCon */ int IrcSockConnect( IrcConnection * ); /* sock() and connect() on IrcConnection */ int IrcJoinChannel( IrcConnection *, char *chan); /* joins a channel */