2022-04-30 01:08:54 +02:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (w *IMAPWorker) handleConfigure(msg *types.Configure) error {
|
|
|
|
u, err := url.Parse(msg.Config.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.config.scheme = u.Scheme
|
|
|
|
if strings.HasSuffix(w.config.scheme, "+insecure") {
|
|
|
|
w.config.scheme = strings.TrimSuffix(w.config.scheme, "+insecure")
|
|
|
|
w.config.insecure = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(w.config.scheme, "+oauthbearer") {
|
|
|
|
w.config.scheme = strings.TrimSuffix(w.config.scheme, "+oauthbearer")
|
|
|
|
w.config.oauthBearer.Enabled = true
|
|
|
|
q := u.Query()
|
|
|
|
|
|
|
|
oauth2 := &oauth2.Config{}
|
|
|
|
if q.Get("token_endpoint") != "" {
|
|
|
|
oauth2.ClientID = q.Get("client_id")
|
|
|
|
oauth2.ClientSecret = q.Get("client_secret")
|
|
|
|
oauth2.Scopes = []string{q.Get("scope")}
|
|
|
|
oauth2.Endpoint.TokenURL = q.Get("token_endpoint")
|
|
|
|
}
|
|
|
|
w.config.oauthBearer.OAuth2 = oauth2
|
|
|
|
}
|
|
|
|
|
2022-09-28 19:49:11 +02:00
|
|
|
if strings.HasSuffix(w.config.scheme, "+xoauth2") {
|
|
|
|
w.config.scheme = strings.TrimSuffix(w.config.scheme, "+xoauth2")
|
|
|
|
w.config.xoauth2.Enabled = true
|
|
|
|
q := u.Query()
|
|
|
|
|
|
|
|
oauth2 := &oauth2.Config{}
|
|
|
|
if q.Get("token_endpoint") != "" {
|
|
|
|
oauth2.ClientID = q.Get("client_id")
|
|
|
|
oauth2.ClientSecret = q.Get("client_secret")
|
|
|
|
oauth2.Scopes = []string{q.Get("scope")}
|
|
|
|
oauth2.Endpoint.TokenURL = q.Get("token_endpoint")
|
|
|
|
}
|
|
|
|
w.config.xoauth2.OAuth2 = oauth2
|
|
|
|
}
|
|
|
|
|
2022-04-30 01:08:54 +02:00
|
|
|
w.config.addr = u.Host
|
|
|
|
if !strings.ContainsRune(w.config.addr, ':') {
|
|
|
|
w.config.addr += ":" + w.config.scheme
|
|
|
|
}
|
|
|
|
|
|
|
|
w.config.user = u.User
|
|
|
|
w.config.folders = msg.Config.Folders
|
2022-04-30 01:08:57 +02:00
|
|
|
|
2022-04-30 01:08:54 +02:00
|
|
|
w.config.idle_timeout = 10 * time.Second
|
2022-04-30 01:08:57 +02:00
|
|
|
w.config.idle_debounce = 10 * time.Millisecond
|
|
|
|
|
2022-04-30 01:08:54 +02:00
|
|
|
w.config.connection_timeout = 30 * time.Second
|
|
|
|
w.config.keepalive_period = 0 * time.Second
|
|
|
|
w.config.keepalive_probes = 3
|
|
|
|
w.config.keepalive_interval = 3
|
2022-04-30 01:08:56 +02:00
|
|
|
|
|
|
|
w.config.reconnect_maxwait = 30 * time.Second
|
|
|
|
|
imap: add option to cache headers
Add option to cache headers for imap accounts. Cache db is located at
$XDG_CACHE_DIR/aerc/{account name}. The cache is cleaned of stale
entries when aerc is first opened.
Two new account level configuration options are introduced:
* cache-headers (Default: false)
* cache-max-age (Default: 30 days (720 hours))
The change in worker/imap/open.go is to set the selected directory. This
is required to access the UIDVALIDITY field, which is used in
combination with the message ID to form the key for use in the cache db.
The key structure is: "header.{UIDVALIDITY}.{UID}"
Where reasonable, cache does not stop aerc from running. In general, if
there is an error in the cache, aerc should continue working as usual.
Errors are either displayed to the user or logged.
All messages are stored without flags, and when retrieved have the flags
set to SEEN. This is to prevent UI flashes. A new method to
FetchMessageFlags is introduced to update flags of cached headers. This
is done asynchronously, and the user will see their messages appear and
then any flags updated. The message will initially show as SEEN, but
will update to unread. I considered updating the cache with the
last-known flag state, however it seems prudent to spare the R/W cycle
and assume that - eventually - all messages will end up read, and if it
isn't the update will occur rather quickly.
Note that leveldb puts a lock on the database, preventing multiple
instances of aerc from accessing the cache at the same time.
Much of this work is based on previous efforts by Vladimír Magyar.
Implements: https://todo.sr.ht/~rjarry/aerc/2
Thanks: Vladimír Magyar <vladimir@mgyar.me>
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Tested-by: inwit <inwit@sindominio.net>
Reviewed-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
2022-06-15 14:23:51 +02:00
|
|
|
w.config.cacheEnabled = false
|
|
|
|
w.config.cacheMaxAge = 30 * 24 * time.Hour // 30 days
|
|
|
|
|
2022-04-30 01:08:54 +02:00
|
|
|
for key, value := range msg.Config.Params {
|
|
|
|
switch key {
|
|
|
|
case "idle-timeout":
|
|
|
|
val, err := time.ParseDuration(value)
|
|
|
|
if err != nil || val < 0 {
|
|
|
|
return fmt.Errorf(
|
2022-07-31 15:15:27 +02:00
|
|
|
"invalid idle-timeout value %v: %w",
|
2022-04-30 01:08:54 +02:00
|
|
|
value, err)
|
|
|
|
}
|
|
|
|
w.config.idle_timeout = val
|
2022-04-30 01:08:57 +02:00
|
|
|
case "idle-debounce":
|
|
|
|
val, err := time.ParseDuration(value)
|
|
|
|
if err != nil || val < 0 {
|
|
|
|
return fmt.Errorf(
|
2022-07-31 15:15:27 +02:00
|
|
|
"invalid idle-debounce value %v: %w",
|
2022-04-30 01:08:57 +02:00
|
|
|
value, err)
|
|
|
|
}
|
|
|
|
w.config.idle_debounce = val
|
2022-04-30 01:08:56 +02:00
|
|
|
case "reconnect-maxwait":
|
|
|
|
val, err := time.ParseDuration(value)
|
|
|
|
if err != nil || val < 0 {
|
|
|
|
return fmt.Errorf(
|
2022-07-31 15:15:27 +02:00
|
|
|
"invalid reconnect-maxwait value %v: %w",
|
2022-04-30 01:08:56 +02:00
|
|
|
value, err)
|
|
|
|
}
|
|
|
|
w.config.reconnect_maxwait = val
|
2022-04-30 01:08:54 +02:00
|
|
|
case "connection-timeout":
|
|
|
|
val, err := time.ParseDuration(value)
|
|
|
|
if err != nil || val < 0 {
|
|
|
|
return fmt.Errorf(
|
2022-07-31 15:15:27 +02:00
|
|
|
"invalid connection-timeout value %v: %w",
|
2022-04-30 01:08:54 +02:00
|
|
|
value, err)
|
|
|
|
}
|
|
|
|
w.config.connection_timeout = val
|
|
|
|
case "keepalive-period":
|
|
|
|
val, err := time.ParseDuration(value)
|
|
|
|
if err != nil || val < 0 {
|
|
|
|
return fmt.Errorf(
|
2022-07-31 15:15:27 +02:00
|
|
|
"invalid keepalive-period value %v: %w",
|
2022-04-30 01:08:54 +02:00
|
|
|
value, err)
|
|
|
|
}
|
|
|
|
w.config.keepalive_period = val
|
|
|
|
case "keepalive-probes":
|
|
|
|
val, err := strconv.Atoi(value)
|
|
|
|
if err != nil || val < 0 {
|
|
|
|
return fmt.Errorf(
|
2022-07-31 15:15:27 +02:00
|
|
|
"invalid keepalive-probes value %v: %w",
|
2022-04-30 01:08:54 +02:00
|
|
|
value, err)
|
|
|
|
}
|
|
|
|
w.config.keepalive_probes = val
|
|
|
|
case "keepalive-interval":
|
|
|
|
val, err := time.ParseDuration(value)
|
|
|
|
if err != nil || val < 0 {
|
|
|
|
return fmt.Errorf(
|
2022-07-31 15:15:27 +02:00
|
|
|
"invalid keepalive-interval value %v: %w",
|
2022-04-30 01:08:54 +02:00
|
|
|
value, err)
|
|
|
|
}
|
|
|
|
w.config.keepalive_interval = int(val.Seconds())
|
imap: add option to cache headers
Add option to cache headers for imap accounts. Cache db is located at
$XDG_CACHE_DIR/aerc/{account name}. The cache is cleaned of stale
entries when aerc is first opened.
Two new account level configuration options are introduced:
* cache-headers (Default: false)
* cache-max-age (Default: 30 days (720 hours))
The change in worker/imap/open.go is to set the selected directory. This
is required to access the UIDVALIDITY field, which is used in
combination with the message ID to form the key for use in the cache db.
The key structure is: "header.{UIDVALIDITY}.{UID}"
Where reasonable, cache does not stop aerc from running. In general, if
there is an error in the cache, aerc should continue working as usual.
Errors are either displayed to the user or logged.
All messages are stored without flags, and when retrieved have the flags
set to SEEN. This is to prevent UI flashes. A new method to
FetchMessageFlags is introduced to update flags of cached headers. This
is done asynchronously, and the user will see their messages appear and
then any flags updated. The message will initially show as SEEN, but
will update to unread. I considered updating the cache with the
last-known flag state, however it seems prudent to spare the R/W cycle
and assume that - eventually - all messages will end up read, and if it
isn't the update will occur rather quickly.
Note that leveldb puts a lock on the database, preventing multiple
instances of aerc from accessing the cache at the same time.
Much of this work is based on previous efforts by Vladimír Magyar.
Implements: https://todo.sr.ht/~rjarry/aerc/2
Thanks: Vladimír Magyar <vladimir@mgyar.me>
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Tested-by: inwit <inwit@sindominio.net>
Reviewed-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
2022-06-15 14:23:51 +02:00
|
|
|
case "cache-headers":
|
|
|
|
cache, err := strconv.ParseBool(value)
|
|
|
|
if err != nil {
|
|
|
|
// Return an error here because the user tried to set header
|
|
|
|
// caching, and we want them to know they didn't set it right -
|
|
|
|
// one way or the other
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("invalid cache-headers value %v: %w", value, err)
|
imap: add option to cache headers
Add option to cache headers for imap accounts. Cache db is located at
$XDG_CACHE_DIR/aerc/{account name}. The cache is cleaned of stale
entries when aerc is first opened.
Two new account level configuration options are introduced:
* cache-headers (Default: false)
* cache-max-age (Default: 30 days (720 hours))
The change in worker/imap/open.go is to set the selected directory. This
is required to access the UIDVALIDITY field, which is used in
combination with the message ID to form the key for use in the cache db.
The key structure is: "header.{UIDVALIDITY}.{UID}"
Where reasonable, cache does not stop aerc from running. In general, if
there is an error in the cache, aerc should continue working as usual.
Errors are either displayed to the user or logged.
All messages are stored without flags, and when retrieved have the flags
set to SEEN. This is to prevent UI flashes. A new method to
FetchMessageFlags is introduced to update flags of cached headers. This
is done asynchronously, and the user will see their messages appear and
then any flags updated. The message will initially show as SEEN, but
will update to unread. I considered updating the cache with the
last-known flag state, however it seems prudent to spare the R/W cycle
and assume that - eventually - all messages will end up read, and if it
isn't the update will occur rather quickly.
Note that leveldb puts a lock on the database, preventing multiple
instances of aerc from accessing the cache at the same time.
Much of this work is based on previous efforts by Vladimír Magyar.
Implements: https://todo.sr.ht/~rjarry/aerc/2
Thanks: Vladimír Magyar <vladimir@mgyar.me>
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Tested-by: inwit <inwit@sindominio.net>
Reviewed-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
2022-06-15 14:23:51 +02:00
|
|
|
}
|
|
|
|
w.config.cacheEnabled = cache
|
|
|
|
case "cache-max-age":
|
|
|
|
val, err := time.ParseDuration(value)
|
|
|
|
if err != nil || val < 0 {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("invalid cache-max-age value %v: %w", value, err)
|
imap: add option to cache headers
Add option to cache headers for imap accounts. Cache db is located at
$XDG_CACHE_DIR/aerc/{account name}. The cache is cleaned of stale
entries when aerc is first opened.
Two new account level configuration options are introduced:
* cache-headers (Default: false)
* cache-max-age (Default: 30 days (720 hours))
The change in worker/imap/open.go is to set the selected directory. This
is required to access the UIDVALIDITY field, which is used in
combination with the message ID to form the key for use in the cache db.
The key structure is: "header.{UIDVALIDITY}.{UID}"
Where reasonable, cache does not stop aerc from running. In general, if
there is an error in the cache, aerc should continue working as usual.
Errors are either displayed to the user or logged.
All messages are stored without flags, and when retrieved have the flags
set to SEEN. This is to prevent UI flashes. A new method to
FetchMessageFlags is introduced to update flags of cached headers. This
is done asynchronously, and the user will see their messages appear and
then any flags updated. The message will initially show as SEEN, but
will update to unread. I considered updating the cache with the
last-known flag state, however it seems prudent to spare the R/W cycle
and assume that - eventually - all messages will end up read, and if it
isn't the update will occur rather quickly.
Note that leveldb puts a lock on the database, preventing multiple
instances of aerc from accessing the cache at the same time.
Much of this work is based on previous efforts by Vladimír Magyar.
Implements: https://todo.sr.ht/~rjarry/aerc/2
Thanks: Vladimír Magyar <vladimir@mgyar.me>
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Tested-by: inwit <inwit@sindominio.net>
Reviewed-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
2022-06-15 14:23:51 +02:00
|
|
|
}
|
|
|
|
w.config.cacheMaxAge = val
|
2022-04-30 01:08:54 +02:00
|
|
|
}
|
|
|
|
}
|
imap: add option to cache headers
Add option to cache headers for imap accounts. Cache db is located at
$XDG_CACHE_DIR/aerc/{account name}. The cache is cleaned of stale
entries when aerc is first opened.
Two new account level configuration options are introduced:
* cache-headers (Default: false)
* cache-max-age (Default: 30 days (720 hours))
The change in worker/imap/open.go is to set the selected directory. This
is required to access the UIDVALIDITY field, which is used in
combination with the message ID to form the key for use in the cache db.
The key structure is: "header.{UIDVALIDITY}.{UID}"
Where reasonable, cache does not stop aerc from running. In general, if
there is an error in the cache, aerc should continue working as usual.
Errors are either displayed to the user or logged.
All messages are stored without flags, and when retrieved have the flags
set to SEEN. This is to prevent UI flashes. A new method to
FetchMessageFlags is introduced to update flags of cached headers. This
is done asynchronously, and the user will see their messages appear and
then any flags updated. The message will initially show as SEEN, but
will update to unread. I considered updating the cache with the
last-known flag state, however it seems prudent to spare the R/W cycle
and assume that - eventually - all messages will end up read, and if it
isn't the update will occur rather quickly.
Note that leveldb puts a lock on the database, preventing multiple
instances of aerc from accessing the cache at the same time.
Much of this work is based on previous efforts by Vladimír Magyar.
Implements: https://todo.sr.ht/~rjarry/aerc/2
Thanks: Vladimír Magyar <vladimir@mgyar.me>
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Tested-by: inwit <inwit@sindominio.net>
Reviewed-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
2022-06-15 14:23:51 +02:00
|
|
|
if w.config.cacheEnabled {
|
|
|
|
w.initCacheDb(msg.Config.Name)
|
|
|
|
}
|
2022-04-30 01:08:55 +02:00
|
|
|
w.idler = newIdler(w.config, w.worker)
|
2022-04-30 01:08:56 +02:00
|
|
|
w.observer = newObserver(w.config, w.worker)
|
2022-04-30 01:08:55 +02:00
|
|
|
|
2022-04-30 01:08:54 +02:00
|
|
|
return nil
|
|
|
|
}
|