use of org.xmpp.packet.Packet 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);
}
use of org.xmpp.packet.Packet in project Openfire by igniterealtime.
the class DiscoIQRegisteredProcessor method process.
@Override
public void process(Packet packet, final String subdomain, String to, String from) throws PacketRejectedException {
Log.debug("Processing packet in DiscoIQRegisteredProcessor for " + subdomain);
// Check if the jabber:iq:register is enabled in admin panel
boolean isFeatureEnabled = JiveGlobals.getBooleanProperty("plugin.remoteroster.sparkDiscoInfo", false);
if (!isFeatureEnabled) {
Log.debug("Spark extension is deactivated. Won't change the disco#info");
return;
}
final InterceptorManager interceptorManager = InterceptorManager.getInstance();
final PacketInterceptor interceptor = new PacketInterceptor() {
public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException {
if (!processed && incoming) {
if (packet instanceof IQ) {
IQ iqPacket = (IQ) packet;
Element packetElement = iqPacket.getChildElement();
if (packetElement == null)
return;
String ns = iqPacket.getChildElement().getNamespace().getURI();
if (iqPacket.getType().equals(IQ.Type.result) && ns.equals("jabber:iq:register") && iqPacket.getFrom().toString().equals(subdomain)) {
// Check if we are already registered
setRegistered(iqPacket.toString().contains("<registered/>"));
throw new PacketRejectedException();
} else if (iqPacket.getType().equals(IQ.Type.result) && ns.equals("http://jabber.org/protocol/disco#info") && iqPacket.getFrom().toString().equals(subdomain)) {
/*
* This is the answer of the disco#info from spark
* to our component. add the jabber:iq:register
* feature if we are registered
*/
if (isRegistered()) {
Log.debug("Modifying disco#info packge to send registered iq feature to Spark user " + iqPacket.getTo().toString());
Attribute attribut = new DefaultAttribute("var", "jabber:iq:registered");
iqPacket.getChildElement().addElement("feature").add(attribut);
}
}
}
}
}
};
Log.debug("Creating my own listener for jabber:iq:register result to external component " + subdomain);
interceptorManager.addInterceptor(interceptor);
IQ askComponent = new IQ();
askComponent.setTo(to);
askComponent.setFrom(from);
askComponent.setType(IQ.Type.get);
Element query = new DefaultElement(QName.get("query", "jabber:iq:register"));
askComponent.setChildElement(query);
// Remove the package intercepter in 1sec
TimerTask removeInterceptorTask = new TimerTask() {
@Override
public void run() {
Log.debug("Removing my created listener for jabber:iq:register. Component " + subdomain);
interceptorManager.removeInterceptor(interceptor);
}
};
Timer timer = new Timer();
timer.schedule(removeInterceptorTask, 1000);
// Send the register query to component
dispatchPacket(askComponent);
}
use of org.xmpp.packet.Packet in project Openfire by igniterealtime.
the class ContentFilterPlugin method interceptPacket.
public void interceptPacket(Packet packet, Session session, boolean read, boolean processed) throws PacketRejectedException {
if (isValidTargetPacket(packet, read, processed)) {
Packet original = packet;
if (Log.isDebugEnabled()) {
Log.debug("Content filter: intercepted packet:" + original.toString());
}
// as it's an expensive operation
if (violationNotificationEnabled && violationIncludeOriginalPacketEnabled && maskEnabled) {
original = packet.createCopy();
}
// filter the packet
boolean contentMatched = contentFilter.filter(packet);
if (Log.isDebugEnabled()) {
Log.debug("Content filter: content matched? " + contentMatched);
}
// notify admin of violations
if (contentMatched && violationNotificationEnabled) {
if (Log.isDebugEnabled()) {
Log.debug("Content filter: sending violation notification");
Log.debug("Content filter: include original msg? " + this.violationIncludeOriginalPacketEnabled);
}
sendViolationNotification(original);
}
// the conversation in the (spy mode!)
if (contentMatched) {
if (allowOnMatch) {
if (Log.isDebugEnabled()) {
Log.debug("Content filter: allowed content:" + packet.toString());
}
// no further action required
} else {
// msg must be rejected
if (Log.isDebugEnabled()) {
Log.debug("Content filter: rejecting packet");
}
PacketRejectedException rejected = new PacketRejectedException("Packet rejected with disallowed content!");
if (rejectionNotificationEnabled) {
// let the sender know about the rejection, this is
// only possible/useful if the content is not masked
rejected.setRejectionMessage(rejectionMessage);
}
throw rejected;
}
}
}
}
use of org.xmpp.packet.Packet 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.Packet in project Openfire by igniterealtime.
the class PacketCounter method start.
/**
* Resets all counters, and starts counting.
*/
public void start() {
// Register a packet listener so that we can track packet traffic.
interceptor = new PacketInterceptor() {
public void interceptPacket(final Packet packet, final Session session, final boolean incoming, final boolean processed) {
if (!processed) {
// don't count packets twice!
return;
}
stanza.incrementAndGet();
if (packet instanceof Message) {
message.incrementAndGet();
}
if (packet instanceof Presence) {
presence.incrementAndGet();
}
if (packet instanceof IQ) {
iq.incrementAndGet();
switch(((IQ) packet).getType()) {
case get:
iqGet.incrementAndGet();
break;
case set:
iqSet.incrementAndGet();
break;
case result:
iqResult.incrementAndGet();
break;
case error:
iqError.incrementAndGet();
break;
}
}
}
};
// reset counters
stanza.set(0);
message.set(0);
presence.set(0);
iq.set(0);
iqGet.set(0);
iqSet.set(0);
iqResult.set(0);
iqError.set(0);
// register listener
InterceptorManager.getInstance().addInterceptor(interceptor);
}
Aggregations