Skip to content

Commit

Permalink
Simplify how we determine network names.
Browse files Browse the repository at this point in the history
Guava provides a nice class which uses the public suffix list,
so we can ditch all of our hand-crafted logic and pathetically
small list of top level domains and do it properly.
  • Loading branch information
csmith committed Feb 27, 2016
1 parent 5775ab3 commit 7feda9c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
37 changes: 8 additions & 29 deletions src/com/dmdirc/Server.java
Expand Up @@ -55,6 +55,8 @@
import com.dmdirc.ui.messages.Formatter;
import com.dmdirc.ui.messages.HighlightManager;

import com.google.common.net.InternetDomainName;

import java.net.NoRouteToHostException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
Expand Down Expand Up @@ -637,40 +639,17 @@ public boolean isNetwork(final String target) {
}

/**
* Calculates a network name from the specified server name. This method implements parts 2-4 of
* the procedure documented at getNetwork().
* Calculates a network name from the specified server name.
*
* @param serverName The server name to parse
*
* @return A network name for the specified server
*/
protected static String getNetworkFromServerName(final String serverName) {
final String[] parts = serverName.split("\\.");
final String[] tlds = {"biz", "com", "info", "net", "org"};
boolean isTLD = false;

for (String tld : tlds) {
if (serverName.endsWith('.' + tld)) {
isTLD = true;
break;
}
}

if (isTLD && parts.length > 2) {
return parts[parts.length - 2] + '.' + parts[parts.length - 1];
} else if (parts.length > 2) {
final StringBuilder network = new StringBuilder();

for (int i = 1; i < parts.length; i++) {
if (network.length() > 0) {
network.append('.');
}

network.append(parts[i]);
}

return network.toString();
} else {
try {
return InternetDomainName.from(serverName).topPrivateDomain().toString();
} catch (IllegalArgumentException | IllegalStateException ex) {
// Either couldn't parse it as a domain name, or it didn't have a public suffix.
// Just use the server name as-is.
return serverName;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/com/dmdirc/interfaces/Connection.java
Expand Up @@ -121,11 +121,11 @@ public interface Connection {
* Retrieves the name of this server's network. The network name is determined using the
* following rules:
*
* 1. If the server includes its network name in the 005 information, we use that 2. If the
* server's name ends in biz, com, info, net or org, we use the second level domain (e.g.,
* foo.com) 3. If the server's name contains more than two dots, we drop everything up to and
* including the first part, and use the remainder 4. In all other cases, we use the full server
* name
* <ol>
* <li> If the server includes its network name in the 005 information, we use that
* <li> If the domain ends in a public suffix, the top private domain is used.
* <li> In all other cases, we use the full server name
* </ol>
*
* @return The name of this server's network
*/
Expand Down

0 comments on commit 7feda9c

Please sign in to comment.