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))
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>")
RunHelpFunctions(nick)
SendTo(nick, "Get help on a particular module with !help <modulename>")
if 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")
return
log_info('Unloading %s module' % modulename)
UnregisterCommands(modulename)
UnregisterModule(modulename)
log_info('Reloading %s module' % modulename)
try:
reload(sys.modules[modulename])
@ -211,7 +216,7 @@ def OnIdentified(nick, identified):
RunNextCommand(nick, identified)
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': '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"})

View File

@ -13,11 +13,11 @@ import tipbot.config as config
from tipbot.utils import *
from tipbot.ircutils import *
modules = dict()
commands = dict()
calltable=dict()
idles = []
cleanup = dict()
helps = dict()
def RunRegisteredCommand(nick,chan,ifyes,yesdata,ifno,nodata):
if nick not in calltable:
@ -71,32 +71,40 @@ def Commands(nick,chan,cmd):
msgs = dict()
for command_name in commands:
c = commands[command_name]
if 'admin' in c and c['admin'] and not all:
continue
module = c['module']
if module_name:
if module_name != module:
for c in commands[command_name]:
if 'admin' in c and c['admin'] and not all:
continue
synopsis = c['name']
if 'parms' in c:
synopsis = synopsis + " " + c['parms']
SendTo(nick, "%s - %s" % (synopsis, c['help']))
else:
if module in msgs:
msgs[module] = msgs[module] +(", ")
module = c['module']
if module_name:
if module_name != module:
continue
synopsis = c['name']
if 'parms' in c:
synopsis = synopsis + " " + c['parms']
SendTo(nick, "%s - %s" % (synopsis, c['help']))
else:
msgs[module] = module + " module: "
msgs[module] = msgs[module] +(c['name'])
if module in msgs:
msgs[module] = msgs[module] +(", ")
else:
msgs[module] = module + " module: "
msgs[module] = msgs[module] +(c['name'])
if not module_name:
for msg in msgs:
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):
if command['name'] in commands:
log_warn('module %s redefined function %s from module %s' % (command['module'],command['name'],commands[command['name']]['module']))
commands[command['name']] = command
log_warn('module %s redefined function %s from module %s' % (command['module'],command['name'],commands[command['name']][0]['module']))
else:
commands[command['name']] = []
commands[command['name']].append(command)
def RegisterIdleFunction(module,function):
idles.append((module,function))
@ -104,12 +112,40 @@ def RegisterIdleFunction(module,function):
def RegisterCleanupFunction(module,function):
cleanup.append((module,function))
def RegisterHelpFunction(module,function):
helps[module]=function
def OnCommand(cmd,chan,who,check_admin,check_registered):
if cmd[0] in commands:
c = commands[cmd[0]]
cmdparts = cmd[0].split(':')
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']:
check_admin(GetNick(who),chan,c['function'],cmd,SendTo,"You must be admin")
elif 'registered' in c and c['registered']:
@ -126,35 +162,39 @@ def RunIdleFunctions(param):
except Exception,e:
log_error("Exception running idle function %s from module %s: %s" % (str(f[1]),str(f[2]),str(e)))
def RunHelpFunctions(param):
for f in helps:
def RunModuleHelpFunction(module,nick,chan):
if module in modules:
try:
helps[f](param)
modules[module]['help'](nick,chan)
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 idles
global helps
if module in cleanup:
cleanup[module]()
del cleanup[module]
if module in modules:
del modules[module]
new_idles = []
for f in idles:
if f[0] != module:
new_idles.append(f)
idles = new_idles
if module in helps:
del helps[module]
new_commands = dict()
for cmd in commands:
c = commands[cmd]
if c['module'] != module:
new_commands[cmd] = c
newlist = []
for c in commands[cmd]:
if c['module'] != module:
newlist.append(c)
if len(newlist) > 0:
new_commands[cmd] = newlist
commands = new_commands

View File

@ -104,11 +104,14 @@ def UpdateCoin(param):
log_error('UpdateCoin: Failed to get bulk payments: %s' % str(e))
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, " Address: %s" % GetTipbotAddress())
SendTo(nick, " Payment ID: %s" % GetPaymentID(nick))
RegisterModule({
'name': __name__,
'help': Help,
})
RegisterIdleFunction(__name__,UpdateCoin)
RegisterHelpFunction(__name__,Help)

View File

@ -242,7 +242,17 @@ def RainActive(nick,chan,cmd):
SendTo(chan, "An error has occured")
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({
'module': __name__,
'name': 'tip',

View File

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