mirror of
https://github.com/nabbar/golib.git
synced 2025-10-05 07:46:56 +08:00
84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2020 Nicolas JUHEL
|
|
*
|
|
* 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 mail
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"mime/quotedprintable"
|
|
"strings"
|
|
)
|
|
|
|
// base64Encode base64 encodes the provided text with line wrapping
|
|
func base64Encode(text []byte) []byte {
|
|
// create buffer
|
|
buf := new(bytes.Buffer)
|
|
|
|
// create base64 encoder that linewraps
|
|
encoder := base64.NewEncoder(base64.StdEncoding, &base64LineWrap{writer: buf})
|
|
|
|
// write the encoded text to buf
|
|
/* #nosec */
|
|
//nolint #nosec
|
|
_, _ = encoder.Write(text)
|
|
_ = encoder.Close()
|
|
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// qpEncode uses the quoted-printable encoding to encode the provided text
|
|
func qpEncode(text []byte) []byte {
|
|
// create buffer
|
|
buf := new(bytes.Buffer)
|
|
|
|
encoder := quotedprintable.NewWriter(buf)
|
|
|
|
/* #nosec */
|
|
//nolint #nosec
|
|
_, _ = encoder.Write(text)
|
|
_ = encoder.Close()
|
|
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func encodeHeader(text string, charset string, usedChars int) string {
|
|
// create buffer
|
|
buf := new(bytes.Buffer)
|
|
|
|
// encode
|
|
encoder := newEncoder(buf, charset, usedChars)
|
|
/* #nosec */
|
|
//nolint #nosec
|
|
_, _ = encoder.encode([]byte(text))
|
|
|
|
return buf.String()
|
|
}
|
|
|
|
func escapeQuotes(s string) string {
|
|
quoteEscaper := strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
|
|
return quoteEscaper.Replace(s)
|
|
}
|