ReceiveMail関数の追加。テストの追加、修正。

This commit is contained in:
taknb2nch
2014-02-15 16:58:57 +09:00
parent c4656c5965
commit 5495a0838d
3 changed files with 71 additions and 2 deletions

65
pop3.go
View File

@@ -9,6 +9,10 @@ import (
"strings"
)
var (
EOF = errors.New("skip the all mail remaining")
)
// MessageInfo has Number, Size, and Uid fields,
// and used as a return value of ListAll and UidlAll.
// When used as the return value of the method ListAll,
@@ -196,6 +200,67 @@ func (c *Client) Quit() error {
return c.cmdSimple("QUIT")
}
// ReceiveMail connects to the server at addr,
// and authenticates with user and pass,
// and calling receiveFn for each mail.
func ReceiveMail(addr, user, pass string, receiveFn ReceiveMailFunc) error {
c, err := Dial(addr)
if err != nil {
return err
}
defer func() {
c.Quit()
c.Close()
}()
if err = c.User(user); err != nil {
return err
}
if err = c.Pass(pass); err != nil {
return err
}
var mis []MessageInfo
if mis, err = c.UidlAll(); err != nil {
return err
}
for _, mi := range mis {
var data string
data, err = c.Retr(mi.Number)
del, err := receiveFn(mi.Number, mi.Uid, data, err)
if err != nil && err != EOF {
return err
}
if del {
if err = c.Dele(mi.Number); err != nil {
return err
}
}
if err == EOF {
break
}
}
return nil
}
// ReceiveMailFunc is the type of the function called for each mail.
// Its arguments are mail's number, uid, data, and mail receiving error.
// if this function returns false value, the mail will be deleted,
// if its returns EOF, skip the all mail of remaining.
// (after deleting mail, if necessary)
type ReceiveMailFunc func(number int, uid, data string, err error) (bool, error)
func (c *Client) cmdSimple(format string, args ...interface{}) error {
var err error