local st = require("util.stanza"); local helpers = module:require("mix/helpers"); Participant = {}; Participant.__index = Participant; function Participant:new(jid, nick) return setmetatable({ jid = jid, nick = nick, }, Participant); end function Participant:from(config) return setmetatable(config, Participant); end Channel = {} Channel.__index = Channel; function Channel:new(jid, name, description, participants, subscriptions, spid, contacts, adhoc) return setmetatable({ jid = jid, name = name, description = description, participants = participants, subscriptions = subscriptions, spid = spid, contacts = contacts, adhoc = adhoc, }, Channel); end function Channel:from(config) -- Turn a channel into a Channel object local o = setmetatable(config, Channel); for i, _ in pairs(o.participants) do o.participants[i] = Participant:from(o.participants[i]); end return o; end function Channel:get_spid(jid) -- Returns the Stable Participant ID for the *BARE* jid return self.spid[jid]; end function Channel:set_spid(jid, spid) -- Sets the Stable Participant ID for the *BARE* jid self.spid[jid] = spid; end function Channel:find_participant(jid) -- Returns the index of a participant in a channel. Returns -1 -- if the participant is not found return helpers.find(self.participants, function(p) return p.jid == jid end); end function Channel:is_participant(jid) -- Returns true if jid is a participant of the channel. False otherwise. local i, _ = self:find_participant(jid); return i ~= -1; end function Channel:is_subscribed(jid, node) -- Returns true of JID is subscribed to node on this channel. Returns false -- otherwise. return helpers.find_str(self.subscriptions[jid], node) ~= -1; end function Channel:debug_print() module:log("debug", "Channel %s (%s)", self.jid, self.name); module:log("debug", "'%s'", self.description); for _, p in pairs(self.participants) do module:log("debug", "=> %s (%s)", p.jid, p.nick); end module:log("debug", "Contacts:"); for _, c in pairs(self.contacts) do module:log("debug", "=> %s", c); end if self.subscriptions then module:log("debug", "Subscriptions:"); for user, subs in pairs(self.subscriptions) do module:log("debug", "[%s]", user); for _, sub in pairs(self.subscriptions[user]) do module:log("debug", "=> %s", sub); end end end end return { Channel = Channel, Participant = Participant };