main.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package main
  26. import (
  27. "bufio"
  28. "fmt"
  29. "html"
  30. "log"
  31. "net/http"
  32. "os"
  33. "regexp"
  34. "strings"
  35. )
  36. const (
  37. StateHeaderScan = iota
  38. StateBodyScan
  39. StateContentScan
  40. )
  41. const (
  42. HeaderRegExp = "^([\x21-\x7E^:]+):(.*)"
  43. FoldingRegExp = "^\\s+(.*)"
  44. BoundaryStartRegExp = "^--(.*)"
  45. BoundaryEndRegExp = "^--(.*)--$"
  46. BoundaryRegExp = "boundary=\"(.*)\""
  47. )
  48. type Email struct {
  49. from string
  50. to string
  51. cc string
  52. bcc string
  53. date string
  54. subject string
  55. contentType string
  56. body string
  57. }
  58. func NewEmail() *Email {
  59. return &Email{
  60. contentType: "plain/text",
  61. }
  62. }
  63. func handler(w http.ResponseWriter, r *http.Request) {
  64. fmt.Fprintf(w, "<!DOCTYPE html>\n<html lang=\"en\">\n<head><meta charset=\"utf-8\"/></head>\n<body>\n")
  65. state := StateHeaderScan
  66. headerFinder, err := regexp.Compile(HeaderRegExp)
  67. if err != nil {
  68. log.Fatalf("Invalid regexp %s\n", err)
  69. }
  70. foldingFinder, err := regexp.Compile(FoldingRegExp)
  71. if err != nil {
  72. log.Fatalf("Invalid regexp %s\n", err)
  73. }
  74. boundaryStartFinder, err := regexp.Compile(BoundaryStartRegExp)
  75. if err != nil {
  76. log.Fatalf("Invalid regexp %s\n", err)
  77. }
  78. boundaryEndFinder, err := regexp.Compile(BoundaryEndRegExp)
  79. if err != nil {
  80. log.Fatalf("Invalid regexp %s\n", err)
  81. }
  82. boundaryFinder, err := regexp.Compile(BoundaryRegExp)
  83. file, _ := os.Open("./semlanik")
  84. scanner := bufio.NewScanner(file)
  85. activeBoundary := ""
  86. var previousHeader *string = nil
  87. for email := NewEmail(); scanner.Scan(); {
  88. if scanner.Text() == "" {
  89. if state == StateHeaderScan {
  90. boundaryCapture := boundaryFinder.FindStringSubmatch(email.contentType)
  91. if len(boundaryCapture) == 2 {
  92. activeBoundary = boundaryCapture[1]
  93. } else {
  94. activeBoundary = ""
  95. }
  96. state = StateBodyScan
  97. fmt.Printf("--------------------------Start body scan content type:%s boundary: %s -------------------------\n", email.contentType, activeBoundary)
  98. } else if state == StateBodyScan {
  99. // fmt.Printf("--------------------------Previous email body activeBoundary: %s -------------------------\n%s\n", activeBoundary, email.body)
  100. // if activeBoundary != "" {
  101. // r := strings.NewReader(email.body)
  102. // multipartReader := multipart.NewReader(r, activeBoundary)
  103. // for part, err := multipartReader.NextPart(); err == nil; {
  104. // fmt.Printf("Body content type: %s\n", part.Header.Get("Content-type"))
  105. // }
  106. // }
  107. fmt.Printf("--------------------------Previous email-------------------------\n%v\n", email)
  108. fmt.Fprintf(w, "<div>\n")
  109. fmt.Fprintf(w, "<b>From:</b>%s<br/>\n", html.EscapeString(email.from))
  110. fmt.Fprintf(w, "<b>Subject:</b>%s<br/>\n", html.EscapeString(email.subject))
  111. previousHeader = nil
  112. activeBoundary = ""
  113. email = NewEmail()
  114. state = StateHeaderScan
  115. } else {
  116. fmt.Printf("Empty line in state %d\n", state)
  117. }
  118. }
  119. if state == StateHeaderScan {
  120. capture := headerFinder.FindStringSubmatch(scanner.Text())
  121. if len(capture) == 3 {
  122. fmt.Printf("capture Header %s : %s\n", strings.ToLower(capture[0]), strings.ToLower(capture[1]))
  123. header := strings.ToLower(capture[1])
  124. switch header {
  125. case "from":
  126. previousHeader = &email.from
  127. case "to":
  128. previousHeader = &email.to
  129. case "cc":
  130. previousHeader = &email.cc
  131. case "bcc":
  132. previousHeader = &email.bcc
  133. case "subject":
  134. previousHeader = &email.subject
  135. case "date":
  136. previousHeader = &email.date
  137. case "content-type":
  138. previousHeader = &email.contentType
  139. default:
  140. previousHeader = nil
  141. }
  142. if previousHeader != nil {
  143. *previousHeader += capture[2]
  144. }
  145. continue
  146. }
  147. capture = foldingFinder.FindStringSubmatch(scanner.Text())
  148. if len(capture) == 2 && previousHeader != nil {
  149. *previousHeader += capture[1]
  150. continue
  151. }
  152. } else {
  153. email.body += scanner.Text() + "\n"
  154. if activeBoundary != "" {
  155. capture := boundaryEndFinder.FindStringSubmatch(scanner.Text())
  156. if len(capture) == 2 {
  157. fmt.Printf("capture Boundary End %s\n", capture[1])
  158. if activeBoundary == capture[1] {
  159. state = StateBodyScan
  160. }
  161. continue
  162. }
  163. capture = boundaryStartFinder.FindStringSubmatch(scanner.Text())
  164. if len(capture) == 2 {
  165. fmt.Printf("capture Boundary Start %s\n", capture[1])
  166. state = StateContentScan
  167. continue
  168. }
  169. }
  170. }
  171. }
  172. fmt.Fprintf(w, "</body>\n</html>")
  173. }
  174. func main() {
  175. http.HandleFunc("/", handler)
  176. log.Fatal(http.ListenAndServe(":8080", nil))
  177. }