mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 18:17:03 +00:00
Package reorganization part 2
End goal is to simplify build
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package inbucket
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/list"
|
"container/list"
|
||||||
@@ -6,7 +6,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jhillyerd/inbucket"
|
"github.com/jhillyerd/inbucket/config"
|
||||||
"github.com/jhillyerd/inbucket/smtpd"
|
"github.com/jhillyerd/inbucket/smtpd"
|
||||||
"github.com/jhillyerd/inbucket/web"
|
"github.com/jhillyerd/inbucket/web"
|
||||||
"os"
|
"os"
|
||||||
@@ -26,7 +26,7 @@ func main() {
|
|||||||
flag.Usage()
|
flag.Usage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
err := inbucket.LoadConfig(flag.Arg(0))
|
err := config.LoadConfig(flag.Arg(0))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Failed to parse config: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Failed to parse config: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jhillyerd/inbucket"
|
"github.com/jhillyerd/inbucket/config"
|
||||||
"github.com/jhillyerd/inbucket/log"
|
"github.com/jhillyerd/inbucket/log"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
@@ -42,7 +42,7 @@ type DataStore struct {
|
|||||||
// NewDataStore creates a new DataStore object. It uses the inbucket.Config object to
|
// NewDataStore creates a new DataStore object. It uses the inbucket.Config object to
|
||||||
// construct it's path.
|
// construct it's path.
|
||||||
func NewDataStore() *DataStore {
|
func NewDataStore() *DataStore {
|
||||||
path, err := inbucket.Config.String("datastore", "path")
|
path, err := config.Config.String("datastore", "path")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error getting datastore path: %v", err)
|
log.Error("Error getting datastore path: %v", err)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package smtpd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jhillyerd/inbucket"
|
"github.com/jhillyerd/inbucket/config"
|
||||||
"github.com/jhillyerd/inbucket/log"
|
"github.com/jhillyerd/inbucket/log"
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
@@ -20,13 +20,13 @@ type Server struct {
|
|||||||
func New() *Server {
|
func New() *Server {
|
||||||
ds := NewDataStore()
|
ds := NewDataStore()
|
||||||
// TODO Make more of these configurable
|
// TODO Make more of these configurable
|
||||||
return &Server{domain: inbucket.GetSmtpConfig().Domain, maxRecips: 100, maxIdleSeconds: 300,
|
return &Server{domain: config.GetSmtpConfig().Domain, maxRecips: 100, maxIdleSeconds: 300,
|
||||||
dataStore: ds, maxMessageBytes: 2048000}
|
dataStore: ds, maxMessageBytes: 2048000}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main listener loop
|
// Main listener loop
|
||||||
func (s *Server) Start() {
|
func (s *Server) Start() {
|
||||||
cfg := inbucket.GetSmtpConfig()
|
cfg := config.GetSmtpConfig()
|
||||||
addr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%v:%v",
|
addr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%v:%v",
|
||||||
cfg.Ip4address, cfg.Ip4port))
|
cfg.Ip4address, cfg.Ip4port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package smtpd
|
|||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -26,11 +25,3 @@ func HashMailboxName(mailbox string) string {
|
|||||||
io.WriteString(h, mailbox)
|
io.WriteString(h, mailbox)
|
||||||
return fmt.Sprintf("%x", h.Sum(nil))
|
return fmt.Sprintf("%x", h.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TextToHtml takes plain text, escapes it and tries to pretty it up for
|
|
||||||
// HTML display
|
|
||||||
func TextToHtml(text string) string {
|
|
||||||
text = html.EscapeString(text)
|
|
||||||
replacer := strings.NewReplacer("\r\n", "<br/>\n", "\r", "<br/>\n", "\n", "<br/>\n")
|
|
||||||
return replacer.Replace(text)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,38 +6,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestParseMailboxName(t *testing.T) {
|
func TestParseMailboxName(t *testing.T) {
|
||||||
in, out := "MailBOX", "mailbox"
|
assert.Equal(t, ParseMailboxName("MailBOX"), "mailbox")
|
||||||
if x := ParseMailboxName(in); x != out {
|
assert.Equal(t, ParseMailboxName("MailBox@Host.Com"), "mailbox")
|
||||||
t.Errorf("ParseMailboxName(%v) = %v, want %v", in, x, out)
|
assert.Equal(t, ParseMailboxName("Mail+extra@Host.Com"), "mail")
|
||||||
}
|
|
||||||
|
|
||||||
in, out = "MailBox@Host.Com", "mailbox"
|
|
||||||
if x := ParseMailboxName(in); x != out {
|
|
||||||
t.Errorf("ParseMailboxName(%v) = %v, want %v", in, x, out)
|
|
||||||
}
|
|
||||||
|
|
||||||
in, out = "Mail+extra@Host.Com", "mail"
|
|
||||||
if x := ParseMailboxName(in); x != out {
|
|
||||||
t.Errorf("ParseMailboxName(%v) = %v, want %v", in, x, out)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHashMailboxName(t *testing.T) {
|
func TestHashMailboxName(t *testing.T) {
|
||||||
in, out := "mail", "1d6e1cf70ec6f9ab28d3ea4b27a49a77654d370e"
|
assert.Equal(t, HashMailboxName("mail"), "1d6e1cf70ec6f9ab28d3ea4b27a49a77654d370e")
|
||||||
if x := HashMailboxName(in); x != out {
|
|
||||||
t.Errorf("HashMailboxName(%v) = %v, want %v", in, x, out)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTextToHtml(t *testing.T) {
|
|
||||||
// Identity
|
|
||||||
assert.Equal(t, TextToHtml("html"), "html")
|
|
||||||
|
|
||||||
// Check it escapes
|
|
||||||
assert.Equal(t, TextToHtml("<html>"), "<html>")
|
|
||||||
|
|
||||||
// Check for linebreaks
|
|
||||||
assert.Equal(t, TextToHtml("line\nbreak"), "line<br/>\nbreak")
|
|
||||||
assert.Equal(t, TextToHtml("line\r\nbreak"), "line<br/>\nbreak")
|
|
||||||
assert.Equal(t, TextToHtml("line\rbreak"), "line<br/>\nbreak")
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,24 +3,29 @@ package web
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jhillyerd/inbucket/log"
|
"github.com/jhillyerd/inbucket/log"
|
||||||
|
"html"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var TemplateFuncs = template.FuncMap{
|
var TemplateFuncs = template.FuncMap{
|
||||||
// Reversable routing function (shared with templates)
|
"friendlyTime": friendlyTime,
|
||||||
"reverse": reverse,
|
"reverse": reverse,
|
||||||
// Friendly date & time rendering
|
"textToHtml": textToHtml,
|
||||||
"friendlyTime": func(t time.Time) template.HTML {
|
}
|
||||||
|
|
||||||
|
// Friendly date & time rendering
|
||||||
|
func friendlyTime(t time.Time) template.HTML {
|
||||||
ty, tm, td := t.Date()
|
ty, tm, td := t.Date()
|
||||||
ny, nm, nd := time.Now().Date()
|
ny, nm, nd := time.Now().Date()
|
||||||
if (ty == ny) && (tm == nm) && (td == nd) {
|
if (ty == ny) && (tm == nm) && (td == nd) {
|
||||||
return template.HTML(t.Format("03:04:05 PM"))
|
return template.HTML(t.Format("03:04:05 PM"))
|
||||||
}
|
}
|
||||||
return template.HTML(t.Format("Mon Jan 2, 2006"))
|
return template.HTML(t.Format("Mon Jan 2, 2006"))
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reversable routing function (shared with templates)
|
||||||
func reverse(name string, things ...interface{}) string {
|
func reverse(name string, things ...interface{}) string {
|
||||||
// Convert the things to strings
|
// Convert the things to strings
|
||||||
strs := make([]string, len(things))
|
strs := make([]string, len(things))
|
||||||
@@ -35,3 +40,11 @@ func reverse(name string, things ...interface{}) string {
|
|||||||
}
|
}
|
||||||
return u.Path
|
return u.Path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// textToHtml takes plain text, escapes it and tries to pretty it up for
|
||||||
|
// HTML display
|
||||||
|
func textToHtml(text string) template.HTML {
|
||||||
|
text = html.EscapeString(text)
|
||||||
|
replacer := strings.NewReplacer("\r\n", "<br/>\n", "\r", "<br/>\n", "\n", "<br/>\n")
|
||||||
|
return template.HTML(replacer.Replace(text))
|
||||||
|
}
|
||||||
|
|||||||
19
web/helpers_test.go
Normal file
19
web/helpers_test.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchrcom/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTextToHtml(t *testing.T) {
|
||||||
|
// Identity
|
||||||
|
assert.Equal(t, textToHtml("html"), "html")
|
||||||
|
|
||||||
|
// Check it escapes
|
||||||
|
assert.Equal(t, textToHtml("<html>"), "<html>")
|
||||||
|
|
||||||
|
// Check for linebreaks
|
||||||
|
assert.Equal(t, textToHtml("line\nbreak"), "line<br/>\nbreak")
|
||||||
|
assert.Equal(t, textToHtml("line\r\nbreak"), "line<br/>\nbreak")
|
||||||
|
assert.Equal(t, textToHtml("line\rbreak"), "line<br/>\nbreak")
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jhillyerd/inbucket"
|
|
||||||
"github.com/jhillyerd/inbucket/log"
|
"github.com/jhillyerd/inbucket/log"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
@@ -73,7 +72,7 @@ func MailboxShow(w http.ResponseWriter, req *http.Request, ctx *Context) (err er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
body := template.HTML(inbucket.TextToHtml(mime.Text))
|
body := template.HTML(textToHtml(mime.Text))
|
||||||
htmlAvailable := mime.Html != ""
|
htmlAvailable := mime.Html != ""
|
||||||
|
|
||||||
return RenderPartial("mailbox/_show.html", w, map[string]interface{}{
|
return RenderPartial("mailbox/_show.html", w, map[string]interface{}{
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
"github.com/jhillyerd/inbucket"
|
"github.com/jhillyerd/inbucket/config"
|
||||||
"github.com/jhillyerd/inbucket/log"
|
"github.com/jhillyerd/inbucket/log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"thegoods.biz/httpbuf"
|
"thegoods.biz/httpbuf"
|
||||||
@@ -20,7 +20,7 @@ var Router *mux.Router
|
|||||||
|
|
||||||
var sessionStore sessions.Store
|
var sessionStore sessions.Store
|
||||||
|
|
||||||
func setupRoutes(cfg inbucket.WebConfig) {
|
func setupRoutes(cfg config.WebConfig) {
|
||||||
Router = mux.NewRouter()
|
Router = mux.NewRouter()
|
||||||
log.Info("Theme templates mapped to '%v'", cfg.TemplateDir)
|
log.Info("Theme templates mapped to '%v'", cfg.TemplateDir)
|
||||||
log.Info("Theme static content mapped to '%v'", cfg.PublicDir)
|
log.Info("Theme static content mapped to '%v'", cfg.PublicDir)
|
||||||
@@ -42,7 +42,7 @@ func setupRoutes(cfg inbucket.WebConfig) {
|
|||||||
|
|
||||||
// Start() the web server
|
// Start() the web server
|
||||||
func Start() {
|
func Start() {
|
||||||
cfg := inbucket.GetWebConfig()
|
cfg := config.GetWebConfig()
|
||||||
setupRoutes(cfg)
|
setupRoutes(cfg)
|
||||||
|
|
||||||
sessionStore = sessions.NewCookieStore([]byte("something-very-secret"))
|
sessionStore = sessions.NewCookieStore([]byte("something-very-secret"))
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jhillyerd/inbucket"
|
"github.com/jhillyerd/inbucket/config"
|
||||||
"github.com/jhillyerd/inbucket/log"
|
"github.com/jhillyerd/inbucket/log"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -49,7 +49,7 @@ func ParseTemplate(name string, partial bool) (*template.Template, error) {
|
|||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := inbucket.GetWebConfig()
|
cfg := config.GetWebConfig()
|
||||||
tempPath := strings.Replace(name, "/", string(filepath.Separator), -1)
|
tempPath := strings.Replace(name, "/", string(filepath.Separator), -1)
|
||||||
tempFile := filepath.Join(cfg.TemplateDir, tempPath)
|
tempFile := filepath.Join(cfg.TemplateDir, tempPath)
|
||||||
log.Trace("Parsing template %v", tempFile)
|
log.Trace("Parsing template %v", tempFile)
|
||||||
|
|||||||
Reference in New Issue
Block a user