Skip to content
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
2 changes: 2 additions & 0 deletions pyaarlo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class PyArlo(object):
* **tfa_password** - When using `imap` or `rest-api`, password/token on server. If `None`
will use Arlo password.
* **cipher_list** - Set if your IMAP server is using less secure ciphers.
* **tfa_grab_all** - When using `imap`, grab all emails from inbox, solves issue with slow indexing mail servers. Defaults to `False`.
* **tfa_delete_after** - When using `imap`, delete tfa-email after reading the code. Defaults to `False`.

**Infrequently used `kwargs` parameters:**

Expand Down
8 changes: 8 additions & 0 deletions pyaarlo/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ def tfa_nickname(self):
def wait_for_initial_setup(self):
return self._kw.get("wait_for_initial_setup", True)

@property
def tfa_grab_all(self):
return self._kw.get("tfa_grab_all", False)

@property
def tfa_delete_after(self):
return self._kw.get("tfa_delete_after", False)

@property
def save_state(self):
return self._kw.get("save_state", True)
Expand Down
35 changes: 31 additions & 4 deletions pyaarlo/tfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def start(self):
if res.lower() != "ok":
self.debug("imap login failed")
return False
res, status = self._imap.select(mailbox='INBOX', readonly=True)
res, status = self._imap.select(
mailbox='INBOX', readonly=(not self._arlo.cfg.tfa_delete_after)
)
if res.lower() != "ok":
self.debug("imap select failed")
return False
Expand Down Expand Up @@ -106,9 +108,22 @@ def get(self):
try:
# grab new email ids
self._imap.check()
res, self._new_ids = self._imap.search(
None, "FROM", "do_not_reply@arlo.com"
)

if self._arlo.cfg.tfa_grab_all:
# Grab all to avoid indexing issues
new_ids = []
_, message_ids = self._imap.search(None, 'ALL')
for msg_id in message_ids[0].split():
_, data = self._imap.fetch(msg_id, '(BODY[HEADER.FIELDS (FROM)])')
if b'do_not_reply@arlo.com' in data[0][1]:
new_ids.append(msg_id)

self._new_ids = [b' '.join(new_ids)]
else:
res, self._new_ids = self._imap.search(
None, "FROM", "do_not_reply@arlo.com"
)

self.debug("new-ids={}".format(self._new_ids))
if self._new_ids == self._old_ids:
self.debug("no change in emails")
Expand Down Expand Up @@ -141,6 +156,9 @@ def get(self):
code = re.match(r"^\W+(\d{6})\W*$", line.decode())
if code is not None:
self.debug(f"code={code.group(1)}")
# If tfa_delete_after is set to True, delete the email
if self._arlo.cfg.tfa_delete_after:
self.delete_email(msg_id)
return code.group(1)
except Exception as e:
self.debug(f"trying next part {str(e)}")
Expand All @@ -156,6 +174,15 @@ def get(self):

return None

def delete_email(self, msg_id):
"""Delete the specified email by ID"""
try:
self._imap.store(msg_id, '+FLAGS', '\\Deleted')
self._imap.expunge()
self.debug(f"Email {msg_id} deleted successfully.")
except Exception as e:
self._arlo.error(f"Failed to delete email {msg_id}: {str(e)}")

def stop(self):
self.debug("stopping")

Expand Down