diff options
author | slene <vslene@gmail.com> | 2014-03-20 09:17:24 +0800 |
---|---|---|
committer | slene <vslene@gmail.com> | 2014-03-20 09:17:24 +0800 |
commit | 8b0f421eb504075b7500575515282cd7da8b0878 (patch) | |
tree | e10c71d7fff99207540bac25437165e0aa4da7ac /modules | |
parent | 0e7a2d9d3c59a67e9681d8e6e0d813c191777e2c (diff) | |
parent | 6f6862086047ba6902f51de3cb66eb3af04fffbd (diff) |
Merge branch 'master' of github.com:gogits/gogs
Diffstat (limited to 'modules')
-rw-r--r-- | modules/base/conf.go | 15 | ||||
-rw-r--r-- | modules/base/template.go | 7 | ||||
-rw-r--r-- | modules/base/tool.go | 65 | ||||
-rw-r--r-- | modules/mailer/mail.go | 14 | ||||
-rw-r--r-- | modules/mailer/mailer.go | 44 | ||||
-rw-r--r-- | modules/middleware/auth.go | 10 |
6 files changed, 114 insertions, 41 deletions
diff --git a/modules/base/conf.go b/modules/base/conf.go index 64c97028..17ba3b87 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -91,9 +91,11 @@ func newLogService() { case "console": config = fmt.Sprintf(`{"level":%s}`, level) case "file": + logPath := Cfg.MustValue(modeSec, "FILE_NAME", "log/gogs.log") + os.MkdirAll(path.Dir(logPath), os.ModePerm) config = fmt.Sprintf( `{"level":%s,"filename":%s,"rotate":%v,"maxlines":%d,"maxsize",%d,"daily":%v,"maxdays":%d}`, level, - Cfg.MustValue(modeSec, "FILE_NAME", "log/gogs.log"), + logPath, Cfg.MustBool(modeSec, "LOG_ROTATE", true), Cfg.MustInt(modeSec, "MAX_LINES", 1000000), 1<<uint(Cfg.MustInt(modeSec, "MAX_SIZE_SHIFT", 28)), @@ -131,15 +133,15 @@ func newMailService() { } } -func newRegisterService() { +func newRegisterMailService() { if !Cfg.MustBool("service", "REGISTER_EMAIL_CONFIRM") { return } else if MailService == nil { - log.Warn("Register Service: Mail Service is not enabled") + log.Warn("Register Mail Service: Mail Service is not enabled") return } Service.RegisterEmailConfirm = true - log.Info("Register Service Enabled") + log.Info("Register Mail Service Enabled") } func init() { @@ -172,10 +174,11 @@ func init() { AppUrl = Cfg.MustValue("server", "ROOT_URL") Domain = Cfg.MustValue("server", "DOMAIN") SecretKey = Cfg.MustValue("security", "SECRET_KEY") +} - // Extensions. +func NewServices() { newService() newLogService() newMailService() - newRegisterService() + newRegisterMailService() } diff --git a/modules/base/template.go b/modules/base/template.go index 23d3d277..e596d1da 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -8,6 +8,7 @@ import ( "container/list" "fmt" "html/template" + "strings" "time" ) @@ -54,4 +55,10 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ "ActionDesc": ActionDesc, "DateFormat": DateFormat, "List": List, + "Mail2Domain": func(mail string) string { + return "mail." + strings.Split(mail, "@")[1] + }, + "SubStr": func(str string, start, length int) string { + return str[start : start+length] + }, } diff --git a/modules/base/tool.go b/modules/base/tool.go index d0b6bfbf..8fabb8c5 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -36,6 +36,35 @@ func GetRandomString(n int) string { return string(bytes) } +// verify time limit code +func VerifyTimeLimitCode(data string, minutes int, code string) bool { + if len(code) <= 18 { + return false + } + + // split code + start := code[:12] + lives := code[12:18] + if d, err := StrTo(lives).Int(); err == nil { + minutes = d + } + + // right active code + retCode := CreateTimeLimitCode(data, minutes, start) + if retCode == code && minutes > 0 { + // check time is expired or not + before, _ := DateParse(start, "YmdHi") + now := time.Now() + if before.Add(time.Minute*time.Duration(minutes)).Unix() > now.Unix() { + return true + } + } + + return false +} + +const TimeLimitCodeLength = 12 + 6 + 40 + // create a time limit code // code format: 12 length date time string + 6 minutes string + 40 sha1 encoded string func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string { @@ -283,16 +312,24 @@ func DateFormat(t time.Time, format string) string { return t.Format(format) } -type argInt []int +// convert string to specify type -func (a argInt) Get(i int, args ...int) (r int) { - if i >= 0 && i < len(a) { - r = a[i] - } - if len(args) > 0 { - r = args[0] +type StrTo string + +func (f StrTo) Exist() bool { + return string(f) != string(0x1E) +} + +func (f StrTo) Int() (int, error) { + v, err := strconv.ParseInt(f.String(), 10, 32) + return int(v), err +} + +func (f StrTo) String() string { + if f.Exist() { + return string(f) } - return + return "" } // convert any type to string @@ -334,6 +371,18 @@ func ToStr(value interface{}, args ...int) (s string) { return s } +type argInt []int + +func (a argInt) Get(i int, args ...int) (r int) { + if i >= 0 && i < len(a) { + r = a[i] + } + if len(args) > 0 { + r = args[0] + } + return +} + type Actioner interface { GetOpType() int GetActUserName() string diff --git a/modules/mailer/mail.go b/modules/mailer/mail.go index de4f24a4..92acd20e 100644 --- a/modules/mailer/mail.go +++ b/modules/mailer/mail.go @@ -37,9 +37,9 @@ func GetMailTmplData(user *models.User) map[interface{}]interface{} { // create a time limit code for user active func CreateUserActiveCode(user *models.User, startInf interface{}) string { - hours := base.Service.ActiveCodeLives / 60 + minutes := base.Service.ActiveCodeLives data := base.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands - code := base.CreateTimeLimitCode(data, hours, startInf) + code := base.CreateTimeLimitCode(data, minutes, startInf) // add tail hex username code += hex.EncodeToString([]byte(user.LowerName)) @@ -62,19 +62,18 @@ func SendRegisterMail(r *middleware.Render, user *models.User) { msg := NewMailMessage([]string{user.Email}, subject, body) msg.Info = fmt.Sprintf("UID: %d, send register mail", user.Id) - // async send mail - SendAsync(msg) + SendAsync(&msg) } // Send email verify active email. func SendActiveMail(r *middleware.Render, user *models.User) { code := CreateUserActiveCode(user, nil) - subject := "Verify your email address" + subject := "Verify your e-mail address" data := GetMailTmplData(user) data["Code"] = code - body, err := r.HTMLString("mail/auth/active_email.html", data) + body, err := r.HTMLString("mail/auth/active_email", data) if err != nil { log.Error("mail.SendActiveMail(fail to render): %v", err) return @@ -83,6 +82,5 @@ func SendActiveMail(r *middleware.Render, user *models.User) { msg := NewMailMessage([]string{user.Email}, subject, body) msg.Info = fmt.Sprintf("UID: %d, send email verify mail", user.Id) - // async send mail - SendAsync(msg) + SendAsync(&msg) } diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go index cc76acb2..3823e01f 100644 --- a/modules/mailer/mailer.go +++ b/modules/mailer/mailer.go @@ -38,8 +38,34 @@ func (m Message) Content() string { return content } +var mailQueue chan *Message + +func init() { + mailQueue = make(chan *Message, base.Cfg.MustInt("mailer", "SEND_BUFFER_LEN", 10)) + go processMailQueue() +} + +func processMailQueue() { + for { + select { + case msg := <-mailQueue: + num, err := Send(msg) + tos := strings.Join(msg.To, "; ") + info := "" + if err != nil { + if len(msg.Info) > 0 { + info = ", info: " + msg.Info + } + log.Error(fmt.Sprintf("Async sent email %d succeed, not send emails: %s%s err: %s", num, tos, info, err)) + return + } + log.Trace(fmt.Sprintf("Async sent email %d succeed, sent emails: %s%s", num, tos, info)) + } + } +} + // Direct Send mail message -func Send(msg Message) (int, error) { +func Send(msg *Message) (int, error) { log.Trace("Sending mails to: %s", strings.Join(msg.To, "; ")) host := strings.Split(base.MailService.Host, ":") @@ -82,21 +108,9 @@ func Send(msg Message) (int, error) { } // Async Send mail message -func SendAsync(msg Message) { - // TODO may be need pools limit concurrent nums +func SendAsync(msg *Message) { go func() { - num, err := Send(msg) - tos := strings.Join(msg.To, "; ") - info := "" - if err != nil { - if len(msg.Info) > 0 { - info = ", info: " + msg.Info - } - // log failed - log.Error(fmt.Sprintf("Async sent email %d succeed, not send emails: %s%s err: %s", num, tos, info, err)) - return - } - log.Trace(fmt.Sprintf("Async sent email %d succeed, sent emails: %s%s", num, tos, info)) + mailQueue <- msg }() } diff --git a/modules/middleware/auth.go b/modules/middleware/auth.go index ed847dd8..d45a21e9 100644 --- a/modules/middleware/auth.go +++ b/modules/middleware/auth.go @@ -6,6 +6,8 @@ package middleware import ( "github.com/codegangsta/martini" + + "github.com/gogits/gogs/modules/base" ) // SignInRequire requires user to sign in. @@ -16,10 +18,10 @@ func SignInRequire(redirect bool) martini.Handler { ctx.Redirect("/") } return - } else if !ctx.User.IsActive { - // ctx.Data["Title"] = "Activate Your Account" - // ctx.Render.HTML(200, "user/active", ctx.Data) - // return + } else if !ctx.User.IsActive && base.Service.RegisterEmailConfirm { + ctx.Data["Title"] = "Activate Your Account" + ctx.Render.HTML(200, "user/active", ctx.Data) + return } } } |