plugins.{ping,version,resolvejid}: Break nickname/jid resolving into a common plugin

Fri, 31 May 2013 19:01:32 +0200

author
Kim Alvefur <zash@zash.se>
date
Fri, 31 May 2013 19:01:32 +0200
changeset 105
4234c8789cc6
parent 104
8202cdfb99b4
child 106
99d9d958bb09

plugins.{ping,version,resolvejid}: Break nickname/jid resolving into a common plugin

plugins/ping.lua file | annotate | diff | comparison | revisions
plugins/resolvejid.lua file | annotate | diff | comparison | revisions
plugins/version.lua file | annotate | diff | comparison | revisions
squishy file | annotate | diff | comparison | revisions
--- a/plugins/ping.lua	Sat May 25 23:21:30 2013 +0200
+++ b/plugins/ping.lua	Fri May 31 19:01:32 2013 +0200
@@ -1,9 +1,11 @@
 
 function riddim.plugins.ping(bot)
 	bot.stream:add_plugin("ping");
+	bot:add_plugin("resolvejid");
 	bot:hook("commands/ping", function (command)
 		local jid = command.param;
 		if jid then
+			jid = bot:resolvejid(jid, command.room);
 			bot.stream:ping(jid, function (time, jid, error)
 				if time then
 					command:reply(string.format("Pong from %s in %0.3f seconds", jid, time));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/resolvejid.lua	Fri May 31 19:01:32 2013 +0200
@@ -0,0 +1,13 @@
+function riddim.plugins.resolvejid(bot)
+	function bot:resolvejid(jid, room)
+		local nows = jid:match"%S+"
+		local trimd = jid:match"^%s*(.-)%s*$"
+		if room then
+			local occupant = room.occupants[jid]
+			or room.occupants[trimd]
+			or room.occupants[nows]
+			if occupant then return occupant.jid end
+		end
+		return nows
+	end
+end
--- a/plugins/version.lua	Sat May 25 23:21:30 2013 +0200
+++ b/plugins/version.lua	Fri May 31 19:01:32 2013 +0200
@@ -10,16 +10,11 @@
 		platform = bot.config.bot_platform or _VERSION;
 	};
 
+	bot:add_plugin("resolvejid");
 	bot:hook("commands/version", function (command)
 		local who, param = bot.stream.jid, command.param;
 		if param then
-			if command.room and command.room.occupants[param] then
-				who = command.room.occupants[param].jid;
-			elseif command.room and command.room.occupants[param:gsub("%s$", "")] then
-				who = command.room.occupants[param:gsub("%s$", "")].jid;
-			else
-				who = param;
-			end
+			who = bot:resolvejid(param, command.room);
 		end
 		
 		bot.stream:query_version(who, function (reply)
--- a/squishy	Sat May 25 23:21:30 2013 +0200
+++ b/squishy	Fri May 31 19:01:32 2013 +0200
@@ -5,6 +5,7 @@
 Module "riddim.plugins.ping"           "plugins/ping.lua"
 Module "riddim.plugins.tell"           "plugins/tell.lua"
 Module "riddim.plugins.uptime"         "plugins/uptime.lua"
+Module "riddim.plugins.resolvejid"     "plugins/resolvejid.lua"
 
 Main "init.lua"
 

mercurial