Subversion Repositories javautils

Rev

Rev 3 | Rev 17 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 9
Line 3... Line 3...
3
import java.util.Arrays;
3
import java.util.Arrays;
4
import java.util.HashSet;
4
import java.util.HashSet;
5
import java.util.regex.Pattern;
5
import java.util.regex.Pattern;
6
 
6
 
7
import de.viathinksoft.utils.mail.EMailAddress;
7
import de.viathinksoft.utils.mail.EMailAddress;
8
import de.viathinksoft.utils.mail.InvalidMailAddressException;
-
 
9
 
8
 
10
/**
9
/**
-
 
10
 * This class is not stable. For a good syntax check, please use the classes of
-
 
11
 * Dominic Sayers or Cal Henderson.
11
 *
12
 *
12
 * @author Daniel Marschall
13
 * @author Daniel Marschall
-
 
14
 * @version 0.1
13
 *
15
 *
14
 */
16
 */
15
public class MailSyntaxChecker {
17
public class MailSyntaxChecker {
16
       
18
 
17
        private static final String REGEX_IP = "\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";
19
        private static final String REGEX_IP = "\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";
Line 102... Line 104...
102
                // TODO
104
                // TODO
103
               
105
 
104
                return true;
106
                return true;
105
        }
107
        }
106
 
108
 
107
        public static boolean isMailValid(String email)
109
        public static boolean isMailValid(String email) {
108
                        throws InvalidMailAddressException {
-
 
109
                return isMailValid(new EMailAddress(email));
110
                return isMailValid(new EMailAddress(email));
110
        }
111
        }
111
 
112
 
112
        /**
113
        /**
113
         * Checks if an E-Mail-Address is valid
114
         * Checks if an E-Mail-Address is valid
Line 133... Line 134...
133
                if (address.contains("..")) {
134
                if (address.contains("..")) {
134
                        return false;
135
                        return false;
135
                }
136
                }
136
 
137
 
137
                // localPart darf keine Punkte am Anfang oder Ende besitzen
138
                // localPart darf keine Punkte am Anfang oder Ende besitzen
-
 
139
               
138
                if (localPart.length() == 0) return false;
140
                if (localPart.length() == 0) {
139
                String lpFirstChar = localPart.substring(0, 1);
141
                        return false;
140
                String lpLastChar = localPart.substring(localPart.length()-1);
-
 
-
 
142
                }
141
                if (lpFirstChar.equals(".") || (lpLastChar.equals("."))) {
143
                if (localPart.startsWith(".") || localPart.endsWith(".")) {
142
                        return false;
144
                        return false;
143
                }
145
                }
144
 
146
 
145
                // domainPart darf keine Punkte am Anfang oder Ende besitzen
147
                // domainPart darf keine Punkte am Anfang oder Ende besitzen
146
 
148
 
147
                String dpFirstChar = domainPart.substring(0, 1);
-
 
148
                String dpLastChar = domainPart.substring(domainPart.length()-1);
-
 
149
 
-
 
150
                if (dpFirstChar.equals(".") || (dpLastChar.equals("."))) {
149
                if (domainPart.startsWith(".") || domainPart.endsWith(".")) {
151
                        return false;
150
                        return false;
152
                }
151
                }
153
 
152
 
154
                // domainPart prüfen
153
                // domainPart prüfen
155
               
154
 
Line 158... Line 157...
158
                        // QUE: Ist das überhaupt gemäß RFC gültig?
157
                        // QUE: Ist das überhaupt gemäß RFC gültig?
159
                       
158
 
160
                        String ip = ""; // TODO
159
                        String ip = ""; // TODO
161
                       
160
 
162
                        if (CHECK_DNS) {
161
                        if (CHECK_DNS) {
163
                                if (!checkDns(ip)) return false;
162
                                if (!checkDns(ip))
-
 
163
                                        return false;
164
                        }
164
                        }
165
                } else if (preg_match("^\\["+REGEX_IP+"\\]$", domainPart)) {
165
                } else if (preg_match("^\\[" + REGEX_IP + "\\]$", domainPart)) {
166
                        // domainPart is [<IP>]
166
                        // domainPart is [<IP>]
167
                       
167
 
168
                        String ip = ""; // TODO
168
                        String ip = ""; // TODO
169
                       
169
 
170
                        if (CHECK_DNS) {
170
                        if (CHECK_DNS) {
171
                                if (!checkDns(ip)) return false;
171
                                if (!checkDns(ip))
-
 
172
                                        return false;
172
                        }
173
                        }
173
                } else {
174
                } else {
174
                        if (!preg_match("^[A-Za-z0-9\\-\\.]+$", domainPart)) {
175
                        if (!preg_match("^[A-Za-z0-9\\-\\.]+$", domainPart)) {
175
                                return false;
176
                                return false;
176
                        }
177
                        }
Line 179... Line 180...
179
                                if (!checkTldRecognized(email))
180
                                if (!checkTldRecognized(email))
180
                                        return false;
181
                                        return false;
181
                        }
182
                        }
182
                       
183
 
183
                        if (CHECK_DNS) {
184
                        if (CHECK_DNS) {
184
                                if (!checkDns(domainPart)) return false;
185
                                if (!checkDns(domainPart))
-
 
186
                                        return false;
185
                        }
187
                        }
186
                }
188
                }
187
               
189
 
188
                // localPart prüfen
190
                // localPart prüfen
189
               
191
 
190
                if (!preg_match("^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$",
192
                if (!preg_match("^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$",
191
                                localPart.replaceAll("\\\\", "").replaceAll("@", "") )) {
193
                                localPart.replaceAll("\\\\", "").replaceAll("@", ""))) {
192
                        // character not valid in local part unless
194
                        // character not valid in local part unless
193
                        // local part is quoted
195
                        // local part is quoted
194
                        if (!preg_match("^\"(\\\\\"|[^\"])+\"$",
196
                        if (!preg_match("^\"(\\\\\"|[^\"])+\"$", localPart.replaceAll(
195
                                          localPart.replaceAll("\\\\", "").replaceAll("@", "") )) {
197
                                        "\\\\", "").replaceAll("@", ""))) {
196
                                return false;
198
                                return false;
197
                        }
199
                        }
198
                }
200
                }
199
               
201
 
200
                // TODO: Weitere Tests gemäß RFC?
202
                // TODO: Weitere Tests gemäß RFC?