basic imap login
This commit is contained in:
parent
82ca3a5565
commit
a0228b4692
|
@ -0,0 +1,91 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"git.sr.ht/~ewintr/gte/pkg/mstore"
|
||||
)
|
||||
|
||||
func main() {
|
||||
iPort, _ := strconv.Atoi(os.Getenv("IMAP_PORT"))
|
||||
//sPort, _ := strconv.Atoi(os.Getenv("SMTP_PORT"))
|
||||
config := &mstore.EMailStoreConfiguration{
|
||||
IMAPURL: os.Getenv("IMAP_URL"),
|
||||
IMAPPort: iPort,
|
||||
IMAPUsername: os.Getenv("MAIL_USER"),
|
||||
IMAPPassword: os.Getenv("MAIL_PASSWORD"),
|
||||
}
|
||||
if !config.Valid() {
|
||||
log.Fatal("please set MAIL_USER, MAIL_PASSWORD, etc environment variables")
|
||||
}
|
||||
//fmt.Printf("conf: %+v\n", config)
|
||||
|
||||
mailStore, err := mstore.EMailConnect(config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer mailStore.Disconnect()
|
||||
|
||||
folders, err := mailStore.Folders()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for _, f := range folders {
|
||||
fmt.Println(f.Name)
|
||||
}
|
||||
/*
|
||||
|
||||
// List mailboxes
|
||||
mailboxes := make(chan *imap.MailboxInfo, 10)
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- c.List("", "*", mailboxes)
|
||||
}()
|
||||
|
||||
log.Println("Mailboxes:")
|
||||
for m := range mailboxes {
|
||||
log.Println("* " + m.Name)
|
||||
}
|
||||
|
||||
if err := <-done; err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Select INBOX
|
||||
mbox, err := c.Select("INBOX", false)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Flags for INBOX:", mbox.Flags)
|
||||
|
||||
// Get the last 4 messages
|
||||
from := uint32(1)
|
||||
to := mbox.Messages
|
||||
if mbox.Messages > 3 {
|
||||
// We're using unsigned integers here, only subtract if the result is > 0
|
||||
from = mbox.Messages - 3
|
||||
}
|
||||
seqset := new(imap.SeqSet)
|
||||
seqset.AddRange(from, to)
|
||||
|
||||
messages := make(chan *imap.Message, 10)
|
||||
done = make(chan error, 1)
|
||||
go func() {
|
||||
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, messages)
|
||||
}()
|
||||
|
||||
log.Println("Last 4 messages:")
|
||||
for msg := range messages {
|
||||
log.Println("* " + msg.Envelope.Subject)
|
||||
}
|
||||
|
||||
if err := <-done; err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("Done!")
|
||||
*/
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/emersion/go-imap"
|
||||
"github.com/emersion/go-imap/client"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.Println("Connecting to server...")
|
||||
|
||||
// Connect to server
|
||||
c, err := client.DialTLS(":993", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Connected")
|
||||
|
||||
// Don't forget to logout
|
||||
defer c.Logout()
|
||||
|
||||
// Login
|
||||
if err := c.Login("", ""); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Logged in")
|
||||
|
||||
// List mailboxes
|
||||
mailboxes := make(chan *imap.MailboxInfo, 10)
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- c.List("", "*", mailboxes)
|
||||
}()
|
||||
|
||||
log.Println("Mailboxes:")
|
||||
for m := range mailboxes {
|
||||
log.Println("* " + m.Name)
|
||||
}
|
||||
|
||||
if err := <-done; err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Select INBOX
|
||||
mbox, err := c.Select("INBOX", false)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Flags for INBOX:", mbox.Flags)
|
||||
|
||||
// Get the last 4 messages
|
||||
from := uint32(1)
|
||||
to := mbox.Messages
|
||||
if mbox.Messages > 3 {
|
||||
// We're using unsigned integers here, only subtract if the result is > 0
|
||||
from = mbox.Messages - 3
|
||||
}
|
||||
seqset := new(imap.SeqSet)
|
||||
seqset.AddRange(from, to)
|
||||
|
||||
messages := make(chan *imap.Message, 10)
|
||||
done = make(chan error, 1)
|
||||
go func() {
|
||||
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, messages)
|
||||
}()
|
||||
|
||||
log.Println("Last 4 messages:")
|
||||
for msg := range messages {
|
||||
log.Println("* " + msg.Envelope.Subject)
|
||||
}
|
||||
|
||||
if err := <-done; err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("Done!")
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
package mstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/emersion/go-imap"
|
||||
"github.com/emersion/go-imap/client"
|
||||
)
|
||||
|
||||
|
@ -15,11 +18,55 @@ type EMailStoreConfiguration struct {
|
|||
SMTPPassword string
|
||||
}
|
||||
|
||||
func (esc *EMailStoreConfiguration) Valid() bool {
|
||||
if esc.IMAPURL == "" || esc.IMAPPort == 0 {
|
||||
return false
|
||||
}
|
||||
if esc.IMAPUsername == "" || esc.IMAPPassword == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
type EMailStore struct {
|
||||
imapClient *client.Client
|
||||
}
|
||||
|
||||
func NewEmailStore(config *EMailStoreConfiguration) (*EMailStore, error) {
|
||||
|
||||
return &EMailStore{}, nil
|
||||
func EMailConnect(conf *EMailStoreConfiguration) (*EMailStore, error) {
|
||||
imapClient, err := client.DialTLS(fmt.Sprintf("%s:%d", conf.IMAPURL, conf.IMAPPort), nil)
|
||||
if err != nil {
|
||||
return &EMailStore{}, err
|
||||
}
|
||||
if err := imapClient.Login(conf.IMAPUsername, conf.IMAPPassword); err != nil {
|
||||
return &EMailStore{}, err
|
||||
}
|
||||
|
||||
return &EMailStore{
|
||||
imapClient: imapClient,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (es EMailStore) Disconnect() {
|
||||
es.imapClient.Logout()
|
||||
}
|
||||
|
||||
func (es EMailStore) Folders() ([]*Folder, error) {
|
||||
boxes, done := make(chan *imap.MailboxInfo), make(chan error)
|
||||
go func() {
|
||||
done <- es.imapClient.List("", "*", boxes)
|
||||
}()
|
||||
|
||||
folders := []*Folder{}
|
||||
for b := range boxes {
|
||||
folders = append(folders, &Folder{
|
||||
Name: b.Name,
|
||||
})
|
||||
}
|
||||
|
||||
if err := <-done; err != nil {
|
||||
return []*Folder{}, err
|
||||
}
|
||||
|
||||
return folders, nil
|
||||
}
|
||||
|
|
|
@ -6,10 +6,11 @@ type Folder struct {
|
|||
|
||||
type Message struct {
|
||||
Subject string
|
||||
Body string
|
||||
}
|
||||
|
||||
type MStorer interface {
|
||||
Folders() ([]Folder, error)
|
||||
Folders() ([]*Folder, error)
|
||||
Messages(folder Folder) ([]Message, error)
|
||||
Move(message Message, folder Folder) error
|
||||
Update(message Message) error
|
||||
|
|
Loading…
Reference in New Issue