Code

hrm. don't activate detectors if they're just pyc
[roundup.git] / roundup / scripts / roundup_mailgw.py
1 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
2 # This module is free software, and you may redistribute it and/or modify
3 # under the same terms as Python, so long as this copyright message and
4 # disclaimer are retained in their original form.
5 #
6 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
7 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
8 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
9 # POSSIBILITY OF SUCH DAMAGE.
10 #
11 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
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"
14 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
15 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
16
17 # $Id: roundup_mailgw.py,v 1.5 2002-09-11 01:19:16 richard Exp $
19 # python version check
20 from roundup import version_check
22 import sys, os, re, cStringIO
24 from roundup.mailgw import Message
25 from roundup.i18n import _
27 def usage(args, message=None):
28     if message is not None:
29         print message
30     print _('Usage: %(program)s <instance home> [method]')%{'program': args[0]}
31     print _('''
33 The roundup mail gateway may be called in one of three ways:
34  . with an instance home as the only argument,
35  . with both an instance home and a mail spool file, or
36  . with both an instance home and a pop server account.
38 PIPE:
39  In the first case, the mail gateway reads a single message from the
40  standard input and submits the message to the roundup.mailgw module.
42 UNIX mailbox:
43  In the second case, the gateway reads all messages from the mail spool
44  file and submits each in turn to the roundup.mailgw module. The file is
45  emptied once all messages have been successfully handled. The file is
46  specified as:
47    mailbox /path/to/mailbox
49 POP:
50  In the third case, the gateway reads all messages from the POP server
51  specified and submits each in turn to the roundup.mailgw module. The
52  server is specified as:
53     pop username:password@server
54  The username and password may be omitted:
55     pop username@server
56     pop server
57  are both valid. The username and/or password will be prompted for if
58  not supplied on the command-line.
59 ''')
60     return 1
62 def main(args):
63     '''Handle the arguments to the program and initialise environment.
64     '''
65     # figure the instance home
66     if len(args) > 1:
67         instance_home = args[1]
68     else:
69         instance_home = os.environ.get('ROUNDUP_INSTANCE', '')
70     if not instance_home:
71         return usage(args)
73     # get the instance
74     import roundup.instance
75     instance = roundup.instance.open(instance_home)
77     # get a mail handler
78     db = instance.open('admin')
79     handler = instance.MailGW(instance, db)
81     # if there's no more arguments, read a single message from stdin
82     if len(args) == 2:
83         return handler.do_pipe()
85     # otherwise, figure what sort of mail source to handle
86     if len(args) < 4:
87         return usage(args, _('Error: not enough source specification information'))
88     source, specification = args[2:]
89     if source == 'mailbox':
90         return handler.do_mailbox(specification)
91     elif source == 'pop':
92         m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)',
93             specification)
94         if m:
95             return handler.do_pop(m.group('server'), m.group('user'),
96                 m.group('pass'))
97         return usage(args, _('Error: pop specification not valid'))
99     return usage(args, _('Error: The source must be either "mailbox" or "pop"'))
101 def run():
102     sys.exit(main(sys.argv))
104 # call main
105 if __name__ == '__main__':
106     run()
108 # vim: set filetype=python ts=4 sw=4 et si