1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-18 10:07:02 +00:00

Modify smtpd to integrate with Revel.

Add plugin to app.go to start smtpd
This commit is contained in:
James Hillyerd
2012-10-07 19:12:58 -07:00
parent cbba067673
commit 9fa93acf0e
4 changed files with 39 additions and 12 deletions

View File

@@ -1,6 +1,9 @@
package controllers package controllers
import "github.com/robfig/revel" import (
"github.com/jhillyerd/inbucket/app/smtpd"
"github.com/robfig/revel"
)
type Application struct { type Application struct {
*rev.Controller *rev.Controller
@@ -9,3 +12,20 @@ type Application struct {
func (c Application) Index() rev.Result { func (c Application) Index() rev.Result {
return c.Render() return c.Render()
} }
type SmtpdPlugin struct {
rev.EmptyPlugin
server *smtpd.Server
}
func (p SmtpdPlugin) OnAppStart() {
domain := rev.Config.StringDefault("smtpd.domain", "localhost")
port := rev.Config.IntDefault("smtpd.port", 2500)
rev.INFO.Printf("SMTP Daemon plugin init {domain: %v, port: %v}", domain, port)
p.server = smtpd.New(domain, port)
go p.server.Start()
}
func init() {
rev.RegisterPlugin(SmtpdPlugin{})
}

View File

@@ -56,6 +56,7 @@ type Session struct {
server *Server server *Server
id int id int
conn net.Conn conn net.Conn
remoteHost string
sendError error sendError error
state State state State
reader *bufio.Reader reader *bufio.Reader
@@ -65,7 +66,8 @@ type Session struct {
func NewSession(server *Server, id int, conn net.Conn) *Session { func NewSession(server *Server, id int, conn net.Conn) *Session {
reader := bufio.NewReader(conn) reader := bufio.NewReader(conn)
return &Session{server: server, id: id, conn: conn, state: GREET, reader: reader} host, _, _ := net.SplitHostPort(conn.RemoteAddr().String())
return &Session{server: server, id: id, conn: conn, state: GREET, reader: reader, remoteHost: host}
} }
func (ss *Session) String() string { func (ss *Session) String() string {
@@ -80,7 +82,7 @@ func (ss *Session) String() string {
* 5. Goto 2 * 5. Goto 2
*/ */
func (s *Server) startSession(id int, conn net.Conn) { func (s *Server) startSession(id int, conn net.Conn) {
s.trace("Starting session <%v>", id) s.info("Connection from %v, starting session <%v>", conn.RemoteAddr(), id)
defer conn.Close() defer conn.Close()
ss := NewSession(s, id, conn) ss := NewSession(s, id, conn)
@@ -343,17 +345,17 @@ func (ss *Session) ooSeq(cmd string) {
// Session specific logging methods // Session specific logging methods
func (ss *Session) trace(msg string, args ...interface{}) { func (ss *Session) trace(msg string, args ...interface{}) {
ss.server.trace("<%v> %v", ss.id, fmt.Sprintf(msg, args...)) ss.server.trace("%v<%v> %v", ss.remoteHost, ss.id, fmt.Sprintf(msg, args...))
} }
func (ss *Session) info(msg string, args ...interface{}) { func (ss *Session) info(msg string, args ...interface{}) {
ss.server.info("<%v> %v", ss.id, fmt.Sprintf(msg, args...)) ss.server.info("%v<%v> %v", ss.remoteHost, ss.id, fmt.Sprintf(msg, args...))
} }
func (ss *Session) warn(msg string, args ...interface{}) { func (ss *Session) warn(msg string, args ...interface{}) {
ss.server.warn("<%v> %v", ss.id, fmt.Sprintf(msg, args...)) ss.server.warn("%v<%v> %v", ss.remoteHost, ss.id, fmt.Sprintf(msg, args...))
} }
func (ss *Session) error(msg string, args ...interface{}) { func (ss *Session) error(msg string, args ...interface{}) {
ss.server.error("<%v> %v", ss.id, fmt.Sprintf(msg, args...)) ss.server.error("%v<%v> %v", ss.remoteHost, ss.id, fmt.Sprintf(msg, args...))
} }

View File

@@ -3,6 +3,7 @@ package smtpd
import ( import (
"fmt" "fmt"
"net" "net"
"github.com/robfig/revel"
) )
// Real server code starts here // Real server code starts here
@@ -15,24 +16,24 @@ type Server struct {
// Init a new Server object // Init a new Server object
func New(domain string, port int) *Server { func New(domain string, port int) *Server {
return &Server{domain: domain, port: port, maxRecips: 3, maxIdleSeconds: 10} return &Server{domain: domain, port: port, maxRecips: 100, maxIdleSeconds: 60}
} }
// Loggers // Loggers
func (s *Server) trace(msg string, args ...interface{}) { func (s *Server) trace(msg string, args ...interface{}) {
fmt.Printf("[trace] %s\n", fmt.Sprintf(msg, args...)) rev.TRACE.Printf(msg, args...)
} }
func (s *Server) info(msg string, args ...interface{}) { func (s *Server) info(msg string, args ...interface{}) {
fmt.Printf("[info ] %s\n", fmt.Sprintf(msg, args...)) rev.INFO.Printf(msg, args...)
} }
func (s *Server) warn(msg string, args ...interface{}) { func (s *Server) warn(msg string, args ...interface{}) {
fmt.Printf("[warn ] %s\n", fmt.Sprintf(msg, args...)) rev.WARN.Printf(msg, args...)
} }
func (s *Server) error(msg string, args ...interface{}) { func (s *Server) error(msg string, args ...interface{}) {
fmt.Printf("[error] %s\n", fmt.Sprintf(msg, args...)) rev.ERROR.Printf(msg, args...)
} }
// Main listener loop // Main listener loop

View File

@@ -6,6 +6,8 @@ http.port=9000
[dev] [dev]
results.pretty=true results.pretty=true
server.watcher=true server.watcher=true
smtpd.domain=skynet
smtpd.port=2500
log.trace.output = off log.trace.output = off
log.info.output = stderr log.info.output = stderr
@@ -15,6 +17,8 @@ log.error.output = stderr
[prod] [prod]
results.pretty=false results.pretty=false
server.watcher=false server.watcher=false
smtpd.domain=skynet
smtpd.port=2500
log.trace.output = off log.trace.output = off
log.info.output = off log.info.output = off