use of org.dom4j.Element in project Openfire by igniterealtime.
the class LocalMUCRoom method changeOccupantRole.
/**
* Updates the presence of the given user with the new role information. Do nothing if the given
* jid is not present in the room.
*
* @param jid the full jid of the user to update his/her role.
* @param newRole the new role for the JID.
* @return the updated presence of the user or null if none.
* @throws NotAllowedException If trying to change the moderator role to an owner or an admin.
*/
private Presence changeOccupantRole(JID jid, MUCRole.Role newRole) throws NotAllowedException {
// Try looking the role in the bare JID list
MUCRole role = occupantsByFullJID.get(jid);
// }
if (role != null) {
if (role.isLocal()) {
// Update the presence with the new role
role.setRole(newRole);
// Notify the other cluster nodes to update the occupant
CacheFactory.doClusterTask(new UpdateOccupant(this, role));
// Prepare a new presence to be sent to all the room occupants
return role.getPresence().createCopy();
} else {
// Ask the cluster node hosting the occupant to make the changes. Note that if the change
// is not allowed a NotAllowedException will be thrown
Element element = (Element) CacheFactory.doSynchronousClusterTask(new UpdateOccupantRequest(this, role.getNickname(), null, newRole), role.getNodeID().toByteArray());
if (element != null) {
// Prepare a new presence to be sent to all the room occupants
return new Presence(element, true);
} else {
throw new NotAllowedException();
}
}
}
return null;
}
use of org.dom4j.Element in project Openfire by igniterealtime.
the class LocalMUCUser method process.
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) {
MUCRole role = roles.get(group);
Element mucInfo = packet.getChildElement("x", "http://jabber.org/protocol/muc");
if (role == null || mucInfo != null) {
// Alternative is that mucInfo is not null, in which case the client thinks it isn't in the room, so we should join anyway.
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
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());
// 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 | ConflictException e) {
sendErrorPacket(packet, PacketError.Condition.conflict);
} catch (RoomLockedException e) {
// If a user attempts to enter a room while it is "locked" (i.e., before the room creator provides an initial configuration and therefore before the room officially exists), the service MUST refuse entry and return an <item-not-found/> error to the user
sendErrorPacket(packet, PacketError.Condition.item_not_found);
} catch (ForbiddenException e) {
sendErrorPacket(packet, PacketError.Condition.forbidden);
} catch (RegistrationRequiredException e) {
sendErrorPacket(packet, PacketError.Condition.registration_required);
} 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
// http://xmpp.org/extensions/xep-0045.html#enter
// If the user does not specify a room nickname (note the bare JID on the 'from' address in the following example), the service MUST return a <jid-malformed/> error
sendErrorPacket(packet, PacketError.Condition.jid_malformed);
}
// TODO: send error message to user (can't send packets to group you haven't
// joined)
}
} else {
// In other words, another user already has this nickname
if (!role.getUserAddress().equals(packet.getFrom())) {
sendErrorPacket(packet, PacketError.Condition.conflict);
} else {
if (Presence.Type.unavailable == packet.getType()) {
try {
// TODO Consider that different nodes can be creating and processing this presence at the same time (when remote node went down)
removeRole(group);
role.getChatRoom().leaveRoom(role);
} catch (Exception e) {
Log.error(e.getMessage(), 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.getChatRoom().presenceUpdated(role, packet);
} else {
// Check if occupants are allowed to change their nicknames
if (!role.getChatRoom().canChangeNickname()) {
sendErrorPacket(packet, PacketError.Condition.not_acceptable);
} else // Answer a conflic error if the new nickname is taken
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.getChatRoom().nicknameChanged(role, packet, oldNick, resource);
}
}
} catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
}
}
}
}
} else {
// Packets to the groupchat server. This should not occur (should be handled by MultiUserChatServiceImpl instead)
Log.warn(LocaleUtils.getLocalizedString("muc.error.not-supported") + " " + packet.toString());
}
}
use of org.dom4j.Element in project Openfire by igniterealtime.
the class RemoteMUCRole method readExternal.
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
serviceDomain = ExternalizableUtil.getInstance().readSafeUTF(in);
presence = new Presence((Element) ExternalizableUtil.getInstance().readSerializable(in), true);
role = Role.values()[ExternalizableUtil.getInstance().readInt(in)];
affiliation = Affiliation.values()[ExternalizableUtil.getInstance().readInt(in)];
nickname = ExternalizableUtil.getInstance().readSafeUTF(in);
voiceOnly = ExternalizableUtil.getInstance().readBoolean(in);
roleAddress = (JID) ExternalizableUtil.getInstance().readSerializable(in);
userAddress = (JID) ExternalizableUtil.getInstance().readSerializable(in);
nodeID = NodeID.getInstance(ExternalizableUtil.getInstance().readByteArray(in));
}
use of org.dom4j.Element in project Openfire by igniterealtime.
the class WorkgroupCompatibleClient method notifyQueueDepartued.
public void notifyQueueDepartued(JID sender, JID receiver, UserRequest request, Request.CancelType type) {
Message message = new Message();
if (sender != null) {
message.setFrom(sender);
}
message.setTo(receiver);
Element depart = message.getElement().addElement("depart-queue", "http://jabber.org/protocol/workgroup");
// Add an element that explains the reason why the user is being removed from the queue
depart.addElement(type.getDescription());
// Send the notification
request.getWorkgroup().send(message);
}
use of org.dom4j.Element in project Openfire by igniterealtime.
the class WorkgroupCompatibleClient method notifyQueueStatus.
public void notifyQueueStatus(JID sender, JID receiver, UserRequest request, boolean isPolling) {
Packet statusPacket;
if (isPolling) {
statusPacket = new IQ();
} else {
statusPacket = new Message();
}
statusPacket.setFrom(sender);
statusPacket.setTo(receiver);
// Add Queue Status Packet to IQ
Element status = statusPacket.getElement().addElement("queue-status", "http://jabber.org/protocol/workgroup");
// Add Time Element
Element time = status.addElement("time");
time.setText(Integer.toString(request.getTimeStatus()));
// Add Position Element
Element position = status.addElement("position");
position.setText(Integer.toString(request.getPosition() + 1));
status.add(request.getSessionElement());
// Send the queue status
request.getWorkgroup().send(statusPacket);
}
Aggregations