mcbot/cmds/xep.lua
author Mikael Berthe <mikael@lilotux.net>
Tue, 27 Nov 2012 16:26:04 +0100
changeset 66 d9c00a9fe9d5
parent 39 237af42156a1
permissions -rw-r--r--
Add notices before public release


--  This module is part of the McBot / mcabbot project
--  XEP module.
--
-- Copyright (C) 2010-2012 Mikael Berthe <mikael@lilotux.net>
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or (at
-- your option) any later version.
--
-- Please check the license in the COPYING file at the root of the tree.
--
-- Some parts were inspired by Matthew Wild (MattJ) code, see below.
--

require "libs.shcmd"

-- The caching idea is borrowed from MattJ's riddim code,
-- and the function parse_xeps() as well.

local xeplisturl = "curl http://xmpp.org/extensions/xeps.xml"

local xep = { ["desc"] = "Search XEP database" }

local xeps = {}

-- parse_xep() from Matthew Wild (aka MattJ)
local function parse_xeps(t)
if not t or t == "" then return nil end
local currxep = {}
    for b in string.gmatch(t,"<xep>(.-)</xep>") do
        for k,v in string.gmatch(b,"<(%w+)>(.-)</%1>") do
            currxep[k] = v
        end
        xeps[currxep.number] = { }
        for k, v in pairs(currxep) do xeps[currxep.number][k] = v end
    end
    return true
end

local function fetch_xeps ()
    local now = os.time()
    if os.difftime(now, xeps_updated_at) < 43200 then return end
    -- Let's get some fresh data
    local xepdata = shcmd(xeplisturl)
    if xepdata then
        xeps_updated_at = now
        parse_xeps(xepdata)
    end
end

-- Deeply inspired by MattJ's riddim bot
function xep.cmd (xepstr)
    if (xepstr) then
        xepstr = xepstr:gsub("[%s%?%.%!]+$", "")
        local n = xepstr:upper():match("^XEP[%-%s]?(%d+)$")
        if n then xepstr = n end
    end
    -- Check that xepnum is a valid number
    xepnum = tonumber(xepstr)
    if xepnum and (xepnum <= 0 or xepnum > 9999) then
        return nil, "What XEP?"
    end
    if not xepnum and not xepstr:match("^[%w%s%-_]+$") then
        return nil, "What XEP?  Please use a simple keyword..."
    end

    -- Download XEP list
    fetch_xeps()

    -- #1 The argument is a XEP number
    if xepnum then
        xepnum = tostring(xepnum)
        -- Expand to full 4 char number
        xepnum = string.rep("0", 4-xepnum:len())..xepnum
        local xep = xeps[tostring(xepnum)]
        if not xep then return nil, "Sorry, XEP-"..xepnum.." not found" end
        return "XEP-"..xep.number.." is \""..xep.name.."\"\n"..
               -- xep.type..", "..xep.status..", last updated "..xep.updated.."\n"..
               "URL: <http://xmpp.org/extensions/xep-"..xep.number..".html>"
    end

    -- #2 The argument is a keyword
    local r = ""
    xepstr = xepstr:lower()
    for n, xep in pairs(xeps) do
        if xep.name:lower():find(xepstr) then
            r = r.."XEP-"..xep.number..": "..xep.name.."\n"
        end
    end
    r = r:gsub("\n$", "")
    if r ~= "" then return r; end
    return nil, "Sorry, no matching XEP found"
end

mcbot_register_command("xep", xep)