plugins/tell.lua

changeset 41
b00d05814d0d
parent 40
313c0662100d
child 103
c0235605d6d5
equal deleted inserted replaced
40:313c0662100d 41:b00d05814d0d
1 local st = require "util.stanza"; 1 local st = require "util.stanza";
2 local memory_ns = "http://code.matthewwild.co.uk/riddim/plugins/tell"
3 local serializer = false;
2 local tellings = {}; 4 local tellings = {};
3 5
4 function riddim.plugins.tell(bot) 6 function riddim.plugins.tell(bot)
7 if bot.config.remember_tells then
8 bot.stream:add_plugin("private");
9 serializer = require "json"; --TODO other serializer?
10 end
11
5 local sameroom = bot.config.tell_in_same_room; 12 local sameroom = bot.config.tell_in_same_room;
13
14 local function remember()
15 if serializer then
16 bot.stream:private_set("tellings", memory_ns, serializer.encode(tellings), function () end);
17 end
18 end
19
6 bot:hook("commands/tell", function (command) 20 bot:hook("commands/tell", function (command)
7 if not command.room then 21 if not command.room then
8 return "This command is only available in groupchats."; 22 return "This command is only available in groupchats.";
9 end 23 end
10 if not command.param then 24 if not command.param then
34 48
35 if tellings[nick_id] == nil then 49 if tellings[nick_id] == nil then
36 tellings[nick_id] = {}; 50 tellings[nick_id] = {};
37 end 51 end
38 tellings[nick_id][#tellings[nick_id] + 1] = {from=command.sender.nick, msg=msg}; 52 tellings[nick_id][#tellings[nick_id] + 1] = {from=command.sender.nick, msg=msg};
53 remember();
39 return "Ok!"; 54 return "Ok!";
40 end); 55 end);
41 56
42 bot:hook("commands/untell", function (command) 57 bot:hook("commands/untell", function (command)
43 if not command.room then 58 if not command.room then
88 103
89 -- remove the message 104 -- remove the message
90 if number > 1 then 105 if number > 1 then
91 tellings[nick_id][id] = tellings[nick_id][number]; 106 tellings[nick_id][id] = tellings[nick_id][number];
92 tellings[nick_id][number] = nil; 107 tellings[nick_id][number] = nil;
108 remember();
93 return "what was I supposed to tell "..nick.." again ?"; 109 return "what was I supposed to tell "..nick.." again ?";
94 else 110 else
95 tellings[nick_id] = nil; 111 tellings[nick_id] = nil;
112 remember();
96 return "who is "..nick.." anyway ?"; 113 return "who is "..nick.." anyway ?";
97 end 114 end
98 end); 115 end);
116
117 if serializer then
118 bot:hook("started", function() -- restore memory
119 bot.stream:private_get("tellings", memory_ns, function (what)
120 if what then
121 local data = tostring(what:get_text());
122 if data and #data > 0 then
123 tellings = serializer.decode(data);
124 end
125 end
126 end);
127 end);
128 end
99 129
100 bot:hook("groupchat/joined", function (room) 130 bot:hook("groupchat/joined", function (room)
101 room:hook("occupant-joined", function (occupant) 131 room:hook("occupant-joined", function (occupant)
102 local nick_id = sameroom and room.jid .. "/" .. occupant.nick or occupant.nick; 132 local nick_id = sameroom and room.jid .. "/" .. occupant.nick or occupant.nick;
103 if(tellings[nick_id] ~= nil) then 133 if(tellings[nick_id] ~= nil) then
104 for _,msg in ipairs(tellings[nick_id]) do 134 for _,msg in ipairs(tellings[nick_id]) do
105 room:send_message(occupant.nick .. ": Welcome back! " .. msg.from .. " told me, to tell you, \"" .. msg.msg .. "\"."); 135 room:send_message(occupant.nick .. ": Welcome back! " .. msg.from .. " told me, to tell you, \"" .. msg.msg .. "\".");
106 end 136 end
107 tellings[nick_id] = nil; 137 tellings[nick_id] = nil;
138 remember();
108 end 139 end
109 end); 140 end);
110 end); 141 end);
111 end 142 end

mercurial