mcbot/cmds/mcabber_bts.lua
author Mikael Berthe <mikael@lilotux.net>
Mon, 12 Oct 2015 12:42:27 +0200
changeset 72 01a4b9048880
parent 66 d9c00a9fe9d5
permissions -rw-r--r--
Fix mcabber_bts


--  This module is part of the McBot / mcabbot project
--
-- 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.
--

require "libs.shcmd"

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

local function get_issue_details (num)
    local item

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

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

    -- Get Status
    item = data:match('<a href="[^"]+/issues%?status=(%w+)"')
    if item then issue.status = item end
    -- Get Type
    item = data:match('<a href="[^"]+/issues%?kind=(%w+)"')
    if item then issue.type = item end
    -- Get Priority
    item = data:match('<a href="[^"]+/issues%?priority=(%w+)"')
    if item then issue.priority = item end

    return issue
end

function mcabber_bts.cmd (num)
    -- Check that xepnum is a valid number
    if num then
        num = num:gsub("[%s%?%.%!]+$", "")
        num = num:gsub("^#", "")
    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
    if issue.type then
        result = result .. "Priority: " .. issue.priority .. "\n"
    end
    return result .. "URL: <"..tostring(issue.url)..">"
end

mcbot_register_command("mcabberbts", mcabber_bts)