From 7f4cd90c03e945dc207f959cb03b8893608d1501 Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Sun, 8 Sep 2024 00:23:15 +1200 Subject: [PATCH] Add undocumented "demonstration mode" --- cmd/root.go | 3 +++ config/config.go | 9 +++++++++ internal/storage/cron.go | 39 +++++++++++++++++++++++++-------------- server/apiv1/api.go | 5 +++++ server/apiv1/release.go | 5 +++++ server/apiv1/send.go | 6 ++++++ 6 files changed, 53 insertions(+), 14 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 03b6376..e7a277c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -305,6 +305,9 @@ func initConfigFromEnv() { if len(os.Getenv("MP_WEBHOOK_LIMIT")) > 0 { webhook.RateLimit, _ = strconv.Atoi(os.Getenv("MP_WEBHOOK_LIMIT")) } + + // Demo mode + config.DemoMode = getEnabledFromEnv("MP_DEMO_MODE") } // load deprecated settings from environment and warn diff --git a/config/config.go b/config/config.go index 129de05..60eeea8 100644 --- a/config/config.go +++ b/config/config.go @@ -178,6 +178,9 @@ var ( // DisableHTMLCheck DEPRECATED 2024/04/13 - kept here to display console warning only DisableHTMLCheck = false + + // DemoMode disables SMTP relay, link checking & HTTP send functionality + DemoMode = false ) // AutoTag struct for auto-tagging @@ -467,6 +470,12 @@ func VerifyConfig() error { logger.Log().Warnf("[relay] auto-relaying all new messages via %s:%d", SMTPRelayConfig.Host, SMTPRelayConfig.Port) } + if DemoMode { + MaxMessages = 1000 + // this deserves a warning + logger.Log().Info("demo mode enabled") + } + return nil } diff --git a/internal/storage/cron.go b/internal/storage/cron.go index 884f1c3..992a83f 100644 --- a/internal/storage/cron.go +++ b/internal/storage/cron.go @@ -61,25 +61,32 @@ func pruneMessages() { // prune using `--max` if set if config.MaxMessages > 0 { - q := sqlf.Select("ID, Size"). - From(tenant("mailbox")). - OrderBy("Created DESC"). - Limit(5000). - Offset(config.MaxMessages) + total := CountTotal() + if total > float64(config.MaxAgeInHours) { + offset := config.MaxMessages + if config.DemoMode { + offset = 500 + } + q := sqlf.Select("ID, Size"). + From(tenant("mailbox")). + OrderBy("Created DESC"). + Limit(5000). + Offset(offset) - if err := q.QueryAndClose(context.TODO(), db, func(row *sql.Rows) { - var id string + if err := q.QueryAndClose(context.TODO(), db, func(row *sql.Rows) { + var id string - if err := row.Scan(&id, &size); err != nil { + if err := row.Scan(&id, &size); err != nil { + logger.Log().Errorf("[db] %s", err.Error()) + return + } + ids = append(ids, id) + prunedSize = prunedSize + int64(size) + + }); err != nil { logger.Log().Errorf("[db] %s", err.Error()) return } - ids = append(ids, id) - prunedSize = prunedSize + int64(size) - - }); err != nil { - logger.Log().Errorf("[db] %s", err.Error()) - return } } @@ -166,6 +173,10 @@ func pruneMessages() { logMessagesDeleted(len(ids)) + if config.DemoMode { + vacuumDb() + } + websockets.Broadcast("prune", nil) } diff --git a/server/apiv1/api.go b/server/apiv1/api.go index ee08616..bdcbebb 100644 --- a/server/apiv1/api.go +++ b/server/apiv1/api.go @@ -601,6 +601,11 @@ func LinkCheck(w http.ResponseWriter, r *http.Request) { // 200: LinkCheckResponse // default: ErrorResponse + if config.DemoMode { + httpError(w, "this functionality has been disabled for demonstration purposes") + return + } + vars := mux.Vars(r) id := vars["id"] diff --git a/server/apiv1/release.go b/server/apiv1/release.go index a406920..06fb969 100644 --- a/server/apiv1/release.go +++ b/server/apiv1/release.go @@ -37,6 +37,11 @@ func ReleaseMessage(w http.ResponseWriter, r *http.Request) { // 200: OKResponse // default: ErrorResponse + if config.DemoMode { + httpError(w, "this functionality has been disabled for demonstration purposes") + return + } + vars := mux.Vars(r) id := vars["id"] diff --git a/server/apiv1/send.go b/server/apiv1/send.go index 54f72a1..90d7203 100644 --- a/server/apiv1/send.go +++ b/server/apiv1/send.go @@ -11,6 +11,7 @@ import ( "net/mail" "strings" + "github.com/axllent/mailpit/config" "github.com/axllent/mailpit/internal/tools" "github.com/axllent/mailpit/server/smtpd" "github.com/jhillyerd/enmime" @@ -141,6 +142,11 @@ func SendMessageHandler(w http.ResponseWriter, r *http.Request) { // 200: sendMessageResponse // default: jsonErrorResponse + if config.DemoMode { + httpJSONError(w, "this functionality has been disabled for demonstration purposes") + return + } + decoder := json.NewDecoder(r.Body) data := SendRequest{}