2014-06-03 18:27:20 +00:00
|
|
|
package models
|
|
|
|
|
2016-02-22 03:09:14 +00:00
|
|
|
import (
|
2016-02-21 02:53:53 +00:00
|
|
|
"errors"
|
2016-02-22 04:12:47 +00:00
|
|
|
"net/mail"
|
2016-02-21 02:53:53 +00:00
|
|
|
"time"
|
|
|
|
)
|
2016-02-02 00:36:59 +00:00
|
|
|
|
2015-02-07 20:31:41 +00:00
|
|
|
// SMTP contains the attributes needed to handle the sending of campaign emails
|
2014-06-03 18:27:20 +00:00
|
|
|
type SMTP struct {
|
2016-02-22 03:09:14 +00:00
|
|
|
Id int64 `json:"id" gorm:"column:id; primary_key:yes"`
|
|
|
|
UserId int64 `json:"-" gorm:"column:user_id"`
|
|
|
|
Interface string `json:"interface_type" gorm:"column:interface_type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
FromAddress string `json:"from_address"`
|
|
|
|
IgnoreCertErrors bool `json:"ignore_cert_errors"`
|
|
|
|
ModifiedDate time.Time `json:"modified_date"`
|
2014-06-03 18:27:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 00:36:59 +00:00
|
|
|
// ErrFromAddressNotSpecified is thrown when there is no "From" address
|
|
|
|
// specified in the SMTP configuration
|
|
|
|
var ErrFromAddressNotSpecified = errors.New("No From Address specified")
|
|
|
|
|
|
|
|
// ErrHostNotSpecified is thrown when there is no Host specified
|
|
|
|
// in the SMTP configuration
|
|
|
|
var ErrHostNotSpecified = errors.New("No SMTP Host specified")
|
|
|
|
|
2015-02-07 20:31:41 +00:00
|
|
|
// TableName specifies the database tablename for Gorm to use
|
|
|
|
func (s SMTP) TableName() string {
|
|
|
|
return "smtp"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate ensures that SMTP configs/connections are valid
|
2016-02-02 00:36:59 +00:00
|
|
|
func (s *SMTP) Validate() error {
|
2014-06-03 18:27:20 +00:00
|
|
|
switch {
|
|
|
|
case s.FromAddress == "":
|
2016-02-02 00:36:59 +00:00
|
|
|
return ErrFromAddressNotSpecified
|
2014-06-05 04:54:46 +00:00
|
|
|
case s.Host == "":
|
2016-02-02 00:36:59 +00:00
|
|
|
return ErrHostNotSpecified
|
2014-06-03 18:27:20 +00:00
|
|
|
}
|
2016-02-22 04:12:47 +00:00
|
|
|
_, err := mail.ParseAddress(s.FromAddress)
|
|
|
|
return err
|
2014-06-03 18:27:20 +00:00
|
|
|
}
|
2016-02-21 02:53:53 +00:00
|
|
|
|
|
|
|
// GetSMTPs returns the SMTPs owned by the given user.
|
|
|
|
func GetSMTPs(uid int64) ([]SMTP, error) {
|
|
|
|
ss := []SMTP{}
|
|
|
|
err := db.Where("user_id=?", uid).Find(&ss).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
return ss, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSMTP returns the SMTP, if it exists, specified by the given id and user_id.
|
|
|
|
func GetSMTP(id int64, uid int64) (SMTP, error) {
|
|
|
|
s := SMTP{}
|
|
|
|
err := db.Where("user_id=? and id=?", uid, id).Find(&s).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSMTPByName returns the SMTP, if it exists, specified by the given name and user_id.
|
|
|
|
func GetSMTPByName(n string, uid int64) (SMTP, error) {
|
|
|
|
s := SMTP{}
|
|
|
|
err := db.Where("user_id=? and name=?", uid, n).Find(&s).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostSMTP creates a new SMTP in the database.
|
|
|
|
func PostSMTP(s *SMTP) error {
|
|
|
|
err := s.Validate()
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Insert into the DB
|
|
|
|
err = db.Save(s).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutSMTP edits an existing SMTP in the database.
|
|
|
|
// Per the PUT Method RFC, it presumes all data for a SMTP is provided.
|
|
|
|
func PutSMTP(s *SMTP) error {
|
2016-02-22 04:12:47 +00:00
|
|
|
err := s.Validate()
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = db.Where("id=?", s.Id).Save(s).Error
|
2016-02-21 02:53:53 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSMTP deletes an existing SMTP in the database.
|
|
|
|
// An error is returned if a SMTP with the given user id and SMTP id is not found.
|
|
|
|
func DeleteSMTP(id int64, uid int64) error {
|
|
|
|
err = db.Where("user_id=?", uid).Delete(SMTP{Id: id}).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|