org.jivesoftware.smack.chat2.ChatManager Java Examples

The following examples show how to use org.jivesoftware.smack.chat2.ChatManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MucChatService.java    From xyTalk-pc with GNU Affero General Public License v3.0 6 votes vote down vote up
private static void sendOfflineInvitationMessage(List<String> users, String roomid, String roomName)
		throws XmppStringprepException {
	// TODO 对离线用户应发送邀请消息,并让服务器存储离线消息,待用户上线后还应对此类消息进行处理
	MucInvitation mi = new MucInvitation(roomid, roomName);

	for (int i = 0; i < users.size(); i++) {
		String userJid = users.get(i);
		Chat chat = ChatManager.getInstanceFor(Launcher.connection).chatWith(JidCreate.entityBareFrom(userJid));
		org.jivesoftware.smack.packet.Message message = new org.jivesoftware.smack.packet.Message();
		message.setType(Type.chat);
		message.addExtension(mi);
		message.setBody("请加入会议");
		try {
			chat.send(message);
			DebugUtil.debug("sendOfflineInvitationMessage:" + message.toXML());
		} catch (NotConnectedException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
 
Example #2
Source File: MucChatService.java    From xyTalk-pc with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void sendInvitationMessage(List<Jid> users, String roomid, String roomName){
	MucInvitation mi = new MucInvitation(roomid, roomName);

	for (int i = 0; i < users.size(); i++) {
		Jid userJid = users.get(i);
		Chat chat = ChatManager.getInstanceFor(Launcher.connection).chatWith(userJid.asEntityBareJidIfPossible());
		org.jivesoftware.smack.packet.Message message = new org.jivesoftware.smack.packet.Message();
		message.setType(Type.chat);
		message.addExtension(mi);
		message.setBody("请加入会议");
		try {
			chat.send(message);
			DebugUtil.debug("sendOfflineInvitationMessage:" + message.toXML());
		} catch (NotConnectedException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
 
Example #3
Source File: MucChatService.java    From xyTalk-pc with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void sendKickMessage(List<Jid> users, String roomId, String name) {
	MucKick mucKick = new MucKick(roomId, name);

	for (int i = 0; i < users.size(); i++) {
		Jid userJid = users.get(i);
		Chat chat;
		chat = ChatManager.getInstanceFor(Launcher.connection).chatWith(userJid.asEntityBareJidIfPossible());
		org.jivesoftware.smack.packet.Message message = new org.jivesoftware.smack.packet.Message();
		message.setType(Type.chat);
		message.addExtension(mucKick);
		message.setBody("被管理员删除出群:"+name);
		try {
			chat.send(message);
			DebugUtil.debug("sendKickMessage:" + message.toXML());
		} catch (NotConnectedException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
 
Example #4
Source File: XmppFileService.java    From xyTalk-pc with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void requestOfflineFile(org.jivesoftware.smack.packet.Message message) {
	// TODO 向离线文件机器人发送请求
	// 将接收到的离线文件消息,原封不动的发给离线文件机器人 ,
	//	离线机器人接收后,会立即将已存储的文件发过来,同时会发送一条消息回执,发送成功后,删除文件
	//	用户收到回执后,需要对聊天UI进行处理,注意:此时的fromJid是机器人,应处理为实际发送者,即senderFullJid
	Chat chat;
	try {
		chat = ChatManager.getInstanceFor(Launcher.connection).chatWith(JidCreate.entityBareFrom(Launcher.OFFLINEFILEROBOTJID));
        
		chat.send(message); //发送请求到机器人,然后机器人会把文件发给我
		OfflineFile of = (OfflineFile)message.getExtension(OfflineFile.NAMESPACE);
		offlineFileJidMap.put(of.getFileName(), of.getSenderFullJid());
		DebugUtil.debug( "send offlinefile request:"+ message.toXML());	
		
	} catch (XmppStringprepException |NotConnectedException | InterruptedException e) {
		
	}
}
 
Example #5
Source File: OXInstantMessagingManager.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Send an OX message to a {@link OpenPgpContact}. The message will be encrypted to all active keys of the contact,
 * as well as all of our active keys. The message is also signed with our key.
 *
 * @param contact contact capable of OpenPGP for XMPP: Instant Messaging.
 * @param body message body.
 *
 * @return {@link OpenPgpMetadata} about the messages encryption + signatures.
 *
 * @throws InterruptedException if the thread is interrupted
 * @throws IOException IO is dangerous
 * @throws SmackException.NotConnectedException if we are not connected
 * @throws SmackException.NotLoggedInException if we are not logged in
 * @throws PGPException PGP is brittle
 */
public OpenPgpMetadata sendOxMessage(OpenPgpContact contact, CharSequence body)
        throws InterruptedException, IOException,
        SmackException.NotConnectedException, SmackException.NotLoggedInException, PGPException {
    MessageBuilder messageBuilder = connection()
            .getStanzaFactory()
            .buildMessageStanza()
            .to(contact.getJid());

    Message.Body mBody = new Message.Body(null, body.toString());
    OpenPgpMetadata metadata = addOxMessage(messageBuilder, contact, Collections.<ExtensionElement>singletonList(mBody));

    Message message = messageBuilder.build();
    ChatManager.getInstanceFor(connection()).chatWith(contact.getJid().asEntityBareJidIfPossible()).send(message);

    return metadata;
}
 
Example #6
Source File: ChatStateIntegrationTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
@SmackIntegrationTest
public void testChatStateListeners() throws Exception {
    ChatStateManager manOne = ChatStateManager.getInstance(conOne);
    ChatStateManager manTwo = ChatStateManager.getInstance(conTwo);

    // Add chatState listeners.
    manTwo.addChatStateListener(this::composingListener);
    manTwo.addChatStateListener(this::activeListener);

    Chat chatOne = ChatManager.getInstanceFor(conOne)
            .chatWith(conTwo.getUser().asEntityBareJid());

    // Test, if setCurrentState works and the chatState arrives
    manOne.setCurrentState(ChatState.composing, chatOne);
    composingSyncPoint.waitForResult(timeout);

    // Test, if the OutgoingMessageInterceptor successfully adds a chatStateExtension of "active" to
    // an outgoing chat message and if it arrives at the other side.
    Chat chat = ChatManager.getInstanceFor(conOne)
            .chatWith(conTwo.getUser().asEntityBareJid());
    chat.send("Hi!");
    activeSyncPoint.waitForResult(timeout);
}
 
Example #7
Source File: StanzaThread.java    From tutorials with MIT License 6 votes vote down vote up
@Override
public void run() {
    XMPPTCPConnectionConfiguration config = null;
    try {
        config = XMPPTCPConnectionConfiguration.builder()
                .setUsernameAndPassword("baeldung2","baeldung2")
                .setXmppDomain("jabb3r.org")
                .setHost("jabb3r.org")
                .build();

        AbstractXMPPConnection connection = new XMPPTCPConnection(config);
        connection.connect();
        connection.login();

        ChatManager chatManager = ChatManager.getInstanceFor(connection);

        Chat chat = chatManager.chatWith(JidCreate.from("[email protected]").asEntityBareJidOrThrow());

        chat.send("Hello!");

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example #8
Source File: SmackIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenSendMessageWithChat_thenReceiveMessage() throws XmppStringprepException, InterruptedException {

    CountDownLatch latch = new CountDownLatch(1);
    ChatManager chatManager = ChatManager.getInstanceFor(connection);
    final String[] expected = {null};

    new StanzaThread().run();

    chatManager.addIncomingListener((entityBareJid, message, chat) -> {
        logger.info("Message arrived: " + message.getBody());
        expected[0] = message.getBody();
        latch.countDown();
    });

    latch.await();
    Assert.assertEquals("Hello!", expected[0]);
}
 
Example #9
Source File: XmppFileService.java    From xyTalk-pc with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void sendOfflineFileMsg(String fileFullPath, String reciveBareJid,String uuid,boolean toRobot) {
	String fileType;
	String fileName = fileFullPath.substring(fileFullPath.lastIndexOf(System.getProperty("file.separator"))+1);

	boolean isImage;
	if (fileFullPath.lastIndexOf(".")<0){
		isImage = false;
	}else{
		String type = MimeTypeUtil.getMime(fileFullPath.substring(fileFullPath.lastIndexOf(".")));
		isImage = type.startsWith("image/");
	}
	
	if (isImage){
		fileType = "image";
	}
	else{
		fileType = "file";
	}
	
	OfflineFile of = null;
	OfflineFileRobot ofr = null; 
	
	if (toRobot){
		ofr = new OfflineFileRobot(UserCache.CurrentBareJid,reciveBareJid,fileType,uuid,fileName);
	}else{
		of = new OfflineFile(UserCache.CurrentBareJid,reciveBareJid,fileType,uuid,fileName);
	}
		
	
	Chat chat;
	try {
		chat = ChatManager.getInstanceFor(Launcher.connection).chatWith(JidCreate.entityBareFrom(reciveBareJid));
        org.jivesoftware.smack.packet.Message message = new org.jivesoftware.smack.packet.Message();
        message.setType(Type.chat);
        
        if (toRobot){
	        message.addExtension(ofr);	        	
        }else{
	        message.addExtension(of);	        	
        }

        message.setBody(UserCache.CurrentUserRealName + " 发给您离线文件,即将接收:"+fileName);
		chat.send(message);
		DebugUtil.debug( "send offlinefile msg:"+ message.toXML());	
		
	} catch (XmppStringprepException |NotConnectedException | InterruptedException e) {
		
	}
}
 
Example #10
Source File: ChatMarkersManager.java    From Smack with Apache License 2.0 4 votes vote down vote up
private ChatMarkersManager(XMPPConnection connection) {
    super(connection);

    chatManager = ChatManager.getInstanceFor(connection);

    connection.addMessageInterceptor(mb -> mb.addExtension(ChatMarkersElements.MarkableExtension.INSTANCE),
            m -> {
                return OUTGOING_MESSAGE_FILTER.accept(m);
        });

    connection.addSyncStanzaListener(new StanzaListener() {
        @Override
        public void processStanza(Stanza packet)
                throws
                NotConnectedException,
                InterruptedException,
                SmackException.NotLoggedInException {
            final Message message = (Message) packet;

            // Note that this listener is used together with a PossibleFromTypeFilter.ENTITY_BARE_JID filter, hence
            // every message is guaranteed to have a from address which is representable as bare JID.
            EntityBareJid bareFrom = message.getFrom().asEntityBareJidOrThrow();

            final Chat chat = chatManager.chatWith(bareFrom);

            asyncButOrdered.performAsyncButOrdered(chat, new Runnable() {
                @Override
                public void run() {
                    for (ChatMarkersListener listener : incomingListeners) {
                        if (ChatMarkersElements.MarkableExtension.from(message) != null) {
                            listener.newChatMarkerMessage(ChatMarkersState.markable, message, chat);
                        }
                        else if (ChatMarkersElements.ReceivedExtension.from(message) != null) {
                            listener.newChatMarkerMessage(ChatMarkersState.received, message, chat);
                        }
                        else if (ChatMarkersElements.DisplayedExtension.from(message) != null) {
                            listener.newChatMarkerMessage(ChatMarkersState.displayed, message, chat);
                        }
                        else if (ChatMarkersElements.AcknowledgedExtension.from(message) != null) {
                            listener.newChatMarkerMessage(ChatMarkersState.acknowledged, message, chat);
                        }
                    }
                }
            });

        }
    }, INCOMING_MESSAGE_FILTER);

    serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
}
 
Example #11
Source File: OpenPgpManager.java    From Smack with Apache License 2.0 4 votes vote down vote up
/**
 * Private constructor to avoid instantiation without putting the object into {@code INSTANCES}.
 *
 * @param connection xmpp connection.
 */
private OpenPgpManager(XMPPConnection connection) {
    super(connection);
    ChatManager.getInstanceFor(connection).addIncomingListener(this::incomingChatMessageListener);
    pepManager = PepManager.getInstanceFor(connection);
}