Add option to search for empty folders only
This commit is contained in:
parent
db2537d3a5
commit
653c0334bc
1 changed files with 40 additions and 15 deletions
|
@ -17,19 +17,20 @@ import json
|
||||||
from operator import itemgetter, attrgetter
|
from operator import itemgetter, attrgetter
|
||||||
|
|
||||||
class Mailbox:
|
class Mailbox:
|
||||||
def __init__(self, name, count):
|
def __init__(self, name, count, children):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.count = count
|
self.count = count
|
||||||
|
self.children = children
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.name} {self.count}"
|
return f"{self.name} {self.count} messages and {len(self.children)} children mailboxes"
|
||||||
|
|
||||||
def toJSON(self):
|
def toJSON(self):
|
||||||
return json.dumps(self, default=lambda o: o.__dict__,
|
return json.dumps(self, default=lambda o: o.__dict__,
|
||||||
sort_keys=True, indent=4)
|
sort_keys=True, indent=4)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return repr((self.name, self.count))
|
return repr((self.name, self.count, self.children))
|
||||||
|
|
||||||
class list_imap_messages:
|
class list_imap_messages:
|
||||||
|
|
||||||
|
@ -51,29 +52,52 @@ class list_imap_messages:
|
||||||
self._list_mailboxes()
|
self._list_mailboxes()
|
||||||
|
|
||||||
def _list_mailboxes(self):
|
def _list_mailboxes(self):
|
||||||
self._log.debug(f"Listing mailboxes in {self.config['mailbox']}")
|
self._log.debug(f"Listing mailboxes in '{self.config['mailbox']}'")
|
||||||
result = self.imap.list()
|
result_list = self.imap.list()
|
||||||
if result[0] == 'NO':
|
if result_list[0] == 'NO':
|
||||||
self._log.error(f"Error listing mailboxes in {self.config['mailbox']}: {result[1]}")
|
self._log.error(f"Error listing mailboxes in {self.config['mailbox']}: {result_list[1]}")
|
||||||
self.imap.close()
|
self.imap.close()
|
||||||
self.imap.logout()
|
self.imap.logout()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
self._log.debug(f"Result: {result}")
|
self._log.debug(f"Result: {result_list}")
|
||||||
mailboxes = list()
|
mailboxes = list()
|
||||||
for raw_mailbox in result[1]:
|
for raw_mailbox in result_list[1]:
|
||||||
self._log.debug(raw_mailbox)
|
self._log.debug(raw_mailbox)
|
||||||
mailbox_list = raw_mailbox.decode().split(' "/" ')
|
mailbox_list = raw_mailbox.decode().split(' "/" ')
|
||||||
result = self._select_mailbox(mailbox_list[1])
|
result = self._select_mailbox(mailbox_list[1])
|
||||||
|
self._log.debug(f"Selecting mailbox '{mailbox_list[1]}', result: {result}")
|
||||||
|
# result_list = self.imap.list(mailbox_list[1])[1]
|
||||||
|
# self._log.debug(f"Listing mailboxes in '{mailbox_list[1]}', result: {result_list}")
|
||||||
|
# if len(result_list) > 1:
|
||||||
|
# children_mailboxes = result_list[1]
|
||||||
|
# else:
|
||||||
|
# children_mailboxes = []
|
||||||
|
if b'hasnochildren' in raw_mailbox:
|
||||||
|
children_mailboxes = []
|
||||||
|
else:
|
||||||
|
children_mailboxes = self._get_children(mailbox_list[1], result_list[1])
|
||||||
|
count = int(result[1][0])
|
||||||
|
if (self.config['only_empty'] and count == 0 and len(children_mailboxes) == 0) or not self.config['only_empty']:
|
||||||
mailboxes.append(
|
mailboxes.append(
|
||||||
Mailbox(
|
Mailbox(
|
||||||
mailbox_list[1].replace('"', ''),
|
name=mailbox_list[1].replace('"', ''),
|
||||||
int(result[1][0].decode())
|
count=count,
|
||||||
|
children=children_mailboxes
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
print(json.dumps(sorted(mailboxes, key=lambda mailbox: mailbox.count, reverse=True), indent=2, default=vars))
|
self._log.info(json.dumps(sorted(mailboxes, key=lambda mailbox: mailbox.count, reverse=True), indent=2, default=vars))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def _get_children(self, mailbox, mlist):
|
||||||
|
children = [ mailbox ]
|
||||||
|
for item in mlist:
|
||||||
|
item_string = item.decode().split(' "/" ')[1].replace('"', '')
|
||||||
|
if mailbox in item_string:
|
||||||
|
if item_string != mailbox:
|
||||||
|
children.append(item_string)
|
||||||
|
return children
|
||||||
|
|
||||||
def _select_mailbox(self, mailbox):
|
def _select_mailbox(self, mailbox):
|
||||||
try:
|
try:
|
||||||
self._log.debug(f"Selecting mailbox '{self.config['username']}@{self.config['server']}:{self.config['port']}/{mailbox}'")
|
self._log.debug(f"Selecting mailbox '{self.config['username']}@{self.config['server']}:{self.config['port']}/{mailbox}'")
|
||||||
|
@ -163,6 +187,7 @@ class list_imap_messages:
|
||||||
@click.option("--username","-u", required=True, help="User name to login.")
|
@click.option("--username","-u", required=True, help="User name to login.")
|
||||||
@click.option("--password","-w", required=True, help="User password to login.")
|
@click.option("--password","-w", required=True, help="User password to login.")
|
||||||
@click.option("--mailbox","-m", default='INBOX', help="Mailbox to start examination.")
|
@click.option("--mailbox","-m", default='INBOX', help="Mailbox to start examination.")
|
||||||
|
@click.option("--only-empty", "-e", is_flag=True, default=False, help='List only empty folder')
|
||||||
@click_config_file.configuration_option()
|
@click_config_file.configuration_option()
|
||||||
def __main__(debug_level, log_file, **kwargs):
|
def __main__(debug_level, log_file, **kwargs):
|
||||||
return list_imap_messages(debug_level, log_file, **kwargs)
|
return list_imap_messages(debug_level, log_file, **kwargs)
|
||||||
|
|
Loading…
Reference in a new issue