Code

*** empty log message ***
[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.9 2003-03-24 02:56:30 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.
71 APOP:
72  Same as POP, but using Authenticated POP:
73     apop username:password@server
75 ''')
76     return 1
78 def main(argv):
79     '''Handle the arguments to the program and initialise environment.
80     '''
81     # take the argv array and parse it leaving the non-option
82     # arguments in the args array.
83     try:
84         optionsList, args = getopt.getopt(argv[1:], 'C:S:', ['set=', 'class='])
85     except getopt.GetoptError:
86         # print help information and exit:
87         usage(argv)
88         sys.exit(2)
90     # figure the instance home
91     if len(args) > 0:
92         instance_home = args[0]
93     else:
94         instance_home = os.environ.get('ROUNDUP_INSTANCE', '')
95     if not instance_home:
96         return usage(argv)
98     # get the instance
99     import roundup.instance
100     instance = roundup.instance.open(instance_home)
102     # get a mail handler
103     db = instance.open('admin')
105     # now wrap in try/finally so we always close the database
106     try:
107         handler = instance.MailGW(instance, db, optionsList)
109         # if there's no more arguments, read a single message from stdin
110         if len(args) == 1:
111             return handler.do_pipe()
113         # otherwise, figure what sort of mail source to handle
114         if len(args) < 3:
115             return usage(argv, _('Error: not enough source specification information'))
116         source, specification = args[1:]
117         if source == 'mailbox':
118             return handler.do_mailbox(specification)
119         elif source == 'pop':
120             m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)',
121                 specification)
122             if m:
123                 return handler.do_pop(m.group('server'), m.group('user'),
124                     m.group('pass'))
125             return usage(argv, _('Error: pop specification not valid'))
126         elif source == 'apop':
127             m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)',
128                 specification)
129             if m:
130                 return handler.do_apop(m.group('server'), m.group('user'),
131                     m.group('pass'))
132             return usage(argv, _('Error: apop specification not valid'))
134         return usage(argv, _('Error: The source must be either "mailbox", "pop" or "apop"'))
135     finally:
136         db.close()
138 def run():
139     sys.exit(main(sys.argv))
141 # call main
142 if __name__ == '__main__':
143     run()
145 # vim: set filetype=python ts=4 sw=4 et si