plugins/groupchat.lua

Mon, 08 Nov 2010 23:06:48 +0100

author
Kim Alvefur <zash@zash.se>
date
Mon, 08 Nov 2010 23:06:48 +0100
changeset 36
ccce42f781de
parent 31
0cafbe17c0aa
child 42
5ee2eccabcc9
permissions
-rw-r--r--

plugins.groupchat: Request zero history

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";
36
ccce42f781de plugins.groupchat: Request zero history
Kim Alvefur <zash@zash.se>
parents: 31
diff changeset
8 local xmlns_muc = "http://jabber.org/protocol/muc";
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 function riddim.plugins.groupchat(bot)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 bot.rooms = {};
14
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
12
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
13 bot:hook("started", function ()
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
14 for k, v in pairs(bot.config.autojoin or {}) do
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
15 if type(k) == "number" then
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
16 bot:join_room(v);
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
17 elseif type(k) == "string" then
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
18 if type(v) == "string" then
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
19 bot:join_room(k, v);
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
20 end
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
21 end
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
22 end
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
23 end);
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 bot.stream:hook("stanza", function (stanza)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local room_jid = jid.bare(stanza.attr.from);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local room = bot.rooms[room_jid]
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if room then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local nick = select(3, jid.split(stanza.attr.from));
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local body = stanza:get_child("body");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local delay = stanza:get_child("delay", xmlns_delay);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local event = {
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
33 room_jid = room_jid;
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
34 room = room;
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
35 sender = room.occupants[nick];
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
36 nick = nick;
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
37 body = (body and body:get_text()) or nil;
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
38 stanza = stanza;
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
39 delay = (delay and delay.attr.stamp);
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
40 };
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 if stanza.name == "message" then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local replied;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local r = st.reply(stanza);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 if stanza.attr.type == "groupchat" then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 r.attr.type = stanza.attr.type;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 r.attr.to = jid.bare(stanza.attr.to);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 function event:reply(reply)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 if not reply then reply = "Nothing to say to you"; end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 if replied then return false; end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 replied = true;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 if r.attr.type == "groupchat" then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 reply = event.sender.nick..": "..reply;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 room:send(r:tag("body"):text(reply));
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local ret;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 if stanza.name ~= "message" or nick ~= room.nick then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 ret = room:event(stanza.name, event);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 return ret or (stanza.name == "message") or nil;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end, 500);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 function bot:join_room(jid, nick)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 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
68 local room = setmetatable({
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 bot = bot, jid = jid, nick = nick,
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 occupants = {},
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
71 events = events.new()
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
72 }, room_mt);
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 self.rooms[jid] = room;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local occupants = room.occupants;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 room:hook("presence", function (presence)
31
0cafbe17c0aa plugins.groupchat: Assume our own nick when receiving presence from the room's bare JID
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
76 local nick = presence.nick or nick;
9
8c3bec93087b groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents: 0
diff changeset
77 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
78 occupants[nick] = {
8c3bec93087b groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents: 0
diff changeset
79 nick = nick;
8c3bec93087b groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents: 0
diff changeset
80 jid = presence.stanza.attr.from;
8c3bec93087b groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents: 0
diff changeset
81 presence = presence.stanza;
16
ae69cea97598 Formatting, indentation and cleanup
Chris <jugg@hotmail.com>
parents: 14
diff changeset
82 };
9
8c3bec93087b groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents: 0
diff changeset
83 if nick == room.nick then
8c3bec93087b groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents: 0
diff changeset
84 room.bot:event("groupchat/joined", room);
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 else
9
8c3bec93087b groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents: 0
diff changeset
86 room:event("occupant-joined", occupants[nick]);
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
9
8c3bec93087b groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents: 0
diff changeset
88 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
89 occupants[nick].presence = presence.stanza;
8c3bec93087b groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents: 0
diff changeset
90 room:event("occupant-left", occupants[nick]);
8c3bec93087b groupchat.lua: make the occupant-left event work
Thilo Cestonaro <thilo@cestona.ro>
parents: 0
diff changeset
91 occupants[nick] = nil;
0
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 end);
36
ccce42f781de plugins.groupchat: Request zero history
Kim Alvefur <zash@zash.se>
parents: 31
diff changeset
94 self:send(st.presence({to = jid.."/"..nick})
ccce42f781de plugins.groupchat: Request zero history
Kim Alvefur <zash@zash.se>
parents: 31
diff changeset
95 :tag("x",{xmlns = xmlns_muc}):tag("history",{maxstanzas = 0}));
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 self:event("groupchat/joining", room);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 return room;
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 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 function room_mt:send(stanza)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 if stanza.name == "message" and not stanza.attr.type then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 stanza.attr.type = "groupchat";
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 if stanza.attr.type == "groupchat" then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 stanza.attr.to = self.jid;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 self.bot:send(stanza);
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:send_message(text)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 self:send(st.message():tag("body"):text(text));
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 function room_mt:leave(message)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 self.bot:event("groupchat/leaving", room);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 self:send(st.presence({type="unavailable"}));
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 self.bot:event("groupchat/left", room);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 function room_mt:event(name, arg)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 self.bot.stream:debug("Firing room event: %s", name);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 return self.events.fire_event(name, arg);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 function room_mt:hook(name, callback, priority)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 return self.events.add_handler(name, callback, priority);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end

mercurial