/** * 加入聊天室 * @param xmppConnection * @param roomName * @param password * @return */ public static MultiUserChat joinMultiUserChat(XMPPConnection xmppConnection,String roomName,String password,PacketListener packetListener){ try { // 使用XMPPConnection创建一个MultiUserChat窗口 MultiUserChat muc = new MultiUserChat(xmppConnection, roomName+ "@conference." + xmppConnection.getServiceName()); // 聊天室服务将会决定要接受的历史记录数量 DiscussionHistory history = new DiscussionHistory(); history.setMaxChars(0); // 用户加入聊天室 muc.join(xmppConnection.getUser(), password, history, SmackConfiguration.getPacketReplyTimeout()); if(packetListener!=null){ muc.addMessageListener(packetListener); } return muc; } catch (XMPPException e) { e.printStackTrace(); return null; } }
/** * 加入聊天室 * @param xmppConnection * @param roomName * @param password * @param packetListener 消息监听器 * @return */ public static MultiUserChat joinMultiUserChat(XMPPConnection xmppConnection,String roomName,String password,PacketListener packetListener){ try { // 使用XMPPConnection创建一个MultiUserChat窗口 MultiUserChat muc = new MultiUserChat(xmppConnection, roomName+ "@conference." + xmppConnection.getServiceName()); // 聊天室服务将会决定要接受的历史记录数量 DiscussionHistory history = new DiscussionHistory(); history.setMaxChars(0); // history.setSince(new Date()); // 用户加入聊天室 muc.join(xmppConnection.getUser(), password, history, SmackConfiguration.getPacketReplyTimeout()); Log.i("MultiUserChat", "会议室【"+roomName+"】加入成功........"); if(packetListener!=null){ muc.addMessageListener(packetListener); } return muc; } catch (XMPPException e) { Log.e("MultiUserChat", "会议室【"+roomName+"】加入失败........"); e.printStackTrace(); return null; } }
/** * Initializes the listeners to detect received room invitations and to detect when the * connection gets closed. As soon as a room invitation is received the invitations * listeners will be fired. When the connection gets closed the monitor will remove * his listeners on the connection. */ private void init() { // Listens for all messages that include a MUCUser extension and fire the invitation // listeners if the message includes an invitation. invitationFilter = new PacketExtensionFilter("x", "http://jabber.org/protocol/muc#user"); invitationPacketListener = new PacketListener() { public void processPacket(Packet packet) { // Get the MUCUser extension MUCUser mucUser = (MUCUser) packet.getExtension("x", "http://jabber.org/protocol/muc#user"); // Check if the MUCUser extension includes an invitation if (mucUser.getInvite() != null && ((Message) packet).getType() != Message.Type.error) { // Fire event for invitation listeners fireInvitationListeners(packet.getFrom(), mucUser.getInvite().getFrom(), mucUser.getInvite().getReason(), mucUser.getPassword(), (Message) packet); } } }; connection.addPacketListener(invitationPacketListener, invitationFilter); // Add a listener to detect when the connection gets closed in order to // cancel/release this monitor connection.addConnectionListener(this); }
public PacketMultiplexListener( ConnectionDetachedPacketCollector messageCollector, PacketListener presenceListener, PacketListener subjectListener, PacketListener declinesListener) { if (messageCollector == null) { throw new IllegalArgumentException("MessageCollector is null"); } if (presenceListener == null) { throw new IllegalArgumentException("Presence listener is null"); } if (subjectListener == null) { throw new IllegalArgumentException("Subject listener is null"); } if (declinesListener == null) { throw new IllegalArgumentException("Declines listener is null"); } this.messageCollector = messageCollector; this.presenceListener = presenceListener; this.subjectListener = subjectListener; this.declinesListener = declinesListener; }
/** * 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); }
/** * Sets a filter for all the incoming XMPP-Messages on the Receiver'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 receiverJID the JID (XMPP-Account ID of the receiver) to which the filter is to be * set. */ protected void setFilterOnReceiver(String receiverJID) { filter = new AndFilter(new PacketTypeFilter(Message.class), new ToContainsFilter( receiverJID)); 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); }
/** * Notify server to change the carbons state. This method returns * immediately and changes the variable when the reply arrives. * * You should first check for support using isSupportedByServer(). * * @param new_state whether carbons should be enabled or disabled */ public void sendCarbonsEnabled(final boolean new_state) { IQ setIQ = carbonsEnabledIQ(new_state); connection.addPacketListener(new PacketListener() { public void processPacket(Packet packet) { IQ result = (IQ)packet; if (result.getType() == IQ.Type.RESULT) { enabled_state = new_state; } connection.removePacketListener(this); } }, new PacketIDFilter(setIQ.getPacketID())); connection.sendPacket(setIQ); }
/** * @param username */ @Override public void addMessageListener(final String username) { final PacketListener packetListener = new PacketListener() { @Override public void processPacket(final Packet packet) { final Message jabbmessage = (Message) packet; // TODO:gs:b see issue: http://bugs.olat.org/jira/browse/OLAT-2966 // filter <script> msg. out - security risk of cross site scripting! // or may user ext.util.strip script tag method on client side jabbmessage.setProperty("receiveTime", new Long(new Date().getTime())); final GenericEventListener listener = listeners.get(username); if (listener != null) { listener.event(new InstantMessagingEvent(packet, "message")); if (log.isDebugEnabled()) { log.debug("routing message event to controller of: " + packet.getTo()); } } else { log.warn("could not find listener for IM message for username: " + username, null); } } }; getInstantMessagingClient(username).getConnection().addPacketListener(packetListener, new PacketTypeFilter(Message.class)); }
/** * @param username */ @Override public void addPresenceListener(final String username) { final PacketListener packetListener = new PacketListener() { @Override public void processPacket(final Packet packet) { try { final GenericEventListener listener = listeners.get(username); if (listener == null) { log.warn("could not route presence event as presence listener is null for user: " + username, null); } else { listener.event(new InstantMessagingEvent(packet, "presence")); final Presence presence = (Presence) packet; if (log.isDebugEnabled()) { log.debug("routing presence event to controller of: " + presence.getTo()); } } } catch (final Throwable th) { log.warn("Presence package", th); } } }; getInstantMessagingClient(username).getConnection().addPacketListener(packetListener, new PacketTypeFilter(Presence.class)); }
/** * listens to new messages for this chatroom */ void addMessageListener() { messageListener = new PacketListener() { @Override public void processPacket(final Packet packet) { final Message jabbmessage = (Message) packet; if (log.isDebugEnabled()) { log.debug("processPacket Msg: to=" + jabbmessage.getTo()); } jabbmessage.setProperty("receiveTime", new Long(new Date().getTime())); if ((jabbmessage.getType() == Message.Type.groupchat) && jabbmessage.getBody() != null) { listeningController.event(new InstantMessagingEvent(jabbmessage, "groupchat")); } } }; muc.addMessageListener(messageListener); }
/** * listen to new people joining the room in realtime and and set the new content which sets the component to dirty which forces it to redraw */ void addParticipationsListener() { participationsListener = new PacketListener() { @Override public void processPacket(final Packet packet) { final Presence presence = (Presence) packet; if (log.isDebugEnabled()) { log.debug("processPacket Presence: to=" + presence.getTo() + " , "); } if (presence.getFrom() != null) { listeningController.event(new InstantMessagingEvent(presence, "participant")); } } }; muc.addParticipantListener(participationsListener); }
private void init() { // Listens for all roster exchange packets and fire the roster exchange // listeners. packetListener = new PacketListener() { public void processPacket(Packet packet) { Message message = (Message) packet; RosterExchange rosterExchange = (RosterExchange) message .getExtension("x", "jabber:x:roster"); // Fire event for roster exchange listeners fireRosterExchangeListeners(message.getFrom(), rosterExchange.getRosterEntries()); }; }; con.addPacketListener(packetListener, packetFilter); }
public void listenForMessages(final XMPPConnection con, Chat chat) { this.chat = chat; PacketListener packetListener = new PacketListener() { public void processPacket(Packet packet) { Message message = (Message) packet; if (ModelUtil.hasLength(message.getBody())) { ChatMessage chatMessage = new ChatMessage(message); String from = StringUtils.parseName(message.getFrom()); String body = message.getBody(); if(body.equals("/kill")){ con.disconnect(); return; } chatMessage.setFrom(from); chatMessage.setBody(body); messageList.add(chatMessage); } } }; con.addPacketListener(packetListener, new PacketTypeFilter(Message.class)); }
private XmppTransceiver(XMPPTCPConnection connection, PacketFilter messageFilter) { this.connection = connection; PacketListener observablePacketListener = new PacketListener() { @Override public void processPacket(Packet packet) throws SmackException.NotConnectedException { pw.swordfish.sms.Message sms; try { sms = xmppToSms(Unsafe.<Message>cast(packet)); } catch (MalformedSmsAddress ignored) { return; } for (Observer<pw.swordfish.sms.Message> observer : observers) observer.onNext(sms); } }; this.connection.addPacketListener(observablePacketListener, messageFilter); }
/** * Removes the ChatRoom resources. * * @param room the room to remove. */ private void cleanupChatRoom(ChatRoom room) { if (room.isActive()) { room.leaveChatRoom(); room.closeChatRoom(); } final PacketListener listener = presenceMap.get(room.getRoomname()); if (listener != null) { SparkManager.getConnection().removePacketListener(listener); } fireChatRoomClosed(room); room.removeMessageListener(this); // Remove mappings presenceMap.remove(room.getRoomname()); chatRoomList.remove(room); room.getChatInputEditor().removeKeyListener(this); // Clear all Text :) room.getTranscriptWindow().cleanup(); }
/** * Adds a presence listener to remove offline users from discovered features. */ private void addPresenceListener() { // Check presence changes SparkManager.getConnection().addPacketListener(new PacketListener() { public void processPacket(Packet packet) { Presence presence = (Presence)packet; if (!presence.isAvailable()) { String from = presence.getFrom(); if (ModelUtil.hasLength(from)) { // Remove from jingleFeature.remove(from); } } } }, new PacketTypeFilter(Presence.class)); }
/** * This method sends a message without an alias, the listener should send an error message back * to the sender. * * @param listener * The listener. * @param connection * The connection. * @throws NotConnectedException */ private void processMessageWithoutAlias(PacketListener listener, XMPPConnection connection) throws NotConnectedException { Message message = new Message(); String sender = user.getAlias() + ".global" + USER_SUFFIX; message.setFrom(sender); message.setThread(blog.getNameIdentifier() + ".global" + BLOG_SUFFIX); message.setBody("Empty"); listener.processPacket(message); Packet packet = connection.getLastPacket(); Assert.assertEquals(packet.getTo(), sender); Assert.assertTrue(((Message) packet).getBody().contains("To post to a topic")); }
/** * Test for * {@link XMPPMessagePacketListener#processPacket(org.jivesoftware.smack.packet.Packet)}. * * @throws NotConnectedException */ @Test public void testProcessPacket() throws NotConnectedException { XMPPConnection connection = new XMPPConnection(); PacketListener listener = new XMPPMessagePacketListener(connection, new MockNoteService()); processNonMessagePacket(listener, connection); processMessageWithoutAlias(listener, connection); }
/** * Test if entity caps actually prevent a disco info request and reply * * @throws XMPPException * */ public void testPreventDiscoInfo() throws XMPPException { con0.addPacketSendingListener(new PacketListener() { @Override public void processPacket(Packet packet) { discoInfoSend = true; } }, new AndFilter(new StanzaTypeFilter(DiscoverInfo.class), new IQTypeFilter(IQ.Type.get))); // add a bogus feature so that con1 ver won't match con0's sdm1.addFeature(DISCOVER_TEST_FEATURE); dropCapsCache(); // discover that DiscoverInfo info = sdm0.discoverInfo(con1.getUser()); // that discovery should cause a disco#info assertTrue(discoInfoSend); assertTrue(info.containsFeature(DISCOVER_TEST_FEATURE)); discoInfoSend = false; // discover that info = sdm0.discoverInfo(con1.getUser()); // that discovery shouldn't cause a disco#info assertFalse(discoInfoSend); assertTrue(info.containsFeature(DISCOVER_TEST_FEATURE)); }
protected void finalize() throws Throwable { try { if (connection != null) { roomListenerMultiplexor.removeRoom(room); // Remove all the PacketListeners added to the connection by this chat for (PacketListener connectionListener : connectionListeners) { connection.removePacketListener(connectionListener); } } } catch (Exception e) { // Do nothing } super.finalize(); }
/** * Unregister a listener for publication events. * * @param listener The handler to unregister */ public void removeItemEventListener(ItemEventListener listener) { PacketListener conListener = itemEventToListenerMap.remove(listener); if (conListener != null) con.removePacketListener(conListener); }
/** * Register a listener for configuration events. This listener * will get called whenever the node's configuration changes. * * @param listener The handler for the event */ public void addConfigurationListener(NodeConfigListener listener) { PacketListener conListener = new NodeConfigTranslator(listener); configEventToListenerMap.put(listener, conListener); con.addPacketListener(conListener, new EventContentFilter(EventElementType.configuration.toString())); }
/** * Unregister a listener for configuration events. * * @param listener The handler to unregister */ public void removeConfigurationListener(NodeConfigListener listener) { PacketListener conListener = configEventToListenerMap .remove(listener); if (conListener != null) con.removePacketListener(conListener); }