plugins/groupchat.lua

Thu, 18 Mar 2010 14:16:05 +0100

author
Thilo Cestonaro <thilo@cestona.ro>
date
Thu, 18 Mar 2010 14:16:05 +0100
changeset 9
8c3bec93087b
parent 0
7d84f4403d67
child 14
3df63aaba9e3
permissions
-rw-r--r--

groupchat.lua: make the occupant-left event work
tell.lua: double check the room and clear told tellings.

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

mercurial