mucuserimpl.java
来自「基于Jabber协议的即时消息服务器」· Java 代码 · 共 542 行 · 第 1/2 页
JAVA
542 行
// An occupant has declined an invitation
Element info = userInfo.element("decline");
room.sendInvitationRejection(new JID(info.attributeValue("to")),
info.elementTextTrim("reason"), packet.getFrom());
}
else {
sendErrorPacket(packet, PacketError.Condition.bad_request);
}
}
else {
sendErrorPacket(packet, PacketError.Condition.bad_request);
}
}
}
catch (ForbiddenException e) {
sendErrorPacket(packet, PacketError.Condition.forbidden);
}
catch (NotFoundException e) {
sendErrorPacket(packet, PacketError.Condition.recipient_unavailable);
}
catch (ConflictException e) {
sendErrorPacket(packet, PacketError.Condition.conflict);
}
}
}
}
}
public void process(IQ packet) {
// Ignore IQs of type ERROR or RESULT sent to a room
if (IQ.Type.error == packet.getType()) {
return;
}
lastPacketTime = System.currentTimeMillis();
JID recipient = packet.getTo();
String group = recipient.getNode();
if (group == null) {
// Ignore packets to the groupchat server
// In the future, we'll need to support TYPE_IQ queries to the server for MUC
Log.info(LocaleUtils.getLocalizedString("muc.error.not-supported") + " "
+ packet.toString());
}
else {
MUCRole role = roles.get(group);
if (role == null) {
// TODO: send error message to user (can't send packets to group you haven't
// joined)
}
else if (IQ.Type.result == packet.getType()) {
// Only process IQ result packet if it's a private packet sent to another
// room occupant
if (packet.getTo().getResource() != null) {
try {
// User is sending an IQ result packet to another room occupant
role.getChatRoom().sendPrivatePacket(packet, role);
}
catch (NotFoundException e) {
// Do nothing. No error will be sent to the sender of the IQ result packet
}
}
}
else {
// Check and reject conflicting packets with conflicting roles
// In other words, another user already has this nickname
if (!role.getChatUser().getAddress().equals(packet.getFrom())) {
sendErrorPacket(packet, PacketError.Condition.conflict);
}
else {
try {
Element query = packet.getElement().element("query");
if (query != null &&
"http://jabber.org/protocol/muc#owner".equals(query.getNamespaceURI())) {
role.getChatRoom().getIQOwnerHandler().handleIQ(packet, role);
}
else if (query != null &&
"http://jabber.org/protocol/muc#admin".equals(query.getNamespaceURI())) {
role.getChatRoom().getIQAdminHandler().handleIQ(packet, role);
}
else {
if (packet.getTo().getResource() != null) {
// User is sending an IQ packet to another room occupant
role.getChatRoom().sendPrivatePacket(packet, role);
}
else {
sendErrorPacket(packet, PacketError.Condition.bad_request);
}
}
}
catch (ForbiddenException e) {
sendErrorPacket(packet, PacketError.Condition.forbidden);
}
catch (NotFoundException e) {
sendErrorPacket(packet, PacketError.Condition.recipient_unavailable);
}
catch (ConflictException e) {
sendErrorPacket(packet, PacketError.Condition.conflict);
}
catch (NotAllowedException e) {
sendErrorPacket(packet, PacketError.Condition.not_allowed);
}
catch (Exception e) {
sendErrorPacket(packet, PacketError.Condition.internal_server_error);
}
}
}
}
}
public void process(Presence packet) {
// Ignore presences of type ERROR sent to a room
if (Presence.Type.error == packet.getType()) {
return;
}
lastPacketTime = System.currentTimeMillis();
JID recipient = packet.getTo();
String group = recipient.getNode();
if (group == null) {
if (Presence.Type.unavailable == packet.getType()) {
server.removeUser(packet.getFrom());
}
}
else {
MUCRole role = roles.get(group);
if (role == null) {
// If we're not already in a room, we either are joining it or it's not
// properly addressed and we drop it silently
if (recipient.getResource() != null
&& recipient.getResource().trim().length() > 0) {
if (packet.isAvailable()) {
try {
// Get or create the room
MUCRoom room = server.getChatRoom(group, packet.getFrom());
// User must support MUC in order to create a room
Element mucInfo = packet.getChildElement("x",
"http://jabber.org/protocol/muc");
HistoryRequest historyRequest = null;
String password = null;
// Check for password & requested history if client supports MUC
if (mucInfo != null) {
password = mucInfo.elementTextTrim("password");
if (mucInfo.element("history") != null) {
historyRequest = new HistoryRequest(mucInfo);
}
}
// The user joins the room
role = room.joinRoom(recipient.getResource().trim(),
password,
historyRequest,
this,
packet.createCopy());
// If the client that created the room is non-MUC compliant then
// unlock the room thus creating an "instant" room
if (mucInfo == null && room.isLocked() && !room.isManuallyLocked()) {
room.unlock(role);
}
}
catch (UnauthorizedException e) {
sendErrorPacket(packet, PacketError.Condition.not_authorized);
}
catch (ServiceUnavailableException e) {
sendErrorPacket(packet, PacketError.Condition.service_unavailable);
}
catch (UserAlreadyExistsException e) {
sendErrorPacket(packet, PacketError.Condition.conflict);
}
catch (RoomLockedException e) {
sendErrorPacket(packet, PacketError.Condition.recipient_unavailable);
}
catch (ForbiddenException e) {
sendErrorPacket(packet, PacketError.Condition.forbidden);
}
catch (RegistrationRequiredException e) {
sendErrorPacket(packet, PacketError.Condition.registration_required);
}
catch (ConflictException e) {
sendErrorPacket(packet, PacketError.Condition.conflict);
}
catch (NotAcceptableException e) {
sendErrorPacket(packet, PacketError.Condition.not_acceptable);
}
catch (NotAllowedException e) {
sendErrorPacket(packet, PacketError.Condition.not_allowed);
}
}
else {
// TODO: send error message to user (can't send presence to group you
// haven't joined)
}
}
else {
if (packet.isAvailable()) {
// A resource is required in order to join a room
sendErrorPacket(packet, PacketError.Condition.bad_request);
}
// TODO: send error message to user (can't send packets to group you haven't
// joined)
}
}
else {
// Check and reject conflicting packets with conflicting roles
// In other words, another user already has this nickname
if (!role.getChatUser().getAddress().equals(packet.getFrom())) {
sendErrorPacket(packet, PacketError.Condition.conflict);
}
else {
if (Presence.Type.unavailable == packet.getType()) {
try {
removeRole(group);
role.getChatRoom().leaveRoom(role.getNickname());
}
catch (UserNotFoundException e) {
// Do nothing since the users has already left the room
}
catch (Exception e) {
Log.error(e);
}
}
else {
try {
String resource = (recipient.getResource() == null
|| recipient.getResource().trim().length() == 0 ? null
: recipient.getResource().trim());
if (resource == null
|| role.getNickname().equalsIgnoreCase(resource)) {
// Occupant has changed his availability status
role.setPresence(packet.createCopy());
role.getChatRoom().send(role.getPresence().createCopy());
}
else {
// Occupant has changed his nickname. Send two presences
// to each room occupant
// Check if occupants are allowed to change their nicknames
if (!role.getChatRoom().canChangeNickname()) {
sendErrorPacket(packet, PacketError.Condition.not_acceptable);
}
// Answer a conflic error if the new nickname is taken
else if (role.getChatRoom().hasOccupant(resource)) {
sendErrorPacket(packet, PacketError.Condition.conflict);
}
else {
// Send "unavailable" presence for the old nickname
Presence presence = role.getPresence().createCopy();
// Switch the presence to OFFLINE
presence.setType(Presence.Type.unavailable);
presence.setStatus(null);
// Add the new nickname and status 303 as properties
Element frag = presence.getChildElement("x",
"http://jabber.org/protocol/muc#user");
frag.element("item").addAttribute("nick", resource);
frag.addElement("status").addAttribute("code", "303");
role.getChatRoom().send(presence);
// Send availability presence for the new nickname
String oldNick = role.getNickname();
role.setPresence(packet.createCopy());
role.changeNickname(resource);
role.getChatRoom().nicknameChanged(oldNick, resource);
role.getChatRoom().send(role.getPresence().createCopy());
}
}
}
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?