summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-ximap-tool86
1 files changed, 52 insertions, 34 deletions
diff --git a/imap-tool b/imap-tool
index dab6ff8..f546379 100755
--- a/imap-tool
+++ b/imap-tool
@@ -27,6 +27,7 @@ class imap_tool():
archive_batch = 50
ssl = False
action = imap_action.MAILBOX_LIST
+ interactive = True
usage = "Usage: " + os.path.basename(sys.argv[0]) + " [options] [actions]\n" \
"\n" \
@@ -35,7 +36,8 @@ class imap_tool():
" -u [user]\n" \
" -p [password]\n" \
" -i [port]\n" \
- " -s\n" \
+ " -s (use ssl)\n" \
+ " -n (non-interactive mode)\n" \
"\n" \
"Actions:\n" \
" mailbox-list\n" \
@@ -48,11 +50,19 @@ class imap_tool():
" mailbox-rename [mailbox] [mailbox]\n" \
" mailbox-archive [mailbox] [year]\n"
+ # utils
+
+ def print_message(self, message):
+ if self.interactive:
+ print(message)
+
def error_decode(self, data):
return data[0].decode()
- def act(self):
- print("Connecting to " + self.host)
+ # imap-tool
+
+ def action_execute(self):
+ self.print_message("Connecting to " + self.host)
if self.ssl:
imap = imaplib2.IMAP4_SSL(host = self.host, port = self.port)
@@ -60,77 +70,83 @@ class imap_tool():
imap = imaplib2.IMAP4(host = self.host, port = self.port)
if "STARTTLS" not in imap.capabilities:
- print("Unable to create a secure TLS connection")
+ self.print_message("Unable to create a secure TLS connection")
return 1
imap.starttls()
- print("Authenticating with user " + self.user)
+ self.print_message("Authenticating with user " + self.user)
imap.login(self.user, self.password)
- print("")
+ self.print_message("")
try:
if self.action == imap_action.MAILBOX_LIST:
ret, data = imap.list()
- print("Mailbox list:")
+ self.print_message("Mailbox list:")
for entry in data:
name = entry.decode().split(" ")[-1]
- print("- " + name)
+ if self.interactive:
+ print("- " + name)
+ else:
+ print(name)
elif self.action == imap_action.MAILBOX_LIST_SUB:
ret, data = imap.lsub()
- print("Subscribed mailbox list:")
+ self.print_message("Subscribed mailbox list:")
for entry in data:
name = entry.decode().split(" ")[-1]
- print("- " + name)
+ if self.interactive:
+ print("- " + name)
+ else:
+ print(name)
elif self.action == imap_action.MAILBOX_STATS:
ret, data = imap.examine(self.mailbox)
if ret != "OK":
- print("Error getting stats for mailbox " + self.mailbox)
+ self.print_message("Error getting stats for mailbox " + self.mailbox)
raise Exception()
count = int(data[0])
- print("Mailbox " + self.mailbox + " has " + str(count) + " entries")
+ self.print_message("Mailbox " + self.mailbox + " has " + str(count) + " entries")
elif self.action == imap_action.MAILBOX_CREATE:
ret, data = imap.create(self.mailbox)
if ret != "OK":
- print("Error creating mailbox " + self.mailbox + ": " + self.error_decode(data))
+ self.print_message("Error creating mailbox " + self.mailbox + ": " + self.error_decode(data))
raise Exception()
- print("Created mailbox " + self.mailbox)
+ self.print_message("Created mailbox " + self.mailbox)
elif self.action == imap_action.MAILBOX_DELETE:
ret, data = imap.delete(self.mailbox)
if ret != "OK":
- print("Error deleting mailbox " + self.mailbox + ": " + self.error_decode(data))
+ self.print_message("Error deleting mailbox " + self.mailbox + ": " + self.error_decode(data))
raise Exception()
- print("Deleted mailbox " + self.mailbox)
+ self.print_message("Deleted mailbox " + self.mailbox)
elif self.action == imap_action.MAILBOX_SUBSCRIBE:
ret, data = imap.subscribe(self.mailbox)
if ret != "OK":
- print("Error subscribing to mailbox " + self.mailbox + ": " + self.error_decode(data))
+ self.print_message("Error subscribing to mailbox " + self.mailbox + ": " + self.error_decode(data))
raise Exception()
- print("Subscribed to mailbox " + self.mailbox)
+ self.print_message("Subscribed to mailbox " + self.mailbox)
elif self.action == imap_action.MAILBOX_UNSUBSCRIBE:
ret, data = imap.unsubscribe(self.mailbox)
if ret != "OK":
- print("Error unsubscribing from mailbox " + self.mailbox + ": " + self.error_decode(data))
+ self.print_message("Error unsubscribing from mailbox " + self.mailbox + ": " + self.error_decode(data))
raise Exception()
- print("Unsubscribed from mailbox " + self.mailbox)
+ self.print_message("Unsubscribed from mailbox " + self.mailbox)
elif self.action == imap_action.MAILBOX_RENAME:
ret, data = imap.rename(self.mailbox[0], self.mailbox[1])
if ret != "OK":
- print("Error renaming mailbox " + self.mailbox[0] + " to " + self.mailbox[1] + ": " + self.error_decode(data))
+ self.print_message("Error renaming mailbox " + self.mailbox[0] + " to " + self.mailbox[1] + ": " + self.error_decode(data))
raise Exception()
- print("Renamed mailbox " + self.mailbox[0] + " to " + self.mailbox[1])
+ self.print_message("Renamed mailbox " + self.mailbox[0] + " to " + self.mailbox[1])
elif self.action == imap_action.MAILBOX_ARCHIVE:
mailbox_archive = "archives/" + self.mailbox + "/" + str(self.archive_year)
year_start = self.archive_year
@@ -138,7 +154,7 @@ class imap_tool():
ret, data = imap.select(self.mailbox)
if ret != "OK":
- print("Error selecting mailbox " + self.mailbox + ": " + self.error_decode(data))
+ self.print_message("Error selecting mailbox " + self.mailbox + ": " + self.error_decode(data))
raise Exception()
# Two ways to search:
@@ -146,13 +162,13 @@ class imap_tool():
# - SENTSINCE/SENTBEFORE that use message dating, a bit slower
ret, data = imap.uid("SEARCH", None, "(SENTSINCE \"01-Jan-" + str(year_start) + "\" SENTBEFORE \"01-Jan-" + str(year_stop) + "\")")
if ret != "OK":
- print("Error searching mailbox " + self.mailbox + ": " + self.error_decode(data))
+ self.print_message("Error searching mailbox " + self.mailbox + ": " + self.error_decode(data))
raise Exception()
uids_search = data[0].decode("ascii").split()
uids_search_count = len(uids_search)
- print("Found " + str(uids_search_count) + " messages from mailbox " + self.mailbox + " for year " + str(self.archive_year))
+ self.print_message("Found " + str(uids_search_count) + " messages from mailbox " + self.mailbox + " for year " + str(self.archive_year))
if uids_search_count == 0:
raise Exception()
@@ -160,10 +176,10 @@ class imap_tool():
ret, data = imap.create(mailbox_archive)
if ret != "OK":
if not self.error_decode(data).startswith("[ALREADYEXISTS]"):
- print("Error creating mailbox " + mailbox_archive + ": " + self.error_decode(data))
+ self.print_message("Error creating mailbox " + mailbox_archive + ": " + self.error_decode(data))
raise Exception()
else:
- print("Created mailbox " + mailbox_archive)
+ self.print_message("Created mailbox " + mailbox_archive)
index = 0
uids = []
@@ -177,17 +193,17 @@ class imap_tool():
if len(uids) == self.archive_batch or index == uids_search_count:
ret, data = imap.uid("COPY", ",".join(uids), mailbox_archive)
if ret != "OK":
- print("Error copying " + str(len(uids)) + " messages to mailbox " + mailbox_archive)
+ self.print_message("Error copying " + str(len(uids)) + " messages to mailbox " + mailbox_archive)
raise Exception()
ret, data = imap.uid("STORE", ",".join(uids), "+FLAGS", "\\Deleted")
if ret != "OK":
- print("Error deleting " + str(len(uids)) + " messages from mailbox " + self.mailbox)
+ self.print_message("Error deleting " + str(len(uids)) + " messages from mailbox " + self.mailbox)
raise Exception()
imap.expunge()
- print("Archived " + str(len(uids)) + " messages to mailbox " + mailbox_archive)
+ self.print_message("Archived " + str(len(uids)) + " messages to mailbox " + mailbox_archive)
uids = []
except Exception:
@@ -198,7 +214,7 @@ class imap_tool():
return 0
def main(self):
- options, arguments = getopt.getopt(sys.argv[1:], "h:u:p:i:s")
+ options, arguments = getopt.getopt(sys.argv[1:], "h:u:p:i:sn")
for key, value in options:
if key == "-h":
@@ -211,9 +227,11 @@ class imap_tool():
self.port = int(value)
elif key == "-s":
self.ssl = True
+ elif key == "-n":
+ self.interactive = False
if len(arguments) == 0:
- print(self.usage)
+ self.print_message(self.usage)
return 1
action_option = arguments[0]
@@ -245,10 +263,10 @@ class imap_tool():
self.mailbox = arguments[1]
self.archive_year = int(arguments[2])
else:
- print("Invalid action specified")
+ self.print_message("Invalid action specified")
return 1
- return self.act()
+ return self.action_execute()
if __name__ == "__main__":
sys.exit(imap_tool().main())