Thu, 22 Oct 2020 15:37:43 +0100
config.docker.lua: Require RIDDIM_DEBUG=1 to enable debug mode
0 | 1 | local st = require "util.stanza"; |
136
e36f5d359aa5
plugins.groupchat: Import util.jid (thanks johan)
Kim Alvefur <zash@zash.se>
parents:
101
diff
changeset
|
2 | local jid = require "util.jid"; |
36
ccce42f781de
plugins.groupchat: Request zero history
Kim Alvefur <zash@zash.se>
parents:
31
diff
changeset
|
3 | local xmlns_muc = "http://jabber.org/protocol/muc"; |
0 | 4 | |
5 | 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
|
6 | bot.stream:add_plugin("groupchat") |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
7 | 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
|
8 | |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
9 | 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
|
10 | 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
|
11 | 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
|
12 | 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
|
13 | 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
|
14 | 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
|
15 | 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
|
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 |
3df63aaba9e3
Decouple plugins from base implementation (in particular no plugins are now loaded by default)
Chris <jugg@hotmail.com>
parents:
9
diff
changeset
|
19 | end); |
0 | 20 | |
56
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
21 | -- 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
|
22 | 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
|
23 | 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
|
24 | 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
|
25 | room.bot = bot; |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
26 | 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
|
27 | end); |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
28 | end |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
29 | |
160
eb5a04a9877a
plugins/groupchat.lua: Add support for muc passwords
Seve Ferrer <seve@delape.net>
parents:
143
diff
changeset
|
30 | function bot:join_room(room_jid, nick, password) |
0 | 31 | nick = nick or bot.config.nick or ("bot"..math.random(10000,99999)); |
160
eb5a04a9877a
plugins/groupchat.lua: Add support for muc passwords
Seve Ferrer <seve@delape.net>
parents:
143
diff
changeset
|
32 | local room = bot.stream:join_room(room_jid, nick, password) |
56
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
33 | room.bot = bot; |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
34 | room:hook("message", function(event) |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
35 | 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
|
36 | return true; |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
37 | end |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
38 | end, 1000); |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
39 | room:hook("message", function(event) |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
40 | local stanza = event.stanza; |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
41 | local replied; |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
42 | local r = st.reply(stanza); |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
43 | 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
|
44 | 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
|
45 | 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
|
46 | end |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
47 | function event:reply(reply) |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
48 | 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
|
49 | 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
|
50 | replied = true; |
67
12e2c702eddc
plugins.groupchat: Optimize reply nick prefixing and prevent a traceback
Kim Alvefur <zash@zash.se>
parents:
60
diff
changeset
|
51 | if reply:sub(1,4) ~= "/me " and event.sender and r.attr.type == "groupchat" then |
101
7d75d7f9d5ae
plugins.groupchat: Allow prefixes on replies to be overriden
Kim Alvefur <zash@zash.se>
parents:
67
diff
changeset
|
52 | reply = (event.reply_to or 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
|
53 | end |
56
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
54 | room:send(r:tag("body"):text(reply)); |
0 | 55 | end |
56
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
56 | end, 500); |
0 | 57 | return room; |
58 | end | |
59 | ||
56
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
60 | 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
|
61 | 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
|
62 | if muc_x then |
143
bad1be5e4674
riddim.plugins.groupchat: Change numeric stanza attribute to string to comply with util.stanza strict mode
Kim Alvefur <zash@zash.se>
parents:
136
diff
changeset
|
63 | muc_x:tag("history",{maxstanzas = "0"}); |
56
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
64 | end |
cb7656ee4dd8
plugins.groupchat: Update to use groupchat code in Verse
Kim Alvefur <zash@zash.se>
parents:
46
diff
changeset
|
65 | end); |
0 | 66 | end |