plugins/commands.lua

Mon, 08 Nov 2010 22:41:24 +0100

author
Kim Alvefur <zash@zash.se>
date
Mon, 08 Nov 2010 22:41:24 +0100
changeset 35
0f17e574b6bb
parent 33
37caf7bd2021
child 58
3e5b57d44fa0
permissions
-rw-r--r--

plugins.commands: Enable commands to be directed to the bots nick

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);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 if type(ret) == "string" then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 event:reply(ret);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 return ret;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
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
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
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 bot:hook("message", process_command);
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 -- 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
47 bot:hook("groupchat/joining", function (room)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 room:hook("message", process_command);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end

mercurial