Module improvements

Register modules, along with their optional help function
Allow running ambiguous commands by prefixing with "modulename:"
This commit is contained in:
moneromooo 2015-01-03 18:32:09 +00:00
parent d7f3ee7f3f
commit e1c881860f
5 changed files with 103 additions and 42 deletions

View file

@ -142,8 +142,13 @@ def DumpUsers(nick,chan,cmd):
log_info(str(userstable)) log_info(str(userstable))
def Help(nick,chan,cmd): def Help(nick,chan,cmd):
module = GetParam(cmd,1)
if module:
RunModuleHelpFunction(module,nick,chan)
return
SendTo(nick, "See available commands with !commands or !commands <modulename>") SendTo(nick, "See available commands with !commands or !commands <modulename>")
RunHelpFunctions(nick) SendTo(nick, "Get help on a particular module with !help <modulename>")
if coinspecs.web_wallet_url: if coinspecs.web_wallet_url:
SendTo(nick, "No %s address ? You can use %s" % (coinspecs.name, coinspecs.web_wallet_url)) SendTo(nick, "No %s address ? You can use %s" % (coinspecs.name, coinspecs.web_wallet_url))
@ -195,7 +200,7 @@ def Reload(nick,chan,cmd):
SendTo(sendto,"Cannot reload builtin module") SendTo(sendto,"Cannot reload builtin module")
return return
log_info('Unloading %s module' % modulename) log_info('Unloading %s module' % modulename)
UnregisterCommands(modulename) UnregisterModule(modulename)
log_info('Reloading %s module' % modulename) log_info('Reloading %s module' % modulename)
try: try:
reload(sys.modules[modulename]) reload(sys.modules[modulename])
@ -211,7 +216,7 @@ def OnIdentified(nick, identified):
RunNextCommand(nick, identified) RunNextCommand(nick, identified)
def RegisterCommands(): def RegisterCommands():
RegisterCommand({'module': 'builtin', 'name': 'help', 'function': Help, 'help': "Displays help about %s" % config.tipbot_name}) RegisterCommand({'module': 'builtin', 'name': 'help', 'parms': '[module]', 'function': Help, 'help': "Displays help about %s" % config.tipbot_name})
RegisterCommand({'module': 'builtin', 'name': 'commands', 'parms': '[module]', 'function': Commands, 'help': "Displays list of commands"}) RegisterCommand({'module': 'builtin', 'name': 'commands', 'parms': '[module]', 'function': Commands, 'help': "Displays list of commands"})
RegisterCommand({'module': 'builtin', 'name': 'isregistered', 'function': IsRegistered, 'help': "show whether you are currently registered with freenode"}) RegisterCommand({'module': 'builtin', 'name': 'isregistered', 'function': IsRegistered, 'help': "show whether you are currently registered with freenode"})
RegisterCommand({'module': 'builtin', 'name': 'balance', 'function': GetBalance, 'registered': True, 'help': "show your current balance"}) RegisterCommand({'module': 'builtin', 'name': 'balance', 'function': GetBalance, 'registered': True, 'help': "show your current balance"})

View file

