From bf86d948b6df8eec6c330464c1e9da78c7d74dbd Mon Sep 17 00:00:00 2001 From: Paul Kocialkowski Date: Wed, 12 Apr 2023 11:41:32 +0200 Subject: imap-tool: Add SSL support Signed-off-by: Paul Kocialkowski --- imap-tool | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/imap-tool b/imap-tool index 0089d87..ecb3f02 100755 --- a/imap-tool +++ b/imap-tool @@ -22,6 +22,7 @@ class imap_tool(): port = 143 user = "user" password = "password" + ssl = False action = imap_action.MAILBOX_LIST usage = "Usage: " + os.path.basename(sys.argv[0]) + " [options] [actions]\n" \ @@ -31,6 +32,7 @@ class imap_tool(): " -u [user]\n" \ " -p [password]\n" \ " -i [port]\n" \ + " -s\n" \ "\n" \ "Actions:\n" \ " mailbox-list\n" \ @@ -47,8 +49,16 @@ class imap_tool(): def act(self): print("Connecting to " + self.host) - imap = imaplib2.IMAP4(host = self.host, port = self.port) - imap.starttls() + if self.ssl: + imap = imaplib2.IMAP4_SSL(host = self.host, port = self.port) + else: + imap = imaplib2.IMAP4(host = self.host, port = self.port) + + if "STARTTLS" not in imap.capabilities: + print("Unable to create a secure TLS connection") + return 1 + + imap.starttls() print("Authenticating with user " + self.user) @@ -106,8 +116,10 @@ class imap_tool(): imap.logout() + return 0 + def main(self): - options, arguments = getopt.getopt(sys.argv[1:], "h:u:p:i:") + options, arguments = getopt.getopt(sys.argv[1:], "h:u:p:i:s") for key, value in options: if key == "-h": @@ -118,6 +130,8 @@ class imap_tool(): self.password = value elif key == "-i": self.port = int(value) + elif key == "-s": + self.ssl = True if len(arguments) == 0: print(self.usage) -- cgit v1.2.3