#!/env/python
# Copyright 2008 Open Source Beef Computing. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY OPEN SOURCE BEEF COMPUTING ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# Author: ROD
# Date: October 30, 2008
# Version: 1.0
#
import mailbox
import os
import sys
__version__ = "1.0"
__doc__ = """
Open Source Beef Computing Creations (c) 2008
rodperson@comcast.net
This script will convert a MH format mailbox folder structure, such as claws-mail creates,
into the more widely used Maildir directory.
Parameters:
-u - name of the user whose mailbox is to be moved. If not specified it is set
to the user executing the script.
-r - the top level of the mail directory to be converted
-d - the top level of the destination mail directory
Example:
python mailconvert.py -r 'Mail' -d 'GMail2'
"""
def MoveMessages(mhroot, source_folder, destination_folder, delete_original = False):
""" Move a MH message to a new Maildir folder.
parameters:
mhroot - the path to the root of the MH directory.
source_folder - full path name of the source folder.
EX: /home/bob/MH/inbox
destination_folder - path name to place new mailbox in.
delete_original - If True this will delete the source message. default is False
Example:
MoveMessages('/home/box/MH/', 'inbox','/home/bob/Maildir')
This will move the messages from the source_folder to a new mailbox
named /home/bob/Maildir/inbox without destroying the original messages.
"""
full_name = os.path.join(mhroot,source_folder)
cm_sub = mailbox.MH(full_name,mailbox.MHMessage,False)
out_dir = os.path.join(destination_folder,source_folder)
destination = mailbox.Maildir(out_dir)
for message in cm_sub:
destination.add(mailbox.MaildirMessage(message))
def ScanSubFolders(root, folder):
sfp = os.path.join(root,folder)
sm = mailbox.MH(sfp,mailbox.MHMessage, False)
return sm.list_folders()
def main():
argc = len(sys.argv)
if argc == 0:
print 'You must supply a source and destination directory. Invoke with -h for help'
sys.exit(1)
user = os.getenv("HOME")
#mhroot = '~/Mail'
#destination_root = '%s/GMail2' % user
idx = 1
while (idx < argc):
if(sys.argv[idx] == "-u"):
user = sys.argv[(idx + 1)]
elif(sys.argv[idx] == "-r"):
mhroot = sys.argv[(idx + 1)]
elif(sys.argv[idx] == "-d"):
destination_root = sys.argv[(idx + 1)]
elif(sys.argv[idx] in ("-h","--help")):
print __doc__
sys.exit(0)
idx += 1
# Fix destination so if is in the form of username/destination_root
destination_root = os.path.join(user,destination_root)
# Make sure that the mhroot specifies the users mail directory
#if mhroot[1:2] != '~':
# mhroot = os.path.join('/home',user,mhroot)
if (os.path.isdir(destination_root) == False):
if (os.path.exists(destination_root) == False):
os.mkdir(destination_root,0770)
new_maildir = mailbox.Maildir(destination_root,None, False)
claws_mail = mailbox.MH(mhroot,mailbox.MHMessage,False)
folders = claws_mail.list_folders()
# Scan All the folders for subfolders until there are no
# subfolder left to scan. Append everything to the folders
# list
for folder in folders:
subfolders = ScanSubFolders(mhroot, folder)
if subfolders != []:
for sub_folder in subfolders:
name = os.path.join(folder,sub_folder)
folders.append(name)
for folder in folders:
print 'Moving messages in %s/%s to %s' % (mhroot,folder,destination_root)
MoveMessages(mhroot,folder,destination_root)
if __name__ == "__main__":
main()