org.jivesoftware.smack.packet.Message.Type Java Examples

The following examples show how to use org.jivesoftware.smack.packet.Message.Type. 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: ChatService.java    From xyTalk-pc with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void sendMessage(String roomId, String content) {
	EntityBareJid jid = null;
	try {
		jid = JidCreate.entityBareFrom(roomId);
	} catch (XmppStringprepException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	Chat chat = ChatManager.getInstanceFor(Launcher.connection).chatWith(jid);

	org.jivesoftware.smack.packet.Message message = new org.jivesoftware.smack.packet.Message();
	message.setType(Type.chat);
	message.addExtension(new Receipt());
	message.setBody(content);
	try {
		chat.send(message);
		DebugUtil.debug("chat.sendMessage::" + message.toString());
	} catch (NotConnectedException | InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}
 
Example #2
Source File: ChatService.java    From xyTalk-pc with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void sendMessage(String roomId, String content,ExtensionElement ext) {
	EntityBareJid jid = null;
	try {
		jid = JidCreate.entityBareFrom(roomId);
	} catch (XmppStringprepException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	Chat chat = ChatManager.getInstanceFor(Launcher.connection).chatWith(jid);

	org.jivesoftware.smack.packet.Message message = new org.jivesoftware.smack.packet.Message();
	message.setType(Type.chat);
	message.addExtension(ext);
	message.setBody(content);
	try {
		chat.send(message);
		DebugUtil.debug("chat.sendMessage::" + message.toString());
	} 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
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 #4
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 #5
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 #6
Source File: ChatConnectionTest.java    From Smack with Apache License 2.0 6 votes vote down vote up
private static MessageBuilder createChatPacket(final String threadId, final boolean isEntityFullJid) {
    MessageBuilder chatMsg = StanzaBuilder.buildMessage()
            .ofType(Message.Type.chat)
            .to(JidTestUtil.BARE_JID_1);
    chatMsg.setBody("the body message - " + System.currentTimeMillis());
    Jid jid;
    if (isEntityFullJid) {
        jid = JidTestUtil.DUMMY_AT_EXAMPLE_ORG_SLASH_DUMMYRESOURCE;
    } else {
        jid = JidTestUtil.DUMMY_AT_EXAMPLE_ORG;
    }
    chatMsg.from(jid);
    if (threadId != null) {
        chatMsg.setThread(threadId);
    }
    return chatMsg;
}
 
Example #7
Source File: ChatConnectionTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void validateMessageTypeWithDefaults1() {
    MessageBuilder incomingChat = createChatPacket("134", true);
    incomingChat.ofType(Type.chat);
    processServerMessage(incomingChat.build());
    assertNotNull(listener.getNewChat());
}
 
Example #8
Source File: BroadcastPlugin.java    From Spark with Apache License 2.0 5 votes vote down vote up
@Override
public void processStanza(final Stanza stanza) {
    SwingUtilities.invokeLater( () -> {
        try {
            final Message message = (Message)stanza;

            // Do not handle errors or offline messages
            final DelayInformation offlineInformation = message.getExtension("delay", "urn:xmpp:delay");
            if (offlineInformation != null || message.getError() != null) {
                return;
            }

            final JivePropertiesExtension extension = ((JivePropertiesExtension) message.getExtension( JivePropertiesExtension.NAMESPACE ));
            final boolean broadcast = extension != null && extension.getProperty( "broadcast" ) != null;

            if ((broadcast || message.getType() == Type.normal
                || message.getType() == Type.headline) && message.getBody() != null) {
                showAlert((Message)stanza);
            }
            else {
                String host = SparkManager.getSessionManager().getServerAddress();
                String from = stanza.getFrom() != null ? stanza.getFrom().toString() : "";
                if (host.equalsIgnoreCase(from) || !ModelUtil.hasLength(from)) {
                    showAlert((Message)stanza);
                }
            }
        }
        catch (Exception e) {
            Log.error(e);
        }
    } );

}
 
Example #9
Source File: ChatConnectionTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void chatNotMatchedWithTypeNormal() {
    cm.setNormalIncluded(false);

    MessageBuilder incomingChat = createChatPacket(null, false);
    incomingChat.ofType(Type.normal);
    processServerMessage(incomingChat.build());

    assertNull(listener.getNewChat());
}
 
Example #10
Source File: ChatConnectionTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void validateMessageTypeWithNoNormal2() {
    cm.setNormalIncluded(false);
    MessageBuilder incomingChat = createChatPacket("134", true);
    incomingChat.ofType(Type.normal);
    processServerMessage(incomingChat.build());
    assertNull(listener.getNewChat());
}
 
Example #11
Source File: ChatConnectionTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void validateMessageTypeWithNoNormal1() {
    cm.setNormalIncluded(false);
    MessageBuilder incomingChat = createChatPacket("134", true);
    incomingChat.ofType(Type.chat);
    processServerMessage(incomingChat.build());
    assertNotNull(listener.getNewChat());
}
 
Example #12
Source File: ChatConnectionTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void validateMessageTypeWithDefaults3() {
    MessageBuilder incomingChat = createChatPacket("134", true);
    incomingChat.ofType(Type.groupchat);
    processServerMessage(incomingChat.build());
    assertNull(listener.getNewChat());
}
 
Example #13
Source File: ChatConnectionTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void validateMessageTypeWithDefaults2() {
    MessageBuilder incomingChat = createChatPacket("134", true);
    incomingChat.ofType(Type.normal);
    processServerMessage(incomingChat.build());
    assertNotNull(listener.getNewChat());
}
 
Example #14
Source File: QueryArchiveTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void checkMamQueryResults() throws Exception {
    Message message = StanzaBuilder.buildMessage("iasd207")
            .from("[email protected]")
            .to("[email protected]/pda")
            .build();

    GregorianCalendar calendar = new GregorianCalendar(2002, 10 - 1, 13, 23, 58, 37);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date date = calendar.getTime();

    DelayInformation delay = new DelayInformation(date);
    Message forwardedMessage = StanzaBuilder.buildMessage("162BEBB1-F6DB-4D9A-9BD8-CFDCC801A0B2")
                    .from(JidCreate.from("[email protected]/firstwitch"))
                    .ofType(Type.chat)
                    .setBody("Thrice the brinded cat hath mew.")
                    .build();

    Forwarded forwarded = new Forwarded(delay, forwardedMessage);

    message.addExtension(new MamResultExtension("g27", "34482-21985-73620", forwarded));

    assertEquals(mamQueryResultExample, message.toXML(StreamOpen.CLIENT_NAMESPACE).toString());

    MamResultExtension mamResultExtension = MamResultExtension.from(message);

    assertEquals(mamResultExtension.getId(), "34482-21985-73620");
    assertEquals(mamResultExtension.getForwarded().getDelayInformation().getStamp(), date);

    Message resultMessage = (Message) mamResultExtension.getForwarded().getForwardedStanza();
    assertEquals(resultMessage.getFrom(), JidCreate.from("[email protected]/firstwitch"));
    assertEquals(resultMessage.getStanzaId(), "162BEBB1-F6DB-4D9A-9BD8-CFDCC801A0B2");
    assertEquals(resultMessage.getType(), Type.chat);
    assertEquals(resultMessage.getBody(), "Thrice the brinded cat hath mew.");
}
 
Example #15
Source File: QueryArchiveTest.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Test
public void checkMamQueryIQ() throws Exception {
    DataForm dataForm = getNewMamForm();
    MamQueryIQ mamQueryIQ = new MamQueryIQ(queryId, dataForm);
    mamQueryIQ.setType(IQ.Type.set);
    mamQueryIQ.setStanzaId("sarasa");
    assertEquals(mamQueryIQ.toXML(StreamOpen.CLIENT_NAMESPACE).toString(), mamSimpleQueryIQ);
}
 
Example #16
Source File: ChatConnectionTest.java    From Smack with Apache License 2.0 4 votes vote down vote up
@Test
public void validateMessageTypeWithDefaults4() {
    MessageBuilder incomingChat = createChatPacket("134", true);
    incomingChat.ofType(Type.headline);
    assertNull(listener.getNewChat());
}
 
Example #17
Source File: ChatService.java    From xyTalk-pc with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void messageProcess(Stanza stanza) {
	// 消息包
	org.jivesoftware.smack.packet.Message message = (org.jivesoftware.smack.packet.Message) stanza;
	if (message.getType() == org.jivesoftware.smack.packet.Message.Type.chat) {// 单聊
		if (message.getBody() == null && message.getExtensions()==null) {
			DebugUtil.debug("(抛弃的)processPacket-Message.Type.chat:" + message.toXML());
		} else {
			boolean mucInvition = message.getExtension("x", MucInvitation.NAMESPACE) != null;
			boolean mucKick = message.getExtension("x", MucKick.NAMESPACE) != null;
			boolean mucUpdateMembers = message.getExtension("x", MucUpdateMembers.NAMESPACE) != null;
			boolean offlineFile = message.getExtension("x", OfflineFile.NAMESPACE) != null;
			boolean processed = false;
			if (mucInvition) {// 群组邀请消息-离线,加入群
				MucChatService.join(message);
				DebugUtil.debug("(离线群邀请)processPacket-Message.Type.chat:" + message.toXML());
				processed = true;
			}

			if (mucKick) {// 被踢出群
				MucChatService.kickMe(message);
				DebugUtil.debug("(群踢人)processPacket-Message.Type.chat:" + message.toXML());
				processed = true;
			}

			if (mucUpdateMembers) {// 更新群成员
				MucChatService.updateMembers(message);
				DebugUtil.debug("(更新群成员)processPacket-Message.Type.chat:" + message.toXML());
				processed = true;
			}

			if (offlineFile) {// 离线文件消息抵达,需要向离线机器人发送请求
				XmppFileService.requestOfflineFile(message);
				DebugUtil.debug("(离线文件)processPacket-Message.Type.chat:" + message.toXML());
				processed = true;
			}

			if (!processed){
				ChatService.recivePacket(message);
				DebugUtil.debug("(单聊)chat:" + message.toString());
			}

		}

	}
	if (message.getType() == org.jivesoftware.smack.packet.Message.Type.headline) {// 重要消息
		HeadlineChatService.recivePacket(message);
		DebugUtil.debug("processPacket-Message.Type.headline:" + message.toXML());
	}
	if (message.getType() == org.jivesoftware.smack.packet.Message.Type.normal) {
		// 可能是普通消息,也可能是回执消息,也可能是扩展消息
		if (message.hasExtension("urn:xmpp:receipts")) {
			// 回执消息
			ChatService.receiptArrived(message);
			DebugUtil.debug("urn:xmpp:receipts:" + message.toXML());
		} else if (message.hasExtension("urn:xmpp:attention:0")) {
			// 震动提醒消息
			DebugUtil.debug("urn:xmpp:attention:0:" + message.toXML());
		} else if (message.hasExtension("http://jabber.org/protocol/muc#user")) {
			// 被邀请加入群聊
			DebugUtil.debug("被邀请加入群聊:" + message.getFrom().toString());
		} else if (message.hasExtension("jabber:x:conference")) {
			// 被邀请加入群聊
			DebugUtil.debug("被邀请加入群聊:" + message.getFrom().toString());
		} else {
			if (message.getBody() == null) {
				DebugUtil.debug("(抛弃的)processPacket-Message.Type.normal:" + message.toXML());
			} else {
				ChatService.recivePacket(message);
				DebugUtil.debug("processPacket-Message.Type.normal:" + message.toXML());
			}
		}
	}
	if (message.getType() == org.jivesoftware.smack.packet.Message.Type.groupchat) {// 表示群聊
		DebugUtil.debug("processPacket-Message.Type.groupchat:" + message.toXML());
		MucChatService.recivePacket(message);
	}
	if (message.getType() == org.jivesoftware.smack.packet.Message.Type.error) {// 表示错误信息
		DebugUtil.debug("processPacket-Message.Type.error:" + message.toXML());
		ErrorMsgService.recivePacket(message);
	}

}
 
Example #18
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 #19
Source File: BroadcastPlugin.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
    * Show Server Alert.
    *
    * @param message the message to show.
    * @param type
    */
   private void showAlert(Message message) {
Type type = message.getType();
       // Do not show alert if the message is an error.
       if (message.getError() != null) {
           return;
       }

       final String body = message.getBody();
       String subject = message.getSubject();

       StringBuilder buf = new StringBuilder();
       if (subject != null) {
           buf.append(Res.getString("subject")).append(": ").append(subject);
           buf.append("\n\n");
       }

       buf.append(body);

       Jid from = message.getFrom();

       final TranscriptWindow window = new TranscriptWindow();
       window.insertNotificationMessage(buf.toString(), ChatManager.TO_COLOR);

       JPanel p = new JPanel();
       p.setLayout(new BorderLayout());
       p.add(window, BorderLayout.CENTER);
       p.setBorder(BorderFactory.createLineBorder(Color.lightGray));

       // Count the number of linebreaks <br> and \n

       String s = message.getBody();
       s = s.replace("<br/>", "\n");
       s = s.replace("<br>", "\n");
       int linebreaks = org.jivesoftware.spark.util.StringUtils.
       countNumberOfOccurences(s,'\n');

       // Currently Serverbroadcasts dont contain Subjects, so this might be a MOTD message
       boolean mightbeMOTD = message.getSubject()!=null;

if (!from.hasLocalpart()) {
    // if theres no "@" it means the message came from the server
    if (Default.getBoolean(Default.BROADCAST_IN_CHATWINDOW)
	    || linebreaks > 20 || message.getBody().length() > 1000 || mightbeMOTD) {
	// if we have more than 20 linebreaks or the message is longer
	// than 1000characters we should broadcast
	// in a normal chatwindow
	broadcastInChat(message);
    } else {
	broadcastWithPanel(message);
    }

}
       else if (message.getFrom() != null) {
           userToUserBroadcast(message, type, from);
       }
   }
 
Example #20
Source File: MessageTypeFilter.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new message type filter using the specified message type.
 *
 * @param type the message type.
 */
private MessageTypeFilter(Message.Type type) {
    super(Message.class);
    this.type = type;
}