From b4235b70cde217c75e79d529a553e387cbaf1bb5 Mon Sep 17 00:00:00 2001 From: Paul Kocialkowski Date: Sun, 16 Apr 2023 15:33:29 +0200 Subject: imap-tool: Implement error management with exception Signed-off-by: Paul Kocialkowski --- imap-tool | 92 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 42 deletions(-) (limited to 'imap-tool') diff --git a/imap-tool b/imap-tool index a5409ee..e99a6fd 100755 --- a/imap-tool +++ b/imap-tool @@ -66,56 +66,64 @@ class imap_tool(): 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: + try: + 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)) + raise Exception() + 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: + 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)) + raise Exception() + 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: + 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)) + raise Exception() + 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: + 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)) + raise Exception() + 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: + elif self.action == imap_action.MAILBOX_STATS: + ret, data = imap.examine(self.mailbox) + if ret != "OK": + print("Error getting stats for mailbox " + self.mailbox) + raise Exception() + count = int(data[0]) print("Mailbox " + self.mailbox + " has " + str(count) + " entries") + except Exception: + imap.logout() + return 1 imap.logout() - return 0 def main(self): -- cgit v1.2.3