diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/net/java/sip/communicator/plugin/otr/OtrActivator.java | 425 | ||||
-rw-r--r-- | src/net/java/sip/communicator/plugin/otr/ScOtrEngineImpl.java | 465 |
2 files changed, 466 insertions, 424 deletions
diff --git a/src/net/java/sip/communicator/plugin/otr/OtrActivator.java b/src/net/java/sip/communicator/plugin/otr/OtrActivator.java index b5309ab..3b10939 100644 --- a/src/net/java/sip/communicator/plugin/otr/OtrActivator.java +++ b/src/net/java/sip/communicator/plugin/otr/OtrActivator.java @@ -28,10 +28,29 @@ public class OtrActivator implements ServiceListener { /** - * A property used in configuration to disable the OTR plugin. + * A property specifying whether private messaging should be automatically + * initiated. */ - public static final String OTR_DISABLED_PROP = - "net.java.sip.communicator.plugin.otr.DISABLED"; + public static final String AUTO_INIT_OTR_PROP = + "net.java.sip.communicator.plugin.otr.AUTO_INIT_PRIVATE_MESSAGING"; + + /** + * The {@link BundleContext} of the {@link OtrActivator}. + */ + public static BundleContext bundleContext; + + /** + * The {@link ConfigurationService} of the {@link OtrActivator}. Can also be + * obtained from the {@link OtrActivator#bundleContext} on demand, but we + * add it here for convenience. + */ + public static ConfigurationService configService; + + /** + * The <tt>Logger</tt> used by the <tt>OtrActivator</tt> class and its + * instances for logging output. + */ + private static final Logger logger = Logger.getLogger(OtrActivator.class); /** * Indicates if the security/chat config form should be disabled, i.e. @@ -41,24 +60,23 @@ public class OtrActivator = "net.java.sip.communicator.plugin.otr.otrchatconfig.DISABLED"; /** - * A property specifying whether private messaging should be made mandatory. + * A property used in configuration to disable the OTR plugin. */ - public static final String OTR_MANDATORY_PROP = - "net.java.sip.communicator.plugin.otr.PRIVATE_MESSAGING_MANDATORY"; + public static final String OTR_DISABLED_PROP = + "net.java.sip.communicator.plugin.otr.DISABLED"; /** - * A property specifying whether private messaging should be automatically - * initiated. + * A property specifying whether private messaging should be made mandatory. */ - public static final String AUTO_INIT_OTR_PROP = - "net.java.sip.communicator.plugin.otr.AUTO_INIT_PRIVATE_MESSAGING"; + public static final String OTR_MANDATORY_PROP = + "net.java.sip.communicator.plugin.otr.PRIVATE_MESSAGING_MANDATORY"; /** - * The {@link BundleContext} of the {@link OtrActivator}. + * The {@link ResourceManagementService} of the {@link OtrActivator}. Can + * also be obtained from the {@link OtrActivator#bundleContext} on demand, + * but we add it here for convenience. */ - public static BundleContext bundleContext; - - private OtrTransformLayer otrTransformLayer; + public static ResourceManagementService resourceService; /** * The {@link ScOtrEngine} of the {@link OtrActivator}. @@ -71,13 +89,6 @@ public class OtrActivator public static ScOtrKeyManager scOtrKeyManager = new ScOtrKeyManagerImpl(); /** - * The {@link ResourceManagementService} of the {@link OtrActivator}. Can - * also be obtained from the {@link OtrActivator#bundleContext} on demand, - * but we add it here for convenience. - */ - public static ResourceManagementService resourceService; - - /** * The {@link UIService} of the {@link OtrActivator}. Can also be obtained * from the {@link OtrActivator#bundleContext} on demand, but we add it here * for convenience. @@ -85,17 +96,176 @@ public class OtrActivator public static UIService uiService; /** - * The {@link ConfigurationService} of the {@link OtrActivator}. Can also be - * obtained from the {@link OtrActivator#bundleContext} on demand, but we - * add it here for convenience. + * Gets an {@link AccountID} by its UID. + * + * @param uid The {@link AccountID} UID. + * @return The {@link AccountID} with the requested UID or null. */ - public static ConfigurationService configService; + public static AccountID getAccountIDByUID(String uid) + { + if (uid == null || uid.length() < 1) + return null; + + Map<Object, ProtocolProviderFactory> providerFactoriesMap = + OtrActivator.getProtocolProviderFactories(); + + if (providerFactoriesMap == null) + return null; + + for (ProtocolProviderFactory providerFactory + : providerFactoriesMap.values()) + { + for (AccountID accountID : providerFactory.getRegisteredAccounts()) + { + if (accountID.getAccountUniqueID().equals(uid)) + return accountID; + } + } + + return null; + } /** - * The <tt>Logger</tt> used by the <tt>OtrActivator</tt> class and its - * instances for logging output. + * Gets all the available accounts in SIP Communicator. + * + * @return a {@link List} of {@link AccountID}. */ - private static final Logger logger = Logger.getLogger(OtrActivator.class); + public static List<AccountID> getAllAccountIDs() + { + Map<Object, ProtocolProviderFactory> providerFactoriesMap = + OtrActivator.getProtocolProviderFactories(); + + if (providerFactoriesMap == null) + return null; + + List<AccountID> accountIDs = new Vector<AccountID>(); + + for (ProtocolProviderFactory providerFactory + : providerFactoriesMap.values()) + { + for (AccountID accountID : providerFactory.getRegisteredAccounts()) + accountIDs.add(accountID); + } + + return accountIDs; + } + + private static Map<Object, ProtocolProviderFactory> + getProtocolProviderFactories() + { + ServiceReference[] serRefs = null; + try + { + // get all registered provider factories + serRefs = + bundleContext.getServiceReferences( + ProtocolProviderFactory.class.getName(), null); + + } + catch (InvalidSyntaxException ex) + { + logger.error("Error while retrieving service refs", ex); + return null; + } + + Map<Object, ProtocolProviderFactory> providerFactoriesMap = + new Hashtable<Object, ProtocolProviderFactory>(); + if (serRefs != null) + { + for (ServiceReference serRef : serRefs) + { + ProtocolProviderFactory providerFactory = + (ProtocolProviderFactory) bundleContext.getService(serRef); + + providerFactoriesMap.put(serRef + .getProperty(ProtocolProviderFactory.PROTOCOL), + providerFactory); + } + } + return providerFactoriesMap; + } + + private OtrTransformLayer otrTransformLayer; + + /** + * The dependent class. We are waiting for the ui service. + * @return the ui service class. + */ + @Override + public Class<?> getDependentServiceClass() + { + return UIService.class; + } + + private void handleProviderAdded(ProtocolProviderService provider) + { + OperationSetInstantMessageTransform opSetMessageTransform + = provider.getOperationSet( + OperationSetInstantMessageTransform.class); + + if (opSetMessageTransform != null) + opSetMessageTransform.addTransformLayer(this.otrTransformLayer); + else if (logger.isTraceEnabled()) + logger.trace("Service did not have a transform op. set."); + } + + private void handleProviderRemoved(ProtocolProviderService provider) + { + // check whether the provider has a basic im operation set + OperationSetInstantMessageTransform opSetMessageTransform + = provider.getOperationSet( + OperationSetInstantMessageTransform.class); + + if (opSetMessageTransform != null) + opSetMessageTransform.removeTransformLayer(this.otrTransformLayer); + } + + /* + * Implements ServiceListener#serviceChanged(ServiceEvent). + */ + public void serviceChanged(ServiceEvent serviceEvent) + { + Object sService = + bundleContext.getService(serviceEvent.getServiceReference()); + + if (logger.isTraceEnabled()) + { + logger.trace( + "Received a service event for: " + + sService.getClass().getName()); + } + + // we don't care if the source service is not a protocol provider + if (!(sService instanceof ProtocolProviderService)) + return; + + if (logger.isDebugEnabled()) + logger.debug("Service is a protocol provider."); + if (serviceEvent.getType() == ServiceEvent.REGISTERED) + { + if (logger.isDebugEnabled()) + { + logger.debug( + "Handling registration of a new Protocol Provider."); + } + this.handleProviderAdded((ProtocolProviderService) sService); + } + else if (serviceEvent.getType() == ServiceEvent.UNREGISTERING) + { + this.handleProviderRemoved((ProtocolProviderService) sService); + } + + } + + /** + * The bundle context to use. + * @param context the context to set. + */ + @Override + public void setBundleContext(BundleContext context) + { + bundleContext = context; + } /* * Implements AbstractServiceDependentActivator#start(UIService). @@ -130,22 +300,26 @@ public class OtrActivator // Register Transformation Layer bundleContext.addServiceListener(this); + bundleContext.addServiceListener((ServiceListener) scOtrEngine); - ServiceReference[] protocolProviderRefs = ServiceUtils - .getServiceReferences( - bundleContext, - ProtocolProviderService.class); + ServiceReference[] protocolProviderRefs + = ServiceUtils.getServiceReferences( + bundleContext, + ProtocolProviderService.class); if (protocolProviderRefs != null && protocolProviderRefs.length > 0) { if (logger.isDebugEnabled()) - logger.debug("Found " + protocolProviderRefs.length - + " already installed providers."); + { + logger.debug( + "Found " + protocolProviderRefs.length + + " already installed providers."); + } for (ServiceReference protocolProviderRef : protocolProviderRefs) { - ProtocolProviderService provider = - (ProtocolProviderService) bundleContext - .getService(protocolProviderRef); + ProtocolProviderService provider + = (ProtocolProviderService) + bundleContext.getService(protocolProviderRef); this.handleProviderAdded(provider); } @@ -153,13 +327,13 @@ public class OtrActivator if(!OSUtils.IS_ANDROID) { - Hashtable<String, String> containerFilter = new Hashtable<String, String>(); + Hashtable<String, String> containerFilter + = new Hashtable<String, String>(); // Register the right-click menu item. containerFilter.put(Container.CONTAINER_ID, Container.CONTAINER_CONTACT_RIGHT_BUTTON_MENU.getID()); - bundleContext .registerService(PluginComponent.class.getName(), new OtrMetaContactMenu( @@ -197,6 +371,7 @@ public class OtrActivator { Dictionary<String, String> properties = new Hashtable<String, String>(); + properties.put( ConfigurationForm.FORM_TYPE, ConfigurationForm.SECURITY_TYPE); // Register the configuration form. @@ -210,43 +385,6 @@ public class OtrActivator } } - /** - * The dependent class. We are waiting for the ui service. - * @return the ui service class. - */ - @Override - public Class<?> getDependentServiceClass() - { - return UIService.class; - } - - /** - * The bundle context to use. - * @param context the context to set. - */ - @Override - public void setBundleContext(BundleContext context) - { - bundleContext = context; - } - - private void handleProviderAdded(ProtocolProviderService provider) - { - OperationSetInstantMessageTransform opSetMessageTransform - = provider - .getOperationSet(OperationSetInstantMessageTransform.class); - - if (opSetMessageTransform != null) - { - opSetMessageTransform.addTransformLayer(this.otrTransformLayer); - } - else - { - if (logger.isTraceEnabled()) - logger.trace("Service did not have a transform op. set."); - } - } - /* * Implements BundleActivator#stop(BundleContext). */ @@ -284,139 +422,4 @@ public class OtrActivator } } } - - private void handleProviderRemoved(ProtocolProviderService provider) - { - // check whether the provider has a basic im operation set - OperationSetInstantMessageTransform opSetMessageTransform - = provider - .getOperationSet(OperationSetInstantMessageTransform.class); - - if (opSetMessageTransform != null) - { - opSetMessageTransform.removeTransformLayer(this.otrTransformLayer); - } - } - - /* - * Implements ServiceListener#serviceChanged(ServiceEvent). - */ - public void serviceChanged(ServiceEvent serviceEvent) - { - Object sService = - bundleContext.getService(serviceEvent.getServiceReference()); - - if (logger.isTraceEnabled()) - logger.trace("Received a service event for: " - + sService.getClass().getName()); - - // we don't care if the source service is not a protocol provider - if (!(sService instanceof ProtocolProviderService)) - return; - - if (logger.isDebugEnabled()) - logger.debug("Service is a protocol provider."); - if (serviceEvent.getType() == ServiceEvent.REGISTERED) - { - if (logger.isDebugEnabled()) - logger.debug("Handling registration of a new Protocol Provider."); - - this.handleProviderAdded((ProtocolProviderService) sService); - } - else if (serviceEvent.getType() == ServiceEvent.UNREGISTERING) - { - this.handleProviderRemoved((ProtocolProviderService) sService); - } - - } - - /** - * Gets all the available accounts in SIP Communicator. - * - * @return a {@link List} of {@link AccountID}. - */ - public static List<AccountID> getAllAccountIDs() - { - Map<Object, ProtocolProviderFactory> providerFactoriesMap = - OtrActivator.getProtocolProviderFactories(); - - if (providerFactoriesMap == null) - return null; - - List<AccountID> accountIDs = new Vector<AccountID>(); - for (ProtocolProviderFactory providerFactory : providerFactoriesMap - .values()) - { - for (AccountID accountID : providerFactory.getRegisteredAccounts()) - { - accountIDs.add(accountID); - } - } - - return accountIDs; - } - - /** - * Gets an {@link AccountID} by its UID. - * - * @param uid The {@link AccountID} UID. - * @return The {@link AccountID} with the requested UID or null. - */ - public static AccountID getAccountIDByUID(String uid) - { - if (uid == null || uid.length() < 1) - return null; - - Map<Object, ProtocolProviderFactory> providerFactoriesMap = - OtrActivator.getProtocolProviderFactories(); - - if (providerFactoriesMap == null) - return null; - - for (ProtocolProviderFactory providerFactory : providerFactoriesMap - .values()) - { - for (AccountID accountID : providerFactory.getRegisteredAccounts()) - { - if (accountID.getAccountUniqueID().equals(uid)) - return accountID; - } - } - - return null; - } - - private static Map<Object, ProtocolProviderFactory> getProtocolProviderFactories() - { - ServiceReference[] serRefs = null; - try - { - // get all registered provider factories - serRefs = - bundleContext.getServiceReferences( - ProtocolProviderFactory.class.getName(), null); - - } - catch (InvalidSyntaxException ex) - { - logger.error("Error while retrieving service refs", ex); - return null; - } - - Map<Object, ProtocolProviderFactory> providerFactoriesMap = - new Hashtable<Object, ProtocolProviderFactory>(); - if (serRefs != null) - { - for (ServiceReference serRef : serRefs) - { - ProtocolProviderFactory providerFactory = - (ProtocolProviderFactory) bundleContext.getService(serRef); - - providerFactoriesMap.put(serRef - .getProperty(ProtocolProviderFactory.PROTOCOL), - providerFactory); - } - } - return providerFactoriesMap; - } } 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;
}
}
}
|