plugins/commands.lua

changeset 14
3df63aaba9e3
parent 0
7d84f4403d67
child 16
ae69cea97598
equal deleted inserted replaced
11:7bb53dcf93d4 14:3df63aaba9e3
1 require "util.xstanza" 1 require "util.xstanza"
2 2
3 function riddim.plugins.commands(bot) 3 function riddim.plugins.commands(bot)
4 local function handle_message(message)
5 -- Parse message body
6 end
7
8 local command_pattern = "^%"..(bot.config.command_prefix or "@").."([%a%-%_%d]+)(%s?)(.*)$"; 4 local command_pattern = "^%"..(bot.config.command_prefix or "@").."([%a%-%_%d]+)(%s?)(.*)$";
9 5
10 local function process_command(event) 6 local function process_command(event)
11 local body, sender = event.body, event.sender; 7 local body = event.body;
12 if not body then return; end 8 if not body then return; end
13 if event.delay then return; end -- Don't process old messages 9 if event.delay then return; end -- Don't process old messages from groupchat
14 10
15 local command, hasparam, param = body:match(command_pattern); 11 local command, hasparam, param = body:match(command_pattern);
16 12
17 if not command then 13 if not command then
18 command, hasparam, param = body:match("%[([%a%-%_%d]+)(%s?)(.*)%]"); 14 command, hasparam, param = body:match("%[([%a%-%_%d]+)(%s?)(.*)%]");
22 18
23 if command then 19 if command then
24 local command_event = { 20 local command_event = {
25 command = command, 21 command = command,
26 param = param, 22 param = param,
27 sender = sender, 23 sender = event.sender,
28 stanza = event.stanza, 24 stanza = event.stanza,
29 reply = event.reply, 25 reply = event.reply,
30 room = event.room, 26 room = event.room, -- groupchat support
31 }; 27 };
32 local ret = bot:event("commands/"..command, command_event); 28 local ret = bot:event("commands/"..command, command_event);
33 if type(ret) == "string" then 29 if type(ret) == "string" then
34 event:reply(ret); 30 event:reply(ret);
35 end 31 end
36 return ret; 32 return ret;
37 end 33 end
38 end 34 end
39 35
40 -- Hook messages to bot and from rooms, fire a command event on the bot 36 -- Hook messages sent to bot, fire a command event on the bot
41 bot:hook("message", process_command); 37 bot:hook("message", process_command);
42 38
39 -- Support groupchat plugin: Hook messages from rooms that the bot joins
43 bot:hook("groupchat/joining", function (room) 40 bot:hook("groupchat/joining", function (room)
44 room:hook("message", process_command); 41 room:hook("message", process_command);
45 end); 42 end);
46 end 43 end

mercurial