List Info

Thread: dbmail2mailbox tool




dbmail2mailbox tool
user name
2006-01-18 18:48:29
Hi,

I wrote very simple dbmail2mailbox tool. It can be used to
learn some 
spam filters directly from IMAP folder with spam messages,
for example, 
or for another purposes.

Usage:

./dbmail2mailbox --server=localhost --database=dbmail
--login=dbmail 
--password=dbmailpwd --address=testusermydomain.com --mailbox=testbox

Can anybody need it or can test it?

-- 
Thanks,
Eugene Prokopiev
#!/usr/bin/python

# Copyright (C) 2006 Eugene Prokopiev <enp at altlinux
dot org>
#
# This program is free software; you can redistribute it
and/or 
# modify it under the terms of the GNU General Public
License 
# as published by the Free Software Foundation; either 
# version 2 of the License, or (at your option) any later 
# version.
#
# This program is distributed in the hope that it will be
useful,
# but WITHOUT ANY WARRANTY; without even the implied
warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
License
# along with this program; if not, write to the Free
Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import sys
import getopt
import psycopg as db
import email.Parser

def export(connection, address, mailbox):
	
	parser = email.Parser.Parser()
	
	cursor_messages = connection.cursor()
	cursor_messageblks = connection.cursor()
	
	sql_messages = """select message_idnr,
internal_date
		from dbmail_aliases
		inner join dbmail_users on dbmail_aliases.deliver_to =
dbmail_users.user_idnr
		inner join dbmail_mailboxes on dbmail_users.user_idnr =
dbmail_mailboxes.owner_idnr
		inner join dbmail_messages on
dbmail_mailboxes.mailbox_idnr = dbmail_messages.mailbox_idnr
		inner join dbmail_physmessage on
dbmail_messages.physmessage_id = dbmail_physmessage.id
		where dbmail_messages.deleted_flag=0 and
dbmail_mailboxes.name = %s and alias = %s
		"""
	
	sql_messageblks = """
		select messageblk, is_header from dbmail_messageblks 
		inner join dbmail_messages on
dbmail_messageblks.physmessage_id =
dbmail_messages.physmessage_id 
		where message_idnr = %s order by
dbmail_messageblks.messageblk_idnr
		"""
	
	cursor_messages.execute(sql_messages, (mailbox, address,))
	for message_idnr, internal_date in
cursor_messages.fetchall():		
		cursor_messageblks.execute(sql_messageblks,
("%s" % (message_idnr),))
		for messageblk, is_header in
cursor_messageblks.fetchall():
			if (is_header == 1):
				print "From %s %s" %
(parser.parsestr(messageblk).get("From"),
internal_date.strftime()) 
			print "%s" % (messageblk)
		
def usage():
	
	usage_text = """
		
dbmail2mailbox - DBMail to mbox conversion (output to
stdout)
		
arguments:
		
	-h|--help	- show this text
	-s|--server	- server where DBMail database installed
	-d|--database	- DBMail database name
	-l|--login	- login to database
	-p|--password	- password to database
	-a|--address	- any mail user address (alias)
	-m|--mailbox	- mailbox name
	
"""
	
	print usage_text

