annotate mod_minimix/mod_minimix.lua @ 3099:c88f91626e07

mod_pastebin: Off-by-one, the hardest problem known to man The threshold set to 4 allowed 4 lines and pastebinned 5. 040eaa3844f4 unintentionally changed this so that 4 lines were pastebinned. This commit restores the previous behavior.
author Kim Alvefur <zash@zash.se>
date Sun, 03 Jun 2018 14:09:30 +0200
parents a57ed544fece
children 24e49391d4e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2941
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- mod_minimix
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 --
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 -- Rewrite MUC stanzas suich that the account / bare JID joins rooms instead of clients / full JIDs
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 --
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local jid_split, jid_join, jid_node, jid_bare = import("util.jid", "split", "join", "node", "bare");
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local st = require "util.stanza";
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local users = prosody.hosts[module.host].sessions;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local joined_rooms = module:open_store("joined_rooms", "map"); -- TODO cache?
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local room_state = module:open_store("joined_rooms_state", "map");
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local all_room_state = module:open_store("joined_rooms_state");
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 -- FIXME You can join but you can never leave.
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 module:hook("pre-presence/full", function (event)
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local origin, stanza = event.origin, event.stanza;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local room_node, room_host, nickname = jid_split(stanza.attr.to);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local room_jid = jid_join(room_node, room_host);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local username = origin.username;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 if stanza.attr.type == nil and stanza:get_child("x", "http://jabber.org/protocol/muc") then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 module:log("debug", "Joining %s as %s", room_jid, nickname);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 -- TODO Should this be kept track of before the *initial* join has been confirmed or?
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 if origin.joined_rooms then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 origin.joined_rooms[room_jid] = nickname;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 else
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 origin.joined_rooms = { [room_jid] = nickname };
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 if joined_rooms:get(username, room_jid) then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 module:log("debug", "Already joined to %s as %s", room_jid, nickname);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 local state = assert(all_room_state:get(username));
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 for jid, stanza in pairs(state) do
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 if jid ~= room_jid and jid ~= stanza.attr.to then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 origin.send(st.clone(st.deserialize(stanza)));
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 origin.send(st.deserialize(state[stanza.attr.to]));
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 origin.send(st.message({type="groupchat",to=origin.full_jid,from=room_jid}):tag("subject"):text(state[room_jid]));
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 -- Send on-join stanzas from local state, somehow
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 -- Maybe tell them their nickname was changed if it doesn't match the account one
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 return true;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 joined_rooms:set(username, room_jid, nickname);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 local account_join = st.clone(stanza);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 account_join.attr.from = jid_join(origin.username, origin.host);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 module:send(account_join);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 return true;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 elseif stanza.attr.type == "unavailable" and joined_rooms:get(username, room_jid) then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 origin.send(st.reply(stanza));
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 return true;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 end);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 module:hook("pre-message/bare", function (event)
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local origin, stanza = event.origin, event.stanza;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 local username = origin.username;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 local room_jid = jid_bare(stanza.attr.to);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 module:log("info", "%s", stanza)
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 if joined_rooms:get(username, room_jid) then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 local from_account = st.clone(stanza);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 from_account.attr.from = jid_join(origin.username, origin.host);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 module:log("debug", "Sending:\n%s\nInstead of:\n%s", from_account, stanza);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 module:send(from_account, origin);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 return true;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 end);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 local function handle_to_bare_jid(event)
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 local origin, stanza = event.origin, event.stanza;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 local username = jid_node(stanza.attr.to);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 local room_jid = jid_bare(stanza.attr.from);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 if joined_rooms:get(username, room_jid) then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 module:log("debug", "handle_to_bare_jid %q, %s", room_jid, stanza);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 -- Broadcast to clients
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 if stanza.name == "message" and stanza.attr.type == "groupchat"
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 and not stanza:get_child("body") and stanza:get_child("subject") then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 room_state:set(username, room_jid, stanza:get_child_text("subject"));
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 elseif stanza.name == "presence" then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 if stanza.attr.type == nil then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 room_state:set(username, stanza.attr.from, st.preserialize(stanza));
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 elseif stanza.attr.type == "unavailable" then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 room_state:set(username, stanza.attr.from, nil);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 if users[username] then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 module:log("debug", "%s has sessions", username);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 for _, session in pairs(users[username].sessions) do
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 module:log("debug", "Session: %s", jid_join(session.username, session.host, session.resource));
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 if session.joined_rooms and session.joined_rooms[room_jid] then
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 module:log("debug", "Is joined");
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 local s = st.clone(stanza);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 s.attr.to = session.full_jid;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 session.send(s);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 else
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 module:log("debug", "session.joined_rooms = %s", session.joined_rooms);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 return true;
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 end
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 module:hook("presence/bare", handle_to_bare_jid);
a57ed544fece mod_minimix: Experiment in account-based MUC joins
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 module:hook("message/bare", handle_to_bare_jid);