use of org.jivesoftware.smackx.muc.MultiUserChat in project Zom-Android by zom.
the class XmppConnection method initConnectionAndLogin.
private void initConnectionAndLogin(Imps.ProviderSettings.QueryMap providerSettings, String userName) throws InterruptedException, IOException, SmackException, XMPPException, KeyManagementException, NoSuchAlgorithmException, IllegalStateException, RuntimeException {
// Roster.SubscriptionMode.accept_all;//load this from a preference
Roster.SubscriptionMode subMode = Roster.SubscriptionMode.manual;
// only activates if Debug TRUE is set, so you can leave this in!
Debug.onConnectionStart();
initConnection(providerSettings, userName);
if (mConnection.isConnected() && mConnection.isSecureConnection() && (!mConnection.isAuthenticated())) {
mResource = providerSettings.getXmppResource();
mRoster = Roster.getInstanceFor(mConnection);
mRoster.setRosterLoadedAtLogin(true);
mRoster.setSubscriptionMode(subMode);
getContactListManager().listenToRoster(mRoster);
ReconnectionManager manager = ReconnectionManager.getInstanceFor(mConnection);
manager.disableAutomaticReconnection();
mChatManager = ChatManager.getInstanceFor(mConnection);
mPingManager = PingManager.getInstanceFor(mConnection);
mPingManager.setPingInterval(PING_INTERVAL);
if (mUser == null)
mUser = makeUser(providerSettings, mContext.getContentResolver());
try {
getOmemo();
} catch (Exception e) {
debug(TAG, "error init'ing omemo during connectoin", e);
}
MultiUserChatManager.getInstanceFor(mConnection).addInvitationListener(new InvitationListener() {
@Override
public void invitationReceived(XMPPConnection xmppConnection, MultiUserChat muc, EntityJid entityJid, String reason, String password, org.jivesoftware.smack.packet.Message message, MUCUser.Invite invite) {
try {
getChatGroupManager().acceptInvitationAsync(invite.getFrom().toString());
XmppAddress xa = new XmppAddress(muc.getRoom().toString());
mChatGroupManager.joinChatGroupAsync(xa, reason);
ChatSession session = mSessionManager.findSession(muc.getRoom());
// create a session
if (session == null) {
ImEntity participant = findOrCreateParticipant(xa.getAddress(), true);
if (participant != null)
session = mSessionManager.createChatSession(participant, false);
if (session != null)
((ChatGroup) session.getParticipant()).setName(muc.getSubject());
}
} catch (Exception se) {
Log.e(TAG, "error accepting invite", se);
}
}
});
mConnection.login(mUsername, mPassword, Resourcepart.from(mResource));
mStreamHandler.notifyInitialLogin();
initServiceDiscovery();
getContactListManager().loadContactListsAsync();
execute(new Runnable() {
public void run() {
sendPresencePacket();
sendVCard();
}
});
} else {
// throw some meaningful error message here
throw new SmackException("Unable to securely conenct to server");
}
}
use of org.jivesoftware.smackx.muc.MultiUserChat in project Zom-Android by zom.
the class XmppConnection method findOrCreateSession.
private ChatSession findOrCreateSession(String address, boolean groupChat) {
try {
if (mConnection == null || (!mConnection.isConnected()))
return null;
ChatSession session = mSessionManager.findSession(JidCreate.bareFrom(address));
BareJid bareJid = JidCreate.bareFrom(address);
if (session == null) {
if (!groupChat) {
ImEntity participant = findOrCreateParticipant(address, groupChat);
if (participant != null) {
session = mSessionManager.createChatSession(participant, false);
mContactListManager.refreshPresence(address);
qAvatar.put(bareJid.toString(), "");
if (getOmemo().getManager().contactSupportsOmemo(bareJid)) {
getOmemo().getManager().requestDeviceListUpdateFor(bareJid);
getOmemo().getManager().buildSessionsWith(bareJid);
}
}
} else {
XmppAddress xAddr = new XmppAddress(address);
mChatGroupManager.joinChatGroupAsync(xAddr, null);
MultiUserChat muc = mChatGroupManager.getMultiUserChat(xAddr.getBareAddress());
session = mSessionManager.findSession(muc.getRoom());
// create a session
if (session == null) {
ImEntity participant = findOrCreateParticipant(xAddr.getAddress(), true);
if (participant != null)
session = mSessionManager.createChatSession(participant, false);
if (session != null)
((ChatGroup) session.getParticipant()).setName(muc.getSubject());
}
}
}
return session;
} catch (Exception e) {
debug(ImApp.LOG_TAG, "error findOrCreateSession", e);
return null;
}
}
use of org.jivesoftware.smackx.muc.MultiUserChat in project Zom-Android by zom.
the class XmppConnection method initOmemo.
private synchronized Omemo initOmemo(XMPPTCPConnection conn) throws Exception {
if (conn != null && conn.isConnected()) {
mOmemoInstance = new Omemo(conn, mUserJid);
mOmemoInstance.getManager().addOmemoMessageListener(new OmemoMessageListener() {
@Override
public void onOmemoKeyTransportReceived(CipherAndAuthTag cipherAndAuthTag, org.jivesoftware.smack.packet.Message message, org.jivesoftware.smack.packet.Message message1, OmemoMessageInformation omemoMessageInformation) {
debug(TAG, "omemo key transport received");
}
@Override
public void onOmemoMessageReceived(String body, org.jivesoftware.smack.packet.Message message, org.jivesoftware.smack.packet.Message message1, OmemoMessageInformation omemoMessageInformation) {
if (body != null) {
debug(TAG, "got inbound message omemo: from:" + message.getFrom() + "=" + message.getBody());
message.setBody(body);
handleMessage(message, true, true);
} else {
debug(TAG, "got empty ibound message omemo: from:" + message.getFrom().toString());
}
}
});
mOmemoInstance.getManager().addOmemoMucMessageListener(new OmemoMucMessageListener() {
@Override
public void onOmemoMucMessageReceived(MultiUserChat muc, BareJid from, String decryptedBody, org.jivesoftware.smack.packet.Message message, org.jivesoftware.smack.packet.Message wrappingMessage, OmemoMessageInformation omemoInformation) {
if (decryptedBody != null) {
debug(TAG, "got inbound MUC message omemo: from:" + message.getFrom() + "=" + message.getBody());
org.jivesoftware.smack.packet.Message messagePlain = new org.jivesoftware.smack.packet.Message();
messagePlain.setType(message.getType());
messagePlain.setFrom(message.getFrom());
messagePlain.setTo(message.getTo());
messagePlain.setBody(decryptedBody);
messagePlain.setStanzaId(message.getStanzaId());
messagePlain.addExtensions(message.getExtensions());
messagePlain.setThread(message.getThread());
messagePlain.setSubject(message.getSubject());
handleMessage(messagePlain, true, true);
} else {
debug(TAG, "got empty ibound MUC message omemo: from:" + message.getFrom().toString());
}
}
@Override
public void onOmemoKeyTransportReceived(MultiUserChat muc, BareJid from, CipherAndAuthTag cipherAndAuthTag, org.jivesoftware.smack.packet.Message message, org.jivesoftware.smack.packet.Message wrappingMessage, OmemoMessageInformation omemoInformation) {
debug(TAG, "got OmemoKey Transport from: " + from.toString());
}
});
addOmemoListener(mOmemoInstance.getManager());
}
return mOmemoInstance;
}
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 AccountJid account, final EntityBareJid room, boolean requested) {
final RoomChat roomChat;
final Resourcepart nickname;
final String password;
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);
AccountItem accountItem = AccountManager.getInstance().getAccount(account);
if (accountItem == null) {
return;
}
final MultiUserChat multiUserChat;
try {
multiUserChat = MultiUserChatManager.getInstanceFor(accountItem.getConnection()).getMultiUserChat(room);
} catch (IllegalStateException e) {
Application.getInstance().onError(R.string.NOT_CONNECTED);
return;
}
roomChat.setState(RoomState.joining);
roomChat.setMultiUserChat(multiUserChat);
roomChat.setRequested(requested);
Application.getInstance().runInBackgroundUserRequest(new Runnable() {
@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);
try {
RosterManager.onContactChanged(account, UserJid.from(room));
VCardManager.getInstance().request(account, room);
} catch (UserJid.UserJidCreateException e) {
LogManager.exception(this, e);
}
}
});
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);
}
try {
RosterManager.onContactChanged(account, UserJid.from(room));
} catch (UserJid.UserJidCreateException e) {
LogManager.exception(this, e);
}
}
});
return;
} 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);
try {
RosterManager.onContactChanged(account, UserJid.from(room));
} catch (UserJid.UserJidCreateException e) {
LogManager.exception(this, e);
}
}
});
}
});
}
use of org.jivesoftware.smackx.muc.MultiUserChat in project ecf by eclipse.
the class XMPPChatRoomContainer method connect.
public void connect(ID remote, IConnectContext connectContext) throws ContainerConnectException {
if (!(remote instanceof XMPPRoomID)) {
throw new ContainerConnectException(NLS.bind(Messages.XMPPChatRoomContainer_Exception_Connect_Wrong_Type, remote));
}
final XMPPRoomID roomID = (XMPPRoomID) remote;
fireContainerEvent(new ContainerConnectingEvent(this.getID(), remote, connectContext));
synchronized (getConnectLock()) {
try {
connectionState = CONNECTING;
remoteServerID = null;
addSharedObjectToContainer(remote);
multiuserchat = new MultiUserChat(getXMPPConnection(), roomID.getMucString());
// Get nickname from join context
String nick = null;
try {
final Callback[] callbacks = new Callback[1];
callbacks[0] = new NameCallback(Messages.XMPPChatRoomContainer_NAME_CALLBACK_NICK, roomID.getNickname());
if (connectContext != null) {
final CallbackHandler handler = connectContext.getCallbackHandler();
if (handler != null) {
handler.handle(callbacks);
}
}
if (callbacks[0] instanceof NameCallback) {
final NameCallback cb = (NameCallback) callbacks[0];
nick = cb.getName();
}
} catch (final Exception e) {
throw new ContainerConnectException(Messages.XMPPChatRoomContainer_EXCEPTION_CALLBACKHANDLER, e);
}
String nickname = null;
if (// $NON-NLS-1$
nick == null || nick.equals(""))
nickname = roomID.getNickname();
else
nickname = nick;
multiuserchat.addSubjectUpdatedListener(new SubjectUpdatedListener() {
public void subjectUpdated(String subject, String from) {
fireSubjectUpdated(subject, from);
}
});
multiuserchat.addMessageListener(new PacketListener() {
public void processPacket(Packet arg0) {
handleXMPPMessage(arg0);
}
});
multiuserchat.addParticipantListener(new PacketListener() {
public void processPacket(Packet arg0) {
handleXMPPMessage(arg0);
}
});
multiuserchat.addParticipantStatusListener(new ParticipantStatusListener() {
public void joined(String arg0) {
handleChatMembershipEvent(arg0, true);
}
public void left(String arg0) {
handleChatMembershipEvent(arg0, false);
}
public void voiceGranted(String arg0) {
// TODO Auto-generated method stub
// $NON-NLS-1$ //$NON-NLS-2$
System.out.println("voiceGranted(" + arg0 + ")");
}
public void voiceRevoked(String arg0) {
// TODO Auto-generated method stub
// $NON-NLS-1$ //$NON-NLS-2$
System.out.println("voiceRevoked(" + arg0 + ")");
}
public void membershipGranted(String arg0) {
// TODO Auto-generated method stub
System.out.println(// $NON-NLS-1$
"membershipGranted(" + arg0 + // $NON-NLS-1$
")");
}
public void membershipRevoked(String arg0) {
// TODO Auto-generated method stub
System.out.println(// $NON-NLS-1$
"membershipRevoked(" + arg0 + // $NON-NLS-1$
")");
}
public void moderatorGranted(String arg0) {
// TODO Auto-generated method stub
System.out.println(// $NON-NLS-1$
"moderatorGranted(" + arg0 + // $NON-NLS-1$
")");
}
public void moderatorRevoked(String arg0) {
// TODO Auto-generated method stub
System.out.println(// $NON-NLS-1$
"moderatorRevoked(" + arg0 + // $NON-NLS-1$
")");
}
public void ownershipGranted(String arg0) {
// TODO Auto-generated method stub
System.out.println(// $NON-NLS-1$
"ownershipGranted(" + arg0 + // $NON-NLS-1$
")");
}
public void ownershipRevoked(String arg0) {
// TODO Auto-generated method stub
System.out.println(// $NON-NLS-1$
"ownershipRevoked(" + arg0 + // $NON-NLS-1$
")");
}
public void adminGranted(String arg0) {
// TODO Auto-generated method stub
// $NON-NLS-1$ //$NON-NLS-2$
System.out.println("adminGranted(" + arg0 + ")");
}
public void adminRevoked(String arg0) {
// TODO Auto-generated method stub
// $NON-NLS-1$ //$NON-NLS-2$
System.out.println("adminRevoked(" + arg0 + ")");
}
public void kicked(String arg0, String arg1, String arg2) {
// TODO Auto-generated method stub
System.out.println(// $NON-NLS-1$ //$NON-NLS-2$
"kicked(" + arg0 + "," + arg1 + "," + arg2 + // $NON-NLS-1$ //$NON-NLS-2$
")");
}
public void banned(String arg0, String arg1, String arg2) {
// TODO Auto-generated method stub
System.out.println(// $NON-NLS-1$ //$NON-NLS-2$
"banned(" + arg0 + "," + arg1 + "," + arg2 + // $NON-NLS-1$ //$NON-NLS-2$
")");
}
public void nicknameChanged(String arg0, String arg1) {
// TODO Auto-generated method stub
System.out.println(// $NON-NLS-1$
"nicknameChanged(" + arg0 + "," + arg1 + // $NON-NLS-1$ //$NON-NLS-2$
")");
}
});
multiuserchat.addInvitationRejectionListener(new InvitationRejectionListener() {
public void invitationDeclined(String arg0, String arg1) {
// TODO Auto-generated method stub
System.out.println(// $NON-NLS-1$
"invitationDeclined(" + arg0 + "," + arg1 + // $NON-NLS-1$ //$NON-NLS-2$
")");
}
});
multiuserchat.join(nickname);
connectionState = CONNECTED;
remoteServerID = roomID;
containerHelper.setRoomID(remoteServerID);
fireContainerEvent(new ContainerConnectedEvent(this.getID(), roomID));
} catch (final Exception e) {
cleanUpConnectFail();
final ContainerConnectException ce = new ContainerConnectException(NLS.bind(Messages.XMPPChatRoomContainer_EXCEPTION_JOINING_ROOM, roomID));
ce.setStackTrace(e.getStackTrace());
throw ce;
}
}
}
Aggregations