From 0086bc8bd745670a758953836684213dd4037e9e Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Sun, 15 Mar 2009 00:51:39 -0600 Subject: [PATCH] adding hash, bf, pyexec, twitter --- bot.py | 27 +++++++---- irc.py | 2 +- plugins/bf.py | 113 ++++++++++++++++++++++++++++++++++++++++++++ plugins/bf.pyc | Bin 0 -> 2578 bytes plugins/hash.py | 14 ++++++ plugins/hash.pyc | Bin 0 -> 866 bytes plugins/misc.py | 8 ++++ plugins/misc.pyc | Bin 0 -> 426 bytes plugins/pyexec.py | 16 +++++++ plugins/pyexec.pyc | Bin 0 -> 689 bytes plugins/twitter.py | 2 +- plugins/twitter.pyc | Bin 1097 -> 1097 bytes 12 files changed, 170 insertions(+), 12 deletions(-) create mode 100644 plugins/bf.py create mode 100644 plugins/bf.pyc create mode 100644 plugins/hash.py create mode 100644 plugins/hash.pyc create mode 100644 plugins/misc.py create mode 100644 plugins/misc.pyc create mode 100644 plugins/pyexec.py create mode 100644 plugins/pyexec.pyc diff --git a/bot.py b/bot.py index a6c05d4..dc7f993 100755 --- a/bot.py +++ b/bot.py @@ -1,8 +1,8 @@ #!/usr/bin/python -network = "localhost" -nickname = "skybot" -channel = "#skybot" +network = "irc.synirc.net" +nick = "skybot" +channel = "#cobol" import sys import os @@ -24,12 +24,13 @@ class Bot(object): self.filters = [] #fn, name, func bot = Bot() -bot.nickname = nickname +bot.nick = nick bot.channel = channel bot.network = network print 'Loading plugins' -magic_re = re.compile(r'^\s*#(command|filter)(?:: +(\S+) *(\S.*)?)?\s*$') +typs = '|'.join('command filter event'.split()) +magic_re = re.compile(r'^\s*#(%s)(?:: +(\S+) *(\S.*)?)?\s*$' % typs) def reload_plugins(mtime=[0]): new_mtime = os.stat('plugins') @@ -65,8 +66,12 @@ def reload_plugins(mtime=[0]): if typ == 'command': args = {'name': nam, 'hook': nam + rest} bot.commands.append((filename, nam, func, args)) - if typ == 'filter': + elif typ == 'filter': bot.filters.append((filename, nam, func)) + elif typ == 'event': + args = {'name': nam, 'prefix':False, + 'events': [nam] + rest.split()} + bot.commands.append((filename, nam, func, args)) except Exception, e: print e @@ -75,9 +80,9 @@ def reload_plugins(mtime=[0]): reload_plugins() print 'Connecting to IRC' -bot.irc = irc.irc(network, nickname) +bot.irc = irc.irc(network, nick) bot.irc.join(channel) -bot.commandprefix = '^(?:\.|'+nickname+'[:,]*\s*)' +bot.commandprefix = '^(?:\.|'+nick+'[:,]*\s*)' print 'Running main loop' @@ -93,7 +98,6 @@ class Input(object): self.host = host self.paraml = paraml self.msg = msg - self.doreply = True class FakeBot(object): def __init__(self, bot, input, fn, func): @@ -101,11 +105,13 @@ class FakeBot(object): self.input = input self.msg = bot.irc.msg self.cmd = bot.irc.cmd + self.join = bot.irc.join self.fn = func self.func = func + self.doreply = True if input.command == "PRIVMSG": self.chan = input.paraml[0] - + def say(self, msg): self.bot.irc.msg(self.input.paraml[0], msg) @@ -135,6 +141,7 @@ while True: break if input == None: continue + print '<<<', input.raw thread.start_new_thread(FakeBot(bot, input, fn, func).run, ()) except Queue.Empty: pass diff --git a/irc.py b/irc.py index aad2ddb..0a35bc0 100644 --- a/irc.py +++ b/irc.py @@ -26,6 +26,7 @@ class crlf_tcp(asynchat.async_chat): self.oqueue = queue() #where we stick things that need to be sent self.iqueue = queue() #where we stick things that were received self.create_socket(socket.AF_INET, socket.SOCK_STREAM) + self.socket.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 0) self.host = host self.port = port @@ -72,7 +73,6 @@ class irc(object): def parse_loop(self): while True: msg = self.conn.iqueue.get() - print '>>>', msg if msg.startswith(":"): #has a prefix prefix, command, params = irc_prefix_re.match(msg).groups() else: diff --git a/plugins/bf.py b/plugins/bf.py new file mode 100644 index 0000000..b83ab68 --- /dev/null +++ b/plugins/bf.py @@ -0,0 +1,113 @@ +'''brainfuck interpreter adapted from (public domain) code at +http://brainfuck.sourceforge.net/brain.py''' + +import re +import random + +BUFFER_SIZE = 5000 + +#command +def bf(bot, inp, input=None, max_steps=1000000, no_input=0): + """Runs a Brainfuck program given as a string. + + The string must contain nothing but "<>[]+-.,", i.e. + be already filtered. + + If 'input' is None, stdin is used. 'input' can be + a string or a tuple; tuples are only used if + extended_interpretation is non-zero, and should + be more or less obvious in that case. + + if max_steps is < 0, there is no restriction.""" + program = re.sub('[^][<>+-.,]', '', inp.inp) + + # create a dict of brackets pairs, for speed later on + parens={} + open_parens=[] + for pos in range(len(program)): + if program[pos] == '[': + open_parens.append(pos) + elif program[pos] == ']': + if len(open_parens) > 0: + parens[pos] = open_parens[-1] + parens[open_parens[-1]] = pos + open_parens.pop() + else: + return 'unbalanced brackets' +# if len(open_parens) != 0: +# return 'unbalanced brackets' + # now we can start interpreting + pc = 0 # program counter + mp = 0 # memory pointer + steps = 0 + memory = [0] * BUFFER_SIZE #initial memory area + rightmost = 0 + if input != None: + if type(input) == type(()): + # we'll only be using input[0] right now + inputs, input = input, input[0] + input_pointer = 0 + + output = "" #we'll save the output here + + if no_input: + eof_reached = 1 + else: + eof_reached = 0 + + # the main program loop: + while pc < len(program): + c = program[pc] + if c == '+': + memory[mp] = memory[mp] + 1 % 256 + elif c == '-': + memory[mp] = memory[mp] - 1 % 256 + elif c == '>': + mp = mp + 1 + if mp > rightmost: + rightmost = mp + if mp >= len(memory): + memory = memory + [0]*BUFFER_SIZE # no restriction on memory growth! + elif c == '<': + mp = mp - 1 % len(memory) + elif c == '.': + output += chr(memory[mp]) + if len(output) > 500: + break + + elif program[pc] == ',': + if eof_reached: + raise Exception, "Program tries reading past EOF" + if input == None: + #char = sys.stdin.read(1) + char = chr(random.randint(1,255)) + if char == '': # EOF + memory[mp] = 0 + eof_reached = 1 + else: + memory[mp] = ord(char) + else: + if input_pointer == len(input): # EOF + memory[mp] = 0 + eof_reached = 1 + else: + memory[mp] = ord(input[input_pointer]) + input_pointer = input_pointer + 1 + + elif program[pc] == '[': + if memory[mp] == 0: + pc = parens[pc] + + elif program[pc] == ']': + if memory[mp] != 0: + pc = parens[pc] + + pc += 1 + steps += 1 + if max_steps >= 0 and steps > max_steps: + output += "Maximum number of steps exceeded" + break + + # end of while loop + + return unicode(re.sub('[\r\n\x00]', '/', output), 'iso-8859-1')[:400] diff --git a/plugins/bf.pyc b/plugins/bf.pyc new file mode 100644 index 0000000000000000000000000000000000000000..0a07a4306cee2f224fe70a51fea7afe283b50fa9 GIT binary patch literal 2578 zcmZ`*-ESL35T83IcH-FiZazvut0K`r6Q@P08j2NFEveK8N|k^rB?{L0Zf##`-(K$a zss!0Q5F~g&2+E&=5HI{oyr4cJmEX)=5&`NtJ2yK!-!nIxU;nCCw|@BJu}{TQ#``{2 zdJiBV+6QzfaOt^2`#^HsrG1xz6784xC>Wz)oPsia3kndI_SPz_1M<{PyfEry{&N*Z zRwr?y0hJecvDJa*EE{o{cwU-5%`fyW{#X;g+if$$2q}%|)#|X!%9>b=RXBgEO;%W@GlI6?$O$U6$6B$TL4GvJT1HiYQw+Nf*$OR= zYC@{h5XD%bqY{}dn4aW4Oalp48DX!lzyVhn$7$6aP1DGw2;Q^inH+OzG)qG`TLpQ7 zMspNZa)voN`Pm&#UgqYA$c$VGdy-a1^E8~NU{VY%!BCBcRpjd`E$3)6ngughrvA(F z7OkAK{OJfwO;|pK76d#&%ZMA;ur6u*P{<=hToq?(65eKYT_lG^W~Hh#;?6iZPsPG*2%`m% zi(pn1{0;?kWa=mfDmF*oC?1vd~N)Y}?`bq+I_r!y(qXn}?c{4KGsi<15#L3v4z z-zzzGnt9+;al$SPXT&?v@g`BQu_)SLhE^}dv6Doj4MMnG#F01qyC4+RL?gqcX%Nu2 zT=hwk*za7*86G3!)lisl|ATEasGojO~F_wKJ=(H@6QCZ*DZ!udl1Hp>yfBR$f2R zUT~^9VIN~p2l>H;>c<^*BaGtA-cVtxo|s6l!&Cs}*k&pCFV1~0Qf(~`o%^T^2C1@H z+}9uHFZiEmWupG6NUN|T0{X=2D9}OcVg!3OG-4z&(Z*LgG3&~U0+sem)(=Dm&JPSL zOH^N{sWR;sp~+HAab7V&ep?RFnYzTiLW(Mney#&P_NRV*x5|0*oUdv*xI`D(Z=oYCzyS*1{z;C1z4K}W?cYWf+f7t ztSxV}fMtD&7j^TKzK$$k#a5jw7$bz}!55Dn?d-Pp9`Em948aPzMi6@%=fZw3 z!N@1tfv+(qTvS0hE?8U_Vh3ScQEQSwXjv5Uy}QOYrW~}5-L3J4J(DICr59DU6QD;1 zJ%#=i6~)IS)?zebl#0U!A+(A;M;)6~k`Vjy8sxD{dPQ`g(Rim~ED5_kI{?2Nh^5rl z&;4n|MjBcUO=j5-&X6{p7AouabRfm?B|U!6OQawIQq~gpvu+rro9zz%i4bSn9j;^g zBLLM_N@b_)EKu2*liz|f%|Ev`#`LmNm$m9%ajI_FtuQQ=Xu+xCWT{w;Lx=M=-YZy( zz$e`~rvkk0GNlT+MVF=UhIY0!UqH~Dt9&xH#@!%UZnXmAw_3aG=aD* literal 0 HcmV?d00001 diff --git a/plugins/hash.py b/plugins/hash.py new file mode 100644 index 0000000..9b69126 --- /dev/null +++ b/plugins/hash.py @@ -0,0 +1,14 @@ +import hashlib + +#command +def md5(bot, input): + return hashlib.md5(input.inp).hexdigest() + +#command +def sha1(bot, input): + return hashlib.sha1(input.inp).hexdigest() + +#command +def hash(bot, input): + return ', '.join(x + ": " + getattr(hashlib, x)(input.inp).hexdigest() + for x in 'md5 sha1 sha256'.split()) diff --git a/plugins/hash.pyc b/plugins/hash.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3a5685e589167df296da4033b8cabf7711fdd805 GIT binary patch literal 866 zcmbVKO-lnY5S{GSYDEzQ^;+;!Q0f;7iin^WFFmZFAb4oIYa6ZG)ud2CPZjky_&2=z ztNa7{-c+fSg5Z)&W|NsWllSuWF+Dl=`hHZG?=Qf*kLi|RVhkf!B4=;{d4yvm4%G=H z&Z!eJ=D0M5c>9U3QYXgGJRsIQjK{H&dm}cG9&9K*L7Kr)rIcg9I83wZnk`%NA^huj zQ&*d2zQD7j8{0NITXRtQ5C+vjeuqSc|1~HD6 zxQe1I7wfzs8)5?+m~I=UyDs-4=S%XS?8hPtkizRvFOV0=@=K6~6LXW7Q&p5u3cXYO zrBEaBRqt<=hoS3xW|UWfjk2ojfQwO*II6naNYbR6-R@2Qlq(nwhWtebj%n66xB8-! zpv$&RRdtuOZ2vI~6VDE`_NcO|9$&>2D-u!6tWhfax6*7%6ft5TrFx?f9ktxOZpWQg Pvd3)4?5VJjE9AZap}2$0 literal 0 HcmV?d00001 diff --git a/plugins/misc.py b/plugins/misc.py new file mode 100644 index 0000000..6b18de3 --- /dev/null +++ b/plugins/misc.py @@ -0,0 +1,8 @@ +#event: KICK INVITE +def rejoin(bot, input): + if input.command == 'KICK': + if input.paraml[1] == bot.bot.nick: + bot.join(input.paraml[0]) + + if input.command == 'INVITE': + bot.join(input.inp) diff --git a/plugins/misc.pyc b/plugins/misc.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9e5b463ae1a4b954e707b736c2909c229ee487da GIT binary patch literal 426 zcmZ`#yH3ME5S+Cg9zhW)CL5K;t^70E?{DCmqGp$qoeBQU|V ziqKv_v=>wnCL6#+Ffmj)roui6e(EtMZvuI_g;<{3lq7hM{(1*cpL)2_A{}sK?i2DX zo6b0ip#=O6Da-G(yPH%?ze_6HrYUR{w60heP3?8}$JU1gYs%+PtXgA(Vr&U#AHq7>eLl5Xe z4L^o?-P+PEZFxB7HtuAZ3upeHWF+i)P3MDT_a9( zbiR&>VQGC7^=qB2g0CH?_W+OEiG8o6t1I5>WAIXFgd|%wl4ykMBr&by0iC@EW)`oO@V4{olt&hNtf aOY@&xJcROi)l^+@&kZo^c*e+bFaHC~8jV^2 literal 0 HcmV?d00001 diff --git a/plugins/twitter.py b/plugins/twitter.py index 2b83896..63d8732 100644 --- a/plugins/twitter.py +++ b/plugins/twitter.py @@ -20,4 +20,4 @@ def twitter(bot, input): return "can't find that username" tweet = tweet.find('status') - bot.say(': '.join(tweet.find(x).text for x in 'user/name created_at text'.split())) + bot.say(': '.join(tweet.find(x).text for x in 'created_at user/name text'.split())) diff --git a/plugins/twitter.pyc b/plugins/twitter.pyc index 39609361b7fb6d928cd43999209e5195ef810861..56b1b59070a6a164da07bc842f8276672487e6df 100644 GIT binary patch delta 41 xcmX@fagu{=^Cw;|mzf*cE;ETF7o{eaq^86tmMD}KrxxkwCFZ7Xe#;cY2mneR51ar1 delta 41 xcmX@fagu{=^Cw=eirS5AmzhLLi&KmA^AdAY6_Sfm6H8K4;uA|Yzh#PH1OQA253~RP