Sat, 27 Jun 2015 22:32:23 +0200
Add plugin for fetching issue info from GitHub
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?)(.*)%]"); | |
102
b7c9a3707e68
plugins.commands: If a message with a [command] was directed at someone, remember their nickname.
Kim Alvefur <zash@zash.se>
parents:
58
diff
changeset
|
22 | if event.room then |
b7c9a3707e68
plugins.commands: If a message with a [command] was directed at someone, remember their nickname.
Kim Alvefur <zash@zash.se>
parents:
58
diff
changeset
|
23 | local direct_to = body:match"^(.-)[,:]" |
b7c9a3707e68
plugins.commands: If a message with a [command] was directed at someone, remember their nickname.
Kim Alvefur <zash@zash.se>
parents:
58
diff
changeset
|
24 | if event.room.occupants[direct_to] then |
b7c9a3707e68
plugins.commands: If a message with a [command] was directed at someone, remember their nickname.
Kim Alvefur <zash@zash.se>
parents:
58
diff
changeset
|
25 | event.reply_to = direct_to |
b7c9a3707e68
plugins.commands: If a message with a [command] was directed at someone, remember their nickname.
Kim Alvefur <zash@zash.se>
parents:
58
diff
changeset
|
26 | end |
b7c9a3707e68
plugins.commands: If a message with a [command] was directed at someone, remember their nickname.
Kim Alvefur <zash@zash.se>
parents:
58
diff
changeset
|
27 | end |
0 | 28 | end |
29 | ||
30 | if hasparam ~= " " then param = nil; end | |
31 | ||
32 | if command then | |
33 | local command_event = { | |
16
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
14
diff
changeset
|
34 | command = command, |
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
14
diff
changeset
|
35 | param = param, |
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
14
diff
changeset
|
36 | sender = event.sender, |
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
14
diff
changeset
|
37 | stanza = event.stanza, |
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
14
diff
changeset
|
38 | reply = event.reply, |
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
14
diff
changeset
|
39 | room = event.room, -- groupchat support |
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
14
diff
changeset
|
40 | }; |
0 | 41 | 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
|
42 | if ret == nil then |
3e5b57d44fa0
plugin.commands: Fire unhandled-command event
Matthew Wild <mwild1@gmail.com>
parents:
35
diff
changeset
|
43 | ret = bot:event("unhandled-command", command_event); |
3e5b57d44fa0
plugin.commands: Fire unhandled-command event
Matthew Wild <mwild1@gmail.com>
parents:
35
diff
changeset
|
44 | end |
0 | 45 | if type(ret) == "string" then |
46 | event:reply(ret); | |
47 | end | |
48 | return ret; | |
49 | end | |
50 | end | |
51 | ||
14
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
0
diff
changeset
|
52 | -- Hook messages sent to bot, fire a command event on the bot |
0 | 53 | bot:hook("message", process_command); |
54 | ||
14
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
0
diff
changeset
|
55 | -- Support groupchat plugin: Hook messages from rooms that the bot joins |
0 | 56 | bot:hook("groupchat/joining", function (room) |
57 | room:hook("message", process_command); | |
58 | end); | |
59 | end |