munin/source/conf.ts

512 lines
8.7 KiB
TypeScript
Raw Normal View History

2025-04-25 12:50:13 +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.conf
2025-04-25 00:48:05 +02:00
{
/**
*/
type type_reminder_raw = {
frequency : (
"hourly"
|
"daily"
|
"weekly"
|
"monthly"
);
offset : int;
from : int;
to : int;
};
2025-04-25 00:48:05 +02:00
/**
2025-07-01 19:19:47 +02:00
* @todo rename to "type_conf"
*/
type type_conf_v6 = {
sources : Array<
(
{
kind : "ical_feed";
data : {
url : string;
filtration : {
category_whitelist : (null | Array<string>);
category_blacklist : (null | Array<string>);
title_whitelist : (null | Array<string>);
title_blacklist : (null | Array<string>);
}
}
}
)
>;
targets : Array<
(
{
kind : "email";
data : {
smtp_host : string;
smtp_port : int;
smtp_username : string;
smtp_password : string;
sender : string;
receivers : Array<string>;
hide_tags : boolean;
reminders : Array<type_reminder_raw>;
}
}
|
{
kind : "telegram_bot";
data : {
bot_token : string;
chat_id : int;
hide_tags : boolean;
reminders : Array<type_reminder_raw>;
}
}
)
>;
settings : {
interval : float;
};
};
/**
2025-07-01 19:19:47 +02:00
*/
const current_version : string = "6";
/**
2025-07-01 19:19:47 +02:00
* @todo remove
*/
2025-07-01 19:19:47 +02:00
export type type_conf = type_conf_v6;
/**
*/
function schema_source_ical_feed(
) : lib_plankton.conf.type_schema
{
return {
"type": "object",
"properties": {
"kind": {
"nullable": false,
"type": "string",
"enum": ["ical_feed"]
},
"data": {
2025-04-25 00:48:05 +02:00
"nullable": false,
"type": "object",
"properties": {
"url": {
"nullable": false,
"type": "string"
},
"filtration": {
2025-04-25 00:48:05 +02:00
"nullable": false,
2025-04-25 12:50:13 +02:00
"type": "object",
"properties": {
"category_whitelist": {
"nullable": true,
"type": "array",
"items": {
"nullable": false,
"type": "string",
},
"default": null,
},
"category_blacklist": {
"nullable": true,
"type": "array",
"items": {
"nullable": false,
"type": "string",
},
"default": null,
},
"title_whitelist": {
"nullable": true,
"type": "array",
"items": {
"nullable": false,
"type": "string",
},
"default": null,
2025-04-25 12:50:13 +02:00
},
"title_blacklist": {
"nullable": true,
"type": "array",
"items": {
"nullable": false,
"type": "string",
2025-04-25 12:50:13 +02:00
},
"default": null,
},
2025-04-25 12:50:13 +02:00
},
"additionalProperties": false,
"required": [
],
"default": {}
},
},
"additionalProperties": false,
"required": [
"url",
]
2025-04-25 00:48:05 +02:00
}
},
"additionalProperties": false,
"required": [
"kind",
"data",
]
};
}
/**
*/
function schema_sources(
) : lib_plankton.conf.type_schema
{
2025-07-01 19:19:47 +02:00
return {
"nullable": false,
"type": "array",
"items": {
"nullable": false,
"anyOf": [
schema_source_ical_feed(),
],
}
2025-07-01 19:19:47 +02:00
};
}
/**
*/
function schema_reminder(
) : lib_plankton.conf.type_schema
{
2025-07-01 19:19:47 +02:00
return {
"nullable": false,
"type": "object",
"properties": {
"frequency": {
"nullable": false,
2025-07-01 19:19:47 +02:00
"type": "string",
"enum": [
"hourly",
"daily",
"weekly",
"monthly",
]
2025-07-01 19:19:47 +02:00
},
"offset": {
"nullable": false,
"type": "integer",
"default": 0
},
"from": {
"nullable": false,
"type": "integer"
},
"to": {
"nullable": false,
"type": "integer"
},
},
"additionalProperties": false,
"required": [
"frequency",
"from",
"to",
]
};
}
/**
*/
function default_reminder(
) : any
{
2025-07-01 19:19:47 +02:00
return [
{
2025-07-01 19:19:47 +02:00
"frequency": "hourly",
"from": 24,
"to": 25
}
2025-07-01 19:19:47 +02:00
];
}
2025-05-06 21:07:38 +02:00
/**
*/
function schema_target_email(
) : lib_plankton.conf.type_schema
{
return {
"nullable": false,
"type": "object",
"properties": {
"kind": {
"nullable": false,
"type": "string",
"enum": ["email"]
},
"data": {
"nullable": false,
"type": "object",
"properties": {
"smtp_host": {
"nullable": false,
"type": "string",
},
"smtp_port": {
"nullable": false,
"type": "integer",
},
"smtp_username": {
"nullable": false,
"type": "string",
},
"smtp_password": {
"nullable": false,
"type": "string",
},
"sender": {
"nullable": false,
"type": "string",
"default": "munin"
},
"receivers": {
"nullable": false,
"type": "array",
"items": {
"nullable": false,
"type": "string",
}
},
"hide_tags": {
"nullable": false,
"type": "boolean",
"default": false,
},
2025-05-06 21:07:38 +02:00
"reminders": {
"nullable": false,
"type": "array",
2025-07-01 19:19:47 +02:00
"items": schema_reminder(),
"default": default_reminder(),
2025-05-06 21:07:38 +02:00
},
"language": {
"nullable": false,
"type": "string",
"default": "deu"
},
2025-05-06 21:07:38 +02:00
},
"additionalProperties": false,
"required": [
"smtp_host",
"smtp_port",
"smtp_username",
"smtp_password",
"receivers",
]
}
},
"additionalProperties": false,
"required": [
"kind",
"data",
]
};
}
/**
*/
function schema_target_telegram_bot(
) : lib_plankton.conf.type_schema
{
return {
"nullable": false,
"type": "object",
"properties": {
"kind": {
"nullable": false,
"type": "string",
"enum": ["telegram_bot"]
2025-04-25 12:50:13 +02:00
},
"data": {
"nullable": false,
"type": "object",
"properties": {
"bot_token": {
"nullable": false,
"type": "string",
},
"chat_id": {
"nullable": false,
"type": "integer",
},
"hide_tags": {
"nullable": false,
"type": "boolean",
"default": false,
},
"reminders": {
"nullable": false,
"type": "array",
2025-07-01 19:19:47 +02:00
"items": schema_reminder(),
"default": default_reminder(),
},
"language": {
"nullable": false,
"type": "string",
"default": "deu"
},
},
"additionalProperties": false,
"required": [
"bot_token",
"chat_id",
]
}
},
"additionalProperties": false,
"required": [
"kind",
"data",
]
};
}
/**
*/
function schema_targets(
) : lib_plankton.conf.type_schema
{
return {
"nullable": false,
"type": "array",
"items": {
"nullable": false,
2025-07-01 19:19:47 +02:00
"anyOf": [
schema_target_email(),
schema_target_telegram_bot(),
]
}
};
}
/**
*/
function schema_settings(
) : lib_plankton.conf.type_schema
{
return {
"nullable": false,
"type": "object",
"properties": {
"interval": {
"nullable": false,
"type": "number",
"default": 1.0
}
2025-04-25 12:50:13 +02:00
},
"additionalProperties": false,
"required": [
],
"default": {
}
};
}
/**
*/
2025-07-01 19:19:47 +02:00
export function schema(
) : lib_plankton.conf.type_schema
{
return {
"nullable": false,
"type": "object",
"properties": {
2025-07-01 19:19:47 +02:00
"sources": schema_sources(),
"targets": schema_targets(),
"settings": schema_settings(),
},
"additionalProperties": false,
"required": [
2025-07-01 19:19:47 +02:00
"sources",
"targets",
],
};
}
/**
*/
2025-07-01 19:19:47 +02:00
export function schema_extended(
) : lib_plankton.conf.type_schema
{
2025-07-01 19:19:47 +02:00
return {
"type": "object",
"properties": {
"version": {
"nullable": false,
2025-07-01 19:19:47 +02:00
"type": "string",
"enum": [current_version],
},
"content": _munin.conf.schema(),
}
}
}
/**
*/
export async function load(
path : string
) : Promise<type_conf>
{
2025-07-01 19:19:47 +02:00
const conf_raw : {version : string; content : any} = await lib_plankton.conf.load(
schema(),
path
);
2025-07-01 19:19:47 +02:00
if (conf_raw.version !== current_version)
{
throw (new Error("conf expected in version '" + current_version + "'"));
}
else
{
return (conf_raw.content as type_conf_v6);
}
}
2025-04-25 00:48:05 +02:00
}