Skip to content

Commit

Permalink
Add new "prefix" option to subject matching. (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
r3c authored Sep 27, 2024
1 parent aeb64be commit 2f4f663
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
9 changes: 5 additions & 4 deletions config.inc.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ $config = array();
**
** - e (exact): an address found in headers can be used if it exactly matches
** one of your identities
** - p (prefix): an address found in headers can be used if it exactly matches
** one of your identities with an optional +suffix
** - d (domain): an address found in headers can be used if its domain matches
** one of your identities
** - o (other): any address found in header can be used
**
** By default only exact and domain matching rules are enabled on most headers
** known to contain e-mail addresses, except for the "X-Original-To" header
** where all rules are enabled.
** By default "prefix" matching rule is enabled on headers known to contain
** e-mail addresses.
*/
//$config['custom_from_header_rules'] = 'X-Original-To=de;To=de;Cc=de;Cci=de;From=de';
//$config['custom_from_header_rules'] = 'X-Original-To=p;To=p;Cc=p;Cci=p;From=p';

/*
** Prevent users from configuring plugin via their preference and only use
Expand Down
30 changes: 20 additions & 10 deletions custom_from.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ public function message_compose($params)
$recipients[] = array(
'domain' => preg_replace('/^[^@]*@(.*)$/', '$1', $email),
'email' => $email,
'email_prefix' => preg_replace('/^([^@+]*)\\+[^@]+@(.*)$/', '$1@$2', $email),
'match_always' => strpos($rule, 'o') !== false,
'match_domain' => strpos($rule, 'd') !== false,
'match_exact' => strpos($rule, 'e') !== false,
'name' => $address['name']
'match_prefix' => strpos($rule, 'p') !== false,
'name' => $address['name'],
);
}
}
Expand All @@ -132,7 +134,7 @@ public function message_compose($params)
}

if (!isset($identity_by_email[$email]) || $identity_by_email[$email]['rank'] < $rank) {
$identity_by_email[$email] = array('rank' => $rank);
$identity_by_email[$email] = array('name' => $identity['name'], 'rank' => $rank);
}
}

Expand All @@ -143,10 +145,17 @@ public function message_compose($params)
foreach ($recipients as $recipient) {
$domain = strtolower($recipient['domain']);
$email = strtolower($recipient['email']);
$email_prefix = strtolower($recipient['email_prefix']);

// Relevance score 3: match by e-mail found in identities
// Relevance score 4: match by e-mail found in identities
if ($recipient['match_exact'] && isset($identity_by_email[$email])) {
$current_email = null;
$current_score = 4;
}

// Relevance score 3: match by e-mail found in identities after removing "+suffix"
if ($recipient['match_prefix'] && isset($identity_by_email[$email_prefix])) {
$current_email = format_email_recipient($recipient['email'], $identity_by_email[$email_prefix]['name']);
$current_score = 3;
}

Expand Down Expand Up @@ -248,6 +257,8 @@ public function preferences_list($params)
$compose_subject_value = 'always';
else if (strpos($rule, 'd') !== false)
$compose_subject_value = 'domain';
else if (strpos($rule, 'p') !== false)
$compose_subject_value = 'prefix';
else if (strpos($rule, 'e') !== false)
$compose_subject_value = 'exact';
else
Expand All @@ -256,6 +267,7 @@ public function preferences_list($params)
$compose_subject = new html_select(array('id' => self::PREFERENCE_COMPOSE_SUBJECT, 'name' => self::PREFERENCE_COMPOSE_SUBJECT));
$compose_subject->add(self::get_text($rcmail, 'preference_compose_subject_never'), 'never');
$compose_subject->add(self::get_text($rcmail, 'preference_compose_subject_exact'), 'exact');
$compose_subject->add(self::get_text($rcmail, 'preference_compose_subject_prefix'), 'prefix');
$compose_subject->add(self::get_text($rcmail, 'preference_compose_subject_domain'), 'domain');
$compose_subject->add(self::get_text($rcmail, 'preference_compose_subject_always'), 'always');

Expand Down Expand Up @@ -353,16 +365,14 @@ private static function get_configuration(rcmail $rcmail)
}

// Read "rules" parameter from global configuration & preferences if allowed
$rules_config = $rcmail->config->get('custom_from_header_rules');
$rules_config = $rcmail->config->get('custom_from_header_rules', 'bcc=p;cc=p;from=p;to=p;x-original-to=p');
$rules = array();

if ($rules_config !== null) {
foreach (explode(';', $rules_config) as $pair) {
$fields = explode('=', $pair, 2);
foreach (explode(';', $rules_config) as $pair) {
$fields = explode('=', $pair, 2);

if (count($fields) === 2) {
$rules[strtolower(trim($fields[0]))] = strtolower(trim($fields[1]));
}
if (count($fields) === 2) {
$rules[strtolower(trim($fields[0]))] = strtolower(trim($fields[1]));
}
}

Expand Down
3 changes: 2 additions & 1 deletion localization/de_DE.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ $labels['preference_compose_contains'] = '...und enthält Text (optional)';
$labels['preference_compose_subject'] = 'Aktivieren, wenn ein Empfänger';
$labels['preference_compose_subject_always'] = 'Immer automatisch aktivieren';
$labels['preference_compose_subject_domain'] = 'Verwendet dieselbe Domäne wie eine Identität';
$labels['preference_compose_subject_exact'] = 'Ist eine meiner Identitäten';
$labels['preference_compose_subject_exact'] = 'Ist genau eine meiner Identitäten';
$labels['preference_compose_subject_never'] = 'Niemals automatisch aktivieren';
$labels['preference_compose_subject_prefix'] = 'Ist eine meiner Identitäten mit +Suffix';
$labels['custom_from_off'] = 'Identität auswählen';
$labels['custom_from_off_hint'] = 'Absender aus Liste auswählen';
$labels['custom_from_on'] = 'Frei wählbare Identität';
Expand Down
3 changes: 2 additions & 1 deletion localization/en_US.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ $labels['preference_compose_contains'] = '...and contains text (optional)';
$labels['preference_compose_subject'] = 'Enable when one recipient';
$labels['preference_compose_subject_always'] = 'Always enable automatically';
$labels['preference_compose_subject_domain'] = 'Uses same domain than an identity';
$labels['preference_compose_subject_exact'] = 'Is one of my identities';
$labels['preference_compose_subject_exact'] = 'Is exactly one of my identities';
$labels['preference_compose_subject_never'] = 'Never enable automatically';
$labels['preference_compose_subject_prefix'] = 'Is one of my identities with +suffix';
$labels['custom_from_off'] = 'Select identity';
$labels['custom_from_off_hint'] = 'Select sender from identities list';
$labels['custom_from_on'] = 'Custom identity';
Expand Down
3 changes: 2 additions & 1 deletion localization/fr_FR.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ $labels['preference_compose_contains'] = '...et contient le texte (optionel)';
$labels['preference_compose_subject'] = 'Activer quand l\'un des destinataires';
$labels['preference_compose_subject_always'] = 'Toujours activer automatiquement';
$labels['preference_compose_subject_domain'] = 'Utilise le même domaine qu\'une identité';
$labels['preference_compose_subject_exact'] = 'Est l\'une de mes identités';
$labels['preference_compose_subject_exact'] = 'Est exactement l\'une de mes identités';
$labels['preference_compose_subject_never'] = 'Ne jamais activer automatiquement';
$labels['preference_compose_subject_prefix'] = 'Est l\'une de mes identités avec +suffixe';
$labels['custom_from_off'] = 'Choisir l\'identité';
$labels['custom_from_off_hint'] = 'Choisir l\'expéditeur dans la liste des identités';
$labels['custom_from_on'] = 'Identité libre';
Expand Down

0 comments on commit 2f4f663

Please sign in to comment.