Page MenuHomePhorge

D4992.1775294044.diff
No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None

D4992.1775294044.diff

diff --git a/src/app/Backends/IMAP.php b/src/app/Backends/IMAP.php
--- a/src/app/Backends/IMAP.php
+++ b/src/app/Backends/IMAP.php
@@ -144,7 +144,7 @@
*
* @param string $mailbox Folder name
*/
- protected static function deleteMailbox($mailbox): bool
+ public static function deleteMailbox($mailbox): bool
{
$config = self::getConfig();
$imap = self::initIMAP($config);
@@ -169,6 +169,26 @@
return $result;
}
+ /**
+ * Empty a mailbox
+ *
+ * @param string $mailbox Folder name
+ */
+ public static function clearMailbox($mailbox): bool
+ {
+ $config = self::getConfig();
+ $imap = self::initIMAP($config);
+
+ // The cyrus user needs permissions to count and delete messages
+ // TODO: maybe we should use proxy authentication instead?
+ $result = $imap->setACL($mailbox, $config['user'], 'ldr');
+ $result = $imap->clearFolder($mailbox);
+
+ $imap->closeConnection();
+
+ return $result;
+ }
+
/**
* Delete a user mailbox.
*
@@ -454,6 +474,9 @@
public static function userMailbox(string $user, string $mailbox): string
{
[$localpart, $domain] = explode('@', $user, 2);
+ if ($mailbox == "INBOX") {
+ return "user/{$localpart}@{$domain}";
+ }
return "user/{$localpart}/{$mailbox}@{$domain}";
}
diff --git a/src/app/Console/Commands/Imap/DeleteCommand.php b/src/app/Console/Commands/Imap/DeleteCommand.php
new file mode 100644
--- /dev/null
+++ b/src/app/Console/Commands/Imap/DeleteCommand.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace App\Console\Commands\Imap;
+
+use App\Console\Command;
+
+class DeleteCommand extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'imap:delete {user} {mailbox} {--clear : Clear mailbox instead}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = "Delete IMAP Folders";
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $user = $this->argument('user');
+ $mailbox = $this->argument('mailbox');
+ if ($mailbox == "*") {
+ // Reverse so subfolders are deleted before parent folders
+ foreach (array_reverse(\App\Backends\IMAP::listMailboxes($user)) as $mailbox) {
+ if ($this->option('clear')) {
+ \App\Backends\IMAP::clearMailbox($mailbox);
+ } else {
+ \App\Backends\IMAP::deleteMailbox($mailbox);
+ }
+ }
+ // Can't delete INBOX
+ \App\Backends\IMAP::clearMailbox(\App\Backends\IMAP::userMailbox($user, "INBOX"));
+ } else {
+ if ($this->option('clear')) {
+ \App\Backends\IMAP::clearMailbox(\App\Backends\IMAP::userMailbox($user, $mailbox));
+ } else {
+ \App\Backends\IMAP::deleteMailbox(\App\Backends\IMAP::userMailbox($user, $mailbox));
+ }
+ }
+ }
+}
diff --git a/src/tests/Feature/Backends/IMAPTest.php b/src/tests/Feature/Backends/IMAPTest.php
--- a/src/tests/Feature/Backends/IMAPTest.php
+++ b/src/tests/Feature/Backends/IMAPTest.php
@@ -352,12 +352,45 @@
$this->assertTrue($result);
}
+ /**
+ * Test userMailbox
+ *
+ * @group imap
+ */
+ public function testUserMailbox(): void
+ {
+ $this->assertSame(IMAP::userMailbox("john@kolab.org", "INBOX"), "user/john@kolab.org");
+ $this->assertSame(IMAP::userMailbox("john@kolab.org", "test"), "user/john/test@kolab.org");
+ }
+
+ /**
+ * Test clearMailbox
+ *
+ * @group imap
+ */
+ public function testClearMailbox(): void
+ {
+ $imap = $this->getImap("john@kolab.org");
+ $message = "From: me@example.com\r\n"
+ . "To: you@example.com\r\n"
+ . "Subject: test\r\n"
+ . "\r\n"
+ . "this is a test message, please ignore\r\n";
+ $result = $imap->append("INBOX", $message);
+ $this->assertNotFalse($result);
+
+ $result = IMAP::clearMailbox(IMAP::userMailbox("john@kolab.org", "INBOX"));
+ $this->assertTrue($result);
+
+ $this->assertSame($imap->countMessages("INBOX"), 0);
+ }
+
/**
* Get configured/initialized rcube_imap_generic instance
*/
- private function getImap()
+ private function getImap($loginAs = null)
{
- if ($this->imap) {
+ if ($this->imap && !$loginAs) {
return $this->imap;
}
@@ -369,6 +402,9 @@
$config = $config->invoke(null);
+ if ($loginAs) {
+ return $init->invokeArgs(null, [$config, $loginAs]);
+ }
return $this->imap = $init->invokeArgs(null, [$config]);
}
}

File Metadata

Mime Type
text/plain
Expires
Sat, Apr 4, 9:14 AM (12 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18785844
Default Alt Text
D4992.1775294044.diff (4 KB)

Event Timeline