#!/usr/bin/python import os import sys import yaml import getopt import imaplib2 from enum import Enum class imap_action(Enum): MAILBOX_LIST = 1 MAILBOX_LIST_SUB = 2 MAILBOX_CREATE = 3 MAILBOX_DELETE = 4 MAILBOX_SUBSCRIBE = 5 MAILBOX_UNSUBSCRIBE = 6 MAILBOX_STATS = 7 class imap_tool(): host = "localhost" port = 143 user = "user" password = "password" action = imap_action.MAILBOX_LIST usage = "Usage: " + os.path.basename(sys.argv[0]) + " [options] [actions]\n" \ "\n" \ "Options:\n" \ " -h [host]\n" \ " -u [user]\n" \ " -p [password]\n" \ " -i [port]\n" \ "\n" \ "Actions:\n" \ " mailbox-list\n" \ " mailbox-list-sub\n" \ " mailbox-create\n" \ " mailbox-delete\n" \ " mailbox-subscribe\n" \ " mailbox-unsubscribe\n" \ " mailbox-stats" def error_decode(self, data): return data[0].decode() def act(self): print("Connecting to " + self.host) imap = imaplib2.IMAP4(host = self.host, port = self.port) imap.starttls() print("Authenticating with user " + self.user) imap.login(self.user, self.password) print("") if self.action == imap_action.MAILBOX_LIST: ret, data = imap.list() print("Mailbox list:") for entry in data: name = entry.decode().split(" ")[-1] print("- " + name) elif self.action == imap_action.MAILBOX_LIST_SUB: ret, data = imap.lsub() print("Subscribed mailbox list:") for entry in data: name = entry.decode().split(" ")[-1] print("- " + name) 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)) else: print("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)) else: print("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)) else: print("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)) else: print("Unsubscribed from mailbox " + self.mailbox) elif self.action == imap_action.MAILBOX_STATS: ret, data = imap.examine(self.mailbox) if ret != "OK": print("Error getting stats for mailbox " + self.mailbox) else: count = int(data[0]) print("Mailbox " + self.mailbox + " has " + str(count) + " entries") imap.logout() def main(self): options, arguments = getopt.getopt(sys.argv[1:], "h:u:p:i:") for key, value in options: if key == "-h": self.host = value elif key == "-u": self.user = value elif key == "-p": self.password = value elif key == "-i": self.port = int(value) if len(arguments) == 0: print(usage) return 1 action_option = arguments[0] if action_option == "mailbox-list": self.action = imap_action.MAILBOX_LIST elif action_option == "mailbox-list-sub": self.action = imap_action.MAILBOX_LIST_SUB elif action_option == "mailbox-create": self.action = imap_action.MAILBOX_CREATE self.mailbox = arguments[1] elif action_option == "mailbox-delete": self.action = imap_action.MAILBOX_DELETE self.mailbox = arguments[1] elif action_option == "mailbox-subscribe": self.action = imap_action.MAILBOX_SUBSCRIBE self.mailbox = arguments[1] elif action_option == "mailbox-unsubscribe": self.action = imap_action.MAILBOX_UNSUBSCRIBE self.mailbox = arguments[1] elif action_option == "mailbox-stats": self.action = imap_action.MAILBOX_STATS self.mailbox = arguments[1] else: print("Invalid action specified") return 1 return self.act() if __name__ == "__main__": sys.exit(imap_tool().main())