author | Mikael Berthe <mikael@lilotux.net> |
Tue, 13 Apr 2010 21:07:41 +0200 | |
changeset 3 | 508108deee63 |
parent 1 | cca972635e5e |
child 6 | 7cf014d0d206 |
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 |
function mcbot_register_command (name, callback) |
|
17 |
commands[name] = callback |
|
18 |
end |
|
19 |
||
20 |
require "cmds.wtf" |
|
21 |
require "cmds.calc" |
|
22 |
require "cmds.spell" |
|
23 |
require "cmds.dict" |
|
24 |
require "cmds.misc" |
|
3 | 25 |
require "cmds.xep" |
0 | 26 |
|
1
cca972635e5e
Provid more data to the bot functions
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
27 |
function process (line, botdata, muc) |
0 | 28 |
local n |
29 |
line = trim(line) |
|
1
cca972635e5e
Provid more data to the bot functions
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
30 |
line, n = line:gsub("^"..botdata.nickname.."[,: ]%s+", "") |
0 | 31 |
if muc and n ~= 1 then return nil, nil end |
32 |
||
33 |
local cmd, arg |
|
34 |
cmd = line:match("^(%w+)%s*[!]*$") |
|
35 |
if not cmd then |
|
36 |
-- Check if there are arguments |
|
37 |
cmd, arg = line:match("^(%w+)%s+(.*)") |
|
38 |
end |
|
39 |
if cmd then |
|
40 |
for name, f in pairs(commands) do |
|
41 |
if cmd and cmd:lower() == name then |
|
1
cca972635e5e
Provid more data to the bot functions
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
42 |
return f(arg, botdata) |
0 | 43 |
end |
44 |
end |
|
45 |
else |
|
46 |
-- Ignore smileys |
|
47 |
if line:match("^[:;8]'?%-?[D()|/\\%[%]pPoO$@]+%s*$") then return nil,nil end |
|
48 |
if line:match("^^^%s*$") then return nil,nil end |
|
49 |
end |
|
50 |
return "I don't understand, Sir" |
|
51 |
end |