about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2021-12-06 23:52:25 +0100
committerRobin Jarry <robin@jarry.cc>2021-12-07 21:26:56 +0100
commitcc432eefd32dc8652651ca0966ba733c7c47e088 (patch)
treebfe5db5d25bc76d7703661ef4550e56b55996ffa
parentabcd327359592ca7b552585a0f3837c930b126b4 (diff)
downloadaerc-cc432eefd32dc8652651ca0966ba733c7c47e088.tar.gz
imap: move connect procedure into a dedicated function
This will prepare for extra tcp connection options support and for
automatic reconnect. No functional change.

Signed-off-by: Robin Jarry <robin@jarry.cc>
-rw-r--r--worker/imap/worker.go100
1 files changed, 55 insertions, 45 deletions
diff --git a/worker/imap/worker.go b/worker/imap/worker.go
index c427e60..55469cf 100644
--- a/worker/imap/worker.go
+++ b/worker/imap/worker.go
@@ -107,54 +107,11 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
 		w.config.user = u.User
 		w.config.folders = msg.Config.Folders
 	case *types.Connect:
-		var (
-			c   *client.Client
-			err error
-		)
 		if w.client != nil && w.client.State() == imap.SelectedState {
 			return fmt.Errorf("Already connected")
 		}
-		switch w.config.scheme {
-		case "imap":
-			c, err = client.Dial(w.config.addr)
-			if err != nil {
-				return err
-			}
-
-			if !w.config.insecure {
-				if err := c.StartTLS(&tls.Config{}); err != nil {
-					return err
-				}
-			}
-		case "imaps":
-			c, err = client.DialTLS(w.config.addr, &tls.Config{})
-			if err != nil {
-				return err
-			}
-		default:
-			return fmt.Errorf("Unknown IMAP scheme %s", w.config.scheme)
-		}
-		c.ErrorLog = w.worker.Logger
-
-		if w.config.user != nil {
-			username := w.config.user.Username()
-			password, hasPassword := w.config.user.Password()
-			if !hasPassword {
-				// TODO: ask password
-			}
-
-			if w.config.oauthBearer.Enabled {
-				if err := w.config.oauthBearer.Authenticate(username, password, c); err != nil {
-					return err
-				}
-			} else if err := c.Login(username, password); err != nil {
-				return err
-			}
-		}
-
-		c.SetDebug(w.worker.Logger.Writer())
-
-		if _, err := c.Select(imap.InboxName, false); err != nil {
+		c, err := w.connect()
+		if err != nil {
 			return err
 		}
 
@@ -255,6 +212,59 @@ func (w *IMAPWorker) handleImapUpdate(update client.Update) {
 	}
 }
 
+func (w *IMAPWorker) connect() (*client.Client, error) {
+	var (
+		c   *client.Client
+		err error
+	)
+	switch w.config.scheme {
+	case "imap":
+		c, err = client.Dial(w.config.addr)
+		if err != nil {
+			return nil, err
+		}
+
+		if !w.config.insecure {
+			if err := c.StartTLS(&tls.Config{}); err != nil {
+				return nil, err
+			}
+		}
+	case "imaps":
+		c, err = client.DialTLS(w.config.addr, &tls.Config{})
+		if err != nil {
+			return nil, err
+		}
+	default:
+		return nil, fmt.Errorf("Unknown IMAP scheme %s", w.config.scheme)
+	}
+	c.ErrorLog = w.worker.Logger
+
+	if w.config.user != nil {
+		username := w.config.user.Username()
+		password, hasPassword := w.config.user.Password()
+		if !hasPassword {
+			// TODO: ask password
+		}
+
+		if w.config.oauthBearer.Enabled {
+			if err := w.config.oauthBearer.Authenticate(
+				username, password, c); err != nil {
+				return nil, err
+			}
+		} else if err := c.Login(username, password); err != nil {
+			return nil, err
+		}
+	}
+
+	c.SetDebug(w.worker.Logger.Writer())
+
+	if _, err := c.Select(imap.InboxName, false); err != nil {
+		return nil, err
+	}
+
+	return c, nil
+}
+
 func (w *IMAPWorker) Run() {
 	for {
 		select {
489abf ^
669a56449 ^

















9faad7591 ^
669a56449 ^











9852cf804 ^
669a56449 ^
669a56449 ^










1255b3c86 ^
2dea92037 ^
669a56449 ^
c35232971 ^
669a56449 ^
9e37e3e5e ^
669a56449 ^











8c1083d3b ^
bd689849f ^
669a56449 ^







dd8a6ef3a ^
9ecb1aae8 ^
669a56449 ^
d07489abf ^


669a56449 ^









dc38b88f7 ^
669a56449 ^

2dea92037 ^
669a56449 ^
d07489abf ^
9e37e3e5e ^
9852cf804 ^
4137a4dbf ^
669a56449 ^
8c1083d3b ^
9ecb1aae8 ^
d07489abf ^

669a56449 ^


















d07489abf ^
dae545094 ^

43832f8e5 ^
dae545094 ^


0a8762eb7 ^
669a56449 ^

d07489abf ^
669a56449 ^




86556ebfd ^

669a56449 ^





c640bd2d1 ^
669a56449 ^


c94647aec ^
669a56449 ^


c640bd2d1 ^
669a56449 ^


























cb9110c43 ^
b3c3a4631 ^

669a56449 ^
c94647aec ^
669a56449 ^



c94647aec ^
669a56449 ^




3d88d06b3 ^



669a56449 ^

cae197385 ^
669a56449 ^
cae197385 ^

669a56449 ^

8fc7cecfa ^
669a56449 ^











1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272