diff --git a/config/config.go b/config/config.go index 2f5feaad8..8fc961e19 100755 --- a/config/config.go +++ b/config/config.go @@ -161,6 +161,7 @@ func NewDefaultCGRConfig() (*CGRConfig, error) { cfg.dispatcherSCfg = new(DispatcherSCfg) cfg.loaderCgrCfg = new(LoaderCgrCfg) cfg.migratorCgrCfg = new(MigratorCgrCfg) + cfg.mailerCfg = new(MailerCfg) //Depricated cfg.cdrStatsCfg = new(CdrStatsCfg) @@ -277,11 +278,6 @@ type CGRConfig struct { httpAgentCfg []*HttpAgentCfg // HttpAgent configuration - MailerServer string // The server to use when sending emails out - MailerAuthUser string // Authenticate to email server using this user - MailerAuthPass string // Authenticate to email server with this password - MailerFromAddr string // From address used when sending emails out - ConfigReloads map[string]chan struct{} // Signals to specific entities that a config reload should occur generalCfg *GeneralCfg // General config @@ -311,6 +307,7 @@ type CGRConfig struct { dispatcherSCfg *DispatcherSCfg // DispatcherS config loaderCgrCfg *LoaderCgrCfg // LoaderCgr config migratorCgrCfg *MigratorCgrCfg // MigratorCgr config + mailerCfg *MailerCfg // Mailer config analyzerSCfg *AnalyzerSCfg // Deprecated @@ -931,6 +928,9 @@ func (self *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) { if err != nil { return err } + if self.mailerCfg.loadFromJsonCfg(jsnMailerCfg); err != nil { + return err + } jsnSureTaxCfg, err := jsnCfg.SureTaxJsonCfg() if err != nil { @@ -1096,21 +1096,6 @@ func (self *CGRConfig) loadFromJsonCfg(jsnCfg *CgrJsonCfg) (err error) { } } ///depricated^^^ - - if jsnMailerCfg != nil { - if jsnMailerCfg.Server != nil { - self.MailerServer = *jsnMailerCfg.Server - } - if jsnMailerCfg.Auth_user != nil { - self.MailerAuthUser = *jsnMailerCfg.Auth_user - } - if jsnMailerCfg.Auth_password != nil { - self.MailerAuthPass = *jsnMailerCfg.Auth_password - } - if jsnMailerCfg.From_address != nil { - self.MailerFromAddr = *jsnMailerCfg.From_address - } - } return nil } @@ -1242,6 +1227,10 @@ func (cfg *CGRConfig) CdrStatsCfg() *CdrStatsCfg { return cfg.cdrStatsCfg } +func (cfg *CGRConfig) MailerCfg() *MailerCfg { + return cfg.mailerCfg +} + func (cfg *CGRConfig) AnalyzerSCfg() *AnalyzerSCfg { return cfg.analyzerSCfg } diff --git a/config/config_test.go b/config/config_test.go index 4ce83093e..96f018b18 100755 --- a/config/config_test.go +++ b/config/config_test.go @@ -965,17 +965,17 @@ func TestCgrCfgJSONDefaultsDiameterAgentCfg(t *testing.T) { } func TestCgrCfgJSONDefaultsMailer(t *testing.T) { - if cgrCfg.MailerServer != "localhost" { - t.Error(cgrCfg.MailerServer) + if cgrCfg.MailerCfg().MailerServer != "localhost" { + t.Error(cgrCfg.MailerCfg().MailerServer) } - if cgrCfg.MailerAuthUser != "cgrates" { - t.Error(cgrCfg.MailerAuthUser) + if cgrCfg.MailerCfg().MailerAuthUser != "cgrates" { + t.Error(cgrCfg.MailerCfg().MailerAuthUser) } - if cgrCfg.MailerAuthPass != "CGRateS.org" { - t.Error(cgrCfg.MailerAuthPass) + if cgrCfg.MailerCfg().MailerAuthPass != "CGRateS.org" { + t.Error(cgrCfg.MailerCfg().MailerAuthPass) } - if cgrCfg.MailerFromAddr != "cgr-mailer@localhost.localdomain" { - t.Error(cgrCfg.MailerFromAddr) + if cgrCfg.MailerCfg().MailerFromAddr != "cgr-mailer@localhost.localdomain" { + t.Error(cgrCfg.MailerCfg().MailerFromAddr) } } diff --git a/config/mailercfg.go b/config/mailercfg.go new file mode 100644 index 000000000..781890c27 --- /dev/null +++ b/config/mailercfg.go @@ -0,0 +1,48 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program 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. + +This program 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 this program. If not, see +*/ + +package config + +// Mailer config section +type MailerCfg struct { + MailerServer string // The server to use when sending emails out + MailerAuthUser string // Authenticate to email server using this user + MailerAuthPass string // Authenticate to email server with this password + MailerFromAddr string // From address used when sending emails out +} + +//loadFromJsonCfg loads Database config from JsonCfg +func (mailcfg *MailerCfg) loadFromJsonCfg(jsnMailerCfg *MailerJsonCfg) (err error) { + if jsnMailerCfg == nil { + return nil + } + if jsnMailerCfg.Server != nil { + mailcfg.MailerServer = *jsnMailerCfg.Server + } + if jsnMailerCfg.Auth_user != nil { + mailcfg.MailerAuthUser = *jsnMailerCfg.Auth_user + } + if jsnMailerCfg.Auth_password != nil { + mailcfg.MailerAuthPass = *jsnMailerCfg.Auth_password + } + if jsnMailerCfg.From_address != nil { + mailcfg.MailerFromAddr = *jsnMailerCfg.From_address + } + + return nil +} diff --git a/config/mailercfg_test.go b/config/mailercfg_test.go new file mode 100644 index 000000000..bbbc4d9d0 --- /dev/null +++ b/config/mailercfg_test.go @@ -0,0 +1,61 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program 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. + +This program 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 this program. If not, see +*/ +package config + +import ( + "reflect" + "strings" + "testing" +) + +func TestMailerCfgloadFromJsonCfg(t *testing.T) { + var mailcfg, expected MailerCfg + if err := mailcfg.loadFromJsonCfg(nil); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(mailcfg, expected) { + t.Errorf("Expected: %+v ,recived: %+v", expected, mailcfg) + } + if err := mailcfg.loadFromJsonCfg(new(MailerJsonCfg)); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(mailcfg, expected) { + t.Errorf("Expected: %+v ,recived: %+v", expected, mailcfg) + } + cfgJSONStr := `{ +"mailer": { + "server": "localhost", // the server to use when sending emails out + "auth_user": "cgrates", // authenticate to email server using this user + "auth_password": "CGRateS.org", // authenticate to email server with this password + "from_address": "cgr-mailer@localhost.localdomain" // from address used when sending emails out + }, +}` + expected = MailerCfg{ + MailerServer: "localhost", + MailerAuthUser: "cgrates", + MailerAuthPass: "CGRateS.org", + MailerFromAddr: "cgr-mailer@localhost.localdomain", + } + if jsnCfg, err := NewCgrJsonCfgFromReader(strings.NewReader(cfgJSONStr)); err != nil { + t.Error(err) + } else if jsnMailCfg, err := jsnCfg.MailerJsonCfg(); err != nil { + t.Error(err) + } else if err = mailcfg.loadFromJsonCfg(jsnMailCfg); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(expected, mailcfg) { + t.Errorf("Expected: %+v , recived: %+v", expected, mailcfg) + } +} diff --git a/engine/action.go b/engine/action.go index 527d5a348..be7d5ad17 100644 --- a/engine/action.go +++ b/engine/action.go @@ -442,10 +442,10 @@ func mailAsync(ub *Account, sq *CDRStatsQueueTriggered, a *Action, acs Actions) message = []byte(fmt.Sprintf("To: %s\r\nSubject: [CGR Notification] Threshold hit on CDRStatsQueueId: %s\r\n\r\nTime: \r\n\t%s\r\n\r\nCDRStatsQueueId:\r\n\t%s\r\n\r\nMetrics:\r\n\t%+v\r\n\r\nTrigger:\r\n\t%+v\r\n\r\nYours faithfully,\r\nCGR CDR Stats Monitor\r\n", toAddrStr, sq.Id, time.Now(), sq.Id, sq.Metrics, sq.Trigger)) } - auth := smtp.PlainAuth("", cgrCfg.MailerAuthUser, cgrCfg.MailerAuthPass, strings.Split(cgrCfg.MailerServer, ":")[0]) // We only need host part, so ignore port + auth := smtp.PlainAuth("", cgrCfg.MailerCfg().MailerAuthUser, cgrCfg.MailerCfg().MailerAuthPass, strings.Split(cgrCfg.MailerCfg().MailerServer, ":")[0]) // We only need host part, so ignore port go func() { for i := 0; i < 5; i++ { // Loop so we can increase the success rate on best effort - if err := smtp.SendMail(cgrCfg.MailerServer, auth, cgrCfg.MailerFromAddr, toAddrs, message); err == nil { + if err := smtp.SendMail(cgrCfg.MailerCfg().MailerServer, auth, cgrCfg.MailerCfg().MailerFromAddr, toAddrs, message); err == nil { break } else if i == 4 { if ub != nil {