Convert to python3 and hook up to o3-base

This commit is contained in:
Juhani Krekelä 2021-01-20 20:00:50 +02:00
parent 3c41450b21
commit 86b0d4e337
3 changed files with 114 additions and 118 deletions

View File

@ -1,9 +1,11 @@
import o2_botcmd
# initialize(*, config) # initialize(*, config)
# Called to initialize the IRC bot # Called to initialize the IRC bot
# Runs before even logger is brought up, and blocks further bringup until it's done # Runs before even logger is brought up, and blocks further bringup until it's done
# config is a configpatser.ConfigParser object containig contents of bot.conf # config is a configpatser.ConfigParser object containig contents of bot.conf
def initialize(*, config): def initialize(*, config):
... o2_botcmd.init()
# on_connect(*, irc) # on_connect(*, irc)
# Called after IRC bot has connected and sent the USER/NICk commands but not yet attempted anything else # Called after IRC bot has connected and sent the USER/NICk commands but not yet attempted anything else
@ -11,14 +13,14 @@ def initialize(*, config):
# Blocks the bot until it's done, including PING/PONG handling # Blocks the bot until it's done, including PING/PONG handling
# irc is the IRC API object # irc is the IRC API object
def on_connect(*, irc): def on_connect(*, irc):
... pass
# on_quit(*, irc) # on_quit(*, irc)
# Called just before IRC bot sends QUIT # Called just before IRC bot sends QUIT
# Blocks the bot until it's done, including PING/PONG handling # Blocks the bot until it's done, including PING/PONG handling
# irc is the IRC API object # irc is the IRC API object
def on_quit(*, irc): def on_quit(*, irc):
... o2_botcmd.cron.ctrl('QUIT')
# handle_message(*, prefix, message, nick, channel, irc) # handle_message(*, prefix, message, nick, channel, irc)
# Called for PRIVMSGs. # Called for PRIVMSGs.
@ -29,7 +31,7 @@ def on_quit(*, irc):
# irc is the IRC API object # irc is the IRC API object
# All strings are bytestrings # All strings are bytestrings
def handle_message(*, prefix, message, nick, channel, irc): def handle_message(*, prefix, message, nick, channel, irc):
... o2_botcmd.parse(nick, channel, b'PRIVMSG', [channel, message], irc)
# handle_nonmessage(*, prefix, command, arguments, irc) # handle_nonmessage(*, prefix, command, arguments, irc)
# Called for all other commands than PINGs and PRIVMSGs. # Called for all other commands than PINGs and PRIVMSGs.
@ -39,4 +41,4 @@ def handle_message(*, prefix, message, nick, channel, irc):
# irc is the IRC API object # irc is the IRC API object
# All strings are bytestrings # All strings are bytestrings
def handle_nonmessage(*, prefix, command, arguments, irc): def handle_nonmessage(*, prefix, command, arguments, irc):
... o2_botcmd.parse(prefix.split(b'!')[0], None, command, arguments, irc)

View File

