File size: 2,145 Bytes
e228f7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# src/protocol/enforcement/alert_trigger.py 📣⚠️

import json
import logging
import smtplib
from email.message import EmailMessage
from pathlib import Path

VIOLATIONS_LOG = Path("logs/violations.json")

# CONFIG – update this with your preferred notification settings
ADMIN_EMAIL = "[email protected]"
SMTP_SERVER = "smtp.yourprovider.com"
SMTP_PORT = 587
SMTP_USER = "[email protected]"
SMTP_PASS = "your_email_password_or_token"

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def check_for_violations():
    """
    Loads the violations log and returns the latest unacknowledged alerts.
    """
    if not VIOLATIONS_LOG.exists():
        logging.warning("Violations log not found.")
        return []

    with open(VIOLATIONS_LOG, "r") as f:
        try:
            data = json.load(f)
            return data.get("violations", [])
        except json.JSONDecodeError:
            logging.error("Invalid format in violations.json")
            return []

def send_email_alert(violation: dict):
    """
    Sends an email alert for a violation.
    """
    msg = EmailMessage()
    msg["Subject"] = f"🚨 Belel Protocol Violation Detected"
    msg["From"] = SMTP_USER
    msg["To"] = ADMIN_EMAIL

    msg.set_content(f"""
⚠️ Protocol Violation Detected

Time: {violation.get('timestamp')}
Type: {violation.get('type')}
Details: {violation.get('details')}

Please review immediately.
    """)
    try:
        with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
            server.starttls()
            server.login(SMTP_USER, SMTP_PASS)
            server.send_message(msg)
            logging.info("Violation alert email sent.")
    except Exception as e:
        logging.error(f"Failed to send email: {e}")

def run_alert_trigger():
    violations = check_for_violations()
    for v in violations:
        if not v.get("notified", False):
            send_email_alert(v)
            v["notified"] = True

    # Save updated state
    with open(VIOLATIONS_LOG, "w") as f:
        json.dump({"violations": violations}, f, indent=2)

if __name__ == "__main__":
    run_alert_trigger()