Code

version info in scripts
[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.12 2004-04-05 23:43:03 richard Exp $
19 """Command-line script stub that calls the roundup.mailgw.
20 """
21 __docformat__ = 'restructuredtext'
23 # python version check
24 from roundup import version_check
25 from roundup import __version__ as roundup_version
27 import sys, os, re, cStringIO, getopt
29 from roundup.mailgw import Message
30 from roundup.i18n import _
32 def usage(args, message=None):
33     if message is not None:
34         print message
35     print _('Usage: %(program)s [-v] [[-C class] -S field=value]* <instance '
36         'home> [method]')%{'program': args[0]}
37     print _('''
38 Options:
39  -v: print version and exit
40  -C / -S: see below
42 The roundup mail gateway may be called in one of three ways:
43  . with an instance home as the only argument,
44  . with both an instance home and a mail spool file, or
45  . with both an instance home and a pop server account.
46  
47 It also supports optional -C and -S arguments that allows you to set a
48 fields for a class created by the roundup-mailgw. The default class if
49 not specified is msg, but the other classes: issue, file, user can
50 also be used. The -S or --set options uses the same
51 property=value[;property=value] notation accepted by the command line
52 roundup command or the commands that can be given on the Subject line
53 of an email message.
55 It can let you set the type of the message on a per email address basis.
57 PIPE:
58  In the first case, the mail gateway reads a single message from the
59  standard input and submits the message to the roundup.mailgw module.
61 UNIX mailbox:
62  In the second case, the gateway reads all messages from the mail spool
63  file and submits each in turn to the roundup.mailgw module. The file is
64  emptied once all messages have been successfully handled. The file is
65  specified as:
66    mailbox /path/to/mailbox
68 POP:
69  In the third case, the gateway reads all messages from the POP server
70  specified and submits each in turn to the roundup.mailgw module. The
71  server is specified as:
72     pop username:password@server
73  The username and password may be omitted:
74     pop username@server
75     pop server
76  are both valid. The username and/or password will be prompted for if
77  not supplied on the command-line.
79 APOP:
80  Same as POP, but using Authenticated POP:
81     apop username:password@server
83 ''')
84     return 1
86 def main(argv):
87     '''Handle the arguments to the program and initialise environment.
88     '''
89     # take the argv array and parse it leaving the non-option
90     # arguments in the args array.
91     try:
92         optionsList, args = getopt.getopt(argv[1:], 'vC:S:', ['set=', 'class='])
93     except getopt.GetoptError:
94         # print help information and exit:
95         usage(argv)
96         sys.exit(2)
98     for (opt, arg) in optionsList:
99         if opt == '-v':
100             print '%s (python %s)'%(roundup_version, sys.version.split()[0])
101             return
103     # figure the instance home
104     if len(args) > 0:
105         instance_home = args[0]
106     else:
107         instance_home = os.environ.get('ROUNDUP_INSTANCE', '')
108     if not instance_home:
109         return usage(argv)
111     # get the instance
112     import roundup.instance
113     instance = roundup.instance.open(instance_home)
115     # get a mail handler
116     db = instance.open('admin')
118     # now wrap in try/finally so we always close the database
119     try:
120         handler = instance.MailGW(instance, db, optionsList)
122         # if there's no more arguments, read a single message from stdin
123         if len(args) == 1:
124             return handler.do_pipe()
126         # otherwise, figure what sort of mail source to handle
127         if len(args) < 3:
128             return usage(argv, _('Error: not enough source specification information'))
129         source, specification = args[1:]
130         if source == 'mailbox':
131             return handler.do_mailbox(specification)
132         elif source == 'pop':
133             m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)',
134                 specification)
135             if m:
136                 return handler.do_pop(m.group('server'), m.group('user'),
137                     m.group('pass'))
138             return usage(argv, _('Error: pop specification not valid'))
139         elif source == 'apop':
140             m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)',
141                 specification)
142             if m:
143                 return handler.do_apop(m.group('server'), m.group('user'),
144                     m.group('pass'))
145             return usage(argv, _('Error: apop specification not valid'))
147         return usage(argv, _('Error: The source must be either "mailbox", "pop" or "apop"'))
148     finally:
149         db.close()
151 def run():
152     # time out after a minute if we can
153     import socket
154     if hasattr(socket, 'setdefaulttimeout'):
155         socket.setdefaulttimeout(60)
156     sys.exit(main(sys.argv))
158 # call main
159 if __name__ == '__main__':
160     run()
162 # vim: set filetype=python ts=4 sw=4 et si