Skip to content

Commit

Permalink
feat: Spam Check Log
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Oct 18, 2024
1 parent 1ff4a93 commit 3573847
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 27 deletions.
44 changes: 44 additions & 0 deletions mail_server/api/spamd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from html import unescape
from typing import Literal

import frappe
from frappe import _

from mail_server.mail_server.doctype.spam_check_log.spam_check_log import create_spam_check_log


@frappe.whitelist(methods=["POST"], allow_guest=True)
def scan(message: str) -> dict:
"""Returns the spam score, spamd response and scanning mode of the message"""

message = get_unescaped_message(message)
spam_log = create_spam_check_log(message)
return {
"spam_score": spam_log.spam_score,
"spamd_response": spam_log.spamd_response,
"scanning_mode": spam_log.scanning_mode,
}


@frappe.whitelist(methods=["POST"], allow_guest=True)
def is_spam(message: str, message_type: Literal["Inbound", "Outbound"] = "Outbound") -> bool:
"""Returns True if the message is spam else False"""

message = get_unescaped_message(message)
spam_log = create_spam_check_log(message)
return spam_log.is_spam(message_type)


@frappe.whitelist(methods=["POST"], allow_guest=True)
def get_spam_score(message: str) -> float:
"""Returns the spam score of the message"""

message = get_unescaped_message(message)
spam_log = create_spam_check_log(message)
return spam_log.spam_score


def get_unescaped_message(message: str) -> str:
"""Returns the unescaped message"""

return unescape(message)
26 changes: 20 additions & 6 deletions mail_server/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,27 @@
app_description = "Frappe Mail Server"
app_email = "developers@frappe.io"
app_license = "agpl-3.0"
# required_apps = []

# Apps
# ------------------

# required_apps = []
website_redirects = [
{
"source": "/spamd/scan",
"target": "/api/method/mail.api.spamd.scan",
"redirect_http_status": 307,
},
{
"source": "/spamd/is-spam",
"target": "/api/method/mail.api.spamd.is_spam",
"redirect_http_status": 307,
},
{
"source": "/spamd/score",
"target": "/api/method/mail.api.spamd.get_spam_score",
"redirect_http_status": 307,
},
]


# Each item in the list will be shown as an app in the apps page
# add_to_apps_screen = [
Expand Down Expand Up @@ -238,6 +254,4 @@
# Automatically update python controller files with type annotations for this app.
# export_python_type_annotations = True

# default_log_clearing_doctypes = {
# "Logging DocType Name": 30 # days to retain logs
# }
default_log_clearing_doctypes = {"Spam Check Log": 7}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
"scanning_mode",
"hybrid_scanning_threshold",
"section_break_f89w",
"scan_outgoing_mail",
"block_spam_outgoing_mail",
"enable_spam_detection_for_outbound",
"block_outbound_spam",
"column_break_rdyc",
"max_spam_score_for_outbound",
"outbound_spam_threshold",
"section_break_viyu",
"scan_incoming_mail",
"reject_spam",
"enable_spam_detection_for_inbound",
"reject_inbound_spam",
"column_break_1gdq",
"max_spam_score_for_inbound"
"inbound_spam_threshold"
],
"fields": [
{
Expand Down Expand Up @@ -206,30 +206,30 @@
},
{
"default": "1",
"fieldname": "scan_outgoing_mail",
"fieldname": "enable_spam_detection_for_outbound",
"fieldtype": "Check",
"label": "Enable",
"read_only_depends_on": "eval: !doc.enable_spam_detection"
},
{
"default": "1",
"fieldname": "block_spam_outgoing_mail",
"fieldname": "block_outbound_spam",
"fieldtype": "Check",
"label": "Block Spam",
"read_only_depends_on": "eval: !doc.enable_spam_detection || !doc.scan_outgoing_mail"
"read_only_depends_on": "eval: !doc.enable_spam_detection || !doc.enable_spam_detection_for_outbound"
},
{
"fieldname": "column_break_rdyc",
"fieldtype": "Column Break"
},
{
"default": "5",
"fieldname": "max_spam_score_for_outbound",
"default": "3.5",
"fieldname": "outbound_spam_threshold",
"fieldtype": "Float",
"label": "Threshold",
"mandatory_depends_on": "eval: doc.scan_outgoing_mail",
"mandatory_depends_on": "eval: doc.enable_spam_detection_for_outbound",
"precision": "1",
"read_only_depends_on": "eval: !doc.enable_spam_detection || !doc.scan_outgoing_mail"
"read_only_depends_on": "eval: !doc.enable_spam_detection || !doc.enable_spam_detection_for_outbound"
},
{
"fieldname": "section_break_viyu",
Expand All @@ -238,36 +238,36 @@
},
{
"default": "1",
"fieldname": "scan_incoming_mail",
"fieldname": "enable_spam_detection_for_inbound",
"fieldtype": "Check",
"label": "Enable",
"read_only_depends_on": "eval: !doc.enable_spam_detection"
},
{
"default": "1",
"fieldname": "reject_spam",
"fieldname": "reject_inbound_spam",
"fieldtype": "Check",
"label": "Reject Spam",
"read_only_depends_on": "eval: !doc.enable_spam_detection || !doc.scan_incoming_mail"
"read_only_depends_on": "eval: !doc.enable_spam_detection || !doc.enable_spam_detection_for_inbound"
},
{
"fieldname": "column_break_1gdq",
"fieldtype": "Column Break"
},
{
"default": "5",
"fieldname": "max_spam_score_for_inbound",
"default": "3",
"fieldname": "inbound_spam_threshold",
"fieldtype": "Float",
"label": "Threshold",
"mandatory_depends_on": "eval: doc.scan_incoming_mail",
"mandatory_depends_on": "eval: doc.enable_spam_detection_for_inbound",
"precision": "1",
"read_only_depends_on": "eval: !doc.enable_spam_detection || !doc.scan_incoming_mail"
"read_only_depends_on": "eval: !doc.enable_spam_detection || !doc.enable_spam_detection_for_inbound"
}
],
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2024-10-18 18:01:43.685607",
"modified": "2024-10-18 19:27:30.550462",
"modified_by": "Administrator",
"module": "Mail Server",
"name": "Mail Server Settings",
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2024, Frappe Technologies Pvt. Ltd. and contributors
// For license information, please see license.txt

