munin/source/targets/email.ts

219 lines
4.8 KiB
TypeScript
Raw Normal View History

2025-05-06 21:07:38 +02:00
/*
This file is part of »munin«.
Copyright 2025 'Fenris Wolf' <fenris@folksprak.org>
»munin« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»munin« 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with »munin«. If not, see <http://www.gnu.org/licenses/>.
*/
namespace _munin.targets.email
{
/**
*/
export type type_parameters = {
smtp_host : string;
smtp_port : int;
smtp_username : string;
smtp_password : string;
sender : string;
receivers : Array<string>;
hide_tags : boolean;
reminders : Array<_munin.type_reminder>;
language : string;
2025-07-19 12:00:38 +02:00
strings : {
notification_head : string;
};
2025-05-06 21:07:38 +02:00
};
/**
*/
function get_translation(
parameters : type_parameters,
path : string
) : string
{
return lib_plankton.translate.get_new(
path,
{
"preferred_language": parameters.language,
}
);
}
2025-05-06 21:07:38 +02:00
/**
*/
function summarize_event(
2025-05-06 21:07:38 +02:00
parameters : type_parameters,
event : _munin.type_event
) : string
{
return lib_plankton.string.coin(
"[{{head}}] {{date}} : {{macro_tags}}{{title}}",
{
"head": lib_plankton.string.coin(
"{{event}}-{{reminder}}",
{
"event": get_translation(parameters, "core.event.event").toLowerCase(),
"reminder": get_translation(parameters, "core.reminder.reminder").toLowerCase(),
}
),
"date": lib_plankton.pit.date_format(
event.begin.date
),
"macro_tags": (
(event.tags === null)
?
""
:
(event.tags.map(tag => ("{" + tag + "}")).join(" ") + " ")
),
"title": event.title,
}
);
}
/**
*/
function render_event(
parameters : type_parameters,
event : _munin.type_event
) : string
{
return lib_plankton.string.coin(
"{{title_label}} | {{macro_tags}}{{title_value}}\n{{time_label}} | {{time_value}}{{macro_location}}{{macro_description}}",
{
"title_label": get_translation(parameters, "core.event.title.short").toUpperCase(),
"macro_tags": (
(parameters.hide_tags || (event.tags === null))
?
""
:
(event.tags.map(tag => ("{" + tag + "}")).join(" ") + " ")
),
"title_value": event.title,
"time_label": get_translation(parameters, "core.event.time.short").toUpperCase(),
"time_value": lib_plankton.pit.timespan_format(
event.begin,
event.end,
{
"adjust_to_ce": true,
}
),
"macro_location": (
(event.location === null)
?
""
:
lib_plankton.string.coin(
"\n{{location_label}} | {{location_value}}",
{
"location_label": get_translation(parameters, "core.event.location.short").toUpperCase(),
"location_value": event.location,
}
)
),
"macro_description": (
(event.description === null)
?
""
:
lib_plankton.string.coin(
"\n\n{{description_value}}",
{
"description_value": event.description,
}
)
),
}
);
}
/**
*/
async function send(
parameters : type_parameters,
events : Array<_munin.type_event>
2025-05-06 21:07:38 +02:00
) : Promise<void>
{
await lib_plankton.email.send(
{
"host": parameters.smtp_host,
"port": parameters.smtp_port,
"username": parameters.smtp_username,
"password": parameters.smtp_password,
},
parameters.sender,
parameters.receivers,
(
(events.length === 1)
?
summarize_event(parameters, events[0])
:
lib_plankton.string.coin(
2025-07-19 12:00:38 +02:00
parameters.strings.notification_head,
{
2025-07-19 12:00:38 +02:00
"core": lib_plankton.string.coin(
"{{event}}-{{reminder}}",
{
"event": get_translation(parameters, "core.event.event").toLowerCase(),
"reminder": get_translation(parameters, "core.reminder.reminder").toLowerCase(),
}
),
2025-07-19 12:00:38 +02:00
"extra": lib_plankton.string.coin(
"{{count}} {{events}}",
{
"count": events.length.toFixed(0),
"events": get_translation(parameters, "core.event.events"),
}
),
}
)
2025-05-06 21:07:38 +02:00
),
(
events
.map(event => render_event(parameters, event))
.join("\n\n--------\n\n")
2025-05-06 21:07:38 +02:00
)
);
}
/**
*/
export function implementation_target(
parameters : type_parameters
) : _munin.type_target
{
return {
"reminders": parameters.reminders,
"show": () => lib_plankton.string.coin(
"email:{{receivers}}",
{
"receivers": parameters.receivers.join(","),
}
),
"send": (events) => send(parameters, events),
2025-05-06 21:07:38 +02:00
};
}
}