|
14 | 14 | # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
15 | 15 | # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
16 | 16 | # |
17 | | -# $Id: roundup_mailgw.py,v 1.23 2006-12-13 23:32:39 richard Exp $ |
| 17 | +# $Id: roundup_mailgw.py,v 1.24 2008-08-19 01:01:32 richard Exp $ |
18 | 18 |
|
19 | 19 | """Command-line script stub that calls the roundup.mailgw. |
20 | 20 | """ |
|
24 | 24 | from roundup import version_check |
25 | 25 | from roundup import __version__ as roundup_version |
26 | 26 |
|
27 | | -import sys, os, re, cStringIO, getopt, socket |
| 27 | +import sys, os, re, cStringIO, getopt, socket, netrc |
28 | 28 |
|
29 | 29 | from roundup import mailgw |
30 | 30 | from roundup.i18n import _ |
@@ -67,16 +67,22 @@ def usage(args, message=None): |
67 | 67 | specified as: |
68 | 68 | mailbox /path/to/mailbox |
69 | 69 |
|
| 70 | +In all of the following the username and password can be stored in a |
| 71 | +~/.netrc file. In this case only the server name need be specified on |
| 72 | +the command-line. |
| 73 | +
|
| 74 | +The username and/or password will be prompted for if not supplied on |
| 75 | +the command-line or in ~/.netrc. |
| 76 | +
|
70 | 77 | POP: |
71 | 78 | In the third case, the gateway reads all messages from the POP server |
72 | 79 | specified and submits each in turn to the roundup.mailgw module. The |
73 | 80 | server is specified as: |
74 | 81 | pop username:password@server |
75 | | - The username and password may be omitted: |
| 82 | + Alternatively, one can omit one or both of username and password: |
76 | 83 | pop username@server |
77 | 84 | pop server |
78 | | - are both valid. The username and/or password will be prompted for if |
79 | | - not supplied on the command-line. |
| 85 | + are both valid. |
80 | 86 |
|
81 | 87 | POPS: |
82 | 88 | Connect to a POP server over ssl. This requires python 2.4 or later. |
@@ -158,33 +164,38 @@ def main(argv): |
158 | 164 |
|
159 | 165 | if source == 'mailbox': |
160 | 166 | return handler.do_mailbox(specification) |
161 | | - elif source == 'pop' or source == 'pops': |
162 | | - m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)', |
163 | | - specification) |
164 | | - if m: |
165 | | - ssl = source.endswith('s') |
166 | | - if ssl and sys.version_info<(2,4): |
167 | | - return usage(argv, _('Error: a later version of python is required')) |
168 | | - return handler.do_pop(m.group('server'), m.group('user'), |
169 | | - m.group('pass'),ssl) |
170 | | - return usage(argv, _('Error: pop specification not valid')) |
| 167 | + |
| 168 | + # Try parsing the netrc file first. |
| 169 | + try: |
| 170 | + authenticator = netrc.netrc().authenticators(specification) |
| 171 | + username = authenticator[0] |
| 172 | + password = authenticator[2] |
| 173 | + server = specification |
| 174 | + # IOError if no ~/.netrc file, TypeError if the hostname |
| 175 | + # not found in the ~/.netrc file: |
| 176 | + except (IOError, TypeError): |
| 177 | + match = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)', |
| 178 | + specification) |
| 179 | + if match: |
| 180 | + username = match.group('user') |
| 181 | + password = match.group('pass') |
| 182 | + server = match.group('server') |
| 183 | + else: |
| 184 | + return usage(argv, _('Error: %s specification not valid') % source) |
| 185 | + |
| 186 | + if source == 'pop' or source == 'pops': |
| 187 | + ssl = source.endswith('s') |
| 188 | + if ssl and sys.version_info<(2,4): |
| 189 | + return usage(argv, _('Error: a later version of python is required')) |
| 190 | + return handler.do_pop(server, username, password, ssl) |
171 | 191 | elif source == 'apop': |
172 | | - m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)', |
173 | | - specification) |
174 | | - if m: |
175 | | - return handler.do_apop(m.group('server'), m.group('user'), |
176 | | - m.group('pass')) |
177 | | - return usage(argv, _('Error: apop specification not valid')) |
| 192 | + return handler.do_apop(server, username, password) |
178 | 193 | elif source == 'imap' or source == 'imaps': |
179 | | - m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)', |
180 | | - specification) |
181 | | - if m: |
182 | | - ssl = source.endswith('s') |
183 | | - mailbox = '' |
184 | | - if len(args) > 3: |
185 | | - mailbox = args[3] |
186 | | - return handler.do_imap(m.group('server'), m.group('user'), |
187 | | - m.group('pass'), mailbox, ssl) |
| 194 | + ssl = source.endswith('s') |
| 195 | + mailbox = '' |
| 196 | + if len(args) > 3: |
| 197 | + mailbox = args[3] |
| 198 | + return handler.do_imap(server, username, password, mailbox, ssl) |
188 | 199 |
|
189 | 200 | return usage(argv, _('Error: The source must be either "mailbox",' |
190 | 201 | ' "pop", "apop", "imap" or "imaps"')) |
|
0 commit comments