2023-03-07 09:57:42 +00:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: mod/notification-matrix
|
2024-01-01 14:25:25 +00:00
|
|
|
# Copyright (c) 2013-2024 Michael Gisbers <michael@gisbers.de>
|
2023-03-07 09:57:42 +00:00
|
|
|
# Christian Hesse <mail@eworm.de>
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
2023-04-04 17:23:23 +00:00
|
|
|
#
|
2024-03-28 21:54:23 +00:00
|
|
|
# requires RouterOS, version=7.13
|
|
|
|
#
|
2023-04-04 17:23:23 +00:00
|
|
|
# send notifications via Matrix
|
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/mod/notification-matrix.md
|
2023-03-07 09:57:42 +00:00
|
|
|
|
|
|
|
:global FlushMatrixQueue;
|
|
|
|
:global NotificationFunctions;
|
2023-08-06 14:25:52 +00:00
|
|
|
:global PurgeMatrixQueue;
|
2023-03-07 09:57:42 +00:00
|
|
|
:global SendMatrix;
|
|
|
|
:global SendMatrix2;
|
2023-11-08 12:08:49 +00:00
|
|
|
:global SetupMatrixAuthenticate;
|
|
|
|
:global SetupMatrixJoinRoom;
|
2023-03-07 09:57:42 +00:00
|
|
|
|
|
|
|
# flush Matrix queue
|
|
|
|
:set FlushMatrixQueue do={
|
|
|
|
:global MatrixQueue;
|
|
|
|
|
|
|
|
:global IsFullyConnected;
|
2024-03-08 11:45:38 +00:00
|
|
|
:global LogPrint;
|
2023-03-07 09:57:42 +00:00
|
|
|
|
|
|
|
:if ([ $IsFullyConnected ] = false) do={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint debug $0 ("System is not fully connected, not flushing.");
|
2023-03-07 09:57:42 +00:00
|
|
|
:return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
:local AllDone true;
|
|
|
|
:local QueueLen [ :len $MatrixQueue ];
|
|
|
|
|
2023-11-14 21:35:06 +00:00
|
|
|
:if ([ :len [ /system/scheduler/find where name="_FlushMatrixQueue" ] ] > 0 && $QueueLen = 0) do={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint warning $0 ("Flushing Matrix messages from scheduler, but queue is empty.");
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
:foreach Id,Message in=$MatrixQueue do={
|
|
|
|
:if ([ :typeof $Message ] = "array" ) do={
|
|
|
|
:do {
|
|
|
|
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \
|
|
|
|
("https://" . $Message->"homeserver" . "/_matrix/client/r0/rooms/" . $Message->"room" . \
|
|
|
|
"/send/m.room.message?access_token=" . $Message->"accesstoken") \
|
|
|
|
http-data=("{ \"msgtype\": \"m.text\", \"body\": \"" . $Message->"plain" . "\"," . \
|
|
|
|
"\"format\": \"org.matrix.custom.html\", \"formatted_body\": \"" . \
|
|
|
|
$Message->"formatted" . "\" }") as-value;
|
|
|
|
:set ($MatrixQueue->$Id);
|
|
|
|
} on-error={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint debug $0 ("Sending queued Matrix message failed.");
|
2023-03-07 09:57:42 +00:00
|
|
|
:set AllDone false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:if ($AllDone = true && $QueueLen = [ :len $MatrixQueue ]) do={
|
2023-11-14 21:35:06 +00:00
|
|
|
/system/scheduler/remove [ find where name="_FlushMatrixQueue" ];
|
2023-03-07 09:57:42 +00:00
|
|
|
:set MatrixQueue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# send notification via Matrix - expects one array argument
|
|
|
|
:set ($NotificationFunctions->"matrix") do={
|
|
|
|
:local Notification $1;
|
|
|
|
|
|
|
|
:global Identity;
|
|
|
|
:global IdentityExtra;
|
|
|
|
:global MatrixAccessToken;
|
|
|
|
:global MatrixAccessTokenOverride;
|
|
|
|
:global MatrixHomeServer;
|
|
|
|
:global MatrixHomeServerOverride;
|
|
|
|
:global MatrixQueue;
|
|
|
|
:global MatrixRoom;
|
|
|
|
:global MatrixRoomOverride;
|
|
|
|
|
|
|
|
:global EitherOr;
|
2024-03-08 11:45:38 +00:00
|
|
|
:global LogPrint;
|
2023-03-07 09:57:42 +00:00
|
|
|
:global SymbolForNotification;
|
|
|
|
|
|
|
|
:local PrepareText do={
|
|
|
|
:local Input [ :tostr $1 ];
|
|
|
|
|
|
|
|
:if ([ :len $Input ] = 0) do={
|
|
|
|
:return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
:local Return "";
|
|
|
|
:local Chars {
|
|
|
|
"plain"={ "\\"; "\""; "\n" };
|
|
|
|
"format"={ "\\"; "\""; "\n"; "&"; "<"; ">" };
|
|
|
|
}
|
|
|
|
:local Subs {
|
|
|
|
"plain"={ "\\\\"; "\\\""; "\\n" };
|
|
|
|
"format"={ "\\\\"; """; "<br/>"; "&"; "<"; ">" };
|
|
|
|
}
|
|
|
|
|
|
|
|
:for I from=0 to=([ :len $Input ] - 1) do={
|
|
|
|
:local Char [ :pick $Input $I ];
|
|
|
|
:local Replace [ :find ($Chars->$2) $Char ];
|
|
|
|
|
|
|
|
:if ([ :typeof $Replace ] = "num") do={
|
|
|
|
:set Char ($Subs->$2->$Replace);
|
|
|
|
}
|
|
|
|
:set Return ($Return . $Char);
|
|
|
|
}
|
|
|
|
|
|
|
|
:return $Return;
|
|
|
|
}
|
|
|
|
|
|
|
|
:local AccessToken [ $EitherOr ($MatrixAccessTokenOverride->($Notification->"origin")) $MatrixAccessToken ];
|
|
|
|
:local HomeServer [ $EitherOr ($MatrixHomeServerOverride->($Notification->"origin")) $MatrixHomeServer ];
|
|
|
|
:local Room [ $EitherOr ($MatrixRoomOverride->($Notification->"origin")) $MatrixRoom ];
|
|
|
|
|
|
|
|
:if ([ :len $AccessToken ] = 0 || [ :len $HomeServer ] = 0 || [ :len $Room ] = 0) do={
|
|
|
|
:return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
:local Plain [ $PrepareText ("## [" . $IdentityExtra . $Identity . "] " . \
|
|
|
|
($Notification->"subject") . "\n```\n" . ($Notification->"message") . "\n```") "plain" ];
|
|
|
|
:local Formatted ("<h2>" . [ $PrepareText ("[" . $IdentityExtra . $Identity . "] " . \
|
|
|
|
($Notification->"subject")) "format" ] . "</h2>" . "<pre><code>" . \
|
|
|
|
[ $PrepareText ($Notification->"message") "format" ] . "</code></pre>");
|
|
|
|
:if ([ :len ($Notification->"link") ] > 0) do={
|
|
|
|
:set Plain ($Plain . "\\n" . [ $SymbolForNotification "link" ] . \
|
|
|
|
[ $PrepareText ("[" . $Notification->"link" . "](" . $Notification->"link" . ")") "plain" ]);
|
|
|
|
:set Formatted ($Formatted . "<br/>" . [ $SymbolForNotification "link" ] . \
|
|
|
|
"<a href=\\\"" . [ $PrepareText ($Notification->"link") "format" ] . "\\\">" . \
|
|
|
|
[ $PrepareText ($Notification->"link") "format" ] . "</a>");
|
|
|
|
}
|
|
|
|
|
|
|
|
:do {
|
|
|
|
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \
|
|
|
|
("https://" . $HomeServer . "/_matrix/client/r0/rooms/" . $Room . \
|
|
|
|
"/send/m.room.message?access_token=" . $AccessToken) \
|
|
|
|
http-data=("{ \"msgtype\": \"m.text\", \"body\": \"" . $Plain . "\"," . \
|
|
|
|
"\"format\": \"org.matrix.custom.html\", \"formatted_body\": \"" . \
|
|
|
|
$Formatted . "\" }") as-value;
|
|
|
|
} on-error={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint info $0 ("Failed sending Matrix notification! Queuing...");
|
2023-03-07 09:57:42 +00:00
|
|
|
|
|
|
|
:if ([ :typeof $MatrixQueue ] = "nothing") do={
|
|
|
|
:set MatrixQueue ({});
|
|
|
|
}
|
|
|
|
:local Text ([ $SymbolForNotification "alarm-clock" ] . \
|
|
|
|
"This message was queued since " . [ /system/clock/get date ] . \
|
|
|
|
" " . [ /system/clock/get time ] . " and may be obsolete.");
|
|
|
|
:set Plain ($Plain . "\\n" . $Text);
|
|
|
|
:set Formatted ($Formatted . "<br/>" . $Text);
|
|
|
|
:set ($MatrixQueue->[ :len $MatrixQueue ]) { room=$Room; \
|
|
|
|
accesstoken=$AccessToken; homeserver=$HomeServer; \
|
|
|
|
plain=$Plain; formatted=$Formatted };
|
2023-11-14 21:35:06 +00:00
|
|
|
:if ([ :len [ /system/scheduler/find where name="_FlushMatrixQueue" ] ] = 0) do={
|
|
|
|
/system/scheduler/add name="_FlushMatrixQueue" interval=1m start-time=startup \
|
2023-03-07 09:57:42 +00:00
|
|
|
on-event=(":global FlushMatrixQueue; \$FlushMatrixQueue;");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 14:25:52 +00:00
|
|
|
# purge the Matrix queue
|
|
|
|
:set PurgeMatrixQueue do={
|
|
|
|
:global MatrixQueue;
|
|
|
|
|
2023-11-14 21:35:06 +00:00
|
|
|
/system/scheduler/remove [ find where name="_FlushMatrixQueue" ];
|
2023-08-06 14:25:52 +00:00
|
|
|
:set MatrixQueue;
|
|
|
|
}
|
|
|
|
|
2023-03-07 09:57:42 +00:00
|
|
|
# send notification via Matrix - expects at least two string arguments
|
|
|
|
:set SendMatrix do={
|
|
|
|
:global SendMatrix2;
|
|
|
|
|
2024-04-09 21:58:29 +00:00
|
|
|
$SendMatrix2 ({ origin=$0; subject=$1; message=$2; link=$3 });
|
2023-03-07 09:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# send notification via Matrix - expects one array argument
|
|
|
|
:set SendMatrix2 do={
|
|
|
|
:local Notification $1;
|
|
|
|
|
|
|
|
:global NotificationFunctions;
|
|
|
|
|
|
|
|
($NotificationFunctions->"matrix") ("\$NotificationFunctions->\"matrix\"") $Notification;
|
|
|
|
}
|
2023-11-08 12:08:49 +00:00
|
|
|
|
|
|
|
# setup - get home server and access token
|
|
|
|
:set SetupMatrixAuthenticate do={
|
|
|
|
:local User [ :tostr $1 ];
|
|
|
|
:local Pass [ :tostr $2 ];
|
|
|
|
|
2024-03-08 11:45:38 +00:00
|
|
|
:global LogPrint;
|
2023-11-08 12:08:49 +00:00
|
|
|
|
|
|
|
:global MatrixAccessToken;
|
|
|
|
:global MatrixHomeServer;
|
|
|
|
|
|
|
|
:local Domain [ :pick $User ([ :find $User ":" ] + 1) [ :len $User] ];
|
|
|
|
:do {
|
|
|
|
:local Data ([ /tool/fetch check-certificate=yes-without-crl output=user \
|
|
|
|
("https://" . $Domain . "/.well-known/matrix/client") as-value ]->"data");
|
2024-03-28 21:54:23 +00:00
|
|
|
:set MatrixHomeServer ([ :deserialize from=json value=$Data ]->"m.homeserver"->"base_url");
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint debug $0 ("Home server is: " . $MatrixHomeServer);
|
2023-11-08 12:08:49 +00:00
|
|
|
} on-error={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint error $0 ("Failed getting home server!");
|
2024-03-08 16:53:48 +00:00
|
|
|
:return false;
|
2023-11-08 12:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
:if ([ :pick $MatrixHomeServer 0 8 ] = "https://") do={
|
|
|
|
:set MatrixHomeServer [ :pick $MatrixHomeServer 8 [ :len $MatrixHomeServer ] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
:do {
|
|
|
|
:local Data ([ /tool/fetch check-certificate=yes-without-crl output=user \
|
|
|
|
http-method=post http-data=("{\"type\":\"m.login.password\", \"user\":\"" . $User . "\", \"password\":\"" . $Pass . "\"}") \
|
|
|
|
("https://" . $MatrixHomeServer . "/_matrix/client/r0/login") as-value ]->"data");
|
2024-03-28 21:54:23 +00:00
|
|
|
:set MatrixAccessToken ([ :deserialize from=json value=$Data ]->"access_token");
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint debug $0 ("Access token is: " . $MatrixAccessToken);
|
2023-11-08 12:08:49 +00:00
|
|
|
} on-error={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint error $0 ("Failed logging in (and getting access token)!");
|
2024-03-08 16:53:48 +00:00
|
|
|
:return false;
|
2023-11-08 12:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
:do {
|
2024-04-01 21:40:09 +00:00
|
|
|
/system/script/remove [ find where name="global-config-overlay.d/mod/notification-matrix" ];
|
|
|
|
/system/script/add name="global-config-overlay.d/mod/notification-matrix" source=( \
|
|
|
|
"# configuration snippet: mod/notification-matrix\n\n" . \
|
2023-11-08 12:08:49 +00:00
|
|
|
":global MatrixHomeServer \"" . $MatrixHomeServer . "\";\n" . \
|
|
|
|
":global MatrixAccessToken \"" . $MatrixAccessToken . "\";\n");
|
2024-04-01 21:40:09 +00:00
|
|
|
$LogPrint info $0 ("Added configuration snippet. Now create and join a room, please!");
|
2023-11-08 12:08:49 +00:00
|
|
|
} on-error={
|
2024-04-01 21:40:09 +00:00
|
|
|
$LogPrint error $0 ("Failed adding configuration snippet!");
|
2024-03-08 16:53:48 +00:00
|
|
|
:return false;
|
2023-11-08 12:08:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# setup - join a room
|
|
|
|
:set SetupMatrixJoinRoom do={
|
|
|
|
:global MatrixRoom [ :tostr $1 ];
|
|
|
|
|
2024-03-08 11:45:38 +00:00
|
|
|
:global LogPrint;
|
2023-11-08 12:08:49 +00:00
|
|
|
:global UrlEncode;
|
|
|
|
|
|
|
|
:global MatrixAccessToken;
|
|
|
|
:global MatrixHomeServer;
|
|
|
|
:global MatrixRoom;
|
|
|
|
|
|
|
|
:do {
|
|
|
|
/tool/fetch check-certificate=yes-without-crl output=none \
|
|
|
|
http-method=post http-data="" \
|
|
|
|
("https://" . $MatrixHomeServer . "/_matrix/client/r0/rooms/" . [ $UrlEncode $MatrixRoom ] . \
|
|
|
|
"/join?access_token=" . [ $UrlEncode $MatrixAccessToken ]) as-value;
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint debug $0 ("Joined the room.");
|
2023-11-08 12:08:49 +00:00
|
|
|
} on-error={
|
2024-03-08 11:45:38 +00:00
|
|
|
$LogPrint error $0 ("Failed joining the room!");
|
2024-03-08 16:53:48 +00:00
|
|
|
:return false;
|
2023-11-08 12:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
:do {
|
2024-04-01 21:40:09 +00:00
|
|
|
:local Snippet [ /system/script/find where name="global-config-overlay.d/mod/notification-matrix" ];
|
|
|
|
/system/script/set $Snippet source=([ get $Snippet source ] . \
|
2023-11-08 12:08:49 +00:00
|
|
|
":global MatrixRoom \"" . $MatrixRoom . "\";\n");
|
2024-04-01 21:40:09 +00:00
|
|
|
$LogPrint info $0 ("Appended configuration to configuration snippet. Please review!");
|
2023-11-08 12:08:49 +00:00
|
|
|
} on-error={
|
2024-04-01 21:40:09 +00:00
|
|
|
$LogPrint error $0 ("Failed appending configuration to snippet!");
|
2024-03-08 16:53:48 +00:00
|
|
|
:return false;
|
2023-11-08 12:08:49 +00:00
|
|
|
}
|
|
|
|
}
|