Other: Log mailbox message counts at startup

This commit is contained in:
James Houlahan
2022-11-10 16:59:44 +01:00
parent 48cf89b1a6
commit 2023df3ef8
3 changed files with 36 additions and 17 deletions

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.18
require ( require (
github.com/0xAX/notificator v0.0.0-20220220101646-ee9b8921e557 github.com/0xAX/notificator v0.0.0-20220220101646-ee9b8921e557
github.com/Masterminds/semver/v3 v3.1.1 github.com/Masterminds/semver/v3 v3.1.1
github.com/ProtonMail/gluon v0.13.1-0.20221109132354-60a88e1e48b3 github.com/ProtonMail/gluon v0.13.1-0.20221110152951-c99ec6195798
github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a
github.com/ProtonMail/go-rfc5322 v0.11.0 github.com/ProtonMail/go-rfc5322 v0.11.0
github.com/ProtonMail/gopenpgp/v2 v2.4.10 github.com/ProtonMail/gopenpgp/v2 v2.4.10

4
go.sum
View File

@ -28,8 +28,8 @@ github.com/ProtonMail/bcrypt v0.0.0-20211005172633-e235017c1baf h1:yc9daCCYUefEs
github.com/ProtonMail/bcrypt v0.0.0-20211005172633-e235017c1baf/go.mod h1:o0ESU9p83twszAU8LBeJKFAAMX14tISa0yk4Oo5TOqo= github.com/ProtonMail/bcrypt v0.0.0-20211005172633-e235017c1baf/go.mod h1:o0ESU9p83twszAU8LBeJKFAAMX14tISa0yk4Oo5TOqo=
github.com/ProtonMail/docker-credential-helpers v1.1.0 h1:+kvUIpwWcbtP3WFv5sSvkFn/XLzSqPOB5AAthuk9xPk= github.com/ProtonMail/docker-credential-helpers v1.1.0 h1:+kvUIpwWcbtP3WFv5sSvkFn/XLzSqPOB5AAthuk9xPk=
github.com/ProtonMail/docker-credential-helpers v1.1.0/go.mod h1:mK0aBveCxhnQ756AmaTfXMZDeULvheYVhF/MWMErN5g= github.com/ProtonMail/docker-credential-helpers v1.1.0/go.mod h1:mK0aBveCxhnQ756AmaTfXMZDeULvheYVhF/MWMErN5g=
github.com/ProtonMail/gluon v0.13.1-0.20221109132354-60a88e1e48b3 h1:ptBp0Mp4Ekr/h71PQm++7GEPR3yY1AOwrg7hdPVg3dI= github.com/ProtonMail/gluon v0.13.1-0.20221110152951-c99ec6195798 h1:0tGxq1h14O922rffSYRWNS1GYtN/hjK3nhjXcaIrdQg=
github.com/ProtonMail/gluon v0.13.1-0.20221109132354-60a88e1e48b3/go.mod h1:XW/gcr4jErc5bX5yMqkUq3U+AucC2QZHJ5L231k3Nw4= github.com/ProtonMail/gluon v0.13.1-0.20221110152951-c99ec6195798/go.mod h1:XW/gcr4jErc5bX5yMqkUq3U+AucC2QZHJ5L231k3Nw4=
github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a h1:D+aZah+k14Gn6kmL7eKxoo/4Dr/lK3ChBcwce2+SQP4= github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a h1:D+aZah+k14Gn6kmL7eKxoo/4Dr/lK3ChBcwce2+SQP4=
github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a/go.mod h1:oTGdE7/DlWIr23G0IKW3OXK9wZ5Hw1GGiaJFccTvZi4= github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a/go.mod h1:oTGdE7/DlWIr23G0IKW3OXK9wZ5Hw1GGiaJFccTvZi4=
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo=

View File

@ -92,19 +92,26 @@ func (bridge *Bridge) closeIMAP(ctx context.Context) error {
// addIMAPUser connects the given user to gluon. // addIMAPUser connects the given user to gluon.
func (bridge *Bridge) addIMAPUser(ctx context.Context, user *user.User) error { func (bridge *Bridge) addIMAPUser(ctx context.Context, user *user.User) error {
logrus.WithField("userID", user.ID()).Info("Adding IMAP user")
imapConn, err := user.NewIMAPConnectors() imapConn, err := user.NewIMAPConnectors()
if err != nil { if err != nil {
return fmt.Errorf("failed to create IMAP connectors: %w", err) return fmt.Errorf("failed to create IMAP connectors: %w", err)
} }
for addrID, imapConn := range imapConn { for addrID, imapConn := range imapConn {
log := logrus.WithFields(logrus.Fields{
"userID": user.ID(),
"addrID": addrID,
})
if gluonID, ok := user.GetGluonID(addrID); ok { if gluonID, ok := user.GetGluonID(addrID); ok {
log.WithField("gluonID", gluonID).Info("Loading existing IMAP user")
if err := bridge.imapServer.LoadUser(ctx, imapConn, gluonID, user.GluonKey()); err != nil { if err := bridge.imapServer.LoadUser(ctx, imapConn, gluonID, user.GluonKey()); err != nil {
return fmt.Errorf("failed to load IMAP user: %w", err) return fmt.Errorf("failed to load IMAP user: %w", err)
} }
} else { } else {
log.Info("Creating new IMAP user")
gluonID, err := bridge.imapServer.AddUser(ctx, imapConn, user.GluonKey()) gluonID, err := bridge.imapServer.AddUser(ctx, imapConn, user.GluonKey())
if err != nil { if err != nil {
return fmt.Errorf("failed to add IMAP user: %w", err) return fmt.Errorf("failed to add IMAP user: %w", err)
@ -113,6 +120,8 @@ func (bridge *Bridge) addIMAPUser(ctx context.Context, user *user.User) error {
if err := user.SetGluonID(addrID, gluonID); err != nil { if err := user.SetGluonID(addrID, gluonID); err != nil {
return fmt.Errorf("failed to set IMAP user ID: %w", err) return fmt.Errorf("failed to set IMAP user ID: %w", err)
} }
log.WithField("gluonID", gluonID).Info("Created new IMAP user")
} }
} }
@ -138,6 +147,15 @@ func (bridge *Bridge) removeIMAPUser(ctx context.Context, user *user.User, withF
func (bridge *Bridge) handleIMAPEvent(event imapEvents.Event) { func (bridge *Bridge) handleIMAPEvent(event imapEvents.Event) {
switch event := event.(type) { switch event := event.(type) {
case imapEvents.UserAdded:
for labelID, count := range event.Counts {
logrus.WithFields(logrus.Fields{
"gluonID": event.UserID,
"labelID": labelID,
"count": count,
}).Info("Received mailbox message count")
}
case imapEvents.SessionAdded: case imapEvents.SessionAdded:
if !bridge.identifier.HasClient() { if !bridge.identifier.HasClient() {
bridge.identifier.SetClient(defaultClientName, defaultClientVersion) bridge.identifier.SetClient(defaultClientName, defaultClientVersion)
@ -213,18 +231,8 @@ func newIMAPServer(
imapServer, err := gluon.New( imapServer, err := gluon.New(
gluon.WithTLS(tlsConfig), gluon.WithTLS(tlsConfig),
gluon.WithDataDir(gluonDir), gluon.WithDataDir(gluonDir),
gluon.WithVersionInfo( gluon.WithLogger(imapClientLog, imapServerLog),
int(version.Major()), getGluonVersionInfo(version),
int(version.Minor()),
int(version.Patch()),
constants.FullAppName,
"TODO",
"TODO",
),
gluon.WithLogger(
imapClientLog,
imapServerLog,
),
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -243,6 +251,17 @@ func newIMAPServer(
return imapServer, nil return imapServer, nil
} }
func getGluonVersionInfo(version *semver.Version) gluon.Option {
return gluon.WithVersionInfo(
int(version.Major()),
int(version.Minor()),
int(version.Patch()),
constants.FullAppName,
"TODO",
"TODO",
)
}
// isEmpty returns whether the given directory is empty. // isEmpty returns whether the given directory is empty.
// If the directory does not exist, the second return value is false. // If the directory does not exist, the second return value is false.
func isEmpty(dir string) (bool, bool, error) { func isEmpty(dir string) (bool, bool, error) {