author | Mikael Berthe <mikael@lilotux.net> |
Sat, 17 Apr 2010 23:35:13 +0200 | |
changeset 30 | e5e20eed6c58 |
parent 27 | a166e9ab8065 |
child 31 | 71bf91612a33 |
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" |
|
29 |
require "cmds.misc" |
|
3 | 30 |
require "cmds.xep" |
6
7cf014d0d206
Add command bts/bug (mcabber_bts)
Mikael Berthe <mikael@lilotux.net>
parents:
3
diff
changeset
|
31 |
require "cmds.mcabber_bts" |
23 | 32 |
require "cmds.bofh" |
25
788c62c29e18
Add a simple xmpp command
Mikael Berthe <mikael@lilotux.net>
parents:
23
diff
changeset
|
33 |
require "cmds.xmpp" |
0 | 34 |
|
27
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
35 |
function process (line, botdata, muc, cmdprefix) |
0 | 36 |
local n |
37 |
line = trim(line) |
|
26
c12be727375b
Do not require ':' or ',' after the bot name
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
38 |
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
|
39 |
if muc and n ~= 1 then |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
40 |
if cmdprefix and line:starts(cmdprefix) then |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
41 |
line = line:sub(string.len(cmdprefix)+1) |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
42 |
line = line:gsub("^%s*", "") |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
43 |
else |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
44 |
return nil, nil |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
45 |
end |
a166e9ab8065
Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
46 |
end |
0 | 47 |
|
48 |
local cmd, arg |
|
49 |
cmd = line:match("^(%w+)%s*[!]*$") |
|
50 |
if not cmd then |
|
51 |
-- Check if there are arguments |
|
52 |
cmd, arg = line:match("^(%w+)%s+(.*)") |
|
53 |
end |
|
54 |
if cmd then |
|
18
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
55 |
local lccmd = cmd:lower() |
16
064a50911e05
Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
56 |
for name, obj in pairs(commands) do |
18
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
57 |
if lccmd == name then |
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
58 |
return obj.cmd(arg, botdata, cmd) |
0 | 59 |
end |
19 | 60 |
if obj.aliases then |
61 |
for i, alias in ipairs(obj.aliases) do |
|
62 |
if lccmd == alias then |
|
63 |
return obj.cmd(arg, botdata, cmd) |
|
64 |
end |
|
65 |
end |
|
66 |
end |
|
0 | 67 |
end |
68 |
else |
|
69 |
-- Ignore smileys |
|
70 |
if line:match("^[:;8]'?%-?[D()|/\\%[%]pPoO$@]+%s*$") then return nil,nil end |
|
71 |
if line:match("^^^%s*$") then return nil,nil end |
|
72 |
end |
|
73 |
return "I don't understand, Sir" |
|
74 |
end |