def main(argv):
	
	server 		= "localhost"
	database	= "dbmail"
	login		= "dbmail"
	password 	= "dbmailpwd"
	address		= "testusermydomain.com"
	mailbox 	= "testbox"
	
	try:
		opts, args = getopt.getopt(argv,
"hs:d:l:p:a:m:", ["help",
"server=", "database=",
"login=", "password=",
"address=", "mailbox="])
	except getopt.GetoptError:
		usage()
		sys.exit(2)
		
	for opt, arg in opts:
		if opt in ("-h", "--help"):
			usage()
			sys.exit()
		elif opt in ("-s", "--server"):
			server = arg
		elif opt in ("-d", "--database"):
			database = arg
		elif opt in ("-l", "--login"):
			login = arg
		elif opt in ("-p", "--password"):
			password = arg
		elif opt in ("-a", "--address"):
			address = arg
		elif opt in ("-m", "--mailbox"):
			mailbox = arg

	connection = db.connect("host="+server+"
dbname="+database+" user="+login+"
password="+password)

	export(connection, address, mailbox)

if __name__ == "__main__":
    main(sys.argv[1:])
_______________________________________________
Dbmail mailing list
Dbmaildbmail.org
htt
ps://mailman.fastxs.nl/mailman/listinfo/dbmail
dbmail2mailbox tool
user name
2006-01-18 20:41:00
Eugene, I had to add a format to the strftime() to make it
work.
Other than that and the changes for mysql, it seems to work
more
or less as expected on the few mailboxes I've tested.
What would be ultra cool is if you (or somebody) would write
the
code to detach attachments as well (and not include their
encoded
data in the output), since I'm not a python coder
at all 
In fact, I was just the other day thinking about a utility
of this
form, I may try my hand at mod'ing it to put each message in
it's
own file or such.  Or maybe not.
/me reaches for the shiny, barely opened python book
thanks,
dave

-- 

Dave Logan
http://www.digitalcoven.
com/

"No!  Try not!  Do.  Or do not.  There is no try."
 -- Yoda
_______________________________________________
Dbmail mailing list
Dbmaildbmail.org
htt
ps://mailman.fastxs.nl/mailman/listinfo/dbmail
dbmail2mailbox tool
user name
2006-01-19 05:30:55
Dave Logan:
> Eugene, I had to add a format to the strftime() to make
it work.

What concrete changes did you do?

> Other than that and the changes for mysql, it seems to
work more
> or less as expected on the few mailboxes I've tested.

I can't test my utility with mysql because I haven't it. Can
you add 
command line switch to database type? Or just send you
changes to me?

> What would be ultra cool is if you (or somebody) would
write the
> code to detach attachments as well (and not include
their encoded
> data in the output), since I'm not a python coder
> at all 

I'm not a python coder too  And I'm
glad to see any corrections in my 
code.

You can extract attachments with email.Parser. See 
http://wingware.com/psupport/python-manual/2.4/li
b/node576.html and 
http://wingware.com/psupport/python-
manual/2.4/lib/module-email.Message.html

> In fact, I was just the other day thinking about a
utility of this
> form, I may try my hand at mod'ing it to put each
message in it's
> own file or such.  Or maybe not.
> /me reaches for the shiny, barely opened python book
> thanks,
> dave
> 


-- 
Thanks,
Eugene Prokopiev
_______________________________________________
Dbmail mailing list
Dbmaildbmail.org
htt
ps://mailman.fastxs.nl/mailman/listinfo/dbmail
dbmail2mailbox tool
user name
2006-01-19 21:17:07
Eugene,
I changed:

< print "From %s %s" %
(parser.parsestr(messageblk).get("From"), 
internal_date.strftime())

 > print "From %s %s" %
(parser.parsestr(messageblk).get("From"), 
internal_date.strftime("%A %B %d %H:%M:%S %Y %Z"))

> I can't test my utility with mysql because I haven't
it. Can you add 
> command line switch to database type? Or just send you
changes to me?

I'll give that a quick try, the only real change is the
include and connect parameters.

-- 

Dave Logan
http://www.digitalcoven.
com/

"No!  Try not!  Do.  Or do not.  There is no try."
 -- Yoda
_______________________________________________
Dbmail mailing list
Dbmaildbmail.org
htt
ps://mailman.fastxs.nl/mailman/listinfo/dbmail
dbmail2mailbox tool
user name
2006-01-20 08:23:50
Check the python libs in the 2.1 tree. This has been well
solved already
for all supported drivers. There a cursor is setup for
whatever driver
is set in dbmail.conf.



Dave Logan wrote:
> Eugene,
> I changed:
> 
> < print "From %s %s" %
(parser.parsestr(messageblk).get("From"),
> internal_date.strftime())
> 
>> print "From %s %s" %
(parser.parsestr(messageblk).get("From"),
> internal_date.strftime("%A %B %d %H:%M:%S %Y
%Z"))
> 
>> I can't test my utility with mysql because I
haven't it. Can you add
>> command line switch to database type? Or just send
you changes to me?
> 
> 
> I'll give that a quick try, the only real change is the
> include and connect parameters.
> 

-- 
 
____________________________________________________________
____
  Paul Stevens                                      paul at
nfg.nl
  NET FACILITIES GROUP                     GPG/PGP:
1024D/11F8CD31
  The Netherlands________________________________http://www.nfg.nl
_______________________________________________
Dbmail mailing list
Dbmaildbmail.org
htt
ps://mailman.fastxs.nl/mailman/listinfo/dbmail
[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )