mcbot/cmds/xep.lua
author Mikael Berthe <mikael@lilotux.net>
Wed, 14 Apr 2010 22:17:59 +0200
changeset 11 4993fc6b1108
parent 7 c515e8218c87
child 16 064a50911e05
permissions -rw-r--r--
Improve xep command Can search by string now
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
11
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    34
-- Deeply inspired by MattJ's riddim bot
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    35
local function lookup_xep (xepstr)
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    36
    if (xepstr) then
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    37
        xepstr = xepstr:gsub("[%s%?%.%!]+$", "")
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    38
        local n = xepstr:upper():match("^XEP[%-%s]?(%d+)$")
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    39
        if n then xepstr = n end
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    40
    end
3
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
    -- Check that xepnum is a valid number
11
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    42
    xepnum = tonumber(xepstr)
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    43
    if xepnum and (xepnum <= 0 or xepnum > 9999) then
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    44
        return nil, "What XEP?"
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    45
    end
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    46
    if not xepnum and not xepstr:match("^[%w%s%-_]+$") then
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    47
        return nil, "What XEP?  Please use a simple keyword..."
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    48
    end
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    49
3
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
    -- Download XEP list
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
    fetch_xeps()
11
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    52
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    53
    -- #1 The argument is a XEP number
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    54
    if xepnum then
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    55
        xepnum = tostring(xepnum)
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    56
        -- Expand to full 4 char number
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    57
        xepnum = string.rep("0", 4-xepnum:len())..xepnum
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    58
        local xep = xeps[tostring(xepnum)]
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    59
        if not xep then return nil, "Sorry, XEP-"..xepnum.." not found" end
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    60
        return "XEP-"..xep.number.." is \""..xep.name.."\"\n"..
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    61
               -- xep.type..", "..xep.status..", last updated "..xep.updated.."\n"..
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    62
               "URL: <http://xmpp.org/extensions/xep-"..xep.number..".html>"
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    63
    end
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    64
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    65
    -- #2 The argument is a keyword
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    66
    local r = ""
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    67
    xepstr = xepstr:lower()
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    68
    for n, xep in pairs(xeps) do
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    69
        if xep.name:lower():find(xepstr) then
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    70
            r = r.."XEP-"..xep.number..": "..xep.name.."\n"
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    71
        end
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    72
    end
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    73
    r = r:gsub("\n$", "")
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    74
    if r ~= "" then return r; end
4993fc6b1108 Improve xep command
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    75
    return nil, "Sorry, not matching XEP found"
3
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
end
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
mcbot_register_command("xep", lookup_xep)