mod/notification-matrix: use custom user agent string

This commit is contained in:
Christian Hesse 2024-04-10 11:51:47 +02:00
parent fb865ba114
commit 9dfa11eac5

View file

@ -39,7 +39,8 @@
:foreach Id,Message in=$MatrixQueue do={ :foreach Id,Message in=$MatrixQueue do={
:if ([ :typeof $Message ] = "array" ) do={ :if ([ :typeof $Message ] = "array" ) do={
:do { :do {
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \ /tool/fetch check-certificate=yes-without-crl output=none \
http-header-field=($Message->"headers") http-method=post \
("https://" . $Message->"homeserver" . "/_matrix/client/r0/rooms/" . $Message->"room" . \ ("https://" . $Message->"homeserver" . "/_matrix/client/r0/rooms/" . $Message->"room" . \
"/send/m.room.message?access_token=" . $Message->"accesstoken") \ "/send/m.room.message?access_token=" . $Message->"accesstoken") \
http-data=("{ \"msgtype\": \"m.text\", \"body\": \"" . $Message->"plain" . "\"," . \ http-data=("{ \"msgtype\": \"m.text\", \"body\": \"" . $Message->"plain" . "\"," . \
@ -74,6 +75,7 @@
:global MatrixRoomOverride; :global MatrixRoomOverride;
:global EitherOr; :global EitherOr;
:global FetchUserAgentStr;
:global LogPrint; :global LogPrint;
:global SymbolForNotification; :global SymbolForNotification;
@ -115,6 +117,7 @@
:return false; :return false;
} }
:local Headers ({ [ $FetchUserAgentStr ($Notification->"origin") ] });
:local Plain [ $PrepareText ("## [" . $IdentityExtra . $Identity . "] " . \ :local Plain [ $PrepareText ("## [" . $IdentityExtra . $Identity . "] " . \
($Notification->"subject") . "\n```\n" . ($Notification->"message") . "\n```") "plain" ]; ($Notification->"subject") . "\n```\n" . ($Notification->"message") . "\n```") "plain" ];
:local Formatted ("<h2>" . [ $PrepareText ("[" . $IdentityExtra . $Identity . "] " . \ :local Formatted ("<h2>" . [ $PrepareText ("[" . $IdentityExtra . $Identity . "] " . \
@ -129,7 +132,8 @@
} }
:do { :do {
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \ /tool/fetch check-certificate=yes-without-crl output=none \
http-header-field=$Headers http-method=post \
("https://" . $HomeServer . "/_matrix/client/r0/rooms/" . $Room . \ ("https://" . $HomeServer . "/_matrix/client/r0/rooms/" . $Room . \
"/send/m.room.message?access_token=" . $AccessToken) \ "/send/m.room.message?access_token=" . $AccessToken) \
http-data=("{ \"msgtype\": \"m.text\", \"body\": \"" . $Plain . "\"," . \ http-data=("{ \"msgtype\": \"m.text\", \"body\": \"" . $Plain . "\"," . \
@ -146,9 +150,9 @@
" " . [ /system/clock/get time ] . " and may be obsolete."); " " . [ /system/clock/get time ] . " and may be obsolete.");
:set Plain ($Plain . "\\n" . $Text); :set Plain ($Plain . "\\n" . $Text);
:set Formatted ($Formatted . "<br/>" . $Text); :set Formatted ($Formatted . "<br/>" . $Text);
:set ($MatrixQueue->[ :len $MatrixQueue ]) { room=$Room; \ :set ($MatrixQueue->[ :len $MatrixQueue ]) { headers=$Headers; \
accesstoken=$AccessToken; homeserver=$HomeServer; \ accesstoken=$AccessToken; homeserver=$HomeServer; room=$Room; \
plain=$Plain; formatted=$Formatted }; plain=$Plain; formatted=$Formatted };
:if ([ :len [ /system/scheduler/find where name="_FlushMatrixQueue" ] ] = 0) do={ :if ([ :len [ /system/scheduler/find where name="_FlushMatrixQueue" ] ] = 0) do={
/system/scheduler/add name="_FlushMatrixQueue" interval=1m start-time=startup \ /system/scheduler/add name="_FlushMatrixQueue" interval=1m start-time=startup \
on-event=(":global FlushMatrixQueue; \$FlushMatrixQueue;"); on-event=(":global FlushMatrixQueue; \$FlushMatrixQueue;");
@ -185,6 +189,7 @@
:local User [ :tostr $1 ]; :local User [ :tostr $1 ];
:local Pass [ :tostr $2 ]; :local Pass [ :tostr $2 ];
:global FetchUserAgentStr;
:global LogPrint; :global LogPrint;
:global MatrixAccessToken; :global MatrixAccessToken;
@ -193,6 +198,7 @@
:local Domain [ :pick $User ([ :find $User ":" ] + 1) [ :len $User] ]; :local Domain [ :pick $User ([ :find $User ":" ] + 1) [ :len $User] ];
:do { :do {
:local Data ([ /tool/fetch check-certificate=yes-without-crl output=user \ :local Data ([ /tool/fetch check-certificate=yes-without-crl output=user \
http-header-field=({ [ $FetchUserAgentStr $0 ] }) \
("https://" . $Domain . "/.well-known/matrix/client") as-value ]->"data"); ("https://" . $Domain . "/.well-known/matrix/client") as-value ]->"data");
:set MatrixHomeServer ([ :deserialize from=json value=$Data ]->"m.homeserver"->"base_url"); :set MatrixHomeServer ([ :deserialize from=json value=$Data ]->"m.homeserver"->"base_url");
$LogPrint debug $0 ("Home server is: " . $MatrixHomeServer); $LogPrint debug $0 ("Home server is: " . $MatrixHomeServer);
@ -207,7 +213,8 @@
:do { :do {
:local Data ([ /tool/fetch check-certificate=yes-without-crl output=user \ :local Data ([ /tool/fetch check-certificate=yes-without-crl output=user \
http-method=post http-data=("{\"type\":\"m.login.password\", \"user\":\"" . $User . "\", \"password\":\"" . $Pass . "\"}") \ http-header-field=({ [ $FetchUserAgentStr $0 ] }) http-method=post \
http-data=("{\"type\":\"m.login.password\", \"user\":\"" . $User . "\", \"password\":\"" . $Pass . "\"}") \
("https://" . $MatrixHomeServer . "/_matrix/client/r0/login") as-value ]->"data"); ("https://" . $MatrixHomeServer . "/_matrix/client/r0/login") as-value ]->"data");
:set MatrixAccessToken ([ :deserialize from=json value=$Data ]->"access_token"); :set MatrixAccessToken ([ :deserialize from=json value=$Data ]->"access_token");
$LogPrint debug $0 ("Access token is: " . $MatrixAccessToken); $LogPrint debug $0 ("Access token is: " . $MatrixAccessToken);
@ -233,6 +240,7 @@
:set SetupMatrixJoinRoom do={ :set SetupMatrixJoinRoom do={
:global MatrixRoom [ :tostr $1 ]; :global MatrixRoom [ :tostr $1 ];
:global FetchUserAgentStr;
:global LogPrint; :global LogPrint;
:global UrlEncode; :global UrlEncode;
@ -242,7 +250,7 @@
:do { :do {
/tool/fetch check-certificate=yes-without-crl output=none \ /tool/fetch check-certificate=yes-without-crl output=none \
http-method=post http-data="" \ http-header-field=({ [ $FetchUserAgentStr $0 ] }) http-method=post http-data="" \
("https://" . $MatrixHomeServer . "/_matrix/client/r0/rooms/" . [ $UrlEncode $MatrixRoom ] . \ ("https://" . $MatrixHomeServer . "/_matrix/client/r0/rooms/" . [ $UrlEncode $MatrixRoom ] . \
"/join?access_token=" . [ $UrlEncode $MatrixAccessToken ]) as-value; "/join?access_token=" . [ $UrlEncode $MatrixAccessToken ]) as-value;
$LogPrint debug $0 ("Joined the room."); $LogPrint debug $0 ("Joined the room.");