mcbot/cmds/tvcal.lua
changeset 38 a51ddbce247b
parent 37 9d7f19e4e4fe
child 39 237af42156a1
equal deleted inserted replaced
37:9d7f19e4e4fe 38:a51ddbce247b
    69     end
    69     end
    70     r = r:gsub("\n+$", "")
    70     r = r:gsub("\n+$", "")
    71     return r
    71     return r
    72 end
    72 end
    73 
    73 
    74 local function tvcal_by_name (name)
    74 local function tvcal_by_name (name, plain)
       
    75     local action = plain and "contain" or "match"
    75     local r = ""
    76     local r = ""
    76     name = name:lower()
    77     name = name:lower()
    77 
    78 
    78     for day, obj in pairsByKeys(tvcaldata) do
    79     for day, obj in pairsByKeys(tvcaldata) do
    79         for i,j in ipairs(obj) do
    80         for i,j in ipairs(obj) do
    80             if j.name:lower():find(name, 1, true) then
    81             if j.name:lower():find(name, 1, plain) then
    81                 r = r .. day .. "\t" .. j.name .. " (" ..j.ep .. ")\n"
    82                 r = r .. day .. "\t" .. j.name .. " (" ..j.ep .. ")\n"
    82             end
    83             end
    83         end
    84         end
    84     end
    85     end
    85 
    86 
    86     if r == "" then
    87     if r == "" then
    87         return "No episode found containing this string"
    88         return "No episode found "..action.."ing this string"
    88     end
    89     end
    89     r = r:gsub("\n+$", "")
    90     r = r:gsub("\n+$", "")
    90     return "Episodes containing \""..name.."\":\n" .. r
    91     return "Episodes "..action.."ing \""..name.."\":\n" .. r
    91 end
    92 end
    92 
    93 
    93 function tvcal.cmd (arg)
    94 function tvcal.cmd (arg)
    94     if not tvcaldata_timestamp or tvcaldata_timestamp ~= os.date("%F") then
    95     if not tvcaldata_timestamp or tvcaldata_timestamp ~= os.date("%F") then
    95         tvcaldata = {} -- Empty the previous list
    96         tvcaldata = {} -- Empty the previous list
   115 
   116 
   116     if arg:match("^%d%d%d%d%-%d%d%-%d%d$") then
   117     if arg:match("^%d%d%d%d%-%d%d%-%d%d$") then
   117         return tvcal_by_date(arg)
   118         return tvcal_by_date(arg)
   118     end
   119     end
   119 
   120 
   120     return tvcal_by_name(arg)
   121     -- Is it a pattern? (string surrounded by quotes)
       
   122     local plain = arg:match('^"(.*)"$')
       
   123     if plain then
       
   124         arg = plain
       
   125         plain = false
       
   126     else
       
   127         plain = true
       
   128     end
       
   129     return tvcal_by_name(arg, plain)
   121 end
   130 end
   122 
   131 
   123 mcbot_register_command("tvcal", tvcal)
   132 mcbot_register_command("tvcal", tvcal)