|
@@ -64,9 +64,12 @@ const (
|
|
// Body string
|
|
// Body string
|
|
// }
|
|
// }
|
|
|
|
|
|
-func NewEmail() *MailHeader {
|
|
|
|
- return &MailHeader{
|
|
|
|
- // ContentType: "plain/text",
|
|
|
|
|
|
+func NewEmail() *Mail {
|
|
|
|
+ return &Mail{
|
|
|
|
+ Header: &MailHeader{},
|
|
|
|
+ Body: &MailBody{
|
|
|
|
+ ContentType: "plain/text",
|
|
|
|
+ },
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -75,13 +78,15 @@ type GofixEngine struct {
|
|
fileServer http.Handler
|
|
fileServer http.Handler
|
|
userChecker *regexp.Regexp
|
|
userChecker *regexp.Regexp
|
|
scanner *MailScanner
|
|
scanner *MailScanner
|
|
|
|
+ mailPath string
|
|
}
|
|
}
|
|
|
|
|
|
-func NewGofixEngine() (e *GofixEngine) {
|
|
|
|
|
|
+func NewGofixEngine(mailPath string) (e *GofixEngine) {
|
|
e = &GofixEngine{
|
|
e = &GofixEngine{
|
|
templater: NewTemplater("templates"),
|
|
templater: NewTemplater("templates"),
|
|
fileServer: http.FileServer(http.Dir("./")),
|
|
fileServer: http.FileServer(http.Dir("./")),
|
|
- scanner: NewMailScanner("/home/vmail/semlanik.org"),
|
|
|
|
|
|
+ scanner: NewMailScanner(mailPath),
|
|
|
|
+ mailPath: mailPath,
|
|
}
|
|
}
|
|
|
|
|
|
var err error = nil
|
|
var err error = nil
|
|
@@ -138,29 +143,29 @@ func (e *GofixEngine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
boundaryFinder, err := regexp.Compile(BoundaryRegExp)
|
|
boundaryFinder, err := regexp.Compile(BoundaryRegExp)
|
|
|
|
|
|
- if !fileExists("/home/vmail/semlanik.org/" + r.URL.Query().Get("user")) {
|
|
|
|
|
|
+ if !fileExists(e.mailPath + "/" + r.URL.Query().Get("user")) {
|
|
w.WriteHeader(http.StatusForbidden)
|
|
w.WriteHeader(http.StatusForbidden)
|
|
fmt.Fprint(w, "403 Unknown user")
|
|
fmt.Fprint(w, "403 Unknown user")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- file, _ := os.Open("/home/vmail/semlanik.org/" + r.URL.Query().Get("user"))
|
|
|
|
|
|
+ file, _ := os.Open(e.mailPath + "/" + r.URL.Query().Get("user"))
|
|
scanner := bufio.NewScanner(file)
|
|
scanner := bufio.NewScanner(file)
|
|
activeBoundary := ""
|
|
activeBoundary := ""
|
|
var previousHeader *string = nil
|
|
var previousHeader *string = nil
|
|
- var emails []*MailHeader
|
|
|
|
- lastContentType := "plain/text"
|
|
|
|
- for email := NewEmail(); scanner.Scan(); {
|
|
|
|
|
|
+ var emails []*Mail
|
|
|
|
+ email := NewEmail()
|
|
|
|
+ for scanner.Scan() {
|
|
if scanner.Text() == "" {
|
|
if scanner.Text() == "" {
|
|
if state == StateHeaderScan {
|
|
if state == StateHeaderScan {
|
|
- boundaryCapture := boundaryFinder.FindStringSubmatch(lastContentType)
|
|
|
|
|
|
+ boundaryCapture := boundaryFinder.FindStringSubmatch(email.Body.ContentType)
|
|
if len(boundaryCapture) == 2 {
|
|
if len(boundaryCapture) == 2 {
|
|
activeBoundary = boundaryCapture[1]
|
|
activeBoundary = boundaryCapture[1]
|
|
} else {
|
|
} else {
|
|
activeBoundary = ""
|
|
activeBoundary = ""
|
|
}
|
|
}
|
|
state = StateBodyScan
|
|
state = StateBodyScan
|
|
- // fmt.Printf("--------------------------Start body scan content type:%s boundary: %s -------------------------\n", lastContentType, activeBoundary)
|
|
|
|
|
|
+ // fmt.Printf("--------------------------Start body scan content type:%s boundary: %s -------------------------\n", email.Body.ContentType, activeBoundary)
|
|
} else if state == StateBodyScan {
|
|
} else if state == StateBodyScan {
|
|
// fmt.Printf("--------------------------Previous email-------------------------\n%v\n", email)
|
|
// fmt.Printf("--------------------------Previous email-------------------------\n%v\n", email)
|
|
|
|
|
|
@@ -181,19 +186,19 @@ func (e *GofixEngine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
header := strings.ToLower(capture[1])
|
|
header := strings.ToLower(capture[1])
|
|
switch header {
|
|
switch header {
|
|
case "from":
|
|
case "from":
|
|
- previousHeader = &email.From
|
|
|
|
|
|
+ previousHeader = &email.Header.From
|
|
case "to":
|
|
case "to":
|
|
- previousHeader = &email.To
|
|
|
|
|
|
+ previousHeader = &email.Header.To
|
|
case "cc":
|
|
case "cc":
|
|
- previousHeader = &email.Cc
|
|
|
|
|
|
+ previousHeader = &email.Header.Cc
|
|
case "bcc":
|
|
case "bcc":
|
|
- previousHeader = &email.Bcc
|
|
|
|
|
|
+ previousHeader = &email.Header.Bcc
|
|
case "subject":
|
|
case "subject":
|
|
- previousHeader = &email.Subject
|
|
|
|
|
|
+ previousHeader = &email.Header.Subject
|
|
case "date":
|
|
case "date":
|
|
- previousHeader = &email.Date
|
|
|
|
|
|
+ previousHeader = &email.Header.Date
|
|
case "content-type":
|
|
case "content-type":
|
|
- previousHeader = &lastContentType
|
|
|
|
|
|
+ previousHeader = &email.Body.ContentType
|
|
default:
|
|
default:
|
|
previousHeader = nil
|
|
previousHeader = nil
|
|
}
|
|
}
|
|
@@ -209,7 +214,7 @@ func (e *GofixEngine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- // email.Body += scanner.Text() + "\n"
|
|
|
|
|
|
+ // email.Body.Content += scanner.Text() + "\n"
|
|
if activeBoundary != "" {
|
|
if activeBoundary != "" {
|
|
capture := boundaryEndFinder.FindStringSubmatch(scanner.Text())
|
|
capture := boundaryEndFinder.FindStringSubmatch(scanner.Text())
|
|
if len(capture) == 2 {
|
|
if len(capture) == 2 {
|
|
@@ -230,6 +235,15 @@ func (e *GofixEngine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if state == StateBodyScan { //Finalize if body read till EOF
|
|
|
|
+ // fmt.Printf("--------------------------Previous email-------------------------\n%v\n", email)
|
|
|
|
+
|
|
|
|
+ previousHeader = nil
|
|
|
|
+ activeBoundary = ""
|
|
|
|
+ emails = append(emails, email)
|
|
|
|
+ state = StateHeaderScan
|
|
|
|
+ }
|
|
|
|
+
|
|
content := template.HTML(e.templater.ExecuteMailList(emails))
|
|
content := template.HTML(e.templater.ExecuteMailList(emails))
|
|
|
|
|
|
fmt.Fprint(w, e.templater.ExecuteIndex(content))
|
|
fmt.Fprint(w, e.templater.ExecuteIndex(content))
|
|
@@ -269,6 +283,10 @@ func fileExists(filename string) bool {
|
|
}
|
|
}
|
|
|
|
|
|
func main() {
|
|
func main() {
|
|
- engine := NewGofixEngine()
|
|
|
|
|
|
+ mailPath := "./"
|
|
|
|
+ if len(os.Args) >= 2 {
|
|
|
|
+ mailPath = os.Args[1]
|
|
|
|
+ }
|
|
|
|
+ engine := NewGofixEngine(mailPath)
|
|
engine.Run()
|
|
engine.Run()
|
|
}
|
|
}
|