/** * Sets a filter for all the incoming XMPP-Messages on the Sender's JID (XMPP-Account ID). * Also creates a listener for the incoming messages and connects the listener to the * XMPPConnection alongside the set filter. * * @param senderJID the JID (XMPP-Account ID of the sender) to which the filter is to be set. */ protected void setFilterOnSender(String senderJID) { filter = new AndFilter(new PacketTypeFilter(Message.class), new FromContainsFilter( senderJID)); listener = new PacketListener() { @Override public void processPacket(Packet packet) { if (packet instanceof Message) { final Message xmppMessage = (Message) packet; Thread msgProcessThread = new Thread() { public void run() { processIncomingMessage(xmppMessage); } }; msgProcessThread.setDaemon(true); msgProcessThread.start(); } } }; connection.addPacketListener(listener, filter); }
/** * Called when the mode provided by the connection is changed to online or the user sets the mode to online. * If Handler is in onlinemode some preparations are done like checking and perhaps retrieving the spaces service adress. */ private void prepareOnlineMode(){ if (getMode() == Mode.ONLINE){ if (domain == null){ domain = userInfo.getDomain(); } if (connectionHandler.getNetworkInformation().getSpacesServiceJID() != null) { this.connection.removePacketListener(packetListener); AndFilter andFilter = new AndFilter(); OrFilter orFilter = new OrFilter(); orFilter.addFilter(new IQTypeFilter(IQ.Type.ERROR)); orFilter.addFilter(new IQTypeFilter(IQ.Type.RESULT)); andFilter.addFilter(orFilter); andFilter.addFilter(new FromContainsFilter(connectionHandler.getNetworkInformation().getSpacesServiceJID())); this.connection.addPacketListener(packetListener, andFilter); } } }
public PacketFilter getInitiationPacketFilter(String from, String streamID) { /* * this method is always called prior to #negotiateIncomingStream() so * the In-Band Bytestream initiation listener must ignore the next * In-Band Bytestream request with the given session ID */ this.manager.ignoreBytestreamRequestOnce(streamID); return new AndFilter(new FromContainsFilter(from), new IBBOpenSidFilter(streamID)); }
/** * Checks the availability of the specified agent. * * @param agentJID the jid of the agent to check. * @return true if the agent is available to accept a request. */ public static boolean isAgentOnline(String agentJID) { ChatManager chatManager = ChatManager.getInstance(); XMPPConnection globalConnection = chatManager.getGlobalConnection(); Presence directedPresence = new Presence(Presence.Type.available); directedPresence.setProperty("anonymous", true); directedPresence.setTo(agentJID); PacketFilter typeFilter = new PacketTypeFilter(Presence.class); PacketFilter fromFilter = new FromContainsFilter(agentJID); PacketCollector collector = globalConnection.createPacketCollector(new AndFilter(fromFilter, typeFilter)); globalConnection.sendPacket(directedPresence); Presence response = (Presence)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); // Cancel the collector. collector.cancel(); if (response == null) { return false; } if (response.getError() != null) { return false; } return Presence.Type.available == response.getType(); }
/** * Setter for the XMPPConnection. Use this only when you create a new connection object. * @param connectionHandler The XMPPConnection object this instance of a SpaceHandler should use. * @throws SpaceManagementException Thrown when there's no response from the server. */ public void changeConnectionHandler(ConnectionHandler connectionHandler) throws SpaceManagementException { this.connectionHandler = connectionHandler; this.connection = this.connectionHandler.getXMPPConnection(); if (connectionHandler.getNetworkInformation().getSpacesServiceJID() != null){ AndFilter andFilter = new AndFilter(); OrFilter orFilter = new OrFilter(); orFilter.addFilter(new IQTypeFilter(IQ.Type.ERROR)); orFilter.addFilter(new IQTypeFilter(IQ.Type.RESULT)); andFilter.addFilter(orFilter); andFilter.addFilter(new FromContainsFilter(connectionHandler.getNetworkInformation().getSpacesServiceJID())); this.connection.addPacketListener(packetListener, andFilter); } }
PacketCollector createPacketCollector(Chat chat) { return connection.createPacketCollector(new AndFilter(new ThreadFilter(chat.getThreadID()), new FromContainsFilter(chat.getParticipant()))); }
/** * Read the PIN number sent by the user as the reply. * * @param connection * @param userName * @return */ public boolean getUserResponse(XMPPConnection connection, String userName) { String response = null; Presence presence = connection.getRoster().getPresence(userName); if (presence.isAvailable()) { try { ChatManager chatManager = connection.getChatManager(); Chat chat = chatManager.createChat(userName, null); PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class), new FromContainsFilter(userName)); XmppResponseListener chatListener = new XmppResponseListener(); connection.addPacketListener(chatListener, filter); if (isPINEnabled) { chat.sendMessage("Please reply with your PIN Number here."); if (log.isInfoEnabled()) { log.info("User PIN is sent to the user and awaiting for the response."); } while (!chatListener.isResponseReceived()) { Thread.sleep(100); } response = chatListener.getResponse(); if (response != null) { return userPIN.contentEquals(response.trim()); } } else { chat.sendMessage( "You are about to get authenticated for your OpenID. Do you want to continue: [Yes] or [No]"); if (log.isInfoEnabled()) { log.info("User PIN is sent to the user and awaiting for the response."); } while (!chatListener.isResponseReceived()) { Thread.sleep(100); } response = chatListener.getResponse(); if (response != null) { if ("YES".equalsIgnoreCase(response.trim())) { return true; } else if ("NO".equalsIgnoreCase(response.trim())) { return false; } else { pinDisabledResponse = false; return false; } } } } catch (Exception e) { log.error("Error while getting user response", e); } } else { return false; } return false; }
PacketCollector createPacketCollector(Chat chat) { return connection.createPacketCollector(new AndFilter(new ThreadFilter( chat.getThreadID()), new FromContainsFilter(chat .getParticipant()))); }
/** * Joins the specified room and removes from queue. * * @param roomName the name of the room to join. */ public void joinRoom(String roomName) { // Set the last check now lastCheck = System.currentTimeMillis(); try { groupChat = new MultiUserChat(connection, roomName); if (name != null) { try { AndFilter presenceFilter = new AndFilter(new PacketTypeFilter(Presence.class), new FromContainsFilter(groupChat.getRoom())); connection.addPacketListener(this, presenceFilter); groupChat.join(name); } // Catch any join exceptions and attempt to join again // using an altered name (since exception is most likely // due to a conflict of names). catch (XMPPException xe) { String conflictName = name + " (Visitor)"; groupChat.join(conflictName); nickname = conflictName; } } else { nickname = "Visitor"; groupChat.join(nickname); } groupChat.addParticipantListener(new PacketListener() { public void processPacket(Packet packet) { final Presence p = (Presence)packet; final String from = p.getFrom(); String user = StringUtils.parseResource(from); if (p.getType() != Presence.Type.available) { lastAgent = user; } final Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { checkForEmptyRoom(); } }, 5000); } }); messageEventManager = new MessageEventManager(connection); messageEventManager.addMessageEventNotificationListener(this); } catch (Exception e) { WebLog.logError("Error joining room:", e); } this.roomName = roomName; listenForMessages(connection, groupChat); }