Skip to content

Commit

Permalink
Merge pull request #231 from ninoseki/fix-sa-report-parse-issue
Browse files Browse the repository at this point in the history
fix: fix SpamAssassin report parsing issue (#229)
  • Loading branch information
ninoseki authored May 25, 2024
2 parents a756fdb + 86da05d commit 157bce0
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions backend/clients/spamassasin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import re

import aiospamc
from aiospamc.header_values import Headers
from async_timeout import timeout

from backend import schemas, settings


def is_header(line: str) -> bool:
headers = ["pts", "rule name", "description"]
return all(header in line for header in headers)


def is_divider(line: str) -> bool:
return re.match(r"^-+\s-+\s-+$", line) is not None


class Parser:
def __init__(self, headers: Headers, body: str):
self.headers = headers
Expand Down Expand Up @@ -42,9 +53,19 @@ def _parse_details(self, details: str) -> list[schemas.SpamAssassinDetail]:

def _parse_body(self):
lines = [line for line in self.body.splitlines() if line != ""]
delimiter_index = 0
delimiter_index: int | None = None

headers_seen: bool = False
divider_seen: bool = False

for index, line in enumerate(lines):
if "---" in line:
if not headers_seen and is_header(line):
headers_seen = True

if headers_seen and not divider_seen and is_divider(line):
divider_seen = True

if headers_seen and divider_seen:
delimiter_index = index + 1
break

Expand Down

0 comments on commit 157bce0

Please sign in to comment.