Merge 0.10->trunk
authorMatthew Wild <mwild1@gmail.com>
Tue, 17 Nov 2015 17:12:45 +0000 (17:12 +0000)
committerMatthew Wild <mwild1@gmail.com>
Tue, 17 Nov 2015 17:12:45 +0000 (17:12 +0000)
1  2 
plugins/muc/muc.lib.lua

index 7db463e5bb0c6360011a4a517e110d78e950bcb4,48231c375a2c6db801e432b35c3164c864112ec8..b2f89972ddf387983ab03e8de9af212c4788958e
@@@ -279,312 -347,240 +279,312 @@@ function room_mt:publicise_occupant_sta
        end
  end
  
 -function room_mt:get_whois()
 -      return self._data.whois;
 +function room_mt:send_occupant_list(to, filter)
 +      local to_bare = jid_bare(to);
 +      local is_anonymous = false;
 +      local whois = self:get_whois();
 +      if whois ~= "anyone" then
 +              local affiliation = self:get_affiliation(to);
 +              if affiliation ~= "admin" and affiliation ~= "owner" then
 +                      local occupant = self:get_occupant_by_real_jid(to);
 +                      if not (occupant and can_see_real_jids(whois, occupant)) then
 +                              is_anonymous = true;
 +                      end
 +              end
 +      end
 +      for occupant_jid, occupant in self:each_occupant() do
 +              if filter == nil or filter(occupant_jid, occupant) then
 +                      local x = st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'});
 +                      self:build_item_list(occupant, x, is_anonymous and to_bare ~= occupant.bare_jid); -- can always see your own jids
 +                      local pres = st.clone(occupant:get_presence());
 +                      pres.attr.to = to;
 +                      pres:add_child(x);
 +                      self:route_stanza(pres);
 +              end
 +      end
  end
  
 -local function construct_stanza_id(room, stanza)
 -      local from_jid, to_nick = stanza.attr.from, stanza.attr.to;
 -      local from_nick = room._jid_nick[from_jid];
 -      local occupant = room._occupants[to_nick];
 -      local to_jid = occupant.jid;
 +function room_mt:get_disco_info(stanza)
 +      local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#info");
 +      local form = dataform.new {
 +              { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/muc#roominfo" };
 +      };
 +      module:fire_event("muc-disco#info", {room = self; reply = reply; form = form;});
 +      reply:add_child(form:form(nil, "result"));
 +      return reply;
 +end
 +module:hook("muc-disco#info", function(event)
 +      event.reply:tag("feature", {var = "http://jabber.org/protocol/muc"}):up();
 +end);
 +module:hook("muc-disco#info", function(event)
 +      local count = iterators.count(event.room:each_occupant());
 +      table.insert(event.form, { name = "muc#roominfo_occupants", label = "Number of occupants", value = tostring(count) });
 +end);
  
 -      return from_nick, to_jid, base64.encode(to_jid.."\0"..stanza.attr.id.."\0"..md5(from_jid));
 +function room_mt:get_disco_items(stanza)
 +      local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items");
 +      for room_jid in self:each_occupant() do
 +              reply:tag("item", {jid = room_jid, name = room_jid:match("/(.*)")}):up();
 +      end
 +      return reply;
  end
 -local function deconstruct_stanza_id(room, stanza)
 -      local from_jid_possiblybare, to_nick = stanza.attr.from, stanza.attr.to;
 -      local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(%Z+)%z(%Z*)%z(.+)$");
 -      local from_nick = room._jid_nick[from_jid];
 -
 -      if not(from_nick) then return; end
 -      if not(from_jid_possiblybare == from_jid or from_jid_possiblybare == jid_bare(from_jid)) then return; end
 -
 -      local occupant = room._occupants[to_nick];
 -      for to_jid in pairs(occupant and occupant.sessions or {}) do
 -              if md5(to_jid) == to_jid_hash then
 -                      return from_nick, to_jid, id;
 -              end
 +
 +function room_mt:handle_kickable(origin, stanza)
 +      local real_jid = stanza.attr.from;
 +      local occupant = self:get_occupant_by_real_jid(real_jid);
 +      if occupant == nil then return nil; end
 +      local type, condition, text = stanza:get_error();
 +      local error_message = "Kicked: "..(condition and condition:gsub("%-", " ") or "presence error");
 +      if text then
 +              error_message = error_message..": "..text;
        end
 +      occupant:set_session(real_jid, st.presence({type="unavailable"})
 +              :tag('status'):text(error_message));
 +      self:save_occupant(occupant);
 +      local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";})
 +              :tag("status", {code = "307"})
 +      self:publicise_occupant_status(occupant, x);
 +      if occupant.jid == real_jid then -- Was last session
 +              module:fire_event("muc-occupant-left", {room = self; nick = occupant.nick; occupant = occupant;});
 +      end
 +      return true;
  end
  
 +-- Give the room creator owner affiliation
 +module:hook("muc-room-pre-create", function(event)
 +      event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner");
 +end, -1);
  
 -function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
 -      local from, to = stanza.attr.from, stanza.attr.to;
 -      local room = jid_bare(to);
 -      local current_nick = self._jid_nick[from];
 +-- check if user is banned
 +module:hook("muc-occupant-pre-join", function(event)
 +      local room, stanza = event.room, event.stanza;
 +      local affiliation = room:get_affiliation(stanza.attr.from);
 +      if affiliation == "outcast" then
 +              local reply = st.error_reply(stanza, "auth", "forbidden"):up();
 +              reply.tags[1].attr.code = "403";
 +              event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
 +              return true;
 +      end
 +end, -10);
 +
 +function room_mt:handle_presence_to_occupant(origin, stanza)
        local type = stanza.attr.type;
 -      log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag());
 -      if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end
 -      if stanza.name == "presence" then
 -              local pr = get_filtered_presence(stanza);
 -              pr.attr.from = current_nick;
 -              if type == "error" then -- error, kick em out!
 -                      if current_nick then
 -                              log("debug", "kicking %s from %s", current_nick, room);
 -                              self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza));
 +      if type == "error" then -- error, kick em out!
 +              return self:handle_kickable(origin, stanza)
 +      elseif type == nil or type == "unavailable" then
 +              local real_jid = stanza.attr.from;
 +              local bare_jid = jid_bare(real_jid);
 +              local orig_occupant, dest_occupant;
 +              local is_new_room = next(self._affiliations) == nil;
 +              if is_new_room then
 +                      if type == "unavailable" then return true; end -- Unavailable from someone not in the room
 +                      if module:fire_event("muc-room-pre-create", {
 +                                      room = self;
 +                                      origin = origin;
 +                                      stanza = stanza;
 +                              }) then return true; end
 +              else
 +                      orig_occupant = self:get_occupant_by_real_jid(real_jid);
 +                      if type == "unavailable" and orig_occupant == nil then return true; end -- Unavailable from someone not in the room
 +              end
 +              local is_first_dest_session;
 +              if type == "unavailable" then
 +                      -- dest_occupant = nil
 +              elseif orig_occupant and orig_occupant.nick == stanza.attr.to then -- Just a presence update
 +                      log("debug", "presence update for %s from session %s", orig_occupant.nick, real_jid);
 +                      dest_occupant = orig_occupant;
 +              else
 +                      local dest_jid = stanza.attr.to;
 +                      dest_occupant = self:get_occupant_by_nick(dest_jid);
 +                      if dest_occupant == nil then
 +                              log("debug", "no occupant found for %s; creating new occupant object for %s", dest_jid, real_jid);
 +                              is_first_dest_session = true;
 +                              dest_occupant = self:new_occupant(bare_jid, dest_jid);
 +                      else
 +                              is_first_dest_session = false;
                        end
 -              elseif type == "unavailable" then -- unavailable
 -                      if current_nick then
 -                              log("debug", "%s leaving %s", current_nick, room);
 -                              self._jid_nick[from] = nil;
 -                              local occupant = self._occupants[current_nick];
 -                              local new_jid = next(occupant.sessions);
 -                              if new_jid == from then new_jid = next(occupant.sessions, new_jid); end
 -                              if new_jid then
 -                                      local jid = occupant.jid;
 -                                      occupant.jid = new_jid;
 -                                      occupant.sessions[from] = nil;
 -                                      pr.attr.to = from;
 -                                      pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
 -                                              :tag("item", {affiliation=occupant.affiliation or "none", role='none'}):up()
 -                                              :tag("status", {code='110'}):up();
 -                                      self:_route_stanza(pr);
 -                                      if jid ~= new_jid then
 -                                              pr = st.clone(occupant.sessions[new_jid])
 -                                                      :tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
 -                                                      :tag("item", {affiliation=occupant.affiliation or "none", role=occupant.role or "none"});
 -                                              pr.attr.from = current_nick;
 -                                              self:broadcast_except_nick(pr, current_nick);
 -                                      end
 -                              else
 -                                      occupant.role = 'none';
 -                                      self:broadcast_presence(pr, from);
 -                                      self._occupants[current_nick] = nil;
 +              end
 +              local is_last_orig_session;
 +              if orig_occupant ~= nil then
 +                      -- Is there are least 2 sessions?
 +                      local iter, ob, last = orig_occupant:each_session();
 +                      is_last_orig_session = iter(ob, iter(ob, last)) == nil;
 +              end
 +
 +              local event, event_name = {
 +                      room = self;
 +                      origin = origin;
 +                      stanza = stanza;
 +                      is_first_session = is_first_dest_session;
 +                      is_last_session = is_last_orig_session;
 +              };
 +              if orig_occupant == nil then
 +                      event_name = "muc-occupant-pre-join";
 +                      event.is_new_room = is_new_room;
 +                      event.occupant = dest_occupant;
 +              elseif dest_occupant == nil then
 +                      event_name = "muc-occupant-pre-leave";
 +                      event.occupant = orig_occupant;
 +              else
 +                      event_name = "muc-occupant-pre-change";
 +                      event.orig_occupant = orig_occupant;
 +                      event.dest_occupant = dest_occupant;
 +              end
 +              if module:fire_event(event_name, event) then return true; end
 +
 +              -- Check for nick conflicts
 +              if dest_occupant ~= nil and not is_first_dest_session and bare_jid ~= jid_bare(dest_occupant.bare_jid) then -- new nick or has different bare real jid
 +                      log("debug", "%s couldn't join due to nick conflict: %s", real_jid, dest_occupant.nick);
 +                      local reply = st.error_reply(stanza, "cancel", "conflict"):up();
 +                      reply.tags[1].attr.code = "409";
 +                      origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
 +                      return true;
 +              end
 +
 +              -- Send presence stanza about original occupant
 +              if orig_occupant ~= nil and orig_occupant ~= dest_occupant then
 +                      local orig_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
 +                      local dest_nick;
 +                      if dest_occupant == nil then -- Session is leaving
 +                              log("debug", "session %s is leaving occupant %s", real_jid, orig_occupant.nick);
 +                              if is_last_orig_session then
 +                                      orig_occupant.role = nil;
                                end
 -                      end
 -              elseif not type then -- available
 -                      if current_nick then
 -                              --if #pr == #stanza or current_nick ~= to then -- commented because google keeps resending directed presence
 -                                      if current_nick == to then -- simple presence
 -                                              log("debug", "%s broadcasted presence", current_nick);
 -                                              self._occupants[current_nick].sessions[from] = pr;
 -                                              self:broadcast_presence(pr, from);
 -                                      else -- change nick
 -                                              local occupant = self._occupants[current_nick];
 -                                              local is_multisession = next(occupant.sessions, next(occupant.sessions));
 -                                              if self._occupants[to] or is_multisession then
 -                                                      log("debug", "%s couldn't change nick", current_nick);
 -                                                      local reply = st.error_reply(stanza, "cancel", "conflict"):up();
 -                                                      reply.tags[1].attr.code = "409";
 -                                                      origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
 -                                              else
 -                                                      local data = self._occupants[current_nick];
 -                                                      local to_nick = select(3, jid_split(to));
 -                                                      if to_nick then
 -                                                              log("debug", "%s (%s) changing nick to %s", current_nick, data.jid, to);
 -                                                              local p = st.presence({type='unavailable', from=current_nick});
 -                                                              self:broadcast_presence(p, from, '303', to_nick);
 -                                                              self._occupants[current_nick] = nil;
 -                                                              self._occupants[to] = data;
 -                                                              self._jid_nick[from] = to;
 -                                                              pr.attr.from = to;
 -                                                              self._occupants[to].sessions[from] = pr;
 -                                                              self:broadcast_presence(pr, from);
 -                                                      else
 -                                                              --TODO malformed-jid
 -                                                      end
 -                                              end
 -                                      end
 -                              --else -- possible rejoin
 -                              --      log("debug", "%s had connection replaced", current_nick);
 -                              --      self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to})
 -                              --              :tag('status'):text('Replaced by new connection'):up()); -- send unavailable
 -                              --      self:handle_to_occupant(origin, stanza); -- resend available
 -                              --end
 -                      else -- enter room
 -                              local new_nick = to;
 -                              local is_merge;
 -                              if self._occupants[to] then
 -                                      if jid_bare(from) ~= jid_bare(self._occupants[to].jid) then
 -                                              new_nick = nil;
 -                                      end
 -                                      is_merge = true;
 +                              orig_occupant:set_session(real_jid, stanza);
 +                      else
 +                              log("debug", "session %s is changing from occupant %s to %s", real_jid, orig_occupant.nick, dest_occupant.nick);
 +                              local generated_unavail = st.presence {from = orig_occupant.nick, to = real_jid, type = "unavailable"};
 +                              orig_occupant:set_session(real_jid, generated_unavail);
 +                              dest_nick = select(3, jid_split(dest_occupant.nick));
 +                              if not is_first_dest_session then -- User is swapping into another pre-existing session
 +                                      log("debug", "session %s is swapping into multisession %s, showing it leave.", real_jid, dest_occupant.nick);
 +                                      -- Show the other session leaving
 +                                      local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";})
 +                                              :tag("status"):text("you are joining pre-existing session " .. dest_nick):up();
 +                                      add_item(x, self:get_affiliation(bare_jid), "none");
 +                                      local pr = st.presence{from = dest_occupant.nick, to = real_jid, type = "unavailable"}
 +                                              :add_child(x);
 +                                      self:route_stanza(pr);
                                end
 -                              local password = stanza:get_child("x", "http://jabber.org/protocol/muc");
 -                              password = password and password:get_child("password", "http://jabber.org/protocol/muc");
 -                              password = password and password[1] ~= "" and password[1];
 -                              if self:get_password() and self:get_password() ~= password then
 -                                      log("debug", "%s couldn't join due to invalid password: %s", from, to);
 -                                      local reply = st.error_reply(stanza, "auth", "not-authorized"):up();
 -                                      reply.tags[1].attr.code = "401";
 -                                      origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
 -                              elseif not new_nick then
 -                                      log("debug", "%s couldn't join due to nick conflict: %s", from, to);
 -                                      local reply = st.error_reply(stanza, "cancel", "conflict"):up();
 -                                      reply.tags[1].attr.code = "409";
 -                                      origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
 -                              else
 -                                      log("debug", "%s joining as %s", from, to);
 -                                      if not next(self._affiliations) then -- new room, no owners
 -                                              self._affiliations[jid_bare(from)] = "owner";
 -                                              if self.locked and not stanza:get_child("x", "http://jabber.org/protocol/muc") then
 -                                                      self.locked = nil; -- Older groupchat protocol doesn't lock
 -                                              end
 -                                      elseif self.locked then -- Deny entry
 -                                              origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
 -                                              return;
 -                                      end
 -                                      local affiliation = self:get_affiliation(from);
 -                                      local role = self:get_default_role(affiliation)
 -                                      if role then -- new occupant
 -                                              if not is_merge then
 -                                                      self._occupants[to] = {affiliation=affiliation, role=role, jid=from, sessions={[from]=get_filtered_presence(stanza)}};
 -                                              else
 -                                                      self._occupants[to].sessions[from] = get_filtered_presence(stanza);
 -                                              end
 -                                              self._jid_nick[from] = to;
 -                                              self:send_occupant_list(from);
 -                                              pr.attr.from = to;
 -                                              pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
 -                                                      :tag("item", {affiliation=affiliation or "none", role=role or "none"}):up();
 -                                              if not is_merge then
 -                                                      self:broadcast_except_nick(pr, to);
 -                                              end
 -                                              pr:tag("status", {code='110'}):up();
 -                                              if self._data.whois == 'anyone' then
 -                                                      pr:tag("status", {code='100'}):up();
 -                                              end
 -                                              if self.locked then
 -                                                      pr:tag("status", {code='201'}):up();
 -                                              end
 -                                              pr.attr.to = from;
 -                                              self:_route_stanza(pr);
 -                                              self:send_history(from, stanza);
 -                                              self:send_subject(from);
 -                                      elseif not affiliation then -- registration required for entering members-only room
 -                                              local reply = st.error_reply(stanza, "auth", "registration-required"):up();
 -                                              reply.tags[1].attr.code = "407";
 -                                              origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
 -                                      else -- banned
 -                                              local reply = st.error_reply(stanza, "auth", "forbidden"):up();
 -                                              reply.tags[1].attr.code = "403";
 -                                              origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
 -                                      end
 +                              if is_first_dest_session and is_last_orig_session then -- Normal nick change
 +                                      log("debug", "no sessions in %s left; publically marking as nick change", orig_occupant.nick);
 +                                      orig_x:tag("status", {code = "303";}):up();
 +                              else -- The session itself always needs to see a nick change
 +                                      -- don't want to get our old nick's available presence,
 +                                      -- so remove our session from there, and manually generate an unavailable
 +                                      orig_occupant:remove_session(real_jid);
 +                                      log("debug", "generating nick change for %s", real_jid);
 +                                      local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
 +                                      -- self:build_item_list(orig_occupant, x, false, dest_nick); -- COMPAT: clients get confused if they see other items besides their own
 +                                      add_item(x, self:get_affiliation(bare_jid), orig_occupant.role, real_jid, dest_nick);
 +                                      x:tag("status", {code = "303";}):up();
 +                                      x:tag("status", {code = "110";}):up();
 +                                      self:route_stanza(generated_unavail:add_child(x));
 +                                      dest_nick = nil; -- set dest_nick to nil; so general populance doesn't see it for whole orig_occupant
                                end
                        end
 -              elseif type ~= 'result' then -- bad type
 -                      if type ~= 'visible' and type ~= 'invisible' then -- COMPAT ejabberd can broadcast or forward XEP-0018 presences
 -                              origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
 +                      self:save_occupant(orig_occupant);
 +                      self:publicise_occupant_status(orig_occupant, orig_x, dest_nick);
 +
 +                      if is_last_orig_session then
 +                              module:fire_event("muc-occupant-left", {room = self; nick = orig_occupant.nick; occupant = orig_occupant;});
                        end
                end
 -      elseif not current_nick then -- not in room
 -              if (type == "error" or type == "result") and stanza.name == "iq" then
 -                      local id = stanza.attr.id;
 -                      stanza.attr.from, stanza.attr.to, stanza.attr.id = deconstruct_stanza_id(self, stanza);
 -                      if stanza.attr.id then
 -                              self:_route_stanza(stanza);
 +
 +              if dest_occupant ~= nil then
 +                      dest_occupant:set_session(real_jid, stanza);
 +                      local dest_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
 +                      if is_new_room then
 +                              dest_x:tag("status", {code = "201"}):up();
                        end
 -                      stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
 -              elseif type ~= "error" then
 -                      origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
 -              end
 -      elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM
 -              origin.send(st.error_reply(stanza, "modify", "bad-request"));
 -      elseif current_nick and stanza.name == "message" and type == "error" and is_kickable_error(stanza) then
 -              log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid);
 -              self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); -- send unavailable
 -      else -- private stanza
 -              local o_data = self._occupants[to];
 -              if o_data then
 -                      log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
 -                      if stanza.name == "iq" then
 -                              local id = stanza.attr.id;
 -                              if stanza.attr.type == "get" or stanza.attr.type == "set" then
 -                                      stanza.attr.from, stanza.attr.to, stanza.attr.id = construct_stanza_id(self, stanza);
 -                              else
 -                                      stanza.attr.from, stanza.attr.to, stanza.attr.id = deconstruct_stanza_id(self, stanza);
 -                              end
 -                              if type == 'get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then
 -                                      stanza.attr.to = jid_bare(stanza.attr.to);
 -                              end
 -                              if stanza.attr.id then
 -                                      self:_route_stanza(stanza);
 +                      if orig_occupant == nil and self:get_whois() == "anyone" then
 +                              dest_x:tag("status", {code = "100"}):up();
 +                      end
 +                      self:save_occupant(dest_occupant);
 +
 +                      if orig_occupant == nil then
 +                              -- Send occupant list to newly joined user
 +                              self:send_occupant_list(real_jid, function(nick, occupant)
 +                                      -- Don't include self
 +                                      return occupant:get_presence(real_jid) == nil;
 +                              end)
 +                      end
 +                      self:publicise_occupant_status(dest_occupant, dest_x);
 +
 +                      if orig_occupant ~= nil and orig_occupant ~= dest_occupant and not is_last_orig_session then -- If user is swapping and wasn't last original session
 +                              log("debug", "session %s split nicks; showing %s rejoining", real_jid, orig_occupant.nick);
 +                              -- Show the original nick joining again
 +                              local pr = st.clone(orig_occupant:get_presence());
 +                              pr.attr.to = real_jid;
 +                              local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
 +                              self:build_item_list(orig_occupant, x, false);
 +                              -- TODO: new status code to inform client this was the multi-session it left?
 +                              pr:add_child(x);
 +                              self:route_stanza(pr);
 +                      end
 +
 +                      if orig_occupant == nil then
 +                              if is_first_dest_session then
 +                                      module:fire_event("muc-occupant-joined", {room = self; nick = dest_occupant.nick; occupant = dest_occupant;});
                                end
 -                              stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
 -                      else -- message
 -                              stanza:tag("x", { xmlns = "http://jabber.org/protocol/muc#user" }):up();
 -                              stanza.attr.from = current_nick;
 -                              for jid in pairs(o_data.sessions) do
 -                                      stanza.attr.to = jid;
 -                                      self:_route_stanza(stanza);
 +                              module:fire_event("muc-occupant-session-new", {room = self; nick = dest_occupant.nick; occupant = dest_occupant; stanza = stanza; jid = real_jid;});
 +                      end
 +              end
 +      elseif type ~= 'result' then -- bad type
 +              if type ~= 'visible' and type ~= 'invisible' then -- COMPAT ejabberd can broadcast or forward XEP-0018 presences
 +                      origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
 +              end
 +      end
 +      return true;
 +end
 +
 +function room_mt:handle_iq_to_occupant(origin, stanza)
 +      local from, to = stanza.attr.from, stanza.attr.to;
 +      local type = stanza.attr.type;
 +      local id = stanza.attr.id;
 +      local occupant = self:get_occupant_by_nick(to);
 +      if (type == "error" or type == "result") then
 +              do -- deconstruct_stanza_id
 +                      if not occupant then return nil; end
