|
|
|
@ -26,6 +26,11 @@ if($env:UNENCRYPTED -eq 'TRUE') {
|
|
|
|
|
$Unencrypted = $true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#$join_time contains rooms we joined during runtime
|
|
|
|
|
#it is used to ignore events sent before we joined
|
|
|
|
|
$join_time = @{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#used in Invoke-RestMethod
|
|
|
|
|
$authentication_headers = @{
|
|
|
|
|
Authentication = 'Bearer'
|
|
|
|
@ -119,20 +124,28 @@ function Send-Pong {
|
|
|
|
|
|
|
|
|
|
Send-MatrixEvent -RoomId $RoomId -Event $event_json -EventType 'm.room.message'
|
|
|
|
|
}
|
|
|
|
|
function Compare-Timestamps {
|
|
|
|
|
function ConvertFrom-MatrixTimestamp {
|
|
|
|
|
param (
|
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
|
[Int64]
|
|
|
|
|
$OriginServerTs
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$current_time = (Get-Date).ToUniversalTime()
|
|
|
|
|
|
|
|
|
|
#$OriginServerTs is milliseconds since 1970-01-01 (epoch time in milliseconds)
|
|
|
|
|
$original_time = Get-Date -Date ((Get-Date -Date '1970-01-01') + [timespan]::FromMilliseconds($OriginServerTs))
|
|
|
|
|
return Get-Date -Date ((Get-Date -Date '1970-01-01') + [timespan]::FromMilliseconds($OriginServerTs))
|
|
|
|
|
}
|
|
|
|
|
function Measure-TimeDifference {
|
|
|
|
|
param (
|
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
|
[datetime]
|
|
|
|
|
$OriginTime,
|
|
|
|
|
|
|
|
|
|
[datetime]
|
|
|
|
|
$ReferenceTime = (Get-Date).ToUniversalTime()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
#return the difference
|
|
|
|
|
Write-Output ($current_time - $original_time)
|
|
|
|
|
Write-Output ($ReferenceTime - $OriginTime)
|
|
|
|
|
}
|
|
|
|
|
function ConvertTo-HumanReadableTimespan {
|
|
|
|
|
param (
|
|
|
|
@ -206,9 +219,23 @@ function Open-JoinEvent {
|
|
|
|
|
$RoomId
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
$origin_time = ConvertFrom-MatrixTimestamp -OriginServerTs $Event.origin_server_ts
|
|
|
|
|
|
|
|
|
|
#check if a $join_time for $RoomId exists and if it newer than our event
|
|
|
|
|
if($join_time.$RoomId) {
|
|
|
|
|
if((Measure-TimeDifference -OriginTime $origin_time -ReferenceTime $join_time.$RoomId) -gt 0) {
|
|
|
|
|
#ignore events sent before we joined the room
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
#first event with negative difference received
|
|
|
|
|
#probably all future events are after our join time
|
|
|
|
|
$join_time.Remove($RoomId)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#the "ball" is a string returned by the bot
|
|
|
|
|
if($Event.content.msgtype -eq 'm.text' -and $Event.content.body -match '^!ping( (?<ball>.*))?') {
|
|
|
|
|
$difference = Compare-Timestamps -OriginServerTs $Event.origin_server_ts
|
|
|
|
|
$difference = Measure-TimeDifference -OriginTime $origin_time
|
|
|
|
|
$readable_timespan = ConvertTo-HumanReadableTimespan -TimeSpan $difference
|
|
|
|
|
|
|
|
|
|
#$bodies contains a hashtable with keys Body and FormattedBody
|
|
|
|
@ -328,6 +355,7 @@ function Invoke-MatrixSync {
|
|
|
|
|
$room_ids = $response.rooms.invite.PSObject.Properties.Name
|
|
|
|
|
foreach($room_id in $room_ids) {
|
|
|
|
|
Join-MatrixRoom -RoomId $room_id
|
|
|
|
|
$join_time.$room_id = (Get-Date).ToUniversalTime()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $response.next_batch
|
|
|
|
|