IMAP client wrapper
This commit is contained in:
10
go.mod
Normal file
10
go.mod
Normal file
@@ -0,0 +1,10 @@
|
||||
module github.com/firestuff/imap2asana
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/emersion/go-imap v1.2.1
|
||||
|
||||
require (
|
||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
)
|
||||
10
go.sum
Normal file
10
go.sum
Normal file
@@ -0,0 +1,10 @@
|
||||
github.com/emersion/go-imap v1.2.1 h1:+s9ZjMEjOB8NzZMVTM3cCenz2JrQIGGo5j1df19WjTA=
|
||||
github.com/emersion/go-imap v1.2.1/go.mod h1:Qlx1FSx2FTxjnjWpIlVNEuX+ylerZQNFE5NsmKFSejY=
|
||||
github.com/emersion/go-message v0.15.0/go.mod h1:wQUEfE+38+7EW8p8aZ96ptg6bAb1iwdgej19uXASlE4=
|
||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
|
||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
|
||||
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
76
imap2asana.go
Normal file
76
imap2asana.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/emersion/go-imap"
|
||||
"github.com/emersion/go-imap/client"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ic, err := NewImapClient(
|
||||
os.Getenv("IMAP_HOST"),
|
||||
os.Getenv("IMAP_USERNAME"),
|
||||
os.Getenv("IMAP_PASSWORD"),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer ic.Close()
|
||||
|
||||
mboxs, err := ic.List("", "*")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, mbox := range mboxs {
|
||||
fmt.Printf("%#v\n", mbox)
|
||||
}
|
||||
}
|
||||
|
||||
// go-imap's API is a pile of stupid; wrap it
|
||||
type ImapClient struct {
|
||||
cli *client.Client
|
||||
}
|
||||
|
||||
func NewImapClient(host, user, pass string) (*ImapClient, error) {
|
||||
c, err := client.DialTLS(host, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = c.Login(user, pass)
|
||||
if err != nil {
|
||||
c.Logout()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ImapClient{
|
||||
cli: c,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (ic *ImapClient) Close() {
|
||||
ic.cli.Logout()
|
||||
}
|
||||
|
||||
func (ic *ImapClient) List(ref, name string) ([]*imap.MailboxInfo, error) {
|
||||
ch := make(chan *imap.MailboxInfo, 10)
|
||||
var err error
|
||||
go func() {
|
||||
err = ic.cli.List(ref, name, ch)
|
||||
}()
|
||||
|
||||
ret := []*imap.MailboxInfo{}
|
||||
for mbox := range ch {
|
||||
ret = append(ret, mbox)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return ret, nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user