2024-01-02 13:32:11 +00:00
|
|
|
// Copyright (c) 2024 Proton AG
|
2022-10-13 08:58:11 +00:00
|
|
|
//
|
|
|
|
// This file is part of Proton Mail Bridge.
|
|
|
|
//
|
|
|
|
// Proton Mail Bridge is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Proton Mail Bridge is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-09-28 09:29:33 +00:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-01-17 16:23:01 +00:00
|
|
|
"github.com/ProtonMail/gluon/reporter"
|
2023-02-27 12:00:32 +00:00
|
|
|
"github.com/ProtonMail/proton-bridge/v3/internal"
|
2022-11-23 14:25:41 +00:00
|
|
|
"github.com/ProtonMail/proton-bridge/v3/internal/events"
|
|
|
|
"github.com/ProtonMail/proton-bridge/v3/internal/safe"
|
|
|
|
"github.com/ProtonMail/proton-bridge/v3/internal/user"
|
2023-01-17 16:23:01 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-09-28 09:29:33 +00:00
|
|
|
)
|
|
|
|
|
2023-07-28 12:59:12 +00:00
|
|
|
func (bridge *Bridge) handleUserEvent(ctx context.Context, user *user.User, event events.Event) {
|
2022-09-28 09:29:33 +00:00
|
|
|
switch event := event.(type) {
|
|
|
|
case events.UserDeauth:
|
2022-11-10 22:28:08 +00:00
|
|
|
bridge.handleUserDeauth(ctx, user)
|
2023-01-17 14:11:13 +00:00
|
|
|
|
|
|
|
case events.UserBadEvent:
|
2023-03-02 13:10:23 +00:00
|
|
|
bridge.handleUserBadEvent(ctx, user, event)
|
2023-02-22 12:30:45 +00:00
|
|
|
|
|
|
|
case events.UncategorizedEventError:
|
|
|
|
bridge.handleUncategorizedErrorEvent(event)
|
2022-09-28 09:29:33 +00:00
|
|
|
}
|
2022-11-10 22:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bridge *Bridge) handleUserDeauth(ctx context.Context, user *user.User) {
|
|
|
|
safe.Lock(func() {
|
2023-06-28 13:26:41 +00:00
|
|
|
bridge.logoutUser(ctx, user, false, false, false)
|
|
|
|
user.ReportConfigStatusFailure("User deauth.")
|
2022-11-10 22:28:08 +00:00
|
|
|
}, bridge.usersLock)
|
|
|
|
}
|
2023-01-17 14:11:13 +00:00
|
|
|
|
2023-07-28 12:59:12 +00:00
|
|
|
func (bridge *Bridge) handleUserBadEvent(ctx context.Context, user *user.User, event events.UserBadEvent) {
|
2023-10-11 10:11:15 +00:00
|
|
|
safe.RLock(func() {
|
2023-03-07 12:48:34 +00:00
|
|
|
if rerr := bridge.reporter.ReportMessageWithContext("Failed to handle event", reporter.Context{
|
2023-03-02 13:10:23 +00:00
|
|
|
"user_id": user.ID(),
|
|
|
|
"old_event_id": event.OldEventID,
|
|
|
|
"new_event_id": event.NewEventID,
|
|
|
|
"event_info": event.EventInfo,
|
|
|
|
"error": event.Error,
|
2023-03-08 10:17:51 +00:00
|
|
|
"error_type": internal.ErrCauseType(event.Error),
|
2023-03-07 12:48:34 +00:00
|
|
|
}); rerr != nil {
|
2024-02-26 13:16:50 +00:00
|
|
|
logrus.WithField("pkg", "bridge/event").WithError(rerr).Error("Failed to report failed event handling")
|
2023-01-17 16:23:01 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 12:59:12 +00:00
|
|
|
user.OnBadEvent(ctx)
|
2023-01-17 14:11:13 +00:00
|
|
|
}, bridge.usersLock)
|
|
|
|
}
|
2023-02-22 12:30:45 +00:00
|
|
|
|
|
|
|
func (bridge *Bridge) handleUncategorizedErrorEvent(event events.UncategorizedEventError) {
|
|
|
|
if rerr := bridge.reporter.ReportMessageWithContext("Failed to handle due to uncategorized error", reporter.Context{
|
2023-03-08 10:17:51 +00:00
|
|
|
"error_type": internal.ErrCauseType(event.Error),
|
2023-02-22 12:30:45 +00:00
|
|
|
"error": event.Error,
|
|
|
|
}); rerr != nil {
|
2024-02-26 13:16:50 +00:00
|
|
|
logrus.WithField("pkg", "bridge/event").WithError(rerr).Error("Failed to report failed event handling")
|
2023-02-22 12:30:45 +00:00
|
|
|
}
|
|
|
|
}
|