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.6 2002-09-13 00:08:44 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')
80 # now wrap in try/finally so we always close the database
81 try:
82 handler = instance.MailGW(instance, db)
84 # if there's no more arguments, read a single message from stdin
85 if len(args) == 2:
86 return handler.do_pipe()
88 # otherwise, figure what sort of mail source to handle
89 if len(args) < 4:
90 return usage(args, _('Error: not enough source specification information'))
91 source, specification = args[2:]
92 if source == 'mailbox':
93 return handler.do_mailbox(specification)
94 elif source == 'pop':
95 m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)',
96 specification)
97 if m:
98 return handler.do_pop(m.group('server'), m.group('user'),
99 m.group('pass'))
100 return usage(args, _('Error: pop specification not valid'))
102 return usage(args, _('Error: The source must be either "mailbox" or "pop"'))
103 finally:
104 db.close()
106 def run():
107 sys.exit(main(sys.argv))
109 # call main
110 if __name__ == '__main__':
111 run()
113 # vim: set filetype=python ts=4 sw=4 et si