Tue, 09 Nov 2010 01:53:26 +0100
plugins.tell: Optinally deliver !tells only in the same room.
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) |
40
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
5 | local sameroom = bot.config.tell_in_same_room; |
8
9663866ca475
add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
6 | 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
|
7 | if not command.room then |
20 | 8 | return "This command is only available in groupchats."; |
16
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
10
diff
changeset
|
9 | end |
29
11154ba6cbf6
plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents:
21
diff
changeset
|
10 | 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
|
11 | 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
|
12 | end |
11154ba6cbf6
plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents:
21
diff
changeset
|
13 | |
11154ba6cbf6
plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents:
21
diff
changeset
|
14 | 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
|
15 | |
11154ba6cbf6
plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents:
21
diff
changeset
|
16 | 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
|
17 | 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
|
18 | end |
11154ba6cbf6
plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents:
21
diff
changeset
|
19 | |
11154ba6cbf6
plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents:
21
diff
changeset
|
20 | 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
|
21 | 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
|
22 | |
11154ba6cbf6
plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents:
21
diff
changeset
|
23 | 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
|
24 | 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
|
25 | end |
11154ba6cbf6
plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents:
21
diff
changeset
|
26 | |
11154ba6cbf6
plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents:
21
diff
changeset
|
27 | 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
|
28 | 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
|
29 | 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
|
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 | end |
11154ba6cbf6
plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents:
21
diff
changeset
|
32 | |
40
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
33 | local nick_id = sameroom and command.room.jid .. "/" .. nick or nick; |
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
34 | |
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
35 | if tellings[nick_id] == nil then |
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
36 | tellings[nick_id] = {}; |
29
11154ba6cbf6
plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents:
21
diff
changeset
|
37 | end |
40
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
38 | tellings[nick_id][#tellings[nick_id] + 1] = {from=command.sender.nick, msg=msg}; |
29
11154ba6cbf6
plugins.tell: More validation on inputs, to prevent crashes
Thomas Mangin <thomas.mangin@exa-networks.co.uk>
parents:
21
diff
changeset
|
39 | return "Ok!"; |
16
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
10
diff
changeset
|
40 | end); |
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
10
diff
changeset
|
41 | |
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
|
42 | 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
|
43 | 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
|
44 | 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
|
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 | 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
|
47 | 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
|
48 | 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
|
49 | |
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 | 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
|
51 | 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
|
52 | 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
|
53 | |
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 | -- 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
|
55 | 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
|
56 | 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
|
57 | 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
|
58 | 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
|
59 | 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
|
60 | 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
|
61 | 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
|
62 | |
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 | -- no message for that user |
40
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
64 | local nick_id = sameroom and command.room.jid .. "/" .. nick or nick; |
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
65 | if not tellings[nick_id] then |
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
|
66 | 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
|
67 | 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
|
68 | |
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 | -- 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
|
70 | 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
|
71 | local response = "I am supposed to relay the following message to "..nick.." :"; |
40
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
72 | for index,msg in ipairs(tellings[nick_id]) do |
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
|
73 | 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
|
74 | 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
|
75 | 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
|
76 | 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
|
77 | |
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 | -- check the message id is valid |
40
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
79 | local number = #tellings[nick_id]; |
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
|
80 | 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
|
81 | 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
|
82 | 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
|
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 | |
40
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
85 | if tellings[nick_id][id].from ~= command.sender.nick then |
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
86 | return "you never said that, "..tellings[nick_id][id].from.." did !"; |
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
|
87 | 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
|
88 | |
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 | -- 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
|
90 | if number > 1 then |
40
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
91 | tellings[nick_id][id] = tellings[nick_id][number]; |
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
92 | tellings[nick_id][number] = nil; |
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
|
93 | 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
|
94 | else |
40
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
95 | tellings[nick_id] = nil; |
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
|
96 | 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
|
97 | 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
|
98 | 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
|
99 | |
16
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
10
diff
changeset
|
100 | bot:hook("groupchat/joined", function (room) |
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
10
diff
changeset
|
101 | room:hook("occupant-joined", function (occupant) |
40
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
102 | local nick_id = sameroom and room.jid .. "/" .. occupant.nick or occupant.nick; |
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
103 | if(tellings[nick_id] ~= nil) then |
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
104 | for _,msg in ipairs(tellings[nick_id]) do |
16
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
10
diff
changeset
|
105 | 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
|
106 | end |
40
313c0662100d
plugins.tell: Optinally deliver !tells only in the same room.
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
107 | tellings[nick_id] = nil; |
8
9663866ca475
add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
108 | end |
9663866ca475
add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
109 | end); |
16
ae69cea97598
Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents:
10
diff
changeset
|
110 | end); |
8
9663866ca475
add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
111 | end |