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