Thu, 20 May 2010 14:32:21 +0100
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
0 | 1 | require "util.xstanza" |
2 | ||
3 | function riddim.plugins.commands(bot) | |
4 | local command_pattern = "^%"..(bot.config.command_prefix or "@").."([%a%-%_%d]+)(%s?)(.*)$"; | |
5 | ||
6 | local function process_command(event) | |
14
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
0
diff
changeset
|
7 | local body = event.body; |
0 | 8 | if not body then return; end |
14
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
0
diff
changeset
|
9 | if event.delay then return; end -- Don't process old messages from groupchat |
0 | 10 | |
11 | local command, hasparam, param = body:match(command_pattern); | |
12 | ||
13 | if not command then | |
14 | command, hasparam, param = body:match("%[([%a%-%_%d]+)(%s?)(.*)%]"); | |
15 | end | |
16 | ||
17 | if hasparam ~= " " then param = nil; end | |
18 | ||
19 | if command then | |
20 | local command_event = { | |
21 | command = command, | |
22 | param = param, | |
14
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
0
diff
changeset
|
23 | sender = event.sender, |
0 | 24 | stanza = event.stanza, |
25 | reply = event.reply, | |
14
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
0
diff
changeset
|
26 | room = event.room, -- groupchat support |
0 | 27 | }; |
28 | local ret = bot:event("commands/"..command, command_event); | |
29 | if type(ret) == "string" then | |
30 | event:reply(ret); | |
31 | end | |
32 | return ret; | |
33 | end | |
34 | end | |
35 | ||
14
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
0
diff
changeset
|
36 | -- Hook messages sent to bot, fire a command event on the bot |
0 | 37 | bot:hook("message", process_command); |
38 | ||
14
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
0
diff
changeset
|
39 | -- Support groupchat plugin: Hook messages from rooms that the bot joins |
0 | 40 | bot:hook("groupchat/joining", function (room) |
41 | room:hook("message", process_command); | |
42 | end); | |
43 | end |