Wed, 31 Dec 2014 15:49:11 -0500
- .idgames now works with the page system
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | import os |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | import re |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
3 | from configfile import Config |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
62
diff
changeset
|
5 | Modules = {} |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | Commands = {} |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | class CommandError (Exception): |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | def __init__ (self, value): |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | self.value = value |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | def __str__ (self): |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | return self.value |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | # |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | # init_data() |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | # |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | # Initializes command module data |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | # |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | def init_data(): |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | global Commands |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
62
diff
changeset
|
21 | global Modules |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | files = os.listdir ('.') |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | for fn in files: |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
62
diff
changeset
|
25 | if fn[0:4] != 'mod_' or fn[-3:] != '.py': |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
26 | continue |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
27 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | fn = fn[0:-3] |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | globals = {} |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | module = __import__ (fn) |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
62
diff
changeset
|
31 | Modules[fn] = module |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | for cmd in module.ModuleData['commands']: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | if cmd['args'] == None: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | cmd['args'] = '' |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | cmd['module'] = module |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | cmd['regex'] = make_regex (cmd['args']) |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | cmd['argnames'] = [] |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | Commands[cmd['name']] = cmd |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
42 | for argname in cmd['args'].split (' '): |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
43 | argname = argname[1:-1] |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
44 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | if argname[-3:] == '...': |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | argname = argname[0:-3] |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | if argname == '': |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | continue |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
50 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
51 | cmd['argnames'].append (argname) |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
62
diff
changeset
|
53 | print "Loaded module %s" % fn |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | |
65
20bd76353eb5
- modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
62
diff
changeset
|
55 | print 'Loaded %d commands in %d modules' % (len (Commands), len (Modules)) |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
56 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | # |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | # command_error (message) |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | # |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
60 | # Raises a command error |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
61 | # |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
62 | def command_error (message): |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | raise CommandError (message) |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | # |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
66 | # is_available (cmd, ident, host) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
67 | # |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
68 | # Is the given command available to the given user? |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
69 | # |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
70 | def is_available (cmd, ident, host): |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
71 | if cmd['level'] == 'admin' \ |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
72 | and not "%s@%s" % (ident, host) in Config.get_value ('admins', default=[]): |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
73 | return False |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
74 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
75 | return True |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
76 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
77 | # |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
78 | # response_function |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
79 | # |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
80 | g_responsePages = [[]] |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
81 | g_responsePageNum = 0 |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
82 | |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
83 | def response_function (message): |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
84 | global g_responsePages |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
85 | |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
86 | if len (g_responsePages[-1]) > 4: |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
87 | g_responsePages.append ([message]) |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
88 | else: |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
89 | g_responsePages[-1].append (message) |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
90 | |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
91 | def print_responses (bot, replyto): |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
92 | global g_responsePages |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
93 | global g_responsePageNum |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
94 | |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
95 | # Check bounds |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
96 | if g_responsePageNum >= len (g_responsePages): |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
97 | bot.privmsg (replyto, "No more messages.") |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
98 | return |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
99 | |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
100 | # Print this page |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
101 | for line in g_responsePages[g_responsePageNum]: |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
102 | bot.privmsg (replyto, line) |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
103 | |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
104 | # Advance page cursor |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
105 | g_responsePageNum += 1 |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
106 | |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
107 | # If this was not the last page, tell the user there's more |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
108 | if g_responsePageNum != len (g_responsePages): |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
109 | num = (len (g_responsePages) - g_responsePageNum) |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
110 | bot.privmsg (replyto, "%d more page%s, use .more to continue output" \ |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
111 | % (num, 's' if num != 1 else '')) |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
112 | |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
113 | # |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
114 | # call_command (bot, message, cmdname, **kvargs) |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
115 | # |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
116 | # Calls a cobalt command |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
117 | # |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
118 | def call_command (bot, message, cmdname, **kvargs): |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
119 | global g_responsePages |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
120 | global g_responsePageNum |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
121 | |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
122 | try: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
123 | cmd = Commands[cmdname] |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
124 | except KeyError: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
125 | return False |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
126 | |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
127 | if not is_available (cmd, kvargs['ident'], kvargs['host']): |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
128 | command_error ("you may not use %s" % cmdname) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
129 | |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | match = re.compile (cmd['regex']).match (message) |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
131 | |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
132 | if match == None: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
133 | # didn't match |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
134 | print "regex: %s" % cmd['regex'] |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
135 | command_error ('invalid arguments\nusage: %s %s' % (cmd['name'], cmd['args'])) |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
136 | |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
137 | # .more is special as it is an interface to the page system. |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
138 | # Anything else resets it. |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
139 | if cmdname != 'more': |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
140 | g_responsePages = [[]] |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
141 | g_responsePageNum = 0 |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
142 | |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
143 | i = 1 |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
144 | args = {} |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
145 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
146 | for argname in cmd['argnames']: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
147 | args[argname] = match.group (i) |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
148 | i += 1 |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
149 | |
113
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
150 | getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, reply=response_function, **kvargs) |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
151 | |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
152 | # Print the first page of responses. |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
153 | if cmdname != 'more': |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
154 | print_responses (bot, kvargs['replyto']) |
08e9b1c1b324
- added page system to prevent commands from printing too much output
Teemu Piippo <crimsondusk64@gmail.com>
parents:
102
diff
changeset
|
155 | |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
156 | return True |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
157 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
158 | # |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
159 | # get_available_commands |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
160 | # |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
161 | # Gets a list of commands available to the given user |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
162 | # |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
163 | def get_available_commands (ident, host): |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
164 | result=[] |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
165 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
166 | for cmdname,cmd in Commands.iteritems(): |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
167 | if not is_available (cmd, ident, host): |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
168 | continue |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
169 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
170 | result.append (cmdname) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
171 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
172 | return result |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
173 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
174 | # |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
175 | # get_command_by_name |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
176 | # |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
177 | # Gets a command by name |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
178 | # |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
179 | def get_command_by_name (name): |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
180 | try: |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
181 | return Commands[name] |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
182 | except: |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
183 | return None |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
184 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
185 | # |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
186 | # make_regex |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
187 | # |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
188 | # Takes the argument list and returns a corresponding regular expression |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
189 | # |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
190 | def make_regex (arglist): |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
191 | if arglist == None: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
192 | return '^.+$' |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
193 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
194 | gotoptional = False |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
195 | gotvariadic = False |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
196 | regex = '' |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
197 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
198 | for arg in arglist.split (' '): |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
199 | if gotvariadic: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
200 | raise CommandError ('variadic argument is not last') |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
201 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
202 | if arg == '': |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
203 | continue |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
204 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
205 | gotliteral = False |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
206 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
207 | if arg[0] == '[' and arg[-1] == ']': |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
208 | arg = arg[1:-1] |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
209 | gotoptional = True |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
210 | elif arg[0] == '<' and arg[-1] == '>': |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
211 | if gotoptional: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
212 | raise CommandError ('mandatory argument after optional one') |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
213 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
214 | arg = arg[1:-1] |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
215 | else: |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
216 | gotliteral = True |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
217 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
218 | if arg[-3:] == '...': |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
219 | gotvariadic = True |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
220 | arg = arg[0:-3] |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
221 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
222 | if gotoptional == False: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
223 | regex += '\s+' |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
224 | else: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
225 | regex += '\s*' |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
226 | |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
227 | if gotliteral: |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
228 | regex += arg |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
65
diff
changeset
|
229 | elif gotoptional: |
62
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
230 | if gotvariadic: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
231 | regex += r'(.*)' |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
232 | else: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
233 | regex += r'([^ ]*)' |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
234 | else: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
235 | if gotvariadic: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
236 | regex += r'(.+)' |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
237 | else: |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
238 | regex += r'([^ ]+)' |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
239 | #fi |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
240 | #done |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
241 | |
052a8a1e3d7d
- revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
242 | return '^[^ ]+%s$' % regex |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
73
diff
changeset
|
243 | #enddef |