Parse SMTP URL from CLI

This commit is contained in:
Simon Ser 2019-12-03 15:21:59 +01:00
parent 1194b98e54
commit ae79f99876
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
3 changed files with 57 additions and 11 deletions

View file

@ -2,7 +2,7 @@
## Usage ## Usage
go run ./cmd/koushin imaps://mail.example.org:993 go run ./cmd/koushin imaps://mail.example.org:993 smtps://mail.example.org:465
## License ## License

View file

@ -9,14 +9,19 @@ import (
) )
func main() { func main() {
if len(os.Args) != 2 { if len(os.Args) != 2 && len(os.Args) != 3 {
fmt.Println("usage: koushin imaps://<host>:<port>") fmt.Println("usage: koushin <IMAP URL> [SMTP URL]")
return return
} }
url := os.Args[1] imapURL := os.Args[1]
e := koushin.New(url) var smtpURL string
if len(os.Args) == 3 {
smtpURL = os.Args[2]
}
e := koushin.New(imapURL, smtpURL)
e.Use(middleware.Logger()) e.Use(middleware.Logger())
e.Use(middleware.Recover()) e.Use(middleware.Recover())
e.Logger.Fatal(e.Start(":1323")) e.Logger.Fatal(e.Start(":1323"))

View file

@ -23,15 +23,20 @@ type Server struct {
pool *ConnPool pool *ConnPool
} }
smtp struct {
host string
tls bool
insecure bool
}
} }
func NewServer(imapURL string) (*Server, error) { func (s *Server) parseIMAPURL(imapURL string) error {
u, err := url.Parse(imapURL) u, err := url.Parse(imapURL)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse IMAP server URL: %v", err) return fmt.Errorf("failed to parse IMAP server URL: %v", err)
} }
s := &Server{}
s.imap.host = u.Host s.imap.host = u.Host
switch u.Scheme { switch u.Scheme {
case "imap": case "imap":
@ -41,11 +46,47 @@ func NewServer(imapURL string) (*Server, error) {
case "imap+insecure": case "imap+insecure":
s.imap.insecure = true s.imap.insecure = true
default: default:
return nil, fmt.Errorf("unrecognized IMAP URL scheme: %s", u.Scheme) return fmt.Errorf("unrecognized IMAP URL scheme: %s", u.Scheme)
} }
return nil
}
func (s *Server) parseSMTPURL(smtpURL string) error {
u, err := url.Parse(smtpURL)
if err != nil {
return fmt.Errorf("failed to parse SMTP server URL: %v", err)
}
s.smtp.host = u.Host
switch u.Scheme {
case "smtp":
// This space is intentionally left blank
case "smtps":
s.smtp.tls = true
case "smtp+insecure":
s.smtp.insecure = true
default:
return fmt.Errorf("unrecognized SMTP URL scheme: %s", u.Scheme)
}
return nil
}
func NewServer(imapURL, smtpURL string) (*Server, error) {
s := &Server{}
if err := s.parseIMAPURL(imapURL); err != nil {
return nil, err
}
s.imap.pool = NewConnPool() s.imap.pool = NewConnPool()
if smtpURL != "" {
if err := s.parseSMTPURL(smtpURL); err != nil {
return nil, err
}
}
return s, nil return s, nil
} }
@ -159,10 +200,10 @@ func handleCompose(ectx echo.Context) error {
return ctx.Render(http.StatusOK, "compose.html", nil) return ctx.Render(http.StatusOK, "compose.html", nil)
} }
func New(imapURL string) *echo.Echo { func New(imapURL, smtpURL string) *echo.Echo {
e := echo.New() e := echo.New()
s, err := NewServer(imapURL) s, err := NewServer(imapURL, smtpURL)
if err != nil { if err != nil {
e.Logger.Fatal(err) e.Logger.Fatal(err)
} }