Skip to content

Commit

Permalink
switch entirely to go standard library for quoted printable
Browse files Browse the repository at this point in the history
  • Loading branch information
laochailan committed Nov 1, 2016
1 parent e52224a commit 00a7406
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
1 change: 1 addition & 0 deletions composebuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func (b *ComposeBuffer) HandleCommand(cmd string, args []string, stack *BufferSt
} else {
StatusLine = "Mail sent."
b.sent = true
stack.refresh()
}
case "attach":
if len(args) == 0 {
Expand Down
26 changes: 12 additions & 14 deletions mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/laochailan/barely/maildir"
"github.com/laochailan/notmuch/bindings/go/src/notmuch"
"github.com/saintfish/chardet"
qp "gopkg.in/alexcesaro/quotedprintable.v2"
)

// Part represents a multipart part. Messages that do not have multipart content
Expand Down Expand Up @@ -83,7 +82,7 @@ func readParts(reader io.Reader, boundary string, parts []Part) ([]Part, error)
return parts, nil
}

// convertToUtf8 dectects the charset of the given plain text slice and converts it to utf-8
// convertToUtf8 detects the charset of the given plain text slice and converts it to utf-8
// if necessary
func convertToUtf8(text []byte) (converted []byte) {
detector := chardet.NewTextDetector()
Expand Down Expand Up @@ -176,11 +175,12 @@ func composeMail() *Mail {
func composeReply(m *Mail) *Mail {
reply := composeMail()

to, _, err := qp.DecodeHeader(m.Header.Get("From"))
dec := new(mime.WordDecoder)
to, err := dec.DecodeHeader(m.Header.Get("From"))
if err != nil {
to = m.Header.Get("From")
}
from, _, err := qp.DecodeHeader(m.Header.Get("To"))
from, err := dec.DecodeHeader(m.Header.Get("To"))
if err != nil {
from = m.Header.Get("To")
}
Expand All @@ -197,7 +197,7 @@ func composeReply(m *Mail) *Mail {
newrefs = append(newrefs, m.Header.Get("Message-ID"))
reply.Header["References"] = newrefs

subj, _, err := qp.DecodeHeader(m.Header.Get("Subject"))
subj, err := dec.DecodeHeader(m.Header.Get("Subject"))
if err != nil {
subj = m.Header.Get("Subject")
}
Expand Down Expand Up @@ -288,7 +288,7 @@ func (m *Mail) Encode() (string, error) {
return "", errors.New("Error: message without content")
}

henc := qp.Q.NewHeaderEncoder("utf-8")
henc := new(mime.WordEncoder)
boundary := randomBoundary()

if len(m.Parts) == 1 {
Expand All @@ -308,13 +308,11 @@ func (m *Mail) Encode() (string, error) {
continue
}

if qp.NeedsEncoding(val[i]) {
if key == "From" || key == "To" { // don't encode mail addresses.
split := strings.Split(val[i], " ")
val[i] = henc.Encode(strings.Join(split[:len(split)-1], " ")) + " " + split[len(split)-1]
} else {
val[i] = henc.Encode(val[i])
}
if key == "From" || key == "To" { // don't encode mail addresses.
split := strings.Split(val[i], " ")
val[i] = henc.Encode("utf-8", strings.Join(split[:len(split)-1], " ")) + " " + split[len(split)-1]
} else {
val[i] = henc.Encode("utf-8", val[i])
}
}

Expand Down Expand Up @@ -350,7 +348,7 @@ func (m *Mail) Encode() (string, error) {
}
writer := pw
if p.Header.Get("Content-Transfer-Encoding") == "quoted-printable" {
writer = qp.NewEncoder(pw)
writer = quotedprintable.NewWriter(pw)
}

writer.Write([]byte(p.Body))
Expand Down
7 changes: 4 additions & 3 deletions mailbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"strings"

termbox "github.com/nsf/termbox-go"
qp "gopkg.in/alexcesaro/quotedprintable.v2"
)

// MailBuffer displays mails and allows replying to them
Expand Down Expand Up @@ -176,7 +175,8 @@ func (b *MailBuffer) refreshBuf() {

func (b *MailBuffer) drawHeader() {
getHeader := func(key string) string {
str, _, err := qp.DecodeHeader(b.mail.Header.Get(key)) // ignore charset for now
dec := new(mime.WordDecoder)
str, err := dec.DecodeHeader(b.mail.Header.Get(key)) // ignore charset for now
if err != nil {
str = err.Error()
}
Expand Down Expand Up @@ -247,7 +247,8 @@ func attachmentName(p *Part) string {
return ""
}
if name := params["name"]; name != "" {
name, _, err = qp.DecodeHeader(name)
dec := new(mime.WordDecoder)
name, err = dec.DecodeHeader(name)
if err != nil {
return ""
}
Expand Down

0 comments on commit 00a7406

Please sign in to comment.