47 |
47 |
48 if argname == '': |
48 if argname == '': |
49 continue |
49 continue |
50 |
50 |
51 cmd['argnames'].append (argname) |
51 cmd['argnames'].append (argname) |
52 #done |
|
53 #done |
|
54 |
52 |
55 print "Loaded module %s" % fn |
53 print "Loaded module %s" % fn |
56 #done |
|
57 |
54 |
58 print 'Loaded %d commands in %d modules' % (len (Commands), len (Modules)) |
55 print 'Loaded %d commands in %d modules' % (len (Commands), len (Modules)) |
59 #enddef |
|
60 |
56 |
61 # |
57 # |
62 # command_error (message) |
58 # command_error (message) |
63 # |
59 # |
64 # Raises a command error |
60 # Raises a command error |
77 return False |
73 return False |
78 |
74 |
79 return True |
75 return True |
80 |
76 |
81 # |
77 # |
|
78 # response_function |
|
79 # |
|
80 g_responsePages = [[]] |
|
81 g_responsePageNum = 0 |
|
82 |
|
83 def response_function (message): |
|
84 global g_responsePages |
|
85 |
|
86 if len (g_responsePages[-1]) > 4: |
|
87 g_responsePages.append ([message]) |
|
88 else: |
|
89 g_responsePages[-1].append (message) |
|
90 |
|
91 def print_responses (bot, replyto): |
|
92 global g_responsePages |
|
93 global g_responsePageNum |
|
94 |
|
95 # Check bounds |
|
96 if g_responsePageNum >= len (g_responsePages): |
|
97 bot.privmsg (replyto, "No more messages.") |
|
98 return |
|
99 |
|
100 # Print this page |
|
101 for line in g_responsePages[g_responsePageNum]: |
|
102 bot.privmsg (replyto, line) |
|
103 |
|
104 # Advance page cursor |
|
105 g_responsePageNum += 1 |
|
106 |
|
107 # If this was not the last page, tell the user there's more |
|
108 if g_responsePageNum != len (g_responsePages): |
|
109 num = (len (g_responsePages) - g_responsePageNum) |
|
110 bot.privmsg (replyto, "%d more page%s, use .more to continue output" \ |
|
111 % (num, 's' if num != 1 else '')) |
|
112 |
|
113 # |
82 # call_command (bot, message, cmdname, **kvargs) |
114 # call_command (bot, message, cmdname, **kvargs) |
83 # |
115 # |
84 # Calls a cobalt command |
116 # Calls a cobalt command |
85 # |
117 # |
86 def call_command (bot, message, cmdname, **kvargs): |
118 def call_command (bot, message, cmdname, **kvargs): |
|
119 global g_responsePages |
|
120 global g_responsePageNum |
|
121 |
87 try: |
122 try: |
88 cmd = Commands[cmdname] |
123 cmd = Commands[cmdname] |
89 except KeyError: |
124 except KeyError: |
90 return False |
125 return False |
91 |
126 |
96 |
131 |
97 if match == None: |
132 if match == None: |
98 # didn't match |
133 # didn't match |
99 print "regex: %s" % cmd['regex'] |
134 print "regex: %s" % cmd['regex'] |
100 command_error ('invalid arguments\nusage: %s %s' % (cmd['name'], cmd['args'])) |
135 command_error ('invalid arguments\nusage: %s %s' % (cmd['name'], cmd['args'])) |
101 |
136 |
|
137 # .more is special as it is an interface to the page system. |
|
138 # Anything else resets it. |
|
139 if cmdname != 'more': |
|
140 g_responsePages = [[]] |
|
141 g_responsePageNum = 0 |
|
142 |
102 i = 1 |
143 i = 1 |
103 args = {} |
144 args = {} |
104 |
145 |
105 for argname in cmd['argnames']: |
146 for argname in cmd['argnames']: |
106 args[argname] = match.group (i) |
147 args[argname] = match.group (i) |
107 i += 1 |
148 i += 1 |
108 |
149 |
109 getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, **kvargs) |
150 getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, reply=response_function, **kvargs) |
|
151 |
|
152 # Print the first page of responses. |
|
153 if cmdname != 'more': |
|
154 print_responses (bot, kvargs['replyto']) |
|
155 |
110 return True |
156 return True |
111 |
157 |
112 # |
158 # |
113 # get_available_commands |
159 # get_available_commands |
114 # |
160 # |