#### todo make nick fetcher into a class so that other parts of the script can access it. #!/usr/bin/env python import socket # This is to identify your user/pass to the IRC server so your bot can be masked # Thought it was safer to have it on user input rather than hardcoded user = raw_input("enter user ") secret = raw_input("enter password ") # name of the bot, channel and irc network botnick = raw_input("BOT NICKNAME ") channel = raw_input("IRC CHANNEL ") network = 'irc.freenode.net' # irc protocol port = 6667 irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) irc.connect((network, port)) print irc.recv(4096) # bot will identify to the IRC server and join desired channel irc.send('NICK ' + botnick + '\r\n') irc.send('USER vitebot vitebot vitebot:Python IRC\r\n') irc.send('nickserv identify ' + user + ' ' + secret+ '\r\n') irc.send('JOIN '+ channel +'\r\n') irc.send('PRIVMSG '+ channel +' :Hello World.\r\n') irc.send('chanserv op ' + channel + " " + botnick + '\r\n') while True: # Creating shortcut variables data = irc.recv(4096) find = data.find send = irc.send print data # This fetches the nicks nick = data.split('!')[0] nick = nick.replace(':', ' ') nick = nick.replace(' ', '') nick = nick.strip(' \t\n\r') # PING PONG with the IRC server to avoid getting booted if find('PING') != -1: send('PONG ' + data.split()[1] + '\r\n') print "PONG SENT" print data # Establishing bot behaviour elif find(':!' + botnick) != -1: send('PRIVMSG ' + channel + ' : Hi\r\n') print "Hi SENT" print data # This will only let the user end the script via IRC elif find(':!op') != -1: if user == nick: send('chanserv op ' + channel + " " + user + '\r\n') print data elif find('!quit') != -1: if user == nick: send(' PRIVMSG ' + channel + ': Good Bye') send('quit\r\n') print data quit()