| 1 |
package imap |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"fmt" |
| 6 |
"net/url" |
| 7 |
"os" |
| 8 |
"strconv" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
|
| 12 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 13 |
"git.sr.ht/~rjarry/aerc/worker/lib" |
| 14 |
"git.sr.ht/~rjarry/aerc/worker/middleware" |
| 15 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 16 |
) |
| 17 |
|
| 18 |
func (w *IMAPWorker) handleConfigure(msg *types.Configure) error { |
| 19 |
w.config.name = msg.Config.Name |
| 20 |
u, err := url.Parse(msg.Config.Source) |
| 21 |
if err != nil { |
| 22 |
return err |
| 23 |
} |
| 24 |
|
| 25 |
w.config.provider = w.providerFromURL(u.Host) |
| 26 |
w.config.url = u |
| 27 |
|
| 28 |
w.config.folders = msg.Config.Folders |
| 29 |
w.config.headers = msg.Config.Headers |
| 30 |
w.config.headersExclude = msg.Config.HeadersExclude |
| 31 |
|
| 32 |
w.config.checkMail = msg.Config.CheckMail |
| 33 |
|
| 34 |
w.config.connection_timeout = 90 * time.Second |
| 35 |
w.config.keepalive_period = 0 * time.Second |
| 36 |
w.config.keepalive_probes = 3 |
| 37 |
w.config.keepalive_interval = 3 |
| 38 |
|
| 39 |
w.config.cacheEnabled = false |
| 40 |
w.config.cacheMaxAge = 30 * 24 * time.Hour // 30 days |
| 41 |
w.config.expungePolicy = ExpungePolicyAuto |
| 42 |
|
| 43 |
for key, value := range msg.Config.Params { |
| 44 |
switch key { |
| 45 |
case "idle-timeout": |
| 46 |
val, err := time.ParseDuration(value) |
| 47 |
if err != nil || val < 0 { |
| 48 |
return fmt.Errorf( |
| 49 |
"invalid idle-timeout value %v: %w", |
| 50 |
value, err) |
| 51 |
} |
| 52 |
w.idler.timeout = val |
| 53 |
case "idle-debounce": |
| 54 |
val, err := time.ParseDuration(value) |
| 55 |
if err != nil || val < 0 { |
| 56 |
return fmt.Errorf( |
| 57 |
"invalid idle-debounce value %v: %w", |
| 58 |
value, err) |
| 59 |
} |
| 60 |
w.idler.debounce = val |
| 61 |
case "connection-timeout": |
| 62 |
val, err := time.ParseDuration(value) |
| 63 |
if err != nil || val < 0 { |
| 64 |
return fmt.Errorf( |
| 65 |
"invalid connection-timeout value %v: %w", |
| 66 |
value, err) |
| 67 |
} |
| 68 |
w.config.connection_timeout = val |
| 69 |
case "keepalive-period": |
| 70 |
val, err := time.ParseDuration(value) |
| 71 |
if err != nil || val < 0 { |
| 72 |
return fmt.Errorf( |
| 73 |
"invalid keepalive-period value %v: %w", |
| 74 |
value, err) |
| 75 |
} |
| 76 |
w.config.keepalive_period = val |
| 77 |
case "keepalive-probes": |
| 78 |
val, err := strconv.Atoi(value) |
| 79 |
if err != nil || val < 0 { |
| 80 |
return fmt.Errorf( |
| 81 |
"invalid keepalive-probes value %v: %w", |
| 82 |
value, err) |
| 83 |
} |
| 84 |
w.config.keepalive_probes = val |
| 85 |
case "keepalive-interval": |
| 86 |
val, err := time.ParseDuration(value) |
| 87 |
if err != nil || val < 0 { |
| 88 |
return fmt.Errorf( |
| 89 |
"invalid keepalive-interval value %v: %w", |
| 90 |
value, err) |
| 91 |
} |
| 92 |
w.config.keepalive_interval = int(val.Seconds()) |
| 93 |
case "cache-headers": |
| 94 |
cache, err := strconv.ParseBool(value) |
| 95 |
if err != nil { |
| 96 |
// Return an error here because the user tried to set header |
| 97 |
// caching, and we want them to know they didn't set it right - |
| 98 |
// one way or the other |
| 99 |
return fmt.Errorf("invalid cache-headers value %v: %w", value, err) |
| 100 |
} |
| 101 |
w.config.cacheEnabled = cache |
| 102 |
case "cache-max-age": |
| 103 |
val, err := time.ParseDuration(value) |
| 104 |
if err != nil || val < 0 { |
| 105 |
return fmt.Errorf("invalid cache-max-age value %v: %w", value, err) |
| 106 |
} |
| 107 |
w.config.cacheMaxAge = val |
| 108 |
case "expunge-policy": |
| 109 |
switch value { |
| 110 |
case "auto": |
| 111 |
w.config.expungePolicy = ExpungePolicyAuto |
| 112 |
case "low-to-high": |
| 113 |
w.config.expungePolicy = ExpungePolicyLowToHigh |
| 114 |
case "stable": |
| 115 |
w.config.expungePolicy = ExpungePolicyStable |
| 116 |
default: |
| 117 |
return fmt.Errorf("invalid expunge-policy value %v", value) |
| 118 |
} |
| 119 |
case "debug-log-path": |
| 120 |
w.config.debugLogPath = value |
| 121 |
} |
| 122 |
} |
| 123 |
if w.config.cacheEnabled { |
| 124 |
w.initCacheDb(msg.Config.Name) |
| 125 |
} |
| 126 |
|
| 127 |
if name, ok := msg.Config.Params["folder-map"]; ok { |
| 128 |
file := xdg.ExpandHome(name) |
| 129 |
f, err := os.Open(file) |
| 130 |
if err != nil { |
| 131 |
return err |
| 132 |
} |
| 133 |
defer f.Close() |
| 134 |
fmap, order, err := lib.ParseFolderMap(bufio.NewReader(f)) |
| 135 |
if err != nil { |
| 136 |
return err |
| 137 |
} |
| 138 |
w.worker = middleware.NewFolderMapper(w.worker, fmap, order) |
| 139 |
} |
| 140 |
|
| 141 |
return nil |
| 142 |
} |
| 143 |
|
| 144 |
func (w *IMAPWorker) providerFromURL(url string) imapProvider { |
| 145 |
isValidURLPrefix := func(url string, prefix string) bool { |
| 146 |
if !strings.HasPrefix(url, prefix) { |
| 147 |
return false |
| 148 |
} |
| 149 |
if len(url) > len(prefix) && url[len(prefix)] != ':' { |
| 150 |
// URL is not of the form "$prefix:$port" |
| 151 |
return false |
| 152 |
} |
| 153 |
return true |
| 154 |
} |
| 155 |
switch { |
| 156 |
case isValidURLPrefix(url, "imap.gmail.com"): |
| 157 |
return GMail |
| 158 |
case isValidURLPrefix(url, "127.0.0.1"): |
| 159 |
return Proton |
| 160 |
case isValidURLPrefix(url, "outlook.office365.com"): |
| 161 |
return Office365 |
| 162 |
case isValidURLPrefix(url, "imap.zoho.com"): |
| 163 |
return Zoho |
| 164 |
case isValidURLPrefix(url, "imap.fastmail.com"): |
| 165 |
return FastMail |
| 166 |
case isValidURLPrefix(url, "imap.mail.me.com"): |
| 167 |
return iCloud |
| 168 |
default: |
| 169 |
return Unknown |
| 170 |
} |
| 171 |
} |