plugins/groupchat.lua

changeset 0
7d84f4403d67
child 9
8c3bec93087b
equal deleted inserted replaced
-1:000000000000 0:7d84f4403d67
1 local events = require "events";
2 local st = require "util.stanza";
3
4 local room_mt = {};
5 room_mt.__index = room_mt;
6
7 local xmlns_delay = "urn:xmpp:delay";
8
9 function riddim.plugins.groupchat(bot)
10 bot.rooms = {};
11
12 bot.stream:hook("stanza", function (stanza)
13 local room_jid = jid.bare(stanza.attr.from);
14 local room = bot.rooms[room_jid]
15 if room then
16 local nick = select(3, jid.split(stanza.attr.from));
17 local body = stanza:get_child("body");
18 local delay = stanza:get_child("delay", xmlns_delay);
19 local event = {
20 room_jid = room_jid;
21 room = room;
22 sender = room.occupants[nick];
23 nick = nick;
24 body = (body and body:get_text()) or nil;
25 stanza = stanza;
26 delay = (delay and delay.attr.stamp);
27 };
28 if stanza.name == "message" then
29 local replied;
30 local r = st.reply(stanza);
31 if stanza.attr.type == "groupchat" then
32 r.attr.type = stanza.attr.type;
33 r.attr.to = jid.bare(stanza.attr.to);
34 end
35 function event:reply(reply)
36 if not reply then reply = "Nothing to say to you"; end
37 if replied then return false; end
38 replied = true;
39 if r.attr.type == "groupchat" then
40 reply = event.sender.nick..": "..reply;
41 end
42 room:send(r:tag("body"):text(reply));
43 end
44 end
45 local ret;
46 if stanza.name ~= "message" or nick ~= room.nick then
47 ret = room:event(stanza.name, event);
48 end
49 return ret or (stanza.name == "message") or nil;
50 end
51 end, 500);
52
53 function bot:join_room(jid, nick)
54 nick = nick or bot.config.nick or ("bot"..math.random(10000,99999));
55 local room = setmetatable({
56 bot = bot, jid = jid, nick = nick,
57 occupants = {},
58 events = events.new() }, room_mt);
59 self.rooms[jid] = room;
60 local occupants = room.occupants;
61 room:hook("presence", function (presence)
62 local nick = presence.nick;
63 if not occupants[nick] then
64 if presence.type ~= "unavailable" then
65 occupants[nick] = {
66 nick = nick;
67 jid = presence.stanza.attr.from;
68 presence = presence.stanza;
69 };
70 if nick == room.nick then
71 room.bot:event("groupchat/joined", room);
72 else
73 room:event("occupant-joined", occupants[nick]);
74 end
75 else
76 occupants[nick].presence = presence.stanza;
77 room:event("occupant-left", occupants[nick]);
78 occupants[nick] = nil;
79 end
80 end
81 end);
82 self:send(st.presence({to = jid.."/"..nick}));
83 self:event("groupchat/joining", room);
84 return room;
85 end
86 end
87
88 function room_mt:send(stanza)
89 if stanza.name == "message" and not stanza.attr.type then
90 stanza.attr.type = "groupchat";
91 end
92 if stanza.attr.type == "groupchat" then
93 stanza.attr.to = self.jid;
94 end
95 self.bot:send(stanza);
96 end
97
98 function room_mt:send_message(text)
99 self:send(st.message():tag("body"):text(text));
100 end
101
102 function room_mt:leave(message)
103 self.bot:event("groupchat/leaving", room);
104 self:send(st.presence({type="unavailable"}));
105 self.bot:event("groupchat/left", room);
106 end
107
108 function room_mt:event(name, arg)
109 self.bot.stream:debug("Firing room event: %s", name);
110 return self.events.fire_event(name, arg);
111 end
112
113 function room_mt:hook(name, callback, priority)
114 return self.events.add_handler(name, callback, priority);
115 end

mercurial