multiple patterns

This commit is contained in:
yago 2014-11-04 22:06:59 +01:00
parent 6ef55411a1
commit 7d8abde1d1
12 changed files with 38 additions and 36 deletions

View File

@ -1,7 +1,7 @@
http = require("socket.http")
URL = require("socket.url")
json = (loadfile "./bot/JSON.lua")()
lrexlib = require("rex_pcre")
-- lrexlib = require("rex_pcre")
VERSION = 'v0.6'
@ -123,21 +123,23 @@
-- Where magic happens
function do_action(msg)
local receiver = get_receiver(msg)
local text = msg.text:sub(2) -- removes the '!'
print("Received msg", msg.text)
--local text = msg.text:sub(2) -- removes the '!'
local text = msg.text
print("Received msg", text)
for name, desc in pairs(plugins) do
regexp = desc.regexp
runmethod = desc.run
print("Trying", text, "against", regexp)
matches = { string.match(text, regexp) }
print("Trying module", name)
for k, pattern in pairs(desc.patterns) do
print("Trying", text, "against", pattern)
matches = { string.match(text, pattern) }
if matches[1] then
print(" matches!")
result = runmethod(msg, matches)
print(" should return", result)
if (result) then
send_msg(receiver, result, ok_cb, false)
end
print(" matches!")
result = desc.run(msg, matches)
print(" should return", result)
if (result) then
send_msg(receiver, result, ok_cb, false)
end
end
end
end
end
@ -189,7 +191,7 @@
-- end
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
return string.sub(String,1,string.len(Start))==Start
end
function load_config()

View File

@ -21,9 +21,9 @@ function run(msg, matches)
end
return {
description = "send random url image from 9gag",
usage = "9gag",
regexp = "^9gag$",
description = "send random image from 9gag",
usage = "!9gag",
patterns = {"^!9gag$"},
run = run
}

View File

@ -5,8 +5,8 @@ end
return {
description = "echoes the msg",
usage = "echo [whatever]",
regexp = "^echo (.*)$",
usage = "!echo [whatever]",
patterns = {"^!echo (.*)$"},
run = run
}

View File

@ -20,8 +20,8 @@ end
return {
description = "EURUSD market value",
usage = "eur [USD]",
regexp = "^eur (%d+[%d%.]*)$",
usage = "!eur [USD]",
patterns = {"^!eur (%d+[%d%.]*)$"},
run = run
}

View File

@ -14,8 +14,8 @@ end
return {
description = "Fortunes from Universidad Carlos III",
usage = "uc3m",
regexp = "^uc3m$",
usage = "!uc3m",
patterns = {"^!uc3m$"},
run = run
}

View File

@ -6,7 +6,7 @@ end
return {
description = "Says hello to someone",
usage = "say hello to [name]",
regexp = "^say hello to (.*)$",
patterns = {"^say hello to (.*)$"},
run = run
}

View File

@ -9,8 +9,8 @@ end
return {
description = "Lists all available commands",
usage = "help",
regexp = "^help$",
usage = "!help",
patterns = {"^!help$"},
run = run
}

View File

@ -26,8 +26,8 @@ end
return {
description = "search image with Google API and sends it",
usage = "img [topic]",
regexp = "^img (.*)$",
usage = "!img [topic]",
patterns = {"^!img (.*)$"},
run = run
}

View File

@ -7,8 +7,8 @@ end
return {
description = "bot sends pong",
usage = "ping",
regexp = "^ping$",
usage = "!ping",
patterns = {"^!ping$"},
run = run
}

View File

@ -39,8 +39,8 @@ end
return {
description = "Spanish dictionary",
usage = "rae [word]",
regexp = "^rae (.*)$",
usage = "!rae [word]",
patterns = {"^!rae (.*)$"},
run = run
}

View File

@ -9,8 +9,8 @@ end
return {
description = "Shows the bot version",
usage = "version",
regexp = "^version$",
usage = "!version",
patterns = {"^!version$"},
run = run
}

View File

@ -32,8 +32,8 @@ end
return {
description = "weather in that city (Madrid is default)",
usage = "weather [city]",
regexp = "^weather(.*)$",
usage = "!weather (city)",
patterns = {"^!weather(.*)$"},
run = run
}