# IRC bot# Normal message protocol:# Incoming message:# :[Chaser]!hostinfo.net PRIVMSG #uberchat :Hello# Outgoing message:# PRIVMSG #uberchat :Message Goes Here\r\n"import socketimport randomimport pickleimport time# Lets user pick the channel that the bot starts in.initchan = raw_input('initchan: ')trigger = '>>'botnick = "BotNick"botname = "BotName"### Initiating connection# IRC Networknetwork = 'irc.freenode.net'port = 6667irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)irc.connect((network, port))print "Please wait as you are being connected..."# Bot's nicknameirc.send ( 'NICK ' + botnick + '\r\n' )# Bot's user infoirc.send ( 'USER '+botname+' '+botname+' '+botname+' :'+botname+'\r\n' )irc.send ( 'JOIN ' + initchan + '\r\n' )irc.send("PRIVMSG " + initchan + " :Hello! I am a bot programmed in Python, that knows python. Type '"+trigger+"?' for more info.\r\n")# Some variable initiationdata = ''channel = initchanchannels = {initchan:initchan}# When run == 1, then mainloop cycles.run = 1# This is a function for use then the users does <trigger>py. The user uses# this function to have the bot speak in a specific channel, or to a specific person.def speak(channel, output): global irc global channels irc.send("PRIVMSG " + channel + " :" + repr(output) + "\r\n")### Every once in awhile, the server sends a PING message at you. This function PONGs it back.def pingpong(data): global irc if data.find ("PING") != -1: print "PINGPONG" irc.send ("PONG " + data.split()[1] + "\r\n")### This function defines all the responses that can be triggered by a user.def respond(channel, nick, msg): global trigger global run # If message contains trigger then it is a command. if msg.find(trigger) != -1: cmd = msg.split(trigger)[1] if cmd.find("shutdown") != -1 and nick == "[Chaser]": irc.send("PRIVMSG " + channel + " :At your will.\r\n") irc.send("QUIT\r\n") run = 0 elif cmd.find("shutdown") != -1 and nick != "[Chaser]": irc.send("PRIVMSG " + channel + " :No. You have no authority over me.\r\n") elif cmd.find("I love you Pythonator") != -1 and nick == "[Chaser]": irc.send("PRIVMSG " + channel + " :I love you too [Chaser].\r\n") elif cmd.find("pyo ") != -1: script = cmd.split("pyo ")[1] if not script.find("import") != -1: output = eval(script) speak(channel, output) elif cmd.find("py ") != -1: script = cmd.split("py ")[1] if script.find("import") != -1 or script.find("open") != -1 or script.find("socket") != -1 or script.find("sleep") != -1: irc.send("PRIVMSG " + channel + " :Python execution canceled; Found invalid text. See '"+trigger+"?py' for more info.\r\n") else: exec(script) elif cmd.find("you suck") != -1: irc.send("PRIVMSG " + channel + " :Nope.\r\n") elif cmd.find("join channel ") != -1: ch = cmd.split("join channel ")[1] irc.send ("JOIN " + ch + "\r\n" ) irc.send("PRIVMSG " + channel + " :I am there.\r\n") channels[ch] = ch irc.send("PRIVMSG " + ch + " :Hello. I am a bot programmed in Python, that knows python. Type '"+trigger+"?' for more info.\r\n") elif cmd.find("leave this channel") != -1 and not cmd == "?leave this channel": irc.send("PRIVMSG " + channel + " :Right away.\r\n") irc.send ("PART " + channel + "\r\n") del channels[channel] elif cmd == "?" or cmd == "help": irc.send("NOTICE " + nick + " :Hello. I am a bot programmed in Python from scratch.\r\n") irc.send("NOTICE " + nick + " :[Chaser] is my creator.\r\n") irc.send("NOTICE " + nick + " :Type '"+trigger+"command' to make me execute a command.\r\n") irc.send("NOTICE " + nick + " :For example, '"+trigger+"pyo 7 + 8' would return 15, as pyo is a command.\r\n") irc.send("NOTICE " + nick + " :For more information on Python Expressions, type '"+trigger+"?pyo'\r\n") irc.send("NOTICE " + nick + " :For a list of commands, type '"+trigger+"commands'.\r\n") elif cmd == "commands" or cmd == "?commands" or cmd == "commands?" or cmd == "list commands": irc.send("NOTICE " + nick + " :Put a '?' if front of each command for help on it. (example: for help with '"+trigger+"pyo', type '"+trigger+"?pyo')\r\n") irc.send("NOTICE " + nick + " :"+trigger+"pyo <python expression>\r\n") irc.send("NOTICE " + nick + " :"+trigger+"py <python script>\r\n") irc.send("NOTICE " + nick + " :"+trigger+"join channel <channel>\r\n") irc.send("NOTICE " + nick + " :"+trigger+"leave this channel\r\n") irc.send("NOTICE " + nick + " :"+trigger+"shutdown (only [Chaser] can use this.)\r\n") elif cmd == "?py" or cmd == "py?": irc.send("NOTICE " + nick + " :'"+trigger+"py <python script>' is a command that executes a python script.\r\n") irc.send("NOTICE " + nick + " :A function for displaying info into an IRC channel is 'speak(channel, info)'\r\n") irc.send("NOTICE " + nick + " :You may not import modules, use 'open' prebuilt function, or socket commands, or use 'sleep'.\r\n") elif cmd == "?pyo" or cmd == "pyo?": irc.send("NOTICE " + nick + " :'"+trigger+"pyo' is a command that reads a Python expression, and displays the output into the IRC channel.\r\n") elif cmd == "?join channel" or cmd == "join channel?" or cmd == "?join": irc.send("NOTICE " + nick + " :'"+trigger+"join channel <channel>' is a command that tells me to join a channel.\r\n") elif cmd == "?leave this channel" or cmd == "leave this channel?" or cmd == "?leave": irc.send("NOTICE " + nick + " :'"+trigger+"leave this channel' is a command that tells me to leave this current channel.\r\n")### This function will parse (read) the data that you received, so that you can interpret### the data.def parse(data): try: global channel global irc sdata = data.split(":") info = sdata[1].split(" ") msgtype = '' # If is a message (Even normal messages in a channel are considered PRIVMSG): if info[1] == "PRIVMSG": # Gets the nickname out of the message nick = info[0].split('!')[0] # Gets the channel it was sent from. channel = info[2] # Gets the message. msg = sdata[2].split('\r\n')[0] # Print data print channel + '_' + nick + ': ' + msg # Now we see if we have to respond, and if so, then we will. respond(channel, nick, msg) except: pass### Mainloopwhile run == 1: msg = "" # Receiving data... data = irc.recv ( 4096 ) # Checking for pings pingpong(data) parse(data)