plugins.tell: More validation on inputs, to prevent crashes

Thu, 24 Jun 2010 17:49:08 +0100

author
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
date
Thu, 24 Jun 2010 17:49:08 +0100
changeset 29
11154ba6cbf6
parent 28
d0d1d88ec0ef
child 30
7bfe0d739b1c

plugins.tell: More validation on inputs, to prevent crashes

plugins/tell.lua file | annotate | diff | comparison | revisions
--- a/plugins/tell.lua	Tue Jun 22 14:52:53 2010 +0100
+++ b/plugins/tell.lua	Thu Jun 24 17:49:08 2010 +0100
@@ -3,35 +3,37 @@
 
 function riddim.plugins.tell(bot)
 	bot:hook("commands/tell", function (command)
-		if command.room then
-			local s, e = command.param:find(" ");
-			local nick = command.param:sub(0, s - 1);
-			local msg = command.param:sub(s + 1);
-			local found = false;
-
-			for tmp,_ in pairs(command.room.occupants) do
-				if tmp == nick then
-					found = true;
-					break;
-				end
-			end
-
-			if not found then
-				if(tellings[nick] == nil) then
-					tellings[nick] = {};
-				end
-				tellings[nick][#tellings[nick] + 1] = {from=command.sender.nick, msg=msg};
-				return "Ok!";
-			else
-				if nick == command.sender.nick then
-					return "Tell yourself.";
-				else
-					return "" .. nick .. " is currently online!";
-				end
-			end
-		else
+		if not command.room then
 			return "This command is only available in groupchats.";
 		end
+		if not command.param then
+			return "If you want me to tell someone something then do so";
+		end
+
+		local s, e = command.param:find(" ");
+
+		if not s then
+			return "if you have nothing to say to "..command.param..", then leave me alone, please";
+		end
+
+		local nick = command.param:sub(0, s - 1);
+		local msg = command.param:sub(s + 1);
+
+		if nick == command.sender.nick then
+			return "Tell yourself.";
+		end
+
+		for tmp,_ in pairs(command.room.occupants) do
+			if tmp == nick then
+				return "" .. nick .. " is currently online!";
+			end
+		end
+
+		if tellings[nick] == nil then
+			tellings[nick] = {};
+		end
+		tellings[nick][#tellings[nick] + 1] = {from=command.sender.nick, msg=msg};
+		return "Ok!";
 	end);
 
 	bot:hook("groupchat/joined", function (room)

mercurial