diff --git a/imap.go b/imap.go index e7e7710..869d457 100644 --- a/imap.go +++ b/imap.go @@ -1,6 +1,8 @@ package koushin import ( + "sort" + "github.com/emersion/go-imap" imapclient "github.com/emersion/go-imap/client" ) @@ -45,5 +47,51 @@ func listMailboxes(conn *imapclient.Client) ([]*imap.MailboxInfo, error) { return nil, err } + sort.Slice(mailboxes, func(i, j int) bool { + return mailboxes[i].Name < mailboxes[j].Name + }) return mailboxes, nil } + +func listMessages(conn *imapclient.Client, mboxName string) ([]*imap.Message, error) { + mbox := conn.Mailbox() + if mbox == nil || mbox.Name != mboxName { + var err error + mbox, err = conn.Select(mboxName, false) + if err != nil { + return nil, err + } + } + + n := uint32(10) + from := uint32(1) + to := mbox.Messages + if mbox.Messages > n { + from = mbox.Messages - n + } + seqSet := new(imap.SeqSet) + seqSet.AddRange(from, to) + + ch := make(chan *imap.Message, 10) + done := make(chan error, 1) + go func() { + done <- conn.Fetch(seqSet, []imap.FetchItem{imap.FetchEnvelope}, ch) + }() + + msgs := make([]*imap.Message, 0, n) + for msg := range ch { + msgs = append(msgs, msg) + } + + if err := <-done; err != nil { + return nil, err + } + + // Reverse list of messages + for i := len(msgs)/2-1; i >= 0; i-- { + opp := len(msgs)-1-i + msgs[i], msgs[opp] = msgs[opp], msgs[i] + } + + return msgs, nil +} diff --git a/public/index.html b/public/mailbox.html similarity index 60% rename from public/index.html rename to public/mailbox.html index 4a9566a..2f5cdb6 100644 --- a/public/index.html +++ b/public/mailbox.html @@ -9,4 +9,11 @@ {{end}} +
Messages:
+