1 local st = require "util.stanza"; |
1 local st = require "util.stanza"; |
2 local tellings = {}; |
2 local tellings = {}; |
3 |
3 |
4 function riddim.plugins.tell(bot) |
4 function riddim.plugins.tell(bot) |
|
5 local sameroom = bot.config.tell_in_same_room; |
5 bot:hook("commands/tell", function (command) |
6 bot:hook("commands/tell", function (command) |
6 if not command.room then |
7 if not command.room then |
7 return "This command is only available in groupchats."; |
8 return "This command is only available in groupchats."; |
8 end |
9 end |
9 if not command.param then |
10 if not command.param then |
27 if tmp == nick then |
28 if tmp == nick then |
28 return "" .. nick .. " is currently online!"; |
29 return "" .. nick .. " is currently online!"; |
29 end |
30 end |
30 end |
31 end |
31 |
32 |
32 if tellings[nick] == nil then |
33 local nick_id = sameroom and command.room.jid .. "/" .. nick or nick; |
33 tellings[nick] = {}; |
34 |
|
35 if tellings[nick_id] == nil then |
|
36 tellings[nick_id] = {}; |
34 end |
37 end |
35 tellings[nick][#tellings[nick] + 1] = {from=command.sender.nick, msg=msg}; |
38 tellings[nick_id][#tellings[nick_id] + 1] = {from=command.sender.nick, msg=msg}; |
36 return "Ok!"; |
39 return "Ok!"; |
37 end); |
40 end); |
38 |
41 |
39 bot:hook("commands/untell", function (command) |
42 bot:hook("commands/untell", function (command) |
40 if not command.room then |
43 if not command.room then |
56 nick = command.param:sub(0, s - 1); |
59 nick = command.param:sub(0, s - 1); |
57 id = command.param:sub(s + 1); |
60 id = command.param:sub(s + 1); |
58 end |
61 end |
59 |
62 |
60 -- no message for that user |
63 -- no message for that user |
61 if not tellings[nick] then |
64 local nick_id = sameroom and command.room.jid .. "/" .. nick or nick; |
|
65 if not tellings[nick_id] then |
62 return "I have no messages for "..nick; |
66 return "I have no messages for "..nick; |
63 end |
67 end |
64 |
68 |
65 -- no message id and message for that user |
69 -- no message id and message for that user |
66 if id == nil then |
70 if id == nil then |
67 local response = "I am supposed to relay the following message to "..nick.." :"; |
71 local response = "I am supposed to relay the following message to "..nick.." :"; |
68 for index,msg in ipairs(tellings[nick]) do |
72 for index,msg in ipairs(tellings[nick_id]) do |
69 response = response .. "\n#"..index.." : "..msg.msg; |
73 response = response .. "\n#"..index.." : "..msg.msg; |
70 end |
74 end |
71 return response; |
75 return response; |
72 end |
76 end |
73 |
77 |
74 -- check the message id is valid |
78 -- check the message id is valid |
75 local number = #tellings[nick]; |
79 local number = #tellings[nick_id]; |
76 id = tonumber(id) |
80 id = tonumber(id) |
77 if id == nil or id < 1 or id > number then |
81 if id == nil or id < 1 or id > number then |
78 return "I need a valid message #id .. sigh !!\n"..nick.." has "..number.." message(s)"; |
82 return "I need a valid message #id .. sigh !!\n"..nick.." has "..number.." message(s)"; |
79 end |
83 end |
80 |
84 |
81 if tellings[nick][id].from ~= command.sender.nick then |
85 if tellings[nick_id][id].from ~= command.sender.nick then |
82 return "you never said that, "..tellings[nick][id].from.." did !"; |
86 return "you never said that, "..tellings[nick_id][id].from.." did !"; |
83 end |
87 end |
84 |
88 |
85 -- remove the message |
89 -- remove the message |
86 if number > 1 then |
90 if number > 1 then |
87 tellings[nick][id] = tellings[nick][number]; |
91 tellings[nick_id][id] = tellings[nick_id][number]; |
88 tellings[nick][number] = nil; |
92 tellings[nick_id][number] = nil; |
89 return "what was I supposed to tell "..nick.." again ?"; |
93 return "what was I supposed to tell "..nick.." again ?"; |
90 else |
94 else |
91 tellings[nick] = nil; |
95 tellings[nick_id] = nil; |
92 return "who is "..nick.." anyway ?"; |
96 return "who is "..nick.." anyway ?"; |
93 end |
97 end |
94 end); |
98 end); |
95 |
99 |
96 bot:hook("groupchat/joined", function (room) |
100 bot:hook("groupchat/joined", function (room) |
97 room:hook("occupant-joined", function (occupant) |
101 room:hook("occupant-joined", function (occupant) |
98 if(tellings[occupant.nick] ~= nil) then |
102 local nick_id = sameroom and room.jid .. "/" .. occupant.nick or occupant.nick; |
99 for _,msg in ipairs(tellings[occupant.nick]) do |
103 if(tellings[nick_id] ~= nil) then |
|
104 for _,msg in ipairs(tellings[nick_id]) do |
100 room:send_message(occupant.nick .. ": Welcome back! " .. msg.from .. " told me, to tell you, \"" .. msg.msg .. "\"."); |
105 room:send_message(occupant.nick .. ": Welcome back! " .. msg.from .. " told me, to tell you, \"" .. msg.msg .. "\"."); |
101 end |
106 end |
102 tellings[occupant.nick] = nil; |
107 tellings[nick_id] = nil; |
103 end |
108 end |
104 end); |
109 end); |
105 end); |
110 end); |
106 end |
111 end |