Code

missed this part of the patch, added doc
[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.7 2003-01-12 00:03:11 richard Exp $
19 # python version check
20 from roundup import version_check
22 import sys, os, re, cStringIO, getopt
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 [[-C class] -S field=value]* <instance '
31         'home> [method]')%{'program': args[0]}
32     print _('''
34 The roundup mail gateway may be called in one of three ways:
35  . with an instance home as the only argument,
36  . with both an instance home and a mail spool file, or
37  . with both an instance home and a pop server account.
38  
39 It also supports optional -C and -S arguments that allows you to set a
40 fields for a class created by the roundup-mailgw. The default class if
41 not specified is msg, but the other classes: issue, file, user can
42 also be used. The -S or --set options uses the same
43 property=value[;property=value] notation accepted by the command line
44 roundup command or the commands that can be given on the Subject line
45 of an email message.
47 It can let you set the type of the message on a per email address basis.
49 PIPE:
50  In the first case, the mail gateway reads a single message from the
51  standard input and submits the message to the roundup.mailgw module.
53 UNIX mailbox:
54  In the second case, the gateway reads all messages from the mail spool
55  file and submits each in turn to the roundup.mailgw module. The file is
56  emptied once all messages have been successfully handled. The file is
57  specified as:
58    mailbox /path/to/mailbox
60 POP:
61  In the third case, the gateway reads all messages from the POP server
62  specified and submits each in turn to the roundup.mailgw module. The
63  server is specified as:
64     pop username:password@server
65  The username and password may be omitted:
66     pop username@server
67     pop server
68  are both valid. The username and/or password will be prompted for if
69  not supplied on the command-line.
70 ''')
71     return 1
73 def main(argv):
74     '''Handle the arguments to the program and initialise environment.
75     '''
76     # take the argv array and parse it leaving the non-option
77     # arguments in the args array.
78     try:
79         optionsList, args = getopt.getopt(argv[1:], 'C:S:', ['set=', 'class='])
80     except getopt.GetoptError:
81         # print help information and exit:
82         usage(argv)
83         sys.exit(2)
85     # figure the instance home
86     if len(args) > 0:
87         instance_home = args[0]
88     else:
89         instance_home = os.environ.get('ROUNDUP_INSTANCE', '')
90     if not instance_home:
91         return usage(argv)
93     # get the instance
94     import roundup.instance
95     instance = roundup.instance.open(instance_home)
97     # get a mail handler
98     db = instance.open('admin')
100     # now wrap in try/finally so we always close the database
101     try:
102         handler = instance.MailGW(instance, db, optionsList)
104         # if there's no more arguments, read a single message from stdin
105         if len(args) == 1:
106             return handler.do_pipe()
108         # otherwise, figure what sort of mail source to handle
109         if len(args) < 3:
110             return usage(argv, _('Error: not enough source specification information'))
111         source, specification = args[1:]
112         if source == 'mailbox':
113             return handler.do_mailbox(specification)
114         elif source == 'pop':
115             m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)',
116                 specification)
117             if m:
118                 return handler.do_pop(m.group('server'), m.group('user'),
119                     m.group('pass'))
120             return usage(argv, _('Error: pop specification not valid'))
122         return usage(argv, _('Error: The source must be either "mailbox" or "pop"'))
123     finally:
124         db.close()
126 def run():
127     sys.exit(main(sys.argv))
129 # call main
130 if __name__ == '__main__':
131     run()
133 # vim: set filetype=python ts=4 sw=4 et si