2014-03-25 03:31:33 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2015-02-21 06:11:22 +00:00
|
|
|
"errors"
|
2018-05-27 02:26:34 +00:00
|
|
|
"net/url"
|
2014-03-25 03:31:33 +00:00
|
|
|
"time"
|
2014-06-03 18:27:20 +00:00
|
|
|
|
2018-05-04 00:07:41 +00:00
|
|
|
log "github.com/gophish/gophish/logger"
|
2019-12-16 02:27:21 +00:00
|
|
|
"github.com/gophish/gophish/webhook"
|
2014-06-03 18:27:20 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2018-05-04 00:07:41 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2014-03-25 03:31:33 +00:00
|
|
|
)
|
|
|
|
|
2016-06-08 03:31:55 +00:00
|
|
|
// Campaign is a struct representing a created campaign
|
2014-03-25 03:31:33 +00:00
|
|
|
type Campaign struct {
|
2016-02-22 03:09:14 +00:00
|
|
|
Id int64 `json:"id"`
|
|
|
|
UserId int64 `json:"-"`
|
|
|
|
Name string `json:"name" sql:"not null"`
|
|
|
|
CreatedDate time.Time `json:"created_date"`
|
2016-06-08 02:42:09 +00:00
|
|
|
LaunchDate time.Time `json:"launch_date"`
|
2018-09-02 16:17:52 +00:00
|
|
|
SendByDate time.Time `json:"send_by_date"`
|
2016-02-22 03:09:14 +00:00
|
|
|
CompletedDate time.Time `json:"completed_date"`
|
|
|
|
TemplateId int64 `json:"-"`
|
|
|
|
Template Template `json:"template"`
|
|
|
|
PageId int64 `json:"-"`
|
|
|
|
Page Page `json:"page"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Results []Result `json:"results,omitempty"`
|
|
|
|
Groups []Group `json:"groups,omitempty"`
|
2020-05-26 02:46:36 +00:00
|
|
|
Events []Event `json:"timeline,omitempty"`
|
2016-02-22 03:09:14 +00:00
|
|
|
SMTPId int64 `json:"-"`
|
|
|
|
SMTP SMTP `json:"smtp"`
|
|
|
|
URL string `json:"url"`
|
2014-06-03 18:27:20 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 03:31:55 +00:00
|
|
|
// CampaignResults is a struct representing the results from a campaign
|
|
|
|
type CampaignResults struct {
|
2018-10-08 15:49:21 +00:00
|
|
|
Id int64 `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Status string `json:"status"`
|
2018-12-16 03:38:51 +00:00
|
|
|
Results []Result `json:"results,omitempty"`
|
2018-10-08 15:49:21 +00:00
|
|
|
Events []Event `json:"timeline,omitempty"`
|
2016-06-08 03:31:55 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 00:07:41 +00:00
|
|
|
// CampaignSummaries is a struct representing the overview of campaigns
|
2017-01-06 03:48:54 +00:00
|
|
|
type CampaignSummaries struct {
|
|
|
|
Total int64 `json:"total"`
|
|
|
|
Campaigns []CampaignSummary `json:"campaigns"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CampaignSummary is a struct representing the overview of a single camaign
|
|
|
|
type CampaignSummary struct {
|
|
|
|
Id int64 `json:"id"`
|
|
|
|
CreatedDate time.Time `json:"created_date"`
|
|
|
|
LaunchDate time.Time `json:"launch_date"`
|
2018-09-02 16:17:52 +00:00
|
|
|
SendByDate time.Time `json:"send_by_date"`
|
2017-01-06 03:48:54 +00:00
|
|
|
CompletedDate time.Time `json:"completed_date"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Stats CampaignStats `json:"stats"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CampaignStats is a struct representing the statistics for a single campaign
|
|
|
|
type CampaignStats struct {
|
|
|
|
Total int64 `json:"total"`
|
|
|
|
EmailsSent int64 `json:"sent"`
|
|
|
|
OpenedEmail int64 `json:"opened"`
|
|
|
|
ClickedLink int64 `json:"clicked"`
|
|
|
|
SubmittedData int64 `json:"submitted_data"`
|
2018-03-19 03:03:00 +00:00
|
|
|
EmailReported int64 `json:"email_reported"`
|
2017-01-06 03:48:54 +00:00
|
|
|
Error int64 `json:"error"`
|
|
|
|
}
|
|
|
|
|
2018-05-27 02:26:34 +00:00
|
|
|
// Event contains the fields for an event
|
|
|
|
// that occurs during the campaign
|
|
|
|
type Event struct {
|
|
|
|
Id int64 `json:"-"`
|
2020-01-30 13:09:04 +00:00
|
|
|
CampaignId int64 `json:"campaign_id"`
|
2018-05-27 02:26:34 +00:00
|
|
|
Email string `json:"email"`
|
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Details string `json:"details"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventDetails is a struct that wraps common attributes we want to store
|
|
|
|
// in an event
|
|
|
|
type EventDetails struct {
|
|
|
|
Payload url.Values `json:"payload"`
|
|
|
|
Browser map[string]string `json:"browser"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventError is a struct that wraps an error that occurs when sending an
|
|
|
|
// email to a recipient
|
|
|
|
type EventError struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2015-02-21 06:11:22 +00:00
|
|
|
// ErrCampaignNameNotSpecified indicates there was no template given by the user
|
|
|
|
var ErrCampaignNameNotSpecified = errors.New("Campaign name not specified")
|
|
|
|
|
|
|
|
// ErrGroupNotSpecified indicates there was no template given by the user
|
|
|
|
var ErrGroupNotSpecified = errors.New("No groups specified")
|
|
|
|
|
|
|
|
// ErrTemplateNotSpecified indicates there was no template given by the user
|
|
|
|
var ErrTemplateNotSpecified = errors.New("No email template specified")
|
|
|
|
|
2015-10-23 03:29:10 +00:00
|
|
|
// ErrPageNotSpecified indicates a landing page was not provided for the campaign
|
|
|
|
var ErrPageNotSpecified = errors.New("No landing page specified")
|
|
|
|
|
2016-02-21 18:05:40 +00:00
|
|
|
// ErrSMTPNotSpecified indicates a sending profile was not provided for the campaign
|
|
|
|
var ErrSMTPNotSpecified = errors.New("No sending profile specified")
|
|
|
|
|
2015-02-21 06:11:22 +00:00
|
|
|
// ErrTemplateNotFound indicates the template specified does not exist in the database
|
|
|
|
var ErrTemplateNotFound = errors.New("Template not found")
|
|
|
|
|
2018-05-04 00:07:41 +00:00
|
|
|
// ErrGroupNotFound indicates a group specified by the user does not exist in the database
|
2015-02-21 06:11:22 +00:00
|
|
|
var ErrGroupNotFound = errors.New("Group not found")
|
|
|
|
|
2015-10-23 03:29:10 +00:00
|
|
|
// ErrPageNotFound indicates a page specified by the user does not exist in the database
|
|
|
|
var ErrPageNotFound = errors.New("Page not found")
|
|
|
|
|
2016-02-21 18:05:40 +00:00
|
|
|
// ErrSMTPNotFound indicates a sending profile specified by the user does not exist in the database
|
|
|
|
var ErrSMTPNotFound = errors.New("Sending profile not found")
|
|
|
|
|
2018-09-02 16:17:52 +00:00
|
|
|
// ErrInvalidSendByDate indicates that the user specified a send by date that occurs before the
|
|
|
|
// launch date
|
|
|
|
var ErrInvalidSendByDate = errors.New("The launch date must be before the \"send emails by\" date")
|
|
|
|
|
2018-02-23 05:02:27 +00:00
|
|
|
// RecipientParameter is the URL parameter that points to the result ID for a recipient.
|
|
|
|
const RecipientParameter = "rid"
|
|
|
|
|
2015-02-07 16:41:53 +00:00
|
|
|
// Validate checks to make sure there are no invalid fields in a submitted campaign
|
2015-02-21 06:11:22 +00:00
|
|
|
func (c *Campaign) Validate() error {
|
2014-06-03 18:27:20 +00:00
|
|
|
switch {
|
|
|
|
case c.Name == "":
|
2015-02-21 06:11:22 +00:00
|
|
|
return ErrCampaignNameNotSpecified
|
2014-06-03 18:27:20 +00:00
|
|
|
case len(c.Groups) == 0:
|
2015-02-21 06:11:22 +00:00
|
|
|
return ErrGroupNotSpecified
|
2014-06-03 18:27:20 +00:00
|
|
|
case c.Template.Name == "":
|
2015-02-21 06:11:22 +00:00
|
|
|
return ErrTemplateNotSpecified
|
2015-10-23 03:29:10 +00:00
|
|
|
case c.Page.Name == "":
|
|
|
|
return ErrPageNotSpecified
|
2016-02-21 18:05:40 +00:00
|
|
|
case c.SMTP.Name == "":
|
|
|
|
return ErrSMTPNotSpecified
|
2018-09-02 16:17:52 +00:00
|
|
|
case !c.SendByDate.IsZero() && !c.LaunchDate.IsZero() && c.SendByDate.Before(c.LaunchDate):
|
|
|
|
return ErrInvalidSendByDate
|
2014-06-03 18:27:20 +00:00
|
|
|
}
|
2016-02-21 18:05:40 +00:00
|
|
|
return nil
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
|
2015-02-07 16:41:53 +00:00
|
|
|
// UpdateStatus changes the campaign status appropriately
|
2014-06-26 02:01:01 +00:00
|
|
|
func (c *Campaign) UpdateStatus(s string) error {
|
|
|
|
// This could be made simpler, but I think there's a bug in gorm
|
|
|
|
return db.Table("campaigns").Where("id=?", c.Id).Update("status", s).Error
|
|
|
|
}
|
|
|
|
|
2015-02-07 16:41:53 +00:00
|
|
|
// AddEvent creates a new campaign event in the database
|
2020-03-01 02:19:54 +00:00
|
|
|
func AddEvent(e *Event, campaignID int64) error {
|
|
|
|
e.CampaignId = campaignID
|
2017-08-29 03:48:49 +00:00
|
|
|
e.Time = time.Now().UTC()
|
2019-12-16 02:27:21 +00:00
|
|
|
|
|
|
|
whs, err := GetActiveWebhooks()
|
|
|
|
if err == nil {
|
|
|
|
whEndPoints := []webhook.EndPoint{}
|
|
|
|
for _, wh := range whs {
|
|
|
|
whEndPoints = append(whEndPoints, webhook.EndPoint{
|
|
|
|
URL: wh.URL,
|
|
|
|
Secret: wh.Secret,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
webhook.SendAll(whEndPoints, e)
|
|
|
|
} else {
|
|
|
|
log.Errorf("error getting active webhooks: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-05-27 02:26:34 +00:00
|
|
|
return db.Save(e).Error
|
2014-06-26 02:01:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 04:41:17 +00:00
|
|
|
// getDetails retrieves the related attributes of the campaign
|
|
|
|
// from the database. If the Events and the Results are not available,
|
|
|
|
// an error is returned. Otherwise, the attribute name is set to [Deleted],
|
|
|
|
// indicating the user deleted the attribute (template, smtp, etc.)
|
|
|
|
func (c *Campaign) getDetails() error {
|
2018-12-15 21:42:32 +00:00
|
|
|
err := db.Model(c).Related(&c.Results).Error
|
2016-03-24 04:41:17 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Warnf("%s: results not found for campaign", err)
|
2016-03-24 04:41:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = db.Model(c).Related(&c.Events).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Warnf("%s: events not found for campaign", err)
|
2016-03-24 04:41:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = db.Table("templates").Where("id=?", c.TemplateId).Find(&c.Template).Error
|
|
|
|
if err != nil {
|
|
|
|
if err != gorm.ErrRecordNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Template = Template{Name: "[Deleted]"}
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Warnf("%s: template not found for campaign", err)
|
2016-03-24 04:41:17 +00:00
|
|
|
}
|
2016-08-03 04:18:53 +00:00
|
|
|
err = db.Where("template_id=?", c.Template.Id).Find(&c.Template.Attachments).Error
|
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Warn(err)
|
2016-08-03 04:18:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-03-24 04:41:17 +00:00
|
|
|
err = db.Table("pages").Where("id=?", c.PageId).Find(&c.Page).Error
|
|
|
|
if err != nil {
|
|
|
|
if err != gorm.ErrRecordNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Page = Page{Name: "[Deleted]"}
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Warnf("%s: page not found for campaign", err)
|
2016-03-24 04:41:17 +00:00
|
|
|
}
|
2016-06-08 02:42:09 +00:00
|
|
|
err = db.Table("smtp").Where("id=?", c.SMTPId).Find(&c.SMTP).Error
|
2016-03-24 04:41:17 +00:00
|
|
|
if err != nil {
|
|
|
|
// Check if the SMTP was deleted
|
|
|
|
if err != gorm.ErrRecordNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.SMTP = SMTP{Name: "[Deleted]"}
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Warnf("%s: sending profile not found for campaign", err)
|
2016-03-24 04:41:17 +00:00
|
|
|
}
|
2017-02-20 00:43:08 +00:00
|
|
|
err = db.Where("smtp_id=?", c.SMTP.Id).Find(&c.SMTP.Headers).Error
|
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Warn(err)
|
2017-02-20 00:43:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-03-24 04:41:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-09 02:20:52 +00:00
|
|
|
// getBaseURL returns the Campaign's configured URL.
|
|
|
|
// This is used to implement the TemplateContext interface.
|
|
|
|
func (c *Campaign) getBaseURL() string {
|
|
|
|
return c.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFromAddress returns the Campaign's configured SMTP "From" address.
|
|
|
|
// This is used to implement the TemplateContext interface.
|
|
|
|
func (c *Campaign) getFromAddress() string {
|
|
|
|
return c.SMTP.FromAddress
|
|
|
|
}
|
|
|
|
|
2018-09-02 16:17:52 +00:00
|
|
|
// generateSendDate creates a sendDate
|
|
|
|
func (c *Campaign) generateSendDate(idx int, totalRecipients int) time.Time {
|
|
|
|
// If no send date is specified, just return the launch date
|
|
|
|
if c.SendByDate.IsZero() || c.SendByDate.Equal(c.LaunchDate) {
|
|
|
|
return c.LaunchDate
|
|
|
|
}
|
|
|
|
// Otherwise, we can calculate the range of minutes to send emails
|
|
|
|
// (since we only poll once per minute)
|
|
|
|
totalMinutes := c.SendByDate.Sub(c.LaunchDate).Minutes()
|
|
|
|
|
|
|
|
// Next, we can determine how many minutes should elapse between emails
|
|
|
|
minutesPerEmail := totalMinutes / float64(totalRecipients)
|
|
|
|
|
|
|
|
// Then, we can calculate the offset for this particular email
|
|
|
|
offset := int(minutesPerEmail * float64(idx))
|
|
|
|
|
|
|
|
// Finally, we can just add this offset to the launch date to determine
|
|
|
|
// when the email should be sent
|
|
|
|
return c.LaunchDate.Add(time.Duration(offset) * time.Minute)
|
|
|
|
}
|
|
|
|
|
2017-01-06 03:48:54 +00:00
|
|
|
// getCampaignStats returns a CampaignStats object for the campaign with the given campaign ID.
|
2017-08-06 02:11:50 +00:00
|
|
|
// It also backfills numbers as appropriate with a running total, so that the values are aggregated.
|
2017-01-06 03:48:54 +00:00
|
|
|
func getCampaignStats(cid int64) (CampaignStats, error) {
|
|
|
|
s := CampaignStats{}
|
|
|
|
query := db.Table("results").Where("campaign_id = ?", cid)
|
|
|
|
err := query.Count(&s.Total).Error
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2018-12-16 03:38:51 +00:00
|
|
|
query.Where("status=?", EventDataSubmit).Count(&s.SubmittedData)
|
2017-01-06 03:48:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2018-12-16 03:38:51 +00:00
|
|
|
query.Where("status=?", EventClicked).Count(&s.ClickedLink)
|
2017-01-06 03:48:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2018-03-19 03:03:00 +00:00
|
|
|
query.Where("reported=?", true).Count(&s.EmailReported)
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2017-08-06 02:11:50 +00:00
|
|
|
// Every submitted data event implies they clicked the link
|
|
|
|
s.ClickedLink += s.SubmittedData
|
2018-12-16 03:38:51 +00:00
|
|
|
err = query.Where("status=?", EventOpened).Count(&s.OpenedEmail).Error
|
2017-01-06 03:48:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2017-08-06 02:11:50 +00:00
|
|
|
// Every clicked link event implies they opened the email
|
|
|
|
s.OpenedEmail += s.ClickedLink
|
2018-12-16 03:38:51 +00:00
|
|
|
err = query.Where("status=?", EventSent).Count(&s.EmailsSent).Error
|
2017-01-06 03:48:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2017-08-29 03:48:49 +00:00
|
|
|
// Every opened email event implies the email was sent
|
2017-08-06 02:11:50 +00:00
|
|
|
s.EmailsSent += s.OpenedEmail
|
2018-12-16 03:38:51 +00:00
|
|
|
err = query.Where("status=?", Error).Count(&s.Error).Error
|
2017-01-06 03:48:54 +00:00
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// GetCampaigns returns the campaigns owned by the given user.
|
|
|
|
func GetCampaigns(uid int64) ([]Campaign, error) {
|
|
|
|
cs := []Campaign{}
|
2014-03-26 20:01:49 +00:00
|
|
|
err := db.Model(&User{Id: uid}).Related(&cs).Error
|
2014-03-26 04:53:51 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-03-26 04:53:51 +00:00
|
|
|
}
|
2018-05-04 00:07:41 +00:00
|
|
|
for i := range cs {
|
2016-03-24 04:41:17 +00:00
|
|
|
err = cs[i].getDetails()
|
2016-03-04 00:47:10 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2016-03-04 00:47:10 +00:00
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
return cs, err
|
|
|
|
}
|
|
|
|
|
2017-01-06 03:48:54 +00:00
|
|
|
// GetCampaignSummaries gets the summary objects for all the campaigns
|
|
|
|
// owned by the current user
|
|
|
|
func GetCampaignSummaries(uid int64) (CampaignSummaries, error) {
|
|
|
|
overview := CampaignSummaries{}
|
|
|
|
cs := []CampaignSummary{}
|
|
|
|
// Get the basic campaign information
|
|
|
|
query := db.Table("campaigns").Where("user_id = ?", uid)
|
2020-01-16 04:02:19 +00:00
|
|
|
query = query.Select("id, name, created_date, launch_date, send_by_date, completed_date, status")
|
2017-01-06 03:48:54 +00:00
|
|
|
err := query.Scan(&cs).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2017-01-06 03:48:54 +00:00
|
|
|
return overview, err
|
|
|
|
}
|
|
|
|
for i := range cs {
|
|
|
|
s, err := getCampaignStats(cs[i].Id)
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2017-01-06 03:48:54 +00:00
|
|
|
return overview, err
|
|
|
|
}
|
|
|
|
cs[i].Stats = s
|
|
|
|
}
|
|
|
|
overview.Total = int64(len(cs))
|
|
|
|
overview.Campaigns = cs
|
|
|
|
return overview, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCampaignSummary gets the summary object for a campaign specified by the campaign ID
|
|
|
|
func GetCampaignSummary(id int64, uid int64) (CampaignSummary, error) {
|
|
|
|
cs := CampaignSummary{}
|
|
|
|
query := db.Table("campaigns").Where("user_id = ? AND id = ?", uid, id)
|
2020-01-16 04:02:19 +00:00
|
|
|
query = query.Select("id, name, created_date, launch_date, send_by_date, completed_date, status")
|
2017-01-06 03:48:54 +00:00
|
|
|
err := query.Scan(&cs).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2017-01-06 03:48:54 +00:00
|
|
|
return cs, err
|
|
|
|
}
|
|
|
|
s, err := getCampaignStats(cs.Id)
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2017-01-06 03:48:54 +00:00
|
|
|
return cs, err
|
|
|
|
}
|
|
|
|
cs.Stats = s
|
|
|
|
return cs, nil
|
|
|
|
}
|
|
|
|
|
2020-03-01 02:19:54 +00:00
|
|
|
// GetCampaignMailContext returns a campaign object with just the relevant
|
|
|
|
// data needed to generate and send emails. This includes the top-level
|
|
|
|
// metadata, the template, and the sending profile.
|
|
|
|
//
|
|
|
|
// This should only ever be used if you specifically want this lightweight
|
|
|
|
// context, since it returns a non-standard campaign object.
|
|
|
|
// ref: #1726
|
|
|
|
func GetCampaignMailContext(id int64, uid int64) (Campaign, error) {
|
|
|
|
c := Campaign{}
|
|
|
|
err := db.Where("id = ?", id).Where("user_id = ?", uid).Find(&c).Error
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
err = db.Table("smtp").Where("id=?", c.SMTPId).Find(&c.SMTP).Error
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
err = db.Where("smtp_id=?", c.SMTP.Id).Find(&c.SMTP.Headers).Error
|
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
err = db.Table("templates").Where("id=?", c.TemplateId).Find(&c.Template).Error
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
err = db.Where("template_id=?", c.Template.Id).Find(&c.Template.Attachments).Error
|
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// GetCampaign returns the campaign, if it exists, specified by the given id and user_id.
|
|
|
|
func GetCampaign(id int64, uid int64) (Campaign, error) {
|
|
|
|
c := Campaign{}
|
2014-03-26 20:01:49 +00:00
|
|
|
err := db.Where("id = ?", id).Where("user_id = ?", uid).Find(&c).Error
|
2014-03-27 02:42:07 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Errorf("%s: campaign not found", err)
|
2014-03-25 03:31:33 +00:00
|
|
|
return c, err
|
|
|
|
}
|
2016-03-24 04:41:17 +00:00
|
|
|
err = c.getDetails()
|
2014-03-27 02:42:07 +00:00
|
|
|
return c, err
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 00:07:41 +00:00
|
|
|
// GetCampaignResults returns just the campaign results for the given campaign
|
2016-06-08 03:31:55 +00:00
|
|
|
func GetCampaignResults(id int64, uid int64) (CampaignResults, error) {
|
|
|
|
cr := CampaignResults{}
|
|
|
|
err := db.Table("campaigns").Where("id=? and user_id=?", id, uid).Find(&cr).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"campaign_id": id,
|
|
|
|
"error": err,
|
|
|
|
}).Error(err)
|
2016-06-08 03:31:55 +00:00
|
|
|
return cr, err
|
|
|
|
}
|
|
|
|
err = db.Table("results").Where("campaign_id=? and user_id=?", cr.Id, uid).Find(&cr.Results).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Errorf("%s: results not found for campaign", err)
|
2016-06-08 03:31:55 +00:00
|
|
|
return cr, err
|
|
|
|
}
|
|
|
|
err = db.Table("events").Where("campaign_id=?", cr.Id).Find(&cr.Events).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Errorf("%s: events not found for campaign", err)
|
2016-06-08 03:31:55 +00:00
|
|
|
return cr, err
|
|
|
|
}
|
|
|
|
return cr, err
|
|
|
|
}
|
|
|
|
|
2016-06-08 02:42:09 +00:00
|
|
|
// GetQueuedCampaigns returns the campaigns that are queued up for this given minute
|
|
|
|
func GetQueuedCampaigns(t time.Time) ([]Campaign, error) {
|
|
|
|
cs := []Campaign{}
|
|
|
|
err := db.Where("launch_date <= ?", t).
|
2018-12-16 03:38:51 +00:00
|
|
|
Where("status = ?", CampaignQueued).Find(&cs).Error
|
2016-06-08 02:42:09 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2016-06-08 02:42:09 +00:00
|
|
|
}
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Infof("Found %d Campaigns to run\n", len(cs))
|
|
|
|
for i := range cs {
|
2016-06-08 02:42:09 +00:00
|
|
|
err = cs[i].getDetails()
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2016-06-08 02:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cs, err
|
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// PostCampaign inserts a campaign and all associated records into the database.
|
|
|
|
func PostCampaign(c *Campaign, uid int64) error {
|
2018-12-15 21:42:32 +00:00
|
|
|
err := c.Validate()
|
|
|
|
if err != nil {
|
2015-02-21 06:11:22 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-06-21 17:19:49 +00:00
|
|
|
// Fill in the details
|
2014-06-26 02:01:01 +00:00
|
|
|
c.UserId = uid
|
2017-08-29 03:48:49 +00:00
|
|
|
c.CreatedDate = time.Now().UTC()
|
2014-06-21 17:19:49 +00:00
|
|
|
c.CompletedDate = time.Time{}
|
2018-12-16 03:38:51 +00:00
|
|
|
c.Status = CampaignQueued
|
2016-06-08 02:42:09 +00:00
|
|
|
if c.LaunchDate.IsZero() {
|
2017-12-09 21:42:07 +00:00
|
|
|
c.LaunchDate = c.CreatedDate
|
2017-08-29 03:48:49 +00:00
|
|
|
} else {
|
|
|
|
c.LaunchDate = c.LaunchDate.UTC()
|
2016-06-08 02:42:09 +00:00
|
|
|
}
|
2018-09-02 16:17:52 +00:00
|
|
|
if !c.SendByDate.IsZero() {
|
|
|
|
c.SendByDate = c.SendByDate.UTC()
|
|
|
|
}
|
2017-12-09 21:42:07 +00:00
|
|
|
if c.LaunchDate.Before(c.CreatedDate) || c.LaunchDate.Equal(c.CreatedDate) {
|
2018-12-16 03:38:51 +00:00
|
|
|
c.Status = CampaignInProgress
|
2017-12-09 21:42:07 +00:00
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
// Check to make sure all the groups already exist
|
2018-09-02 16:17:52 +00:00
|
|
|
// Also, later we'll need to know the total number of recipients (counting
|
|
|
|
// duplicates is ok for now), so we'll do that here to save a loop.
|
|
|
|
totalRecipients := 0
|
2014-03-25 03:31:33 +00:00
|
|
|
for i, g := range c.Groups {
|
|
|
|
c.Groups[i], err = GetGroupByName(g.Name, uid)
|
2016-03-09 04:37:55 +00:00
|
|
|
if err == gorm.ErrRecordNotFound {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"group": g.Name,
|
|
|
|
}).Error("Group does not exist")
|
2015-02-21 06:11:22 +00:00
|
|
|
return ErrGroupNotFound
|
2014-03-25 03:31:33 +00:00
|
|
|
} else if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-03-25 03:31:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-09-02 16:17:52 +00:00
|
|
|
totalRecipients += len(c.Groups[i].Targets)
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
2014-06-03 18:27:20 +00:00
|
|
|
// Check to make sure the template exists
|
|
|
|
t, err := GetTemplateByName(c.Template.Name, uid)
|
2016-03-09 04:37:55 +00:00
|
|
|
if err == gorm.ErrRecordNotFound {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
2020-03-05 13:31:19 +00:00
|
|
|
"template": c.Template.Name,
|
2018-05-04 00:07:41 +00:00
|
|
|
}).Error("Template does not exist")
|
2015-02-21 06:11:22 +00:00
|
|
|
return ErrTemplateNotFound
|
2014-06-03 18:27:20 +00:00
|
|
|
} else if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-06-03 18:27:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-06-05 04:54:46 +00:00
|
|
|
c.Template = t
|
2014-06-03 18:27:20 +00:00
|
|
|
c.TemplateId = t.Id
|
2015-10-23 03:29:10 +00:00
|
|
|
// Check to make sure the page exists
|
|
|
|
p, err := GetPageByName(c.Page.Name, uid)
|
2016-03-09 04:37:55 +00:00
|
|
|
if err == gorm.ErrRecordNotFound {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
2020-03-05 13:31:19 +00:00
|
|
|
"page": c.Page.Name,
|
2018-05-04 00:07:41 +00:00
|
|
|
}).Error("Page does not exist")
|
2015-10-23 03:29:10 +00:00
|
|
|
return ErrPageNotFound
|
|
|
|
} else if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2015-10-23 03:29:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Page = p
|
|
|
|
c.PageId = p.Id
|
2016-02-21 18:05:40 +00:00
|
|
|
// Check to make sure the sending profile exists
|
2016-02-22 03:09:14 +00:00
|
|
|
s, err := GetSMTPByName(c.SMTP.Name, uid)
|
2016-03-09 04:37:55 +00:00
|
|
|
if err == gorm.ErrRecordNotFound {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
2020-03-05 13:31:19 +00:00
|
|
|
"smtp": c.SMTP.Name,
|
2018-05-04 00:07:41 +00:00
|
|
|
}).Error("Sending profile does not exist")
|
|
|
|
return ErrSMTPNotFound
|
2016-02-22 03:09:14 +00:00
|
|
|
} else if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2016-02-22 03:09:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.SMTP = s
|
|
|
|
c.SMTPId = s.Id
|
2014-03-25 03:31:33 +00:00
|
|
|
// Insert into the DB
|
2014-03-26 20:01:49 +00:00
|
|
|
err = db.Save(c).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-03-25 03:31:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-03-01 02:19:54 +00:00
|
|
|
err = AddEvent(&Event{Message: "Campaign Created"}, c.Id)
|
2014-06-26 02:01:01 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-06-26 02:01:01 +00:00
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
// Insert all the results
|
2017-12-12 04:12:17 +00:00
|
|
|
resultMap := make(map[string]bool)
|
2018-09-02 16:17:52 +00:00
|
|
|
recipientIndex := 0
|
2019-12-03 05:00:11 +00:00
|
|
|
tx := db.Begin()
|
2014-03-25 03:31:33 +00:00
|
|
|
for _, g := range c.Groups {
|
|
|
|
// Insert a result for each target in the group
|
|
|
|
for _, t := range g.Targets {
|
2017-12-12 04:12:17 +00:00
|
|
|
// Remove duplicate results - we should only
|
|
|
|
// send emails to unique email addresses.
|
|
|
|
if _, ok := resultMap[t.Email]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resultMap[t.Email] = true
|
2018-09-02 16:17:52 +00:00
|
|
|
sendDate := c.generateSendDate(recipientIndex, totalRecipients)
|
2017-12-09 21:42:07 +00:00
|
|
|
r := &Result{
|
2018-06-09 02:20:52 +00:00
|
|
|
BaseRecipient: BaseRecipient{
|
|
|
|
Email: t.Email,
|
|
|
|
Position: t.Position,
|
|
|
|
FirstName: t.FirstName,
|
|
|
|
LastName: t.LastName,
|
|
|
|
},
|
2018-12-16 03:38:51 +00:00
|
|
|
Status: StatusScheduled,
|
2018-05-27 02:26:34 +00:00
|
|
|
CampaignId: c.Id,
|
|
|
|
UserId: c.UserId,
|
2018-09-02 16:17:52 +00:00
|
|
|
SendDate: sendDate,
|
2018-05-27 02:26:34 +00:00
|
|
|
Reported: false,
|
|
|
|
ModifiedDate: c.CreatedDate,
|
2017-12-09 21:42:07 +00:00
|
|
|
}
|
2019-12-03 05:00:11 +00:00
|
|
|
err = r.GenerateId(tx)
|
2017-01-28 03:16:47 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2019-12-03 05:00:11 +00:00
|
|
|
tx.Rollback()
|
|
|
|
return err
|
2017-01-28 03:16:47 +00:00
|
|
|
}
|
2018-12-15 21:42:32 +00:00
|
|
|
processing := false
|
|
|
|
if r.SendDate.Before(c.CreatedDate) || r.SendDate.Equal(c.CreatedDate) {
|
2018-12-16 03:38:51 +00:00
|
|
|
r.Status = StatusSending
|
2018-12-15 21:42:32 +00:00
|
|
|
processing = true
|
|
|
|
}
|
2019-12-03 05:00:11 +00:00
|
|
|
err = tx.Save(r).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"email": t.Email,
|
2019-12-03 05:00:11 +00:00
|
|
|
}).Errorf("error creating result: %v", err)
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
2016-02-02 02:42:46 +00:00
|
|
|
c.Results = append(c.Results, *r)
|
2019-12-03 05:00:11 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"email": r.Email,
|
|
|
|
"send_date": sendDate,
|
|
|
|
}).Debug("creating maillog")
|
2018-12-15 21:42:32 +00:00
|
|
|
m := &MailLog{
|
|
|
|
UserId: c.UserId,
|
|
|
|
CampaignId: c.Id,
|
|
|
|
RId: r.RId,
|
|
|
|
SendDate: sendDate,
|
|
|
|
Processing: processing,
|
|
|
|
}
|
2019-12-03 05:00:11 +00:00
|
|
|
err = tx.Save(m).Error
|
2017-12-09 21:42:07 +00:00
|
|
|
if err != nil {
|
2019-12-03 05:00:11 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"email": t.Email,
|
|
|
|
}).Errorf("error creating maillog entry: %v", err)
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
2017-12-09 21:42:07 +00:00
|
|
|
}
|
2018-09-02 16:17:52 +00:00
|
|
|
recipientIndex++
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-03 05:00:11 +00:00
|
|
|
return tx.Commit().Error
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
|
2024-09-22 14:47:23 +00:00
|
|
|
// DeleteCampaign deletes the specified campaign
|
2014-03-25 03:31:33 +00:00
|
|
|
func DeleteCampaign(id int64) error {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"campaign_id": id,
|
|
|
|
}).Info("Deleting campaign")
|
2014-03-26 20:01:49 +00:00
|
|
|
// Delete all the campaign results
|
2014-05-27 21:13:30 +00:00
|
|
|
err := db.Where("campaign_id=?", id).Delete(&Result{}).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-03-25 03:31:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-07-24 02:04:38 +00:00
|
|
|
err = db.Where("campaign_id=?", id).Delete(&Event{}).Error
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-07-24 02:04:38 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-12-16 03:47:40 +00:00
|
|
|
err = db.Where("campaign_id=?", id).Delete(&MailLog{}).Error
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
2014-03-26 20:01:49 +00:00
|
|
|
// Delete the campaign
|
|
|
|
err = db.Delete(&Campaign{Id: id}).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2016-07-12 03:11:40 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteCampaign effectively "ends" a campaign.
|
|
|
|
// Any future emails clicked will return a simple "404" page.
|
|
|
|
func CompleteCampaign(id int64, uid int64) error {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"campaign_id": id,
|
|
|
|
}).Info("Marking campaign as complete")
|
2016-07-12 03:11:40 +00:00
|
|
|
c, err := GetCampaign(id, uid)
|
|
|
|
if err != nil {
|
2014-03-25 03:31:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-12-16 04:09:15 +00:00
|
|
|
// Delete any maillogs still set to be sent out, preventing future emails
|
|
|
|
err = db.Where("campaign_id=?", id).Delete(&MailLog{}).Error
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
2016-07-12 03:11:40 +00:00
|
|
|
// Don't overwrite original completed time
|
2018-12-16 03:38:51 +00:00
|
|
|
if c.Status == CampaignComplete {
|
2016-07-12 03:11:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Mark the campaign as complete
|
2017-08-29 03:48:49 +00:00
|
|
|
c.CompletedDate = time.Now().UTC()
|
2018-12-16 03:38:51 +00:00
|
|
|
c.Status = CampaignComplete
|
2022-10-13 15:16:37 +00:00
|
|
|
err = db.Model(&Campaign{}).Where("id=? and user_id=?", id, uid).
|
|
|
|
Select([]string{"completed_date", "status"}).UpdateColumns(&c).Error
|
2016-07-12 03:11:40 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2016-07-12 03:11:40 +00:00
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
return err
|
|
|
|
}
|