diff -r 497b7290977d -r df862cca1773 mod_util.py --- a/mod_util.py Sun Aug 16 19:27:14 2015 +0300 +++ b/mod_util.py Sun Aug 16 23:30:11 2015 +0300 @@ -30,8 +30,7 @@ import math import json import re -from modulecore import command_error -import modulecore as ModuleCore +import modulecore import utility import calc import urllib.parse @@ -108,21 +107,14 @@ 'args': '', 'level': 'normal' }, - - { - 'name': 'py', - 'description': 'Evaluates python', - 'args': '', - 'level': 'normal' - } ] } -def cmd_convert (bot, args, reply, **rest): +def cmd_convert (bot, args, reply, error, **rest): try: value = float (args['value']) except Exception as e: - command_error (str (e)) + error (str (e)) valuetype = args['valuetype'] @@ -148,10 +140,10 @@ reply ('%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue)) return - command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' + + error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' + 'celsius, fahrenheit (temperature conversion)' % valuetype) -def cmd_ud (bot, args, reply, **rest): +def cmd_ud (bot, args, reply, error, **rest): try: url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20')) response = utility.read_url (url) @@ -161,7 +153,7 @@ or len(data['list']) == 0 \ or not 'word' in data['list'][0] \ or not 'definition' in data['list'][0]: - command_error ("couldn't find a definition of \002%s\002" % args['term']) + error ("couldn't find a definition of \002%s\002" % args['term']) word = data['list'][0]['word'] definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ') @@ -169,10 +161,10 @@ down = data['list'][0]['thumbs_down'] reply ("\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down)) except Exception as e: - command_error ('Urban dictionary lookup failed: %s' % e) + error ('Urban dictionary lookup failed: %s' % e) def cmd_commands (bot, reply, ident, host, **rest): - commandlist = ModuleCore.get_available_commands (ident, host) + commandlist = modulecore.get_available_commands (ident, host) partitioned=[] while len (commandlist) > 0: @@ -182,27 +174,17 @@ for part in partitioned: reply ('\002Available commands\002: %s' % (", ".join (part))) -def cmd_help (bot, reply, ident, host, args, **rest): - cmd = ModuleCore.get_command_by_name (args['command']) +def cmd_help (bot, reply, ident, host, args, error, **rest): + cmd = modulecore.get_command_by_name (args['command']) if not cmd: - command_error ('unknown command \'%s\'' % args['command']) + error ('unknown command \'%s\'' % args['command']) - if not ModuleCore.is_available (cmd, ident, host): - command_error ('you may not use %s' % cmd['name']) + if not modulecore.is_available (cmd, ident, host): + error ('you may not use %s' % cmd['name']) reply ('%s %s: %s' % (cmd['name'], cmd['args'], cmd['description'])) -def mathsubstitute (expr, token, value): - rex = re.compile (r'^(.*)\b' + token + r'\b(.*)$') - match = rex.match (expr) - - while match: - expr = match.group(1) + str (value) + match.group(2) - match = rex.match (expr) - - return expr - def cmd_calcfunctions (bot, reply, **rest): reply ('Available functions for .calc: %s' % \ ', '.join (sorted ([name for name, data in calc.Functions.items()]))) @@ -211,20 +193,26 @@ reply (calc.Calculator().calc (args['expression'])) def cmd_more (commandObject, **rest): - ModuleCore.print_responses (commandObject) + modulecore.print_responses (commandObject) def cmd_yes (**k): - ModuleCore.confirm (k, True) + modulecore.confirm (k, True) def cmd_no (**k): - ModuleCore.confirm (k, False) + modulecore.confirm (k, False) def cmd_bitly (reply, args, **k): reply ('Result: %s' % utility.shorten_link (args['link'])) -def cmd_py (reply, args, **rest): +@modulecore.irc_command (args='') +def py (reply, args, **rest): + ''' Evaluates the given Python string using appspot.com ''' url = 'http://eval.appspot.com/eval?statement=' + urllib.parse.quote (args['command']) - result = utility.read_url (url).splitlines() + result = utility.read_url (url, timeout=15).splitlines() + + if not result: + reply ('No output.') + return # \x0f is the 'reset colors' code, prepended to all reply lines to prevent other bots from # reacting to this .py call.