summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Kocialkowski2022-08-21 13:13:05 +0200
committerPaul Kocialkowski2022-08-21 13:13:05 +0200
commit39a88053ce79967b4f2fa5f41557111e7b14b5a4 (patch)
treedf0415159180ea0fc43b1d9addb19282619bbff6
parent78db0a8b9b9bab55ee7bd2d14e934b4ec3bc1a40 (diff)
Add imap-tool util
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
-rwxr-xr-ximap-tool129
1 files changed, 129 insertions, 0 deletions
diff --git a/imap-tool b/imap-tool
new file mode 100755
index 0000000..f8a3474
--- /dev/null
+++ b/imap-tool
@@ -0,0 +1,129 @@
+#!/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
+
+def imap_main():
+ 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)
+ 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")
+
+ imap.logout()
+
+if __name__ == "__main__":
+ imap_main()