// frappe.ui.form.on("Spam Check Log", {
// refresh(frm) {

// },
// });
162 changes: 162 additions & 0 deletions mail_server/mail_server/doctype/spam_check_log/spam_check_log.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"actions": [],
"allow_rename": 1,
"autoname": "hash",
"creation": "2024-10-11 14:41:45.624692",
"doctype": "DocType",
"engine": "InnoDB",
"field_order": [
"section_break_vtj1",
"source_ip_address",
"column_break_9x8t",
"source_host",
"section_break_opob",
"spamd_response",
"column_break_c5vz",
"scanning_mode",
"hybrid_scanning_threshold",
"spam_score",
"section_break_5qon",
"started_at",
"completed_at",
"column_break_svei",
"duration",
"section_break_o1qt",
"message"
],
"fields": [
{
"fieldname": "section_break_vtj1",
"fieldtype": "Section Break"
},
{
"fieldname": "source_ip_address",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Source IP Address",
"no_copy": 1,
"read_only": 1
},
{
"fieldname": "column_break_9x8t",
"fieldtype": "Column Break"
},
{
"fieldname": "source_host",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Source Host",
"no_copy": 1,
"read_only": 1
},
{
"fieldname": "section_break_opob",
"fieldtype": "Section Break"
},
{
"fieldname": "spamd_response",
"fieldtype": "Code",
"label": "Response",
"no_copy": 1,
"read_only": 1
},
{
"fieldname": "column_break_c5vz",
"fieldtype": "Column Break"
},
{
"fieldname": "scanning_mode",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Scanning Mode",
"no_copy": 1,
"read_only": 1
},
{
"depends_on": "eval: doc.scanning_mode == \"Hybrid Approach\"",
"fieldname": "hybrid_scanning_threshold",
"fieldtype": "Float",
"label": "Hybrid Scanning Threshold",
"no_copy": 1,
"precision": "1",
"read_only": 1
},
{
"fieldname": "spam_score",
"fieldtype": "Float",
"in_list_view": 1,
"label": "Spam Score",
"no_copy": 1,
"precision": "1",
"read_only": 1
},
{
"fieldname": "section_break_5qon",
"fieldtype": "Section Break"
},
{
"fieldname": "started_at",
"fieldtype": "Datetime",
"label": "Started At",
"no_copy": 1,
"read_only": 1
},
{
"fieldname": "completed_at",
"fieldtype": "Datetime",
"label": "Completed At",
"no_copy": 1,
"read_only": 1
},
{
"fieldname": "column_break_svei",
"fieldtype": "Column Break"
},
{
"depends_on": "eval: doc.started_at && doc.completed_at",
"fieldname": "duration",
"fieldtype": "Float",
"in_list_view": 1,
"label": "Duration (Seconds)",
"no_copy": 1,
"read_only": 1
},
{
"fieldname": "section_break_o1qt",
"fieldtype": "Section Break"
},
{
"fieldname": "message",
"fieldtype": "Code",
"label": "Message",
"no_copy": 1,
"read_only": 1
}
],
"in_create": 1,
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-10-18 19:02:13.151207",
"modified_by": "Administrator",
"module": "Mail Server",
"name": "Spam Check Log",
"naming_rule": "Random",
"owner": "Administrator",
"permissions": [
{
"create": 1,
"delete": 1,
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "System Manager",
"share": 1,
"write": 1
}
],
"sort_field": "creation",
"sort_order": "DESC",
"states": []
}
Loading

0 comments on commit 3573847

Please sign in to comment.