Add regex support to tvcal
authorMikael Berthe <mikael@lilotux.net>
Mon, 19 Apr 2010 10:20:28 +0200
changeset 38 a51ddbce247b
parent 37 9d7f19e4e4fe
child 39 237af42156a1
Add regex support to tvcal
mcbot/cmds/tvcal.lua
--- a/mcbot/cmds/tvcal.lua	Mon Apr 19 00:31:05 2010 +0200
+++ b/mcbot/cmds/tvcal.lua	Mon Apr 19 10:20:28 2010 +0200
@@ -71,23 +71,24 @@
     return r
 end
 
-local function tvcal_by_name (name)
+local function tvcal_by_name (name, plain)
+    local action = plain and "contain" or "match"
     local r = ""
     name = name:lower()
 
     for day, obj in pairsByKeys(tvcaldata) do
         for i,j in ipairs(obj) do
-            if j.name:lower():find(name, 1, true) then
+            if j.name:lower():find(name, 1, plain) then
                 r = r .. day .. "\t" .. j.name .. " (" ..j.ep .. ")\n"
             end
         end
     end
 
     if r == "" then
-        return "No episode found containing this string"
+        return "No episode found "..action.."ing this string"
     end
     r = r:gsub("\n+$", "")
-    return "Episodes containing \""..name.."\":\n" .. r
+    return "Episodes "..action.."ing \""..name.."\":\n" .. r
 end
 
 function tvcal.cmd (arg)
@@ -117,7 +118,15 @@
         return tvcal_by_date(arg)
     end
 
-    return tvcal_by_name(arg)
+    -- Is it a pattern? (string surrounded by quotes)
+    local plain = arg:match('^"(.*)"$')
+    if plain then
+        arg = plain
+        plain = false
+    else
+        plain = true
+    end
+    return tvcal_by_name(arg, plain)
 end
 
 mcbot_register_command("tvcal", tvcal)