aboutsummaryrefslogtreecommitdiffstats
path: root/mod
diff options
context:
space:
mode:
Diffstat (limited to 'mod')
-rw-r--r--mod/notification-matrix.rsc43
-rw-r--r--mod/notification-telegram.rsc36
-rw-r--r--mod/ssh-keys-import.rsc12
3 files changed, 49 insertions, 42 deletions
diff --git a/mod/notification-matrix.rsc b/mod/notification-matrix.rsc
index 3e3a33e..196633a 100644
--- a/mod/notification-matrix.rsc
+++ b/mod/notification-matrix.rsc
@@ -76,6 +76,7 @@
:global EitherOr;
:global FetchUserAgentStr;
:global LogPrint;
+ :global ProtocolStrip;
:global SymbolForNotification;
:local PrepareText do={
@@ -86,21 +87,15 @@
}
:local Return "";
- :local Chars {
- "plain"={ "\\"; "\""; "\n" };
- "format"={ "\\"; "\""; "\n"; "&"; "<"; ">" };
- }
- :local Subs {
- "plain"={ "\\\\"; "\\\""; "\\n" };
- "format"={ "\\\\"; "&quot;"; "<br/>"; "&amp;"; "&lt;"; "&gt;" };
- }
+ :local Chars { "\""; "\n"; "&"; "<"; ">" };
+ :local Subs { "&quot;"; "<br/>"; "&amp;"; "&lt;"; "&gt;" };
:for I from=0 to=([ :len $Input ] - 1) do={
:local Char [ :pick $Input $I ];
- :local Replace [ :find ($Chars->$2) $Char ];
+ :local Replace [ :find $Chars $Char ];
:if ([ :typeof $Replace ] = "num") do={
- :set Char ($Subs->$2->$Replace);
+ :set Char ($Subs->$Replace);
}
:set Return ($Return . $Char);
}
@@ -117,17 +112,18 @@
}
:local Headers ({ [ $FetchUserAgentStr ($Notification->"origin") ] });
- :local Plain [ $PrepareText ("## [" . $IdentityExtra . $Identity . "] " . \
- ($Notification->"subject") . "\n```\n" . ($Notification->"message") . "\n```") "plain" ];
+ :local Plain ("## [" . $IdentityExtra . $Identity . "] " . \
+ ($Notification->"subject") . "\n```\n" . ($Notification->"message") . "\n```");
:local Formatted ("<h2>" . [ $PrepareText ("[" . $IdentityExtra . $Identity . "] " . \
- ($Notification->"subject")) "format" ] . "</h2>" . "<pre><code>" . \
- [ $PrepareText ($Notification->"message") "format" ] . "</code></pre>");
+ ($Notification->"subject")) ] . "</h2>" . "<pre><code>" . \
+ [ $PrepareText ($Notification->"message") ] . "</code></pre>");
:if ([ :len ($Notification->"link") ] > 0) do={
- :set Plain ($Plain . "\\n" . [ $SymbolForNotification "link" ] . \
- [ $PrepareText ("[" . $Notification->"link" . "](" . $Notification->"link" . ")") "plain" ]);
+ :local Label [ $ProtocolStrip ($Notification->"link") ];
+ :set Plain ($Plain . "\n" . [ $SymbolForNotification "link" ] . \
+ "[" . $Label . "](" . $Notification->"link" . ")");
:set Formatted ($Formatted . "<br/>" . [ $SymbolForNotification "link" ] . \
- "<a href=\\\"" . [ $PrepareText ($Notification->"link") "format" ] . "\\\">" . \
- [ $PrepareText ($Notification->"link") "format" ] . "</a>");
+ "<a href=\"" . [ $PrepareText ($Notification->"link") ] . "\">" . \
+ [ $PrepareText $Label ] . "</a>");
}
:do {
@@ -143,11 +139,12 @@
: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);
+ :local Symbol [ $SymbolForNotification "alarm-clock" ];
+ :local DateTime ([ /system/clock/get date ] . " " . [ /system/clock/get time ]);
+ :set Plain ($Plain . "\n" . $Symbol . "This message was queued since *" . \
+ $DateTime . "* and may be obsolete.");
+ :set Formatted ($Formatted . "<br/>" . $Symbol . "This message was queued since <em>" . \
+ $DateTime . "</em> and may be obsolete.");
:set ($MatrixQueue->[ :len $MatrixQueue ]) { headers=$Headers; \
accesstoken=$AccessToken; homeserver=$HomeServer; room=$Room; \
plain=$Plain; formatted=$Formatted };
diff --git a/mod/notification-telegram.rsc b/mod/notification-telegram.rsc
index c78a8ad..9a628ce 100644
--- a/mod/notification-telegram.rsc
+++ b/mod/notification-telegram.rsc
@@ -76,28 +76,34 @@
:global EitherOr;
:global IfThenElse;
:global LogPrint;
+ :global ProtocolStrip;
:global SymbolForNotification;
:global UrlEncode;
:local EscapeMD do={
+ :local Text [ :tostr $1 ];
+ :local Mode [ :tostr $2 ];
+ :local Excl [ :tostr $3 ];
+
:global CharacterReplace;
:global IfThenElse;
- :local Return $1;
:local Chars {
- "body"={ "\\"; "`" };
+ "body"={ "\\"; "`" };
"plain"={ "_"; "*"; "["; "]"; "("; ")"; "~"; "`"; ">";
"#"; "+"; "-"; "="; "|"; "{"; "}"; "."; "!" };
}
- :foreach Char in=($Chars->$2) do={
- :set Return [ $CharacterReplace $Return $Char ("\\" . $Char) ];
+ :foreach Char in=($Chars->$Mode) do={
+ :if ([ :typeof [ :find $Excl $Char ] ] = "nil") do={
+ :set Text [ $CharacterReplace $Text $Char ("\\" . $Char) ];
+ }
}
- :if ($2 = "body") do={
- :return ("```\n" . $Return . "\n```");
+ :if ($Mode = "body") do={
+ :return ("```\n" . $Text . "\n```");
}
- :return $Return;
+ :return $Text;
}
:local ChatId [ $EitherOr ($Notification->"chatid") \
@@ -117,7 +123,7 @@
($Notification->"subject")) "plain" ] . "__*\n\n");
:local LenSubject [ :len $Text ];
:local LenMessage [ :len ($Notification->"message") ];
- :local LenLink [ :len ($Notification->"link") ];
+ :local LenLink ([ :len ($Notification->"link") ] * 2);
:local LenSum ($LenSubject + $LenMessage + $LenLink);
:if ($LenSum > 3968) do={
:set Text ($Text . [ $EscapeMD ([ :pick ($Notification->"message") 0 (3840 - $LenSubject - $LenLink) ] . "...") "body" ]);
@@ -126,12 +132,14 @@
:set Text ($Text . [ $EscapeMD ($Notification->"message") "body" ]);
}
:if ($LenLink > 0) do={
- :set Text ($Text . "\n" . [ $SymbolForNotification "link" ] . [ $EscapeMD ($Notification->"link") "plain" ]);
+ :set Text ($Text . "\n" . [ $SymbolForNotification "link" ] . \
+ "[" . [ $EscapeMD [ $ProtocolStrip ($Notification->"link") ] "plain" ] . "]" . \
+ "(" . [ $EscapeMD ($Notification->"link") "plain" ] . ")");
}
:if ($Truncated = true) do={
:set Text ($Text . "\n" . [ $SymbolForNotification "scissors" ] . \
- [ $EscapeMD ("The message was too long and has been truncated, cut off " . \
- (($LenSum - [ :len $Text ]) * 100 / $LenSum) . "%!") "plain" ]);
+ [ $EscapeMD ("The message was too long and has been truncated, cut off _" . \
+ (($LenSum - [ :len $Text ]) * 100 / $LenSum) . "%_!") "plain" "_" ]);
}
:do {
@@ -146,14 +154,14 @@
"&parse_mode=MarkdownV2&text=" . [ $UrlEncode $Text ]) as-value ]->"data");
:set ($TelegramMessageIDs->[ :tostr ([ :deserialize from=json value=$Data ]->"result"->"message_id") ]) 1;
} on-error={
- $LogPrint info $0 ("Failed sending telegram notification! Queuing...");
+ $LogPrint info $0 ("Failed sending Telegram notification! Queuing...");
:if ([ :typeof $TelegramQueue ] = "nothing") do={
:set TelegramQueue ({});
}
:set Text ($Text . "\n" . [ $SymbolForNotification "alarm-clock" ] . \
- [ $EscapeMD ("This message was queued since " . [ /system/clock/get date ] . \
- " " . [ /system/clock/get time ] . " and may be obsolete.") "plain" ]);
+ [ $EscapeMD ("This message was queued since _" . [ /system/clock/get date ] . \
+ " " . [ /system/clock/get time ] . "_ and may be obsolete.") "plain" "_" ]);
:set ($TelegramQueue->[ :len $TelegramQueue ]) { chatid=$ChatId; tokenid=$TokenId;
text=$Text; silent=($Notification->"silent"); replyto=($Notification->"replyto") };
:if ([ :len [ /system/scheduler/find where name="_FlushTelegramQueue" ] ] = 0) do={
diff --git a/mod/ssh-keys-import.rsc b/mod/ssh-keys-import.rsc
index f37c28b..6272a93 100644
--- a/mod/ssh-keys-import.rsc
+++ b/mod/ssh-keys-import.rsc
@@ -38,11 +38,6 @@
:return false;
}
- :if ([ $MkDir "tmpfs/ssh-keys-import" ] = false) do={
- $LogPrint warning $0 ("Creating directory 'tmpfs/ssh-keys-import' failed!");
- :return false;
- }
-
:local FingerPrintMD5 [ :convert from=base64 transform=md5 to=hex ($KeyVal->1) ];
:if ([ :len [ /user/ssh-keys/find where user=$User key-owner~("\\bmd5=" . $FingerPrintMD5 . "\\b") ] ] > 0) do={
@@ -51,6 +46,11 @@
:return false;
}
+ :if ([ $MkDir "tmpfs/ssh-keys-import" ] = false) do={
+ $LogPrint warning $0 ("Creating directory 'tmpfs/ssh-keys-import' failed!");
+ :return false;
+ }
+
:local FileName ("tmpfs/ssh-keys-import/key-" . [ $GetRandom20CharAlNum 6 ] . ".pub");
/file/add name=$FileName contents=($Key . ", md5=" . $FingerPrintMD5);
$WaitForFile $FileName;
@@ -59,8 +59,10 @@
/user/ssh-keys/import public-key-file=$FileName user=$User;
$LogPrint info $0 ("Imported ssh public key (" . $KeyVal->2 . ", " . $KeyVal->0 . ", " . \
"MD5:" . $FingerPrintMD5 . ") for user '" . $User . "'.");
+ /file/remove "tmpfs/ssh-keys-import";
} on-error={
$LogPrint warning $0 ("Failed importing key.");
+ /file/remove "tmpfs/ssh-keys-import";
:return false;
}
}