Search in sources :

Example 11 with MultiUserChat

use of org.jivesoftware.smackx.muc.MultiUserChat in project xabber-android by redsolution.

the class MUCManager method leaveRoom.

public void leaveRoom(String account, String room) {
    final MultiUserChat multiUserChat;
    RoomChat roomChat = getRoomChat(account, room);
    if (roomChat == null) {
        return;
    }
    multiUserChat = roomChat.getMultiUserChat();
    roomChat.setState(RoomState.unavailable);
    roomChat.setRequested(false);
    roomChat.newAction(roomChat.getNickname(), null, ChatAction.leave);
    requestToWriteRoom(account, room, roomChat.getNickname(), roomChat.getPassword(), false);
    if (multiUserChat != null) {
        Thread thread = new Thread("Leave to room " + room + " from " + account) {

            @Override
            public void run() {
                try {
                    multiUserChat.leave();
                } catch (SmackException.NotConnectedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.setDaemon(true);
        thread.start();
    }
    RosterManager.getInstance().onContactChanged(account, room);
}
Also used : MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) SmackException(org.jivesoftware.smack.SmackException) ConnectionThread(com.xabber.android.data.connection.ConnectionThread)

Example 12 with MultiUserChat

use of org.jivesoftware.smackx.muc.MultiUserChat in project xabber-android by redsolution.

the class MUCManager method joinRoom.

/**
     * Requests to join to the room.
     *
     * @param requested Whether user request to join the room.
     */
public void joinRoom(final String account, final String room, boolean requested) {
    final XMPPConnection xmppConnection;
    final RoomChat roomChat;
    final String nickname;
    final String password;
    final Thread thread;
    roomChat = getRoomChat(account, room);
    if (roomChat == null) {
        Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
        return;
    }
    RoomState state = roomChat.getState();
    if (state == RoomState.available || state == RoomState.occupation) {
        Application.getInstance().onError(R.string.ALREADY_JOINED);
        return;
    }
    if (state == RoomState.creating || state == RoomState.joining) {
        Application.getInstance().onError(R.string.ALREADY_IN_PROGRESS);
        return;
    }
    nickname = roomChat.getNickname();
    password = roomChat.getPassword();
    requestToWriteRoom(account, room, nickname, password, true);
    ConnectionThread connectionThread = AccountManager.getInstance().getAccount(account).getConnectionThread();
    if (connectionThread == null) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    xmppConnection = connectionThread.getXMPPConnection();
    if (xmppConnection == null) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    final MultiUserChat multiUserChat;
    try {
        multiUserChat = MultiUserChatManager.getInstanceFor(xmppConnection).getMultiUserChat(room);
    } catch (IllegalStateException e) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
        return;
    }
    roomChat.setState(RoomState.joining);
    roomChat.setMultiUserChat(multiUserChat);
    roomChat.setRequested(requested);
    thread = new Thread("Join to room " + room + " from " + account) {

        @Override
        public void run() {
            try {
                if (roomChat.getMultiUserChat() != multiUserChat) {
                    return;
                }
                multiUserChat.join(nickname, password);
                Application.getInstance().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (roomChat.getMultiUserChat() != multiUserChat) {
                            return;
                        }
                        if (roomChat.getState() == RoomState.joining) {
                            roomChat.setState(RoomState.occupation);
                        }
                        removeAuthorizationError(account, room);
                        RosterManager.getInstance().onContactChanged(account, room);
                    }
                });
                return;
            } catch (final XMPPException.XMPPErrorException e) {
                Application.getInstance().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (roomChat.getMultiUserChat() != multiUserChat) {
                            return;
                        }
                        roomChat.setState(RoomState.error);
                        addAuthorizationError(account, room);
                        XMPPError xmppError = e.getXMPPError();
                        if (xmppError != null && xmppError.getCondition() == XMPPError.Condition.conflict) {
                            Application.getInstance().onError(R.string.NICK_ALREADY_USED);
                        } else if (xmppError != null && xmppError.getCondition() == XMPPError.Condition.not_authorized) {
                            Application.getInstance().onError(R.string.AUTHENTICATION_FAILED);
                        } else {
                            Application.getInstance().onError(R.string.NOT_CONNECTED);
                        }
                        RosterManager.getInstance().onContactChanged(account, room);
                    }
                });
                return;
            } catch (IllegalStateException e) {
            } catch (Exception e) {
                LogManager.exception(this, e);
            }
            Application.getInstance().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (roomChat.getMultiUserChat() != multiUserChat) {
                        return;
                    }
                    roomChat.setState(RoomState.waiting);
                    Application.getInstance().onError(R.string.NOT_CONNECTED);
                    RosterManager.getInstance().onContactChanged(account, room);
                }
            });
        }
    };
    thread.setDaemon(true);
    thread.start();
}
Also used : MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPError(org.jivesoftware.smack.packet.XMPPError) SmackException(org.jivesoftware.smack.SmackException) NetworkException(com.xabber.android.data.NetworkException) XMPPException(org.jivesoftware.smack.XMPPException) ConnectionThread(com.xabber.android.data.connection.ConnectionThread) ConnectionThread(com.xabber.android.data.connection.ConnectionThread)

Example 13 with MultiUserChat

use of org.jivesoftware.smackx.muc.MultiUserChat in project openhab1-addons by openhab.

the class XMPP method chatXMPP.

/**
     * Sends a message to an XMPP multi user chat.
     * 
     * @param message the message to send
     * 
     * @return <code>true</code>, if sending the message has been successful and
     *         <code>false</code> in all other cases.
     */
