1 #! /usr/bin/python
2 #
3 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
4 # This module is free software, and you may redistribute it and/or modify
5 # under the same terms as Python, so long as this copyright message and
6 # disclaimer are retained in their original form.
7 #
8 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
9 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
10 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
11 # POSSIBILITY OF SUCH DAMAGE.
12 #
13 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
14 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
16 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
17 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
18 #
19 # $Id: roundup-mailgw,v 1.12 2001-11-08 05:16:55 richard Exp $
21 import sys, os, re
22 if int(sys.version[0]) < 2:
23 print "Roundup requires Python 2.0 or newer."
24 sys.exit(1)
26 from mimetools import Message
28 def do_pipe(handler):
29 '''Read a message from standard input and pass it to the mail handler.
30 '''
31 handler.main(sys.stdin)
32 return 0
34 def do_mailbox(handler, filename):
35 '''Read a series of messages from the specified unix mailbox file and
36 pass each to the mail handler.
37 '''
38 # open the spool file and lock it
39 import fcntl, FCNTL
40 f = open(filename, 'r+')
41 fcntl.flock(f.fileno(), FCNTL.LOCK_EX)
43 # handle and clear the mailbox
44 try:
45 from mailbox import UnixMailbox
46 mailbox = UnixMailbox(f, factory=Message)
47 # grab one message
48 message = mailbox.next()
49 while message:
50 # call the instance mail handler
51 handler.handle_Message(message)
52 message = mailbox.next()
53 # nuke the file contents
54 os.ftruncate(f.fileno(), 0)
55 except:
56 import traceback
57 traceback.print_exc()
58 return 1
59 fcntl.flock(f.fileno(), FCNTL.LOCK_UN)
60 return 0
62 def do_pop(handler, server, user='', password=''):
63 '''Read a series of messages from the specified POP server.
64 '''
65 import getpass, poplib
66 if not user:
67 user = raw_input('User: ')
68 if not password:
69 password = getpass.getpass()
71 # open a connection to the server and retrieve all messages
72 server = poplib.POP3(server)
73 server.user(user)
74 server.pass_(password)
75 numMessages = len(server.list()[1])
76 for i in range(numMessages):
77 for j in server.retr(i+1)[1]:
78 s = cStringIO.StringIO('\n'.join(j))
79 s.seek(0)
80 handler.handle_Message(Message(s))
81 return 0
83 def usage(args, message=None):
84 if message is not None:
85 print message
86 print 'Usage: %s <instance home> [source spec]'%args[0]
87 print '''
88 The roundup mail gateway may be called in one of two ways:
89 . with an instance home as the only argument,
90 . with both an instance home and a mail spool file, or
91 . with both an instance home and a pop server account.
93 PIPE:
94 In the first case, the mail gateway reads a single message from the
95 standard input and submits the message to the roundup.mailgw module.
97 UNIX mailbox:
98 In the second case, the gateway reads all messages from the mail spool
99 file and submits each in turn to the roundup.mailgw module. The file is
100 emptied once all messages have been successfully handled. The file is
101 specified as:
102 mailbox /path/to/mailbox
104 POP:
105 In the third case, the gateway reads all messages from the POP server
106 specified and submits each in turn to the roundup.mailgw module. The
107 server is specified as:
108 pop username:password@server
109 The username and password may be omitted:
110 pop username@server
111 pop server
112 are both valid. The username and/or password will be prompted for if
113 not supplied on the command-line.
114 '''
115 return 1
117 def main(args):
118 '''Handle the arguments to the program and initialise environment.
119 '''
120 # figure the instance home
121 if len(args) > 1:
122 instance_home = args[1]
123 else:
124 instance_home = os.environ.get('ROUNDUP_INSTANCE', '')
125 if not instance_home:
126 return usage(args)
128 # get the instance
129 import roundup.instance
130 instance = roundup.instance.open(instance_home)
132 # get a mail handler
133 db = instance.open('admin')
134 handler = instance.MailGW(db)
136 # if there's no more arguments, read a single message from stdin
137 if len(args) == 2:
138 return do_pipe(handler)
140 # otherwise, figure what sort of mail source to handle
141 if len(args) < 4:
142 return usage(args, 'Error: not enough source specification information')
143 source, specification = args[2:]
144 if source == 'mailbox':
145 return do_mailbox(handler, specification)
146 elif source == 'pop':
147 m = re.match(r'((?P<user>[^:]+)(:(?P<pass>.+))?@)?(?P<server>.+)',
148 specification)
149 if m:
150 return do_pop(handler, m.group('server'), m.group('user'),
151 m.group('pass'))
152 return usage(args, 'Error: pop specification not valid')
154 return usage(args, 'Error: The source must be either "mailbox" or "pop"')
156 # call main
157 if __name__ == '__main__':
158 sys.exit(main(sys.argv))
160 #
161 # $Log: not supported by cvs2svn $
162 # Revision 1.11 2001/11/07 05:32:58 richard
163 # More roundup-mailgw usage help.
164 #
165 # Revision 1.10 2001/11/07 05:30:11 richard
166 # Nicer usage message.
167 #
168 # Revision 1.9 2001/11/07 05:29:26 richard
169 # Modified roundup-mailgw so it can read e-mails from a local mail spool
170 # file. Truncates the spool file after parsing.
171 # Fixed a couple of small bugs introduced in roundup.mailgw when I started
172 # the popgw.
173 #
174 # Revision 1.8 2001/11/01 22:04:37 richard
175 # Started work on supporting a pop3-fetching server
176 # Fixed bugs:
177 # . bug #477104 ] HTML tag error in roundup-server
178 # . bug #477107 ] HTTP header problem
179 #
180 # Revision 1.7 2001/08/07 00:24:42 richard
181 # stupid typo
182 #
183 # Revision 1.6 2001/08/07 00:15:51 richard
184 # Added the copyright/license notice to (nearly) all files at request of
185 # Bizar Software.
186 #
187 # Revision 1.5 2001/08/05 07:44:25 richard
188 # Instances are now opened by a special function that generates a unique
189 # module name for the instances on import time.
190 #
191 # Revision 1.4 2001/08/03 01:28:33 richard
192 # Used the much nicer load_package, pointed out by Steve Majewski.
193 #
194 # Revision 1.3 2001/08/03 00:59:34 richard
195 # Instance import now imports the instance using imp.load_module so that
196 # we can have instance homes of "roundup" or other existing python package
197 # names.
198 #
199 # Revision 1.2 2001/07/29 07:01:39 richard
200 # Added vim command to all source so that we don't get no steenkin' tabs :)
201 #
202 # Revision 1.1 2001/07/23 03:46:48 richard
203 # moving the bin files to facilitate out-of-the-boxness
204 #
205 # Revision 1.1 2001/07/22 11:15:45 richard
206 # More Grande Splite stuff
207 #
208 #
209 # vim: set filetype=python ts=4 sw=4 et si