package com.kiasoft.util; /* * ------------------------------------------------------------------ * Kiasoft, Inc. * http://kiasoft.com * * Copyright (c) 2003-2006 Kiasoft, Inc. All Rights Reserved. Permission * to copy, modify and distribute this software and code * included and its documentation (collectively, the "PROGRAM") for * any purpose is hereby prohibited. * * THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT * LIMITATION, WARRANTIES THAT THE PROGRAM IS FREE OF * DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR * NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND * PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD ANY PART * OF THE PROGRAM PROVE DEFECTIVE IN ANY RESPECT, YOU * (NOT KIASOFT) ASSUME THE COST OF ANY NECESSARY SERVICING, * REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES * AN ESSENTIAL PART OF THIS LICENSE. NO USE OF * THE PROGRAM IS AUTHORIZED HEREUNDER EXCEPT * UNDER THIS DISCLAIMER. * * ------------------------------------------------------------------ */ // Turbine // import org.apache.turbine.util.Log; // ORO import org.apache.oro.text.regex.*; // xbill dnsjava import org.xbill.DNS.Record; import org.xbill.DNS.dns; import org.xbill.DNS.Type; /** * Email validation utility * *@author painter */ public class EmailValidator { /* * string to validate email pattern */ private final static String basicAddress = "^\\w+([_]*[\\-\\.]?[_]*\\w+[_]*)*@\\w+([_]*[\\-\\.]?[_]*\\w+[_]*)*(\\.\\w{1}\\w+)+$"; /** * Test the email address input string * *@param email Description of the Parameter *@return Description of the Return Value *@exception Exception Description of the Exception */ public boolean validate(String email) throws Exception { if (email == null) { throw new Exception("Validator: Email address is null."); } email = email.trim(); if (email.length() == 0) { throw new Exception("Validator: Email address is empty (0-length)"); } // don't allow illegal characters in email address final String illegal[] = {"!", "#", "$", "%", "^", "&", "*", "(", ")", "[", "]", "{", "}", "+", "=", "<", ">", "?", "/", "\\", "|", ":", ";" }; int illegalChar = 0; for (int x = 0; x < illegal.length; x++) { illegalChar = email.lastIndexOf(illegal[x]); if (illegalChar > 0) { throw new Exception("Validator: Email address [" + email + "] contains an illegal character - " + illegal[x]); } illegalChar = 0; } int loc = email.lastIndexOf("@"); if (loc <= 0) { throw new Exception("Validator: Email address [" + email + "] does not have @ symbol."); } int groups; PatternMatcher matcher; PatternCompiler compiler; Pattern pattern; PatternMatcherInput input; MatchResult result; compiler = new Perl5Compiler(); matcher = new Perl5Matcher(); try { pattern = compiler.compile(basicAddress); } catch(MalformedPatternException e) { throw new Exception("Validator: pattern cannot be compiled: " + e.getMessage() ); } input = new PatternMatcherInput(email); while(matcher.contains(input, pattern)) { result = matcher.getMatch(); if ( result.length() > 0 ) { boolean dtest = false; dtest = this.validateDomain( email ); if ( dtest ) { return true; } else { throw new Exception("The domain found for the address: " + email + " is not valid."); } } else { throw new Exception("Validator: '" + email + "' does not look like an internet email address: a@b.c"); } } return false; } /** * copied from scarab implementation for validating email addresses * private boolean checkRFC2505(String email) */ public boolean validateDomain(String email) { // try just the end portion of the domain String domain = getDomain(email); if (domain != null) { // try to find any A records for the domain try { // Search for the MX record first, if found, then proceed Record[] records = dns.getRecords(domain, Type.MX); if (records != null || records.length > 0) { return true; } records = dns.getRecords(domain, Type.A); if (records != null || records.length > 0) { return true; } // now try just the domain after the @ // this is for domains like foo.co.uk String fullDomain = email.substring(email.indexOf('@')+1); records = dns.getRecords(fullDomain, Type.A); if (records != null || records.length > 0) { return true; } // now try to find any MX records for the fullDomain records = dns.getRecords(fullDomain, Type.MX); if (records != null || records.length > 0) { return true; } } catch ( Exception e ) { // Log.error( "Error validating domain: " + e.toString() ); return false; } } return false; } /** * If email: jon@foo.bar.com then return bar.com * *@param email Description of the Parameter *@return The domain value */ private static String getDomain(String email) { String result = null; char[] emailChar = email.toCharArray(); int dotCount = 0; for (int i = emailChar.length - 1; i >= 0; i--) { if (emailChar[i] == '.') { dotCount++; } if (dotCount == 2 || emailChar[i] == '@') { result = email.substring(i + 1, email.length()); break; } } return result; } }