All pastes #1429215 Raw Edit

Unnamed

public text v1 · immutable
#1429215 ·published 2009-05-20 21:04 UTC
rendered paste body
from irc import *from time import *import sys, httplib, pycurl, re, random, MultipartPostHandler, urllib2, cookielibclass basicCURL:	def __init__(self):		self.contents = ''	def body_callback(self, buf):		self.contents = self.contents + bufclass mzxbot(IRC):	seencheck = 0	seentarget = ""	seenargs = ""	leftseen = []	departures = []	yahtzee = 0 	yplayers = []	dice = [0, 0, 0, 0, 0]	keep = []	pnum = 0	rolls = 0	def on_privmsg(self, nick, ident, host, target, msg):		if 'CJA' in nick:			pass		else:			if msg.startswith(('.','!')) and target[:1] == '#':				msg = msg.lstrip('.!')				if msg != "":					templist = msg.split() 					command = templist[0]					args = msg.split(command + " ")					try: 						args = args[1]					except:						args = ''					if command == "help":						if args == '':							self.msg(target, "Current commands are: wiki, mzxwiki, trope, help, version, whotoblame, seen, dict, foldoc, jargon, restart*, quit*, code. Prefix commands with . or ! Commands with an asterisk are privileged only.")						elif "whotoblame" in args:							self.msg(target, "Syntax: (./!)whotoblame, Purpose: Tells you who made this bot.")						elif "version" in args: 							self.msg(target, "Syntax: (./!)version, Purpose: Gives the current version of the bot.")						elif "wiki" in args:							self.msg(target, "Syntax: (./!)wiki <thing to look up>, Purpose: Looks up the specified string on wikipedia.")						elif "mzxwiki" in args:							self.msg(target, "Syntax: (./!)mzxwiki <thing to look up>, Purpose: Looks up the specified string on MZXWiki.")						elif "trope" in args:							self.msg(target, "Syntax: (./!)trope <thing to look up>, Purpose: Looks up the specified string on TVTropes. Title search only.")						elif "seen" in args:							self.msg(target, "Syntax: (./!)seen <Nickname>, Purpose: Ask if the bot has seen a nick. It will reply with the last time it saw them quit, part, or nickchange. Or if they're still here, it'll tell you that, and make you feel like an idiot.")						elif "dict" in args:							self.msg(target, "Syntax: (./!)dict <thing to look up>, Purpose: Look up a word in the WordNet dictionary.")						elif "foldoc" in args:							self.msg(target, "Syntax: (./!)foldoc <thing to look up>, Purpose: Look up a term in the Free On-Line Dictionary of Computing")						elif "jargon" in args:							self.msg(target, "Syntax: (./!)jargon <thing to look up>, Purpose: Look up some jargon in the Jargon File")						elif "help" in args:							self.msg(target, "You're using it, moron.")						elif "code" in args:							self.msg(target, "Syntax: (./!)code, Purpose: MSGs the nick with the code of the bot.")						else:							self.msg(target, "I don't appear to have that command implemented/documented!")					if command == "restart" and nick.lower() == "insidious":						self.msg(target, "Restarting.")						sys.exit(0)					if command == "quit" and nick.lower() == "insidious":						self.msg(target, "Quitting.")						sys.exit(1)					if command == "code":						binit = ""						f = open("mzxbot.py")						for line in f:							binit = binit + line						o = basicCURL()						c = pycurl.Curl()						c.setopt(c.URL, 'http://pastebin.ca/index.php')						c.setopt(c.WRITEFUNCTION, o.body_callback)						c.setopt(c.HTTPPOST, [("s", "Submit Post"), ("content", binit)])						c.setopt(c.HTTPHEADER, [ "Expect: " ])												c.perform()						c.close()						try:							url = o.contents.split('URL for it is:</p><p><a href="')[1]							url = url.split('">')[0]							self.msg(target, "http://pastebin.ca" + url)						except:							self.msg(target, "Unable to pastebin my code for some reason.")					if command == "whotoblame":						self.msg(target, "I was created by Insidious.")					if command == "version":						self.msg(target, "MZXBot v0.34");					if command == "wiki":						wiki = httplib.HTTPConnection("en.wikipedia.org")						wiki.request("GET", "/w/index.php?title=Special%3ASearch&search=" + args.replace(" ", "_") + '&go=Go')						response = wiki.getresponse()						if response.status == 302:							self.msg(target, response.msg['location'])						else: 							message = response.read()							if 'Search results' in message:								if 'No page text matches' in message and 'No article title matches' in message:									self.msg(target, "Can't find anything. Sorry.")								else: 									temp = message.split('<ul class=\'mw-search-results\'>\n<li><a href="')[1]									temp = temp.split('"')[0]									self.msg(target, "http://en.wikipedia.org" + temp)							else:								temp = message.split('<title>')[1]								temp = temp.split(' - Wikipedia,')[0]								self.msg(target, "http://en.wikipedia.org/en/wiki/" + temp.replace(" ", "_"))					if command == "trope":						wiki = httplib.HTTPConnection("tvtropes.org")						wiki.request("GET", "/pmwiki/pmwiki.php/Main/SearchWiki?text=" + args.replace(" ", "+"))						response = wiki.getresponse()						message = response.read()						if 'wasn\'t found in a title.' in message:							self.msg(target, "Can't find anything. Sorry. Try .tropebodysearch")						else: 							temp = message.split('</p><ul><li><a href="')[1]										temp = temp.split('"')[0]							self.msg(target, temp)					if command == "mzxwiki":						wiki = httplib.HTTPConnection("www.digitalmzx.net")						wiki.request("GET", "/wiki/index.php?search=" + args.replace(" ", "+"))						response = wiki.getresponse()						if response.status == 302:							self.msg(target, response.msg['location'])						else: 							message = response.read()							if 'Search results' in message:								if 'No page text matches' in message and 'No page title matches' in message:									self.msg(target, "Can't find anything. Sorry.")								else: 									temp = message.split('<ol start=\'1\'>\n<li><a href="')[1]									temp = temp.split('"')[0]									self.msg(target, "http://www.digitalmzx.net" + temp)											else:								self.msg(target, "Huh?")					if command == "seen":						self.raw_send("NAMES " + target)						self.seencheck = 1						self.seentarget = target						self.seenargs = args					if command == "dict":						if args != '':							o = basicCURL()							c = pycurl.Curl()							args = args.replace(" ","\%20")							c.setopt(c.URL, 'dict://dict.org/d:' + args + ':wn')							c.setopt(c.WRITEFUNCTION, o.body_callback)							c.perform()							c.close()							if '552 no match' in o.contents:								self.msg(target, "No match found.")							else:								definition = o.contents.split('wn "WordNet (r) 2.0"')[1]								definition = definition.split('250 ok')[0]								definition = definition.split('\n')								for i in definition:									p = re.compile('[a-zA-Z0-9_]+')									if p.search(i) != None:										self.msg(target, i)					if command == "jargon":						if args != '':							o = basicCURL()							c = pycurl.Curl()							args = args.replace(" ","\%20")							c.setopt(c.URL, 'dict://dict.org/d:' + args + ':jargon')							c.setopt(c.WRITEFUNCTION, o.body_callback)							c.perform()							c.close()							if '552 no match' in o.contents:								self.msg(target, "No match found.")							else:								definition = o.contents.split('jargon "Jargon File (4.3.1, 29 Jun 2001)"')[1]								definition = definition.split('250 ok')[0]								definition = definition.split('\n')								for i in definition:									p = re.compile('[a-zA-Z0-9_]+')									if p.search(i) != None:										self.msg(target, i)										if command == "yahtzee":						self.msg(target, "Yahtzee game started!")						self.yahtzee = 1						self.msg(target, "Type !join to join the game!")						self.msg(target, "Type !start to start!")					if command == "join" and self.yahtzee == 1:						self.msg(target, "You have joined the game.")						self.yplayers.append([nick, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1])						self.msg(target, "We currently have " + str(len(self.yplayers)) + " players queueing up to play Yahtzee! Type !join to join them!")											if command == "start" and self.yahtzee == 1:						self.yahtzee = 2						self.msg(target, "Ok! Yahtzee game starting... And the dice are being rolled!")						random.seed()						self.dice[0] = random.randint(1, 6)						self.dice[1] = random.randint(1, 6)						self.dice[2] = random.randint(1, 6)						self.dice[3] = random.randint(1, 6)						self.dice[4] = random.randint(1, 6)						self.keep = []						self.rolls = 1						self.msg(target, "Initial dice: " + str(self.dice[0]) + ", " + str(self.dice[1]) + ", " + str(self.dice[2]) + ", " + str(self.dice[3]) + ", " + str(self.dice[4]))						random.shuffle(self.yplayers)						self.pnum = 0						self.msg(target, self.yplayers[self.pnum][0] + ", you're up! use !1 through !6 to chose the dice to keep, then use !roll to roll again, !board to see the scoreboard, or !choose <category> to choose a score category")											if command == "1" and self.yahtzee == 2 and nick == self.yplayers[self.pnum][0]:						self.msg(target, "Keeping die 1, value " + str(self.dice[0]) + " for " + self.yplayers[self.pnum][0])						self.keep.append(self.dice[0])					if command == "2" and self.yahtzee == 2 and nick == self.yplayers[self.pnum][0]:						self.msg(target, "Keeping die 2, value " + str(self.dice[1]) + " for " + self.yplayers[self.pnum][0])						self.keep.append(self.dice[1])					if command == "3" and self.yahtzee == 2 and nick == self.yplayers[self.pnum][0]:						self.msg(target, "Keeping die 3, value " + str(self.dice[2]) + " for " + self.yplayers[self.pnum][0])						self.keep.append(self.dice[2])					if command == "4" and self.yahtzee == 2 and nick == self.yplayers[self.pnum][0]:						self.msg(target, "Keeping die 4, value " + str(self.dice[3]) + " for " + self.yplayers[self.pnum][0])						self.keep.append(self.dice[3])					if command == "5" and self.yahtzee == 2 and nick == self.yplayers[self.pnum][0]:						self.msg(target, "Keeping die 5, value " + str(self.dice[4]) + " for " + self.yplayers[self.pnum][0])						self.keep.append(self.dice[4])					if command == "roll" and self.yahtzee == 2 and nick == self.yplayers[self.pnum][0]:						if self.rolls >= 3:							self.msg(target, "No more rolls. Choose a category.")						else:							self.rolls = self.rolls + 1							random.seed()							self.dice[0] = random.randint(1, 6)							self.dice[1] = random.randint(1, 6)							self.dice[2] = random.randint(1, 6)							self.dice[3] = random.randint(1, 6)							self.dice[4] = random.randint(1, 6)							x = 0							for i in self.keep:								self.dice[x] = i								x = x + 1							self.keep = []							self.msg(target, "Next roll is: " + str(self.dice[0]) + ", " + str(self.dice[1]) + ", " + str(self.dice[2]) + ", " + str(self.dice[3]) + ", " + str(self.dice[4]))					if command == "board" and self.yahtzee == 2:						self.msg(target, "1. 1s: " + str(self.yplayers[self.pnum][2]).replace("-1", "-") + ", 2. 2s: " + str(self.yplayers[self.pnum][3]).replace("-1", "-") + ", 3. 3s: " + str(self.yplayers[self.pnum][4]).replace("-1", "-") + ", 4. 4s: " + str(self.yplayers[self.pnum][5]).replace("-1", "-") + ", 5. 5s: " + str(self.yplayers[self.pnum][6]).replace("-1", "-") + ", 6. 6s: " + str(self.yplayers[self.pnum][7]).replace("-1", "-") + ", 7. 3 of a kind: " + str(self.yplayers[self.pnum][8]).replace("-1", "-") + ", 8. 4 of a kind: " + str(self.yplayers[self.pnum][9]).replace("-1", "-") + ", 9. Full house: " + str(self.yplayers[self.pnum][10]).replace("-1", "-") + ", 10. Small straight: " + str(self.yplayers[self.pnum][11]).replace("-1", "-") + ", 11. Large straight: " + str(self.yplayers[self.pnum][12]).replace("-1", "-") + ", 12. Yahtzee: " + str(self.yplayers[self.pnum][13]).replace("-1", "-") + ", 13. Chance: " + str(self.yplayers[self.pnum][14]).replace("-1", "-"));					if command == "choose" and self.yahtzee == 2 and nick == self.yplayers[self.pnum][0]:						if args == "13":							if self.yplayers[self.pnum][14] == -1:								x = 0								for i in self.dice:									x += i								self.msg(target, "Chance. Score +" + str(x) + ". The next player will be...")								self.yplayers[self.pnum][14] = x								self.pnum = self.pnum + 1								if self.pnum > (len(self.yplayers) - 1):									self.pnum = 0								self.msg(target, self.yplayers[self.pnum][0] + "!!! Please use !roll to begin.")								self.dice = [0, 0, 0, 0, 0]								self.keep = []								self.rolls = 0														if args == "12":							if self.yplayers[self.pnum][13] != 0:								yes = 0								for i in range(1, 6):									if self.dice.count(i) == 5:										yes = 1								if yes == 1:									self.msg(target, "YAHTZEE! Score +50. The next player will be...")									self.yplayers[self.pnum][13] += 50									self.pnum = self.pnum + 1									if self.pnum > (len(self.yplayers) - 1):										self.pnum = 0									self.msg(target, self.yplayers[self.pnum][0] + "!!! Please use !roll to begin.")									self.dice = [0, 0, 0, 0, 0]									self.keep = []									self.rolls = 0								else:									self.msg(target, "No YAHTZEE. Score remains the same, YAHTZEE place is used. The next player will be...")									self.yplayers[self.pnum][13] = 0									self.pnum = self.pnum + 1									if self.pnum > (len(self.yplayers) - 1):										self.pnum = 0									self.msg(target, self.yplayers[self.pnum][0] + "!!! Please use !roll to begin.")									self.dice = [0, 0, 0, 0, 0]									self.keep = []									self.rolls = 0					if command == "foldoc":						if args != '':							o = basicCURL()							c = pycurl.Curl()							args = args.replace(" ","\%20")							c.setopt(c.URL, 'dict://dict.org/d:' + args + ':foldoc')							c.setopt(c.WRITEFUNCTION, o.body_callback)							c.perform()							c.close()							if '552 no match' in o.contents:								self.msg(target, "No match found.")							else:								definition = o.contents.split('foldoc "The Free On-line Dictionary of Computing (27 SEP 03)"')[1]								definition = definition.split('250 ok')[0]								definition = definition.split('\n')								for i in definition:									p = re.compile('[a-zA-Z0-9_]+')									if p.search(i) != None:										self.msg(target, i)												elif target[:1] != '#':				self.msg(target, "Sorry, I don't accept private messages. Please send commands in channel only.")	def on_motd_end(self, input):		self.join('#testmzxbot')		self.join('#mzx')	def on_channel_names(self, channel, ulist):		if self.seencheck == 1:			if channel == self.seentarget:				try:					ulist.index(self.seenargs)					self.msg(self.seentarget, "They're right here!")				except:					try:						ulist.index("@" + self.seenargs)						self.msg(self.seentarget, "They're right here!")					except: 						try:							ulist.index("+" + self.seenargs)							self.msg(self.seentarget, "They're right here!")						except:							try: 														index = self.leftseen.index(self.seenargs)								if self.departures[index][0] == "Quit":									self.msg(self.seentarget, self.seenargs + " quit at " + self.departures[index][1] + " saying " + self.departures[index][2])								if self.departures[index][0] == "Part":									self.msg(self.seentarget, self.seenargs + " left " + self.departures[index][3] + " at " + self.departures[index][1] + " saying " + self.departures[index][2])								if self.departures[index][0] == "Nick":									self.msg(self.seentarget, self.seenargs + " changed their nick to " + self.departures[index][2] + " at " + self.departures[index][1])							except:								self.msg(self.seentarget, "No, I haven't seen " + self.seenargs)			self.seencheck = 0	def on_quit(self, nick, ident, host, msg):		self.leftseen.insert(0, nick)		curtime = asctime()		self.departures.insert(0, ["Quit", curtime, msg])		def on_part(self, nick, ident, host, channel, msg):		self.leftseen.insert(0, nick)		curtime = asctime()		if msg == None:			msg = "Nothing"		if msg == "":			msg = "Nothing"		self.departures.insert(0, ["Part", curtime, msg, channel])	def on_nick(self, oldnick, ident, host, newnick):		self.leftseen.insert(0, oldnick)		curtime = asctime()		self.departures.insert(0, ["Nick", curtime, newnick])bot = mzxbot('irc.esper.net', 'mzxbot', ident='mzxbot', realname='MZXBot Test')bot.connect()