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