From 4cd8dff0cdc1582e277aae97908bb18592f9c375 Mon Sep 17 00:00:00 2001 From: Danny van Heumen Date: Mon, 30 Jun 2014 22:47:40 +0200 Subject: [PATCH] Implemented IRC account equality based on userID, host and port. Implemented IRC account equality based on userID, host and port, instead of only on userID, because there are different IRC networks where users can have the same user ID without being the same person. --- .../impl/protocol/irc/IrcAccountID.java | 90 +++++++++++++++---- .../irc/ProtocolProviderFactoryIrcImpl.java | 17 ++-- 2 files changed, 87 insertions(+), 20 deletions(-) diff --git a/src/net/java/sip/communicator/impl/protocol/irc/IrcAccountID.java b/src/net/java/sip/communicator/impl/protocol/irc/IrcAccountID.java index 0dc2b935d..8b956b5ed 100644 --- a/src/net/java/sip/communicator/impl/protocol/irc/IrcAccountID.java +++ b/src/net/java/sip/communicator/impl/protocol/irc/IrcAccountID.java @@ -15,42 +15,102 @@ * * @author Stephane Remy * @author Loic Kempf + * @author Danny van Heumen */ public class IrcAccountID extends AccountID { + /** + * IRC account server host. + */ + private final String host; + /** + * IRC account server port. + */ + private final int port; + /** * Creates an account id from the specified id and account properties. * * @param userID the user identifier corresponding to this account + * @param host IRC server host + * @param port IRC server port * @param accountProperties any other properties necessary for the account. */ - IrcAccountID(String userID, Map accountProperties) + IrcAccountID(final String userID, final String host, final String port, + final Map accountProperties) + { + super(userID, accountProperties, ProtocolNames.IRC, + getServiceName(accountProperties)); + if (host == null) + { + throw new IllegalArgumentException("host cannot be null"); + } + this.host = host; + if (port == null) + { + throw new IllegalArgumentException("port cannot be null"); + } + this.port = Integer.parseInt(port); + } + + /** + * Equality extended with checking IRC server host and port, since different + * IRC networks can have users with similar names. + */ + @Override + public boolean equals(Object obj) { - super( userID, - accountProperties, - ProtocolNames.IRC, - getServiceName(accountProperties)); + if (this == obj) + return true; + + return super.equals(obj) + && this.host.equals(((IrcAccountID) obj).host) + && this.port == ((IrcAccountID) obj).port; } - // TODO Danny: Fix equality: Since we can have completely different IRC - // networks, we should not limit account creation purely on the user's - // id/nickname. + /** + * Get the IRC server host. + * + * @return returns IRC server host + */ + public String getHost() + { + return this.host; + } + + /** + * Get the IRC server port. + * + * @return returns IRC server port + */ + public int getPort() + { + return this.port; + } /** - * Returns the service name - the server we are logging to - * if it is null which is not supposed to be - we return for compatibility - * the string we used in the first release for creating AccountID - * (Using this string is wrong, but used for compatibility for now) + * Returns the service name - the server we are logging to if it is null + * which is not supposed to be - we return for compatibility the string we + * used in the first release for creating AccountID (Using this string is + * wrong, but used for compatibility for now) * * @param accountProperties Map the properties table configuring the account * @return String the service name */ private static String getServiceName(Map accountProperties) { - String serviceName - = accountProperties.get(ProtocolProviderFactory.SERVER_ADDRESS); - + String serviceName = + accountProperties.get(ProtocolProviderFactory.SERVER_ADDRESS); + if (serviceName != null) + { + final String port = + accountProperties.get(ProtocolProviderFactory.SERVER_PORT); + if (port != null) + { + serviceName += ":" + port; + } + } return (serviceName == null) ? ProtocolNames.IRC : serviceName; } } diff --git a/src/net/java/sip/communicator/impl/protocol/irc/ProtocolProviderFactoryIrcImpl.java b/src/net/java/sip/communicator/impl/protocol/irc/ProtocolProviderFactoryIrcImpl.java index 1425f7c8a..3d0f19fe8 100644 --- a/src/net/java/sip/communicator/impl/protocol/irc/ProtocolProviderFactoryIrcImpl.java +++ b/src/net/java/sip/communicator/impl/protocol/irc/ProtocolProviderFactoryIrcImpl.java @@ -60,8 +60,12 @@ public AccountID installAccount( String userIDStr, "The specified property map was null"); accountProperties.put(USER_ID, userIDStr); - - AccountID accountID = new IrcAccountID(userIDStr, accountProperties); + final String host = + accountProperties.get(ProtocolProviderFactory.SERVER_ADDRESS); + final String port = + accountProperties.get(ProtocolProviderFactory.SERVER_PORT); + AccountID accountID = + new IrcAccountID(userIDStr, host, port, accountProperties); //make sure we haven't seen this account id before. if (registeredAccounts.containsKey(accountID)) @@ -85,11 +89,14 @@ public AccountID installAccount( String userIDStr, * {@inheritDoc} */ @Override - protected AccountID createAccountID( - String userID, + protected AccountID createAccountID(String userID, Map accountProperties) { - return new IrcAccountID(userID, accountProperties); + final String host = + accountProperties.get(ProtocolProviderFactory.SERVER_ADDRESS); + final String port = + accountProperties.get(ProtocolProviderFactory.SERVER_PORT); + return new IrcAccountID(userID, host, port, accountProperties); } /**