Search in sources :

Example 31 with MessageBuilder

use of org.jivesoftware.smack.packet.MessageBuilder in project Smack by igniterealtime.

the class ChatConnectionTest method validateMessageTypeWithNoNormal1.

@Test
public void validateMessageTypeWithNoNormal1() {
    cm.setNormalIncluded(false);
    MessageBuilder incomingChat = createChatPacket("134", true);
    incomingChat.ofType(Message.Type.chat);
    processServerMessage(incomingChat.build());
    assertNotNull(listener.getNewChat());
}
Also used : MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) Test(org.junit.Test)

Example 32 with MessageBuilder

use of org.jivesoftware.smack.packet.MessageBuilder in project Smack by igniterealtime.

the class MultiUserChat method changeSubject.

/**
 * Changes the subject within the room. As a default, only users with a role of "moderator"
 * are allowed to change the subject in a room. Although some rooms may be configured to
 * allow a mere participant or even a visitor to change the subject.
 *
 * @param subject the new room's subject to set.
 * @throws XMPPErrorException if someone without appropriate privileges attempts to change the
 *          room subject will throw an error with code 403 (i.e. Forbidden)
 * @throws NoResponseException if there was no response from the server.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void changeSubject(final String subject) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    MessageBuilder message = buildMessage();
    message.setSubject(subject);
    // Wait for an error or confirmation message back from the server.
    StanzaFilter responseFilter = new AndFilter(fromRoomGroupchatFilter, new StanzaFilter() {

        @Override
        public boolean accept(Stanza packet) {
            Message msg = (Message) packet;
            return subject.equals(msg.getSubject());
        }
    });
    StanzaCollector response = connection.createStanzaCollectorAndSend(responseFilter, message.build());
    // Wait up to a certain number of seconds for a reply.
    response.nextResultOrThrow();
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) Message(org.jivesoftware.smack.packet.Message) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 33 with MessageBuilder

use of org.jivesoftware.smack.packet.MessageBuilder in project Smack by igniterealtime.

the class OmemoClient method handleInput.

public void handleInput(String input) throws NotConnectedException, NotLoggedInException, InterruptedException, IOException {
    String[] com = input.split(" ", 3);
    switch(com[0]) {
        case "/omemo":
            if (com.length < 3) {
                print("Usage: /omemo <contact-jid> <message>");
                return;
            }
            BareJid recipient = JidCreate.bareFrom(com[1]);
            String body = com[2];
            MessageBuilder messageBuilder = connection.getStanzaFactory().buildMessageStanza();
            try {
                Message omemoMessage = omemoManager.encrypt(recipient, body).buildMessage(messageBuilder, recipient);
                connection.sendStanza(omemoMessage);
            } catch (UndecidedOmemoIdentityException e) {
                print("Undecided Identities!\n" + Arrays.toString(e.getUndecidedDevices().toArray()));
            } catch (CryptoFailedException | SmackException.NoResponseException e) {
                LOGGER.log(Level.SEVERE, "Unexpected Exception", e);
            }
            break;
        case "/trust":
            print("Trust");
            if (com.length != 2) {
                print("Usage: /trust <contact-jid>");
            }
            BareJid contact = JidCreate.bareFrom(com[1]);
            HashMap<OmemoDevice, OmemoFingerprint> devices;
            try {
                devices = omemoManager.getActiveFingerprints(contact);
            } catch (CorruptedOmemoKeyException | CannotEstablishOmemoSessionException | SmackException.NoResponseException e) {
                LOGGER.log(Level.SEVERE, "Unexpected Exception", e);
                return;
            }
            for (OmemoDevice d : devices.keySet()) {
                print("Trust (1) or distrust (2)?\n" + devices.get(d).blocksOf8Chars());
                if (Integer.parseInt(scanner.nextLine()) == 1) {
                    omemoManager.trustOmemoIdentity(d, devices.get(d));
                } else {
                    omemoManager.distrustOmemoIdentity(d, devices.get(d));
                }
            }
            print("Done.");
            break;
        case "/purge":
            try {
                omemoManager.purgeDeviceList();
                print("Purged.");
            } catch (XMPPException.XMPPErrorException | SmackException.NoResponseException | PubSubException.NotALeafNodeException e) {
                LOGGER.log(Level.SEVERE, "Unexpected Exception", e);
            }
    }
}
Also used : CryptoFailedException(org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException) UndecidedOmemoIdentityException(org.jivesoftware.smackx.omemo.exceptions.UndecidedOmemoIdentityException) OmemoMessage(org.jivesoftware.smackx.omemo.OmemoMessage) Message(org.jivesoftware.smack.packet.Message) OmemoDevice(org.jivesoftware.smackx.omemo.internal.OmemoDevice) EntityBareJid(org.jxmpp.jid.EntityBareJid) BareJid(org.jxmpp.jid.BareJid) CorruptedOmemoKeyException(org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException) CannotEstablishOmemoSessionException(org.jivesoftware.smackx.omemo.exceptions.CannotEstablishOmemoSessionException) MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) OmemoFingerprint(org.jivesoftware.smackx.omemo.trust.OmemoFingerprint)

Example 34 with MessageBuilder

use of org.jivesoftware.smack.packet.MessageBuilder in project Smack by igniterealtime.

the class OXInstantMessagingManager method sendOxMessage.

/**
 * 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 EncryptionResult} containing metadata 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 EncryptionResult 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());
    EncryptionResult metadata = addOxMessage(messageBuilder, contact, Collections.<ExtensionElement>singletonList(mBody));
    Message message = messageBuilder.build();
    ChatManager.getInstanceFor(connection()).chatWith(contact.getJid().asEntityBareJidIfPossible()).send(message);
    return metadata;
}
Also used : MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) OpenPgpMessage(org.jivesoftware.smackx.ox.OpenPgpMessage) Message(org.jivesoftware.smack.packet.Message) EncryptionResult(org.pgpainless.encryption_signing.EncryptionResult)

Aggregations

MessageBuilder (org.jivesoftware.smack.packet.MessageBuilder)34 Message (org.jivesoftware.smack.packet.Message)10 XMPPConnection (org.jivesoftware.smack.XMPPConnection)9 Test (org.junit.jupiter.api.Test)8 Test (org.junit.Test)7 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)5 Stanza (org.jivesoftware.smack.packet.Stanza)3 RosterExchange (org.jivesoftware.smackx.xroster.packet.RosterExchange)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 Date (java.util.Date)2 DummyConnection (org.jivesoftware.smack.DummyConnection)2 AndFilter (org.jivesoftware.smack.filter.AndFilter)2 XmlElement (org.jivesoftware.smack.packet.XmlElement)2 OpenPgpMessage (org.jivesoftware.smackx.ox.OpenPgpMessage)2 EntityBareJid (org.jxmpp.jid.EntityBareJid)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1