author | Mikael Berthe <mikael@lilotux.net> |
Thu, 15 Apr 2010 20:56:25 +0200 | |
changeset 21 | bc5d611e6d6f |
parent 19 | 96e08713cb6a |
child 23 | 491f7d046353 |
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 |
||
12 |
function string.starts(String,Start) |
|
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" |
0 | 32 |
|
1
cca972635e5e
Provid more data to the bot functions
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
33 |
function process (line, botdata, muc) |
0 | 34 |
local n |
35 |
line = trim(line) |
|
1
cca972635e5e
Provid more data to the bot functions
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
36 |
line, n = line:gsub("^"..botdata.nickname.."[,: ]%s+", "") |
0 | 37 |
if muc and n ~= 1 then return nil, nil end |
38 |
||
39 |
local cmd, arg |
|
40 |
cmd = line:match("^(%w+)%s*[!]*$") |
|
41 |
if not cmd then |
|
42 |
-- Check if there are arguments |
|
43 |
cmd, arg = line:match("^(%w+)%s+(.*)") |
|
44 |
end |
|
45 |
if cmd then |
|
18
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
46 |
local lccmd = cmd:lower() |
16
064a50911e05
Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
47 |
for name, obj in pairs(commands) do |
18
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
48 |
if lccmd == name then |
615c9b336207
Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
49 |
return obj.cmd(arg, botdata, cmd) |
0 | 50 |
end |
19 | 51 |
if obj.aliases then |
52 |
for i, alias in ipairs(obj.aliases) do |
|
53 |
if lccmd == alias then |
|
54 |
return obj.cmd(arg, botdata, cmd) |
|
55 |
end |
|
56 |
end |
|
57 |
end |
|
0 | 58 |
end |
59 |
else |
|
60 |
-- Ignore smileys |
|
61 |
if line:match("^[:;8]'?%-?[D()|/\\%[%]pPoO$@]+%s*$") then return nil,nil end |
|
62 |
if line:match("^^^%s*$") then return nil,nil end |
|
63 |
end |
|
64 |
return "I don't understand, Sir" |
|
65 |
end |