Skip to content

Commit

Permalink
add group replies
Browse files Browse the repository at this point in the history
  • Loading branch information
laochailan committed Sep 17, 2017
1 parent 00a7406 commit 9d5128c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Things that are left to do

- encryption/signing support
- making the `:` prompt a bit friendlier (history)
- proper group reply

## Installation

Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ key = pageup move pageup
key = pagedown move pagedown
key = enter show
key = r reply
key = R groupreply
key = / prompt search
key = | prompt search
key = n search
Expand Down
60 changes: 46 additions & 14 deletions mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,54 @@ func composeMail() *Mail {
return m
}

// composeReply creates a Mail structure for a Reply to Mail m.
func composeReply(m *Mail) *Mail {
reply := composeMail()

dec := new(mime.WordDecoder)
to, err := dec.DecodeHeader(m.Header.Get("From"))
// chooseReplyToFrom chooses the To and From fields of the reply based on the fields of the mail replied to
func chooseReplyToFrom(origTo, origFrom string, groupReply bool) (to, from string) {
fromAddr, err := mail.ParseAddressList(origFrom)
if err != nil {
to = m.Header.Get("From")
return origFrom, origTo
}
from, err := dec.DecodeHeader(m.Header.Get("To"))
toAddr, err := mail.ParseAddressList(origTo)
if err != nil {
from = m.Header.Get("To")
return origFrom, origTo
}

from = origTo

// find an address that can be sent from
outer:
for i, addr := range toAddr {
for _, account := range config.Account {
if addr.Address == account.Addr {
from = addr.String()
if groupReply {
fromAddr = append(fromAddr, toAddr[:i]...)
fromAddr = append(fromAddr, toAddr[i+1:]...)
StatusLine = fmt.Sprintf("%d", len(fromAddr))
}
break outer
}
}
}

strList := make([]string, len(fromAddr))
for i, a := range fromAddr {
strList[i] = a.String()
}
reply.Header["To"] = []string{to}
reply.Header["From"] = []string{from}

to = strings.Join(strList, ", ")
return to, from
}

// composeReply creates a Mail structure for a Reply to Mail m.
func composeReply(m *Mail, groupReply bool) *Mail {
reply := composeMail()

dec := new(mime.WordDecoder)

oldFrom := m.Header.Get("From")
oldTo := m.Header.Get("To")
to, from := chooseReplyToFrom(oldTo, oldFrom, groupReply)
reply.Header["To"], reply.Header["From"] = []string{to}, []string{from}
reply.Header["In-Reply-To"] = []string{m.Header.Get("Message-ID")}

refs := m.Header["References"]
Expand Down Expand Up @@ -288,7 +321,6 @@ func (m *Mail) Encode() (string, error) {
return "", errors.New("Error: message without content")
}

henc := new(mime.WordEncoder)
boundary := randomBoundary()

if len(m.Parts) == 1 {
Expand All @@ -310,9 +342,9 @@ func (m *Mail) Encode() (string, error) {

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]
val[i] = mime.QEncoding.Encode("utf-8", strings.Join(split[:len(split)-1], " ")) + " " + split[len(split)-1]
} else {
val[i] = henc.Encode("utf-8", val[i])
val[i] = mime.QEncoding.Encode("utf-8", val[i])
}
}

Expand Down
5 changes: 4 additions & 1 deletion mailbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,10 @@ func (b *MailBuffer) HandleCommand(cmd string, args []string, stack *BufferStack
termbox.Sync()
stack.refresh()
case "reply":
reply := composeReply(b.mail)
reply := composeReply(b.mail, false)
stack.Push(NewComposeBuffer(reply))
case "groupreply":
reply := composeReply(b.mail, true)
stack.Push(NewComposeBuffer(reply))
case "search":
if len(args) > 0 {
Expand Down

0 comments on commit 9d5128c

Please sign in to comment.