You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.7 KiB

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
import datetime
from datetime import timedelta
import sys
import kopano
from MAPI.Util import *
if sys.hexversion >= 0x03000000:
def _encode(s):
return s
else: # pragma: no cover
def _encode(s):
return s.encode(sys.stdout.encoding or 'utf8')
def opt_args():
parser = kopano.parser('skpcfmUP')
parser.add_option("--user", dest="user", action="store", help="Run script for user")
parser.add_option("--wastebasket", dest="wastebasket", action="store_true",
help="Run cleanup script for the wastebasket folder")
parser.add_option("--archive", dest="archive", action="store", help="instead of removing items archive them into this folder")
parser.add_option("--junk", dest="junk", action="store_true", help="Run cleanup script for the junk folder")
parser.add_option("--force", dest="force", action="store_true", help="Force items without date to be removed")
parser.add_option("--days", dest="days", action="store", help="Delete older then x days")
parser.add_option("--verbose", dest="verbose", action="store_true", help="Verbose mode")
parser.add_option("--dry-run", dest="dryrun", action="store_true", help="Run script in dry mode")
parser.add_option("--progressbar", dest="progressbar", action="store_true", help="Show progressbar ")
return parser.parse_args()
def progressbar(folder, daysbeforedeleted):
from progressbar import Bar, AdaptiveETA, Percentage, ProgressBar
except ImportError:
print('''Please download the progressbar library from or
run without the --progressbar parameter''')
widgets = [Percentage(),
' ', Bar(),
' ', AdaptiveETA()]
progressmax = 0
for item in folder.items():
if item.received <= daysbeforedeleted:
progressmax += 1
pbar = ProgressBar(widgets=widgets, maxval=progressmax)
return pbar
def deleteitems(options, user, folder):
itemcount = 0
daysbeforedeleted = - timedelta(days=int(options.days))
pbar = None
if options.progressbar:
pbar = progressbar(folder, daysbeforedeleted)
if options.archive:
archive_folder =, create=True)
for item in folder.items():
date = None
if not item.prop(PR_LAST_MODIFICATION_TIME).value and options.force:
date = daysbeforedeleted
elif item.prop(PR_LAST_MODIFICATION_TIME).value:
date = item.prop(PR_LAST_MODIFICATION_TIME).value
if date:
if date <= daysbeforedeleted:
if options.verbose:
if options.archive:
print('Archiving \'{}\''.format(_encode(item.subject)))
print('Deleting \'{}\''.format(_encode(item.subject)))
if not options.dryrun:
if options.archive:
folder.move(item, archive_folder)
if pbar:
pbar.update(itemcount + 1)
itemcount += 1
if options.progressbar:
if options.archive:
print('Archived {} item(s) for user \'{}\' in folder \'{}\' to folder \'{}\''.format(itemcount, _encode(,
print('Deleted {} item(s) for user \'{}\' in folder \'{}\''.format(itemcount, _encode(, _encode(
return itemcount
def main():
options, args = opt_args()
if not options.user or not options.days:
print('Please use:\n {} --user <username> --days <days> ',format(sys.argv[0]))
user = kopano.server(options).user(options.user)
print('Running script for \'{}\''.format(_encode(
if options.wastebasket:
folder =
deleteitems(options, user, folder)
if options.junk:
folder =
deleteitems(options, user, folder)
Loop over all the folders that are passed with the -f parameter
if options.folders:
for folder in
deleteitems(options, user, folder)
if __name__ == "__main__":