summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Kocialkowski2023-04-16 15:33:29 +0200
committerPaul Kocialkowski2023-04-16 15:33:58 +0200
commitb4235b70cde217c75e79d529a553e387cbaf1bb5 (patch)
treee4ab202a608c95ca62d6916eda7745842afc1602
parent187892eb31cb5d379ef8a7e9e6a72c3ef60e022c (diff)
imap-tool: Implement error management with exception
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
-rwxr-xr-ximap-tool92
1 files changed, 50 insertions, 42 deletions
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):