Merge pull request #576 from micz/mail_headers_issue527

Mail headers with dynamic placeholders
This commit is contained in:
Mic 2025-12-26 12:10:38 +01:00 committed by GitHub
commit d34597ff4d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 78 additions and 6 deletions

View file

@ -619,6 +619,10 @@
"message": "Folder path",
"description": ""
},
"placeholder_mail_headers": {
"message": "Mail headers",
"description": ""
},
"placeholder_selected_text": {
"message": "Selected text",
"description": ""

View file

@ -17,6 +17,7 @@
*/
import { prefs_default } from '../options/mzta-options-default.js';
import { getMailHeader } from './mzta-utils.js';
/* ================= PLACEHOLDERS PROPERTIES ========================================
@ -35,6 +36,10 @@ import { prefs_default } from '../options/mzta-options-default.js';
0: Custom placeholder
1: Default placeholder (not editable, cannot be deleted)
is_dynamic attribute:
0: it's a fixed placeholder
1: it's a dynamic placehoder (it means that it will have a : and then a value, like {%my_placeholder:test_value%})
================ USER PROPERTIES
enabled attribute:
0: Disabled
@ -53,6 +58,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -61,6 +67,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -69,6 +76,7 @@ const defaultPlaceholders = [
default_value: "",
type: 2,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -77,6 +85,7 @@ const defaultPlaceholders = [
default_value: "",
type: 2,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -85,6 +94,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -93,6 +103,7 @@ const defaultPlaceholders = [
default_value: "",
type: 1,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -101,6 +112,16 @@ const defaultPlaceholders = [
default_value: "",
type: 1,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
id: 'mail_headers',
name: "__MSG_placeholder_mail_headers__",
default_value: "",
type: 1,
is_default: "1",
is_dynamic: "1",
enabled: 1,
},
{
@ -109,6 +130,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -117,6 +139,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -125,6 +148,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -133,6 +157,7 @@ const defaultPlaceholders = [
default_value: "0",
type: 1,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -141,6 +166,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -149,6 +175,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -157,6 +184,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -165,6 +193,7 @@ const defaultPlaceholders = [
default_value: "",
type: 1,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -173,6 +202,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -181,6 +211,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -189,6 +220,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -197,6 +229,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -205,6 +238,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -213,6 +247,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -221,6 +256,7 @@ const defaultPlaceholders = [
default_value: "",
type: 0,
is_default: "1",
is_dynamic: "0",
enabled: 1,
},
{
@ -229,6 +265,7 @@ const defaultPlaceholders = [
default_value: "",
type: 1,
is_default: "1",
is_dynamic: "0",
enabled: 1,
}
];
@ -269,6 +306,7 @@ export async function setCustomPlaceholders(placeholders) {
placeholders.forEach(ph => {
ph.id = placeholdersUtils.validateCustomDataPH_ID(ph.id);
ph.is_default = "0";
ph.is_dynamic = "0";
});
await browser.storage.local.set({_custom_placeholder: placeholders});
}
@ -330,10 +368,21 @@ export const placeholdersUtils = {
// Use exec to find all matches
while ((match = regex.exec(text)) !== null) {
const foundPH = activePHs.find(ph => ph.id === match[1].trim());
if (foundPH) {
matches.push(foundPH);
}
console.log(">>>>>>>>>> extractPlaceholders match: " + JSON.stringify(match));
const foundPH = activePHs.find(ph => ph.id === match[1].trim() || (ph.is_dynamic == 1 && match[1].startsWith(ph.id + ':')));
if (foundPH) {
if (foundPH.is_dynamic == 1 && match[1].includes(':')) {
const [id, custom_value] = match[1].split(':', 2);
const dynamicPH = { ...foundPH }; // Create a copy to avoid modifying the original
dynamicPH.id = id.trim();
dynamicPH.custom_value = custom_value.trim();
matches.push(dynamicPH);
console.log(">>>>>>>>>> extractPlaceholders dynamicPH: " + JSON.stringify(dynamicPH));
} else {
matches.push(foundPH);
console.log(">>>>>>>>>> extractPlaceholders foundPH: " + JSON.stringify(foundPH));
}
}
}
return matches;
@ -346,13 +395,17 @@ export const placeholdersUtils = {
use_default_value = false,
skip_additional_text = false
} = args || {};
console.log(">>>>>>>>>> replacePlaceholders replacements: " + JSON.stringify(replacements));
// Regular expression to match patterns like {%...%}
return text.replace(/{%\s*(.*?)\s*%}/g, function(match, p1) {
console.log(">>>>>>>>>> replacePlaceholders match: " + JSON.stringify(match));
console.log(">>>>>>>>>> replacePlaceholders p1: " + JSON.stringify(p1));
// p1 contains the key inside {% %}
if (skip_additional_text && (p1 === 'additional_text')) {
return match;
}
const currPlaceholder = defaultPlaceholders.find(ph => ph.id === p1);
const currPlaceholder = defaultPlaceholders.find(ph => (ph.id === p1) || (ph.is_dynamic == 1 && p1.startsWith(ph.id + ':')));
console.log(">>>>>>>>>> replacePlaceholders currPlaceholder: " + JSON.stringify(currPlaceholder));
if (!currPlaceholder) {
return match;
}
@ -424,6 +477,7 @@ export const placeholdersUtils = {
// console.log(">>>>>>>>>> curr_message: " + JSON.stringify(curr_message));
let finalSubs = {};
for(let currPH of currPHs){
console.log(">>>>>>>>>> currPH: " + JSON.stringify(currPH));
switch(currPH.id){
case 'mail_text_body':
finalSubs['mail_text_body'] = placeholdersUtils.failSafePlaceholders(body_text);
@ -446,6 +500,9 @@ export const placeholdersUtils = {
case 'mail_folder_path':
finalSubs['mail_folder_path'] = placeholdersUtils.failSafePlaceholders(curr_message.folder?.path);
break;
case 'mail_headers':
finalSubs['mail_headers:' + currPH.custom_value] = placeholdersUtils.failSafePlaceholders(await getMailHeader(curr_message, currPH.custom_value));
break;
case 'selected_text':
finalSubs['selected_text'] = placeholdersUtils.failSafePlaceholders(selection_text);
break;
@ -506,7 +563,7 @@ export const placeholdersUtils = {
break;
}
}
console.log(">>>>>>>>>> finalSubs: " + JSON.stringify(finalSubs));
return finalSubs;
},

View file

@ -189,6 +189,17 @@ export async function replaceBody(tabId, replyHtml) {
await messenger.compose.setComposeDetails(tabId, {body: fullBody});
}
export async function getMailHeader(curr_message, mail_header_id) {
let mail_header_value = "";
let full_message = await browser.messages.getFull(curr_message.id);
console.log(">>>>>>>>>>>> getMailHeader full_message: " + JSON.stringify(full_message));
if(full_message.hasOwnProperty("headers") && Object.keys(full_message.headers).some(header => header.toLowerCase() === mail_header_id.toLowerCase())){
mail_header_value = full_message.headers[Object.keys(full_message.headers).find(header => header.toLowerCase() === mail_header_id.toLowerCase())];
}
console.log(">>>>>>>>>>>> getMailHeader mail_header_value: " + mail_header_value)
return mail_header_value;
}
export function sanitizeHtml(input) {
// Keep only <br> tags and remove all other HTML tags
return input.replace(/<(?!br\s*\/?)[^>]+>/gi, '');