Mercurial > prosody-hg
comparison plugins/muc/mod_muc.lua @ 6109:566aba0482b6
plugins/muc/mod_muc: Refactor to use new methods available
| author | daurnimator <quae@daurnimator.com> |
|---|---|
| date | Tue, 18 Mar 2014 18:55:52 -0400 |
| parents | 3a1c39b31497 |
| children | d007b3c49078 |
comparison
equal
deleted
inserted
replaced
| 6108:aae7bc9d6e93 | 6109:566aba0482b6 |
|---|---|
| 104 end | 104 end |
| 105 module:fire_event("muc-room-created", { room = room }); | 105 module:fire_event("muc-room-created", { room = room }); |
| 106 return room; | 106 return room; |
| 107 end | 107 end |
| 108 | 108 |
| 109 function forget_room(jid) | |
| 110 rooms[jid] = nil; | |
| 111 end | |
| 112 | |
| 113 function get_room_from_jid(room_jid) | |
| 114 return rooms[room_jid] | |
| 115 end | |
| 116 | |
| 109 local persistent_errors = false; | 117 local persistent_errors = false; |
| 110 for jid in pairs(persistent_rooms) do | 118 for jid in pairs(persistent_rooms) do |
| 111 local node = jid_split(jid); | 119 local node = jid_split(jid); |
| 112 local data = room_configs:get(node); | 120 local data = room_configs:get(node); |
| 113 if data then | 121 if data then |
| 123 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end | 131 if persistent_errors then persistent_rooms_storage:set(nil, persistent_rooms); end |
| 124 | 132 |
| 125 local host_room = muc_new_room(muc_host); | 133 local host_room = muc_new_room(muc_host); |
| 126 host_room.route_stanza = room_route_stanza; | 134 host_room.route_stanza = room_route_stanza; |
| 127 host_room.save = room_save; | 135 host_room.save = room_save; |
| 136 rooms[muc_host] = host_room; | |
| 128 | 137 |
| 129 module:hook("host-disco-items", function(event) | 138 module:hook("host-disco-items", function(event) |
| 130 local reply = event.reply; | 139 local reply = event.reply; |
| 131 module:log("debug", "host-disco-items called"); | 140 module:log("debug", "host-disco-items called"); |
| 132 for jid, room in pairs(rooms) do | 141 for jid, room in pairs(rooms) do |
| 134 reply:tag("item", {jid=jid, name=room:get_name()}):up(); | 143 reply:tag("item", {jid=jid, name=room:get_name()}):up(); |
| 135 end | 144 end |
| 136 end | 145 end |
| 137 end); | 146 end); |
| 138 | 147 |
| 139 function stanza_handler(event) | 148 module:hook("muc-room-destroyed",function(event) |
| 149 local room = event.room | |
| 150 forget_room(room.jid) | |
| 151 end) | |
| 152 | |
| 153 module:hook("muc-occupant-left",function(event) | |
| 154 local room = event.room | |
| 155 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room | |
| 156 module:fire_event("muc-room-destroyed", { room = room }); | |
| 157 end | |
| 158 end); | |
| 159 | |
| 160 -- Watch presence to create rooms | |
| 161 local function attempt_room_creation(event) | |
| 140 local origin, stanza = event.origin, event.stanza; | 162 local origin, stanza = event.origin, event.stanza; |
| 141 local bare = jid_bare(stanza.attr.to); | 163 local room_jid = jid_bare(stanza.attr.to); |
| 142 local room = rooms[bare]; | 164 if stanza.attr.type == nil and |
| 143 if not room then | 165 get_room_from_jid(room_jid) == nil and |
| 144 if stanza.name ~= "presence" then | 166 ( |
| 145 origin.send(st.error_reply(stanza, "cancel", "item-not-found")); | 167 not(restrict_room_creation) or |
| 168 is_admin(stanza.attr.from) or | |
| 169 ( | |
| 170 restrict_room_creation == "local" and | |
| 171 select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "") | |
| 172 ) | |
| 173 ) then | |
| 174 create_room(room_jid); | |
| 175 end | |
| 176 end | |
| 177 module:hook("presence/full", attempt_room_creation, -1) | |
| 178 module:hook("presence/bare", attempt_room_creation, -1) | |
| 179 module:hook("presence/host", attempt_room_creation, -1) | |
| 180 | |
| 181 for event_name, method in pairs { | |
| 182 -- Normal room interactions | |
| 183 ["iq-get/bare/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ; | |
| 184 ["iq-get/bare/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ; | |
| 185 ["iq-set/bare/http://jabber.org/protocol/muc#admin:item"] = "handle_admin_item_set_command" ; | |
| 186 ["iq-get/bare/http://jabber.org/protocol/muc#admin:item"] = "handle_admin_item_get_command" ; | |
| 187 ["iq-set/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ; | |
| 188 ["iq-get/bare/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ; | |
| 189 ["message/bare"] = "handle_message_to_room" ; | |
| 190 ["presence/bare"] = "handle_presence_to_room" ; | |
| 191 -- Host room | |
| 192 ["iq-get/host/http://jabber.org/protocol/disco#info:query"] = "handle_disco_info_get_query" ; | |
| 193 ["iq-get/host/http://jabber.org/protocol/disco#items:query"] = "handle_disco_items_get_query" ; | |
| 194 ["iq-set/host/http://jabber.org/protocol/muc#admin:item"] = "handle_admin_item_set_command" ; | |
| 195 ["iq-get/host/http://jabber.org/protocol/muc#admin:item"] = "handle_admin_item_get_command" ; | |
| 196 ["iq-set/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_set_to_room" ; | |
| 197 ["iq-get/host/http://jabber.org/protocol/muc#owner:query"] = "handle_owner_query_get_to_room" ; | |
| 198 ["message/host"] = "handle_message_to_room" ; | |
| 199 ["presence/host"] = "handle_presence_to_room" ; | |
| 200 -- Direct to occupant (normal rooms and host room) | |
| 201 ["presence/full"] = "handle_presence_to_occupant" ; | |
| 202 ["iq/full"] = "handle_iq_to_occupant" ; | |
| 203 ["message/full"] = "handle_message_to_occupant" ; | |
| 204 } do | |
| 205 module:hook(event_name, function (event) | |
| 206 local origin, stanza = event.origin, event.stanza; | |
| 207 local room = get_room_from_jid(jid_bare(stanza.attr.to)) | |
| 208 if room == nil then | |
| 209 origin.send(st.error_reply(stanza, "cancel", "not-allowed")); | |
| 146 return true; | 210 return true; |
| 147 end | 211 end |
| 148 if not(restrict_room_creation) or | 212 return room[method](room, origin, stanza); |
| 149 is_admin(stanza.attr.from) or | 213 end, -2) |
| 150 (restrict_room_creation == "local" and select(2, jid_split(stanza.attr.from)) == module.host:gsub("^[^%.]+%.", "")) then | 214 end |
| 151 room = create_room(bare); | 215 |
| 152 end | |
| 153 end | |
| 154 if room then | |
| 155 room:handle_stanza(origin, stanza); | |
| 156 if not next(room._occupants) and not persistent_rooms[room.jid] then -- empty, non-persistent room | |
| 157 module:fire_event("muc-room-destroyed", { room = room }); | |
| 158 rooms[bare] = nil; -- discard room | |
| 159 end | |
| 160 else | |
| 161 origin.send(st.error_reply(stanza, "cancel", "not-allowed")); | |
| 162 end | |
| 163 return true; | |
| 164 end | |
| 165 module:hook("iq/bare", stanza_handler, -1); | |
| 166 module:hook("message/bare", stanza_handler, -1); | |
| 167 module:hook("presence/bare", stanza_handler, -1); | |
| 168 module:hook("iq/full", stanza_handler, -1); | |
| 169 module:hook("message/full", stanza_handler, -1); | |
| 170 module:hook("presence/full", stanza_handler, -1); | |
| 171 | |
| 172 local function handle_to_domain(event) | |
| 173 local origin, stanza = event.origin, event.stanza; | |
| 174 local type = stanza.attr.type; | |
| 175 if type == "error" then return; end | |
| 176 host_room:handle_stanza(origin, stanza); | |
| 177 -- origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it")); | |
| 178 return true; | |
| 179 end | |
| 180 module:hook("message/host", handle_to_domain, -1); | |
| 181 module:hook("presence/host", handle_to_domain, -1); | |
| 182 | 216 |
| 183 hosts[module.host].send = function(stanza) -- FIXME do a generic fix | 217 hosts[module.host].send = function(stanza) -- FIXME do a generic fix |
| 184 if stanza.attr.type == "result" or stanza.attr.type == "error" then | 218 if stanza.attr.type == "result" or stanza.attr.type == "error" then |
| 185 module:send(stanza); | 219 module:send(stanza); |
| 186 else error("component.send only supports result and error stanzas at the moment"); end | 220 else error("component.send only supports result and error stanzas at the moment"); end |
