plugins/commands.lua

Sat, 22 Jan 2011 17:12:20 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 22 Jan 2011 17:12:20 +0000
changeset 58
3e5b57d44fa0
parent 35
0f17e574b6bb
child 102
b7c9a3707e68
permissions
-rw-r--r--

plugin.commands: Fire unhandled-command event

0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 function riddim.plugins.commands(bot)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local command_pattern = "^%"..(bot.config.command_prefix or "@").."([%a%-%_%d]+)(%s?)(.*)$";
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local command, hasparam, param = body:match(command_pattern);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 if not command then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 command, hasparam, param = body:match("%[([%a%-%_%d]+)(%s?)(.*)%]");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 if hasparam ~= " " then param = nil; end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if command then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local ret = bot:event("commands/"..command, command_event);
58
3e5b57d44fa0 plugin.commands: Fire unhandled-command event
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
36 if ret == nil then
3e5b57d44fa0 plugin.commands: Fire unhandled-command event
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
37 ret = bot:event("unhandled-command", command_event);
3e5b57d44fa0 plugin.commands: Fire unhandled-command event
Matthew Wild <mwild1@gmail.com>
parents: 35
diff changeset
38 end
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 if type(ret) == "string" then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 event:reply(ret);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 return ret;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
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 -- Hook messages sent to bot, fire a command event on the bot
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 bot:hook("message", process_command);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
14
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 0
diff changeset
49 -- Support groupchat plugin: Hook messages from rooms that the bot joins
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 bot:hook("groupchat/joining", function (room)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 room:hook("message", process_command);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end

mercurial