From 4cfd536d415737734d0ef45eb39215330106e7ee Mon Sep 17 00:00:00 2001 From: tyr84 Date: Tue, 18 Feb 2025 11:19:29 +0100 Subject: [PATCH 1/4] initial commit --- README.md | 6 +++++- pyaarlo/cfg.py | 4 ++++ pyaarlo/tfa.py | 19 ++++++++++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5e0f44a..ff0922f 100644 --- a/README.md +++ b/README.md @@ -292,7 +292,11 @@ ar = pyaarlo.PyArlo(username=USERNAME, password=PASSWORD, tfa_username='your-user-name', tfa_password='your-imap-password' ) ``` - +Some email providers are slow to index new mails, set: +```python +tfa_grab_all=True +``` +to grab all emails in inbox and apply sender filter locally. ## Pyaarlo Executable diff --git a/pyaarlo/cfg.py b/pyaarlo/cfg.py index 2b9749a..b723400 100644 --- a/pyaarlo/cfg.py +++ b/pyaarlo/cfg.py @@ -245,6 +245,10 @@ def tfa_nickname(self): @property 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 save_state(self): diff --git a/pyaarlo/tfa.py b/pyaarlo/tfa.py index 8c67daa..1c85a10 100644 --- a/pyaarlo/tfa.py +++ b/pyaarlo/tfa.py @@ -106,9 +106,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") From be8a0d9efbdf49817e486abbf55b99dba847d6c5 Mon Sep 17 00:00:00 2001 From: kaffetorsk Date: Tue, 18 Feb 2025 11:19:29 +0100 Subject: [PATCH 2/4] add delete_after option --- README.md | 6 ++++++ pyaarlo/cfg.py | 6 +++++- pyaarlo/tfa.py | 16 +++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ff0922f..83ec8df 100644 --- a/README.md +++ b/README.md @@ -298,6 +298,12 @@ tfa_grab_all=True ``` to grab all emails in inbox and apply sender filter locally. + +To delete tfa mails after use, set: +```python +tfa_delete_after=True +``` + ## Pyaarlo Executable diff --git a/pyaarlo/cfg.py b/pyaarlo/cfg.py index b723400..b085938 100644 --- a/pyaarlo/cfg.py +++ b/pyaarlo/cfg.py @@ -245,11 +245,15 @@ def tfa_nickname(self): @property 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) diff --git a/pyaarlo/tfa.py b/pyaarlo/tfa.py index 1c85a10..75cd1e0 100644 --- a/pyaarlo/tfa.py +++ b/pyaarlo/tfa.py @@ -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 @@ -154,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)}") @@ -169,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") From fa4427d533fafe3479e639807c85b75bb4f1a5b2 Mon Sep 17 00:00:00 2001 From: kaffetorsk Date: Tue, 18 Feb 2025 11:19:29 +0100 Subject: [PATCH 3/4] cleanup docs --- README.md | 10 ---------- pyaarlo/__init__.py | 3 ++- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 83ec8df..5e0f44a 100644 --- a/README.md +++ b/README.md @@ -292,17 +292,7 @@ ar = pyaarlo.PyArlo(username=USERNAME, password=PASSWORD, tfa_username='your-user-name', tfa_password='your-imap-password' ) ``` -Some email providers are slow to index new mails, set: -```python -tfa_grab_all=True -``` -to grab all emails in inbox and apply sender filter locally. - -To delete tfa mails after use, set: -```python -tfa_delete_after=True -``` ## Pyaarlo Executable diff --git a/pyaarlo/__init__.py b/pyaarlo/__init__.py index b42234c..d8127ca 100644 --- a/pyaarlo/__init__.py +++ b/pyaarlo/__init__.py @@ -109,7 +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:** These parameters are very rarely changed. From bab75503e812b9aec4974af2af374da49282c1c9 Mon Sep 17 00:00:00 2001 From: kaffetorsk Date: Tue, 18 Feb 2025 11:19:29 +0100 Subject: [PATCH 4/4] typo --- pyaarlo/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyaarlo/__init__.py b/pyaarlo/__init__.py index d8127ca..38f68e2 100644 --- a/pyaarlo/__init__.py +++ b/pyaarlo/__init__.py @@ -111,6 +111,7 @@ class PyArlo(object): * **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:** These parameters are very rarely changed.