-
-
Notifications
You must be signed in to change notification settings - Fork 811
/
IdleClient.cs
242 lines (207 loc) · 7.79 KB
/
IdleClient.cs
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
//
// IdleClient.cs
//
// Author: Jeffrey Stedfast <jeff@xamarin.com>
//
// Copyright (c) 2014-2024 Jeffrey Stedfast
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using MailKit;
using MailKit.Net.Imap;
namespace ImapIdle
{
public class IdleClient : IDisposable
{
readonly List<IMessageSummary> messages;
CancellationTokenSource cancel;
CancellationTokenSource done;
readonly FetchRequest request;
readonly ImapClient client;
bool messagesArrived;
public IdleClient ()
{
client = new ImapClient (new ProtocolLogger (Console.OpenStandardError ()));
request = new FetchRequest (MessageSummaryItems.Full | MessageSummaryItems.UniqueId);
messages = new List<IMessageSummary> ();
cancel = new CancellationTokenSource ();
}
async Task ReconnectAsync ()
{
if (!client.IsConnected)
await client.ConnectAsync (Program.Host, Program.Port, Program.SslOptions, cancel.Token);
if (!client.IsAuthenticated) {
await client.AuthenticateAsync (Program.Username, Program.Password, cancel.Token);
await client.Inbox.OpenAsync (FolderAccess.ReadOnly, cancel.Token);
}
}
async Task FetchMessageSummariesAsync (bool print)
{
IList<IMessageSummary> fetched;
do {
try {
// fetch summary information for messages that we don't already have
int startIndex = messages.Count;
fetched = client.Inbox.Fetch (startIndex, -1, request, cancel.Token);
break;
} catch (ImapProtocolException) {
// protocol exceptions often result in the client getting disconnected
await ReconnectAsync ();
} catch (IOException) {
// I/O exceptions always result in the client getting disconnected
await ReconnectAsync ();
}
} while (true);
foreach (var message in fetched) {
if (print)
Console.WriteLine ("{0}: new message: {1}", client.Inbox, message.Envelope.Subject);
messages.Add (message);
}
}
async Task WaitForNewMessagesAsync ()
{
do {
try {
if (client.Capabilities.HasFlag (ImapCapabilities.Idle)) {
// Note: IMAP servers are only supposed to drop the connection after 30 minutes, so normally
// we'd IDLE for a max of, say, ~29 minutes... but GMail seems to drop idle connections after
// about 10 minutes, so we'll only idle for 9 minutes.
done = new CancellationTokenSource (TimeSpan.FromMinutes (9));
try {
await client.IdleAsync (done.Token, cancel.Token);
} finally {
done.Dispose ();
done = null;
}
} else {
// Note: we don't want to spam the IMAP server with NOOP commands, so lets wait a minute
// between each NOOP command.
await Task.Delay (TimeSpan.FromMinutes (1), cancel.Token);
await client.NoOpAsync (cancel.Token);
}
break;
} catch (ImapProtocolException) {
// protocol exceptions often result in the client getting disconnected
await ReconnectAsync ();
} catch (IOException) {
// I/O exceptions always result in the client getting disconnected
await ReconnectAsync ();
}
} while (true);
}
async Task IdleAsync ()
{
do {
try {
await WaitForNewMessagesAsync ();
if (messagesArrived) {
await FetchMessageSummariesAsync (true);
messagesArrived = false;
}
} catch (OperationCanceledException) {
break;
}
} while (!cancel.IsCancellationRequested);
}
public async Task RunAsync ()
{
// connect to the IMAP server and get our initial list of messages
try {
await ReconnectAsync ();
await FetchMessageSummariesAsync (false);
} catch (OperationCanceledException) {
await client.DisconnectAsync (true);
return;
}
// Note: We capture client.Inbox here because cancelling IdleAsync() *may* require
// disconnecting the IMAP client connection, and, if it does, the `client.Inbox`
// property will no longer be accessible which means we won't be able to disconnect
// our event handlers.
var inbox = client.Inbox;
// keep track of changes to the number of messages in the folder (this is how we'll tell if new messages have arrived).
inbox.CountChanged += OnCountChanged;
// keep track of messages being expunged so that when the CountChanged event fires, we can tell if it's
// because new messages have arrived vs messages being removed (or some combination of the two).
inbox.MessageExpunged += OnMessageExpunged;
// keep track of flag changes
inbox.MessageFlagsChanged += OnMessageFlagsChanged;
await IdleAsync ();
inbox.MessageFlagsChanged -= OnMessageFlagsChanged;
inbox.MessageExpunged -= OnMessageExpunged;
inbox.CountChanged -= OnCountChanged;
await client.DisconnectAsync (true);
}
// Note: the CountChanged event will fire when new messages arrive in the folder and/or when messages are expunged.
void OnCountChanged (object sender, EventArgs e)
{
var folder = (ImapFolder) sender;
// Note: because we are keeping track of the MessageExpunged event and updating our
// 'messages' list, we know that if we get a CountChanged event and folder.Count is
// larger than messages.Count, then it means that new messages have arrived.
if (folder.Count > messages.Count) {
int arrived = folder.Count - messages.Count;
if (arrived > 1)
Console.WriteLine ("\t{0} new messages have arrived.", arrived);
else
Console.WriteLine ("\t1 new message has arrived.");
// Note: your first instinct may be to fetch these new messages now, but you cannot do
// that in this event handler (the ImapFolder is not re-entrant).
//
// Instead, cancel the `done` token and update our state so that we know new messages
// have arrived. We'll fetch the summaries for these new messages later...
messagesArrived = true;
done?.Cancel ();
}
}
void OnMessageExpunged (object sender, MessageEventArgs e)
{
var folder = (ImapFolder) sender;
if (e.Index < messages.Count) {
var message = messages[e.Index];
Console.WriteLine ("{0}: message #{1} has been expunged: {2}", folder, e.Index, message.Envelope.Subject);
// Note: If you are keeping a local cache of message information
// (e.g. MessageSummary data) for the folder, then you'll need
// to remove the message at e.Index.
messages.RemoveAt (e.Index);
} else {
Console.WriteLine ("{0}: message #{1} has been expunged.", folder, e.Index);
}
}
void OnMessageFlagsChanged (object sender, MessageFlagsChangedEventArgs e)
{
var folder = (ImapFolder) sender;
Console.WriteLine ("{0}: flags have changed for message #{1} ({2}).", folder, e.Index, e.Flags);
}
public void Exit ()
{
cancel.Cancel ();
}
public void Dispose ()
{
client.Dispose ();
cancel.Dispose ();
GC.SuppressFinalize (this);
}
}
}