Skip to content

Commit

Permalink
move regex matching to server object, filter all matches by from
Browse files Browse the repository at this point in the history
  • Loading branch information
jesopo committed Sep 11, 2021
1 parent 803ca86 commit c4758fc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 43 deletions.
7 changes: 5 additions & 2 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ oper:
pass: hunter4

patterns:
- 'postfix/qmgr[[]\S+ (?P<id>\S+): from=<(?P<from>noreply\.support@libera\.chat)>, '
- 'postfix/qmgr[[]\S+ (?P<id>\S+): from=<(?P<from>\S+)>, '
- 'postfix/smtp[[]\S+ (?P<id>\S+): to=<(?P<to>\S+)>, .*? status=(?P<status>\S+) [(](?P<reason>.*)[)]$'
- 'postfix-submission/smtpd[[]\S+ NOQUEUE: (?P<status>\S+): (?P<reason>[^;]+); from=<(?P<from>noreply\.support@libera\.chat)> to=<(?P<to>\S+)>'
- 'postfix-submission/smtpd[[]\S+ NOQUEUE: (?P<status>\S+): (?P<reason>[^;]+); from=<(?P<from>\S+)> to=<(?P<to>\S+)>'

froms:
- [email protected]

log-file: /var/log/mail.info
log-line: "PRIVMSG #services :EMAIL:STATUS: {email} {status}: {reason}"
Expand Down
43 changes: 34 additions & 9 deletions tungstite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def __init__(self,
config: Config):

self._config = config
self._emails: TOrderedDict[str, EmailInfo] = \
self._email_queue: TOrderedDict[str, EmailInfo] = \
LimitedOrderedDict(8)
self._emails: TOrderedDict[str, EmailInfo] = \
LimitedOrderedDict(config.history)

super().__init__(bot, name)
Expand Down Expand Up @@ -66,15 +68,38 @@ async def _oper_challenge(self,
await self.send(build("CHALLENGE", [f"+{retort}"]))
break

async def email_sent(self, info: EmailInfo):
self._emails[info.to.lower()] = info
async def log_read_line(self, line: str):
for pattern in self._config.patterns:
if match := pattern.search(line):
groups = dict(match.groupdict())

log = self._config.log_line.format(**{
"email": info.to,
"status": info.status,
"reason": info.reason
})
await self.send_raw(log)
if "id" in groups:
id = groups["id"]
if not id in queue:
sefl._email_queue[id] = EmailInfo()
info = self._email_queue[id]
else:
info = EmailInfo()

if "to" in groups:
info.to = groups["to"]
if "from" in groups:
info._from = groups["from"]
if "status" in groups:
info.status = groups["status"]
if "reason" in groups:
info.reason = groups["reason"]

if (info.finalised() and
info._from in self._config.froms):

self._emails[info.to] = info
log = self._config.log_line.format(**{
"email": info.to,
"status": info.status,
"reason": info.reason
})
await self.send_raw(log)

async def line_read(self, line: Line):
if line.command == RPL_WELCOME:
Expand Down
3 changes: 3 additions & 0 deletions tungstite/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class EmailInfo:
status: Optional[str] = None
reason: Optional[str] = None

def finalised(self) -> bool:
return all([self.to, self._from, self.status, self.reason])

TKey = TypeVar("TKey")
TValue = TypeVar("TValue")

Expand Down
4 changes: 3 additions & 1 deletion tungstite/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
from os.path import expanduser
from re import compile as re_compile
from typing import Any, List, Optional, Pattern, Tuple
from typing import Any, List, Optional, Pattern, Set, Tuple

import yaml

Expand All @@ -19,6 +19,7 @@ class Config(object):
log_file: str
log_line: str
patterns: List[Pattern]
froms: Set[str]
history: int

def load(filepath: str):
Expand Down Expand Up @@ -52,5 +53,6 @@ def load(filepath: str):
expanduser(config_yaml["log-file"]),
config_yaml["log-line"],
[re_compile(p) for p in config_yaml["patterns"]],
set(config_yaml["froms"]),
config_yaml["history"]
)
34 changes: 3 additions & 31 deletions tungstite/tail.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,9 @@ async def tail_log_file(
while True:
line = await file.readline()
if line is not None:
for pattern in patterns:
if match := pattern.search(line):
groups = dict(match.groupdict())

if "id" in groups:
id = groups["id"]
if not id in queue:
queue[id] = EmailInfo()
info = queue[id]
else:
info = EmailInfo()

if "to" in groups:
info.to = groups["to"]
if "from" in groups:
info._from = groups["from"]
if "status" in groups:
info.status = groups["status"]
if "reason" in groups:
info.reason = groups["reason"]

if (info.to is not None and
info._from is not None and
info.status is not None and
info.reason is not None):

servers = list(bot.servers.values())
if servers:
await servers[0].email_sent(info)

break
servers = list(bot.servers.values())
if servers:
await servers[0].log_read_line(line)
else:
asyncio.sleep(0.1)

Expand Down

0 comments on commit c4758fc

Please sign in to comment.