aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2019-12-03 15:21:59 +0100
committerSimon Ser <contact@emersion.fr>2019-12-03 15:21:59 +0100
commitae79f998769cdd62dbf856db0b3b53528b532438 (patch)
tree41873bf524bf5fda96b693c1a9372244ee03e13b
parent1194b98e541cbe11af7c28e21590eb6d401fbaa0 (diff)
downloadalps-ae79f998769cdd62dbf856db0b3b53528b532438.tar.gz
alps-ae79f998769cdd62dbf856db0b3b53528b532438.zip
Parse SMTP URL from CLI
-rw-r--r--README.md2
-rw-r--r--cmd/koushin/main.go13
-rw-r--r--server.go53
3 files changed, 57 insertions, 11 deletions
diff --git a/README.md b/README.md
index de5b359..874ac56 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
## 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
diff --git a/cmd/koushin/main.go b/cmd/koushin/main.go
index 6e364cb..d30cb85 100644
--- a/cmd/koushin/main.go
+++ b/cmd/koushin/main.go
@@ -9,14 +9,19 @@ import (
)
func main() {
- if len(os.Args) != 2 {
- fmt.Println("usage: koushin imaps://<host>:<port>")
+ if len(os.Args) != 2 && len(os.Args) != 3 {
+ fmt.Println("usage: koushin <IMAP URL> [SMTP URL]")
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.Recover())
e.Logger.Fatal(e.Start(":1323"))
diff --git a/server.go b/server.go
index 1519ee6..6e800d0 100644
--- a/server.go
+++ b/server.go
@@ -23,15 +23,20 @@ type Server struct {
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)
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
switch u.Scheme {
case "imap":
@@ -41,11 +46,47 @@ func NewServer(imapURL string) (*Server, error) {
case "imap+insecure":
s.imap.insecure = true
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()
+ if smtpURL != "" {
+ if err := s.parseSMTPURL(smtpURL); err != nil {
+ return nil, err
+ }
+ }
+
return s, nil
}
@@ -159,10 +200,10 @@ func handleCompose(ectx echo.Context) error {
return ctx.Render(http.StatusOK, "compose.html", nil)
}
-func New(imapURL string) *echo.Echo {
+func New(imapURL, smtpURL string) *echo.Echo {
e := echo.New()
- s, err := NewServer(imapURL)
+ s, err := NewServer(imapURL, smtpURL)
if err != nil {
e.Logger.Fatal(err)
}