-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroyer.py
executable file
·180 lines (130 loc) · 5.5 KB
/
destroyer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/python
"""
Empties email folder for a provided list of email accounts
Author: Edward Tirado Jr
"""
from __future__ import print_function
from socket import error
import csv
import argparse
import sys
import datetime
import threading
import logging
import modules.imap as imap_mod
import modules.pop as pop_mod
MONTHS = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]
logging.basicConfig(filename='logs/deleter.log',format='%(asctime)s:%(levelname)s:%(message)s',datefmt='%Y-%m-%d %H:%M',level=logging.DEBUG)
class EmailAccount(object):
"""An email account"""
def __init__(self, host="", email="", password="", folder="", port=993, connection='imap'):
self.host = host
self.port = port
self.email = email
self.password = password
self.folder = folder
self.connection = connection
def process_args():
"""Process provided command line arguments"""
parser = argparse.ArgumentParser(description="Empty email folders using csv account list")
parser.add_argument('--file', '-f', help='File location')
parser.add_argument('--list', '-l', help="List available folders and exit. Takes in file location.")
parser.add_argument('--count', '-c', help="Get count of emails in provided accounts. Takes in a file location.")
parser.add_argument('--before', '-b', help="Limit deleted messages to those before the provided date")
args = parser.parse_args()
return args
def list_folders(accounts):
"""Show available folders for provided accounts"""
for email_account in accounts:
account_info = email_account.split(',')
email_account = EmailAccount(account_info[0], # host
account_info[1].strip(), # email
account_info[2].strip(), # password
account_info[3].strip(), # folder
account_info[4], # port
account_info[5], #connection
)
if 'imap' in email_account.connection.lower():
imap_conn = imap_mod.connect_imap(email_account)
logging.info('\nFolders for %s:\n' % email_account.email)
logging.info(imap_conn.list())
else:
logging.info('\nFolders for %s:' % email_account.email)
logging.warning('Cannot list folders for accounts using POP3')
sys.exit()
def get_accounts(file_name):
"""creates list of accounts from csv"""
account_info = []
try:
with open(file_name, 'rU') as csvfile:
accounts = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in accounts:
account_info.append(', '.join(row))
except IOError:
logging.debug("Could not read account list")
sys.exit()
return account_info
def get_date_for_processing(current_date):
"""Takes in a date and returns a date that is one month earlier."""
split_date = current_date.split('-')
day,month,year= split_date[0],split_date[1].upper(),split_date[2]
if MONTHS.index(month) == 0:
month = 'Dec'
year = int(year) - 1
else:
month = MONTHS[MONTHS.index(month) -1 ]
date = '-'.join([str(day),str(month),str(year)])
return date
def empty_folder(email_account, imap_search, before_date):
"""Empties trash folder for provided account"""
if before_date == None:
date = datetime.datetime.now().strftime("%d-%b-%Y")
delete_date = get_date_for_processing(date)
else:
delete_date = before_date
logging.info('Deleting emails sent before %s' % delete_date)
if 'imap' in email_account.connection.lower():
logging.info('Deleting using Imap module')
email_count = imap_mod.get_inbox_count(email_account)
while (email_count > 0):
imap_mod.delete_imap(email_account, delete_date, imap_search)
email_count = imap_mod.get_inbox_count(email_account)
else:
logging.info('Deleting using POP module')
pop_mod.delete_pop(email_account)
def main():
"""Main. Where the magic happens."""
args = process_args()
arg_list = [args.file, args.count, args.list]
accounts_csv = 'accounts.csv'
for arg in arg_list:
if arg != None:
accounts_csv = arg
all_accounts = get_accounts(accounts_csv)
if args.list != None:
list_folders(all_accounts)
if args.before != None:
imap_search = 'SENTBEFORE "%s"' % str(args.before)
else:
imap_search = 'ALL'
logging.info("Search criteria: %s" % imap_search)
for account in all_accounts:
account_info = account.split(',')
email_account = EmailAccount(account_info[0], # host
account_info[1].strip(), # email
account_info[2].strip(), # password
account_info[3].strip(), # folder
account_info[4], # port
account_info[5], #connection
)
if args.count != None:
email_count = imap_mod.get_inbox_count(email_account)
else:
new_thread = threading.Thread(target=empty_folder, args=(email_account, imap_search, args.before))
new_thread.start()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt as e:
cleanup_stop_thread()
print('\nBye!')