From 14912ae056a3cf8669164dc9d92a045cebe5d11c Mon Sep 17 00:00:00 2001 From: Paul Kocialkowski Date: Tue, 24 Jan 2023 10:42:59 +0100 Subject: imap-tool: Update with error decode and class Signed-off-by: Paul Kocialkowski --- imap-tool | 225 ++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 125 insertions(+), 100 deletions(-) (limited to 'imap-tool') diff --git a/imap-tool b/imap-tool index f8a3474..7d55770 100755 --- a/imap-tool +++ b/imap-tool @@ -17,113 +17,138 @@ class imap_action(Enum): MAILBOX_UNSUBSCRIBE = 6 MAILBOX_STATS = 7 -def imap_main(): +class imap_tool(): host = "localhost" port = 143 user = "user" password = "password" action = imap_action.MAILBOX_LIST - options, arguments = getopt.getopt(sys.argv[1:], "h:u:p:i:") - - for option, value in options: - if option == "-h": - host = value - elif option == "-u": - user = value - elif option == "-p": - password = value - elif option == "-i": - port = int(value) - - if len(arguments) == 0: - print("No action specified") - return 1 - - action_option = arguments[0] - - if action_option == "mailbox-list": - action = imap_action.MAILBOX_LIST - elif action_option == "mailbox-list-sub": - action = imap_action.MAILBOX_LIST_SUB - elif action_option == "mailbox-create": - action = imap_action.MAILBOX_CREATE - mailbox = arguments[1] - elif action_option == "mailbox-delete": - action = imap_action.MAILBOX_DELETE - mailbox = arguments[1] - elif action_option == "mailbox-subscribe": - action = imap_action.MAILBOX_SUBSCRIBE - mailbox = arguments[1] - elif action_option == "mailbox-unsubscribe": - action = imap_action.MAILBOX_UNSUBSCRIBE - mailbox = arguments[1] - elif action_option == "mailbox-stats": - action = imap_action.MAILBOX_STATS - mailbox = arguments[1] - else: - print("Invalid action specified") - return 1 - - print("Connecting to " + host) - - imap = imaplib2.IMAP4(host = host, port = port) - imap.starttls() - - print("Authenticating with user " + user) - - imap.login(user, password) - - print("") - - if action == imap_action.MAILBOX_LIST: - ret, data = imap.list() - - print("Mailbox list:") - for entry in data: - name = entry.decode().split(" ")[-1] - print("- " + name) - elif 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 action == imap_action.MAILBOX_CREATE: - ret = imap.create(mailbox) - if ret != "OK": - print("Error creating mailbox " + mailbox) + 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("Created mailbox " + mailbox) - elif action == imap_action.MAILBOX_DELETE: - ret = imap.delete(mailbox) - if ret != "OK": - print("Error deleting mailbox " + mailbox) - else: - print("Deleted mailbox " + mailbox) - elif action == imap_action.MAILBOX_SUBSCRIBE: - ret = imap.subscribe(mailbox) - if ret != "OK": - print("Error subscribing to mailbox " + mailbox) - else: - print("Subscribed to mailbox " + mailbox) - elif action == imap_action.MAILBOX_UNSUBSCRIBE: - ret = imap.unsubscribe(mailbox) - if ret != "OK": - print("Error unsubscribing from mailbox " + mailbox) - else: - print("Unsubscribed from mailbox " + mailbox) - elif action == imap_action.MAILBOX_STATS: - ret, data = imap.examine(mailbox) - if ret != "OK": - print("Error getting stats for mailbox " + mailbox) - else: - count = int(data[0]) - print("Mailbox " + mailbox + " has " + str(count) + " entries") + print("Invalid action specified") + return 1 - imap.logout() + return self.act() if __name__ == "__main__": - imap_main() + imap_tool().main() -- cgit v1.2.3