plugins.tell: Optinally store !tells in iq:private for persistence (requires json)

Tue, 09 Nov 2010 03:01:30 +0100

author
Kim Alvefur <zash@zash.se>
date
Tue, 09 Nov 2010 03:01:30 +0100
changeset 41
b00d05814d0d
parent 40
313c0662100d
child 42
5ee2eccabcc9

plugins.tell: Optinally store !tells in iq:private for persistence (requires json)

plugins/tell.lua file | annotate | diff | comparison | revisions
--- a/plugins/tell.lua	Tue Nov 09 01:53:26 2010 +0100
+++ b/plugins/tell.lua	Tue Nov 09 03:01:30 2010 +0100
@@ -1,8 +1,22 @@
 local st = require "util.stanza";
+local memory_ns = "http://code.matthewwild.co.uk/riddim/plugins/tell"
+local serializer = false;
 local tellings = {};
 
 function riddim.plugins.tell(bot)
+	if bot.config.remember_tells then
+		bot.stream:add_plugin("private");
+		serializer = require "json"; --TODO other serializer?
+	end
+
 	local sameroom = bot.config.tell_in_same_room;
+
+	local function remember()
+		if serializer then
+			bot.stream:private_set("tellings", memory_ns, serializer.encode(tellings), function () end);
+		end
+	end
+
 	bot:hook("commands/tell", function (command)
 		if not command.room then
 			return "This command is only available in groupchats.";
@@ -36,6 +50,7 @@
 			tellings[nick_id] = {};
 		end
 		tellings[nick_id][#tellings[nick_id] + 1] = {from=command.sender.nick, msg=msg};
+		remember();
 		return "Ok!";
 	end);
 
@@ -90,13 +105,28 @@
 		if number > 1 then
 			tellings[nick_id][id] = tellings[nick_id][number];
 			tellings[nick_id][number] = nil;
+			remember();
 			return "what was I supposed to tell "..nick.." again ?";
 		else
 			tellings[nick_id] = nil;
+			remember();
 			return "who is "..nick.." anyway ?";
 		end
 	end);
 
+	if serializer then
+		bot:hook("started", function() -- restore memory
+			bot.stream:private_get("tellings", memory_ns, function (what)
+				if what then
+					local data = tostring(what:get_text());
+					if data and #data > 0 then
+						tellings = serializer.decode(data);
+					end
+				end
+			end);
+		end);
+	end
+
 	bot:hook("groupchat/joined", function (room)
 		room:hook("occupant-joined", function (occupant)
 			local nick_id = sameroom and room.jid .. "/" .. occupant.nick or occupant.nick;
@@ -105,6 +135,7 @@
 					room:send_message(occupant.nick .. ": Welcome back! " .. msg.from .. " told me, to tell you, \"" .. msg.msg .. "\".");
 				end
 				tellings[nick_id] = nil;
+				remember();
 			end
 		end);
 	end);

mercurial