Mercurial > prosody-hg
comparison plugins/muc/occupant.lib.lua @ 6179:e488a90195bc
plugins/muc: Massive refactor
We now have occupant objects; you grab them, modify them, save them.
New presence handling code.
Modify all presence sending to go via new functions.
| author | daurnimator <quae@daurnimator.com> |
|---|---|
| date | Thu, 27 Mar 2014 19:16:13 -0400 |
| parents | |
| children | e9d62fff82a8 |
comparison
equal
deleted
inserted
replaced
| 6143:82b3a2155a55 | 6179:e488a90195bc |
|---|---|
| 1 local next = next; | |
| 2 local pairs = pairs; | |
| 3 local setmetatable = setmetatable; | |
| 4 local st = require "util.stanza"; | |
| 5 | |
| 6 local get_filtered_presence do | |
| 7 local presence_filters = { | |
| 8 ["http://jabber.org/protocol/muc"] = true; | |
| 9 ["http://jabber.org/protocol/muc#user"] = true; | |
| 10 } | |
| 11 local function presence_filter(tag) | |
| 12 if presence_filters[tag.attr.xmlns] then | |
| 13 return nil; | |
| 14 end | |
| 15 return tag; | |
| 16 end | |
| 17 function get_filtered_presence(stanza) | |
| 18 return st.clone(stanza):maptags(presence_filter); | |
| 19 end | |
| 20 end | |
| 21 | |
| 22 local occupant_mt = {}; | |
| 23 occupant_mt.__index = occupant_mt; | |
| 24 | |
| 25 local function new_occupant(bare_real_jid, nick) | |
| 26 return setmetatable({ | |
| 27 bare_jid = bare_real_jid; | |
| 28 nick = nick; -- in-room jid | |
| 29 sessions = {}; -- hash from real_jid to presence stanzas. stanzas should not be modified | |
| 30 role = nil; | |
| 31 jid = nil; -- Primary session | |
| 32 }, occupant_mt); | |
| 33 end | |
| 34 | |
| 35 -- Deep copy an occupant | |
| 36 local function copy_occupant(occupant) | |
| 37 local sessions = {}; | |
| 38 for full_jid, presence_stanza in pairs(occupant.sessions) do | |
| 39 if presence_stanza.attr.type ~= "unavailable" then | |
| 40 sessions[full_jid] = presence_stanza; | |
| 41 end | |
| 42 end | |
| 43 return setmetatable({ | |
| 44 bare_jid = occupant.bare_jid; | |
| 45 nick = occupant.nick; | |
| 46 sessions = sessions; | |
| 47 role = occupant.role; | |
| 48 jid = occupant.jid; | |
| 49 }, occupant_mt); | |
| 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 or self.jid == nil then | |
| 59 self.jid = real_jid; | |
| 60 end | |
| 61 end | |
| 62 | |
| 63 function occupant_mt:remove_session(real_jid) | |
| 64 -- Delete original session | |
| 65 local presence_stanza = self.sessions[real_jid]; | |
| 66 self.sessions[real_jid] = nil; | |
| 67 if self.jid == real_jid then | |
| 68 -- find another session to be the primary (might be nil) | |
| 69 self.jid = next(self.sessions); | |
| 70 end | |
| 71 end | |
| 72 | |
| 73 function occupant_mt:each_session() | |
| 74 return pairs(self.sessions) | |
| 75 end | |
| 76 | |
| 77 function occupant_mt:get_presence(real_jid) | |
| 78 return self.sessions[real_jid or self.jid] | |
| 79 end | |
| 80 | |
| 81 return { | |
| 82 new = new_occupant; | |
| 83 copy = copy_occupant; | |
| 84 mt = occupant_mt; | |
| 85 } |
