Skip to content

Commit

Permalink
Send bridge alert if connection fails
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jan 28, 2024
1 parent 1523d72 commit 59b1c27
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
14 changes: 9 additions & 5 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,16 @@ var cmdDeleteSession = &commands.FullHandler{
}

func fnDeleteSession(ce *WrappedCommandEvent) {
if !ce.User.IsLoggedIn() {
ce.Reply("You're not logged in")
return
}
wasLoggedIn := ce.User.IsLoggedIn()
hadCookies := ce.User.Cookies != nil || ce.User.MetaID != 0
ce.User.DeleteSession()
ce.Reply("Disconnected and deleted session")
if wasLoggedIn {
ce.Reply("Disconnected and deleted session")
} else if hadCookies {
ce.Reply("Wasn't connected, but deleted session")
} else {
ce.Reply("You weren't logged in, but deleted session anyway")
}
}

var cmdPing = &commands.FullHandler{
Expand Down
1 change: 1 addition & 0 deletions config/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type BridgeConfig struct {
DeliveryReceipts bool `yaml:"delivery_receipts"`
MessageStatusEvents bool `yaml:"message_status_events"`
MessageErrorNotices bool `yaml:"message_error_notices"`
DisableBridgeAlerts bool `yaml:"disable_bridge_alerts"`
SyncDirectChatList bool `yaml:"sync_direct_chat_list"`
ResendBridgeInfo bool `yaml:"resend_bridge_info"`
CaptionInMessage bool `yaml:"caption_in_message"`
Expand Down
1 change: 1 addition & 0 deletions config/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func DoUpgrade(helper *up.Helper) {
helper.Copy(up.Bool, "bridge", "delivery_receipts")
helper.Copy(up.Bool, "bridge", "message_status_events")
helper.Copy(up.Bool, "bridge", "message_error_notices")
helper.Copy(up.Bool, "bridge", "disable_bridge_alerts")
helper.Copy(up.Bool, "bridge", "sync_direct_chat_list")
helper.Copy(up.Bool, "bridge", "resend_bridge_info")
helper.Copy(up.Bool, "bridge", "caption_in_message")
Expand Down
3 changes: 3 additions & 0 deletions example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ bridge:
message_status_events: false
# Whether the bridge should send error notices via m.notice events when a message fails to bridge.
message_error_notices: true
# Should the bridge never send alerts to the bridge management room?
# These are mostly things like the user being logged out.
disable_bridge_alerts: false
# Should the bridge update the m.direct account data event when double puppeting is enabled.
# Note that updating the m.direct event is not atomic and is therefore prone to race conditions.
sync_direct_chat_list: false
Expand Down
41 changes: 41 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"maunium.net/go/mautrix/bridge/commands"
"maunium.net/go/mautrix/bridge/status"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"

"go.mau.fi/mautrix-meta/database"
Expand Down Expand Up @@ -173,6 +174,7 @@ type User struct {

spaceMembershipChecked bool
spaceCreateLock sync.Mutex
mgmtCreateLock sync.Mutex

stopBackfillTask atomic.Pointer[context.CancelFunc]

Expand Down Expand Up @@ -289,6 +291,43 @@ func (user *User) ensureInvited(ctx context.Context, intent *appservice.IntentAP
return
}

func (user *User) sendMarkdownBridgeAlert(ctx context.Context, formatString string, args ...interface{}) {
if user.bridge.Config.Bridge.DisableBridgeAlerts {
return
}
notice := fmt.Sprintf(formatString, args...)
content := format.RenderMarkdown(notice, true, false)
_, err := user.bridge.Bot.SendMessageEvent(ctx, user.GetManagementRoom(ctx), event.EventMessage, content)
if err != nil {
user.log.Err(err).Str("notice_text", notice).Msg("Failed to send bridge alert")
}
}

func (user *User) GetManagementRoom(ctx context.Context) id.RoomID {
if len(user.ManagementRoom) == 0 {
user.mgmtCreateLock.Lock()
defer user.mgmtCreateLock.Unlock()
if len(user.ManagementRoom) > 0 {
return user.ManagementRoom
}
creationContent := make(map[string]interface{})
if !user.bridge.Config.Bridge.FederateRooms {
creationContent["m.federate"] = false
}
resp, err := user.bridge.Bot.CreateRoom(ctx, &mautrix.ReqCreateRoom{
Topic: user.bridge.ProtocolName + " bridge notices",
IsDirect: true,
CreationContent: creationContent,
})
if err != nil {
user.log.Err(err).Msg("Failed to auto-create management room")
} else {
user.SetManagementRoom(resp.RoomID)
}
}
return user.ManagementRoom
}

func (user *User) GetSpaceRoom(ctx context.Context) id.RoomID {
if !user.bridge.Config.Bridge.PersonalFilteringSpaces {
return ""
Expand Down Expand Up @@ -400,6 +439,7 @@ func (user *User) Connect() {
Error: "meta-connect-error",
Message: err.Error(),
})
go user.sendMarkdownBridgeAlert(context.TODO(), "Failed to connect to %s: %v", user.bridge.ProtocolName, err)
}
}

Expand Down Expand Up @@ -706,6 +746,7 @@ func (user *User) eventHandler(rawEvt any) {
stateEvt = status.StateBadCredentials
}
user.BridgeState.Send(status.BridgeState{StateEvent: stateEvt, Message: evt.Err.Error()})
go user.sendMarkdownBridgeAlert(context.TODO(), "Error in %s connection: %v", user.bridge.ProtocolName, evt.Err)
user.StopBackfillLoop()
default:
user.log.Warn().Type("event_type", evt).Msg("Unrecognized event type from messagix")
Expand Down

0 comments on commit 59b1c27

Please sign in to comment.