diff options
Diffstat (limited to 'src/net/java/sip/communicator/plugin/otr/ScOtrEngineImpl.java')
-rw-r--r-- | src/net/java/sip/communicator/plugin/otr/ScOtrEngineImpl.java | 465 |
1 files changed, 252 insertions, 213 deletions
diff --git a/src/net/java/sip/communicator/plugin/otr/ScOtrEngineImpl.java b/src/net/java/sip/communicator/plugin/otr/ScOtrEngineImpl.java index f500c5a..8ddc98e 100644 --- a/src/net/java/sip/communicator/plugin/otr/ScOtrEngineImpl.java +++ b/src/net/java/sip/communicator/plugin/otr/ScOtrEngineImpl.java @@ -25,68 +25,13 @@ import org.osgi.framework.*; * @author George Politis
* @author Lyubomir Marinov
* @author Pawel Domas
+ * @author Marin Dzhigarov
*/
public class ScOtrEngineImpl
implements ScOtrEngine,
- ChatLinkClickedListener
+ ChatLinkClickedListener,
+ ServiceListener
{
- /**
- * The logger
- */
- private final Logger logger = Logger.getLogger(ScOtrEngineImpl.class);
-
- private final OtrConfigurator configurator = new OtrConfigurator();
-
- private static final Map<ScSessionID, Contact> contactsMap =
- new Hashtable<ScSessionID, Contact>();
-
- private final List<String> injectedMessageUIDs = new Vector<String>();
-
- private final List<ScOtrEngineListener> listeners =
- new Vector<ScOtrEngineListener>();
-
- private final OtrEngine otrEngine
- = new OtrEngineImpl(new ScOtrEngineHost());
-
- public void addListener(ScOtrEngineListener l)
- {
- synchronized (listeners)
- {
- if (!listeners.contains(l))
- listeners.add(l);
- }
- }
-
- /**
- * Gets a copy of the list of <tt>ScOtrEngineListener</tt>s registered with
- * this instance which may safely be iterated without the risk of a
- * <tt>ConcurrentModificationException</tt>.
- *
- * @return a copy of the list of <tt>ScOtrEngineListener<tt>s registered
- * with this instance which may safely be iterated without the risk of a
- * <tt>ConcurrentModificationException</tt>
- */
- private ScOtrEngineListener[] getListeners()
- {
- synchronized (listeners)
- {
- return listeners.toArray(new ScOtrEngineListener[listeners.size()]);
- }
- }
-
- public void removeListener(ScOtrEngineListener l)
- {
- synchronized (listeners)
- {
- listeners.remove(l);
- }
- }
-
- public boolean isMessageUIDInjected(String mUID)
- {
- return injectedMessageUIDs.contains(mUID);
- }
-
class ScOtrEngineHost
implements OtrEngineHost
{
@@ -102,21 +47,9 @@ public class ScOtrEngineImpl return OtrActivator.scOtrKeyManager.loadKeyPair(accountID);
}
- public void showWarning(SessionID sessionID, String warn)
- {
- Contact contact = getContact(sessionID);
- if (contact == null)
- return;
-
- OtrActivator.uiService.getChat(contact).addMessage(
- contact.getDisplayName(), new Date(),
- Chat.SYSTEM_MESSAGE, warn,
- OperationSetBasicInstantMessaging.DEFAULT_MIME_TYPE);
- }
-
- public void showError(SessionID sessionID, String err)
+ public OtrPolicy getSessionPolicy(SessionID sessionID)
{
- ScOtrEngineImpl.this.showError(sessionID, err);
+ return getContactPolicy(getContact(sessionID));
}
public void injectMessage(SessionID sessionID, String messageText)
@@ -125,31 +58,95 @@ public class ScOtrEngineImpl OperationSetBasicInstantMessaging imOpSet
= contact
.getProtocolProvider()
- .getOperationSet(OperationSetBasicInstantMessaging.class);
+ .getOperationSet(
+ OperationSetBasicInstantMessaging.class);
Message message = imOpSet.createMessage(messageText);
injectedMessageUIDs.add(message.getMessageUID());
imOpSet.sendInstantMessage(contact, message);
}
- public OtrPolicy getSessionPolicy(SessionID sessionID)
+ public void showError(SessionID sessionID, String err)
{
- return getContactPolicy(getContact(sessionID));
+ ScOtrEngineImpl.this.showError(sessionID, err);
+ }
+
+ public void showWarning(SessionID sessionID, String warn)
+ {
+ Contact contact = getContact(sessionID);
+ if (contact == null)
+ return;
+
+ OtrActivator.uiService.getChat(contact).addMessage(
+ contact.getDisplayName(), new Date(),
+ Chat.SYSTEM_MESSAGE, warn,
+ OperationSetBasicInstantMessaging.DEFAULT_MIME_TYPE);
}
}
- public void showError(SessionID sessionID, String err)
+ private static final Map<ScSessionID, Contact> contactsMap =
+ new Hashtable<ScSessionID, Contact>();
+
+ public static Contact getContact(SessionID sessionID)
{
- Contact contact = getContact(sessionID);
- if (contact == null)
- return;
+ return contactsMap.get(new ScSessionID(sessionID));
+ }
- OtrActivator.uiService.getChat(contact).addMessage(
- contact.getDisplayName(), new Date(),
- Chat.ERROR_MESSAGE, err,
- OperationSetBasicInstantMessaging.DEFAULT_MIME_TYPE);
+ /**
+ * Returns the <tt>ScSessionID</tt> for given <tt>UUID</tt>.
+ * @param guid the <tt>UUID</tt> identifying <tt>ScSessionID</tt>.
+ * @return the <tt>ScSessionID</tt> for given <tt>UUID</tt> or <tt>null</tt>
+ * if no matching session found.
+ */
+ public static ScSessionID getScSessionForGuid(UUID guid)
+ {
+ for(ScSessionID scSessionID : contactsMap.keySet())
+ {
+ if(scSessionID.getGUID().equals(guid))
+ {
+ return scSessionID;
+ }
+ }
+ return null;
+ }
+
+ public static SessionID getSessionID(Contact contact)
+ {
+ ProtocolProviderService pps = contact.getProtocolProvider();
+ SessionID sessionID
+ = new SessionID(
+ pps.getAccountID().getAccountUniqueID(),
+ contact.getAddress(),
+ pps.getProtocolName());
+
+ synchronized (contactsMap)
+ {
+ if(contactsMap.containsKey(new ScSessionID(sessionID)))
+ return sessionID;
+
+ ScSessionID scSessionID = new ScSessionID(sessionID);
+
+ contactsMap.put(scSessionID, contact);
+ }
+
+ return sessionID;
}
+ private final OtrConfigurator configurator = new OtrConfigurator();
+
+ private final List<String> injectedMessageUIDs = new Vector<String>();
+
+ private final List<ScOtrEngineListener> listeners =
+ new Vector<ScOtrEngineListener>();
+
+ /**
+ * The logger
+ */
+ private final Logger logger = Logger.getLogger(ScOtrEngineImpl.class);
+
+ private final OtrEngine otrEngine
+ = new OtrEngineImpl(new ScOtrEngineHost());
+
public ScOtrEngineImpl()
{
// Clears the map after previous instance
@@ -193,10 +190,10 @@ public class ScOtrEngineImpl OtrActivator.uiService.getChat(contact)
.addChatLinkClickedListener(ScOtrEngineImpl.this);
- String unverifiedSessionWarning =
- OtrActivator.resourceService
- .getI18NString(
- "plugin.otr.activator.unverifiedsessionwarning",
+ String unverifiedSessionWarning
+ = OtrActivator.resourceService.getI18NString(
+ "plugin.otr.activator"
+ + ".unverifiedsessionwarning",
new String[]
{
contact.getDisplayName(),
@@ -211,14 +208,13 @@ public class ScOtrEngineImpl OperationSetBasicInstantMessaging.HTML_MIME_TYPE);
}
- message =
- OtrActivator.resourceService
- .getI18NString(
- (OtrActivator.scOtrKeyManager
- .isVerified(contact)) ? "plugin.otr.activator.sessionstared"
- : "plugin.otr.activator.unverifiedsessionstared",
- new String[]
- { contact.getDisplayName() });
+ message
+ = OtrActivator.resourceService.getI18NString(
+ OtrActivator.scOtrKeyManager.isVerified(contact)
+ ? "plugin.otr.activator.sessionstared"
+ : "plugin.otr.activator"
+ + ".unverifiedsessionstared",
+ new String[] { contact.getDisplayName() });
break;
case FINISHED:
@@ -247,49 +243,41 @@ public class ScOtrEngineImpl });
}
- public static SessionID getSessionID(Contact contact)
+ public void addListener(ScOtrEngineListener l)
{
- ProtocolProviderService pps = contact.getProtocolProvider();
- SessionID sessionID
- = new SessionID(
- pps.getAccountID().getAccountUniqueID(),
- contact.getAddress(),
- pps.getProtocolName());
-
- synchronized (contactsMap)
+ synchronized (listeners)
{
- if(contactsMap.containsKey(new ScSessionID(sessionID)))
- return sessionID;
-
- ScSessionID scSessionID = new ScSessionID(sessionID);
-
- contactsMap.put(scSessionID, contact);
+ if (!listeners.contains(l))
+ listeners.add(l);
}
-
- return sessionID;
}
- /**
- * Returns the <tt>ScSessionID</tt> for given <tt>UUID</tt>.
- * @param guid the <tt>UUID</tt> identifying <tt>ScSessionID</tt>.
- * @return the <tt>ScSessionID</tt> for given <tt>UUID</tt> or <tt>null</tt>
- * if no matching session found.
- */
- public static ScSessionID getScSessionForGuid(UUID guid)
+ public void chatLinkClicked(URI url)
{
- for(ScSessionID scSessionID : contactsMap.keySet())
+ String action = url.getPath();
+ if(action.equals("/AUTHENTIFICATION"))
{
- if(scSessionID.getGUID().equals(guid))
+ UUID guid = UUID.fromString(url.getQuery());
+
+ if(guid == null)
+ throw new RuntimeException(
+ "No UUID found in OTR authenticate URL");
+
+ // Looks for registered action handler
+ OtrActionHandler actionHandler
+ = ServiceUtils.getService(
+ OtrActivator.bundleContext,
+ OtrActionHandler.class);
+
+ if(actionHandler != null)
{
- return scSessionID;
+ actionHandler.onAuthenticateLinkClicked(guid);
+ }
+ else
+ {
+ logger.error("No OtrActionHandler registered");
}
}
- return null;
- }
-
- public static Contact getContact(SessionID sessionID)
- {
- return contactsMap.get(new ScSessionID(sessionID));
}
public void endSession(Contact contact)
@@ -305,41 +293,66 @@ public class ScOtrEngineImpl }
}
- public SessionStatus getSessionStatus(Contact contact)
+ public OtrPolicy getContactPolicy(Contact contact)
{
- return otrEngine.getSessionStatus(getSessionID(contact));
+ int policy =
+ this.configurator.getPropertyInt(getSessionID(contact) + "policy",
+ -1);
+ if (policy < 0)
+ return getGlobalPolicy();
+ else
+ return new OtrPolicyImpl(policy);
}
- public String transformReceiving(Contact contact, String msgText)
+ public OtrPolicy getGlobalPolicy()
{
- SessionID sessionID = getSessionID(contact);
- try
- {
- return otrEngine.transformReceiving(sessionID, msgText);
- }
- catch (OtrException e)
- {
- logger.error("Error receiving the message", e);
- showError(sessionID, e.getMessage());
- return null;
- }
+ return new OtrPolicyImpl(this.configurator.getPropertyInt("POLICY",
+ OtrPolicy.OTRL_POLICY_DEFAULT));
}
- public String transformSending(Contact contact, String msgText)
+ /**
+ * Gets a copy of the list of <tt>ScOtrEngineListener</tt>s registered with
+ * this instance which may safely be iterated without the risk of a
+ * <tt>ConcurrentModificationException</tt>.
+ *
+ * @return a copy of the list of <tt>ScOtrEngineListener<tt>s registered
+ * with this instance which may safely be iterated without the risk of a
+ * <tt>ConcurrentModificationException</tt>
+ */
+ private ScOtrEngineListener[] getListeners()
{
- SessionID sessionID = getSessionID(contact);
- try
- {
- return otrEngine.transformSending(sessionID, msgText);
- }
- catch (OtrException e)
+ synchronized (listeners)
{
- logger.error("Error transforming the message", e);
- showError(sessionID, e.getMessage());
- return null;
+ return listeners.toArray(new ScOtrEngineListener[listeners.size()]);
}
}
+ public SessionStatus getSessionStatus(Contact contact)
+ {
+ return otrEngine.getSessionStatus(getSessionID(contact));
+ }
+
+ public boolean isMessageUIDInjected(String mUID)
+ {
+ return injectedMessageUIDs.contains(mUID);
+ }
+
+ public void launchHelp()
+ {
+ ServiceReference ref =
+ OtrActivator.bundleContext
+ .getServiceReference(BrowserLauncherService.class.getName());
+
+ if (ref == null)
+ return;
+
+ BrowserLauncherService service =
+ (BrowserLauncherService) OtrActivator.bundleContext.getService(ref);
+
+ service.openURL(OtrActivator.resourceService
+ .getI18NString("plugin.otr.authbuddydialog.HELP_URI"));
+ }
+
public void refreshSession(Contact contact)
{
SessionID sessionID = getSessionID(contact);
@@ -354,24 +367,61 @@ public class ScOtrEngineImpl }
}
- public void startSession(Contact contact)
+ public void removeListener(ScOtrEngineListener l)
{
- SessionID sessionID = getSessionID(contact);
- try
+ synchronized (listeners)
{
- otrEngine.startSession(sessionID);
+ listeners.remove(l);
}
- catch (OtrException e)
+ }
+
+ /**
+ * Cleans the contactsMap when <tt>ProtocolProviderService</tt>
+ * gets unregistered.
+ */
+ public void serviceChanged(ServiceEvent ev)
+ {
+ Object service
+ = OtrActivator.bundleContext.getService(ev.getServiceReference());
+
+ if (!(service instanceof ProtocolProviderService))
+ return;
+
+ if (ev.getType() == ServiceEvent.UNREGISTERING)
{
- logger.error("Error starting session", e);
- showError(sessionID, e.getMessage());
+ if (logger.isDebugEnabled())
+ {
+ logger.debug(
+ "Unregistering a ProtocolProviderService, cleaning"
+ + " OTR's ScSessionID to Contact map.");
+ }
+
+ ProtocolProviderService provider
+ = (ProtocolProviderService) service;
+
+ synchronized(contactsMap)
+ {
+ Iterator<Contact> i = contactsMap.values().iterator();
+
+ while (i.hasNext())
+ {
+ if (provider.equals(i.next().getProtocolProvider()))
+ i.remove();
+ }
+ }
}
}
- public OtrPolicy getGlobalPolicy()
+ public void setContactPolicy(Contact contact, OtrPolicy policy)
{
- return new OtrPolicyImpl(this.configurator.getPropertyInt("POLICY",
- OtrPolicy.OTRL_POLICY_DEFAULT));
+ String propertyID = getSessionID(contact) + "policy";
+ if (policy == null)
+ this.configurator.removeProperty(propertyID);
+ else
+ this.configurator.setProperty(propertyID, policy.getPolicy());
+
+ for (ScOtrEngineListener l : getListeners())
+ l.contactPolicyChanged(contact);
}
public void setGlobalPolicy(OtrPolicy policy)
@@ -385,70 +435,59 @@ public class ScOtrEngineImpl l.globalPolicyChanged();
}
- public void launchHelp()
+ public void showError(SessionID sessionID, String err)
{
- ServiceReference ref =
- OtrActivator.bundleContext
- .getServiceReference(BrowserLauncherService.class.getName());
-
- if (ref == null)
+ Contact contact = getContact(sessionID);
+ if (contact == null)
return;
- BrowserLauncherService service =
- (BrowserLauncherService) OtrActivator.bundleContext.getService(ref);
-
- service.openURL(OtrActivator.resourceService
- .getI18NString("plugin.otr.authbuddydialog.HELP_URI"));
+ OtrActivator.uiService.getChat(contact).addMessage(
+ contact.getDisplayName(), new Date(),
+ Chat.ERROR_MESSAGE, err,
+ OperationSetBasicInstantMessaging.DEFAULT_MIME_TYPE);
}
- public OtrPolicy getContactPolicy(Contact contact)
+ public void startSession(Contact contact)
{
- int policy =
- this.configurator.getPropertyInt(getSessionID(contact) + "policy",
- -1);
- if (policy < 0)
- return getGlobalPolicy();
- else
- return new OtrPolicyImpl(policy);
+ SessionID sessionID = getSessionID(contact);
+ try
+ {
+ otrEngine.startSession(sessionID);
+ }
+ catch (OtrException e)
+ {
+ logger.error("Error starting session", e);
+ showError(sessionID, e.getMessage());
+ }
}
- public void setContactPolicy(Contact contact, OtrPolicy policy)
+ public String transformReceiving(Contact contact, String msgText)
{
- String propertyID = getSessionID(contact) + "policy";
- if (policy == null)
- this.configurator.removeProperty(propertyID);
- else
- this.configurator.setProperty(propertyID, policy.getPolicy());
-
- for (ScOtrEngineListener l : getListeners())
- l.contactPolicyChanged(contact);
+ SessionID sessionID = getSessionID(contact);
+ try
+ {
+ return otrEngine.transformReceiving(sessionID, msgText);
+ }
+ catch (OtrException e)
+ {
+ logger.error("Error receiving the message", e);
+ showError(sessionID, e.getMessage());
+ return null;
+ }
}
- public void chatLinkClicked(URI url)
+ public String transformSending(Contact contact, String msgText)
{
- String action = url.getPath();
- if(action.equals("/AUTHENTIFICATION"))
+ SessionID sessionID = getSessionID(contact);
+ try
{
- UUID guid = UUID.fromString(url.getQuery());
-
- if(guid == null)
- throw new RuntimeException(
- "No UUID found in OTR authenticate URL");
-
- // Looks for registered action handler
- OtrActionHandler actionHandler
- = ServiceUtils.getService(
- OtrActivator.bundleContext,
- OtrActionHandler.class);
-
- if(actionHandler != null)
- {
- actionHandler.onAuthenticateLinkClicked(guid);
- }
- else
- {
- logger.error("No OtrActionHandler registered");
- }
+ return otrEngine.transformSending(sessionID, msgText);
+ }
+ catch (OtrException e)
+ {
+ logger.error("Error transforming the message", e);
+ showError(sessionID, e.getMessage());
+ return null;
}
}
}
|