1414# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1515# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1616#
17- # $Id: roundup_mailgw.py,v 1.6 2002-09-13 00:08:44 richard Exp $
17+ # $Id: roundup_mailgw.py,v 1.7 2003-01-12 00:03:11 richard Exp $
1818
1919# python version check
2020from roundup import version_check
2121
22- import sys , os , re , cStringIO
22+ import sys , os , re , cStringIO , getopt
2323
2424from roundup .mailgw import Message
2525from roundup .i18n import _
2626
2727def usage (args , message = None ):
2828 if message is not None :
2929 print message
30- print _ ('Usage: %(program)s <instance home> [method]' )% {'program' : args [0 ]}
30+ print _ ('Usage: %(program)s [[-C class] -S field=value]* <instance '
31+ 'home> [method]' )% {'program' : args [0 ]}
3132 print _ ('''
3233
3334The roundup mail gateway may be called in one of three ways:
3435 . with an instance home as the only argument,
3536 . with both an instance home and a mail spool file, or
3637 . 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.
46+
47+ It can let you set the type of the message on a per email address basis.
3748
3849PIPE:
3950 In the first case, the mail gateway reads a single message from the
@@ -59,16 +70,25 @@ def usage(args, message=None):
5970''' )
6071 return 1
6172
62- def main (args ):
73+ def main (argv ):
6374 '''Handle the arguments to the program and initialise environment.
6475 '''
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 )
84+
6585 # figure the instance home
66- if len (args ) > 1 :
67- instance_home = args [1 ]
86+ if len (args ) > 0 :
87+ instance_home = args [0 ]
6888 else :
6989 instance_home = os .environ .get ('ROUNDUP_INSTANCE' , '' )
7090 if not instance_home :
71- return usage (args )
91+ return usage (argv )
7292
7393 # get the instance
7494 import roundup .instance
@@ -79,16 +99,16 @@ def main(args):
7999
80100 # now wrap in try/finally so we always close the database
81101 try :
82- handler = instance .MailGW (instance , db )
102+ handler = instance .MailGW (instance , db , optionsList )
83103
84104 # if there's no more arguments, read a single message from stdin
85- if len (args ) == 2 :
105+ if len (args ) == 1 :
86106 return handler .do_pipe ()
87107
88108 # 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 :]
109+ if len (args ) < 3 :
110+ return usage (argv , _ ('Error: not enough source specification information' ))
111+ source , specification = args [1 :]
92112 if source == 'mailbox' :
93113 return handler .do_mailbox (specification )
94114 elif source == 'pop' :
@@ -97,9 +117,9 @@ def main(args):
97117 if m :
98118 return handler .do_pop (m .group ('server' ), m .group ('user' ),
99119 m .group ('pass' ))
100- return usage (args , _ ('Error: pop specification not valid' ))
120+ return usage (argv , _ ('Error: pop specification not valid' ))
101121
102- return usage (args , _ ('Error: The source must be either "mailbox" or "pop"' ))
122+ return usage (argv , _ ('Error: The source must be either "mailbox" or "pop"' ))
103123 finally :
104124 db .close ()
105125
0 commit comments