123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- /*
- * MIT License
- *
- * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
- *
- * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this
- * software and associated documentation files (the "Software"), to deal in the Software
- * without restriction, including without limitation the rights to use, copy, modify,
- * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
- * to permit persons to whom the Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies
- * or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
- * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
- package main
- import (
- "bufio"
- "fmt"
- template "html/template"
- "log"
- "net/http"
- "os"
- "regexp"
- "strings"
- unix "golang.org/x/sys/unix"
- )
- const (
- StateHeaderScan = iota
- StateBodyScan
- StateContentScan
- )
- const (
- AtLeastOneHeaderMask = 1 << iota
- FromHeaderMask
- DateHeaderMask
- ToHeaderMask
- AllHeaderMask = 15
- )
- const (
- HeaderRegExp = "^([\x21-\x7E^:]+):(.*)"
- FoldingRegExp = "^\\s+(.*)"
- BoundaryStartRegExp = "^--(.*)"
- BoundaryEndRegExp = "^--(.*)--$"
- BoundaryRegExp = "boundary=\"(.*)\""
- UserRegExp = "^[a-zA-Z][\\w0-9\\._]*"
- )
- const (
- Version = "0.1.0 alpha"
- )
- // type Email struct {
- // From string
- // To string
- // Cc string
- // Bcc string
- // Date string
- // Subject string
- // ContentType string
- // Body string
- // }
- func NewEmail() *Mail {
- return &Mail{
- Header: &MailHeader{},
- Body: &MailBody{
- ContentType: "plain/text",
- },
- }
- }
- type GofixEngine struct {
- templater *Templater
- fileServer http.Handler
- userChecker *regexp.Regexp
- scanner *MailScanner
- mailPath string
- }
- func NewGofixEngine(mailPath string) (e *GofixEngine) {
- e = &GofixEngine{
- templater: NewTemplater("templates"),
- fileServer: http.FileServer(http.Dir("./")),
- scanner: NewMailScanner(mailPath),
- mailPath: mailPath,
- }
- var err error = nil
- e.userChecker, err = regexp.Compile(UserRegExp)
- if err != nil {
- log.Fatal("Could not compile user checker regex")
- }
- return
- }
- func (e *GofixEngine) Run() {
- defer e.scanner.watcher.Close()
- e.scanner.Run()
- http.Handle("/", e)
- log.Fatal(http.ListenAndServe(":65200", nil))
- }
- func (e *GofixEngine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- fmt.Println(r.URL.Path)
- if strings.Index(r.URL.Path, "/css/") == 0 || strings.Index(r.URL.Path, "/assets/") == 0 {
- e.fileServer.ServeHTTP(w, r)
- } else {
- user := r.URL.Query().Get("user")
- if e.userChecker.FindString(user) != user || user == "" {
- fmt.Print("Invalid user")
- w.WriteHeader(http.StatusUnauthorized)
- fmt.Fprint(w, "401 - Access denied")
- return
- }
- state := StateHeaderScan
- headerFinder, err := regexp.Compile(HeaderRegExp)
- if err != nil {
- log.Fatalf("Invalid regexp %s\n", err)
- }
- foldingFinder, err := regexp.Compile(FoldingRegExp)
- if err != nil {
- log.Fatalf("Invalid regexp %s\n", err)
- }
- // boundaryStartFinder, err := regexp.Compile(BoundaryStartRegExp)
- // if err != nil {
- // log.Fatalf("Invalid regexp %s\n", err)
- // }
- boundaryEndFinder, err := regexp.Compile(BoundaryEndRegExp)
- if err != nil {
- log.Fatalf("Invalid regexp %s\n", err)
- }
- boundaryFinder, err := regexp.Compile(BoundaryRegExp)
- if !fileExists(e.mailPath + "/" + r.URL.Query().Get("user")) {
- w.WriteHeader(http.StatusForbidden)
- fmt.Fprint(w, "403 Unknown user")
- return
- }
- file, _ := os.Open(e.mailPath + "/" + r.URL.Query().Get("user"))
- scanner := bufio.NewScanner(file)
- activeBoundary := ""
- var previousHeader *string = nil
- var emails []*Mail
- mandatoryHeaders := 0
- email := NewEmail()
- for scanner.Scan() {
- if scanner.Text() == "" {
- if state == StateHeaderScan && mandatoryHeaders&AtLeastOneHeaderMask == AtLeastOneHeaderMask {
- boundaryCapture := boundaryFinder.FindStringSubmatch(email.Body.ContentType)
- if len(boundaryCapture) == 2 {
- activeBoundary = boundaryCapture[1]
- } else {
- activeBoundary = ""
- }
- state = StateBodyScan
- // fmt.Printf("--------------------------Start body scan content type:%s boundary: %s -------------------------\n", email.Body.ContentType, activeBoundary)
- } else if state == StateBodyScan {
- // fmt.Printf("--------------------------Previous email-------------------------\n%v\n", email)
- if activeBoundary == "" {
- previousHeader = nil
- activeBoundary = ""
- fmt.Printf("Actual headers: %d\n", mandatoryHeaders)
- if mandatoryHeaders == AllHeaderMask {
- emails = append(emails, email)
- }
- email = NewEmail()
- state = StateHeaderScan
- mandatoryHeaders = 0
- } else {
- fmt.Printf("Still in body scan\n")
- continue
- }
- } else {
- fmt.Printf("Empty line in state %d\n", state)
- }
- }
- if state == StateHeaderScan {
- capture := headerFinder.FindStringSubmatch(scanner.Text())
- if len(capture) == 3 {
- // fmt.Printf("capture Header %s : %s\n", strings.ToLower(capture[0]), strings.ToLower(capture[1]))
- header := strings.ToLower(capture[1])
- mandatoryHeaders |= AtLeastOneHeaderMask
- switch header {
- case "from":
- previousHeader = &email.Header.From
- mandatoryHeaders |= FromHeaderMask
- case "to":
- previousHeader = &email.Header.To
- mandatoryHeaders |= ToHeaderMask
- case "cc":
- previousHeader = &email.Header.Cc
- case "bcc":
- previousHeader = &email.Header.Bcc
- mandatoryHeaders |= ToHeaderMask
- case "subject":
- previousHeader = &email.Header.Subject
- case "date":
- previousHeader = &email.Header.Date
- mandatoryHeaders |= DateHeaderMask
- case "content-type":
- previousHeader = &email.Body.ContentType
- default:
- previousHeader = nil
- }
- if previousHeader != nil {
- *previousHeader += capture[2]
- }
- continue
- }
- capture = foldingFinder.FindStringSubmatch(scanner.Text())
- if len(capture) == 2 && previousHeader != nil {
- *previousHeader += capture[1]
- continue
- }
- } else {
- // email.Body.Content += scanner.Text() + "\n"
- if activeBoundary != "" {
- capture := boundaryEndFinder.FindStringSubmatch(scanner.Text())
- if len(capture) == 2 {
- // fmt.Printf("capture Boundary End %s\n", capture[1])
- if activeBoundary == capture[1] {
- state = StateBodyScan
- activeBoundary = ""
- }
- continue
- }
- // capture = boundaryStartFinder.FindStringSubmatch(scanner.Text())
- // if len(capture) == 2 && activeBoundary == capture[1] {
- // // fmt.Printf("capture Boundary Start %s\n", capture[1])
- // state = StateContentScan
- // continue
- // }
- }
- }
- }
- if state == StateBodyScan && mandatoryHeaders == AllHeaderMask { //Finalize if body read till EOF
- // fmt.Printf("--------------------------Previous email-------------------------\n%v\n", email)
- previousHeader = nil
- activeBoundary = ""
- emails = append(emails, email)
- state = StateHeaderScan
- }
- fmt.Fprint(w, e.templater.ExecuteIndex(&Index{
- MailList: template.HTML(e.templater.ExecuteMailList(emails)),
- Folders: "Folders",
- Version: Version,
- }))
- }
- }
- func openAndLockMailFile() {
- file, err := os.OpenFile("/home/vmail/semlanik.org/ci", os.O_RDWR, 0)
- if err != nil {
- log.Fatalf("Error to open /home/vmail/semlanik.org/ci %s", err)
- }
- defer file.Close()
- lk := &unix.Flock_t{
- Type: unix.F_WRLCK,
- }
- err = unix.FcntlFlock(file.Fd(), unix.F_SETLKW, lk)
- lk.Type = unix.F_UNLCK
- if err != nil {
- log.Fatalf("Error to set lock %s", err)
- }
- defer unix.FcntlFlock(file.Fd(), unix.F_SETLKW, lk)
- fmt.Printf("Succesfully locked PID: %d", lk.Pid)
- input := bufio.NewScanner(os.Stdin)
- input.Scan()
- }
- func fileExists(filename string) bool {
- info, err := os.Stat(filename)
- if os.IsNotExist(err) {
- return false
- }
- return err == nil && !info.IsDir() && info != nil
- }
- func main() {
- mailPath := "./"
- if len(os.Args) >= 2 {
- mailPath = os.Args[1]
- }
- engine := NewGofixEngine(mailPath)
- engine.Run()
- }
|