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 Oct 18, 2024
1 parent 18c2c4a commit 6ca536a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 19 deletions.
4 changes: 3 additions & 1 deletion modules/core/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,13 +1008,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 @@ -1036,6 +1036,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 @@ -1113,6 +1114,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 @@ -333,6 +333,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 @@ -2260,6 +2260,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 @@ -2313,6 +2314,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 @@ -2462,6 +2464,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 @@ -1516,18 +1516,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, $show_errors = true) {
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, $show_errors = true) {
$imap_list = array(
'name' => $name,
'server' => $address,
Expand All @@ -1550,7 +1547,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 @@ -1574,7 +1570,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, $sieve_tls);

if (!$client && $show_errors) {
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 @@ -1412,10 +1412,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
6 changes: 4 additions & 2 deletions modules/nux/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@
),
'sieve' => array(
'port' => 4190,
'host' => 'tls://imap.migadu.com'
'host' => 'imap.migadu.com',
'tls' => true
)
));

Expand All @@ -258,7 +259,8 @@
),
'sieve' => array(
'port' => 4190,
'host' => 'tls://mail.postale.io'
'host' => 'mail.postale.io',
'tls' => true
)
));

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 6ca536a

Please sign in to comment.