use of org.xmpp.packet.Message in project Openfire by igniterealtime.
the class MUCRoomHistory method addMessage.
public void addMessage(Message packet) {
boolean isSubjectChangeRequest = isSubjectChangeRequest(packet);
JID fromJID = packet.getFrom();
// unless the message is changing the room's subject
if (!isSubjectChangeRequest && (fromJID == null || fromJID.toString().length() == 0 || fromJID.equals(room.getRole().getRoleAddress()))) {
return;
}
// Do not store regular messages if there is no message strategy (keep subject change requests)
if (!isSubjectChangeRequest && !historyStrategy.isHistoryEnabled()) {
return;
}
// Ignore empty messages (no subject AND no body)
if (!isSubjectChangeRequest && (packet.getBody() == null || packet.getBody().trim().length() == 0)) {
return;
}
Message packetToAdd = packet.createCopy();
// Check if the room has changed its configuration
if (isNonAnonymousRoom != room.canAnyoneDiscoverJID()) {
isNonAnonymousRoom = room.canAnyoneDiscoverJID();
// TODO Make this update in a separate thread
for (Iterator<Message> it = getMessageHistory(); it.hasNext(); ) {
Message message = it.next();
Element delayElement = message.getChildElement("delay", "urn:xmpp:delay");
if (room.canAnyoneDiscoverJID()) {
// Set the Full JID as the "from" attribute
try {
MUCRole role = room.getOccupant(message.getFrom().getResource());
delayElement.addAttribute("from", role.getUserAddress().toString());
} catch (UserNotFoundException e) {
// Ignore.
}
} else {
// Set the Room JID as the "from" attribute
delayElement.addAttribute("from", message.getFrom().toString());
}
}
}
// Add the delay information to the message
Element delayInformation = packetToAdd.addChildElement("delay", "urn:xmpp:delay");
Date current = new Date();
delayInformation.addAttribute("stamp", XMPPDateTimeFormat.format(current));
if (room.canAnyoneDiscoverJID()) {
// Set the Full JID as the "from" attribute
try {
MUCRole role = room.getOccupant(packet.getFrom().getResource());
delayInformation.addAttribute("from", role.getUserAddress().toString());
} catch (UserNotFoundException e) {
// Ignore.
}
} else {
// Set the Room JID as the "from" attribute
delayInformation.addAttribute("from", packet.getFrom().toString());
}
historyStrategy.addMessage(packetToAdd);
}
use of org.xmpp.packet.Message in project Openfire by igniterealtime.
the class LocalOutgoingServerSession method returnErrorToSender.
private void returnErrorToSender(Packet packet) {
RoutingTable routingTable = XMPPServer.getInstance().getRoutingTable();
if (packet.getError() != null) {
Log.debug("Possible double bounce: " + packet.toXML());
}
try {
if (packet instanceof IQ) {
if (((IQ) packet).isResponse()) {
Log.debug("XMPP specs forbid us to respond with an IQ error to: " + packet.toXML());
return;
}
IQ reply = new IQ();
reply.setID(packet.getID());
reply.setTo(packet.getFrom());
reply.setFrom(packet.getTo());
reply.setChildElement(((IQ) packet).getChildElement().createCopy());
reply.setType(IQ.Type.error);
reply.setError(PacketError.Condition.remote_server_not_found);
routingTable.routePacket(reply.getTo(), reply, true);
} else if (packet instanceof Presence) {
if (((Presence) packet).getType() == Presence.Type.error) {
Log.debug("Double-bounce of presence: " + packet.toXML());
return;
}
Presence reply = new Presence();
reply.setID(packet.getID());
reply.setTo(packet.getFrom());
reply.setFrom(packet.getTo());
reply.setType(Presence.Type.error);
reply.setError(PacketError.Condition.remote_server_not_found);
routingTable.routePacket(reply.getTo(), reply, true);
} else if (packet instanceof Message) {
if (((Message) packet).getType() == Message.Type.error) {
Log.debug("Double-bounce of message: " + packet.toXML());
return;
}
Message reply = new Message();
reply.setID(packet.getID());
reply.setTo(packet.getFrom());
reply.setFrom(packet.getTo());
reply.setType(Message.Type.error);
reply.setThread(((Message) packet).getThread());
reply.setError(PacketError.Condition.remote_server_not_found);
routingTable.routePacket(reply.getTo(), reply, true);
}
} catch (Exception e) {
Log.error("Error returning error to sender. Original packet: " + packet, e);
}
}
use of org.xmpp.packet.Message in project Openfire by igniterealtime.
the class Workgroup method createGroupChatRoom.
void createGroupChatRoom() {
String roomJID = getGroupChatRoomName() + "/workgroup";
// Create the room by joining it. The workgroup will be the owner of the room and will
// invite the Agent and the user to join the room
JoinRoom joinRoom = new JoinRoom(getFullJID().toString(), roomJID);
send(joinRoom);
// Configure the newly created room
Map<String, Collection<String>> fields = new HashMap<String, Collection<String>>();
// Make a non-public room
List<String> values = new ArrayList<String>();
values.add("0");
fields.put("muc#roomconfig_publicroom", values);
// Set the room name
values = new ArrayList<String>();
values.add("Workgroup " + getJID().getNode() + " Chat Room");
fields.put("muc#roomconfig_roomname", values);
// Set the room description
values = new ArrayList<String>();
values.add("Workgroup Chat Room");
fields.put("muc#roomconfig_roomdesc", values);
// Set the max number of occupants to unlimited
values = new ArrayList<String>();
values.add("0");
fields.put("muc#roomconfig_maxusers", values);
// Set that anyone can change the room subject
values = new ArrayList<String>();
values.add("1");
fields.put("muc#roomconfig_changesubject", values);
// Make the room persistent
values = new ArrayList<String>();
values.add("1");
fields.put("muc#roomconfig_persistentroom", values);
// Make the room not moderated
values = new ArrayList<String>();
values.add("0");
fields.put("muc#roomconfig_moderatedroom", values);
// Make the room not members-only
values = new ArrayList<String>();
values.add("0");
fields.put("muc#roomconfig_membersonly", values);
// Set that anyone can send invitations
values = new ArrayList<String>();
values.add("1");
fields.put("muc#roomconfig_allowinvites", values);
// Make the room not password protected
values = new ArrayList<String>();
values.add("0");
fields.put("muc#roomconfig_passwordprotectedroom", values);
// Enable the log for the room
values = new ArrayList<String>();
values.add("1");
fields.put("muc#roomconfig_enablelogging", values);
// Set that only moderators can see the occupants' JID
values = new ArrayList<String>();
values.add("moderators");
fields.put("muc#roomconfig_whois", values);
// Only broadcast presences of participants and visitors
values = new ArrayList<String>();
values.add("moderator");
values.add("participant");
values.add("visitor");
fields.put("muc#roomconfig_presencebroadcast", values);
RoomConfiguration conf = new RoomConfiguration(fields);
conf.setTo(getGroupChatRoomName());
conf.setFrom(getFullJID());
send(conf);
// Change the subject of the room by sending a new message
Message message = new Message();
message.setType(Message.Type.groupchat);
message.setSubject("This is a private discussion room for members of this workgroup.");
message.setFrom(getFullJID());
message.setTo(getGroupChatRoomName());
send(message);
// Leave Chat Room
LeaveRoom leaveRoom = new LeaveRoom(getFullJID().toString(), roomJID);
send(leaveRoom);
}
use of org.xmpp.packet.Message in project Openfire by igniterealtime.
the class Workgroup method roomActivity.
// ###############################################################################
// MUC related packets
// ###############################################################################
/**
* Notification message indicating that there has been new activity in a room. This implies
* that we need to update the conversation transcript of the group chat room and possibly
* update the number of occupants within the room.<p>
* <p/>
* If only the workgroup is present in the room then leave the room (i.e. destroying room) and
* proceed to save the room conversation transcript to the database.<p>
*
* @param packet the packet that was sent to the group chat room.
*/
private void roomActivity(Packet packet) {
// Skip packet sent from this workgroup in the room
if (packet.getFrom().toBareJID().equals(getGroupChatRoomName())) {
return;
}
RoomInterceptorManager interceptorManager = RoomInterceptorManager.getInstance();
String roomID = packet.getFrom().getNode();
// Get the sessionID
String sessionID = packet.getFrom().getNode();
synchronized (sessionID.intern()) {
if (packet instanceof Presence) {
Presence presence = (Presence) packet;
if (Presence.Type.error == presence.getType()) {
// A configuration must be wrong (eg. workgroup is not allowed to create rooms).
// Log the error presence
String warnMessage = "Possible server misconfiguration. Received error " + "presence:" + presence.toXML();
Log.warn(warnMessage);
return;
}
// Get the JID of the presence's user
Element mucUser = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
// Skip this presence if no extended info was included in the presence
if (mucUser == null) {
return;
}
Element item = mucUser.element("item");
// Skip this presence if no item was included in the presence
if (item == null) {
return;
}
// Skip this presence if it's the presence of this workgroup in the room
if (workgroupName.equals(packet.getFrom().getResource())) {
return;
}
JID presenceFullJID = new JID(item.attributeValue("jid"));
String presenceJID = presenceFullJID.toBareJID();
// Invoke the room interceptor before processing the presence
interceptorManager.invokeInterceptors(getJID().toBareJID(), packet, false, false);
// Get the userID associated to this sessionID
UserRequest initialRequest = requests.get(sessionID);
// Add the new presence to the list of sent packets
Map<Packet, java.util.Date> messageList = transcripts.get(roomID);
if (messageList == null) {
messageList = new LinkedHashMap<Packet, java.util.Date>();
transcripts.put(roomID, messageList);
// Trigger the event that a chat support has started
WorkgroupEventDispatcher.chatSupportStarted(this, sessionID);
}
messageList.put(packet.createCopy(), new java.util.Date());
// Update the number of occupants in the room.
boolean occupantAdded = false;
Set<String> set = occupantsCounter.get(roomID);
if (set == null) {
set = new HashSet<String>();
occupantsCounter.put(roomID, set);
}
if (presence.isAvailable()) {
occupantAdded = set.add(presenceJID);
} else {
String xpath = "/presence/*[name()='x']/*[name()='status']";
Element status = (Element) presence.getElement().selectSingleNode(xpath);
if (status == null || !"303".equals(status.attributeValue("code"))) {
// Remove the occupant unless the occupant is changing his nickname
set.remove(presenceJID);
}
}
// If the presence belongs to an Agent then create/update a track
// Look for an agent whose JID matches the presence's JID
String agentJID = null;
for (Agent agent : getAgents()) {
if (agent.getAgentJID().toBareJID().equals(presenceJID)) {
agentJID = agent.getAgentJID().toBareJID();
}
}
if (agentJID != null) {
AgentSession agentSession;
// Update the current chats that the agent is having
try {
agentSession = agentManager.getAgentSession(presenceFullJID);
if (agentSession != null) {
if (presence.isAvailable()) {
if (occupantAdded) {
agentSession.addChatInfo(this, sessionID, initialRequest, new java.util.Date());
// Trigger the event that an agent has joined a chat session
WorkgroupEventDispatcher.agentJoinedChatSupport(this, sessionID, agentSession);
}
} else {
agentSession.removeChatInfo(this, sessionID);
// Trigger the event that an agent has left a chat session
WorkgroupEventDispatcher.agentLeftChatSupport(this, sessionID, agentSession);
}
}
} catch (AgentNotFoundException e) {
// Do nothing since the AgentSession was not found
}
if (presence.isAvailable()) {
if (occupantAdded) {
// Store in the DB that an agent has joined a room
DbWorkgroup.updateJoinedSession(sessionID, agentJID, true);
}
} else {
// Store in the DB that an agent has left a room
DbWorkgroup.updateJoinedSession(sessionID, agentJID, false);
}
} else {
if (occupantAdded) {
// Notify the request that the user has joined a support session
initialRequest.supportStarted(roomID);
}
}
if (occupantAdded) {
initialRequest.userJoinedRoom(new JID(packet.getFrom().toBareJID()), presenceFullJID);
}
// If just the user has left the room, just persist the transcript
boolean isAgent = false;
try {
isAgent = agentManager.getAgentSession(presenceFullJID) != null;
} catch (AgentNotFoundException e) {
// Ignore.
}
if (!((Presence) packet).isAvailable() && !isAgent) {
// Build the XML for the transcript
Map<Packet, java.util.Date> map = transcripts.get(roomID);
StringBuilder buf = new StringBuilder();
buf.append("<transcript>");
for (Packet p : map.keySet()) {
java.util.Date date = map.get(p);
// Add the delay information
if (p instanceof Message) {
Message storedMessage = (Message) p;
Element delay = storedMessage.addChildElement("x", "jabber:x:delay");
delay.addAttribute("stamp", UTC_FORMAT.format(date));
if (ModelUtil.hasLength(storedMessage.getBody())) {
buf.append(p.toXML());
}
} else {
Presence storedPresence = (Presence) p;
Element delay = storedPresence.addChildElement("x", "jabber:x:delay");
delay.addAttribute("stamp", UTC_FORMAT.format(date));
buf.append(p.toXML());
}
// Append an XML representation of the packet to the string buffer
}
buf.append("</transcript>");
// Save the transcript (in XML) to the DB
DbWorkgroup.updateTranscript(sessionID, buf.toString(), new java.util.Date());
}
// the DB and destroy the room
if (!((Presence) packet).isAvailable() && set.isEmpty()) {
// Delete the counter of occupants for this room
occupantsCounter.remove(roomID);
initialRequest = requests.remove(sessionID);
if (initialRequest != null && initialRequest.hasJoinedRoom()) {
// Notify the request that the support session has finished
initialRequest.supportEnded();
}
// Build the XML for the transcript
Map<Packet, java.util.Date> map = transcripts.get(roomID);
StringBuilder buf = new StringBuilder();
buf.append("<transcript>");
for (Packet p : map.keySet()) {
java.util.Date date = map.get(p);
// Add the delay information
if (p instanceof Message) {
Message storedMessage = (Message) p;
Element delay = storedMessage.addChildElement("x", "jabber:x:delay");
delay.addAttribute("stamp", UTC_FORMAT.format(date));
if (ModelUtil.hasLength(storedMessage.getBody())) {
buf.append(p.toXML());
}
} else {
Presence storedPresence = (Presence) p;
Element delay = storedPresence.addChildElement("x", "jabber:x:delay");
delay.addAttribute("stamp", UTC_FORMAT.format(date));
buf.append(p.toXML());
}
// Append an XML representation of the packet to the string buffer
}
buf.append("</transcript>");
// Save the transcript (in XML) to the DB
//DbWorkgroup.updateTranscript(sessionID, buf.toString(), new java.util.Date());
// Leave Chat Room (the room will be destroyed)
String roomJID = packet.getFrom().toString() + "/" + getJID().getNode();
LeaveRoom leaveRoom = new LeaveRoom(getFullJID().toString(), roomJID);
send(leaveRoom);
// Remove the transcript information of this room since the room no
// longer exists
transcripts.remove(roomID);
// Trigger the event that a chat support has finished
WorkgroupEventDispatcher.chatSupportFinished(this, sessionID);
}
// Invoke the room interceptor after the presence has been processed
interceptorManager.invokeInterceptors(getJID().toBareJID(), packet, false, true);
} else if (packet instanceof Message) {
// transcript to include things like "room locked"
if (packet.getFrom().getResource() != null) {
// Invoke the room interceptor before processing the presence
interceptorManager.invokeInterceptors(getJID().toBareJID(), packet, false, false);
// Add the new message to the list of sent packets
Map<Packet, java.util.Date> messageList = transcripts.get(roomID);
if (messageList == null) {
messageList = new LinkedHashMap<Packet, java.util.Date>();
transcripts.put(roomID, messageList);
}
messageList.put(packet.createCopy(), new java.util.Date());
// Invoke the room interceptor after the presence has been processed
interceptorManager.invokeInterceptors(getJID().toBareJID(), packet, false, true);
}
}
}
}
use of org.xmpp.packet.Message in project Openfire by igniterealtime.
the class WorkgroupManager method processPacket.
public void processPacket(Packet packet) {
try {
// Check if the packet is a disco request
if (packet instanceof IQ) {
if (process((IQ) packet)) {
return;
}
}
// was found then let the workgroup process the packet
try {
Workgroup workgroup = getWorkgroup(packet.getTo());
workgroup.process(packet);
} catch (UserNotFoundException e) {
// above situations
if (packet instanceof Presence) {
if (((Presence) packet).getType() == Presence.Type.error) {
// Skip Presence packets of type error
return;
}
Presence reply = new Presence();
reply.setID(packet.getID());
reply.setTo(packet.getFrom());
reply.setFrom(packet.getTo());
reply.setError(PacketError.Condition.not_authorized);
send(reply);
} else if (packet instanceof IQ) {
if (((IQ) packet).getType() == IQ.Type.error) {
// Skip IQ packets of type error
return;
}
IQ reply = IQ.createResultIQ((IQ) packet);
reply.setChildElement(((IQ) packet).getChildElement().createCopy());
reply.setError(PacketError.Condition.not_authorized);
send(reply);
} else {
if (((Message) packet).getType() == Message.Type.error) {
// Skip Message packets of type error
return;
}
Message reply = new Message();
reply.setID(packet.getID());
reply.setTo(packet.getFrom());
reply.setFrom(packet.getTo());
reply.setError(PacketError.Condition.not_authorized);
send(reply);
}
}
} catch (Exception e) {
Log.error(e.getMessage(), e);
}
}
Aggregations