Skip to content

Commit

Permalink
Merge pull request #1 from Brawn1/expunge_feature
Browse files Browse the repository at this point in the history
Added Expunge feature with option, to use direct with server.
  • Loading branch information
Brawn1 authored Aug 13, 2024
2 parents b232a13 + e65a1b9 commit 5cf1388
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ If you have a hierarchy of folders, you can search recursively within the childr

You can use the `-R` option to reverse the order in which the folders are processed. The main use of this is with recursive searches, if you want duplicates of messages found in child folders to be removed from the parents, rather than the other way around.

With the `-d` option, it's removes all marked messages from the server (Expunge). This is a dangerous option, so use with caution!

## Specifying the password

If you don't wish to specify a password via a command-line argument, where it could be seen by other users of the system, and you don't want to type it in each time, you have three options:
Expand Down
16 changes: 15 additions & 1 deletion imapdedup.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,19 @@ def get_arguments(args: Optional[List[str]] = None) -> Tuple[argparse.Namespace,
"-y", "--copy", dest="copy_mailbox",
help="Copy messages to specified mailbox before deleting them from current location."
)
parser.add_argument(
"-d",
"--delete",
dest="delete_marked_messages",
action="store_true",
help="Delete marked messages (expunge)"
)
parser.add_argument('mailbox', nargs='*')

options = parser.parse_args(args)
mboxes = options.mailbox

if ((not options.server) or (not options.user)) and not options.process:
if (not options.server or not options.user) and not options.process:
sys.stderr.write(
"\nError: Must specify server, user, and at least one mailbox.\n\n"
)
Expand Down Expand Up @@ -359,6 +366,11 @@ def process_messages(server: imaplib.IMAP4, msgs_to_delete: List[int], tag_name:
)


def delete_marked_messages(server: imaplib.IMAP4):
print("Expunging deleted messages...")
check_response(server.expunge())


def get_msg_headers(server: imaplib.IMAP4, msg_ids: List[int]) -> List[Tuple[int, bytes]]:
"""
Get the dict of headers for each message in the list of provided IDs.
Expand Down Expand Up @@ -599,6 +611,8 @@ def process(options, mboxes: List[str]):
"There are now %s messages tagged as '%s' in %s."
% (numtagged, options.tag_name, mbox)
)
if options.delete_marked_messages:
delete_marked_messages(server)

if not options.no_close:
server.close()
Expand Down

0 comments on commit 5cf1388

Please sign in to comment.