Skip to content

Commit

Permalink
make sieve data format automatic on connect
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Nov 11, 2024
1 parent a4c136d commit 9d21016
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,7 @@ function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $ima

include_once APP_PATH.'modules/sievefilters/hm-sieve.php';
$sieveClientFactory = new Hm_Sieve_Client_Factory();
$client = $sieveClientFactory->init(null, $server);
$client = $sieveClientFactory->init(null, $server, $context->module_is_supported('nux'));

if (!$client && $show_errors) {
Hm_Msgs::add("ERRFailed to authenticate to the Sieve host");
Expand Down
4 changes: 4 additions & 0 deletions modules/nux/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -709,4 +709,8 @@ static public function details($id) {
}
return array();
}

static public function get() {
return self::$services;
}
}
24 changes: 20 additions & 4 deletions modules/sievefilters/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function get_mailbox_filters($mailbox, $site_config, $user_config)
{
$factory = get_sieve_client_factory($site_config);
try {
$client = $factory->init($user_config, $mailbox);
$client = $factory->init($user_config, $mailbox, in_array(mb_strtolower('nux'), $site_config->get_modules(true), true));
$scripts = [];
foreach ($client->listScripts() as $script) {
if (mb_strstr($script, 'cypht')) {
Expand Down Expand Up @@ -414,7 +414,7 @@ function get_blocked_senders_array($mailbox, $site_config, $user_config)
{
$factory = get_sieve_client_factory($site_config);
try {
$client = $factory->init($user_config, $mailbox);
$client = $factory->init($user_config, $mailbox, in_array(mb_strtolower('nux'), $site_config->get_modules(true), true));
$scripts = $client->listScripts();

if (array_search('blocked_senders', $scripts, true) === false) {
Expand Down Expand Up @@ -450,7 +450,7 @@ function get_blocked_senders_array($mailbox, $site_config, $user_config)
function get_blocked_senders($mailbox, $mailbox_id, $icon_svg, $icon_block_domain_svg, $site_config, $user_config, $module) {
$factory = get_sieve_client_factory($site_config);
try {
$client = $factory->init($user_config, $mailbox);
$client = $factory->init($user_config, $mailbox, in_array(mb_strtolower('nux'), $site_config->get_modules(true), true));
$scripts = $client->listScripts();
if (array_search('blocked_senders', $scripts, true) === false) {
return '';
Expand Down Expand Up @@ -521,6 +521,22 @@ function get_blocked_senders($mailbox, $mailbox_id, $icon_svg, $icon_block_domai
if (!hm_exists('initialize_sieve_client_factory')) {
function initialize_sieve_client_factory($site_config, $user_config, $imapServer) {
$factory = get_sieve_client_factory($site_config);
return $factory->init($user_config, $imapServer);
return $factory->init($user_config, $imapServer, in_array(mb_strtolower('nux'), $site_config->get_modules(true), true));
}
}

if (!hm_exists('get_sieve_host_from_services')) {
require_once APP_PATH.'modules/nux/modules.php';
function get_sieve_host_from_services($imap_host) {
$services = Nux_Quick_Services::get();
foreach ($services as $service) {
if (isset($service['server']) && $service['server'] === $imap_host && isset($service['sieve'])) {
return [
'host' => $service['sieve']['host'],
'port' => $service['sieve']['port'] ?? 4190,
];
}
}
return null;
}
}
10 changes: 8 additions & 2 deletions modules/sievefilters/hm-sieve.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
<?php

class Hm_Sieve_Client_Factory {
public function init($user_config = null, $imap_account = null)
public function init($user_config = null, $imap_account = null, $is_nux_supported = false)
{
if ($imap_account && ! empty($imap_account['sieve_config_host'])) {
list($sieve_host, $sieve_port) = parse_sieve_config_host($imap_account['sieve_config_host']);
// Check if module nux is enabled and if it is, get the sieve host from the services
if($is_nux_supported && $sieve_config = get_sieve_host_from_services($imap_account['server'])) {
$sieve_host = $sieve_config['host'];
$sieve_port = $sieve_config['port'];
} else {
list($sieve_host, $sieve_port) = parse_sieve_config_host($imap_account['sieve_config_host']);
}
$client = new PhpSieveManager\ManageSieve\Client($sieve_host, $sieve_port);
$client->connect($imap_account['user'], $imap_account['pass'], $imap_account['sieve_tls'], "", "PLAIN");
return $client;
Expand Down
22 changes: 11 additions & 11 deletions modules/sievefilters/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function process() {

$factory = get_sieve_client_factory($this->config);
try {
$client = $factory->init($this->user_config, $imap_account);
$client = $factory->init($this->user_config, $imap_account, $this->module_is_supported('nux'));
$script = $client->getScript($this->request->post['sieve_script_name']);
$base64_obj = str_replace("# ", "", preg_split('#\r?\n#', $script, 0)[1]);
$this->out('conditions', json_encode(base64_decode($base64_obj)));
Expand Down Expand Up @@ -66,7 +66,7 @@ public function process() {
if ($sieve_filters_enabled && !empty($server['sieve_config_host'])) {
$factory = get_sieve_client_factory($this->config);
try {
$client = $factory->init($this->user_config, $server);
$client = $factory->init($this->user_config, $server, $this->module_is_supported('nux'));
$sieve_filters_enabled = true;
$this->out('sieve_filters_client', $client);
} catch (Exception $e) {
Expand Down Expand Up @@ -103,7 +103,7 @@ public function process() {
}
$factory = get_sieve_client_factory($this->config);
try {
$client = $factory->init($this->user_config, $imap_account);
$client = $factory->init($this->user_config, $imap_account, $this->module_is_supported('nux'));
$script = $client->getScript($this->request->post['sieve_script_name']);
$client->close();
$this->out('script', $script);
Expand Down Expand Up @@ -136,7 +136,7 @@ public function process() {
}
$factory = get_sieve_client_factory($this->config);
try {
$client = $factory->init($this->user_config, $imap_account);
$client = $factory->init($this->user_config, $imap_account, $this->module_is_supported('nux'));

$scripts = $client->listScripts();
foreach ($scripts as $script) {
Expand Down Expand Up @@ -173,7 +173,7 @@ public function process() {
}
$factory = get_sieve_client_factory($this->config);
try {
$client = $factory->init($this->user_config, $imap_account);
$client = $factory->init($this->user_config, $imap_account, $this->module_is_supported('nux'));

$scripts = $client->listScripts();
foreach ($scripts as $script) {
Expand Down Expand Up @@ -212,7 +212,7 @@ public function process() {
$email_sender = $this->request->post['sender'];
$factory = get_sieve_client_factory($this->config);
try {
$client = $factory->init($this->user_config, $imap_account);
$client = $factory->init($this->user_config, $imap_account, $this->module_is_supported('nux'));
$scripts = $client->listScripts();

$current_script = $client->getScript('blocked_senders');
Expand Down Expand Up @@ -344,7 +344,7 @@ public function process() {

$factory = get_sieve_client_factory($this->config);
try {
$client = $factory->init($this->user_config, $imap_account);
$client = $factory->init($this->user_config, $imap_account, $this->module_is_supported('nux'));
$scripts = $client->listScripts();

if(array_search('blocked_senders', $scripts, true) === false) {
Expand Down Expand Up @@ -482,7 +482,7 @@ public function process() {

$factory = get_sieve_client_factory($this->config);
try {
$client = $factory->init($this->user_config, $imap_account);
$client = $factory->init($this->user_config, $imap_account, $this->module_is_supported('nux'));
$scripts = $client->listScripts();

if(array_search('blocked_senders', $scripts, true) === false) {
Expand Down Expand Up @@ -978,7 +978,7 @@ public function process() {

$factory = get_sieve_client_factory($this->config);
try {
$client = $factory->init($this->user_config, $imap_account);
$client = $factory->init($this->user_config, $imap_account, $this->module_is_supported('nux'));
$scripts = $client->listScripts();
foreach ($scripts as $script) {
if ($script == 'main_script') {
Expand Down Expand Up @@ -1024,7 +1024,7 @@ public function process() {
}
$factory = get_sieve_client_factory($this->config);
try {
$client = $factory->init($this->user_config, $imap_account);
$client = $factory->init($this->user_config, $imap_account, $this->module_is_supported('nux'));
$scripts = $client->listScripts();
foreach ($scripts as $script) {
if ($script == $this->request->post['current_editing_script']) {
Expand Down Expand Up @@ -1359,7 +1359,7 @@ public function process() {
$factory = get_sieve_client_factory($this->config);
$success = false;
try {
$client = $factory->init($this->user_config, $imap_account);
$client = $factory->init($this->user_config, $imap_account, $this->module_is_supported('nux'));
$state = $form['script_state'] ? 'enabled': 'disabled';
$scripts = $client->listScripts();
foreach ($scripts as $key => $script) {
Expand Down

0 comments on commit 9d21016

Please sign in to comment.