@ActionDoc(text = "Sends a message to an XMPP multi user chat.")
public static boolean chatXMPP(@ParamDoc(name = "message") String message) {
    boolean success = false;
    try {
        MultiUserChat chat = XMPPConnect.getChat();
        try {
            while (message.length() >= 2000) {
                chat.sendMessage(message.substring(0, 2000));
                message = message.substring(2000);
            }
            chat.sendMessage(message);
            logger.debug("Sent message '{}' to multi user chat.", message);
            success = true;
        } catch (XMPPException e) {
            logger.warn("Error Delivering block", e);
        } catch (NotConnectedException e) {
            logger.warn("Error Delivering block", e);
        }
    } catch (NotInitializedException e) {
        logger.warn("Could not send XMPP message as connection is not correctly initialized!");
    }
    return success;
}
Also used : MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) XMPPException(org.jivesoftware.smack.XMPPException) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Example 14 with MultiUserChat

use of org.jivesoftware.smackx.muc.MultiUserChat in project Payara by payara.

the class XmppNotificationRunnable method run.

@Override
public void run() {
    while (queue.size() > 0) {
        try {
            XmppMessage xmppMessage = queue.getMessage();
            MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
            MultiUserChat multiUserChat = manager.getMultiUserChat(executionOptions.getRoomId() + "@" + executionOptions.getServiceName());
            if (multiUserChat != null) {
                if (!multiUserChat.isJoined()) {
                    multiUserChat.join(executionOptions.getUsername(), executionOptions.getPassword());
                }
                Message message = new Message();
                message.setSubject(xmppMessage.getSubject());
                message.setBody(xmppMessage.getMessage());
                multiUserChat.sendMessage(message);
            }
            logger.log(Level.FINE, "Message sent successfully");
        } catch (XMPPException | SmackException e) {
            logger.log(Level.SEVERE, "Error occurred while sending message to room", e);
        }
    }
}
Also used : MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) MultiUserChatManager(org.jivesoftware.smackx.muc.MultiUserChatManager) Message(org.jivesoftware.smack.packet.Message) SmackException(org.jivesoftware.smack.SmackException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 15 with MultiUserChat

use of org.jivesoftware.smackx.muc.MultiUserChat in project Spark by igniterealtime.

the class ConferenceRoomBrowser method createRoom.

/**
 * Create a new room based on room table selection.
 */
private void createRoom() {
    RoomCreationDialog mucRoomDialog = new RoomCreationDialog();
    final MultiUserChat groupChat = mucRoomDialog.createGroupChat(SparkManager.getMainWindow(), serviceName);
    LocalPreferences pref = SettingsManager.getLocalPreferences();
    if (null != groupChat) {
        // Join Room
        try {
            GroupChatRoom room = UIComponentRegistry.createGroupChatRoom(groupChat);
            groupChat.create(pref.getNickname());
            chatManager.getChatContainer().addChatRoom(room);
            chatManager.getChatContainer().activateChatRoom(room);
            // Send Form
            Form form = groupChat.getConfigurationForm().createAnswerForm();
            if (mucRoomDialog.isPasswordProtected()) {
                String password = mucRoomDialog.getPassword();
                room.setPassword(password);
                form.setAnswer("muc#roomconfig_passwordprotectedroom", true);
                form.setAnswer("muc#roomconfig_roomsecret", password);
            }
            form.setAnswer("muc#roomconfig_roomname", mucRoomDialog.getRoomName());
            form.setAnswer("muc#roomconfig_roomdesc", mucRoomDialog.getRoomTopic());
            if (mucRoomDialog.isPermanent()) {
                form.setAnswer("muc#roomconfig_persistentroom", true);
            }
            List<String> owners = new ArrayList<>();
            owners.add(SparkManager.getSessionManager().getBareAddress());
            form.setAnswer("muc#roomconfig_roomowners", owners);
            // new DataFormDialog(groupChat, form);
            groupChat.sendConfigurationForm(form);
            addRoomToTable(groupChat.getRoom(), XmppStringUtils.parseLocalpart(groupChat.getRoom()), 1);
        } catch (XMPPException | SmackException e1) {
            Log.error("Error creating new room.", e1);
            UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
            JOptionPane.showMessageDialog(this, Res.getString("message.room.creation.error"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) Form(org.jivesoftware.smackx.xdata.Form) SmackException(org.jivesoftware.smack.SmackException) ArrayList(java.util.ArrayList) LocalPreferences(org.jivesoftware.sparkimpl.settings.local.LocalPreferences) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

MultiUserChat (org.jivesoftware.smackx.muc.MultiUserChat)26 SmackException (org.jivesoftware.smack.SmackException)15 XMPPException (org.jivesoftware.smack.XMPPException)15 GroupChatRoom (org.jivesoftware.spark.ui.rooms.GroupChatRoom)6 MultiUserChatManager (org.jivesoftware.smackx.muc.MultiUserChatManager)5 Form (org.jivesoftware.smackx.xdata.Form)5 LocalPreferences (org.jivesoftware.sparkimpl.settings.local.LocalPreferences)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 CryptoFailedException (org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3 MultiUserChatException (org.jivesoftware.smackx.muc.MultiUserChatException)3 BareJid (org.jxmpp.jid.BareJid)3 EntityBareJid (org.jxmpp.jid.EntityBareJid)3 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)3 RemoteException (android.os.RemoteException)2 NetworkException (com.xabber.android.data.NetworkException)2 ConnectionThread (com.xabber.android.data.connection.ConnectionThread)2 UserJid (com.xabber.android.data.entity.UserJid)2