Code

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