author | Mikael Berthe <mikael@lilotux.net> |
Sun, 25 Nov 2012 19:01:31 +0100 | |
changeset 64 | 62248d5fbb24 |
parent 54 | be04d7241ef6 |
child 66 | d9c00a9fe9d5 |
permissions | -rw-r--r-- |
0 | 1 |
#! /usr/bin/env lua |
2 |
||
3 |
local commands = {} |
|
4 |
||
5 |
function trim(s) |
|
6 |
return s:match("^%s*(.-)%s*$") |
|
7 |
end |
|
8 |
function rtrim(s) |
|
9 |
return s:gsub("%s+$", "") |
|
10 |
end |
|
11 |
||
27
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
12 |
function string.starts(String, Start) |
0 | 13 |
return string.sub(String, 1, string.len(Start)) == Start |
14 |
end |
|
15 |
||
16
064a50911e05
Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
16 |
function mcbot_register_command (name, object) |
064a50911e05
Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
17 |
commands[name] = object |
0 | 18 |
end |
19 |
||
18
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
20 |
function mcbot_get_command_list () |
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
21 |
return commands |
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
22 |
end |
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
23 |
|
21 | 24 |
require "cmds.help" |
0 | 25 |
require "cmds.wtf" |
26 |
require "cmds.calc" |
|
27 |
require "cmds.spell" |
|
28 |
require "cmds.dict" |
|
54 | 29 |
require "cmds.littre" |
0 | 30 |
require "cmds.misc" |
3 | 31 |
require "cmds.xep" |
31 | 32 |
require "cmds.rfc" |
6
7cf014d0d206
Add command bts/bug (mcabber_bts)
Mikael Berthe <mikael@lilotux.net>
parents:
3
diff
changeset
|
33 |
require "cmds.mcabber_bts" |
23 | 34 |
require "cmds.bofh" |
25
788c62c29e18
Add a simple xmpp command
Mikael Berthe <mikael@lilotux.net>
parents:
23
diff
changeset
|
35 |
require "cmds.xmpp" |
35 | 36 |
require "cmds.tvcal" |
64
62248d5fbb24
Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents:
54
diff
changeset
|
37 |
require "cmds.shortenurl" |
0 | 38 |
|
27
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
39 |
function process (line, botdata, muc, cmdprefix) |
0 | 40 |
local n |
41 |
line = trim(line) |
|
26
c12be727375b
Do not require ':' or ',' after the bot name
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
42 |
line, n = line:gsub("^"..botdata.nickname.."[,:]?%s+", "") |
27
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
43 |
if muc and n ~= 1 then |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
44 |
if cmdprefix and line:starts(cmdprefix) then |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
45 |
line = line:sub(string.len(cmdprefix)+1) |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
46 |
line = line:gsub("^%s*", "") |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
47 |
else |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
48 |
return nil, nil |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
49 |
end |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
50 |
end |
0 | 51 |
|
52 |
local cmd, arg |
|
53 |
cmd = line:match("^(%w+)%s*[!]*$") |
|
54 |
if not cmd then |
|
55 |
-- Check if there are arguments |
|
56 |
cmd, arg = line:match("^(%w+)%s+(.*)") |
|
57 |
end |
|
58 |
if cmd then |
|
18
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
59 |
local lccmd = cmd:lower() |
16
064a50911e05
Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
60 |
for name, obj in pairs(commands) do |
18
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
61 |
if lccmd == name then |
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
62 |
return obj.cmd(arg, botdata, cmd) |
0 | 63 |
end |
19 | 64 |
if obj.aliases then |
65 |
for i, alias in ipairs(obj.aliases) do |
|
66 |
if lccmd == alias then |
|
67 |
return obj.cmd(arg, botdata, cmd) |
|
68 |
end |
|
69 |
end |
|
70 |
end |
|
0 | 71 |
end |
72 |
else |
|
73 |
-- Ignore smileys |
|
74 |
if line:match("^[:;8]'?%-?[D()|/\\%[%]pPoO$@]+%s*$") then return nil,nil end |
|
75 |
if line:match("^^^%s*$") then return nil,nil end |
|
76 |
end |
|
77 |
return "I don't understand, Sir" |
|
78 |
end |