#!/usr/local/bin/python3
import getpass
import subprocess
from pathlib import Path
LOG = Path("/var/gab/chatlog.txt")
STATE = Path.home() / ".gab-notify.lines"
TO = f"{getpass.getuser()}@texto-plano.xyz"
def format_message(line: str) -> str:
"""Turn 'user > text' into 'user: text'."""
user, _, text = line.partition(">")
return f"{user.strip()}: {text.strip()}"
def main() -> None:
lines = LOG.read_text(encoding="utf-8", errors="replace").splitlines()
current = len(lines)
if not STATE.exists():
STATE.write_text(str(current))
return
previous = int(STATE.read_text())
STATE.write_text(str(current))
if current <= previous:
return
new_lines = lines[previous:]
body = "\n- - -\n".join(format_message(line) for line in new_lines)
subprocess.run(
["/usr/bin/mail", "-s", f"gab: {len(new_lines)} mensaje(s) nuevo(s)", TO],
input=body + "\n",
text=True,
check=True,
)
if __name__ == "__main__":
main()