49_Arlo.pm: fixed login problems due to new mail format

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@24510 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
maluk 2021-05-25 17:21:07 +00:00
parent 48a09c2e4a
commit 3bf4df8527
2 changed files with 26 additions and 12 deletions

View File

@ -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') {

View File

@ -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)