From c5e123f304e5c7f47177e5494713c9e69d067d91 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Mon, 26 Sep 2022 12:56:01 +0200 Subject: [PATCH] first draft for virusdetected email template Signed-off-by: jkoberg --- .../email/templates/virusdetected.email.tmpl | 19 ++++++++ services/notifications/pkg/service/service.go | 48 ++++++++++++------- 2 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 services/notifications/pkg/email/templates/virusdetected.email.tmpl diff --git a/services/notifications/pkg/email/templates/virusdetected.email.tmpl b/services/notifications/pkg/email/templates/virusdetected.email.tmpl new file mode 100644 index 00000000000..a9c5927f5dc --- /dev/null +++ b/services/notifications/pkg/email/templates/virusdetected.email.tmpl @@ -0,0 +1,19 @@ +Hello {{ .ExecutingUser }}, + +The virusscan of file {{ .Filename }} discovered it is infected with {{ .Virus }}. +The system is configured to handle infected files like: {{ .Outcome }}. + +Please contact your administrator for more information. + +---------------------------------------------------------- + +Hallo {{ .ExecutingUser }}, + +Der Viren Scan der Datei {{ .Filename }} hat herausgefunden dass sie mit {{ .Virus }} infiziert ist. +Das System ist konfiguriert auf infizierte Dateien mit {{ .Outcome }} zu reagieren. /* NOTE: .Outcome is always english */ + +Für mehr informationen wende dich bitte an deinen Systemadministrator. + +--- +ownCloud - Store. Share. Work. +https://owncloud.com diff --git a/services/notifications/pkg/service/service.go b/services/notifications/pkg/service/service.go index cee2e9fcbfd..983a7a87b65 100644 --- a/services/notifications/pkg/service/service.go +++ b/services/notifications/pkg/service/service.go @@ -64,22 +64,7 @@ func (s eventsNotifier) Run() error { case events.ShareCreated: s.handleShareCreated(e) case events.VirusscanFinished: - // TODO: use templating system - - if !e.Infected { - // no need to annoy the user - return - } - - if e.ExecutingUser == nil { - s.logger.Error().Str("events", "VirusscanFinished").Str("uploadid", e.UploadID).Msg("no executing user") - return - } - m := "Dear %s,\nThe virusscan of file '%s' discovered it is infected with '%s'.\nThe system is configured to handle infected files like: %s.\nContact your administrator for more information." - msg := fmt.Sprintf(m, e.ExecutingUser.GetUsername(), e.Filename, e.Description, e.Outcome) - if err := s.channel.SendMessage([]string{e.ExecutingUser.GetId().GetOpaqueId()}, msg, "", ""); err != nil { - s.logger.Error().Err(err).Str("event", "VirusScanFinished").Msg("failed to send a message") - } + s.handleVirusscanFinished(e) } }() case <-s.signals: @@ -325,6 +310,37 @@ func (s eventsNotifier) handleShareCreated(e events.ShareCreated) { } } +func (s eventsNotifier) handleVirusscanFinished(e events.VirusscanFinished) { + if !e.Infected { + // no need to annoy the user + return + } + + // should we send the email to all user who have access to the file? If so - how to find them? + if e.ExecutingUser == nil { + s.logger.Error().Str("events", "VirusscanFinished").Interface("resourceid", e.ResourceID).Str("uploadid", e.UploadID).Msg("no executing user") + return + } + + msg, err := email.RenderEmailTemplate("virusdetected.email.tmpl", map[string]string{ + "ExecutingUser": e.ExecutingUser.Username, + "Virus": e.Description, + "Outcome": string(e.Outcome), + "Filename": e.Filename, + }, s.emailTemplatePath) + if err != nil { + s.logger.Error().Str("events", "VirusscanFinished").Interface("resourceid", e.ResourceID).Str("uploadid", e.UploadID).Msg("can't render email template") + return + } + + senderDisplayName := "Owncloud" + emailSubject := "Virus detected" + if err := s.channel.SendMessage([]string{e.ExecutingUser.GetId().GetOpaqueId()}, msg, emailSubject, senderDisplayName); err != nil { + s.logger.Error().Err(err).Str("event", "VirusScanFinished").Msg("failed to send a message") + } + +} + // TODO: this function is a backport for go1.19 url.JoinPath, upon go bump, replace this func urlJoinPath(base string, elements ...string) (string, error) { u, err := url.Parse(base)