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);
}
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();
}
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;
}
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);
}
}
}
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);
}
}
}
Aggregations