3 local setmetatable = setmetatable;
4 local st = require "util.stanza";
5 local util = module:require "muc/util";
7 local function get_filtered_presence(stanza)
8 return util.filter_muc_x(st.clone(stanza));
11 local occupant_mt = {};
12 occupant_mt.__index = occupant_mt;
14 local function new_occupant(bare_real_jid, nick)
16 bare_jid = bare_real_jid;
17 nick = nick; -- in-room jid
18 sessions = {}; -- hash from real_jid to presence stanzas. stanzas should not be modified
20 jid = nil; -- Primary session
24 -- Deep copy an occupant
25 local function copy_occupant(occupant)
27 for full_jid, presence_stanza in pairs(occupant.sessions) do
28 -- Don't keep unavailable presences, as they'll accumulate; unless they're the primary session
29 if presence_stanza.attr.type ~= "unavailable" or full_jid == occupant.jid then
30 sessions[full_jid] = presence_stanza;
34 bare_jid = occupant.bare_jid;
42 -- finds another session to be the primary (there might not be one)
43 function occupant_mt:choose_new_primary()
44 for jid, pr in self:each_session() do
45 if pr.attr.type == nil then
52 function occupant_mt:set_session(real_jid, presence_stanza, replace_primary)
53 local pr = get_filtered_presence(presence_stanza);
54 pr.attr.from = self.nick;
55 pr.attr.to = real_jid;
57 self.sessions[real_jid] = pr;
58 if replace_primary then
60 elseif self.jid == nil or (pr.attr.type == "unavailable" and self.jid == real_jid) then
61 -- Only leave an unavailable presence as primary when there are no other options
62 self.jid = self:choose_new_primary() or real_jid;
66 function occupant_mt:remove_session(real_jid)
67 -- Delete original session
68 self.sessions[real_jid] = nil;
69 if self.jid == real_jid then
70 self.jid = self:choose_new_primary();
74 function occupant_mt:each_session()
75 return pairs(self.sessions)
78 function occupant_mt:get_presence(real_jid)
79 return self.sessions[real_jid or self.jid]