summaryrefslogtreecommitdiffstats
path: root/.bin/get-mailbox-imap
diff options
context:
space:
mode:
authorRomain Gonçalves <me@rgoncalves.se>2021-12-23 18:28:03 +0000
committerRomain Gonçalves <me@rgoncalves.se>2021-12-23 18:28:03 +0000
commit0f08d04698c814955116b6bae50752e64b774d8f (patch)
tree8cf9a33557093eebfd25aab2872e97639c7e2f62 /.bin/get-mailbox-imap
downloaddots-0f08d04698c814955116b6bae50752e64b774d8f.tar.gz
Thu Dec 23 06:28:03 PM UTC 2021
Diffstat (limited to '.bin/get-mailbox-imap')
-rwxr-xr-x.bin/get-mailbox-imap77
1 files changed, 77 insertions, 0 deletions
diff --git a/.bin/get-mailbox-imap b/.bin/get-mailbox-imap
new file mode 100755
index 0000000..fad7e21
--- /dev/null
+++ b/.bin/get-mailbox-imap
@@ -0,0 +1,77 @@
+#!/usr/bin/env python3
+
+import imaplib
+import sys
+
+
+def get_boxes(list):
+ """
+ Retrieve and decode all mailboxes.
+ """
+ return [box.decode('utf-8').rsplit(' ')[-1] for box in list]
+
+
+def flatten_output(list):
+ """
+ Print all boxes with a flattened output,
+ primarily for neomutt usage.
+ """
+ print(''.join([f"+'{box}' " for box in list]), end=' ')
+
+
+def sort_boxes(bxs):
+ """
+ Sort boxes list,
+ according to a predefined order
+ """
+
+ order = [
+ "INBOX",
+ "Unread",
+ "Drafts",
+ "Sent",
+ "Spam",
+ "Trash",
+ "Junk",
+ "Archive"
+ ]
+
+ bxs_orig = sorted(bxs)
+
+ # sort based on predefined order
+ bxs = []
+ for exp in order:
+ matching = [s for s in bxs_orig if exp in s]
+ bxs.extend(matching)
+
+ # ensure all retrieved boxes are present
+ for bx in bxs_orig:
+ if bx not in bxs:
+ bxs.append(bx)
+
+ return bxs
+
+
+def main():
+ """
+ Retrieve, sort, and pretty print for neomutt
+ """
+
+ # user information
+ remote = sys.argv[1]
+ username = sys.argv[2]
+ password = sys.argv[3]
+
+ # connection
+ mail = imaplib.IMAP4_SSL(remote)
+ mail.login(username, password)
+
+ # parse folders output
+ bxs = sort_boxes(get_boxes(mail.list()[1]))
+
+ # oneline pretty-print for neomutt
+ flatten_output(bxs)
+
+
+if __name__ == "__main__":
+ main()
remember that computers suck.