Comparison

plugins/muc/muc.lib.lua @ 6097:538cdc3d8225

plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant` It is the only place they were used; and I left the old function names in as comments. One reason for doing this was to reduce accesses to _occupants; which may be in a database in future revisions
author daurnimator <quae@daurnimator.com>
date Fri, 21 Feb 2014 17:17:01 -0500
parent 6096:84f9123637d4
child 6098:1d7e5d091980
comparison
equal deleted inserted replaced
6096:84f9123637d4 6097:538cdc3d8225
376 end 376 end
377 end 377 end
378 378
379 function room_mt:get_whois() 379 function room_mt:get_whois()
380 return self._data.whois; 380 return self._data.whois;
381 end
382
383 local function construct_stanza_id(room, stanza)
384 local from_jid, to_nick = stanza.attr.from, stanza.attr.to;
385 local from_nick = room._jid_nick[from_jid];
386 local occupant = room._occupants[to_nick];
387 local to_jid = occupant.jid;
388
389 return from_nick, to_jid, base64.encode(to_jid.."\0"..stanza.attr.id.."\0"..md5(from_jid));
390 end
391 local function deconstruct_stanza_id(room, stanza)
392 local from_jid_possiblybare, to_nick = stanza.attr.from, stanza.attr.to;
393 local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(.+)%z(.*)%z(.+)$");
394 local from_nick = room._jid_nick[from_jid];
395
396 if not(from_nick) then return; end
397 if not(from_jid_possiblybare == from_jid or from_jid_possiblybare == jid_bare(from_jid)) then return; end
398
399 local occupant = room._occupants[to_nick];
400 for to_jid in pairs(occupant and occupant.sessions or {}) do
401 if md5(to_jid) == to_jid_hash then
402 return from_nick, to_jid, id;
403 end
404 end
405 end 381 end
406 382
407 function room_mt:handle_presence_error_to_occupant(origin, stanza) 383 function room_mt:handle_presence_error_to_occupant(origin, stanza)
408 local current_nick = self._jid_nick[stanza.attr.from]; 384 local current_nick = self._jid_nick[stanza.attr.from];
409 if not current_nick then 385 if not current_nick then
605 581
606 function room_mt:handle_iq_to_occupant(origin, stanza) 582 function room_mt:handle_iq_to_occupant(origin, stanza)
607 local from, to = stanza.attr.from, stanza.attr.to; 583 local from, to = stanza.attr.from, stanza.attr.to;
608 local type = stanza.attr.type; 584 local type = stanza.attr.type;
609 local id = stanza.attr.id; 585 local id = stanza.attr.id;
586 local current_nick = self._jid_nick[from];
587 local o_data = self._occupants[to];
610 if (type == "error" or type == "result") then 588 if (type == "error" or type == "result") then
611 stanza.attr.from, stanza.attr.to, stanza.attr.id = deconstruct_stanza_id(self, stanza); 589 do -- deconstruct_stanza_id
590 if not current_nick or not o_data then return nil; end
591 local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(.+)%z(.*)%z(.+)$");
592 if not(from == from_jid or from == jid_bare(from_jid)) then return nil; end
593 local session_jid
594 for to_jid in pairs(o_data.sessions) do
595 if md5(to_jid) == to_jid_hash then
596 session_jid = to_jid;
597 break;
598 end
599 end
600 if session_jid == nil then return nil; end
601 stanza.attr.from, stanza.attr.to, stanza.attr.id = current_nick, session_jid, id
602 end
612 log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to); 603 log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to);
613 if stanza.attr.id then 604 self:_route_stanza(stanza);
614 self:_route_stanza(stanza);
615 end
616 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id; 605 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
617 return true; 606 return true;
618 else -- Type is "get" or "set" 607 else -- Type is "get" or "set"
619 local current_nick = self._jid_nick[from];
620 if not current_nick then 608 if not current_nick then
621 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); 609 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
622 return true; 610 return true;
623 end 611 end
624 local o_data = self._occupants[to];
625 if not o_data then -- recipient not in room 612 if not o_data then -- recipient not in room
626 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); 613 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
627 return true; 614 return true;
628 end 615 end
629 stanza.attr.from, stanza.attr.to, stanza.attr.id = construct_stanza_id(self, stanza); 616 do -- construct_stanza_id
617 stanza.attr.id = base64.encode(to_jid.."\0"..stanza.attr.id.."\0"..md5(from));
618 end
619 stanza.attr.from, stanza.attr.to = current_nick, o_data.jid;
630 log("debug", "%s sent private iq stanza to %s (%s)", from, to, o_data.jid); 620 log("debug", "%s sent private iq stanza to %s (%s)", from, to, o_data.jid);
631 if stanza.tags[1].attr.xmlns == 'vcard-temp' then 621 if stanza.tags[1].attr.xmlns == 'vcard-temp' then
632 stanza.attr.to = jid_bare(stanza.attr.to); 622 stanza.attr.to = jid_bare(stanza.attr.to);
633 end 623 end
634 if stanza.attr.id then 624 self:_route_stanza(stanza);
635 self:_route_stanza(stanza);
636 end
637 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id; 625 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
638 return true; 626 return true;
639 end 627 end
640 end 628 end
641 629
1025 end 1013 end
1026 stanza.attr.from = from; 1014 stanza.attr.from = from;
1027 end 1015 end
1028 end 1016 end
1029 1017
1030
1031 function room_mt:handle_kickable_to_room(origin, stanza) 1018 function room_mt:handle_kickable_to_room(origin, stanza)
1032 local current_nick = self._jid_nick[stanza.attr.from]; 1019 local current_nick = self._jid_nick[stanza.attr.from];
1033 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid); 1020 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid);
1034 self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); -- send unavailable 1021 self:handle_to_occupant(origin, build_unavailable_presence_from_error(stanza)); -- send unavailable
1035 end 1022 end