summaryrefslogtreecommitdiff
path: root/src/mailman/email/validate.py
diff options
context:
space:
mode:
authorBarry Warsaw2009-02-12 22:52:18 -0500
committerBarry Warsaw2009-02-12 22:52:18 -0500
commit8d43a75b5c7a278bbf9f5bd5143bee4b2925f3b1 (patch)
tree49538d15e71f0b520758f6e2f189aa5f143baaa3 /src/mailman/email/validate.py
parent8b8586ca375b23787b5a74761773cd759f74994e (diff)
downloadmailman-8d43a75b5c7a278bbf9f5bd5143bee4b2925f3b1.tar.gz
mailman-8d43a75b5c7a278bbf9f5bd5143bee4b2925f3b1.tar.zst
mailman-8d43a75b5c7a278bbf9f5bd5143bee4b2925f3b1.zip
Diffstat (limited to 'src/mailman/email/validate.py')
-rw-r--r--src/mailman/email/validate.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/mailman/email/validate.py b/src/mailman/email/validate.py
new file mode 100644
index 000000000..fc33b1509
--- /dev/null
+++ b/src/mailman/email/validate.py
@@ -0,0 +1,70 @@
+# Copyright (C) 2009 by the Free Software Foundation, Inc.
+#
+# This file is part of GNU Mailman.
+#
+# GNU Mailman is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option)
+# any later version.
+#
+# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
+
+"""Module stuff."""
+
+from __future__ import absolute_import, unicode_literals
+
+__metaclass__ = type
+__all__ = [
+ 'is_valid',
+ 'validate',
+ ]
+
+
+import re
+
+from mailman.core.errors import InvalidEmailAddress
+from mailman.email.utils import split_email
+
+
+# What other characters should be disallowed?
+_badchars = re.compile(r'[][()<>|;^,\000-\037\177-\377]')
+
+
+
+def validate(address):
+ """Validate an email address.
+
+ :param address: An email address.
+ :type address: string
+ :raise `InvalidEmailAddress`: when the address is deemed invalid.
+ """
+ if not is_valid(address):
+ raise InvalidEmailAddress(repr(address))
+
+
+
+def is_valid(address):
+ """Check if an email address if valid.
+
+ :param address: An email address.
+ :type address: string
+ :return: A flag indicating whether the email address is okay or not.
+ :rtype: bool
+ """
+ if not address or ' ' in address:
+ return False
+ if _badchars.search(address) or address[0] == '-':
+ return False
+ user, domain_parts = split_email(address)
+ # Local, unqualified addresses are not allowed.
+ if not domain_parts:
+ return False
+ if len(domain_parts) < 2:
+ return False
+ return True