aboutsummaryrefslogtreecommitdiff
path: root/internal/imap/url.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/imap/url.go')
-rw-r--r--internal/imap/url.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/internal/imap/url.go b/internal/imap/url.go
new file mode 100644
index 0000000..6ffea72
--- /dev/null
+++ b/internal/imap/url.go
@@ -0,0 +1,76 @@
+package imap
+
+import (
+ "net"
+ "net/url"
+
+ "github.com/Necoro/feed2imap-go/internal/log"
+)
+
+// Our own convenience wrapper
+type URL struct {
+ *url.URL
+ // url.URL has no port field and splits it everytime from Host
+ port *string
+}
+
+const (
+ imapsPort = "993"
+ imapPort = "143"
+ imapsSchema = "imaps"
+ imapSchema = "imap"
+)
+
+func (url *URL) Port() string {
+ if url.port == nil {
+ port := url.URL.Port()
+ url.port = &port
+ }
+ return *url.port
+}
+
+func (url *URL) ForceTLS() bool {
+ return url.Scheme == imapsSchema || url.Port() == imapsPort
+}
+
+func (url *URL) setDefaultScheme() {
+ switch url.Scheme {
+ case imapSchema, imapsSchema:
+ return
+ default:
+ oldScheme := url.Scheme
+ if url.Port() == imapsPort {
+ url.Scheme = imapsSchema
+ } else {
+ url.Scheme = imapSchema
+ }
+
+ if oldScheme != "" {
+ log.Warnf("Unknown scheme '%s', defaulting to '%s'", oldScheme, url.Scheme)
+ }
+ }
+}
+
+func (url *URL) setDefaultPort() {
+ if url.Port() == "" {
+ var port string
+ if url.Scheme == imapsSchema {
+ port = imapsPort
+ } else {
+ port = imapPort
+ }
+ url.port = &port
+ url.Host = net.JoinHostPort(url.Host, port)
+ }
+}
+
+func (url *URL) sanitizeUrl() {
+ url.setDefaultScheme()
+ url.setDefaultPort()
+}
+
+func NewUrl(url *url.URL) *URL {
+ u := URL{URL: url}
+ u.sanitizeUrl()
+ return &u
+} \ No newline at end of file
mp;follow=1'>Updating some depsRené 'Necoro' Neumann2-2/+7 2020-05-07Better detection if a text starts with html or notRené 'Necoro' Neumann2-4/+13 2020-05-07go fmtRené 'Necoro' Neumann1-3/+2 2020-05-07Add header X-Feed2Imap-GUIDRené 'Necoro' Neumann3-1/+7 2020-05-07update changelogRené 'Necoro' Neumann1-0/+1 2020-05-07FixRené 'Necoro' Neumann1-1/+1 2020-05-07Unified publishedDate and updatedDate into one (just as the old feed2imap...)René 'Necoro' Neumann5-21/+32 2020-05-06Print version during startupRené 'Necoro' Neumann1-1/+1 2020-05-06Improve templateRené 'Necoro' Neumann3-20/+28 2020-05-05Fix pipelineRené 'Necoro' Neumann1-2/+5 2020-05-05Make changelog a part of the release pipeline (untested)René 'Necoro' Neumann2-0/+12