Skip to content

Commit

Permalink
Merge branch 'logout-notice'
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFerr committed Nov 23, 2023
2 parents 1c128d4 + a2d74d9 commit cf26587
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ COPY pkg/libsignalgo/* pkg/libsignalgo/
COPY pkg/libsignalgo/resources/. pkg/libsignalgo/resources/.
COPY config/. config/.
COPY database/. database/.
COPY event/. event/.
COPY .git .git
COPY Makefile .
COPY docker-run.sh .
Expand Down
23 changes: 23 additions & 0 deletions event/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package event

import (
"reflect"

"maunium.net/go/mautrix/event"
)

var (
StateBridgeConnection = event.Type{Type: "im.vector.mautrix-signalgo.connection", Class: event.AccountDataEventType}
)

type BridgeConnectionEventContent struct {
LoggedIn bool `json:"logged_in"`
}

func registerEventTypes() error {
event.TypeMap[StateBridgeConnection] = reflect.TypeOf(BridgeConnectionEventContent{})

return nil
}

var _ = registerEventTypes()
51 changes: 51 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/vector-im/mautrix-signal/database"
bridgeEvent "github.com/vector-im/mautrix-signal/event"
"github.com/vector-im/mautrix-signal/pkg/signalmeow"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/appservice"
"maunium.net/go/mautrix/bridge"
"maunium.net/go/mautrix/bridge/bridgeconfig"
"maunium.net/go/mautrix/bridge/status"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
)

Expand Down Expand Up @@ -71,10 +73,16 @@ func (user *User) SetManagementRoom(roomID id.RoomID) {

existing, ok := user.bridge.managementRooms[roomID]
if ok {
if existing.ManagementRoom != "" {
existing.clearBridgeConnectionState(existing.ManagementRoom)
}
existing.ManagementRoom = ""
existing.Update()
}

if user.ManagementRoom != "" {
user.clearBridgeConnectionState(user.ManagementRoom)
}
user.ManagementRoom = roomID
user.bridge.managementRooms[user.ManagementRoom] = user
err := user.Update()
Expand Down Expand Up @@ -331,6 +339,9 @@ func (user *User) startupTryConnect(retryCount int) {
case signalmeow.SignalConnectionEventConnected:
user.log.Debug().Msg("Sending Connected BridgeState")
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
if managementRoomID := user.GetManagementRoomID(); managementRoomID != "" {
user.setBridgeConnectionState(managementRoomID, true)
}

case signalmeow.SignalConnectionEventDisconnected:
user.log.Debug().Msg("Received SignalConnectionEventDisconnected")
Expand Down Expand Up @@ -391,6 +402,22 @@ func (user *User) startupTryConnect(retryCount int) {
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateBadCredentials, Message: err.Error()})
}
user.clearKeysAndDisconnect()
if managementRoomID := user.GetManagementRoomID(); managementRoomID != "" {
var existingBridgeConnection bridgeEvent.BridgeConnectionEventContent
if err := user.bridge.Bot.GetRoomAccountData(
managementRoomID, bridgeEvent.StateBridgeConnection.Type, &existingBridgeConnection,
); err != nil || existingBridgeConnection.LoggedIn {
if err != nil && !errors.Is(err, mautrix.MNotFound) {
user.log.Warn().Err(err).Msg("Failed to get bridge connection state from management room account data")
}
user.bridge.Bot.SendMessageEvent(
managementRoomID,
event.EventMessage,
format.RenderMarkdown("You have been logged out of Signal. To reconnect, use the `login` command.", true, false),
)
user.setBridgeConnectionState(managementRoomID, false)
}
}

case signalmeow.SignalConnectionEventError:
user.log.Debug().Msg("Sending UnknownError BridgeState")
Expand All @@ -408,6 +435,30 @@ func (user *User) startupTryConnect(retryCount int) {
}()
}

func (user *User) setBridgeConnectionState(roomId id.RoomID, loggedIn bool) {
if err := user.bridge.Bot.SetRoomAccountData(
roomId,
bridgeEvent.StateBridgeConnection.Type,
bridgeEvent.BridgeConnectionEventContent{LoggedIn: loggedIn},
); err != nil {
user.log.Warn().Err(err).Msg(
"Failed to update bridge connection state for management room account data",
)
}
}

func (user *User) clearBridgeConnectionState(roomId id.RoomID) {
if err := user.bridge.Bot.SetRoomAccountData(
roomId,
bridgeEvent.StateBridgeConnection.Type,
nil,
); err != nil {
user.log.Warn().Err(err).Msg(
"Failed to clear bridge connection state from management room account data",
)
}
}

func (user *User) clearKeysAndDisconnect() {
// We need to clear out keys associated with the Signal device that no longer has valid credentials
user.log.Debug().Msg("Clearing out Signal device keys")
Expand Down

0 comments on commit cf26587

Please sign in to comment.