Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as text by oblique ( 17 years ago )
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netdb.h>

#define BUFFER_SIZE 256
#define FLAG_P 0x1
#define FLAG_I 0x2
#define FLAG_C 0x4
#define FLAG_F 0x8

int sockfd;
char rawbuf[BUFFER_SIZE];

const char *usage = "IRC WarBot by OBLiQUE

"
					"	-h	server host or ip
"
					"	-p	port (default is 6667)
"
					"	-n	bot nickname
"
					"	-c	channel..write it without the # (optional)
"
					"	-m	nickname of the master (case sensitive)
"
					"	-i	server nickname identification (optional)
"
					"	-f	very fast performance (run it as root)
"
					"
example: ircbot -n botnick -h irc.server.net -m botmaster -c warchan
";

void raw(char *fmt, ...)
{
	va_list ap;
	va_start(ap,fmt);
	int len=vsnprintf(rawbuf,BUFFER_SIZE,fmt,ap);
	va_end(ap);
	write(sockfd,rawbuf,len);
	write(STDOUT_FILENO,"
<< ",4);
	write(STDOUT_FILENO,rawbuf,len);
}

int main(int argc, char *argv[])
{
	char *nick = NULL;
	char *channel = NULL;
	char *host = NULL;
	char *port = NULL;
	char *master = NULL;
	char *identify = NULL;
	int c,opt=0,flags=0;

	while ((c=getopt(argc,argv,"n:c:h:p:m:i:f"))!=-1) {
		switch(c) {
			case 'n':
				opt++;
				nick=optarg;
				break;
			case 'c':
				flags|=FLAG_C;
				channel=optarg;
				break;
			case 'h':
				opt++;
				host=optarg;
				break;
			case 'p':
				flags|=FLAG_P;
				port=optarg;
				break;
			case 'm':
				opt++;
				master=optarg;
				break;
			case 'i':
				flags|=FLAG_I;
				identify=optarg;
				break;
			case 'f':
				flags|=FLAG_F;
				break;
			default:
				return -1;
		}
	}

	if (opt==0) {
		printf(usage);
		return 1;
	}
	if (!(flags & FLAG_P)) {
		port=(char*)malloc(sizeof(char)*5);
		strncpy(port,"6667",5);
	}
	if (opt<3) {
		fprintf(stderr,"some arguments are missing.
");
		return -1;
	}
	if (optind!=argc) {
		fprintf(stderr,"too many arguments.
");
		return -1;
	}

	if (flags & FLAG_F) {
		if (setuid(0)==-1) {
			fprintf(stderr,"-f: %s
",strerror(errno));
			return -1;
		} 
		if (setgid(0)==-1) {
			fprintf(stderr,"-f: %s
",strerror(errno));
			return -1;
		} 
		if (nice(-20)==-1) {
			fprintf(stderr,"-f: %s
",strerror(errno));
			return -1;
		}
	}

	char *saveptr, *user, *command, *where, *msgcmd;
	char *enemy=NULL, *chan=NULL;
	char buf[BUFFER_SIZE+1]; // buffer
	int err, readlen, chansz, usersz, kicks=0;
	struct addrinfo hint, *aip;

	memset(&hint, 0, sizeof(hint)); // initialize hint to 0
	hint.ai_family=AF_INET;
	hint.ai_socktype=SOCK_STREAM;
	if ((err=getaddrinfo(host, port, &hint, &aip))!=0) {
		fprintf(stderr, "getaddrinfo: %s
", gai_strerror(err));
		return -1;
	}
	if ((sockfd=socket(aip->ai_family,aip->ai_socktype,aip->ai_protocol))<0) {
		fprintf(stderr, "socket: %s
",strerror(errno));
		return -1;
	}
	if (connect(sockfd,aip->ai_addr,aip->ai_addrlen)<0) {
		fprintf(stderr, "socket: %s
",strerror(errno));
		return -1;
	}

	raw("USER %s 0 0 :Digital Love
",nick);
	raw("NICK %s
",nick);
	if (flags & FLAG_I) raw("PRIVMSG NickServ :IDENTIFY %s
",identify);
	if (flags & FLAG_C) raw("JOIN #%s
", channel);

	while (readlen = read(sockfd, buf, BUFFER_SIZE)) {
		buf[readlen]=''; 
		write(STDOUT_FILENO,buf,readlen);

		if (!strncmp(buf, "PING", 4)) { // ping reply to the server
			buf[1]='O';
			raw(buf);
		} else if (buf[0]==':') {
			user = strtok_r(buf,"!", &saveptr);
			user++;
			usersz=saveptr-user;
			buf[readlen-2]='';
			if (usersz==readlen-1) {
				saveptr=NULL;
				strtok_r(buf," ",&saveptr);
				command = strtok_r(NULL," ", &saveptr);
			} else {
				strtok_r(NULL," ",&saveptr);
				command = strtok_r(NULL," ", &saveptr);
			}
			where = strtok_r(NULL," ", &saveptr);
			chansz=saveptr-where;
			msgcmd=strtok_r(NULL," ",&saveptr);

			if (!strcmp(command, "JOIN")) { // if i get JOIN
				if (enemy!=NULL && chan!=NULL) {
					if (!strcmp(user,enemy)) { // and its my enemy
						raw("KICK %s %s
",chan,enemy);
					}
				}
			} else if (!strcmp(command, "MODE")) { // if i get MODE and its op
				if (!strcmp(saveptr,nick) && enemy!=NULL && chan!=NULL) 
					raw("KICK %s %s
",chan,enemy);
			} else if (!strcmp(command, "482")) {
				sleep(2);
				raw("JOIN %s
", chan);
			} else if (!strcmp(command, "PRIVMSG")) {
				if (strcmp(user,master)) continue;
				//master commands
				msgcmd++;
				if  (!strcmp(msgcmd,"!exit")) { // close the connection and exit
					raw("QUIT :Digital Love
");
					close(sockfd);
					free(enemy);
					free(chan);
					return 0;
				} else if (!strcmp(msgcmd,"!join")) { // join to channel
					msgcmd=strtok_r(NULL," ",&saveptr);
					raw("JOIN %s
",msgcmd);
				} else if (!strcmp(msgcmd,"!part")) { // join to channel
					msgcmd=strtok_r(NULL," ",&saveptr);
					raw("PART %s
",msgcmd);
				} else if (!strcmp(msgcmd,"!stop")) { // remove the enemy
					if (enemy!=NULL) {
						free(enemy);
						enemy=NULL;
					}
					if (chan!=NULL) {
						free(chan);
						chan=NULL;
					}
				} else if (!strcmp(msgcmd,"!kick")) { // kick the enemy
					if (enemy!=NULL) free(enemy);
					if (chan!=NULL) free(chan);
					msgcmd=strtok_r(NULL," ",&saveptr);
					enemy=(char*)malloc(strlen(msgcmd));
					strcpy(enemy,msgcmd);
					chan=(char*)malloc(chansz);
					strcpy(chan,where);
					raw("KICK %s %s
",chan,enemy);
				} else if (!strcmp(msgcmd,"!rejoin")) {
					msgcmd=strtok_r(NULL," ",&saveptr);
					if (msgcmd==NULL) c=1;
					else c=atoi(msgcmd);
					for (;c>0;c--) {
						raw("PART %s
",where);
						raw("JOIN %s
",where);
					}
				}
			} else if (!strcmp(command, "KICK")) { // if i get KICK
				if (!strcmp(msgcmd,nick)) { // and its me
					if (enemy==NULL) { // add the enemy
						enemy=(char*)malloc(usersz);
						strcpy(enemy,user);
					}
					if (chan==NULL) { // add the channel
						chan=(char*)malloc(chansz);
						strcpy(chan,where);
					}
					kicks++;
					if (kicks==5) {
						printf("
wait 10sec
");
						kicks=0;
						sleep(10);
					}
					raw("JOIN %s
", chan);
				}
			}
		}
	}
}

 

Revise this Paste

Your Name: Code Language: