64 lines
1.0 KiB
Go
64 lines
1.0 KiB
Go
package mailclient
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"io"
|
|
)
|
|
|
|
type LineWriter struct {
|
|
w io.Writer
|
|
length int
|
|
}
|
|
|
|
func NewLineWriter(w io.Writer, length int) *LineWriter {
|
|
return &LineWriter{
|
|
w: w,
|
|
length: length,
|
|
}
|
|
}
|
|
|
|
func (r *LineWriter) Write(p []byte) (n int, err error) {
|
|
for i := 0; i < len(p); i += r.length {
|
|
end := i + r.length
|
|
|
|
if end > len(p) {
|
|
end = len(p) - 1
|
|
}
|
|
|
|
var chunk []byte
|
|
chunk = append(chunk, p[i:end]...)
|
|
|
|
if len(p) >= end+r.length {
|
|
chunk = append(chunk, []byte("\r\n")...)
|
|
}
|
|
|
|
addN, err := r.w.Write(chunk)
|
|
if err != nil {
|
|
return n, err
|
|
}
|
|
n += addN
|
|
}
|
|
|
|
return n, nil
|
|
}
|
|
|
|
func (r *LineWriter) WriteString(s string) (n int, err error) {
|
|
p := []byte(s)
|
|
return r.Write(p)
|
|
}
|
|
|
|
func (r *LineWriter) WriteFormatString(format string, a ...any) (n int, err error) {
|
|
p := []byte(fmt.Sprintf(format, a...))
|
|
return r.Write(p)
|
|
}
|
|
|
|
func randomBoundary() string {
|
|
var buf [30]byte
|
|
_, err := io.ReadFull(rand.Reader, buf[:])
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return fmt.Sprintf("%x", buf[:])
|
|
}
|