Commit 20306502 authored by Grigory Fedorov's avatar Grigory Fedorov

OTRManager: code formatting.

parent d5d31d71
...@@ -74,22 +74,17 @@ import java.util.concurrent.ThreadFactory; ...@@ -74,22 +74,17 @@ import java.util.concurrent.ThreadFactory;
* @author alexander.ivanov * @author alexander.ivanov
*/ */
public class OTRManager implements OtrEngineHost, OtrEngineListener, public class OTRManager implements OtrEngineHost, OtrEngineListener,
OnLoadListener, OnAccountAddedListener, OnAccountRemovedListener, OnLoadListener, OnAccountAddedListener, OnAccountRemovedListener, OnCloseListener {
OnCloseListener {
private final static OTRManager instance; private final static OTRManager instance;
private static Map<SecurityOtrMode, OtrPolicy> POLICIES; private static Map<SecurityOtrMode, OtrPolicy> POLICIES;
static { static {
POLICIES = new HashMap<SettingsManager.SecurityOtrMode, OtrPolicy>(); POLICIES = new HashMap<>();
POLICIES.put(SecurityOtrMode.disabled, new OtrPolicy( POLICIES.put(SecurityOtrMode.disabled, new OtrPolicy(OtrPolicy.NEVER));
OtrPolicy.NEVER)); POLICIES.put(SecurityOtrMode.manual, new OtrPolicy(OtrPolicy.OTRL_POLICY_MANUAL & ~OtrPolicy.ALLOW_V1));
POLICIES.put(SecurityOtrMode.manual, new OtrPolicy( POLICIES.put(SecurityOtrMode.auto, new OtrPolicy(OtrPolicy.OPPORTUNISTIC & ~OtrPolicy.ALLOW_V1));
OtrPolicy.OTRL_POLICY_MANUAL & ~OtrPolicy.ALLOW_V1)); POLICIES.put(SecurityOtrMode.required, new OtrPolicy(OtrPolicy.OTRL_POLICY_ALWAYS & ~OtrPolicy.ALLOW_V1));
POLICIES.put(SecurityOtrMode.auto, new OtrPolicy(
OtrPolicy.OPPORTUNISTIC & ~OtrPolicy.ALLOW_V1));
POLICIES.put(SecurityOtrMode.required, new OtrPolicy(
OtrPolicy.OTRL_POLICY_ALWAYS & ~OtrPolicy.ALLOW_V1));
} }
static { static {
...@@ -104,8 +99,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -104,8 +99,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
*/ */
private final NestedNestedMaps<String, Boolean> fingerprints; private final NestedNestedMaps<String, Boolean> fingerprints;
/** /**
* Fingerprint of encrypted or encrypted and verified session for user in * Fingerprint of encrypted or encrypted and verified session for user in account.
* account.
*/ */
private final NestedMap<String> actives; private final NestedMap<String> actives;
/** /**
...@@ -122,21 +116,17 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -122,21 +116,17 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
private final ExecutorService keyPairGenerator; private final ExecutorService keyPairGenerator;
private OTRManager() { private OTRManager() {
smRequestProvider = new EntityNotificationProvider<SMRequest>( smRequestProvider = new EntityNotificationProvider<>(R.drawable.ic_stat_ic_help_black);
R.drawable.ic_stat_ic_help_black); smProgressProvider = new EntityNotificationProvider<>(R.drawable.ic_stat_ic_play_circle_fill);
smProgressProvider = new EntityNotificationProvider<SMProgress>(
R.drawable.ic_stat_ic_play_circle_fill);
smProgressProvider.setCanClearNotifications(false); smProgressProvider.setCanClearNotifications(false);
fingerprints = new NestedNestedMaps<String, Boolean>(); fingerprints = new NestedNestedMaps<>();
actives = new NestedMap<String>(); actives = new NestedMap<>();
finished = new NestedMap<Boolean>(); finished = new NestedMap<>();
sessions = new NestedMap<Session>(); sessions = new NestedMap<>();
keyPairGenerator = Executors keyPairGenerator = Executors.newSingleThreadExecutor(new ThreadFactory() {
.newSingleThreadExecutor(new ThreadFactory() {
@Override @Override
public Thread newThread(Runnable runnable) { public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable, Thread thread = new Thread(runnable, "Key pair generator service");
"Key pair generator service");
thread.setPriority(Thread.MIN_PRIORITY); thread.setPriority(Thread.MIN_PRIORITY);
thread.setDaemon(true); thread.setDaemon(true);
return thread; return thread;
...@@ -150,7 +140,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -150,7 +140,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
@Override @Override
public void onLoad() { public void onLoad() {
final NestedNestedMaps<String, Boolean> fingerprints = new NestedNestedMaps<String, Boolean>(); final NestedNestedMaps<String, Boolean> fingerprints = new NestedNestedMaps<>();
Cursor cursor = OTRTable.getInstance().list(); Cursor cursor = OTRTable.getInstance().list();
try { try {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
...@@ -175,28 +165,24 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -175,28 +165,24 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
private void onLoaded(NestedNestedMaps<String, Boolean> fingerprints) { private void onLoaded(NestedNestedMaps<String, Boolean> fingerprints) {
this.fingerprints.addAll(fingerprints); this.fingerprints.addAll(fingerprints);
NotificationManager.getInstance().registerNotificationProvider( NotificationManager.getInstance().registerNotificationProvider(smRequestProvider);
smRequestProvider); NotificationManager.getInstance().registerNotificationProvider(smProgressProvider);
NotificationManager.getInstance().registerNotificationProvider(
smProgressProvider);
} }
private Session getOrCreateSession(String account, String user) { private Session getOrCreateSession(String account, String user) {
Session session = sessions.get(account, user); Session session = sessions.get(account, user);
if (session != null) if (session != null) {
return session; return session;
AccountItem accountItem = AccountManager.getInstance().getAccount( }
account); AccountItem accountItem = AccountManager.getInstance().getAccount(account);
session = new Session(new SessionID(account, user, session = new Session(new SessionID(account, user,
accountItem == null ? "" : accountItem.getConnectionSettings() accountItem == null ? "" : accountItem.getConnectionSettings().getProtocol().toString()), this);
.getProtocol().toString()), this);
session.addOtrEngineListener(this); session.addOtrEngineListener(this);
sessions.put(account, user, session); sessions.put(account, user, session);
return session; return session;
} }
public void startSession(String account, String user) public void startSession(String account, String user) throws NetworkException {
throws NetworkException {
try { try {
getOrCreateSession(account, user).startSession(); getOrCreateSession(account, user).startSession();
} catch (OtrException e) { } catch (OtrException e) {
...@@ -204,8 +190,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -204,8 +190,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
} }
} }
public void refreshSession(String account, String user) public void refreshSession(String account, String user) throws NetworkException {
throws NetworkException {
try { try {
getOrCreateSession(account, user).refreshSession(); getOrCreateSession(account, user).refreshSession();
} catch (OtrException e) { } catch (OtrException e) {
...@@ -214,94 +199,73 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -214,94 +199,73 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
} }
public void endSession(String account, String user) throws NetworkException { public void endSession(String account, String user) throws NetworkException {
LogManager.i(this, "Ending session for " + account);
try { try {
getOrCreateSession(account, user).endSession(); getOrCreateSession(account, user).endSession();
} catch (OtrException e) { } catch (OtrException e) {
throw new NetworkException(R.string.OTR_ERROR, e); throw new NetworkException(R.string.OTR_ERROR, e);
} }
AbstractChat abstractChat = MessageManager.getInstance().getChat( AbstractChat abstractChat = MessageManager.getInstance().getChat(account, user);
account, user); MessageArchiveManager.getInstance().setSaveMode(account, user, abstractChat.getThreadId(), SaveMode.body);
MessageArchiveManager.getInstance().setSaveMode(account, user, SSNManager.getInstance().setSessionOtrMode(account, user, abstractChat.getThreadId(), OtrMode.concede);
abstractChat.getThreadId(), SaveMode.body); LogManager.i(this, "Session ended for " + account);
SSNManager.getInstance().setSessionOtrMode(account, user, }
abstractChat.getThreadId(), OtrMode.concede);
} private void injectMessage(String account, String user, String msg) throws OtrException {
AbstractChat abstractChat = MessageManager.getInstance().getChat(account, user);
private void injectMessage(String account, String user, String msg)
throws OtrException {
AbstractChat abstractChat = MessageManager.getInstance().getChat(
account, user);
try { try {
MessageArchiveManager.getInstance().setSaveMode(account, user, MessageArchiveManager.getInstance().setSaveMode(account, user, abstractChat.getThreadId(), SaveMode.fls);
abstractChat.getThreadId(), SaveMode.fls);
} catch (NetworkException e) { } catch (NetworkException e) {
throw new OtrException(e); throw new OtrException(e);
} }
SSNManager.getInstance().setSessionOtrMode(account, user, SSNManager.getInstance().setSessionOtrMode(account, user, abstractChat.getThreadId(), OtrMode.prefer);
abstractChat.getThreadId(), OtrMode.prefer);
try { try {
ConnectionManager.getInstance().sendPacket( ConnectionManager.getInstance()
abstractChat.getAccount(), .sendPacket(abstractChat.getAccount(), abstractChat.createMessagePacket(msg));
abstractChat.createMessagePacket(msg));
} catch (NetworkException e) { } catch (NetworkException e) {
throw new OtrException(e); throw new OtrException(e);
} }
} }
@Override @Override
public void injectMessage(SessionID sessionID, String msg) public void injectMessage(SessionID sessionID, String msg) throws OtrException {
throws OtrException {
injectMessage(sessionID.getAccountID(), sessionID.getUserID(), msg); injectMessage(sessionID.getAccountID(), sessionID.getUserID(), msg);
} }
/** /**
* Creates new action in specified chat. * Creates new action in specified chat.
*
* @param account
* @param user
* @param text
* @param action
*/ */
private void newAction(String account, String user, String text, private void newAction(String account, String user, String text, ChatAction action) {
ChatAction action) { MessageManager.getInstance().getChat(account, user).newAction(null, text, action);
MessageManager.getInstance().getChat(account, user)
.newAction(null, text, action);
} }
@Override @Override
public void unreadableMessageReceived(SessionID sessionID) public void unreadableMessageReceived(SessionID sessionID) throws OtrException {
throws OtrException { newAction(sessionID.getAccountID(), sessionID.getUserID(), null, ChatAction.otr_unreadable);
newAction(sessionID.getAccountID(), sessionID.getUserID(), null,
ChatAction.otr_unreadable);
} }
@Override @Override
public String getReplyForUnreadableMessage(SessionID sessionID) { public String getReplyForUnreadableMessage(SessionID sessionID) {
return Application.getInstance().getString( return Application.getInstance().getString(R.string.otr_unreadable_message);
R.string.otr_unreadable_message);
} }
@Override @Override
public void unencryptedMessageReceived(SessionID sessionID, String msg) public void unencryptedMessageReceived(SessionID sessionID, String msg) throws OtrException {
throws OtrException {
throw new OtrException(new OTRUnencryptedException(msg)); throw new OtrException(new OTRUnencryptedException(msg));
} }
@Override @Override
public void showError(SessionID sessionID, String error) public void showError(SessionID sessionID, String error) throws OtrException {
throws OtrException { newAction(sessionID.getAccountID(), sessionID.getUserID(), error, ChatAction.otr_error);
newAction(sessionID.getAccountID(), sessionID.getUserID(), error,
ChatAction.otr_error);
} }
@Override @Override
public void smpError(SessionID sessionID, int tlvType, boolean cheated) public void smpError(SessionID sessionID, int tlvType, boolean cheated) throws OtrException {
throws OtrException {
newAction(sessionID.getAccountID(), sessionID.getUserID(), null, newAction(sessionID.getAccountID(), sessionID.getUserID(), null,
cheated ? ChatAction.otr_smp_cheated cheated ? ChatAction.otr_smp_cheated : ChatAction.otr_smp_failed);
: ChatAction.otr_smp_failed); if (cheated) {
if (cheated)
removeSMProgress(sessionID.getAccountID(), sessionID.getUserID()); removeSMProgress(sessionID.getAccountID(), sessionID.getUserID());
}
} }
@Override @Override
...@@ -312,18 +276,14 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -312,18 +276,14 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
@Override @Override
public void finishedSessionMessage(SessionID sessionID, String msgText) throws OtrException { public void finishedSessionMessage(SessionID sessionID, String msgText) throws OtrException {
newAction(sessionID.getAccountID(), sessionID.getUserID(), null, newAction(sessionID.getAccountID(), sessionID.getUserID(), null, ChatAction.otr_finished_session);
ChatAction.otr_finished_session); throw new OtrException(new IllegalStateException(
throw new OtrException(
new IllegalStateException(
"Prevent from null to be returned. Just process it as regular exception.")); "Prevent from null to be returned. Just process it as regular exception."));
} }
@Override @Override
public void requireEncryptedMessage(SessionID sessionID, String msgText) public void requireEncryptedMessage(SessionID sessionID, String msgText) throws OtrException {
throws OtrException { throw new OtrException(new IllegalStateException(
throw new OtrException(
new IllegalStateException(
"Prevent from null to be returned. Just process it as regular exception.")); "Prevent from null to be returned. Just process it as regular exception."));
} }
...@@ -333,11 +293,10 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -333,11 +293,10 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
} }
private KeyPair getLocalKeyPair(String account) throws OtrException { private KeyPair getLocalKeyPair(String account) throws OtrException {
KeyPair keyPair = AccountManager.getInstance().getAccount(account) KeyPair keyPair = AccountManager.getInstance().getAccount(account).getKeyPair();
.getKeyPair(); if (keyPair == null) {
if (keyPair == null) throw new OtrException(new IllegalStateException("KeyPair is not ready, yet."));
throw new OtrException(new IllegalStateException( }
"KeyPair is not ready, yet."));
return keyPair; return keyPair;
} }
...@@ -350,8 +309,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -350,8 +309,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
public void sessionStatusChanged(SessionID sessionID) { public void sessionStatusChanged(SessionID sessionID) {
removeSMRequest(sessionID.getAccountID(), sessionID.getUserID()); removeSMRequest(sessionID.getAccountID(), sessionID.getUserID());
removeSMProgress(sessionID.getAccountID(), sessionID.getUserID()); removeSMProgress(sessionID.getAccountID(), sessionID.getUserID());
Session session = sessions.get(sessionID.getAccountID(), Session session = sessions.get(sessionID.getAccountID(), sessionID.getUserID());
sessionID.getUserID());
SessionStatus sStatus = session.getSessionStatus(); SessionStatus sStatus = session.getSessionStatus();
if (sStatus == SessionStatus.ENCRYPTED) { if (sStatus == SessionStatus.ENCRYPTED) {
finished.remove(sessionID.getAccountID(), sessionID.getUserID()); finished.remove(sessionID.getAccountID(), sessionID.getUserID());
...@@ -364,25 +322,16 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -364,25 +322,16 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
value = null; value = null;
} }
if (value != null) { if (value != null) {
actives.put(sessionID.getAccountID(), sessionID.getUserID(), actives.put(sessionID.getAccountID(), sessionID.getUserID(), value);
value); if (fingerprints.get(sessionID.getAccountID(), sessionID.getUserID(), value) == null) {
if (fingerprints.get(sessionID.getAccountID(), fingerprints.put(sessionID.getAccountID(), sessionID.getUserID(), value, false);
sessionID.getUserID(), value) == null) { requestToWrite(sessionID.getAccountID(), sessionID.getUserID(), value, false);
fingerprints.put(sessionID.getAccountID(),
sessionID.getUserID(), value, false);
requestToWrite(sessionID.getAccountID(),
sessionID.getUserID(), value, false);
} }
} }
newAction( newAction(sessionID.getAccountID(), sessionID.getUserID(), null, isVerified(sessionID.getAccountID(),
sessionID.getAccountID(), sessionID.getUserID()) ? ChatAction.otr_verified : ChatAction.otr_encryption);
sessionID.getUserID(),
null,
isVerified(sessionID.getAccountID(), sessionID.getUserID()) ? ChatAction.otr_verified
: ChatAction.otr_encryption);
MessageManager.getInstance() MessageManager.getInstance()
.getChat(sessionID.getAccountID(), sessionID.getUserID()) .getChat(sessionID.getAccountID(), sessionID.getUserID()).sendMessages();
.sendMessages();
} else if (sStatus == SessionStatus.PLAINTEXT) { } else if (sStatus == SessionStatus.PLAINTEXT) {
actives.remove(sessionID.getAccountID(), sessionID.getUserID()); actives.remove(sessionID.getAccountID(), sessionID.getUserID());
finished.remove(sessionID.getAccountID(), sessionID.getUserID()); finished.remove(sessionID.getAccountID(), sessionID.getUserID());
...@@ -391,39 +340,27 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -391,39 +340,27 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
} catch (OtrException e) { } catch (OtrException e) {
LogManager.exception(this, e); LogManager.exception(this, e);
} }
newAction(sessionID.getAccountID(), sessionID.getUserID(), null, newAction(sessionID.getAccountID(), sessionID.getUserID(), null, ChatAction.otr_plain);
ChatAction.otr_plain);
} else if (sStatus == SessionStatus.FINISHED) { } else if (sStatus == SessionStatus.FINISHED) {
actives.remove(sessionID.getAccountID(), sessionID.getUserID()); actives.remove(sessionID.getAccountID(), sessionID.getUserID());
finished.put(sessionID.getAccountID(), sessionID.getUserID(), true); finished.put(sessionID.getAccountID(), sessionID.getUserID(), true);
newAction(sessionID.getAccountID(), sessionID.getUserID(), null, newAction(sessionID.getAccountID(), sessionID.getUserID(), null, ChatAction.otr_finish);
ChatAction.otr_finish); } else {
} else
throw new IllegalStateException(); throw new IllegalStateException();
RosterManager.getInstance().onContactChanged(sessionID.getAccountID(), }
sessionID.getUserID()); RosterManager.getInstance().onContactChanged(sessionID.getAccountID(), sessionID.getUserID());
} }
@Override @Override
public void askForSecret(SessionID sessionID, InstanceTag receiverTag, String question) { public void askForSecret(SessionID sessionID, InstanceTag receiverTag, String question) {
smRequestProvider.add( smRequestProvider.add(new SMRequest(sessionID.getAccountID(), sessionID.getUserID(), question), true);
new SMRequest(sessionID.getAccountID(), sessionID.getUserID(),
question), true);
} }
/** /**
* Transform outgoing message before sending. * Transform outgoing message before sending.
*
* @param account
* @param user
* @param content
* @return
* @throws OtrException
*/ */
public String transformSending(String account, String user, String content) public String transformSending(String account, String user, String content) throws OtrException {
throws OtrException { String parts[] = getOrCreateSession(account, user).transformSending(content, null);
String parts[] = getOrCreateSession(account, user)
.transformSending(content, null);
if (BuildConfig.DEBUG && parts.length != 1) { if (BuildConfig.DEBUG && parts.length != 1) {
throw new RuntimeException( throw new RuntimeException(
"We do not use fragmentation, so there must be only one otr fragment."); "We do not use fragmentation, so there must be only one otr fragment.");
...@@ -433,15 +370,8 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -433,15 +370,8 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
/** /**
* Transform incoming message after receiving. * Transform incoming message after receiving.
*
* @param account
* @param user
* @param content
* @return
* @throws OtrException
*/ */
public String transformReceiving(String account, String user, String content) public String transformReceiving(String account, String user, String content) throws OtrException {
throws OtrException {
Session session = getOrCreateSession(account, user); Session session = getOrCreateSession(account, user);
try { try {
return session.transformReceiving(content); return session.transformReceiving(content);
...@@ -452,73 +382,65 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -452,73 +382,65 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
public SecurityLevel getSecurityLevel(String account, String user) { public SecurityLevel getSecurityLevel(String account, String user) {
if (actives.get(account, user) == null) { if (actives.get(account, user) == null) {
if (finished.get(account, user) == null) if (finished.get(account, user) == null) {
return SecurityLevel.plain; return SecurityLevel.plain;
else } else {
return SecurityLevel.finished; return SecurityLevel.finished;
}
} else { } else {
if (isVerified(account, user)) if (isVerified(account, user)) {
return SecurityLevel.verified; return SecurityLevel.verified;
else } else {
return SecurityLevel.encrypted; return SecurityLevel.encrypted;
}
} }
} }
public boolean isVerified(String account, String user) { public boolean isVerified(String account, String user) {
String active = actives.get(account, user); String active = actives.get(account, user);
if (active == null) if (active == null) {
return false; return false;
}
Boolean value = fingerprints.get(account, user, active); Boolean value = fingerprints.get(account, user, active);
return value != null && value; return value != null && value;
} }
private void setVerifyWithoutNotification(String account, String user, private void setVerifyWithoutNotification(String account, String user, String fingerprint, boolean value) {
String fingerprint, boolean value) {
fingerprints.put(account, user, fingerprint, value); fingerprints.put(account, user, fingerprint, value);
requestToWrite(account, user, fingerprint, value); requestToWrite(account, user, fingerprint, value);
} }
/** /**
* Set whether fingerprint was verified. Add action to the chat history. * Set whether fingerprint was verified. Add action to the chat history.
*
* @param account
* @param user
* @param fingerprint
* @param value
*/ */
public void setVerify(String account, String user, String fingerprint, public void setVerify(String account, String user, String fingerprint, boolean value) {
boolean value) {
setVerifyWithoutNotification(account, user, fingerprint, value); setVerifyWithoutNotification(account, user, fingerprint, value);
if (value) if (value) {
newAction(account, user, null, ChatAction.otr_smp_verified); newAction(account, user, null, ChatAction.otr_smp_verified);
else if (actives.get(account, user) != null) } else if (actives.get(account, user) != null) {
newAction(account, user, null, ChatAction.otr_encryption); newAction(account, user, null, ChatAction.otr_encryption);
}
} }
private void setVerify(SessionID sessionID, boolean value) { private void setVerify(SessionID sessionID, boolean value) {
String active = actives.get(sessionID.getAccountID(), String active = actives.get(sessionID.getAccountID(), sessionID.getUserID());
sessionID.getUserID());
if (active == null) { if (active == null) {
LogManager.exception(this, new IllegalStateException( LogManager.exception(this, new IllegalStateException("There is no active fingerprint"));
"There is no active fingerprint"));
return; return;
} }
setVerifyWithoutNotification(sessionID.getAccountID(), setVerifyWithoutNotification(sessionID.getAccountID(), sessionID.getUserID(), active, value);
sessionID.getUserID(), active, value);
newAction(sessionID.getAccountID(), sessionID.getUserID(), null, newAction(sessionID.getAccountID(), sessionID.getUserID(), null,
value ? ChatAction.otr_smp_verified value ? ChatAction.otr_smp_verified : ChatAction.otr_smp_unverified);
: ChatAction.otr_smp_unverified); RosterManager.getInstance().onContactChanged(sessionID.getAccountID(), sessionID.getUserID());
RosterManager.getInstance().onContactChanged(sessionID.getAccountID(),
sessionID.getUserID());
} }
@Override @Override
public void verify(SessionID sessionID, String fingerprint, boolean approved) { public void verify(SessionID sessionID, String fingerprint, boolean approved) {
if (approved) if (approved) {
setVerify(sessionID, true); setVerify(sessionID, true);
else if (isVerified(sessionID.getAccountID(), sessionID.getUserID())) } else if (isVerified(sessionID.getAccountID(), sessionID.getUserID())) {
newAction(sessionID.getAccountID(), sessionID.getUserID(), null, newAction(sessionID.getAccountID(), sessionID.getUserID(), null, ChatAction.otr_smp_not_approved);
ChatAction.otr_smp_not_approved); }
removeSMProgress(sessionID.getAccountID(), sessionID.getUserID()); removeSMProgress(sessionID.getAccountID(), sessionID.getUserID());
} }
...@@ -534,10 +456,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -534,10 +456,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
public String getLocalFingerprint(String account) { public String getLocalFingerprint(String account) {
try { try {
return OtrCryptoEngine.getFingerprint(getLocalKeyPair( return OtrCryptoEngine.getFingerprint(getLocalKeyPair(account).getPublic());
account).getPublic());
} catch (OtrCryptoException e) {
LogManager.exception(this, e);
} catch (OtrException e) { } catch (OtrException e) {
LogManager.exception(this, e); LogManager.exception(this, e);
} }
...@@ -546,9 +465,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -546,9 +465,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
@Override @Override
public byte[] getLocalFingerprintRaw(SessionID sessionID) { public byte[] getLocalFingerprintRaw(SessionID sessionID) {
return SerializationUtils return SerializationUtils.hexStringToByteArray(getLocalFingerprint(sessionID.getAccountID()));
.hexStringToByteArray(getLocalFingerprint(sessionID
.getAccountID()));
} }
@Override @Override
...@@ -558,15 +475,8 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -558,15 +475,8 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
/** /**
* Respond using SM protocol. * Respond using SM protocol.
*
* @param account
* @param user
* @param question
* @param secret
* @throws NetworkException
*/ */
public void respondSmp(String account, String user, String question, public void respondSmp(String account, String user, String question, String secret) throws NetworkException {
String secret) throws NetworkException {
removeSMRequest(account, user); removeSMRequest(account, user);
addSMProgress(account, user); addSMProgress(account, user);
try { try {
...@@ -578,15 +488,8 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -578,15 +488,8 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
/** /**
* Initiate request using SM protocol. * Initiate request using SM protocol.
*
* @param account
* @param user
* @param question
* @param secret
* @throws NetworkException
*/ */
public void initSmp(String account, String user, String question, public void initSmp(String account, String user, String question, String secret) throws NetworkException {
String secret) throws NetworkException {
removeSMRequest(account, user); removeSMRequest(account, user);
addSMProgress(account, user); addSMProgress(account, user);
try { try {
...@@ -598,10 +501,6 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -598,10 +501,6 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
/** /**
* Abort SM negotiation. * Abort SM negotiation.
*
* @param account
* @param user
* @throws NetworkException
*/ */
public void abortSmp(String account, String user) throws NetworkException { public void abortSmp(String account, String user) throws NetworkException {
removeSMRequest(account, user); removeSMRequest(account, user);
...@@ -627,13 +526,13 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -627,13 +526,13 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
@Override @Override
public void onAccountAdded(final AccountItem accountItem) { public void onAccountAdded(final AccountItem accountItem) {
if (accountItem.getKeyPair() != null) if (accountItem.getKeyPair() != null) {
return; return;
}
keyPairGenerator.execute(new Runnable() { keyPairGenerator.execute(new Runnable() {
@Override @Override
public void run() { public void run() {
LogManager.i(this, "KeyPair generation started for " LogManager.i(this, "KeyPair generation started for " + accountItem.getAccount());
+ accountItem.getAccount());
final KeyPair keyPair; final KeyPair keyPair;
try { try {
keyPair = KeyPairGenerator.getInstance("DSA").genKeyPair(); keyPair = KeyPairGenerator.getInstance("DSA").genKeyPair();
...@@ -649,12 +548,10 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -649,12 +548,10 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
Application.getInstance().runOnUiThread(new Runnable() { Application.getInstance().runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
LogManager.i(this, "KeyPair generation finished for " LogManager.i(this, "KeyPair generation finished for " + accountItem.getAccount());
+ accountItem.getAccount()); if (AccountManager.getInstance().getAccount(accountItem.getAccount()) != null) {
if (AccountManager.getInstance().getAccount( AccountManager.getInstance().setKeyPair(accountItem.getAccount(), keyPair);
accountItem.getAccount()) != null) }
AccountManager.getInstance().setKeyPair(
accountItem.getAccount(), keyPair);
} }
}); });
} }
...@@ -671,32 +568,28 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -671,32 +568,28 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
/** /**
* Save chat specific otr settings. * Save chat specific otr settings.
*
* @param account
* @param user
* @param fingerprint
* @param verified
*/ */
private void requestToWrite(final String account, final String user, private void requestToWrite(final String account, final String user,
final String fingerprint, final boolean verified) { final String fingerprint, final boolean verified) {
Application.getInstance().runInBackground(new Runnable() { Application.getInstance().runInBackground(new Runnable() {
@Override @Override
public void run() { public void run() {
OTRTable.getInstance().write(account, user, fingerprint, OTRTable.getInstance().write(account, user, fingerprint, verified);
verified);
} }
}); });
} }
private void endAllSessions() { private void endAllSessions() {
NestedMap<String> entities = new NestedMap<String>(); LogManager.i(this, "End all sessions");
NestedMap<String> entities = new NestedMap<>();
entities.addAll(actives); entities.addAll(actives);
for (Entry<String> entry : entities) for (Entry<String> entry : entities) {
try { try {
endSession(entry.getFirst(), entry.getSecond()); endSession(entry.getFirst(), entry.getSecond());
} catch (NetworkException e) { } catch (NetworkException e) {
LogManager.exception(this, e); LogManager.exception(this, e);
} }
}
} }
@Override @Override
...@@ -705,8 +598,9 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -705,8 +598,9 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
} }
public void onSettingsChanged() { public void onSettingsChanged() {
if (SettingsManager.securityOtrMode() == SecurityOtrMode.disabled) if (SettingsManager.securityOtrMode() == SecurityOtrMode.disabled) {
endAllSessions(); endAllSessions();
}
} }
@Override @Override
...@@ -725,8 +619,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener, ...@@ -725,8 +619,7 @@ public class OTRManager implements OtrEngineHost, OtrEngineListener,
public void messageFromAnotherInstanceReceived(SessionID sessionID) { public void messageFromAnotherInstanceReceived(SessionID sessionID) {
LogManager.i(this, "Message from another instance received on SessionID " LogManager.i(this, "Message from another instance received on SessionID "
+ sessionID + ". Restarting OTR session for this user."); + sessionID + ". Restarting OTR session for this user.");
newAction(sessionID.getAccountID(), sessionID.getUserID(), null, newAction(sessionID.getAccountID(), sessionID.getUserID(), null, ChatAction.otr_unreadable);
ChatAction.otr_unreadable);
} }
@Override @Override
......
...@@ -31,8 +31,7 @@ public class SMProgress extends BaseEntity implements EntityNotificationItem { ...@@ -31,8 +31,7 @@ public class SMProgress extends BaseEntity implements EntityNotificationItem {
@Override @Override
public Intent getIntent() { public Intent getIntent() {
return QuestionViewer.createCancelIntent( return QuestionViewer.createCancelIntent(Application.getInstance(), account, user);
Application.getInstance(), account, user);
} }
@Override @Override
...@@ -43,8 +42,7 @@ public class SMProgress extends BaseEntity implements EntityNotificationItem { ...@@ -43,8 +42,7 @@ public class SMProgress extends BaseEntity implements EntityNotificationItem {
@Override @Override
public String getText() { public String getText() {
return Application.getInstance().getString( return Application.getInstance().getString(R.string.otr_verification_in_progress);
R.string.otr_verification_in_progress);
} }
} }
...@@ -35,8 +35,7 @@ public class SMRequest extends BaseEntity implements EntityNotificationItem { ...@@ -35,8 +35,7 @@ public class SMRequest extends BaseEntity implements EntityNotificationItem {
@Override @Override
public Intent getIntent() { public Intent getIntent() {
return QuestionViewer.createIntent( return QuestionViewer.createIntent(
Application.getInstance(), account, user, question != null, Application.getInstance(), account, user, question != null, true, question);
true, question);
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment