Skip to content

Commit 7a54a6e

Browse files
committed
docs(README): update architecture section with detailed component overview and usage examples
1 parent a024f8a commit 7a54a6e

File tree

3 files changed

+20
-73
lines changed

3 files changed

+20
-73
lines changed

modules/sievefilters/functions.php

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -104,49 +104,22 @@ function get_classic_filter_modal_content()
104104
if (!hm_exists('get_mailbox_filters')) {
105105
function get_mailbox_filters($mailbox, $site_config, $user_config)
106106
{
107+
$factory = get_sieve_client_factory($site_config);
107108
try {
108-
// Initialize SieveService with proper cache and server config
109-
$servers = [];
110-
foreach ($user_config->get('imap_servers') as $id => $server) {
111-
if (!empty($server['sieve_config_host'])) {
112-
$servers[$id] = $server;
113-
}
114-
}
115-
116-
// Get cache instance safely
117-
$cacheInstance = null;
118-
if ($site_config && method_exists($site_config, 'get')) {
119-
$cacheInstance = $site_config->get('hm_cache_instance');
120-
}
121-
122-
// Only initialize if we have a valid cache instance
123-
if ($cacheInstance && is_object($cacheInstance)) {
124-
SieveService::init($cacheInstance, $servers);
125-
126-
// Get scripts using SieveService
127-
$scripts = SieveService::listScripts($mailbox['id']);
128-
} else {
129-
// Fallback to direct client connection if no cache available
130-
$factory = get_sieve_client_factory($site_config);
131-
$client = $factory->init($user_config, $mailbox, in_array(mb_strtolower('nux'), $site_config->get_modules(true), true));
132-
$scripts = $client->listScripts();
133-
}
134-
135-
// Filter only cypht scripts
136-
$cypht_scripts = [];
137-
foreach ($scripts as $script) {
109+
$client = $factory->init($user_config, $mailbox, in_array(mb_strtolower('nux'), $site_config->get_modules(true), true));
110+
$scripts = [];
111+
foreach ($client->listScripts() as $script) {
138112
if (mb_strstr($script, 'cypht')) {
139-
$cypht_scripts[] = $script;
113+
$scripts[] = $script;
140114
}
141115
}
142-
143116
} catch (Exception $e) {
144117
Hm_Msgs::add("Sieve: {$e->getMessage()}", "danger");
145118
return ['count' => 0, 'list' => ''];
146119
}
147120

148121
$scripts_sorted = [];
149-
foreach ($cypht_scripts as $script_name) {
122+
foreach ($scripts as $script_name) {
150123
$exp_name = explode('-', $script_name);
151124
if (end($exp_name) == 'cypht') {
152125
$base_class = 'script';
@@ -198,7 +171,7 @@ function get_mailbox_filters($mailbox, $site_config, $user_config)
198171
</tr>
199172
';
200173
}
201-
return ['count' => count($cypht_scripts), 'list' => $script_list];
174+
return ['count' => count($scripts), 'list' => $script_list];
202175
}
203176
}
204177

@@ -642,22 +615,18 @@ function initialize_sieve_client_factory($site_config, $user_config, $imapServer
642615
if (!hm_exists('get_all_scripts')) {
643616
function get_all_scripts($imapServer, $load_current = true, $return_only = null) {
644617
try {
645-
$client = SieveService::getConnection($imapServer);
646-
if(!is_null($client)){
647-
$scripts = $client->listScripts();
648-
if (!is_array($scripts) || array_search('blocked_senders', $scripts, true) === false) {
649-
return '';
650-
}
651-
$current_script = '';
652-
if($load_current) {
653-
$current_script = SieveService::getScript($imapServer, 'blocked_senders');
654-
}
655-
if ($return_only === 'scripts') return $scripts;
656-
if ($return_only === 'current_script') return $current_script;
657-
658-
return [$scripts, $current_script];
618+
$scripts = SieveService::listScripts($imapServer);
619+
if (!is_array($scripts) || array_search('blocked_senders', $scripts, true) === false) {
620+
return '';
659621
}
660-
return null;
622+
$current_script = '';
623+
if($load_current) {
624+
$current_script = SieveService::getScript($imapServer, 'blocked_senders');
625+
}
626+
if ($return_only === 'scripts') return $scripts;
627+
if ($return_only === 'current_script') return $current_script;
628+
629+
return [$scripts, $current_script];
661630
} catch (Exception $e) {
662631
Hm_Msgs::add("Sieve: {$e->getMessage()}", "danger");
663632
return null;

modules/sievefilters/hm-sieve-script-cache.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ class SieveScriptCache
1717
*/
1818
private static $cache = null;
1919

20-
/**
21-
* Debug: Track cache access count per request
22-
*/
23-
private static $accessCount = [];
24-
2520
private function __construct() {}
2621

2722
/**
@@ -58,12 +53,6 @@ public static function getCachedScript($key, string $scriptName)
5853

5954
$cacheKey = "sieve_script_{$key}_{$scriptName}";
6055

61-
// Track access count
62-
if (!isset(self::$accessCount[$cacheKey])) {
63-
self::$accessCount[$cacheKey] = 0;
64-
}
65-
self::$accessCount[$cacheKey]++;
66-
6756
$cached = self::$cache->get($cacheKey, false, true);
6857

6958
if ($cached && isset($cached['time']) && (time() - $cached['time']) < self::$scriptCacheTTL) {
@@ -186,6 +175,7 @@ public static function clearScriptCache($key, string $scriptName)
186175
*/
187176
public static function clearAllCache($key)
188177
{
178+
//TODO: Implement a method to clear all cache entries for a given key
189179
if (!self::$cache) {
190180
return false;
191181
}
@@ -327,17 +317,14 @@ public static function init($cacheInstance, array $serverConfigs)
327317
*/
328318
public static function getScript($key, string $scriptName)
329319
{
330-
// Try cache first
331320
$cachedScript = SieveScriptCache::getCachedScript($key, $scriptName);
332321
if ($cachedScript !== false) {
333322
return $cachedScript;
334323
}
335324

336-
// Cache miss — fetch from server
337325
$client = SieveConnectionManager::getConnection($key);
338326
$script = $client->getScript($scriptName);
339327

340-
// Cache the result
341328
SieveScriptCache::cacheScript($key, $scriptName, $script);
342329

343330
return $script;
@@ -348,17 +335,14 @@ public static function getScript($key, string $scriptName)
348335
*/
349336
public static function listScripts($key)
350337
{
351-
// Try cache first
352338
$cached = SieveScriptCache::getCachedScriptsList($key);
353339
if ($cached !== false) {
354340
return $cached;
355341
}
356342

357-
// Get from server and cache
358343
$client = SieveConnectionManager::getConnection($key);
359344
$scripts = $client->listScripts();
360345

361-
// Cache the list
362346
if ($scripts !== false) {
363347
SieveScriptCache::cacheScriptsList($key, $scripts);
364348
}
@@ -374,10 +358,8 @@ public static function putScript($key, string $scriptName, string $scriptContent
374358
$client = SieveConnectionManager::getConnection($key);
375359
$result = $client->putScript($scriptName, $scriptContent);
376360

377-
// Clear cache for this script since it's been updated
378361
SieveScriptCache::clearScriptCache($key, $scriptName);
379362

380-
// Invalidate scripts list cache since a new script might have been added
381363
SieveScriptCache::invalidateScriptsList($key);
382364

383365
return $result;
@@ -400,10 +382,8 @@ public static function removeScripts($key, string $scriptName)
400382
$client = SieveConnectionManager::getConnection($key);
401383
$result = $client->removeScripts($scriptName);
402384

403-
// Clear cache for this script since it's been removed
404385
SieveScriptCache::clearScriptCache($key, $scriptName);
405386

406-
// Invalidate scripts list cache since a script has been removed
407387
SieveScriptCache::invalidateScriptsList($key);
408388

409389
return $result;

modules/sievefilters/modules.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,7 @@ public function process()
16111611
'id' => $key
16121612
];
16131613
}
1614-
// set the sieve connection pool with the sieve accounts configs
1614+
// set sieve accounts in cache for SieveService
16151615
if (!empty($sieve_accounts_configs)) {
16161616
SieveService::init($this->cache, $sieve_accounts_configs);
16171617
}
@@ -1636,7 +1636,6 @@ public function process()
16361636

16371637
$mailbox = Hm_IMAP_List::get_connected_mailbox($form['imap_server_id'], $this->cache);
16381638
if ($mailbox && $mailbox->authed() && $mailbox->is_imap()) {
1639-
// list($scripts, $current_script, $client) = get_all_scripts($this->config, $this->user_config, $mailbox, true);
16401639
list($scripts, $current_script) = get_all_scripts($form['imap_server_id'], true);
16411640

16421641
// Initialize SieveService with cache and server configurations
@@ -1689,7 +1688,6 @@ public function process()
16891688
if ($mailbox && $mailbox->authed() && $mailbox->is_imap()) {
16901689
$del_folder = prep_folder_name($mailbox->get_connection(), $form['folder'], true);
16911690
$client = SieveService::getConnection($mailbox['id']);
1692-
// list($scripts, $current_script, $client) = get_all_scripts($this->config, $this->user_config, $mailbox);
16931691
list($scripts) = get_all_scripts($form['imap_server_id'], false);
16941692
if (is_mailbox_linked_with_filters($del_folder, $form['imap_server_id'], $this, $scripts, $client)) {
16951693
$this->out('sieve_can_delete_folder', false);

0 commit comments

Comments
 (0)