Added parameter to save receive message into a directory
Benjamin Renard

Benjamin Renard commited on 2012-04-19 10:15:37
Showing 2 changed files, with 32 additions and 1 deletions.

... ...
@@ -9,6 +9,7 @@ import MantisSMTPServer
9 9
 import logging
10 10
 from optparse import OptionParser
11 11
 import simplejson as json
12
+import os
12 13
 
13 14
 parser = OptionParser()
14 15
 
... ...
@@ -63,6 +64,15 @@ parser.add_option('-p',
63 64
                   help="The bind port",
64 65
                   default=1025)
65 66
 
67
+parser.add_option('-s',
68
+                  '--save',
69
+                  action="store",
70
+                  type="string",
71
+                  dest="save_dir",
72
+                  help="Save all message receive in this directory",
73
+                  default=None)
74
+
75
+
66 76
 (options, args) = parser.parse_args()
67 77
 
68 78
 
... ...
@@ -100,7 +110,7 @@ if options.configfile:
100 110
 		sys.exit(2)
101 111
 
102 112
 	try:
103
-		for param in ['mail_from','ws_url','ws_user','ws_pwd','ws_default_category']:
113
+		for param in ['mail_from','ws_url','ws_user','ws_pwd','ws_default_category','save_dir']:
104 114
 			if param in config:
105 115
 				server.set_option(param,config[param])
106 116
 
... ...
@@ -119,6 +129,13 @@ if options.configfile:
119 129
 else:
120 130
 	logging.warning('No config file supply. Running with default configuration...')
121 131
 
132
+if options.save_dir:
133
+	if os.path.isdir(options.save_dir):
134
+		server.set_option('save_dir',options.save_dir)
135
+	else:
136
+		logging.error("Incorrect directory path : %s" % options.save_dir)
137
+		sys.exit(4)
138
+
122 139
 try:
123 140
 	if options.daemon:
124 141
 		server.startDaemon(options.pidfile)
... ...
@@ -9,6 +9,7 @@ import MantisWS
9 9
 import asyncore
10 10
 import os
11 11
 import signal
12
+import time
12 13
 
13 14
 class MantisSMTPServer(smtpd.SMTPServer):
14 15
 
... ...
@@ -22,6 +23,7 @@ class MantisSMTPServer(smtpd.SMTPServer):
22 23
 	_options['ws_user']='mantis'
23 24
 	_options['ws_pwd']='password'
24 25
 	_options['ws_default_category']='other'
26
+	_options['save_dir']=None
25 27
 
26 28
 	ws=None
27 29
 
... ...
@@ -62,6 +64,7 @@ class MantisSMTPServer(smtpd.SMTPServer):
62 64
 	def process_message(self, peer, mailfrom, rcpttos, data):
63 65
 
64 66
 		logging.info('Message receive from %s, send by %s to %s' % (peer,mailfrom,rcpttos))
67
+		self.save_msg(peer, mailfrom, rcpttos, data)
65 68
 		
66 69
 		try:
67 70
 			msg = MantisMail.MantisMail(mailfrom,rcpttos,data)
... ...
@@ -163,3 +166,14 @@ class MantisSMTPServer(smtpd.SMTPServer):
163 166
 			return issue_id
164 167
 		else:
165 168
 			logging.error('Error during add issue')
169
+
170
+	def save_msg(self,peer, mailfrom, rcpttos, data):
171
+		if self._options['save_dir']:
172
+			f="%s/%s" % (self._options['save_dir'],"%s-%s--%s--%s.msg" % (time.time(), peer[0],mailfrom,rcpttos[0]))
173
+			try:
174
+				logging.info('Save mail data into %s' % f)
175
+				fd=open(f,'w')
176
+				fd.write(data)
177
+				fd.close()
178
+			except Exception, e:
179
+				logging.error("Can't save message from %s to % receive from %s into %s : %s" % (mailfrom,rcpttos,peer,f,e))
166 180