Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ $ python3 -m pip install --upgrade git+https://github.com/Unrud/RadicaleIMAP
type = radicale_imap

# IMAP server host name
# Syntax: address:port
# For example: imap.server.tld
#imap_host =
#imap_hostname =

# IMAP port to use
# For example: 143
#imap_port = 143

# Use StartTLS to secure the connection
# Requires Python >= 3.4
#imap_secure = True
#imap_ssl = True
```

## License
Expand Down
34 changes: 15 additions & 19 deletions radicale_imap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,26 @@ class Auth(BaseAuth):

[auth]
type = radicale_imap
imap_host = imap.server.tld
imap_secure = True

imap_hostname = imap.server.tld
imap_ssl = True
imap_port = 143
"""

def is_authenticated(self, user, password):
host = ""
if self.configuration.has_option("auth", "imap_host"):
host = self.configuration.get("auth", "imap_host")
hostname = ""
if self.configuration.has_option("auth", "imap_hostname"):
hostname = self.configuration.get("auth", "imap_hostname")
secure = True
if self.configuration.has_option("auth", "imap_secure"):
secure = self.configuration.getboolean("auth", "imap_secure")
try:
if ":" in host:
address, port = host.rsplit(":", maxsplit=1)
else:
address, port = host, 143
address, port = address.strip("[] "), int(port)
except ValueError as e:
raise RuntimeError(
"Failed to parse address %r: %s" % (host, e)) from e
if self.configuration.has_option("auth", "imap_ssl"):
secure = self.configuration.getboolean("auth", "imap_ssl")
port = imaplib.IMAP4_SSL_PORT
if self.configuration.has_option("auth", "imap_port"):
port = self.configuration.get("auth", "imap_port") #TODO: cast port to int with proper error handling

if sys.version_info < (3, 4) and secure:
raise RuntimeError("Secure IMAP is not availabe in Python < 3.4")
try:
connection = imaplib.IMAP4(host=address, port=port)
connection = imaplib.IMAP4(host=hostname, port=port)
try:
if sys.version_info < (3, 4):
connection.starttls()
Expand All @@ -75,4 +70,5 @@ def is_authenticated(self, user, password):
return True
except (OSError, imaplib.IMAP4.error) as e:
raise RuntimeError("Failed to communicate with IMAP server %r: "
"%s" % (host, e)) from e
"%s" % (hostname, e)) from e
# TODO: ^ find out how to add int port in log message