plugins/tell.lua

Thu, 24 Jun 2010 17:51:36 +0100

author
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
date
Thu, 24 Jun 2010 17:51:36 +0100
changeset 30
7bfe0d739b1c
parent 29
11154ba6cbf6
child 40
313c0662100d
permissions
-rw-r--r--

plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells

8
9663866ca475 add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
1 local st = require "util.stanza";
9663866ca475 add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
2 local tellings = {};
9663866ca475 add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
3
9663866ca475 add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
4 function riddim.plugins.tell(bot)
9663866ca475 add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
5 bot:hook("commands/tell", function (command)
29
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
6 if not command.room then
20
4f4d98345952 Merge with Chris
Matthew Wild <mwild1@gmail.com>
parents: 16 13
diff changeset
7 return "This command is only available in groupchats.";
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 10
diff changeset
8 end
29
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
9 if not command.param then
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
10 return "If you want me to tell someone something then do so";
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
11 end
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
12
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
13 local s, e = command.param:find(" ");
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
14
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
15 if not s then
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
16 return "if you have nothing to say to "..command.param..", then leave me alone, please";
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
17 end
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
18
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
19 local nick = command.param:sub(0, s - 1);
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
20 local msg = command.param:sub(s + 1);
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
21
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
22 if nick == command.sender.nick then
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
23 return "Tell yourself.";
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
24 end
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
25
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
26 for tmp,_ in pairs(command.room.occupants) do
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
27 if tmp == nick then
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
28 return "" .. nick .. " is currently online!";
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
29 end
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
30 end
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
31
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
32 if tellings[nick] == nil then
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
33 tellings[nick] = {};
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
34 end
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
35 tellings[nick][#tellings[nick] + 1] = {from=command.sender.nick, msg=msg};
11154ba6cbf6 plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 21
diff changeset
36 return "Ok!";
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 10
diff changeset
37 end);
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 10
diff changeset
38
30
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
39 bot:hook("commands/untell", function (command)
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
40 if not command.room then
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
41 return "This command is only available in groupchats.";
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
42 end
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
43 if not command.param then
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
44 return "If you changed your mind tell me for who";
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
45 end
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
46
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
47 local s, e = command.param:find(" ");
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
48 local nick;
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
49 local id;
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
50
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
51 -- parameter parsing
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
52 if not s then
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
53 nick = command.param;
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
54 id = nil;
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
55 else
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
56 nick = command.param:sub(0, s - 1);
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
57 id = command.param:sub(s + 1);
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
58 end
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
59
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
60 -- no message for that user
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
61 if not tellings[nick] then
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
62 return "I have no messages for "..nick;
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
63 end
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
64
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
65 -- no message id and message for that user
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
66 if id == nil then
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
67 local response = "I am supposed to relay the following message to "..nick.." :";
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
68 for index,msg in ipairs(tellings[nick]) do
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
69 response = response .. "\n#"..index.." : "..msg.msg;
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
70 end
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
71 return response;
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
72 end
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
73
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
74 -- check the message id is valid
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
75 local number = #tellings[nick];
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
76 id = tonumber(id)
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
77 if id == nil or id < 1 or id > number then
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
78 return "I need a valid message #id .. sigh !!\n"..nick.." has "..number.." message(s)";
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
79 end
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
80
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
81 if tellings[nick][id].from ~= command.sender.nick then
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
82 return "you never said that, "..tellings[nick][id].from.." did !";
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
83 end
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
84
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
85 -- remove the message
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
86 if number > 1 then
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
87 tellings[nick][id] = tellings[nick][number];
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
88 tellings[nick][number] = nil;
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
89 return "what was I supposed to tell "..nick.." again ?";
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
90 else
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
91 tellings[nick] = nil;
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
92 return "who is "..nick.." anyway ?";
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
93 end
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
94 end);
7bfe0d739b1c plugins.tell: Add !untell <nick> and !untell <nick> <id> to remove tells
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents: 29
diff changeset
95
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 10
diff changeset
96 bot:hook("groupchat/joined", function (room)
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 10
diff changeset
97 room:hook("occupant-joined", function (occupant)
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 10
diff changeset
98 if(tellings[occupant.nick] ~= nil) then
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 10
diff changeset
99 for _,msg in ipairs(tellings[occupant.nick]) do
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 10
diff changeset
100 room:send_message(occupant.nick .. ": Welcome back! " .. msg.from .. " told me, to tell you, \"" .. msg.msg .. "\".");
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 10
diff changeset
101 end
20
4f4d98345952 Merge with Chris
Matthew Wild <mwild1@gmail.com>
parents: 16 13
diff changeset
102 tellings[occupant.nick] = nil;
8
9663866ca475 add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
103 end
9663866ca475 add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
104 end);
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 10
diff changeset
105 end);
8
9663866ca475 add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
106 end

mercurial