plugins/version.lua

1
local st = require "prosody.util.stanza";
2
 
3
local xmlns_version = "jabber:iq:version";
4
 
5
local friendly_errors = {
6
	["service-unavailable"] = " doesn't reply to version requests";
7
	["feature-not-implemented"] = " doesn't support version requests";
8
	["remote-server-not-found"] = " can't be reached via XMPP";
9
}
10
 
11
function riddim.plugins.version(bot)
12
	bot.stream:add_plugin("version");
13
	bot.stream.version:set{
14
		name = bot.config.bot_name or "Riddim";
15
		version = bot.config.bot_version or "alpha";
16
		platform = bot.config.bot_platform or _VERSION;
17
	};
18
 
19
	bot:add_plugin("resolvejid");
20
	bot:hook("commands/version", function (command)
21
		local who, param = bot.stream.jid, command.param;
22
		if param then
23
			who = bot:resolvejid(param, command.room);
24
		end
25
		
26
		bot.stream:query_version(who, function (reply)
27
			if not reply.error then
28
				local saywho = (who == command.sender.jid and "You are") or (param and param.." is" or "I am");
29
				local isrunning = saywho.." running "..(reply.name or "something");
30
				if reply.version then
31
					isrunning = isrunning .." version "..reply.version;
32
				end
33
				if reply.platform then
34
					isrunning = isrunning .." on "..reply.platform;
35
				end
36
				command:reply(isrunning);
37
			else
38
				local type, condition, text = reply.type, reply.condition, reply.text;
39
				local r = "There was an error requesting "..param.."'s version";
40
				local friendly_error = friendly_errors[condition];
41
				if friendly_error then
42
					r = r .. friendly_error;
43
				elseif condition and not text then
44
					r = r..": "..(condition):gsub("%-", "  ");
45
				end
46
				if text then
47
					r = r .. " ("..text..")";
48
				end
49
				command:reply(r);
50
			end
51
		end);
52
		return true;
53
	end);	
54
end