| 65 # |
65 # |
| 66 def command_error (message): |
66 def command_error (message): |
| 67 raise CommandError (message) |
67 raise CommandError (message) |
| 68 |
68 |
| 69 # |
69 # |
| |
70 # is_available (cmd, ident, host) |
| |
71 # |
| |
72 # Is the given command available to the given user? |
| |
73 # |
| |
74 def is_available (cmd, ident, host): |
| |
75 if cmd['level'] == 'admin' \ |
| |
76 and not "%s@%s" % (ident, host) in Config.get_value ('admins', default=[]): |
| |
77 return False |
| |
78 |
| |
79 return True |
| |
80 |
| |
81 # |
| 70 # call_command (bot, message, cmdname, **kvargs) |
82 # call_command (bot, message, cmdname, **kvargs) |
| 71 # |
83 # |
| 72 # Calls a cobalt command |
84 # Calls a cobalt command |
| 73 # |
85 # |
| 74 def call_command (bot, message, cmdname, **kvargs): |
86 def call_command (bot, message, cmdname, **kvargs): |
| 75 try: |
87 try: |
| 76 cmd = Commands[cmdname] |
88 cmd = Commands[cmdname] |
| 77 except KeyError: |
89 except KeyError: |
| 78 return False |
90 return False |
| 79 |
91 |
| 80 if cmd['level'] == 'admin' \ |
92 if not is_available (cmd, kvargs['ident'], kvargs['host']): |
| 81 and not "%s@%s" % (kvargs['ident'], kvargs['host']) in Config.get_value ('admins', default=[]): |
|
| 82 command_error ("you may not use %s" % cmdname) |
93 command_error ("you may not use %s" % cmdname) |
| 83 |
94 |
| 84 match = re.compile (cmd['regex']).match (message) |
95 match = re.compile (cmd['regex']).match (message) |
| 85 |
96 |
| 86 if match == None: |
97 if match == None: |
| 95 args[argname] = match.group (i) |
106 args[argname] = match.group (i) |
| 96 i += 1 |
107 i += 1 |
| 97 |
108 |
| 98 getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, **kvargs) |
109 getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, **kvargs) |
| 99 return True |
110 return True |
| |
111 |
| |
112 # |
| |
113 # get_available_commands |
| |
114 # |
| |
115 # Gets a list of commands available to the given user |
| |
116 # |
| |
117 def get_available_commands (ident, host): |
| |
118 result=[] |
| |
119 |
| |
120 for cmdname,cmd in Commands.iteritems(): |
| |
121 if not is_available (cmd, ident, host): |
| |
122 continue |
| |
123 |
| |
124 result.append (cmdname) |
| |
125 |
| |
126 return result |
| |
127 |
| |
128 # |
| |
129 # get_command_by_name |
| |
130 # |
| |
131 # Gets a command by name |
| |
132 # |
| |
133 def get_command_by_name (name): |
| |
134 try: |
| |
135 return Commands[name] |
| |
136 except: |
| |
137 return None |
| 100 |
138 |
| 101 # |
139 # |
| 102 # make_regex |
140 # make_regex |
| 103 # |
141 # |
| 104 # Takes the argument list and returns a corresponding regular expression |
142 # Takes the argument list and returns a corresponding regular expression |