@ -13,11 +13,11 @@ import tipbot.config as config
from tipbot.utils import * from tipbot.utils import *
from tipbot.ircutils import * from tipbot.ircutils import *
modules = dict()
commands = dict() commands = dict()
calltable=dict() calltable=dict()
idles = [] idles = []
cleanup = dict() cleanup = dict()
helps = dict()
def RunRegisteredCommand(nick,chan,ifyes,yesdata,ifno,nodata): def RunRegisteredCommand(nick,chan,ifyes,yesdata,ifno,nodata):
if nick not in calltable: if nick not in calltable:
@ -71,7 +71,7 @@ def Commands(nick,chan,cmd):
msgs = dict() msgs = dict()
for command_name in commands: for command_name in commands:
c = commands[command_name] for c in commands[command_name]:
if 'admin' in c and c['admin'] and not all: if 'admin' in c and c['admin'] and not all:
continue continue
module = c['module'] module = c['module']
@ -93,10 +93,18 @@ def Commands(nick,chan,cmd):
for msg in msgs: for msg in msgs:
SendTo(nick, "%s" % msgs[msg]) SendTo(nick, "%s" % msgs[msg])
def RegisterModule(module):
if module['name'] in modules:
log_error('a module named %s is already registered' % module['name'])
return
modules[module['name']] = module
def RegisterCommand(command): def RegisterCommand(command):
if command['name'] in commands: if command['name'] in commands:
log_warn('module %s redefined function %s from module %s' % (command['module'],command['name'],commands[command['name']]['module'])) log_warn('module %s redefined function %s from module %s' % (command['module'],command['name'],commands[command['name']][0]['module']))
commands[command['name']] = command else:
commands[command['name']] = []
commands[command['name']].append(command)
def RegisterIdleFunction(module,function): def RegisterIdleFunction(module,function):
idles.append((module,function)) idles.append((module,function))
@ -104,12 +112,40 @@ def RegisterIdleFunction(module,function):
def RegisterCleanupFunction(module,function): def RegisterCleanupFunction(module,function):
cleanup.append((module,function)) cleanup.append((module,function))
def RegisterHelpFunction(module,function):
helps[module]=function
def OnCommand(cmd,chan,who,check_admin,check_registered): def OnCommand(cmd,chan,who,check_admin,check_registered):
if cmd[0] in commands: cmdparts = cmd[0].split(':')
c = commands[cmd[0]] log_log('cmdparts: %s' % str(cmdparts))
if len(cmdparts) == 2:
modulename = cmdparts[0]
cmdname = cmdparts[1]
elif len(cmdparts) == 1:
modulename = None
cmdname = cmdparts[0]
else:
SendTo(GetNick(who), "Invalid command, try !help")
return
log_log('modulename: %s, cmdname: %s' % (str(modulename),str(cmdname)))
if cmdname in commands:
log_log('%s found in commands' % (str(cmd[0])))
if len(commands[cmdname]) > 1:
if not modulename:
msg = ""
for c in commands[cmdname]:
if msg != "":
msg = msg + ", "
msg = msg + c['module'] + ":" + cmd[0]
SendTo(GetNick(who), "Ambiguous command, try one of: %s" % msg)
return
c = None
for command in commands[cmdname]:
if command['module'] == modulename:
c = command
break
if not c:
SendTo(GetNick(who), "Invalid command, try !help")
return
else:
c = commands[cmdname][0]
if 'admin' in c and c['admin']: if 'admin' in c and c['admin']:
check_admin(GetNick(who),chan,c['function'],cmd,SendTo,"You must be admin") check_admin(GetNick(who),chan,c['function'],cmd,SendTo,"You must be admin")
elif 'registered' in c and c['registered']: elif 'registered' in c and c['registered']:
@ -126,35 +162,39 @@ def RunIdleFunctions(param):
except Exception,e: except Exception,e:
log_error("Exception running idle function %s from module %s: %s" % (str(f[1]),str(f[2]),str(e))) log_error("Exception running idle function %s from module %s: %s" % (str(f[1]),str(f[2]),str(e)))
def RunHelpFunctions(param): def RunModuleHelpFunction(module,nick,chan):
for f in helps: if module in modules:
try: try:
helps[f](param) modules[module]['help'](nick,chan)
except Exception,e: except Exception,e:
log_error("Exception running help function %s from module %s: %s" % (str(helps[f]),str(f),str(e))) log_error("Exception running help function %s from module %s: %s" % (str(modules[module]['help']),str(module),str(e)))
else:
SendTo(nick,'No help found for module %s' % module)
def UnregisterCommands(module): def UnregisterModule(module):
global commands global commands
global idles global idles
global helps
if module in cleanup: if module in cleanup:
cleanup[module]() cleanup[module]()
del cleanup[module] del cleanup[module]
if module in modules:
del modules[module]
new_idles = [] new_idles = []
for f in idles: for f in idles:
if f[0] != module: if f[0] != module:
new_idles.append(f) new_idles.append(f)
idles = new_idles idles = new_idles
if module in helps:
del helps[module]
new_commands = dict() new_commands = dict()
for cmd in commands: for cmd in commands:
c = commands[cmd] newlist = []
for c in commands[cmd]:
if c['module'] != module: if c['module'] != module:
new_commands[cmd] = c newlist.append(c)
if len(newlist) > 0:
new_commands[cmd] = newlist
commands = new_commands commands = new_commands

View file

@ -104,11 +104,14 @@ def UpdateCoin(param):
log_error('UpdateCoin: Failed to get bulk payments: %s' % str(e)) log_error('UpdateCoin: Failed to get bulk payments: %s' % str(e))
last_wallet_update_time = time.time() last_wallet_update_time = time.time()
def Help(nick): def Help(nick,chan):
SendTo(nick, "You can send %s to your account:" % coinspecs.name); SendTo(nick, "You can send %s to your account:" % coinspecs.name);
SendTo(nick, " Address: %s" % GetTipbotAddress()) SendTo(nick, " Address: %s" % GetTipbotAddress())
SendTo(nick, " Payment ID: %s" % GetPaymentID(nick)) SendTo(nick, " Payment ID: %s" % GetPaymentID(nick))
RegisterModule({
'name': __name__,
'help': Help,
})
RegisterIdleFunction(__name__,UpdateCoin) RegisterIdleFunction(__name__,UpdateCoin)
RegisterHelpFunction(__name__,Help)

View file

@ -242,7 +242,17 @@ def RainActive(nick,chan,cmd):
SendTo(chan, "An error has occured") SendTo(chan, "An error has occured")
return return
def Help(nick,chan):
SendTo(nick,'You can tip other people, or rain %s on them' % coinspecs.name)
SendTo(nick,'!tip tips a single person, while !rain shares equally between people in the channel')
SendTo(nick,'!rainactive tips all within the last N hours, with more recently active people')
SendTo(nick,'getting a larger share.')
RegisterModule({
'name': __name__,
'help': Help,
})
RegisterCommand({ RegisterCommand({
'module': __name__, 'module': __name__,
'name': 'tip', 'name': 'tip',

View file

@ -144,7 +144,7 @@ def Withdraw(nick,chan,cmd):
log_error('Withdraw: FAILED TO SUBTRACT BALANCE: exception: %s' % str(e)) log_error('Withdraw: FAILED TO SUBTRACT BALANCE: exception: %s' % str(e))
CheckDisableWithdraw() CheckDisableWithdraw()
def Help(nick): def Help(nick,chan):
fee = config.withdrawal_fee or coinspecs.min_withdrawal_fee fee = config.withdrawal_fee or coinspecs.min_withdrawal_fee
min_amount = config.min_withdraw_amount or fee min_amount = config.min_withdraw_amount or fee
SendTo(nick, "Minimum withdrawal: %s" % AmountToString(min_amount)) SendTo(nick, "Minimum withdrawal: %s" % AmountToString(min_amount))
@ -152,6 +152,10 @@ def Help(nick):
RegisterModule({
'name': __name__,
'help': Help,
})
RegisterCommand({ RegisterCommand({
'module': __name__, 'module': __name__,
'name': 'withdraw', 'name': 'withdraw',
@ -174,4 +178,3 @@ RegisterCommand({
'admin': True, 'admin': True,
'help': "Disable withdrawals" 'help': "Disable withdrawals"
}) })
RegisterHelpFunction(__name__,Help)