From 42ee822a2d5fd875a2209cf00d6a623498935731 Mon Sep 17 00:00:00 2001 From: maluk <> Date: Tue, 25 May 2021 17:21:07 +0000 Subject: [PATCH] 49_Arlo.pm: fixed login problems due to new mail format git-svn-id: https://svn.fhem.de/fhem/trunk@24510 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/49_Arlo.pm | 16 ++++++++++------ fhem/contrib/49_Arlo.py | 22 ++++++++++++++++------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/fhem/FHEM/49_Arlo.pm b/fhem/FHEM/49_Arlo.pm index f8c685843..3417e24df 100644 --- a/fhem/FHEM/49_Arlo.pm +++ b/fhem/FHEM/49_Arlo.pm @@ -1128,11 +1128,8 @@ sub Arlo_Login($) { return; } - my $tmpFile = '/tmp/arlo'; - system "python3 contrib/49_Arlo.py $hash->{helper}{username} $hash->{helper}{password} $mailServer $hash->{helper}{mailUser} $hash->{helper}{mailPassword} > $tmpFile &"; + system "python3 contrib/49_Arlo.py $hash->{helper}{username} $hash->{helper}{password} $mailServer $hash->{helper}{mailUser} $hash->{helper}{mailPassword} > /tmp/arlo &"; - open(my $fh, '<', $tmpFile); - $hash->{helper}{pythonFh} = $fh; $hash->{helper}{pythonTimeout} = gettimeofday() + 120; InternalTimer(gettimeofday() + 1, "Arlo_ReadPythonResult", $hash); } @@ -1145,6 +1142,10 @@ sub Arlo_ReadPythonResult($) { return; } my $fh = $hash->{helper}{pythonFh}; + if (!defined($fh)) { + open($fh, '<', '/tmp/arlo'); + $hash->{helper}{pythonFh} = $fh; + } my $line = <$fh>; while (defined($line)) { $line =~ s/\s+$//; @@ -1358,8 +1359,11 @@ sub Arlo_ProcessResponse($$) { $hash->{SSE_STATUS} = 299; } } elsif ($check eq 'Vary:') { - Log3 $hash->{NAME}, 3, "Arlo event queue error: subscription declined (Header $line). Retry event subscription."; - $hash->{SSE_STATUS} = 298; + my $val = substr($line, 6, 6); + if ($val ne 'Origin' && $val ne 'Access') { + Log3 $hash->{NAME}, 3, "Arlo event queue error: subscription declined (Header $line). $val Retry event subscription."; + $hash->{SSE_STATUS} = 298; + } } elsif ($check ne 'event' && $check ne 'Cache' && $check ne 'Conte' && $check ne 'Date:' && $check ne 'Pragm' && $check ne 'Server' && $check ne 'Acces' && substr($check, 0, 2) ne 'X-' && $check ne 'trans' && $check ne 'Serve' && $check ne 'Expir' && $check ne 'Stric' && $check ne 'Trans' && $check ne 'Expec' && $check ne 'CF-RA' && $check ne 'CF-Ca' && $check ne 'reque' && $check ne 'x-tra' && $check ne 'cf-re') { diff --git a/fhem/contrib/49_Arlo.py b/fhem/contrib/49_Arlo.py index c63722bb5..5c7ffe4b6 100644 --- a/fhem/contrib/49_Arlo.py +++ b/fhem/contrib/49_Arlo.py @@ -5,6 +5,7 @@ import cloudscraper import email import imaplib import re +from html.parser import HTMLParser class Arlo: def __init__(self, tfa_mail_check) -> None: @@ -55,7 +56,6 @@ class Arlo: error("getFactors not successful, response code " + str(r.status_code)) return - factor_id = None for factor in data["items"]: if factor["factorType"] == "EMAIL": self._auth_tfa(factor["factorId"]) @@ -173,16 +173,26 @@ class TfaMailCheck: res, msg = self._imap.fetch(msg_id, "(RFC822)") for part in email.message_from_bytes(msg[0][1]).walk(): if part.get_content_type() == "text/html": - for line in part.get_payload().splitlines(): - code = re.match(r"^\W*(\d{6})\W*$", line) - if code is not None: - self._imap.store(msg_id, "+FLAGS", "\\Deleted") - return code.group(1) + code_filter = CodeFilter() + code_filter.feed(part.get_payload()) + if code_filter.code: + self._imap.store(msg_id, "+FLAGS", "\\Deleted") + return code_filter.code def close(self): self._imap.close() self._imap.logout() +class CodeFilter(HTMLParser): + code = None + def handle_data(self, data): + if self.code: + return + line = data.strip().replace("=09", "") + match = re.match(r"\d{6}", line) + if match: + self.code = match.group(0) + def status(status): print("status:", status, flush=True)