- [ChatGPT API][Ollama API][OpenAI Comp API][Gemini API] Added an option to automatically tag incoming emails [#237].
+ - [ChatGPT API][Ollama API][OpenAI Comp API][Gemini API] Added an configurable antispam filter for incoming emails [#231].
- Added the {%thunderai_def_sign%} placeholder to get the default signature as defined in the options [#248].
- Added the {%thunderai_def_lang%} placeholder to get the default language as defined in the options [#248].
- Various minor improvements.
diff --git a/pages/spamfilter/mzta-spamfilter.css b/pages/spamfilter/mzta-spamfilter.css
new file mode 100644
index 00000000..e62e300d
--- /dev/null
+++ b/pages/spamfilter/mzta-spamfilter.css
@@ -0,0 +1,154 @@
+#spamfilter_prompt_container{
+ width: 90%;
+ margin: 20px auto;
+}
+
+#spamfilter_prompt_text{
+ width: 100%;
+}
+
+.infoline{
+ font-size: 0.8em;
+ font-style: italic;
+}
+
+.btn_div{
+ width: 100%;
+ display: flex;
+ justify-content: space-between;
+}
+
+table#miczPrefs {
+ padding: 10px;
+ border-spacing: 0px;
+ width: 90%;
+ margin: 0px auto 40px auto;
+ border: 1px solid #ccc;
+}
+
+.spamfilter_table_title{
+ width: 90%;
+ margin: auto;
+}
+
+.section_title{
+ font-weight: bold;
+}
+
+table#miczPrefs td{
+ border-top: 1px solid #ccc;
+ border-bottom: 1px solid #ccc;
+ vertical-align: top;
+ padding: 2px;
+}
+
+table#miczPrefs tr:first-child td {
+ border-top: none;
+}
+
+table#miczPrefs tr:last-child td {
+ border-bottom: none;
+}
+
+.autocomplete-container {
+ position: relative;
+}
+
+.autocomplete-list {
+ position: absolute;
+ top: 100%;
+ left: 0;
+ right: 0;
+ background-color: white;
+ border: 1px solid #ccc;
+ z-index: 1000;
+ max-height: 200px;
+ overflow-y: auto;
+ padding: 0;
+ margin: 0;
+ list-style: none;
+ font-size: small;
+}
+
+.autocomplete-list li {
+ padding: 8px;
+ cursor: pointer;
+}
+
+.autocomplete-list li:hover {
+ background-color: #f0f0f0;
+}
+
+.autocomplete-list li.active {
+ background-color: #ddd;
+}
+
+.hidden {
+ display: none;
+}
+
+.unsaved{
+ color: red;
+}
+
+#spamfilter_threshold_too_low{
+ display: none;
+ color:red;
+ font-weight: bold;
+ font-size: 1.1em;
+ margin-left: 10px;
+}
+
+table#report_data {
+ width: 100%;
+ border-collapse: collapse;
+ margin-top: 20px;
+}
+table#report_data th, table#report_data td {
+ border: 1px solid #ddd;
+ padding: 8px;
+ text-align: left;
+}
+table#report_data th {
+ background-color: #f4f4f4;
+}
+
+@media (prefers-color-scheme: dark) {
+ body {
+ background-color: #1C1B22;
+ color: rgb(251, 251, 254);
+ }
+
+ a:link { color: #409EFF; }
+ a:visited { color: #409EFF; }
+ a:hover { color: #66B1FF; }
+ a:active { color: #66B1FF; }
+ a:active { color: #66B1FF; }
+
+ .autocomplete-list {
+ background-color: #2E2F36;
+ border: 1px solid #2E2F36;
+ }
+
+ .autocomplete-list li:hover {
+ background-color: #4c4e58;
+ }
+
+ .autocomplete-list li.active {
+ background-color: #4c4e58;
+ }
+
+ table#report_data th,
+ table#report_data td {
+ border-color: #444;
+ color: #ddd;
+ }
+
+ table#report_data th {
+ background-color: #333;
+ }
+
+ table#report_data td {
+ background-color: #222;
+ }
+}
\ No newline at end of file
diff --git a/pages/spamfilter/mzta-spamfilter.html b/pages/spamfilter/mzta-spamfilter.html
new file mode 100644
index 00000000..2a2f2846
--- /dev/null
+++ b/pages/spamfilter/mzta-spamfilter.html
@@ -0,0 +1,59 @@
+
+
+
+
+ ThunderAI - __MSG_SpamFilter_PageTitle__
+
+
+
+
+
+
__MSG_SpamFilter_PageTitle__
+
__MSG_SpamFilter_info_default__
+
+ __MSG_SpamFilter_prompt_prefs_title__
+
+
+
__MSG_GetCalendarEvent_prompt_text_title__ __MSG_customPrompts_unsaved_changes__
+
__MSG_prefs_OptionText_btnManagePrompts_infoline__ __MSG_customPrompts_managePrompts_help__
+
__MSG_prefs_OptionText_GetCalendarEvent_infoline2__
+
+
+
+
+
+ __MSG_SpamReport_Title__
+
+
+
+ | Message_ID |
+ __MSG_Date__ |
+ __MSG_From__ |
+ __MSG_Subject__ |
+ __MSG_Spam_Value__ |
+ __MSG_Moved_to_Spam__ |
+ __MSG_Explanation__ |
+ __MSG_Report_Date__ |
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pages/spamfilter/mzta-spamfilter.js b/pages/spamfilter/mzta-spamfilter.js
new file mode 100644
index 00000000..4b979b94
--- /dev/null
+++ b/pages/spamfilter/mzta-spamfilter.js
@@ -0,0 +1,207 @@
+/*
+ * ThunderAI [https://micz.it/thunderbird-addon-thunderai/]
+ * Copyright (C) 2024 - 2025 Mic (m@micz.it)
+
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+import { prefs_default } from '../../options/mzta-options-default.js';
+import { taLogger } from '../../js/mzta-logger.js';
+import { getSpecialPrompts, setSpecialPrompts } from "../../js/mzta-prompts.js";
+import { getPlaceholders } from "../../js/mzta-placeholders.js";
+import { textareaAutocomplete } from "../../js/mzta-placeholders-autocomplete.js";
+import { taSpamReport } from '../../js/mzta-spamreport.js';
+
+let autocompleteSuggestions = [];
+let taLog = new taLogger("mzta-spamfilter-page",true);
+taSpamReport.logger = taLog;
+
+document.addEventListener('DOMContentLoaded', async () => {
+
+ i18n.updateDocument();
+ await restoreOptions();
+
+ document.querySelectorAll(".option-input").forEach(element => {
+ element.addEventListener("change", saveOptions);
+ });
+
+ document.getElementById("spamfilter_threshold").addEventListener("input", check_spamfilter_threshold);
+ check_spamfilter_threshold({target: document.getElementById("spamfilter_threshold")});
+
+ let spamfilter_textarea = document.getElementById('spamfilter_prompt_text');
+ let spamfilter_save_btn = document.getElementById('btn_save_prompt');
+ let spamfilter_reset_btn = document.getElementById('btn_reset_prompt');
+
+ let specialPrompts = await getSpecialPrompts();
+ let spamfilter_prompt = specialPrompts.find(prompt => prompt.id === 'prompt_spamfilter');
+
+ spamfilter_textarea.addEventListener('input', (event) => {
+ spamfilter_reset_btn.disabled = (event.target.value === browser.i18n.getMessage('prompt_spamfilter_full_text'));
+ spamfilter_save_btn.disabled = (event.target.value === spamfilter_prompt.text);
+ if(spamfilter_save_btn.disabled){
+ document.getElementById('spamfilter_prompt_unsaved').classList.add('hidden');
+ } else {
+ document.getElementById('spamfilter_prompt_unsaved').classList.remove('hidden');
+ }
+ });
+
+ spamfilter_reset_btn.addEventListener('click', () => {
+ spamfilter_textarea.value = browser.i18n.getMessage('prompt_spamfilter_full_text');
+ spamfilter_reset_btn.disabled = true;
+ let event = new Event('input', { bubbles: true, cancelable: true });
+ spamfilter_textarea.dispatchEvent(event);
+ });
+
+ spamfilter_save_btn.addEventListener('click', () => {
+ specialPrompts.find(prompt => prompt.id === 'prompt_spamfilter').text = spamfilter_textarea.value;
+ setSpecialPrompts(specialPrompts);
+ spamfilter_save_btn.disabled = true;
+ document.getElementById('spamfilter_prompt_unsaved').classList.add('hidden');
+ browser.runtime.sendMessage({command: "reload_menus"});
+ });
+
+ if(spamfilter_prompt.text === 'prompt_spamfilter_full_text'){
+ spamfilter_prompt.text = browser.i18n.getMessage(spamfilter_prompt.text);
+ }
+ spamfilter_textarea.value = spamfilter_prompt.text;
+ spamfilter_reset_btn.disabled = (spamfilter_textarea.value === browser.i18n.getMessage('prompt_spamfilter_full_text'));
+
+ autocompleteSuggestions = (await getPlaceholders(true)).filter(p => !(p.id === 'additional_text')).map(p => ({command: '{%'+p.id+'%}', type: p.type}));
+ textareaAutocomplete(spamfilter_textarea, autocompleteSuggestions, 1); // type_value = 1, only when reading an email
+
+ loadSpamReport()
+});
+
+function check_spamfilter_threshold(event) {
+ let spamfilter_threshold_too_low = document.getElementById("spamfilter_threshold_too_low");
+ if(event.target.value < 50){
+ spamfilter_threshold_too_low.style.display = "inline";
+ if(event.target.value == 0){
+ spamfilter_threshold_too_low.textContent = browser.i18n.getMessage('spamfilter_threshold_zero');
+ spamfilter_threshold_too_low.style.fontSize = "1.5em";
+ }else{
+ spamfilter_threshold_too_low.textContent = browser.i18n.getMessage('spamfilter_threshold_too_low');
+ spamfilter_threshold_too_low.style.fontSize = "1.1em";
+ }
+ }else{
+ spamfilter_threshold_too_low.style.display = "none";
+ }
+}
+
+async function loadSpamReport(){
+ let report_data = await taSpamReport.getAllReportData();
+ console.log(">>>>>>>>>>>> loadSpamReport: " + JSON.stringify(report_data));
+ //document.getElementById("report_data").textContent = JSON.stringify(report_data, null, 2);
+ if(report_data == undefined){
+ document.getElementById("report_data").innerText = browser.i18n.getMessage("spamfilter_no_reports");
+ }else{
+ populateTable(report_data);
+ }
+}
+
+ // Function to populate the table
+ function populateTable(data) {
+ const tableBody = document.getElementById("report_data_body");
+ tableBody.innerHTML = ""; // Clear table before inserting new data
+
+ Object.keys(data).forEach(email => {
+ const report = data[email];
+
+ // Create a new row
+ const row = document.createElement("tr");
+
+ row.innerHTML = `
+ ${report.headerMessageId} |
+ ${new Date(report.message_date).toLocaleString()} |
+ ${report.from.join(", ")} |
+ ${report.subject.join(", ")} |
+ ${report.spamValue} |
+ ${report.moved?browser.i18n.getMessage("spamfilter_moved"):browser.i18n.getMessage("spamfilter_not_moved")} (${report.SpamThreshold}) |
+ ${report.explanation} |
+ ${new Date(report.report_date).toLocaleString()} |
+ `;
+
+ // Append the row to the table
+ tableBody.appendChild(row);
+ });
+}
+
+
+// Methods to manage options, derived from: /options/mzta-options.js
+function saveOptions(e) {
+ e.preventDefault();
+ let options = {};
+ let element = e.target;
+
+ switch (element.type) {
+ case 'checkbox':
+ options[element.id] = element.checked;
+ break;
+ case 'number':
+ options[element.id] = element.valueAsNumber;
+ break;
+ case 'text':
+ case 'password':
+ options[element.id] = element.value.trim();
+ break;
+ default:
+ if (element.tagName === 'SELECT') {
+ options[element.id] = element.value;
+ }else{
+ console.error("[ThunderAI] Unhandled input type:", element.type);
+ }
+ }
+
+ browser.storage.sync.set(options);
+}
+
+async function restoreOptions() {
+ function setCurrentChoice(result) {
+ document.querySelectorAll(".option-input").forEach(element => {
+ taLog.log("Options restoring " + element.id + " = " + (element.id=="chatgpt_api_key" || element.id=="openai_comp_api_key" ? "****************" : result[element.id]));
+ switch (element.type) {
+ case 'checkbox':
+ element.checked = result[element.id] || false;
+ break;
+ case 'number':
+ let default_number_value = 0;
+ if(element.id == 'chatgpt_win_height') default_number_value = prefs_default.chatgpt_win_height;
+ if(element.id == 'chatgpt_win_width') default_number_value = prefs_default.chatgpt_win_width;
+ element.value = result[element.id] ?? default_number_value;
+ break;
+ case 'text':
+ case 'password':
+ let default_text_value = '';
+ if(element.id == 'default_chatgpt_lang') default_text_value = prefs_default.default_chatgpt_lang;
+ element.value = result[element.id] || default_text_value;
+ break;
+ default:
+ if (element.tagName === 'SELECT') {
+ let default_select_value = '';
+ if(element.id == 'reply_type') default_select_value = 'reply_all';
+ if(element.id == 'connection_type') default_select_value = 'chatgpt_web';
+ element.value = result[element.id] || default_select_value;
+ if (element.value === '') {
+ element.selectedIndex = -1;
+ }
+ }else{
+ console.error("[ThunderAI] Unhandled input type:", element.type);
+ }
+ }
+ });
+ }
+
+ let getting = await browser.storage.sync.get(prefs_default);
+ setCurrentChoice(getting);
+}
\ No newline at end of file