diff --git a/common/email.go b/common/email.go index 6cbb3869..833617ae 100644 --- a/common/email.go +++ b/common/email.go @@ -5,14 +5,20 @@ import ( "fmt" "net/mail" "net/smtp" + "strings" + + p "github.com/Azareal/Gosora/common/phrases" ) -type Email struct { - UserID int - Email string - Validated bool - Primary bool - Token string +func SendActivationEmail(username string, email string, token string) error { + schema := "http" + if Site.EnableSsl { + schema += "s" + } + // TODO: Move these to the phrase system + subject := "Account Activation - " + Site.Name + msg := "Dear " + username + ", to complete your registration on our forums, we need you to validate your email, so that we can confirm that this email actually belongs to you.\n\nClick on the following link to do so. " + schema + "://" + Site.URL + "/user/edit/token/" + token + "\n\nIf you haven't created an account here, then please feel free to ignore this email.\nWe're sorry for the inconvenience this may have caused." + return SendEmail(email, subject, msg) } func SendValidationEmail(username string, email string, token string) error { @@ -20,11 +26,21 @@ func SendValidationEmail(username string, email string, token string) error { if Site.EnableSsl { schema += "s" } - - // TODO: Move these to the phrase system - subject := "Validate Your Email - " + Site.Name - msg := "Dear " + username + ", to complete your registration on our forums, we need you to validate your email, so that we can confirm that this email actually belongs to you.\n\nClick on the following link to do so. " + schema + "://" + Site.URL + "/user/edit/token/" + token + "\n\nIf you haven't created an account here, then please feel free to ignore this email.\nWe're sorry for the inconvenience this may have caused." - return SendEmail(email, subject, msg) + r := func(body *string) func(name, val string) { + return func(name, val string) { + *body = strings.Replace(*body,"{{"+name+"}}",val,-1) + } + } + subject := p.GetAccountPhrase("ValidateEmailSubject") + r1 := r(&subject) + r1("name",Site.Name) + body := p.GetAccountPhrase("ValidateEmailBody") + r2 := r(&body) + r2("username",username) + r2("schema",schema) + r2("url",Site.URL) + r2("token",token) + return SendEmail(email, subject, body) } // TODO: Refactor this @@ -60,16 +76,12 @@ func SendEmail(email string, subject string, msg string) (err error) { return err } c, err = smtp.NewClient(conn, Config.SMTPServer) - if err != nil { - LogWarning(err) - return err - } } else { c, err = smtp.Dial(Config.SMTPServer + ":" + Config.SMTPPort) - if err != nil { - LogWarning(err) - return err - } + } + if err != nil { + LogWarning(err) + return err } if Config.SMTPUsername != "" { @@ -80,14 +92,11 @@ func SendEmail(email string, subject string, msg string) (err error) { return err } } - - err = c.Mail(from.Address) - if err != nil { + if err = c.Mail(from.Address); err != nil { LogWarning(err) return err } - err = c.Rcpt(to.Address) - if err != nil { + if err = c.Rcpt(to.Address); err != nil { LogWarning(err) return err } @@ -102,14 +111,11 @@ func SendEmail(email string, subject string, msg string) (err error) { LogWarning(err) return err } - - err = w.Close() - if err != nil { + if err = w.Close(); err != nil { LogWarning(err) return err } - err = c.Quit() - if err != nil { + if err = c.Quit(); err != nil { LogWarning(err) return err } diff --git a/common/email_store.go b/common/email_store.go index db7c132d..4e75217f 100644 --- a/common/email_store.go +++ b/common/email_store.go @@ -8,25 +8,50 @@ import ( var Emails EmailStore +type Email struct { + UserID int + Email string + Validated bool + Primary bool + Token string +} + type EmailStore interface { + // TODO: Add an autoincrement key + Get(user *User, email string) (Email, error) GetEmailsByUser(user *User) (emails []Email, err error) + Add(uid int, email, token string) error + Delete(uid int, email string) error VerifyEmail(email string) error } type DefaultEmailStore struct { + get *sql.Stmt getEmailsByUser *sql.Stmt + add *sql.Stmt + delete *sql.Stmt verifyEmail *sql.Stmt } func NewDefaultEmailStore(acc *qgen.Accumulator) (*DefaultEmailStore, error) { + e := "emails" return &DefaultEmailStore{ - getEmailsByUser: acc.Select("emails").Columns("email,validated,token").Where("uid=?").Prepare(), + get: acc.Select(e).Columns("email,validated,token").Where("uid=? AND email=?").Prepare(), + getEmailsByUser: acc.Select(e).Columns("email,validated,token").Where("uid=?").Prepare(), + add: acc.Insert(e).Columns("uid,email,validated,token").Fields("?,?,?,?").Prepare(), + delete: acc.Delete(e).Where("uid=? AND email=?").Prepare(), // Need to fix this: Empty string isn't working, it gets set to 1 instead x.x -- Has this been fixed? - verifyEmail: acc.Update("emails").Set("validated = 1, token = ''").Where("email = ?").Prepare(), + verifyEmail: acc.Update(e).Set("validated=1,token=''").Where("email=?").Prepare(), }, acc.FirstError() } +func (s *DefaultEmailStore) Get(user *User, email string) (Email, error) { + e := Email{UserID:user.ID, Primary:email !="" && user.Email==email} + err := s.get.QueryRow(user.ID, email).Scan(&e.Email, &e.Validated, &e.Token) + return e, err +} + func (s *DefaultEmailStore) GetEmailsByUser(user *User) (emails []Email, err error) { e := Email{UserID: user.ID} rows, err := s.getEmailsByUser.Query(user.ID) @@ -34,13 +59,11 @@ func (s *DefaultEmailStore) GetEmailsByUser(user *User) (emails []Email, err err return emails, err } defer rows.Close() - for rows.Next() { err := rows.Scan(&e.Email, &e.Validated, &e.Token) if err != nil { return emails, err } - if e.Email == user.Email { e.Primary = true } @@ -49,6 +72,16 @@ func (s *DefaultEmailStore) GetEmailsByUser(user *User) (emails []Email, err err return emails, rows.Err() } +func (s *DefaultEmailStore) Add(uid int, email string, token string) error { + _, err := s.add.Exec(uid, email, 0, token) + return err +} + +func (s *DefaultEmailStore) Delete(uid int, email string) error { + _, err := s.delete.Exec(uid,email) + return err +} + func (s *DefaultEmailStore) VerifyEmail(email string) error { _, err := s.verifyEmail.Exec(email) return err diff --git a/common/widget.go b/common/widget.go index e721b66a..88c33630 100644 --- a/common/widget.go +++ b/common/widget.go @@ -16,18 +16,22 @@ type WidgetStmts struct { delete *sql.Stmt create *sql.Stmt update *sql.Stmt + + //qgen.SimpleModel } var widgetStmts WidgetStmts func init() { DbInits.Add(func(acc *qgen.Accumulator) error { + w := "widgets" widgetStmts = WidgetStmts{ - //getList: acc.Select("widgets").Columns("wid, position, side, type, active, location, data").Orderby("position ASC").Prepare(), - getDockList: acc.Select("widgets").Columns("wid, position, type, active, location, data").Where("side = ?").Orderby("position ASC").Prepare(), - delete: acc.Delete("widgets").Where("wid = ?").Prepare(), - create: acc.Insert("widgets").Columns("position, side, type, active, location, data").Fields("?,?,?,?,?,?").Prepare(), - update: acc.Update("widgets").Set("position = ?, side = ?, type = ?, active = ?, location = ?, data = ?").Where("wid = ?").Prepare(), + //getList: acc.Select(w).Columns("wid, position, side, type, active, location, data").Orderby("position ASC").Prepare(), + getDockList: acc.Select(w).Columns("wid, position, type, active, location, data").Where("side = ?").Orderby("position ASC").Prepare(), + //model: acc.SimpleModel(w,"position,type,active,location,data","wid"), + delete: acc.Delete(w).Where("wid = ?").Prepare(), + create: acc.Insert(w).Columns("position, side, type, active, location, data").Fields("?,?,?,?,?,?").Prepare(), + update: acc.Update(w).Set("position = ?, side = ?, type = ?, active = ?, location = ?, data = ?").Where("wid = ?").Prepare(), } return acc.FirstError() }) @@ -111,7 +115,7 @@ func (widget *Widget) Build(hvars interface{}) (string, error) { return widget.BuildFunc(widget, hvars) } - var header = hvars.(*Header) + header := hvars.(*Header) err := header.Theme.RunTmpl(widget.Body, hvars, header.Writer) return "", err } diff --git a/langs/english.json b/langs/english.json index eeea400a..43e5d6f7 100644 --- a/langs/english.json +++ b/langs/english.json @@ -67,8 +67,11 @@ }, "Accounts": { - "VerifyEmailSubject": "Validate Your Email @ {{name}}", - "VerifyEmailBody": "Dear {{username}}, following your registration on our forums, we ask you to validate your email, so that we can confirm that this email actually belongs to you.\n\nClick on the following link to do so. {{schema}}://{{url}}/user/edit/token/{{token}}\n\nIf you haven't created an account here, then please feel free to ignore this email.\nWe're sorry for the inconvenience this may have caused." + "ActivateEmailSubject": "Validate Your Email - {{name}}", + "ActivateEmailBody": "Dear {{username}}, following your registration on our forums, we ask you to validate your email, so that we can confirm that this email actually belongs to you.\n\nClick on the following link to do so. {{schema}}://{{url}}/user/edit/token/{{token}}\n\nIf you haven't created an account here, then please feel free to ignore this email.\nWe're sorry for the inconvenience this may have caused.", + + "ValidateEmailSubject":"Validate Your Email - {{name}}", + "ValidateEmailBody":"Dear {{username}}, to receive emails from our site on this address, we need you to confirm that this email address actually belongs to you.\n\nPlease click on the following link to do so: {{schema}}://{{url}}/user/edit/token/{{token}}\n\nIf you're not a user of our site, then please ignore this email." }, "Errors": { @@ -440,7 +443,7 @@ "panel_delete_button_text":"Delete", "menu_forums_tooltip":"Forum List", - "menu_forums_aria":"The Forum list", + "menu_forums_aria":"The forum list", "menu_topics_tooltip":"Topic List", "menu_topics_aria":"The topic list", "menu_alert_counter_aria":"The number of alerts",