]> Sergey Matveev's repositories - sgblog.git/blob - cmd/sgblog-comment-add/mail.go
88d95d84a64cb0d6913ceac476452257ff77635d
[sgblog.git] / cmd / sgblog-comment-add / mail.go
1 /*
2 SGBlog -- Git-backed CGI/inetd blogging/phlogging engine
3 Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as
7 published by the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU Affero General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "bytes"
22         "encoding/base64"
23         "errors"
24         "io"
25         "io/ioutil"
26         "mime"
27         "mime/multipart"
28         "mime/quotedprintable"
29         "net/mail"
30         "strings"
31 )
32
33 const (
34         CT  = "Content-Type"
35         CTE = "Content-Transfer-Encoding"
36         TP  = "text/plain"
37 )
38
39 func processTP(ct, cte string, body io.Reader) (io.Reader, error) {
40         _, params, err := mime.ParseMediaType(ct)
41         if err != nil {
42                 return nil, err
43         }
44         if c := strings.ToLower(params["charset"]); !(c == "" ||
45                 c == "utf-8" ||
46                 c == "iso-8859-1" ||
47                 c == "us-ascii") {
48                 return nil, errors.New("only utf-8/iso-8859-1/us-ascii charsets supported")
49         }
50         switch cte {
51         case "quoted-printable":
52                 return quotedprintable.NewReader(body), nil
53         case "base64":
54                 return base64.NewDecoder(base64.StdEncoding, body), nil
55         }
56         return body, nil
57 }
58
59 func parseEmail(msg *mail.Message) (subj string, body io.Reader, err error) {
60         subj = msg.Header.Get("Subject")
61         if subj == "" {
62                 err = errors.New("no Subject")
63                 return
64         }
65         words := strings.Fields(subj)
66         for i, word := range words {
67                 if strings.HasPrefix(word, "=?") && strings.HasSuffix(word, "?=") {
68                         word, err = new(mime.WordDecoder).Decode(word)
69                         if err != nil {
70                                 return
71                         }
72                         words[i] = word
73                 }
74         }
75         subj = strings.Join(words, " ")
76
77         ct := msg.Header.Get(CT)
78         if ct == "" {
79                 ct = "text/plain"
80         }
81         if strings.HasPrefix(ct, TP) {
82                 body, err = processTP(ct, msg.Header.Get(CTE), msg.Body)
83                 return
84         }
85         ct, params, err := mime.ParseMediaType(ct)
86         if ct != "multipart/signed" {
87                 err = errors.New("only text/plain and multipart/signed+text/plain Content-Type supported")
88                 return
89         }
90         boundary := params["boundary"]
91         if len(boundary) == 0 {
92                 err = errors.New("no boundary string")
93                 return
94         }
95         data, err := ioutil.ReadAll(msg.Body)
96         if err != nil {
97                 return
98         }
99         boundaryIdx := bytes.Index(data, []byte("--"+boundary))
100         if boundaryIdx == -1 {
101                 err = errors.New("no boundary found")
102                 return
103         }
104         mpr := multipart.NewReader(bytes.NewReader(data[boundaryIdx:]), boundary)
105         var part *multipart.Part
106         for {
107                 part, err = mpr.NextPart()
108                 if err != nil {
109                         if err == io.EOF {
110                                 break
111                         }
112                         return
113                 }
114                 ct = part.Header.Get(CT)
115                 if strings.HasPrefix(ct, TP) {
116                         body, err = processTP(ct, part.Header.Get(CTE), part)
117                         return
118                 }
119                 if strings.HasPrefix(ct, "multipart/mixed") {
120                         ct, params, err = mime.ParseMediaType(ct)
121                         boundary = params["boundary"]
122                         if len(boundary) == 0 {
123                                 err = errors.New("no boundary string")
124                                 return
125                         }
126                         mpr := multipart.NewReader(part, boundary)
127                         for {
128                                 part, err = mpr.NextPart()
129                                 if err != nil {
130                                         if err == io.EOF {
131                                                 break
132                                         }
133                                         return
134                                 }
135                                 ct = part.Header.Get(CT)
136                                 if strings.HasPrefix(ct, TP) {
137                                         body, err = processTP(ct, part.Header.Get(CTE), part)
138                                         return
139                                 }
140                         }
141                 }
142         }
143         err = errors.New("no text/plain part found")
144         return
145 }