modulecore.py

changeset 73
d67cc4fbc3f1
parent 65
20bd76353eb5
child 102
2bad379cd416
equal deleted inserted replaced
72:2266d6d73de3 73:d67cc4fbc3f1
1 import os 1 import os
2 import re 2 import re
3 from configfile import Config
3 4
4 Modules = {} 5 Modules = {}
5 Commands = {} 6 Commands = {}
6 7
7 class CommandError (Exception): 8 class CommandError (Exception):
73 def call_command (bot, message, cmdname, **kvargs): 74 def call_command (bot, message, cmdname, **kvargs):
74 try: 75 try:
75 cmd = Commands[cmdname] 76 cmd = Commands[cmdname]
76 except KeyError: 77 except KeyError:
77 return False 78 return False
78 79
79 if cmd['level'] == 'admin' and not bot.is_admin (kvargs['ident'], kvargs['host']): 80 if cmd['level'] == 'admin' \
80 command_error ("%s requires admin access" % cmdname) 81 and not "%s@%s" % (kvargs['ident'], kvargs['host']) in Config.get_value ('admins', default=[]):
81 82 command_error ("you may not use %s" % cmdname)
83
82 match = re.compile (cmd['regex']).match (message) 84 match = re.compile (cmd['regex']).match (message)
83 85
84 if match == None: 86 if match == None:
85 # didn't match 87 # didn't match
88 print "regex: %s" % cmd['regex']
86 command_error ('invalid arguments\nusage: %s %s' % (cmd['name'], cmd['args'])) 89 command_error ('invalid arguments\nusage: %s %s' % (cmd['name'], cmd['args']))
87 #fi 90
88
89 i = 1 91 i = 1
90 args = {} 92 args = {}
91 93
92 for argname in cmd['argnames']: 94 for argname in cmd['argnames']:
93 args[argname] = match.group (i) 95 args[argname] = match.group (i)
94 i += 1 96 i += 1
95 #done 97
96
97 getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, **kvargs) 98 getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, **kvargs)
98 return True 99 return True
99 100
100 # 101 #
101 # make_regex 102 # make_regex
114 if gotvariadic: 115 if gotvariadic:
115 raise CommandError ('variadic argument is not last') 116 raise CommandError ('variadic argument is not last')
116 117
117 if arg == '': 118 if arg == '':
118 continue 119 continue
120
121 gotliteral = False
119 122
120 if arg[0] == '[' and arg[-1] == ']': 123 if arg[0] == '[' and arg[-1] == ']':
121 arg = arg[1:-1] 124 arg = arg[1:-1]
122 gotoptional = True 125 gotoptional = True
123 elif arg[0] == '<' and arg[-1] == '>': 126 elif arg[0] == '<' and arg[-1] == '>':
124 if gotoptional: 127 if gotoptional:
125 raise CommandError ('mandatory argument after optional one') 128 raise CommandError ('mandatory argument after optional one')
126 129
127 arg = arg[1:-1] 130 arg = arg[1:-1]
128 else: 131 else:
129 raise CommandError ('badly formed argument list') 132 gotliteral = True
130 #fi
131 133
132 if arg[-3:] == '...': 134 if arg[-3:] == '...':
133 gotvariadic = True 135 gotvariadic = True
134 arg = arg[0:-3] 136 arg = arg[0:-3]
135 #fi
136 137
137 if gotoptional == False: 138 if gotoptional == False:
138 regex += '\s+' 139 regex += '\s+'
139 else: 140 else:
140 regex += '\s*' 141 regex += '\s*'
141 142
142 if gotoptional: 143 if gotliteral:
144 regex += arg
145 elif gotoptional:
143 if gotvariadic: 146 if gotvariadic:
144 regex += r'(.*)' 147 regex += r'(.*)'
145 else: 148 else:
146 regex += r'([^ ]*)' 149 regex += r'([^ ]*)'
147 else: 150 else:

mercurial