#!/usr/bin/python # Tag or untag messages in a mailbox # Quick and dirty, mainly copy and pasted from some python docs # # Copyright 2010 Ray Strode # # No warranty, no restrictions on use. import getpass import imaplib import os import re import sys args = sys.argv[:] def usage(): sys.stderr.write(sys.argv[0] + ' [-u username ] [-s server] [list] [[tag "mailbox" tag-name|untag "mailbox" tag-name] [-c \'criteria\']]\n') sys.stderr.write(" (Hint: use the '\Seen' tag to mark a mailbox as read)\n") sys.stderr.write(" (Hint: an example criteria might be '(FROM \"buildsys@redhat.com\" SUBJECT \"Package Tagged\")')\n") sys.exit(1) username = None if "-u" in args: index = args.index("-u") username = args[index + 1] args.remove("-u") elif "--username" in args: index = args.index("--username") username = args[index + 1] args.remove("--username") if username is None: username = os.environ["LOGNAME"] server = None if "-s" in args: index = args.index("-s") if index + 1 >= len(args): usage() server = args[index + 1] args.remove("-s") elif "--server" in args: index = args.index("--server") if index + 1 >= len(args): usage() server = args[index + 1] args.remove("--server") if server is None: server = "mail.corp.redhat.com" if "list" in args: do_list = True args.remove("list") else: do_list = False if "tag" in args: do_tag = True index = args.index("tag") if index + 2 >= len(args): usage() box = args[index + 1] tag = args[index + 2] args.remove("tag") else: do_tag = False if "untag" in args: do_untag = True index = args.index("untag") if index + 2 >= len(args): usage() box = args[index + 1] tag = args[index + 2] args.remove("untag") else: do_untag = False criteria = None if "-c" in args: index = args.index("-c") if index + 1 >= len(args): usage() criteria = args[index + 1] args.remove("-c") elif "--criteria" in args: index = args.index("--criteria") if index + 1 >= len(args): usage() criteria = args[index + 1] args.remove("--criteria") if criteria is None: criteria = "ALL" elif not do_tag and not do_untag: usage() if not do_list and not do_tag and not do_untag: usage() if do_tag and do_untag: usage() print "Logging into %s as %s" % (server, username) password = getpass.getpass() c = imaplib.IMAP4_SSL(server) c.login(username, password) if do_list: list_response_pattern = re.compile(r'\((?P.*?)\) "(?P.*)" (?P.*)') def parse_list_response(line): flags, delimiter, mailbox_name = list_response_pattern.match(line).groups() mailbox_name = mailbox_name.strip('"') return (flags, delimiter, mailbox_name) print typ, data = c.list() for line in data: flags, delimiter, box = parse_list_response(line) print box if do_tag or do_untag: try: c.select(box) typ, [ids] = c.search(None, criteria) except c.error: c.select("INBOX/" + sys.argv[2]) typ, [ids] = c.search(None, criteria) print "%s has %d matched messages" % (sys.argv[2], len(ids)) if len(ids) == 0: sys.exit(0) ids = ','.join(ids.split(' ')) if do_tag: c.store(ids, "+FLAGS", tag) elif do_untag: c.store(ids, "-FLAGS", tag) c.logout()