plugins/groupchat.lua

Mon, 21 Feb 2011 01:22:08 +0100

author
Kim Alvefur <zash@zash.se>
date
Mon, 21 Feb 2011 01:22:08 +0100
changeset 60
87ecc6324155
parent 56
cb7656ee4dd8
child 67
12e2c702eddc
permissions
-rw-r--r--

plugins.groupchat: Don't prepend replies with the senders nickname if it's an action

0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local st = require "util.stanza";
36
ccce42f781de plugins.groupchat: Request zero history
Kim Alvefur <zash@zash.se>
parents: 31
diff changeset
2 local xmlns_muc = "http://jabber.org/protocol/muc";
0
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 function riddim.plugins.groupchat(bot)
56
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
5 bot.stream:add_plugin("groupchat")
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
6 bot.rooms = bot.stream.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
7
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
8 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
9 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
10 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
11 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
12 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
13 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
14 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
15 end
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
16 end
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
17 end
3df63aaba9e3 Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents: 9
diff changeset
18 end);
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
56
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
20 -- Re-broadcast groupchat event on the bot
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
21 local reflect_events = { "groupchat/joining"; "groupchat/joined"; "groupchat/leaving"; "groupchat/left" };
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
22 for i = 1,#reflect_events do
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
23 bot.stream:hook(reflect_events[i], function(room, ...)
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
24 room.bot = bot;
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
25 bot:event(reflect_events[i], room, ...)
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
26 end);
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
27 end
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
28
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
29 function bot:join_room(room_jid, nick)
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 nick = nick or bot.config.nick or ("bot"..math.random(10000,99999));
56
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
31 local room = bot.stream:join_room(room_jid, nick)
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
32 room.bot = bot;
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
33 room:hook("message", function(event)
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
34 if event.nick == room.nick then
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
35 return true;
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
36 end
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
37 end, 1000);
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
38 room:hook("message", function(event)
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
39 local stanza = event.stanza;
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
40 local replied;
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
41 local r = st.reply(stanza);
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
42 if stanza.attr.type == "groupchat" then
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
43 r.attr.type = stanza.attr.type;
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
44 r.attr.to = jid.bare(stanza.attr.to);
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
45 end
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
46 function event:reply(reply)
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
47 if not reply then reply = "Nothing to say to you"; end
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
48 if replied then return false; end
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
49 replied = true;
60
87ecc6324155 plugins.groupchat: Don't prepend replies with the senders nickname if it's an action
Kim Alvefur <zash@zash.se>
parents: 56
diff changeset
50 if r.attr.type == "groupchat" and reply:sub(1,4) ~= "/me " then
56
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
51 reply = event.sender.nick..": "..reply;
46
1f0fa4af29b8 plugins.groupchat: Keep track of real jid, affiliation and role.
Kim Alvefur <zash@zash.se>
parents: 45
diff changeset
52 end
56
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
53 room:send(r:tag("body"):text(reply));
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
56
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
55 end, 500);
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 return room;
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
56
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
59 bot.stream:hook("pre-groupchat/joining", function(presence)
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
60 local muc_x = presence:get_child("x", xmlns_muc);
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
61 if muc_x then
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
62 muc_x:tag("history",{maxstanzas = 0});
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
63 end
cb7656ee4dd8 plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
64 end);
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end

mercurial