diff --git a/bot.ps1 b/bot.ps1 index acda622..8ce802a 100644 --- a/bot.ps1 +++ b/bot.ps1 @@ -11,6 +11,7 @@ param ( [securestring] $AccessToken ) +$ErrorActionPreference = 'Stop' $account_data_type_prefix = 'de.lubiland.pingposh.' $account_data_types = @{ @@ -180,7 +181,7 @@ function Join-Pong { FormattedBody = $formatted_body } } -function Open-Event { +function Open-JoinEvent { param ( [Parameter(Mandatory=$true)] $Event, @@ -201,6 +202,31 @@ function Open-Event { Send-Pong -RoomId $RoomId @bodies -OriginHomeServer $origin_homeserver -Duration $difference.TotalMilliseconds -PingEventId $Event.event_id } } +function Join-MatrixRoom { + param ( + [Parameter(Mandatory=$true)] + $RoomId + ) + + $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 + if($response.room_id) { + Write-Host ('Joined room {0}' -f $RoomId) + } else { + Write-Error ('Error joining room {0}: {1}' -f $RoomId,$response.error) + } +} function Get-MatrixAccountData { param ( [Parameter(Mandatory=$true)] @@ -298,7 +324,16 @@ function Invoke-MatrixSync { foreach($room_id in $room_ids) { $events = $response.rooms.join.$room_id.timeline.events foreach($event in $events) { - Open-Event -Event $event -RoomId $room_id + Open-JoinEvent -Event $event -RoomId $room_id + } + } + + #.PSObject.Properties because the rooms under .invite are [NoteProperty] + $room_ids = $response.rooms.invite.PSObject.Properties.Name + foreach($room_id in $room_ids) { + $invites = $response.rooms.invite.$room_id.invite_state.events | Where-Object {$_.content.join_rule -eq 'invite'} + foreach($invite in $invites) { + Join-MatrixRoom -RoomId $room_id } } @@ -332,12 +367,13 @@ function Get-MatrixFilterId { function Get-FilterId { $filter = @{ event_fields = @( - #all fields used in Open-Event + #all fields used in Open-JoinEvent 'event_id' 'sender' 'origin_server_ts' 'content.msgtype' 'content.body' + #'content.membership' #invites only ) presence = @{ #we don't use presence information @@ -353,8 +389,11 @@ function Get-FilterId { types = @('invalid') } state = @{ - #we don't use room state - types = @('invalid') + types = @( + #to receive invites + 'm.room.member' + ) + lazy_load_members = $true } timeline = @{ #exclude ourself