Skip to content

Commit

Permalink
fix: attempt to fix IO blocking with ssl contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 committed Nov 20, 2024
1 parent 3dcde19 commit dc347c1
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions custom_components/mail_and_packages/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import os
import quopri
import re
import ssl
import subprocess # nosec
import uuid
from datetime import timezone
Expand All @@ -33,6 +32,7 @@
CONF_USERNAME,
)
from homeassistant.core import HomeAssistant
from homeassistant.util import ssl
from PIL import Image, ImageOps

from .const import (
Expand Down Expand Up @@ -119,21 +119,16 @@ async def _test_login(
Returns success boolean
"""
context = ssl.create_default_context()
# Catch invalid mail server / host names
try:
if security == "SSL":
if not verify:
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
context = ssl.client_context_no_verify()
else:
context = ssl.create_default_context(purpose=Purpose.SERVER_AUTH)
context = ssl.client_context()
account = imaplib.IMAP4_SSL(host=host, port=port, ssl_context=context)
elif security == "startTLS":
context = ssl.create_default_context(purpose=Purpose.SERVER_AUTH)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
context = ssl.client_context()
account = imaplib.IMAP4(host=host, port=port)
account.starttls(context)
else:
Expand Down Expand Up @@ -465,16 +460,12 @@ def login(
try:
if security == "SSL":
if not verify:
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
context = ssl.client_context_no_verify()
else:
context = ssl.create_default_context(purpose=Purpose.SERVER_AUTH)
context = ssl.client_context()
account = imaplib.IMAP4_SSL(host=host, port=port, ssl_context=context)
elif security == "startTLS":
context = ssl.create_default_context(purpose=Purpose.SERVER_AUTH)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
context = ssl.client_context()
account = imaplib.IMAP4(host=host, port=port)
account.starttls(context)
else:
Expand Down

0 comments on commit dc347c1

Please sign in to comment.