Software /
code /
prosody
Comparison
plugins/muc/occupant.lib.lua @ 6408:7e69d61a0ef7
Merge 0.10->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 11 Sep 2014 01:17:56 +0200 |
parent | 6271:006b0e0f0de2 |
child | 7086:6cc7c9da29ed |
comparison
equal
deleted
inserted
replaced
6407:4bbd198cf3e6 | 6408:7e69d61a0ef7 |
---|---|
1 local next = next; | |
2 local pairs = pairs; | |
3 local setmetatable = setmetatable; | |
4 local st = require "util.stanza"; | |
5 local util = module:require "muc/util"; | |
6 | |
7 local function get_filtered_presence(stanza) | |
8 return util.filter_muc_x(st.clone(stanza)); | |
9 end | |
10 | |
11 local occupant_mt = {}; | |
12 occupant_mt.__index = occupant_mt; | |
13 | |
14 local function new_occupant(bare_real_jid, nick) | |
15 return setmetatable({ | |
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 | |
19 role = nil; | |
20 jid = nil; -- Primary session | |
21 }, occupant_mt); | |
22 end | |
23 | |
24 -- Deep copy an occupant | |
25 local function copy_occupant(occupant) | |
26 local sessions = {}; | |
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; | |
31 end | |
32 end | |
33 return setmetatable({ | |
34 bare_jid = occupant.bare_jid; | |
35 nick = occupant.nick; | |
36 sessions = sessions; | |
37 role = occupant.role; | |
38 jid = occupant.jid; | |
39 }, occupant_mt); | |
40 end | |
41 | |
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 | |
46 return jid; | |
47 end | |
48 end | |
49 return nil; | |
50 end | |
51 | |
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; | |
56 | |
57 self.sessions[real_jid] = pr; | |
58 if replace_primary then | |
59 self.jid = real_jid; | |
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; | |
63 end | |
64 end | |
65 | |
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(); | |
71 end | |
72 end | |
73 | |
74 function occupant_mt:each_session() | |
75 return pairs(self.sessions) | |
76 end | |
77 | |
78 function occupant_mt:get_presence(real_jid) | |
79 return self.sessions[real_jid or self.jid] | |
80 end | |
81 | |
82 return { | |
83 new = new_occupant; | |
84 copy = copy_occupant; | |
85 mt = occupant_mt; | |
86 } |