@ -4,29 +4,29 @@
# a cheezy little Eliza knock-off by Joe Strout <joe@strout.net> # a cheezy little Eliza knock-off by Joe Strout <joe@strout.net>
# with some updates by Jeff Epler <jepler@inetnebr.com> # with some updates by Jeff Epler <jepler@inetnebr.com>
# hacked into a module and updated by Jez Higgins <jez@jezuk.co.uk> # hacked into a module and updated by Jez Higgins <jez@jezuk.co.uk>
# last revised: 28 February 2005 # converted to python3 by nortti
# last revised: 20 January 2021
#---------------------------------------------------------------------- #----------------------------------------------------------------------
import string
import re import re
import random import random
class eliza: class eliza:
def __init__(self): def __init__(self):
self.keys = map(lambda x:re.compile(x[0], re.IGNORECASE),gPats) self.keys = list(map(lambda x:re.compile(x[0], re.IGNORECASE),gPats))
self.values = map(lambda x:x[1],gPats) self.values = list(map(lambda x:x[1],gPats))
#---------------------------------------------------------------------- #----------------------------------------------------------------------
# translate: take a string, replace any words found in dict.keys() # translate: take a string, replace any words found in dict.keys()
# with the corresponding dict.values() # with the corresponding dict.values()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def translate(self,str,dict): def translate(self,str,dict):
words = string.split(string.lower(str)) words = str.lower().split()
keys = dict.keys(); keys = dict.keys();
for i in range(0,len(words)): for i in range(0,len(words)):
if words[i] in keys: if words[i] in keys:
words[i] = dict[words[i]] words[i] = dict[words[i]]
return string.join(words) return ' '.join(words)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
# respond: take a string, a set of regexps, and a corresponding # respond: take a string, a set of regexps, and a corresponding
@ -42,13 +42,13 @@ class eliza:
# chosen randomly from among the available options # chosen randomly from among the available options
resp = random.choice(self.values[i]) resp = random.choice(self.values[i])
# we've got a response... stuff in reflected text where indicated # we've got a response... stuff in reflected text where indicated
pos = string.find(resp,'%') pos = resp.find('%')
while pos > -1: while pos > -1:
num = string.atoi(resp[pos+1:pos+2]) num = int(resp[pos+1:pos+2])
resp = resp[:pos] + \ resp = resp[:pos] + \
self.translate(match.group(num),gReflections) + \ self.translate(match.group(num),gReflections) + \
resp[pos+2:] resp[pos+2:]
pos = string.find(resp,'%') pos = resp.find('%')
# fix munged punctuation at the end # fix munged punctuation at the end
if resp[-2:] == '?.': resp = resp[:-2] + '.' if resp[-2:] == '?.': resp = resp[:-2] + '.'
if resp[-2:] == '??': resp = resp[:-2] + '?' if resp[-2:] == '??': resp = resp[:-2] + '?'
@ -291,20 +291,20 @@ gPats = [
# command_interface # command_interface
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def command_interface(): def command_interface():
print "Therapist\n---------" print("Therapist\n---------")
print "Talk to the program by typing in plain English, using normal upper-" print("Talk to the program by typing in plain English, using normal upper-")
print 'and lower-case letters and punctuation. Enter "quit" when done.' print('and lower-case letters and punctuation. Enter "quit" when done.')
print '='*72 print('='*72)
print "Hello. How are you feeling today?" print("Hello. How are you feeling today?")
s = "" s = ""
therapist = eliza(); therapist = eliza();
while s != "quit": while s != "quit":
try: s = raw_input(">") try: s = input(">")
except EOFError: except EOFError:
s = "quit" s = "quit"
print s print(s)
while s[-1] in "!.": s = s[:-1] while s[-1] in "!.": s = s[:-1]
print therapist.respond(s) print(therapist.respond(s))
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -3,8 +3,7 @@ import threading
import random import random
import re import re
import time import time
import functools
concmd=['/q', '/lt', '/st', '/lg', '/lm', '/sm']
blacklist = [] blacklist = []
@ -58,9 +57,9 @@ class Cron(threading.Thread):
self.cronctrllock.release() self.cronctrllock.release()
self.timedjobslock.acquire() self.timedjobslock.acquire()
self.timedjobs = map((lambda (time, fn): (time-1, fn)), self.timedjobs) self.timedjobs = list(map((lambda time_fn: (time_fn[0]-1, time_fn[1])), self.timedjobs))
torun = map((lambda (time, fn): fn), filter((lambda (time, fn): time<=0), self.timedjobs)) torun = list(map((lambda time_fn: time_fn[1]), filter((lambda time_fn: time_fn[0]<=0), self.timedjobs)))
self.timedjobs = filter((lambda (time, fn): time>0), self.timedjobs) self.timedjobs = list(filter((lambda time_fn: time_fn[0]>0), self.timedjobs))
self.timedjobslock.release() self.timedjobslock.release()
for fn in torun: for fn in torun:
@ -182,23 +181,26 @@ def init():
loadgods() loadgods()
def chmode(irc, chan, nick, mode, args): def chmode(irc, chan, nick, mode, args):
set_unset = mode[0] if type(nick) != str: nick = nick.decode()
mode = mode[1:] set_unset = mode[0].encode()
mode = mode[1:].encode()
if isauthorized(irc, chan, nick): if isauthorized(irc, chan, nick):
if args == ['']: if args == ['']:
irc.send('MODE %s %s %s' % (chan, set_unset+mode, nick)) irc.send_raw(b'MODE %s %s %s' % (chan, set_unset+mode, nick.encode()))
else: else:
nicks = [] nicks = []
for nick in args: for nick in args:
nicks.append(nick) nicks.append(nick.encode())
if len(nicks) == 4: if len(nicks) == 4:
irc.send('MODE %s %s %s' % (chan, set_unset+mode*4, ' '.join(nicks))) irc.send_raw(b'MODE %s %s %s' % (chan, set_unset+mode*4, b' '.join(nicks)))
nicks = [] nicks = []
if nicks: if nicks:
irc.send('MODE %s %s %s' % (chan, set_unset+mode*len(nicks), ' '.join(nicks))) irc.send_raw(b'MODE %s %s %s' % (chan, set_unset+mode*len(nicks), b' '.join(nicks)))
def istrusted(chan, account): def istrusted(chan, account):
if type(chan) != str: chan = chan.decode()
if type(account) != str: account = account.decode()
trustedlock.acquire() trustedlock.acquire()
if chan in trusted and account in trusted[chan]: if chan in trusted and account in trusted[chan]:
trustedlock.release() trustedlock.release()
@ -262,18 +264,21 @@ def removeaccountcheck(id):
def getaccountcheckidbynick(nick): def getaccountcheckidbynick(nick):
global accountcheck, accountchecklock global accountcheck, accountchecklock
if type(nick) != str: nick = nick.decode()
accountchecklock.acquire() accountchecklock.acquire()
getid = lambda (id, nick, account): id getid = lambda id_nick_account: id_nick_account[0]
filterbynick = lambda (id, cknick, account): cknick == nick filterbynick = lambda id_cknick_account: id_cknick_account[1] == nick
ids = map(getid, filter(filterbynick, accountcheck)) ids = list(map(getid, filter(filterbynick, accountcheck)))
accountchecklock.release() accountchecklock.release()
return ids return ids
def getaccount(irc, nick): def getaccount(irc, nick):
if type(nick) != str: nick = nick.decode()
id = initaccountcheck(nick) id = initaccountcheck(nick)
irc.send('WHOIS ' + nick) irc.send_raw(b'WHOIS ' + nick.encode())
cron.queuejob(5, (lambda : setaccountcheckvalue(id, ''))) cron.queuejob(5, (lambda : setaccountcheckvalue(id, '')))
account = None account = None
@ -288,11 +293,12 @@ def getaccount(irc, nick):
return account return account
def isauthorized(irc, chan, nick): def isauthorized(irc, chan, nick):
if type(nick) != str: nick = nick.decode()
account = getaccount(irc, nick) account = getaccount(irc, nick)
if account: if account:
return istrusted(chan, account) return istrusted(chan, account)
else: else:
irc.msg(nick, zwsp + 'Identify with NickServ') irc.bot_response(nick.encode(), 'Identify with NickServ')
class ArgsfmtError(Exception): class ArgsfmtError(Exception):
def __init__(self, msg): def __init__(self, msg):
@ -363,7 +369,7 @@ def parsecmd(line, args):
if len(argtypes) >= 1 and ARG_UNL in argtypes[:-1]: # Disallow non-final unlimited arguments if len(argtypes) >= 1 and ARG_UNL in argtypes[:-1]: # Disallow non-final unlimited arguments
raise ArgsfmtError('Non-final unlimited argument') raise ArgsfmtError('Non-final unlimited argument')
if len(filter((lambda type: type == ARG_OPT or type == ARG_UNL), argtypes)) > 1: # Disallow more than one optional or unlimited argument per argument string if len(list(filter((lambda type: type == ARG_OPT or type == ARG_UNL), argtypes))) > 1: # Disallow more than one optional or unlimited argument per argument string
raise ArgsfmtError('Ambiguous argument format') raise ArgsfmtError('Ambiguous argument format')
# Remove the command # Remove the command
@ -408,27 +414,23 @@ def parsecmd(line, args):
else: else:
return out return out
def parse((line, irc)): def parse(nick, chan, command, arguments, irc):
global blacklist global blacklist
global msgs, msgslock global msgs, msgslock
global trusted, trustedlock, gods, godslock global trusted, trustedlock, gods, godslock
global doctor, die_expr global doctor, die_expr
line = line.split(' ') zwsp = '\u200b'
nick = line[0].split('!')[0][1:]
chan = line[2] if line[2][0] == '#' else nick
zwsp = '\xe2\x80\x8b'
if nick in blacklist: if nick in blacklist:
return return
elif len(line) >= 4 and len(line[3]) >= len(zwsp)+1 and line[3][:len(zwsp)+1] == ':'+zwsp: # If line begins with ZWSP elif len(arguments) >= 2 and len(arguments[1]) >= len(zwsp.encode('utf-8')) and arguments[1][:len(zwsp.encode('utf-8'))] == zwsp.encode('utf-8'): # If line begins with ZWSP
return return
if line[1]=='PRIVMSG' and line[3][:2] != ': ': if command==b'PRIVMSG' and arguments[1][:1] != b' ':
reply = chan reply = chan
cmdline = [line[3][1:]] + line[4:] cmdline = arguments[1].decode('utf-8').split(' ')
while '' in cmdline: while '' in cmdline:
cmdline.remove('') cmdline.remove('')
@ -439,14 +441,14 @@ def parse((line, irc)):
newchan, newcmdline = parsecmd(cmdline, 'channel {command}') newchan, newcmdline = parsecmd(cmdline, 'channel {command}')
newcmdline = newcmdline.split(' ') newcmdline = newcmdline.split(' ')
if isauthorized(irc, newchan, nick): if isauthorized(irc, newchan, nick):
chan = newchan chan = newchan.encode()
cmdline = newcmdline cmdline = newcmdline
else: else:
irc.msg(chan, zwsp + 'Usage #chan channel command') irc.bot_response(chan, usage('#chan'))
if matchcmd(cmdline, '#echo'): if matchcmd(cmdline, '#echo'):
text = parsecmd(cmdline, '{text}') text = parsecmd(cmdline, '{text}')
irc.msg(reply, zwsp+text) irc.bot_response(reply, text)
elif matchcmd(cmdline, '#op'): elif matchcmd(cmdline, '#op'):
args = parsecmd(cmdline, '{args}') args = parsecmd(cmdline, '{args}')
chmode(irc, chan, nick, '+o', args.split(' ')) chmode(irc, chan, nick, '+o', args.split(' '))
@ -457,57 +459,63 @@ def parse((line, irc)):
args = parsecmd(cmdline, '{args}') args = parsecmd(cmdline, '{args}')
chmode(irc, chan, nick, '+v', args.split(' ')) chmode(irc, chan, nick, '+v', args.split(' '))
elif matchcmd(cmdline, '#quiet'): elif matchcmd(cmdline, '#quiet'):
arg = parsecmd(cmdline, 'nick') if matchcmd(cmdline, '#quiet', 'nick'):
chmode(irc, chan, nick, '+q', [arg + '!*@*']) arg = parsecmd(cmdline, 'nick')
chmode(irc, chan, nick, '+q', [arg + '!*@*'])
else:
irc.bot_response(reply, usage('#quiet'))
elif matchcmd(cmdline, '#dequiet'): elif matchcmd(cmdline, '#dequiet'):
arg = parsecmd(cmdline, 'nick') if matchcmd(cmdline, '#dequiet', 'nick'):
chmode(irc, chan, nick, '-q', [arg + '!*@*']) arg = parsecmd(cmdline, 'nick')
chmode(irc, chan, nick, '-q', [arg + '!*@*'])
else:
irc.bot_response(reply, usage('#dequiet'))
elif matchcmd(cmdline, '#devoice'): elif matchcmd(cmdline, '#devoice'):
args = parsecmd(cmdline, '{args}') args = parsecmd(cmdline, '{args}')
chmode(irc, chan, nick, '-v', args.split(' ')) chmode(irc, chan, nick, '-v', args.split(' '))
elif matchcmd(cmdline, '#kick'): elif matchcmd(cmdline, '#kick'):
if matchcmd(cmdline, '#kick', 'nick {reason}'): if matchcmd(cmdline, '#kick', 'nick {reason}'):
kicknick, kickreason = parsecmd(cmdline, 'nick {reason}') kicknick, kickreason = parsecmd(cmdline, 'nick {reason}')
if kicknick.lower() == irc.nick: if kicknick.lower() == irc.get_nick().decode('utf-8'):
irc.send('KICK %s %s :Fuck you' % (chan, nick)) irc.send_raw(b'KICK %s %s :Fuck you' % (chan, nick))
else: else:
if isauthorized(irc, chan, nick): if isauthorized(irc, chan, nick):
irc.send('KICK %s %s :%s'%(chan, kicknick, kickreason)) irc.send_raw(b'KICK %s %s :%s'%(chan, kicknick.encode(), kickreason.encode()))
else: else:
irc.msg(reply, zwsp + 'Usage #kick nick reason') irc.bot_response(reply, usage('#kick'))
elif matchcmd(cmdline, '#src'): elif matchcmd(cmdline, '#src'):
irc.msg(reply, zwsp + 'https://github.com/JuEeHa/oonbotti2') irc.bot_response(reply, 'https://github.com/JuEeHa/oonbotti2')
elif matchcmd(cmdline, '#prefix') and chan == '#osdev-offtopic': elif matchcmd(cmdline, '#prefix') and chan == '#osdev-offtopic':
irc.msg(reply, zwsp + 'gopher://ayu.smar.fi:7070/0/hash-prefix') irc.bot_response(reply, 'gopher://ayu.smar.fi:7070/0/hash-prefix')
elif matchcmd(cmdline, '#msg'): elif matchcmd(cmdline, '#msg'):
if matchcmd(cmdline, '#msg', 'nick {message}'): if matchcmd(cmdline, '#msg', 'nick {message}'):
msgnick, message = parsecmd(cmdline, 'nick {message}') msgnick, message = parsecmd(cmdline, 'nick {message}')
if chan == nick: # In a query: if chan == nick: # In a query:
origin = "[query]" origin = "[query]"
else: # In a channel else: # In a channel
origin = chan origin = chan.decode()
with msgslock: with msgslock:
if msgnick not in msgs: if msgnick not in msgs:
msgs[msgnick] = [] msgs[msgnick] = []
msgs[msgnick].append((nick, origin, message)) msgs[msgnick].append((nick.decode(), origin, message))
savemessages() savemessages()
else: else:
irc.msg(reply, zwsp + 'Usage: #msg nick message') irc.bot_response(reply, usage('#msg'))
elif matchcmd(cmdline, '#trusted?'): elif matchcmd(cmdline, '#trusted?'):
if matchcmd(cmdline, '#trusted?', '[nick]'): if matchcmd(cmdline, '#trusted?', '[nick]'):
trustnick = parsecmd(cmdline, '[nick]') trustnick = parsecmd(cmdline, '[nick]')
if trustnick == '': if trustnick == '':
trustnick = nick trustnick = nick.decode()
account = getaccount(irc, trustnick) account = getaccount(irc, trustnick)
if account: if account:
if istrusted(chan, account): if istrusted(chan, account):
irc.msg(reply, zwsp + '%s is trusted' % trustnick) irc.bot_response(reply, '%s is trusted' % trustnick)
else: else:
irc.msg(reply, zwsp + '%s is not trusted' % trustnick) irc.bot_response(reply, '%s is not trusted' % trustnick)
else: else:
irc.msg(reply, zwsp + 'Failed to get account for %s' % trustnick) irc.bot_response(reply, 'Failed to get account for %s' % trustnick)
else: else:
irc.msg(reply, zwsp + 'Usage: #trusted? [nick]') irc.bot_response(reply, usage('#truste?'))
elif matchcmd(cmdline, '#trust'): elif matchcmd(cmdline, '#trust'):
if matchcmd(cmdline, '#trust', 'nick'): if matchcmd(cmdline, '#trust', 'nick'):
trustnick = parsecmd(cmdline, 'nick') trustnick = parsecmd(cmdline, 'nick')
@ -517,9 +525,9 @@ def parse((line, irc)):
addtrusted(chan, account) addtrusted(chan, account)
savetrusted() savetrusted()
else: else:
irc.msg(reply, zwsp + 'Failed to get account for %s' % trustnick) irc.bot_response(reply, 'Failed to get account for %s' % trustnick)
else: else:
irc.msg(reply, zwsp + 'Usage #trust nick') irc.bot_response(reply, usage('#trust'))
elif matchcmd(cmdline, '#untrust'): elif matchcmd(cmdline, '#untrust'):
if matchcmd(cmdline, '#untrust', 'nick'): if matchcmd(cmdline, '#untrust', 'nick'):
untrustnick = parsecmd(cmdline, 'nick') untrustnick = parsecmd(cmdline, 'nick')
@ -531,20 +539,20 @@ def parse((line, irc)):
account = untrustnick account = untrustnick
if account: if account:
godslock.acquire() godslock.acquire()
if chan not in gods or account not in gods[chan]: if chan.decode() not in gods or account not in gods[chan.decode()]:
rmtrusted(chan, untrustnick) rmtrusted(chan.decode(), untrustnick)
godslock.release() godslock.release()
savetrusted() savetrusted()
else: else:
irc.msg(reply, zwsp + 'Failed to get account for %s' % untrustnick) irc.bot_response(reply, 'Failed to get account for %s' % untrustnick)
else: else:
irc.msg(reply, zwsp + 'Usage #untrust nick') irc.bot_response(reply, usage('#untrust'))
elif matchcmd(cmdline, '#ls-trusted'): elif matchcmd(cmdline, '#ls-trusted'):
trustedlock.acquire() trustedlock.acquire()
if chan in trusted: if chan.decode() in trusted:
lines = [] lines = []
line = '' line = ''
for account in trusted[chan]: for account in trusted[chan.decode()]:
if line == '': if line == '':
line = account line = account
elif len(line + ', ' + account) <= 255: # Playing it safe not to get truncated elif len(line + ', ' + account) <= 255: # Playing it safe not to get truncated
@ -556,22 +564,22 @@ def parse((line, irc)):
lines.append(line) lines.append(line)
for line in lines: for line in lines:
irc.msg(nick, zwsp + '%s: %s' % (chan, line)) irc.bot_response(nick, '%s: %s' % (chan.decode(), line))
trustedlock.release() trustedlock.release()
elif matchcmd(cmdline, '#invite'): elif matchcmd(cmdline, '#invite'):
irc.msg(chan, zwsp + '%s: #invite has been removed. Use manual invite' % nick) irc.bot_response(chan, '%s: #invite has been removed. Use manual invite' % nick)
elif matchcmd(cmdline, '#help'): elif matchcmd(cmdline, '#help'):
if matchcmd(cmdline, '#help', '[command]'): if matchcmd(cmdline, '#help', '[command]'):
command = parsecmd(cmdline, '[command]') command = parsecmd(cmdline, '[command]')
helptext = help(command) helptext = help(command)
if helptext: if helptext:
irc.msg(reply, zwsp+helptext) irc.bot_response(reply, helptext)
elif matchcmd(cmdline, '#esoteric') and chan == '#esoteric': elif matchcmd(cmdline, '#esoteric') and chan == '#esoteric':
irc.msg(reply, zwsp + 'Nothing here') irc.bot_response(reply, 'Nothing here')
elif cmdline[0] in [irc.nick, irc.nick+',', irc.nick+':']: elif cmdline[0] in [irc.get_nick().decode(), irc.get_nick().decode()+',', irc.get_nick().decode()+':']:
question = parsecmd(cmdline, '{question}') question = parsecmd(cmdline, '{question}')
if len(question) < 2 or question[:2] != ':D': # Mandated by #osdev-offtopic law if len(question) < 2 or question[:2] != ':D': # Mandated by #osdev-offtopic law
irc.msg(reply, zwsp + '%s: %s' % (nick, doctor.respond(question))) irc.bot_response(reply, '%s: %s' % (nick.decode(), doctor.respond(question)))
elif die_expr.match(cmdline[0]): elif die_expr.match(cmdline[0]):
die = cmdline[0][1:].split('d') die = cmdline[0][1:].split('d')
times = int(die[0]) if die[0] else 1 times = int(die[0]) if die[0] else 1
@ -590,18 +598,18 @@ def parse((line, irc)):
if die == '%': if die == '%':
if times != 1: if times != 1:
irc.msg(reply, zwsp + 'Not supported') irc.bot_response(reply, 'Not supported')
else: else:
irc.msg(reply, zwsp + '%s%s' % (random.randint(0,9), random.randint(0,9))) irc.bot_response(reply, '%s%s' % (random.randint(0,9), random.randint(0,9)))
elif die < 1: elif die < 1:
irc.msg(reply, zwsp + 'This die is not available in your space-time region.') irc.bot_response(reply, 'This die is not available in your space-time region.')
elif times < 1: elif times < 1:
irc.msg(reply, zwsp + 'What exactly do you want me to do?') irc.bot_response(reply, 'What exactly do you want me to do?')
elif times > 128: elif times > 128:
irc.msg(reply, zwsp + 'Sorry, I don\'t have that many. Can I borrow yours?') irc.bot_response(reply, 'Sorry, I don\'t have that many. Can I borrow yours?')
else: else:
rolls = [random.randint(1, die) for i in xrange(times)] rolls = [random.randint(1, die) for i in range(times)]
result = reduce((lambda x, y: x + y), rolls) result = functools.reduce((lambda x, y: x + y), rolls)
if times > 1: if times > 1:
text = '%s (%s)' % (str(result), ', '.join([str(i) for i in rolls])) text = '%s (%s)' % (str(result), ', '.join([str(i) for i in rolls]))
@ -613,46 +621,32 @@ def parse((line, irc)):
elif plus < 0: elif plus < 0:
text = '%i (%s - %i)' % (result + plus, text, -plus) text = '%i (%s - %i)' % (result + plus, text, -plus)
irc.msg(reply, zwsp + text) irc.bot_response(reply, text)
elif line[1] == '330': # WHOIS: is logged in as elif command == b'330': # WHOIS: is logged in as
whoisnick = line[3] whoisnick = arguments[1].decode('utf-8')
account = line[4] account = arguments[2].decode('utf-8')
for id in getaccountcheckidbynick(whoisnick): for id in getaccountcheckidbynick(whoisnick):
setaccountcheckvalue(id, account) setaccountcheckvalue(id, account)
elif line[1] == '318': # WHOIS: End of /WHOIS list. elif command == '318': # WHOIS: End of /WHOIS list.
whoisnick = line[3] whoisnick = arguments[1].decode('utf-8')
for id in getaccountcheckidbynick(whoisnick): for id in getaccountcheckidbynick(whoisnick):
if getaccountcheckvalue(id) == None: if getaccountcheckvalue(id) == None:
setaccountcheckvalue(id, '') # Mark as failed, '' is used because None is already reserved setaccountcheckvalue(id, '') # Mark as failed, '' is used because None is already reserved
elif line[1] == 'INVITE' and line[2] == irc.nick and line[3][1:] in irc.chan.split(' '): elif command == b'INVITE' and arguments[0] == irc.get_nick() and arguments[1] in irc.get_channel().split(' '):
if isauthorized(irc, line[3][1:], nick): if isauthorized(irc, arguments[1], nick):
irc.send('JOIN ' + line[3]) irc.send_raw(b'JOIN ' + arguments[1])
elif line[1] == '482': elif command == b'482':
irc.msg(line[3], zwsp + 'Not op') irc.bot_response(arguments[1], 'Not op')
msgs_changed = False msgs_changed = False
with msgslock: with msgslock:
if (line[1] == 'PRIVMSG' or line[1] == 'JOIN') and nick in msgs: if (command == b'PRIVMSG' or command == b'JOIN') and nick.decode('utf-8') in msgs:
for sender, origin, msg in msgs.pop(nick): for sender, origin, msg in msgs.pop(nick.decode()):
irc.msg(nick, zwsp + '%s <%s> %s' % (origin, sender, msg)) irc.bot_response(nick, '%s <%s> %s' % (origin, sender, msg))
msgs_changed = True msgs_changed = True
if msgs_changed: if msgs_changed:
savemessages() savemessages()
def execcmd(cmdline):
if cmdline[0] == '/q':
cron.ctrl('QUIT')
elif cmdline[0] == '/lt':
loadtrusted()
elif cmdline[0] == '/st':
savetrusted()
elif cmdline[0] == '/lg':
loadgods()
elif cmdline[0] == '/lm':
loadmessages()
elif cmdline[0] == '/sm':
savemessages()
def usage(cmd, message = True): def usage(cmd, message = True):
usage = {'#echo': 'text', usage = {'#echo': 'text',
'#op': '[nick]', '#op': '[nick]',