Skip to content

Commit

Permalink
Make sieve tls mode dynamic to support all servers
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Aug 27, 2024
1 parent af3bc1e commit 7ac9050
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
4 changes: 3 additions & 1 deletion modules/core/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1006,13 +1006,13 @@ public function process() {
'srv_setup_stepper_profile_signature',
'srv_setup_stepper_profile_reply_to',
'srv_setup_stepper_imap_sieve_host',
'srv_setup_stepper_imap_sieve_mode_tls',
'srv_setup_stepper_only_jmap',
'srv_setup_stepper_imap_hide_from_c_page',
'srv_setup_stepper_jmap_address',
'srv_setup_stepper_imap_server_id',
'srv_setup_stepper_smtp_server_id',
));

if ($success) {
// Destructure form array into variables
[
Expand All @@ -1034,6 +1034,7 @@ public function process() {
'srv_setup_stepper_profile_signature' => $profileSignature,
'srv_setup_stepper_profile_reply_to' => $profileReplyTo,
'srv_setup_stepper_imap_sieve_host' => $imapSieveHost,
'srv_setup_stepper_imap_sieve_mode_tls' => $imapSieveTls,
'srv_setup_stepper_only_jmap' => $onlyJmap,
'srv_setup_stepper_imap_hide_from_c_page' => $hideFromCombinedView,
'srv_setup_stepper_jmap_address' => $jmapAddress,
Expand Down Expand Up @@ -1111,6 +1112,7 @@ public function process() {
$this,
$hideFromCombinedView,
$imapServerId,
$imapSieveTls
);

if(!isset($this->imap_server_id)) {
Expand Down
1 change: 1 addition & 0 deletions modules/core/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@
'srv_setup_stepper_profile_signature' => FILTER_DEFAULT,
'srv_setup_stepper_profile_reply_to' => FILTER_DEFAULT,
'srv_setup_stepper_imap_sieve_host' => FILTER_DEFAULT,
'srv_setup_stepper_imap_sieve_mode_tls' => FILTER_VALIDATE_BOOLEAN,
'srv_setup_stepper_only_jmap' => FILTER_VALIDATE_BOOLEAN,
'srv_setup_stepper_jmap_hide_from_c_page' => FILTER_VALIDATE_BOOLEAN,
'srv_setup_stepper_jmap_address' => FILTER_DEFAULT,
Expand Down
3 changes: 3 additions & 0 deletions modules/core/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -2290,6 +2290,7 @@ function submitSmtpImapServer() {
{ name: 'srv_setup_stepper_imap_port', value: $('#srv_setup_stepper_imap_port').val() },
{ name: 'srv_setup_stepper_imap_tls', value: $('input[name="srv_setup_stepper_imap_tls"]:checked').val() },
{ name: 'srv_setup_stepper_enable_sieve', value: $('#srv_setup_stepper_enable_sieve').prop('checked') },
{ name: 'srv_setup_stepper_imap_sieve_mode_tls', value: $('#srv_setup_stepper_imap_sieve_mode_tls').prop('checked') },
{ name: 'srv_setup_stepper_create_profile', value: $('#srv_setup_stepper_create_profile').prop('checked') },
{ name: 'srv_setup_stepper_profile_is_default', value: $('#srv_setup_stepper_profile_is_default').prop('checked') },
{ name: 'srv_setup_stepper_profile_signature', value: $('#srv_setup_stepper_profile_signature').val() },
Expand Down Expand Up @@ -2343,6 +2344,7 @@ function resetQuickSetupForm() {
$("#srv_setup_stepper_is_sender").prop('checked', true);
$("#srv_setup_stepper_is_receiver").prop('checked', true);
$("#srv_setup_stepper_enable_sieve").prop('checked', false);
$("#srv_setup_stepper_imap_sieve_mode_tls").prop('checked', false);
$("#srv_setup_stepper_only_jmap").prop('checked', false);
$('#step_config-imap_bloc').show();
$('#step_config-smtp_bloc').show();
Expand Down Expand Up @@ -2492,6 +2494,7 @@ function display_config_step(stepNumber) {
if($('#srv_setup_stepper_enable_sieve').is(':checked')) {
requiredFields.push(
{key: 'srv_setup_stepper_imap_sieve_host', value: $('#srv_setup_stepper_imap_sieve_host').val()},
{key: 'srv_setup_stepper_imap_sieve_mode_tls', value: $('#srv_setup_stepper_imap_sieve_mode_tls').val()},
)
}

Expand Down
16 changes: 6 additions & 10 deletions modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1495,18 +1495,15 @@ function forward_dropdown($output,$reply_args) {
if (!hm_exists('parse_sieve_config_host')) {
function parse_sieve_config_host($host) {
$url = parse_url($host);
$host = $url['host'] ?? $url['path'];
$port = $url['port'] ?? '4190';
$scheme = $url['scheme'] ?? '';
$tls = $scheme === 'tls';
if (!empty($scheme)) {
$host = $scheme . '://' . $host;
if(!isset($url['host'])) {
$host = $url['path'];
}
return [$host, $port, $tls];
$port = $url['port'] ?? '4190';
return [$host, $port];
}}

if (!hm_exists('connect_to_imap_server')) {
function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $imap_sieve_host, $enableSieve, $type, $context, $hidden = false, $server_id = false) {
function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $imap_sieve_host, $enableSieve, $type, $context, $hidden = false, $server_id = false, $sieve_tls = false) {
$imap_list = array(
'name' => $name,
'server' => $address,
Expand All @@ -1529,7 +1526,6 @@ function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $ima
if ($enableSieve && $imap_sieve_host) {
$imap_list['sieve_config_host'] = $imap_sieve_host;
}

if ($server_id) {
if (Hm_IMAP_List::edit($server_id, $imap_list)) {
$imap_server_id = $server_id;
Expand All @@ -1553,7 +1549,7 @@ function connect_to_imap_server($address, $name, $port, $user, $pass, $tls, $ima

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

if (!$client) {
Hm_Msgs::add("ERRFailed to authenticate to the Sieve host");
Expand Down
18 changes: 14 additions & 4 deletions modules/imap/output_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1384,10 +1384,20 @@ protected function output() {
<input class="form-check-input" type="checkbox" id="srv_setup_stepper_enable_sieve" onchange="handleSieveStatusChange(this)">
<label class="form-check-label" for="srv_setup_stepper_enable_sieve">'.$this->trans('Enable Sieve').'</label>
</div>
<div class="form-floating hide" id="srv_setup_stepper_imap_sieve_host_bloc">
<input required type="text" id="srv_setup_stepper_imap_sieve_host" name="srv_setup_stepper_imap_sieve_host" class="txt_fld form-control" value="" placeholder="'.$this->trans('Sieve Host').'">
<label class="" for="srv_setup_stepper_imap_sieve_host">'.$this->trans('Sieve Host').'</label>
<span id="srv_setup_stepper_imap_sieve_host-error" class="invalid-feedback"></span>
<div id="srv_setup_stepper_imap_sieve_host_bloc" class="hide">
<div class="form-floating">
<input required type="text" id="srv_setup_stepper_imap_sieve_host" name="srv_setup_stepper_imap_sieve_host" class="txt_fld form-control" value="" placeholder="'.$this->trans('Sieve Host').'">
<label class="" for="srv_setup_stepper_imap_sieve_host">'.$this->trans('Sieve Host').'</label>
<span id="srv_setup_stepper_imap_sieve_host-error" class="invalid-feedback"></span>
</div>
<div class="form-floating">
<div class="form-check" id="srv_setup_stepper_imap_sieve_mode_tls_bloc">
<input class="form-check-input" type="checkbox" role="switch" value="1" id="srv_setup_stepper_imap_sieve_mode_tls" name="srv_setup_stepper_imap_sieve_mode_tls">
<label class="form-check-label" for="srv_setup_stepper_imap_sieve_mode_tls">
'.$this->trans('Sieve TLS Mode').'
</label>
</div>
</div>
</div>';
}
$res .= '</div>';
Expand Down
4 changes: 2 additions & 2 deletions modules/sievefilters/hm-sieve.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<?php

class Hm_Sieve_Client_Factory {
public function init($user_config = null, $imap_account = null)
public function init($user_config = null, $imap_account = null, $sieve_tls=false)
{
if ($imap_account && ! empty($imap_account['sieve_config_host'])) {
list($sieve_host, $sieve_port, $sieve_tls) = parse_sieve_config_host($imap_account['sieve_config_host']);
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'], $sieve_tls, "", "PLAIN");
return $client;
Expand Down

0 comments on commit 7ac9050

Please sign in to comment.