-                       local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(.+)%z(.*)%z(.+)$");
++                      local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(%Z+)%z(%Z*)%z(.+)$");
 +                      if not(from == from_jid or from == jid_bare(from_jid)) then return nil; end
 +                      local from_occupant_jid = self:get_occupant_jid(from_jid);
 +                      if from_occupant_jid == nil then return nil; end
 +                      local session_jid
 +                      for to_jid in occupant:each_session() do
 +                              if md5(to_jid) == to_jid_hash then
 +                                      session_jid = to_jid;
 +                                      break;
                                end
 -                              stanza.attr.from, stanza.attr.to = from, to;
                        end
 -              elseif type ~= "error" and type ~= "result" then -- recipient not in room
 +                      if session_jid == nil then return nil; end
 +                      stanza.attr.from, stanza.attr.to, stanza.attr.id = from_occupant_jid, session_jid, id;
 +              end
 +              log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to);
 +              self:route_stanza(stanza);
 +              stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
 +              return true;
 +      else -- Type is "get" or "set"
 +              local current_nick = self:get_occupant_jid(from);
 +              if not current_nick then
 +                      origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
 +                      return true;
 +              end
 +              if not occupant then -- recipient not in room
                        origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
 +                      return true;
 +              end
 +              do -- construct_stanza_id
 +                      stanza.attr.id = base64.encode(occupant.jid.."\0"..stanza.attr.id.."\0"..md5(from));
                end
 +              stanza.attr.from, stanza.attr.to = current_nick, occupant.jid;
 +              log("debug", "%s sent private iq stanza to %s (%s)", from, to, occupant.jid);
 +              if stanza.tags[1].attr.xmlns == 'vcard-temp' then
 +                      stanza.attr.to = jid_bare(stanza.attr.to);
 +              end
 +              self:route_stanza(stanza);
 +              stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
 +              return true;
        end
  end