Mercurial > p > roundup > code
comparison roundup/scripts/roundup_mailgw.py @ 4000:39af38d6f77d
Add use of username/password stored in ~/.netrc in mailgw
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Tue, 19 Aug 2008 01:01:32 +0000 |
| parents | ee73abcc95d2 |
| children | 82bdcff42c17 |
comparison
equal
deleted
inserted
replaced
| 3999:953234004ba2 | 4000:39af38d6f77d |
|---|---|
| 12 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 12 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 13 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" | 13 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" |
| 14 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, | 14 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
| 15 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | 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 """Command-line script stub that calls the roundup.mailgw. | 19 """Command-line script stub that calls the roundup.mailgw. |
| 20 """ | 20 """ |
| 21 __docformat__ = 'restructuredtext' | 21 __docformat__ = 'restructuredtext' |
| 22 | 22 |
| 23 # python version check | 23 # python version check |
| 24 from roundup import version_check | 24 from roundup import version_check |
| 25 from roundup import __version__ as roundup_version | 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 from roundup import mailgw | 29 from roundup import mailgw |
| 30 from roundup.i18n import _ | 30 from roundup.i18n import _ |
| 31 | 31 |
| 32 def usage(args, message=None): | 32 def usage(args, message=None): |
| 65 file and submits each in turn to the roundup.mailgw module. The file is | 65 file and submits each in turn to the roundup.mailgw module. The file is |
| 66 emptied once all messages have been successfully handled. The file is | 66 emptied once all messages have been successfully handled. The file is |
| 67 specified as: | 67 specified as: |
| 68 mailbox /path/to/mailbox | 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 POP: | 77 POP: |
| 71 In the third case, the gateway reads all messages from the POP server | 78 In the third case, the gateway reads all messages from the POP server |
| 72 specified and submits each in turn to the roundup.mailgw module. The | 79 specified and submits each in turn to the roundup.mailgw module. The |
| 73 server is specified as: | 80 server is specified as: |
| 74 pop username:password@server | 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 pop username@server | 83 pop username@server |
| 77 pop server | 84 pop server |
| 78 are both valid. The username and/or password will be prompted for if | 85 are both valid. |
| 79 not supplied on the command-line. | |
| 80 | 86 |
| 81 POPS: | 87 POPS: |
| 82 Connect to a POP server over ssl. This requires python 2.4 or later. | 88 Connect to a POP server over ssl. This requires python 2.4 or later. |
| 83 This supports the same notation as POP. | 89 This supports the same notation as POP. |
| 84 | 90 |
| 156 if hasattr(socket, 'setdefaulttimeout'): | 162 if hasattr(socket, 'setdefaulttimeout'): |
| 157 socket.setdefaulttimeout(60) | 163 socket.setdefaulttimeout(60) |
| 158 | 164 |
| 159 if source == 'mailbox': | 165 if source == 'mailbox': |
| 160 return handler.do_mailbox(specification) | 166 return handler.do_mailbox(specification) |
| 161 elif source == 'pop' or source == 'pops': | 167 |
| 162 m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)', | 168 # Try parsing the netrc file first. |
| 163 specification) | 169 try: |
| 164 if m: | 170 authenticator = netrc.netrc().authenticators(specification) |
| 165 ssl = source.endswith('s') | 171 username = authenticator[0] |
| 166 if ssl and sys.version_info<(2,4): | 172 password = authenticator[2] |
| 167 return usage(argv, _('Error: a later version of python is required')) | 173 server = specification |
| 168 return handler.do_pop(m.group('server'), m.group('user'), | 174 # IOError if no ~/.netrc file, TypeError if the hostname |
| 169 m.group('pass'),ssl) | 175 # not found in the ~/.netrc file: |
| 170 return usage(argv, _('Error: pop specification not valid')) | 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 elif source == 'apop': | 191 elif source == 'apop': |
| 172 m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)', | 192 return handler.do_apop(server, username, password) |
| 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')) | |
| 178 elif source == 'imap' or source == 'imaps': | 193 elif source == 'imap' or source == 'imaps': |
| 179 m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)', | 194 ssl = source.endswith('s') |
| 180 specification) | 195 mailbox = '' |
| 181 if m: | 196 if len(args) > 3: |
| 182 ssl = source.endswith('s') | 197 mailbox = args[3] |
| 183 mailbox = '' | 198 return handler.do_imap(server, username, password, mailbox, ssl) |
| 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) | |
| 188 | 199 |
| 189 return usage(argv, _('Error: The source must be either "mailbox",' | 200 return usage(argv, _('Error: The source must be either "mailbox",' |
| 190 ' "pop", "apop", "imap" or "imaps"')) | 201 ' "pop", "apop", "imap" or "imaps"')) |
| 191 finally: | 202 finally: |
| 192 # handler might have closed the initial db and opened a new one | 203 # handler might have closed the initial db and opened a new one |
