mcbot/cmds/xep.lua
author Mikael Berthe <mikael@lilotux.net>
Tue, 13 Apr 2010 22:45:12 +0200
changeset 7 c515e8218c87
parent 5 a0b9529f17db
child 11 4993fc6b1108
permissions -rw-r--r--
Small optimization
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
require "libs.shcmd"
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
-- The caching idea is borrowed from MattJ's riddim code,
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
-- and the function parse_xeps() as well.
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
local xeplisturl = "curl http://xmpp.org/extensions/xeps.xml"
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
local xeps = {}
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
-- parse_xep() from Matthew Wild (aka MattJ)
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
local function parse_xeps(t)
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
if not t or t == "" then return nil end
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
local currxep = {}
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
    for b in string.gmatch(t,"<xep>(.-)</xep>") do
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
        for k,v in string.gmatch(b,"<(%w+)>(.-)</%1>") do
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
            currxep[k] = v
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
        end
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
        xeps[currxep.number] = { }
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
        for k, v in pairs(currxep) do xeps[currxep.number][k] = v end
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
    end
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
    return true
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
end
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
local function fetch_xeps ()
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
    local now = os.time()
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
    if os.difftime(now, xeps_updated_at) < 43200 then return end
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
    -- Let's get some fresh data
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
    local xepdata = shcmd(xeplisturl)
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
    xeps_updated_at = now
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
    parse_xeps(xepdata)
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
end
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
-- Logics comes from MattJ's riddim bot
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
local function lookup_xep (xepnum)
7
c515e8218c87 Small optimization
Mikael Berthe <mikael@lilotux.net>
parents: 5
diff changeset
    36
    if (xepnum) then xepnum = xepnum:gsub("[%s%?%.%!]+$", "") end
3
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
    -- Check that xepnum is a valid number
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
    xepnum = tonumber(xepnum)
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
    if not xepnum or xepnum <= 0 or xepnum > 9999 then return nil, "What XEP?" end
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
    xepnum = tostring(xepnum)
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
    -- Download XEP list
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
    fetch_xeps()
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
    -- Expand to full 4 char number
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
    xepnum = string.rep("0", 4-xepnum:len())..xepnum
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
    xep = xeps[tostring(xepnum)]
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
    if not xep then return nil,"Sorry, XEP-"..xepnum.." not found" end
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
    return "XEP-"..xep.number.." is \""..xep.name.."\"\n"..
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
           -- xep.type..", "..xep.status..", last updated "..xep.updated.."\n"..
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
           "URL: <http://xmpp.org/extensions/xep-"..xep.number..".html>"
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
end
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
mcbot_register_command("xep", lookup_xep)