diff --git a/bot.ps1 b/bot.ps1 index dc7c823..af435f4 100755 --- a/bot.ps1 +++ b/bot.ps1 @@ -14,10 +14,26 @@ param ( [ValidateNotNullOrEmpty()] [securestring] $AccessToken = ((Get-Content -Path $env:ACCESSTOKEN_FILE -Raw).Trim() ` - | ConvertTo-SecureString -AsPlainText -Force) + | ConvertTo-SecureString -AsPlainText -Force), + + [switch] + $Unencrypted = $false #is alternatively set with $env:UNENCRYPTED ) $ErrorActionPreference = 'Stop' + +if($env:UNENCRYPTED -eq 'TRUE') { + $Unencrypted = $true +} + +#used in Invoke-RestMethod +$authentication_headers = @{ + Authentication = 'Bearer' + Token = $AccessToken + ContentType = 'application/json' + AllowUnencryptedAuthentication = $Unencrypted +} + $account_data_type_prefix = 'de.lubiland.pingposh.' $account_data_types = @{ next_batch = $account_data_type_prefix+'next_batch' @@ -44,18 +60,13 @@ function Send-MatrixEvent { $uri = '{0}_matrix/client/r0/rooms/{1}/send/{2}/{3}' -f $HomeServer,$RoomId,$EventType,$txn_id - $header_splat = @{ - Authentication = 'Bearer' - Token = $AccessToken - ContentType = 'application/json' - } $http_splat = @{ Uri = $uri Method = 'Put' Body = $Event } - $response = Invoke-RestMethod @header_splat @http_splat + $response = Invoke-RestMethod @authentication_headers @http_splat if($response.event_id) { Write-Host ('Event {0} sent to room {1}' -f $response.event_id,$RoomId) } @@ -215,17 +226,12 @@ function Join-MatrixRoom { $uri = '{0}_matrix/client/r0/rooms/{1}/join' -f $HomeServer,$RoomId - $header_splat = @{ - Authentication = 'Bearer' - Token = $AccessToken - ContentType = 'application/json' - } $http_splat = @{ Uri = $uri Method = 'Post' } - $response = Invoke-RestMethod @header_splat @http_splat + $response = Invoke-RestMethod @authentication_headers @http_splat if($response.room_id) { Write-Host ('Joined room {0}' -f $RoomId) } else { @@ -241,17 +247,12 @@ function Get-MatrixAccountData { $uri = '{0}_matrix/client/r0/user/{1}/account_data/{2}' -f $HomeServer,$UserId,$Type - $header_splat = @{ - Authentication = 'Bearer' - Token = $AccessToken - ContentType = 'application/json' - } $http_splat = @{ Uri = $uri Method = 'Get' } - $response = Invoke-RestMethod @header_splat @http_splat + $response = Invoke-RestMethod @authentication_headers @http_splat return $response } function Set-MatrixAccountData { @@ -267,18 +268,13 @@ function Set-MatrixAccountData { $uri = '{0}_matrix/client/r0/user/{1}/account_data/{2}' -f $HomeServer,$UserId,$Type - $header_splat = @{ - Authentication = 'Bearer' - Token = $AccessToken - ContentType = 'application/json' - } $http_splat = @{ Uri = $uri Method = 'Put' Body = $Content | ConvertTo-Json -Compress } - Invoke-RestMethod @header_splat @http_splat | Out-Null + Invoke-RestMethod @authentication_headers @http_splat | Out-Null } function Get-NextBatchToken { $account_data = Get-MatrixAccountData -Type $account_data_types.next_batch @@ -313,16 +309,11 @@ function Invoke-MatrixSync { $uri += '&filter={0}' -f $FilterId } - $header_splat = @{ - Authentication = 'Bearer' - Token = $AccessToken - ContentType = 'application/json' - } $http_splat = @{ Uri = $uri } - $response = Invoke-RestMethod @header_splat @http_splat + $response = Invoke-RestMethod @authentication_headers @http_splat #.PSObject.Properties because the rooms under .join are [NoteProperty] $room_ids = $response.rooms.join.PSObject.Properties.Name @@ -352,11 +343,6 @@ function Get-MatrixFilterId { $uri = '{0}_matrix/client/r0/user/{1}/filter' -f $HomeServer,$UserId - $header_splat = @{ - Authentication = 'Bearer' - Token = $AccessToken - ContentType = 'application/json' - } $http_splat = @{ Uri = $uri Method = 'Post' @@ -365,7 +351,7 @@ function Get-MatrixFilterId { Body = $Filter | ConvertTo-Json -Compress -Depth 3 } - $response = Invoke-RestMethod @header_splat @http_splat + $response = Invoke-RestMethod @authentication_headers @http_splat return $response.filter_id }