Software / code / prosody
Annotate
plugins/muc/muc.lib.lua @ 1741:2919f3b985fc
MUC: Added support for generating unique room names
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Mon, 07 Sep 2009 20:29:04 +0500 |
| parent | 1740:b37ccf9bec89 |
| child | 1742:1483a62d69bb |
| rev | line source |
|---|---|
| 1734 | 1 -- Prosody IM |
| 2 -- Copyright (C) 2008-2009 Matthew Wild | |
| 3 -- Copyright (C) 2008-2009 Waqas Hussain | |
| 4 -- | |
| 5 -- This project is MIT/X11 licensed. Please see the | |
| 6 -- COPYING file in the source package for more information. | |
| 7 -- | |
| 8 | |
| 9 local datamanager = require "util.datamanager"; | |
| 10 local datetime = require "util.datetime"; | |
| 11 | |
| 12 local jid_split = require "util.jid".split; | |
| 13 local jid_bare = require "util.jid".bare; | |
| 14 local st = require "util.stanza"; | |
| 15 local log = require "util.logger".init("mod_muc"); | |
| 16 local multitable_new = require "util.multitable".new; | |
| 17 local t_insert, t_remove = table.insert, table.remove; | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
18 local setmetatable = setmetatable; |
| 1734 | 19 |
| 20 local muc_domain = nil; --module:get_host(); | |
| 21 local history_length = 20; | |
| 22 | |
| 23 ------------ | |
| 24 local function filter_xmlns_from_array(array, filters) | |
| 25 local count = 0; | |
| 26 for i=#array,1,-1 do | |
| 27 local attr = array[i].attr; | |
| 28 if filters[attr and attr.xmlns] then | |
| 29 t_remove(array, i); | |
| 30 count = count + 1; | |
| 31 end | |
| 32 end | |
| 33 return count; | |
| 34 end | |
| 35 local function filter_xmlns_from_stanza(stanza, filters) | |
| 36 if filters then | |
| 37 if filter_xmlns_from_array(stanza.tags, filters) ~= 0 then | |
| 38 return stanza, filter_xmlns_from_array(stanza, filters); | |
| 39 end | |
| 40 end | |
| 41 return stanza, 0; | |
| 42 end | |
| 43 local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true}; | |
| 44 local function get_filtered_presence(stanza) | |
| 45 return filter_xmlns_from_stanza(st.clone(stanza), presence_filters); | |
| 46 end | |
| 47 local kickable_error_conditions = { | |
| 48 ["gone"] = true; | |
| 49 ["internal-server-error"] = true; | |
| 50 ["item-not-found"] = true; | |
| 51 ["jid-malformed"] = true; | |
| 52 ["recipient-unavailable"] = true; | |
| 53 ["redirect"] = true; | |
| 54 ["remote-server-not-found"] = true; | |
| 55 ["remote-server-timeout"] = true; | |
| 56 ["service-unavailable"] = true; | |
| 57 }; | |
| 58 local function get_kickable_error(stanza) | |
| 59 for _, tag in ipairs(stanza.tags) do | |
| 60 if tag.name == "error" and tag.attr.xmlns == "jabber:client" then | |
| 61 for _, cond in ipairs(tag.tags) do | |
| 62 if cond.attr.xmlns == "urn:ietf:params:xml:ns:xmpp-stanzas" then | |
| 63 return kickable_error_conditions[cond.name] and cond.name; | |
| 64 end | |
| 65 end | |
| 66 return true; -- malformed error message | |
| 67 end | |
| 68 end | |
| 69 return true; -- malformed error message | |
| 70 end | |
| 71 local function getUsingPath(stanza, path, getText) | |
| 72 local tag = stanza; | |
| 73 for _, name in ipairs(path) do | |
| 74 if type(tag) ~= 'table' then return; end | |
| 75 tag = tag:child_with_name(name); | |
| 76 end | |
| 77 if tag and getText then tag = table.concat(tag); end | |
| 78 return tag; | |
| 79 end | |
| 80 local function getTag(stanza, path) return getUsingPath(stanza, path); end | |
| 81 local function getText(stanza, path) return getUsingPath(stanza, path, true); end | |
| 82 ----------- | |
| 83 | |
| 84 --[[function get_room_disco_info(room, stanza) | |
| 85 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info") | |
| 86 :tag("identity", {category='conference', type='text', name=room._data["name"]):up() | |
| 87 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply | |
| 88 end | |
| 89 function get_room_disco_items(room, stanza) | |
| 90 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items"); | |
| 91 end -- TODO allow non-private rooms]] | |
| 92 | |
| 93 -- | |
| 94 | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
95 local room_mt = {}; |
|
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
96 room_mt.__index = room_mt; |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
97 |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
98 function room_mt:get_default_role(affiliation) |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
99 if affiliation == "owner" or affiliation == "admin" then |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
100 return "moderator"; |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
101 elseif affiliation == "member" or not affiliation then |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
102 return "participant"; |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
103 end |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
104 end |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
105 |
|
1736
98f833669d7f
MUC: Fixed function declarations.
Waqas Hussain <waqas20@gmail.com>
parents:
1735
diff
changeset
|
106 function room_mt:broadcast_presence(stanza, code, nick) |
| 1734 | 107 stanza = get_filtered_presence(stanza); |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
108 local data = self._occupants[stanza.attr.from]; |
| 1734 | 109 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) |
| 110 :tag("item", {affiliation=data.affiliation, role=data.role, nick=nick}):up(); | |
| 111 if code then | |
| 112 stanza:tag("status", {code=code}):up(); | |
| 113 end | |
| 114 local me; | |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
115 for occupant, o_data in pairs(self._occupants) do |
| 1734 | 116 if occupant ~= stanza.attr.from then |
| 117 for jid in pairs(o_data.sessions) do | |
| 118 stanza.attr.to = jid; | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
119 self:route_stanza(stanza); |
| 1734 | 120 end |
| 121 else | |
| 122 me = o_data; | |
| 123 end | |
| 124 end | |
| 125 if me then | |
| 126 stanza:tag("status", {code='110'}); | |
| 127 for jid in pairs(me.sessions) do | |
| 128 stanza.attr.to = jid; | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
129 self:route_stanza(stanza); |
| 1734 | 130 end |
| 131 end | |
| 132 end | |
|
1736
98f833669d7f
MUC: Fixed function declarations.
Waqas Hussain <waqas20@gmail.com>
parents:
1735
diff
changeset
|
133 function room_mt:broadcast_message(stanza, historic) |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
134 for occupant, o_data in pairs(self._occupants) do |
| 1734 | 135 for jid in pairs(o_data.sessions) do |
| 136 stanza.attr.to = jid; | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
137 self:route_stanza(stanza); |
| 1734 | 138 end |
| 139 end | |
| 140 if historic then -- add to history | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
141 local history = self._data['history']; |
|
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
142 if not history then history = {}; self._data['history'] = history; end |
| 1734 | 143 -- stanza = st.clone(stanza); |
| 144 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203 | |
| 145 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated) | |
| 146 t_insert(history, st.clone(st.preserialize(stanza))); | |
| 147 while #history > history_length do t_remove(history, 1) end | |
| 148 end | |
| 149 end | |
| 150 | |
| 151 | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
152 function room_mt:send_occupant_list(to) |
|
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
153 local current_nick = self._jid_nick[to]; |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
154 for occupant, o_data in pairs(self._occupants) do |
| 1734 | 155 if occupant ~= current_nick then |
| 156 local pres = get_filtered_presence(o_data.sessions[o_data.jid]); | |
| 157 pres.attr.to, pres.attr.from = to, occupant; | |
| 158 pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) | |
| 159 :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up(); | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
160 self:route_stanza(pres); |
| 1734 | 161 end |
| 162 end | |
| 163 end | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
164 function room_mt:send_history(to) |
|
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
165 local history = self._data['history']; -- send discussion history |
| 1734 | 166 if history then |
| 167 for _, msg in ipairs(history) do | |
| 168 msg = st.deserialize(msg); | |
| 169 msg.attr.to=to; | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
170 self:route_stanza(msg); |
| 1734 | 171 end |
| 172 end | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
173 if self._data['subject'] then |
|
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
174 self:route_stanza(st.message({type='groupchat', from=self.jid, to=to}):tag("subject"):text(self._data['subject'])); |
| 1734 | 175 end |
| 176 end | |
| 177 | |
| 178 local function room_get_disco_info(self, stanza) end | |
| 179 local function room_get_disco_items(self, stanza) end | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
180 function room_mt:set_subject(current_nick, subject) |
| 1734 | 181 -- TODO check nick's authority |
| 182 if subject == "" then subject = nil; end | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
183 self._data['subject'] = subject; |
| 1734 | 184 local msg = st.message({type='groupchat', from=current_nick}) |
| 185 :tag('subject'):text(subject):up(); | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
186 self:broadcast_message(msg, false); |
| 1734 | 187 return true; |
| 188 end | |
| 189 | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
190 function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc |
| 1734 | 191 local from, to = stanza.attr.from, stanza.attr.to; |
| 192 local room = jid_bare(to); | |
| 193 local current_nick = self._jid_nick[from]; | |
| 194 local type = stanza.attr.type; | |
| 195 log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag()); | |
| 196 if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end | |
| 197 if stanza.name == "presence" then | |
| 198 local pr = get_filtered_presence(stanza); | |
| 199 pr.attr.from = current_nick; | |
| 200 if type == "error" then -- error, kick em out! | |
| 201 if current_nick then | |
| 202 log("debug", "kicking %s from %s", current_nick, room); | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
203 self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}) |
|
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
204 :tag('status'):text('This participant is kicked from the room because he sent an error presence')); -- send unavailable |
| 1734 | 205 end |
| 206 elseif type == "unavailable" then -- unavailable | |
| 207 if current_nick then | |
| 208 log("debug", "%s leaving %s", current_nick, room); | |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
209 local data = self._occupants[current_nick]; |
| 1734 | 210 data.role = 'none'; |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
211 self:broadcast_presence(pr); |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
212 self._occupants[current_nick] = nil; |
| 1734 | 213 self._jid_nick[from] = nil; |
| 214 end | |
| 215 elseif not type then -- available | |
| 216 if current_nick then | |
| 217 --if #pr == #stanza or current_nick ~= to then -- commented because google keeps resending directed presence | |
| 218 if current_nick == to then -- simple presence | |
| 219 log("debug", "%s broadcasted presence", current_nick); | |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
220 self._occupants[current_nick].sessions[from] = pr; |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
221 self:broadcast_presence(pr); |
| 1734 | 222 else -- change nick |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
223 if self._occupants[to] then |
| 1734 | 224 log("debug", "%s couldn't change nick", current_nick); |
| 225 origin.send(st.error_reply(stanza, "cancel", "conflict"):tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | |
| 226 else | |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
227 local data = self._occupants[current_nick]; |
| 1734 | 228 local to_nick = select(3, jid_split(to)); |
| 229 if to_nick then | |
| 230 log("debug", "%s (%s) changing nick to %s", current_nick, data.jid, to); | |
| 231 local p = st.presence({type='unavailable', from=current_nick}); | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
232 self:broadcast_presence(p, '303', to_nick); |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
233 self._occupants[current_nick] = nil; |
|
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
234 self._occupants[to] = data; |
| 1734 | 235 self._jid_nick[from] = to; |
| 236 pr.attr.from = to; | |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
237 self._occupants[to].sessions[from] = pr; |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
238 self:broadcast_presence(pr); |
| 1734 | 239 else |
| 240 --TODO malformed-jid | |
| 241 end | |
| 242 end | |
| 243 end | |
| 244 --else -- possible rejoin | |
| 245 -- log("debug", "%s had connection replaced", current_nick); | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
246 -- self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}) |
|
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
247 -- :tag('status'):text('Replaced by new connection'):up()); -- send unavailable |
|
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
248 -- self:handle_to_occupant(origin, stanza); -- resend available |
| 1734 | 249 --end |
| 250 else -- enter room | |
| 251 local new_nick = to; | |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
252 if self._occupants[to] then |
| 1734 | 253 new_nick = nil; |
| 254 end | |
| 255 if not new_nick then | |
| 256 log("debug", "%s couldn't join due to nick conflict: %s", from, to); | |
| 257 origin.send(st.error_reply(stanza, "cancel", "conflict"):tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | |
| 258 else | |
| 259 log("debug", "%s joining as %s", from, to); | |
|
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
260 if not next(self._affiliations) then -- new room, no owners |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
261 self._affiliations[jid_bare(from)] = "owner"; |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
262 end |
|
1740
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
263 local affiliation = self:get_affiliation(from); |
|
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
264 local role = self:get_default_role(affiliation) |
|
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
265 if role then -- new occupant |
|
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
266 self._occupants[to] = {affiliation=affiliation, role=role, jid=from, sessions={[from]=get_filtered_presence(stanza)}}; |
|
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
267 self._jid_nick[from] = to; |
|
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
268 self:send_occupant_list(from); |
|
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
269 pr.attr.from = to; |
|
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
270 self:broadcast_presence(pr); |
|
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
271 self:send_history(from); |
|
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
272 else -- banned |
|
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
273 origin.send(st.error_reply(stanza, "auth", "forbidden"):tag("x", {xmlns = "http://jabber.org/protocol/muc"})); |
| 1734 | 274 end |
| 275 end | |
| 276 end | |
| 277 elseif type ~= 'result' then -- bad type | |
| 278 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error? | |
| 279 end | |
| 280 elseif not current_nick and type ~= "error" and type ~= "result" then -- not in room | |
| 281 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); | |
| 282 elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM | |
| 283 origin.send(st.error_reply(stanza, "modify", "bad-request")); | |
| 284 elseif stanza.name == "message" and type == "error" and get_kickable_error(stanza) then | |
| 285 log("debug", "%s kicked from %s for sending an error message", current_nick, room); | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
286 self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('This participant is kicked from the room because he sent an error message to another occupant')); -- send unavailable |
| 1734 | 287 else -- private stanza |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
288 local o_data = self._occupants[to]; |
| 1734 | 289 if o_data then |
| 290 log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid); | |
| 291 local jid = o_data.jid; | |
| 292 -- TODO if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end | |
| 293 stanza.attr.to, stanza.attr.from = jid, current_nick; | |
| 294 self:route_stanza(stanza); | |
| 295 elseif type ~= "error" and type ~= "result" then -- recipient not in room | |
| 296 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); | |
| 297 end | |
| 298 end | |
| 299 end | |
| 300 | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
301 function room_mt:handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc |
| 1734 | 302 local type = stanza.attr.type; |
| 303 if stanza.name == "iq" and type == "get" then -- disco requests | |
| 304 local xmlns = stanza.tags[1].attr.xmlns; | |
| 305 if xmlns == "http://jabber.org/protocol/disco#info" then | |
| 306 origin.send(room_get_disco_info(self, stanza)); | |
| 307 elseif xmlns == "http://jabber.org/protocol/disco#items" then | |
| 308 origin.send(room_get_disco_items(self, stanza)); | |
| 309 else | |
| 310 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
| 311 end | |
| 312 elseif stanza.name == "message" and type == "groupchat" then | |
| 313 local from, to = stanza.attr.from, stanza.attr.to; | |
| 314 local room = jid_bare(to); | |
| 315 local current_nick = self._jid_nick[from]; | |
| 316 if not current_nick then -- not in room | |
| 317 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); | |
| 318 else | |
| 319 local from = stanza.attr.from; | |
| 320 stanza.attr.from = current_nick; | |
| 321 local subject = getText(stanza, {"subject"}); | |
| 322 if subject then | |
| 323 self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza | |
| 324 else | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
325 self:broadcast_message(stanza, true); |
| 1734 | 326 end |
| 327 end | |
| 328 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick | |
| 329 local to = stanza.attr.to; | |
| 330 local current_nick = self._jid_nick[stanza.attr.from]; | |
| 331 if current_nick then | |
| 332 stanza.attr.to = current_nick; | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
333 self:handle_to_occupant(origin, stanza); |
| 1734 | 334 stanza.attr.to = to; |
| 335 elseif type ~= "error" and type ~= "result" then | |
| 336 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
| 337 end | |
| 338 elseif stanza.name == "message" and not stanza.attr.type and #stanza.tags == 1 and self._jid_nick[stanza.attr.from] | |
| 339 and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" and #stanza.tags[1].tags == 1 | |
| 340 and stanza.tags[1].tags[1].name == "invite" and stanza.tags[1].tags[1].attr.to then | |
| 341 local _from, _to = stanza.attr.from, stanza.attr.to; | |
| 342 local _invitee = stanza.tags[1].tags[1].attr.to; | |
| 343 stanza.attr.from, stanza.attr.to = _to, _invitee; | |
| 344 stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = _from, nil; | |
| 345 self:route_stanza(stanza); | |
| 346 stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = nil, _invitee; | |
| 347 stanza.attr.from, stanza.attr.to = _from, _to; | |
| 348 else | |
| 349 if type == "error" or type == "result" then return; end | |
| 350 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
| 351 end | |
| 352 end | |
| 353 | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
354 function room_mt:handle_stanza(origin, stanza) |
| 1734 | 355 local to_node, to_host, to_resource = jid_split(stanza.attr.to); |
| 356 if to_resource then | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
357 self:handle_to_occupant(origin, stanza); |
| 1734 | 358 else |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
359 self:handle_to_room(origin, stanza); |
| 1734 | 360 end |
| 361 end | |
| 362 | |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
363 function room_mt:route_stanza(stanza) end -- Replace with a routing function, e.g., function(room, stanza) core_route_stanza(origin, stanza); end |
|
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
364 |
|
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
365 function room_mt:get_affiliation(jid) |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
366 local node, host, resource = jid_split(jid); |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
367 local bare = node and node.."@"..host or host; |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
368 local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID. |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
369 if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
370 return result; |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
371 end |
| 1734 | 372 |
|
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
373 function room_mt:set_affiliation(jid, affiliation) |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
374 local node, host, resource = jid_split(jid); |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
375 local bare = node and node.."@"..host or host; |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
376 if affiliation == "none" then affiliation = nil; end |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
377 if affiliation and affiliation ~= "outcast" and affiliation ~= "owner" and affiliation ~= "admin" and affiliation ~= "member" then return false; end |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
378 self._affiliations[bare] = affiliation; |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
379 -- TODO set roles based on new affiliation |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
380 return true; |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
381 end |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
382 |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
383 local _M = {}; -- module "muc" |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
384 |
|
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
385 function _M:new_room(jid) |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
386 return setmetatable({ |
| 1734 | 387 jid = jid; |
| 388 _jid_nick = {}; | |
|
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
389 _occupants = {}; |
| 1734 | 390 _data = {}; |
|
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
391 _affiliations = {}; |
|
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
392 }, room_mt); |
| 1734 | 393 end |
| 394 | |
| 395 return _M; | |
| 396 | |
| 397 --[[function get_disco_info(stanza) | |
| 398 return st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info") | |
| 399 :tag("identity", {category='conference', type='text', name=muc_name}):up() | |
| 400 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply | |
| 401 end | |
| 402 function get_disco_items(stanza) | |
| 403 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items"); | |
| 404 for room in pairs(rooms_info:get()) do | |
| 405 reply:tag("item", {jid=room, name=rooms_info:get(room, "name")}):up(); | |
| 406 end | |
| 407 return reply; -- TODO cache disco reply | |
| 408 end]] | |
| 409 | |
| 410 --[[function handle_to_domain(origin, stanza) | |
| 411 local type = stanza.attr.type; | |
| 412 if type == "error" or type == "result" then return; end | |
| 413 if stanza.name == "iq" and type == "get" then | |
| 414 local xmlns = stanza.tags[1].attr.xmlns; | |
| 415 if xmlns == "http://jabber.org/protocol/disco#info" then | |
| 416 origin.send(get_disco_info(stanza)); | |
| 417 elseif xmlns == "http://jabber.org/protocol/disco#items" then | |
| 418 origin.send(get_disco_items(stanza)); | |
| 419 else | |
| 420 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc | |
| 421 end | |
| 422 else | |
| 423 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it")); | |
| 424 end | |
| 425 end | |
| 426 | |
| 427 register_component(muc_domain, function(origin, stanza) | |
| 428 local to_node, to_host, to_resource = jid_split(stanza.attr.to); | |
| 429 if to_resource and not to_node then | |
| 430 if type == "error" or type == "result" then return; end | |
| 431 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource | |
| 432 elseif to_resource then | |
| 433 handle_to_occupant(origin, stanza); | |
| 434 elseif to_node then | |
| 435 handle_to_room(origin, stanza) | |
| 436 else -- to the main muc domain | |
| 437 if type == "error" or type == "result" then return; end | |
| 438 handle_to_domain(origin, stanza); | |
| 439 end | |
| 440 end);]] | |
| 441 | |
| 442 --[[module.unload = function() | |
| 443 deregister_component(muc_domain); | |
| 444 end | |
| 445 module.save = function() | |
| 446 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list}; | |
| 447 end | |
| 448 module.restore = function(data) | |
| 449 rooms.data, jid_nick.data, rooms_info.data, persist_list = | |
| 450 data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {}; | |
| 451 end]] |