1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
""" |
5
|
|
|
Module to download and to detach/strip/remove attachments |
6
|
|
|
from e-mails on IMAP servers. |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from __future__ import print_function |
10
|
|
|
|
11
|
|
|
import argparse |
12
|
|
|
import logging |
13
|
|
|
|
14
|
|
|
from src.mailbox_imap import MailboxCleanerIMAP |
15
|
|
|
from src.mailbox_message import MailboxCleanerMessage |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
__author__ = "Alexander Willner" |
19
|
|
|
__copyright__ = "Copyright 2020, Alexander Willner" |
20
|
|
|
__credits__ = ["github.com/guido4000", |
21
|
|
|
"github.com/halteproblem", "github.com/jamesridgway"] |
22
|
|
|
__license__ = "MIT" |
23
|
|
|
__version__ = "1.0.0" |
24
|
|
|
__maintainer__ = "Alexander Willner" |
25
|
|
|
__email__ = "[email protected]" |
26
|
|
|
__status__ = "Development" |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def handle_arguments() -> argparse.ArgumentParser: |
30
|
|
|
"""Provide CLI handler for application.""" |
31
|
|
|
|
32
|
|
|
parser = argparse.ArgumentParser() |
33
|
|
|
parser.add_argument("-a", "--all", |
34
|
|
|
help="iterate over all folders", action='store_true') |
35
|
|
|
parser.add_argument("-d", "--detach", |
36
|
|
|
help="remove attachments", action='store_true') |
37
|
|
|
parser.add_argument("-k", "--skip-download", |
38
|
|
|
help="download attachments", action='store_true') |
39
|
|
|
parser.add_argument("-r", "--reset-cache", |
40
|
|
|
help="reset cache", action='store_true') |
41
|
|
|
parser.add_argument("-m", "--max-size", |
42
|
|
|
help="max attachment size in KB", default=200) |
43
|
|
|
parser.add_argument("-f", "--folder", |
44
|
|
|
help="imap folder to process", default="Inbox") |
45
|
|
|
parser.add_argument("-l", "--upload", |
46
|
|
|
help="local folder with messages to upload") |
47
|
|
|
|
48
|
|
|
parser.add_argument("-t", "--target", |
49
|
|
|
help="download attachments to this local folder", |
50
|
|
|
default="attachments") |
51
|
|
|
parser.add_argument("-s", "--server", help="imap server", required=True) |
52
|
|
|
parser.add_argument("-u", "--user", help="imap user", required=True) |
53
|
|
|
parser.add_argument("-o", "--port", help="imap port", required=False) |
54
|
|
|
parser.add_argument("-p", "--password", help="imap user", required=True) |
55
|
|
|
parser.add_argument( |
56
|
|
|
"-v", |
57
|
|
|
"--verbose", |
58
|
|
|
action="count", |
59
|
|
|
default=0, |
60
|
|
|
dest="verbosity", |
61
|
|
|
help="be more verbose (-v, -vv)") |
62
|
|
|
parser.add_argument( |
63
|
|
|
"--version", |
64
|
|
|
action="version", |
65
|
|
|
version="%(prog)s (version {version})".format(version=__version__)) |
66
|
|
|
|
67
|
|
|
args = parser.parse_args() |
68
|
|
|
|
69
|
|
|
logging.basicConfig(level=logging.WARNING - args.verbosity * 10, |
70
|
|
|
format="%(message)s") |
71
|
|
|
|
72
|
|
|
return args |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def main(): |
76
|
|
|
"""Setup and run remover.""" |
77
|
|
|
|
78
|
|
|
args = handle_arguments() |
79
|
|
|
message = MailboxCleanerMessage(args) |
80
|
|
|
imap = MailboxCleanerIMAP(args) |
81
|
|
|
|
82
|
|
|
# todo: repeat here a couple of times pylint: disable=W0511 |
83
|
|
|
try: |
84
|
|
|
imap.login() |
85
|
|
|
if args.upload: |
86
|
|
|
message.process_directory(imap.upload) |
87
|
|
|
else: |
88
|
|
|
imap.process_folders() |
89
|
|
|
except KeyboardInterrupt as error: |
90
|
|
|
raise SystemExit('\nCancelling...') from error |
91
|
|
|
finally: |
92
|
|
|
imap.cleanup() |
93
|
|
|
imap.logout() |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
if __name__ == '__main__': |
97
|
|
|
main() |
98
|
|
|
|