Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to sync specific account & folder #104. Docs typo. Fix default other smtp port to from 993 to 586. #106

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion sync/config/default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ db[sleep_minutes] = 1
sync[wait_seconds] = 10
sync[sleep_minutes] = 15
timezone = "America/New_York"
ignored_folders[] = "[Gmail]"

[log]

Expand All @@ -36,6 +37,7 @@ hostname = "localhost"

services[] = "GMail"
services[] = "Yahoo"
services[] = "Office365"
services[] = "Other"
gmail[domain] = "gmail.com"
gmail[host] = "imap.gmail.com"
Expand All @@ -47,8 +49,12 @@ yahoo[host] = "imap.mail.yahoo.com"
yahoo[port] = 993
yahoo[smtp_host] = "imap.mail.yahoo.com"
yahoo[smtp_port] = "smtp.mail.yahoo.com"
office365[host] = "outlook.office365.com"
office365[port] = 993
office365[smtp_host] = "smtp.office365.com"
office365[smtp_port] = 587
other[port] = 993
other[smtp_port] = 993
other[smtp_port] = 587
attachments[path] = "attachments"

[daemon]
Expand Down
7 changes: 7 additions & 0 deletions sync/src/Console/SyncConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SyncConsole extends Console
public $sleep;
public $create;
public $folder;
public $email;
public $daemon;
public $actions;
public $verbose;
Expand Down Expand Up @@ -87,6 +88,11 @@ protected function setupArgs()
'longPrefix' => 'folder',
'description' => 'Sync the selected folder'
],
'email' => [
'prefix' => 'm',
'longPrefix' => 'email',
'description' => 'Sync the selected email account (passing email)'
],
'help' => [
'prefix' => 'h',
'longPrefix' => 'help',
Expand Down Expand Up @@ -170,6 +176,7 @@ protected function parseArgs()
$this->diagnostics = $this->cli->arguments->get('diagnostics');
$this->interactive = $this->cli->arguments->get('interactive');
$this->databaseExists = $this->cli->arguments->get('exists');
$this->email = $this->cli->arguments->get('email');

// Some flags also enable interactive mode
if (true === $this->sleep
Expand Down
50 changes: 47 additions & 3 deletions sync/src/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Sync
private $maxRetries = 5;
private $retriesFolders;
private $retriesMessages;
private $email;
private $ignoredFolders;

// Config
const READY_THRESHOLD = 60;
Expand Down Expand Up @@ -101,15 +103,37 @@ public function __construct(Container $di = null)
$this->quick = $di['console']->quick;
$this->sleep = $di['console']->sleep;
$this->folder = $di['console']->folder;
$this->email = $di['console']->email;
$this->daemon = $di['console']->daemon;
$this->actions = $di['console']->actions;
$this->threading = $di['console']->threading;
$this->interactive = $di['console']->interactive;
if (array_key_exists('ignored_folders', $di['config']['app'])) {
$this->ignoredFolders = $di['config']['app']['ignored_folders'];
} else {
$this->ignoredFolders = [];
}
}

$this->initGc();
}

/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}

/**
* @param mixed $email
*/
public function setEmail($email)
{
$this->email = $email;
}

/**
* @param CLImate $cli
*/
Expand All @@ -134,6 +158,22 @@ public function setConfig(array $config)
$this->config = $config;
}

/**
* @return array
*/
public function getIgnoredFolders(): array
{
return $this->ignoredFolders;
}

/**
* @param array $ignoredFolders
*/
public function setIgnoredFolders(array $ignoredFolders)
{
$this->ignoredFolders = $ignoredFolders;
}

/**
* Runs sync forever. This is a while loop that runs a sync
* for all accounts, then sleeps for a designated period of
Expand All @@ -143,6 +183,7 @@ public function loop()
{
$wakeUnix = 0;
$sleepMinutes = $this->config['app']['sync']['sleep_minutes'];
$account = ((new AccountModel)->getByEmail($this->email)) ?: null;

while (true) {
$this->gc();
Expand All @@ -157,7 +198,7 @@ public function loop()
// Run action sync every minute
if ($this->isReadyToRun()) {
$this->setAsleep(false);
$this->run(null, [self::OPT_ONLY_SYNC_ACTIONS => true]);
$this->run($account, [self::OPT_ONLY_SYNC_ACTIONS => true]);
$this->setAsleep(true);
}

Expand All @@ -167,7 +208,7 @@ public function loop()

$this->setAsleep(false);

if (! $this->run()) {
if (! $this->run($account)) {
throw new TerminateException('Sync was prevented from running');
}

Expand Down Expand Up @@ -572,7 +613,8 @@ private function syncFolders(AccountModel $account)
$this->log,
$this->cli,
$this->emitter,
$this->interactive
$this->interactive,
$this->ignoredFolders
);
$folderList = $this->mailbox->getFolders();
$savedFolders = (new FolderModel)->getByAccount($account->getId());
Expand Down Expand Up @@ -832,4 +874,6 @@ private function checkForClosedConnection(Exception $e)
throw new StopException;
}
}


}
28 changes: 22 additions & 6 deletions sync/src/Sync/Folders.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ class Folders
private $cli;
private $emitter;
private $interactive;

const IGNORED_LIST = [
'[Gmail]'
];
private $ignoredFolders;

public function __construct(
Logger $log,
CLImate $cli,
Emitter $emitter,
bool $interactive
bool $interactive,
array $ignoredFolders
) {
$this->log = $log;
$this->cli = $cli;
$this->emitter = $emitter;
$this->interactive = $interactive;
$this->ignoredFolders = $ignoredFolders;
}

/**
Expand Down Expand Up @@ -155,8 +154,25 @@ private function removeOldFolders(
*/
private function getIgnored(string $folderName)
{
return in_array($folderName, self::IGNORED_LIST)
return in_array($folderName, $this->ignoredFolders)
? 1
: 0;
}

/**
* @return array
*/
public function getIgnoredFolders(): array
{
return $this->ignoredFolders;
}

/**
* @param array $ignoredFolders
*/
public function setIgnoredFolders(array $ignoredFolders)
{
$this->ignoredFolders = $ignoredFolders;
}

}