mcbot/cmds/mcabber_bts.lua
author Mikael Berthe <mikael@lilotux.net>
Thu, 15 Apr 2010 20:28:34 +0200
changeset 20 d6c602aaa231
parent 16 064a50911e05
child 39 237af42156a1
permissions -rw-r--r--
Use aliases


require "libs.shcmd"

local mcabber_bts = { ["desc"] = "Query mcabber (crew) BTS",
                      ["aliases"] = { "bug", "issue" }
                    }

local function get_issue_details (num)
    local item

    num = tonumber(num);
    if not num then return nil end
    local issue = {}
    local btsurl = "http://bitbucket.org/McKael/mcabber-crew/issue/"

    issue.id = tostring(num)
    issue.url = btsurl..issue.id.."/"
    local data = shcmd("curl "..issue.url)
    local title = data:match("#"..issue.id.."%s*-%s*(.*)%s*&mdash;")
    if not title then return nil end
    issue.title = title:gsub("&quot;", "\""):gsub("&amp;", "&")
    issue.title = issue.title:gsub("&lt;", "<"):gsub("&gt;", ">")
    issue.title = issue.title:gsub("&#39;", "'")

    -- Get Status
    item = data:match("Status:%s*</td>%s*<td [^>]*>(.-)</td>")
    if item then issue.status = item end
    -- Get Type
    item = data:match("Type:%s*</td>%s*<td [^>]*>(.-)</td>")
    if item then issue.type = item end

    return issue
end

function mcabber_bts.cmd (num)
    -- Check that xepnum is a valid number
    if num then num = num:gsub("[%s%?%.%!]+$", "") end
    num = tonumber(num)
    if not num or num < 1 then
        return nil, "Please provide an issue number"
    end
    local issue = get_issue_details(num)
    if not issue then
        return nil,"Sorry, couldn't find issue "..tostring(num)
    end

    local result = "Issue #"..issue.id.." -- "..issue.title.."\n"
    if issue.status then
        result = result .. "Status: " .. issue.status .. "\n"
    end
    if issue.type then
        result = result .. "Type: " .. issue.type .. "\n"
    end
    return result .. "URL: <"..tostring(issue.url)..">"
end

mcbot_register_command("bts", mcabber_bts)