8 VirtualMailManager package initialization code |
8 VirtualMailManager package initialization code |
9 """ |
9 """ |
10 |
10 |
11 import gettext |
11 import gettext |
12 import os |
12 import os |
13 import re |
|
14 import locale |
13 import locale |
15 |
14 |
16 from encodings.idna import ToASCII, ToUnicode |
|
17 |
15 |
18 from VirtualMailManager.constants.ERROR import \ |
16 from VirtualMailManager.constants.ERROR import \ |
19 DOMAIN_INVALID, DOMAIN_TOO_LONG, NOT_EXECUTABLE, NO_SUCH_BINARY, \ |
17 NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY |
20 NO_SUCH_DIRECTORY |
|
21 from VirtualMailManager.constants.version import __author__, __date__, \ |
18 from VirtualMailManager.constants.version import __author__, __date__, \ |
22 __version__ |
19 __version__ |
23 from VirtualMailManager.errors import VMMError |
20 from VirtualMailManager.errors import VMMError |
24 |
21 |
25 |
22 |
26 __all__ = [ |
23 __all__ = [ |
27 # version information from VERSION |
24 # version information from VERSION |
28 '__author__', '__date__', '__version__', |
25 '__author__', '__date__', '__version__', |
29 # defined stuff |
26 # defined stuff |
30 'ENCODING', 'ace2idna', 'check_domainname', 'exec_ok', 'expand_path', |
27 'ENCODING', 'exec_ok', 'expand_path', 'get_unicode', 'is_dir', |
31 'get_unicode', 'idn2ascii', 'is_dir', |
|
32 ] |
28 ] |
33 |
29 |
34 |
30 |
35 # Try to set all of the locales according to the current |
31 # Try to set all of the locales according to the current |
36 # environment variables and get the character encoding. |
32 # environment variables and get the character encoding. |
37 try: |
33 try: |
38 locale.setlocale(locale.LC_ALL, '') |
34 locale.setlocale(locale.LC_ALL, '') |
39 except locale.Error: |
35 except locale.Error: |
40 locale.setlocale(locale.LC_ALL, 'C') |
36 locale.setlocale(locale.LC_ALL, 'C') |
41 ENCODING = locale.nl_langinfo(locale.CODESET) |
37 ENCODING = locale.nl_langinfo(locale.CODESET) |
42 |
|
43 # there may be many domain and e-mail address checks |
|
44 RE_DOMAIN = re.compile(r"^(?:[a-z0-9-]{1,63}\.){1,}[a-z]{2,6}$") |
|
45 |
38 |
46 gettext.install('vmm', '/usr/local/share/locale', unicode=1) |
39 gettext.install('vmm', '/usr/local/share/locale', unicode=1) |
47 |
40 |
48 |
41 |
49 _ = lambda msg: msg |
42 _ = lambda msg: msg |
88 binary = expand_path(binary) |
81 binary = expand_path(binary) |
89 if not os.path.isfile(binary): |
82 if not os.path.isfile(binary): |
90 raise VMMError(_(u"'%s' is not a file") % get_unicode(binary), |
83 raise VMMError(_(u"'%s' is not a file") % get_unicode(binary), |
91 NO_SUCH_BINARY) |
84 NO_SUCH_BINARY) |
92 if not os.access(binary, os.X_OK): |
85 if not os.access(binary, os.X_OK): |
93 raise VMMError(_(u"File is not executable: '%s'") % |
86 raise VMMError(_(u"File is not executable: '%s'") % |
94 get_unicode(binary), NOT_EXECUTABLE) |
87 get_unicode(binary), NOT_EXECUTABLE) |
95 return binary |
88 return binary |
96 |
89 |
97 |
90 |
98 def idn2ascii(domainname): |
|
99 """Converts the idn domain name `domainname` into punycode.""" |
|
100 return '.'.join([ToASCII(lbl) for lbl in domainname.split('.') if lbl]) |
|
101 |
|
102 |
|
103 def ace2idna(domainname): |
|
104 """Converts the domain name `domainname` from ACE according to IDNA.""" |
|
105 return u'.'.join([ToUnicode(lbl) for lbl in domainname.split('.') if lbl]) |
|
106 |
|
107 |
|
108 def check_domainname(domainname): |
|
109 """Returns the validated domain name `domainname`. |
|
110 |
|
111 It also converts the name of the domain from IDN to ASCII, if |
|
112 necessary. |
|
113 |
|
114 Throws an `VMMError`, if the domain name is too long or doesn't |
|
115 look like a valid domain name (label.label.label). |
|
116 |
|
117 """ |
|
118 if not RE_DOMAIN.match(domainname): |
|
119 domainname = idn2ascii(domainname) |
|
120 if len(domainname) > 255: |
|
121 raise VMMError(_(u'The domain name is too long'), DOMAIN_TOO_LONG) |
|
122 if not RE_DOMAIN.match(domainname): |
|
123 raise VMMError(_(u'The domain name %r is invalid') % domainname, |
|
124 DOMAIN_INVALID) |
|
125 return domainname |
|
126 |
|
127 |
|
128 del _ |
91 del _ |