2021-01-27 10:07:31 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-27 15:01:42 +01:00
|
|
|
"io"
|
2021-01-27 10:07:31 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
2021-01-27 15:01:42 +01:00
|
|
|
"strings"
|
2021-01-27 10:07:31 +01:00
|
|
|
|
|
|
|
"git.sr.ht/~ewintr/gte/pkg/mstore"
|
|
|
|
)
|
|
|
|
|
2021-01-27 15:01:42 +01:00
|
|
|
type Body struct {
|
|
|
|
reader io.Reader
|
|
|
|
length int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBody(msg string) *Body {
|
|
|
|
return &Body{
|
|
|
|
reader: strings.NewReader(msg),
|
|
|
|
length: len([]byte(msg)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Body) Read(p []byte) (int, error) {
|
|
|
|
return b.reader.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Body) Len() int {
|
|
|
|
return b.length
|
|
|
|
}
|
|
|
|
|
2021-01-27 10:07:31 +01:00
|
|
|
func main() {
|
2021-01-28 09:40:08 +01:00
|
|
|
config := &mstore.EmailConfiguration{
|
2021-01-27 10:07:31 +01:00
|
|
|
IMAPURL: os.Getenv("IMAP_URL"),
|
2021-01-27 12:58:43 +01:00
|
|
|
IMAPUsername: os.Getenv("IMAP_USERNAME"),
|
|
|
|
IMAPPassword: os.Getenv("IMAP_PASSWORD"),
|
2021-01-27 10:07:31 +01:00
|
|
|
}
|
|
|
|
if !config.Valid() {
|
2021-01-27 12:58:43 +01:00
|
|
|
fmt.Printf("conf: %v\n", config)
|
2021-01-27 10:07:31 +01:00
|
|
|
log.Fatal("please set MAIL_USER, MAIL_PASSWORD, etc environment variables")
|
|
|
|
}
|
|
|
|
//fmt.Printf("conf: %+v\n", config)
|
|
|
|
|
2021-01-28 09:40:08 +01:00
|
|
|
mailStore, err := mstore.EmailConnect(config)
|
2021-01-27 10:07:31 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer mailStore.Disconnect()
|
|
|
|
|
2021-01-27 15:01:42 +01:00
|
|
|
/*
|
2021-01-28 09:40:08 +01:00
|
|
|
folders, err := mailStore.FolderNames()
|
2021-01-27 15:01:42 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-01-28 09:40:08 +01:00
|
|
|
for _, f := range folders {
|
|
|
|
fmt.Println(f)
|
2021-01-27 15:01:42 +01:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-01-28 09:40:08 +01:00
|
|
|
if err := mailStore.Select("Today"); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-01-27 15:01:42 +01:00
|
|
|
|
2021-01-28 09:40:08 +01:00
|
|
|
messages, err := mailStore.Messages()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, m := range messages {
|
2021-01-28 14:43:00 +01:00
|
|
|
fmt.Printf("%d: %s\n", m.Uid, m.Subject)
|
2021-01-28 09:40:08 +01:00
|
|
|
}
|
|
|
|
if len(messages) == 0 {
|
|
|
|
log.Fatal("no messages")
|
|
|
|
return
|
|
|
|
}
|
2021-01-27 15:01:42 +01:00
|
|
|
|
2021-01-28 14:43:00 +01:00
|
|
|
if err := mailStore.Remove(messages[0].Uid); err != nil {
|
2021-01-27 12:58:43 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-01-28 09:40:08 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
body := NewBody(`From: todo <process@erikwinter.nl>
|
|
|
|
Subject: the subject
|
|
|
|
|
|
|
|
And here comes the body`)
|
|
|
|
|
|
|
|
if err := mailStore.Append("INBOX", imap.Literal(body)); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
*/
|
2021-01-27 10:07:31 +01:00
|
|
|
}
|