summaryrefslogtreecommitdiffstats
blob: 5862981f9e5e38b80928eac8cae39eb2d26ab544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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:upper()] = 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"].."("..xep["shortname"].."): http://xmpp.org/extensions/xep-"..xep["number"]..".html\n"..xep["name"].." - "..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:upper()]
	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)