diff options
author | Kevin Smith <git@kismith.co.uk> | 2012-07-28 17:41:07 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2012-07-28 17:41:07 (GMT) |
commit | 94949a9a0bc637905447a0dd666958ea5f08e729 (patch) | |
tree | 6e38549f477ab701cc11c656884ba873abaf7ad8 | |
parent | 432dadc21305572695e091de80aab3b5eace4f36 (diff) | |
download | swift-contrib-94949a9a0bc637905447a0dd666958ea5f08e729.zip swift-contrib-94949a9a0bc637905447a0dd666958ea5f08e729.tar.bz2 |
Simple XEP script for Swiftob
-rw-r--r-- | Swiftob/scripts/xep.lua | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/Swiftob/scripts/xep.lua b/Swiftob/scripts/xep.lua new file mode 100644 index 0000000..7d73c97 --- /dev/null +++ b/Swiftob/scripts/xep.lua @@ -0,0 +1,54 @@ +last_fetched = 0 +xeps_by_number = {} +xeps_by_shortname = {} + +function parse_xeps(body) + for xep_string in body:gmatch("<xep>.-</xep>") do + local number = xep_string:match("<number>(.+)</number>") + local name = xep_string:match("<name>(.+)</name>") + local xeptype = xep_string:match("<type>(.+)</type>") + local status = xep_string:match("<status>(.+)</status>") + local updated = xep_string:match("<updated>(.+)</updated>") + local shortname = xep_string:match("<shortname>(.+)</shortname>") + local abstract = xep_string:match("<abstract>(.+)</abstract>"):gsub("\n", "") + xep = {number=number, name=name, type=xeptype, status=status, updated=updated, shortname=shortname, abstract=abstract} + xeps_by_number[tonumber(number)] = xep + xeps_by_shortname[shortname] = xep + end +end + +function update_xeps() + local http = require("socket.http") + http.TIMEOUT = 5 + if os.time() > (last_fetched + 30 * 60) then + b, c, h = http.request("http://xmpp.org/extensions/xeps.xml") + if c == 200 then + last_fetched = os.time() + parse_xeps(b) + end + end +end + +function xep_to_string(xep) + return "XEP-"..xep["number"].."(Shortname "..xep["shortname"].."): "..xep["name"].."\n"..xep["type"].."/"..xep["status"].." Updated: "..xep["updated"] +end + +function xep_command(command, params, message) + update_xeps() + local xep_number = tonumber(params) + local xep + if xep_number then + xep = xeps_by_number[xep_number] + else + xep = xeps_by_shortname[params] + end + local reply + if xep then + reply = xep_to_string(xep) + else + reply = "Sorry, that XEP doesn't seem to exist" + end + swiftob_reply_to(message, reply) +end + +swiftob_register_command("xep", "Anyone", "Lookup XEP data", xep_command) |