-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathMailboxSync.php
311 lines (279 loc) Β· 9.79 KB
/
MailboxSync.php
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
declare(strict_types=1);
/**
* @copyright 2019 Christoph Wurst <[email protected]>
*
* @author 2019 Christoph Wurst <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OCA\Mail\IMAP;
use Horde_Imap_Client;
use Horde_Imap_Client_Data_Namespace;
use Horde_Imap_Client_Exception;
use Horde_Imap_Client_Namespace_List;
use OCA\Mail\Account;
use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Db\MailboxMapper;
use OCA\Mail\Events\MailboxesSynchronizedEvent;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Folder;
use OCP\AppFramework\Db\TTransactional;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;
use function array_combine;
use function array_map;
use function array_reduce;
use function array_slice;
use function in_array;
use function json_encode;
use function shuffle;
use function sprintf;
use function str_starts_with;
class MailboxSync {
use TTransactional;
/** @var MailboxMapper */
private $mailboxMapper;
/** @var FolderMapper */
private $folderMapper;
/** @var MailAccountMapper */
private $mailAccountMapper;
/** @var IMAPClientFactory */
private $imapClientFactory;
/** @var ITimeFactory */
private $timeFactory;
/** @var IEventDispatcher */
private $dispatcher;
private IDBConnection $dbConnection;
public function __construct(MailboxMapper $mailboxMapper,
FolderMapper $folderMapper,
MailAccountMapper $mailAccountMapper,
IMAPClientFactory $imapClientFactory,
ITimeFactory $timeFactory,
IEventDispatcher $dispatcher,
IDBConnection $dbConnection) {
$this->mailboxMapper = $mailboxMapper;
$this->folderMapper = $folderMapper;
$this->mailAccountMapper = $mailAccountMapper;
$this->imapClientFactory = $imapClientFactory;
$this->timeFactory = $timeFactory;
$this->dispatcher = $dispatcher;
$this->dbConnection = $dbConnection;
}
/**
* @throws ServiceException
*/
public function sync(Account $account,
LoggerInterface $logger,
bool $force = false): void {
if (!$force && $account->getMailAccount()->getLastMailboxSync() >= ($this->timeFactory->getTime() - 7200)) {
$logger->debug("account is up to date, skipping mailbox sync");
return;
}
$client = $this->imapClientFactory->getClient($account);
try {
try {
$namespaces = $client->getNamespaces([], [
'ob_return' => true,
]);
$personalNamespace = $this->getPersonalNamespace($namespaces);
$account->getMailAccount()->setPersonalNamespace(
$personalNamespace
);
} catch (Horde_Imap_Client_Exception $e) {
$logger->debug('Getting namespaces for account ' . $account->getId() . ' failed: ' . $e->getMessage(), [
'exception' => $e,
]);
$namespaces = null;
$personalNamespace = null;
}
try {
$folders = $this->folderMapper->getFolders($account, $client);
$this->folderMapper->fetchFolderAcls($folders, $client);
} catch (Horde_Imap_Client_Exception $e) {
throw new ServiceException(
sprintf("IMAP error synchronizing account %d: %s", $account->getId(), $e->getMessage()),
$e->getCode(),
$e
);
}
$this->folderMapper->detectFolderSpecialUse($folders);
$mailboxes = $this->atomic(function () use ($account, $folders, $namespaces) {
$old = $this->mailboxMapper->findAll($account);
$indexedOld = array_combine(
array_map(static function (Mailbox $mb) {
return $mb->getName();
}, $old),
$old
);
return $this->persist($account, $folders, $indexedOld, $namespaces);
}, $this->dbConnection);
$this->syncMailboxStatus($mailboxes, $personalNamespace, $client);
$this->dispatcher->dispatchTyped(
new MailboxesSynchronizedEvent($account)
);
} finally {
$client->logout();
}
}
/**
* Sync unread and total message statistics.
*
* @param Account $account
* @param Mailbox $mailbox
*
* @throws ServiceException
*/
public function syncStats(Account $account, Mailbox $mailbox): void {
$client = $this->imapClientFactory->getClient($account);
try {
$stats = $this->folderMapper->getFoldersStatusAsObject($client, [$mailbox->getName()])[$mailbox->getName()];
} catch (Horde_Imap_Client_Exception $e) {
$id = $mailbox->getId();
throw new ServiceException(
"Could not fetch stats of mailbox $id. IMAP error: " . $e->getMessage(),
$e->getCode(),
$e
);
} finally {
$client->logout();
}
$mailbox->setMessages($stats->getTotal());
$mailbox->setUnseen($stats->getUnread());
$this->mailboxMapper->update($mailbox);
}
/**
* @return Mailbox[]
*/
private function persist(Account $account,
array $folders,
array $existing,
?Horde_Imap_Client_Namespace_List $namespaces): array {
$mailboxes = [];
foreach ($folders as $folder) {
if (isset($existing[$folder->getMailbox()])) {
$mailboxes[] = $this->updateMailboxFromFolder(
$folder,
$existing[$folder->getMailbox()],
$namespaces,
);
} else {
$mailboxes[] = $this->createMailboxFromFolder(
$account,
$folder,
$namespaces,
);
}
unset($existing[$folder->getMailbox()]);
}
foreach ($existing as $leftover) {
$this->mailboxMapper->delete($leftover);
}
$account->getMailAccount()->setLastMailboxSync($this->timeFactory->getTime());
$this->mailAccountMapper->update($account->getMailAccount());
return $mailboxes;
}
private function getPersonalNamespace(Horde_Imap_Client_Namespace_List $namespaces): ?string {
foreach ($namespaces as $namespace) {
/** @var Horde_Imap_Client_Data_Namespace $namespace */
if ($namespace->type === Horde_Imap_Client::NS_PERSONAL) {
return $namespace->name !== "" ? $namespace->name : null;
}
}
return null;
}
private function updateMailboxFromFolder(Folder $folder, Mailbox $mailbox, ?Horde_Imap_Client_Namespace_List $namespaces): Mailbox {
$mailbox->setAttributes(json_encode($folder->getAttributes()));
$mailbox->setDelimiter($folder->getDelimiter());
$status = $folder->getStatus();
if ($status !== null) {
$mailbox->setMessages($status['messages'] ?? 0);
$mailbox->setUnseen($status['unseen'] ?? 0);
}
$mailbox->setSelectable(!in_array('\noselect', $folder->getAttributes()));
$mailbox->setSpecialUse(json_encode($folder->getSpecialUse()));
$mailbox->setMyAcls($folder->getMyAcls());
$mailbox->setShared($this->isMailboxShared($namespaces, $mailbox));
return $this->mailboxMapper->update($mailbox);
}
private function createMailboxFromFolder(Account $account, Folder $folder, ?Horde_Imap_Client_Namespace_List $namespaces): Mailbox {
$mailbox = new Mailbox();
$mailbox->setName($folder->getMailbox());
$mailbox->setAccountId($account->getId());
$mailbox->setAttributes(json_encode($folder->getAttributes()));
$mailbox->setDelimiter($folder->getDelimiter());
$status = $folder->getStatus();
$mailbox->setMessages($status['messages'] ?? 0);
$mailbox->setUnseen($status['unseen'] ?? 0);
$mailbox->setSelectable(!in_array('\noselect', $folder->getAttributes()));
$mailbox->setSpecialUse(json_encode($folder->getSpecialUse()));
$mailbox->setMyAcls($folder->getMyAcls());
$mailbox->setShared($this->isMailboxShared($namespaces, $mailbox));
$mailbox->setNameHash(md5($folder->getMailbox()));
return $this->mailboxMapper->insert($mailbox);
}
private function isMailboxShared(?Horde_Imap_Client_Namespace_List $namespaces, Mailbox $mailbox): bool {
foreach (($namespaces ?? []) as $namespace) {
/** @var Horde_Imap_Client_Data_Namespace $namespace */
if ($namespace->type === Horde_Imap_Client_Data_Namespace::NS_OTHER && str_starts_with($mailbox->getName(), $namespace->name)) {
return true;
}
}
return false;
}
private function syncMailboxStatus(mixed $mailboxes, ?string $personalNamespace, \Horde_Imap_Client_Socket $client): void {
/** @var array{0: Mailbox[], 1: Mailbox[]} */
[$sync, $doNotSync] = array_reduce($mailboxes, function (array $carry, Mailbox $mailbox) use ($personalNamespace): array {
/** @var array{0: Mailbox[], 1: Mailbox[]} $carry */
[$sync, $doNotSync] = $carry;
$inboxName = $personalNamespace === null ? "INBOX" : ($personalNamespace . $mailbox->getDelimiter() . "INBOX");
if ($inboxName === $mailbox->getName() || $mailbox->getSyncInBackground()) {
return [
array_merge($sync, [$mailbox]),
$doNotSync,
];
}
return [
$sync,
array_merge($doNotSync, [$mailbox]),
];
}, [[], []]);
// Synchronize the mailboxes selected for sync and keep the rest updated occasionally
shuffle($doNotSync);
/** @var Mailbox[] $syncStatus */
$syncStatus = [...$sync, ...array_slice($doNotSync, 0, 5)];
$statuses = $this->folderMapper->getFoldersStatusAsObject($client, array_map(function (Mailbox $mailbox) {
return $mailbox->getName();
}, $syncStatus));
foreach ($syncStatus as $mailbox) {
$status = $statuses[$mailbox->getName()];
if(isset($status)) {
$mailbox->setMessages($status->getTotal());
$mailbox->setUnseen($status->getUnread());
}
}
$this->atomic(function () use ($syncStatus) {
foreach ($syncStatus as $mailbox) {
if(isset($statuses[$mailbox->getName()])) {
$this->mailboxMapper->update($mailbox);
}
}
}, $this->dbConnection);
}
}