Skip to content

Commit

Permalink
Read only "from" header when composing a draft. (#53)
Browse files Browse the repository at this point in the history
This will avoid incorrectly matching recipient address when composing a draft
while sender address was already saved.
  • Loading branch information
r3c authored Nov 1, 2024
1 parent 37274c9 commit e600398
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
3 changes: 2 additions & 1 deletion custom_from.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ public function message_compose($params)

// Browse headers where addresses will be fetched from
$recipients = array();
$rules = isset($params['param']['draft_uid']) ? array_intersect_key($this->rules, array('from' => null)) : $this->rules;

foreach ($this->rules as $header => $rule) {
foreach ($rules as $header => $rule) {
$header_value = $headers->get($header);
$addresses = $header_value !== null ? rcube_mime::decode_address_list($header_value, null, false) : array();

Expand Down
39 changes: 37 additions & 2 deletions tests/CustomFromTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,18 @@ public function test_storage_init_should_fetch_headers($config_values, $user_pre
public static function message_compose_should_set_state_provider(): array
{
return array(
// Missing message shouldn't match anything
array(
'unknown',
array('to' => '[email protected]'),
array(self::RULES => 'to=o'),
array(),
null,
null
),
// Subject rule "exact" should match address exactly
array(
'uid',
array('to' => '[email protected]'),
array(self::RULES => 'to=e'),
array(),
Expand All @@ -109,6 +119,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Subject rule "exact" shouldn't match suffix
array(
'uid',
array('to' => '[email protected]'),
array(self::RULES => 'to=e'),
array(),
Expand All @@ -117,6 +128,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Subject rule "prefix" should match address exactly
array(
'uid',
array('to' => '[email protected]'),
array(),
array(),
Expand All @@ -125,6 +137,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Subject rule "prefix" should match address by prefix
array(
'uid',
array('to' => '[email protected]'),
array(),
array(),
Expand All @@ -133,6 +146,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Subject rule "prefix" should not match different user
array(
'uid',
array('to' => '[email protected]'),
array(),
array(),
Expand All @@ -141,6 +155,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Subject rule "domain" on custom header should match address by domain
array(
'uid',
array('to' => '[email protected]', 'x-custom' => '[email protected]'),
array(self::RULES => 'x-custom=d'),
array(),
Expand All @@ -149,6 +164,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Subject rule "domain" should not match different domain
array(
'uid',
array('to' => '[email protected]'),
array(self::RULES => 'to=d'),
array(),
Expand All @@ -157,6 +173,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Subject rule "other" should match anything
array(
'uid',
array('to' => '[email protected]'),
array(self::RULES => 'to=o'),
array(),
Expand All @@ -165,6 +182,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Subject rule is overridden by user prefrences
array(
'uid',
array('to' => '[email protected]'),
array(self::RULES => 'to=e'),
array(self::SUBJECT => 'domain'),
Expand All @@ -173,6 +191,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Contains constraint in configuration options matches address
array(
'uid',
array('to' => '[email protected]'),
array(self::CONTAINS => 'match'),
array(self::SUBJECT => 'domain'),
Expand All @@ -181,6 +200,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Contains constraint in configuration options rejects no match
array(
'uid',
array('to' => '[email protected]'),
array(self::CONTAINS => 'match'),
array(self::SUBJECT => 'domain'),
Expand All @@ -189,6 +209,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Contains constraint in user preferences rejects no match
array(
'uid',
array('to' => '[email protected]'),
array(self::CONTAINS => 'other'),
array(self::CONTAINS => 'match', self::SUBJECT => 'always'),
Expand All @@ -197,6 +218,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Identity behavior "default" returns matched identity with no sender on exact match
array(
'uid',
array('to' => '[email protected]'),
array(),
array(self::SUBJECT => 'always'),
Expand All @@ -205,6 +227,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Identity behavior "default" returns matched identity and sender with identity name on domain match
array(
'uid',
array('to' => '[email protected]'),
array(),
array(self::SUBJECT => 'domain'),
Expand All @@ -213,6 +236,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Identity behavior "loose" returns matched identity and sender with identity name on prefix match
array(
'uid',
array('to' => 'SomeName <[email protected]>'),
array(),
array(self::IDENTITY => 'loose', self::SUBJECT => 'prefix'),
Expand All @@ -221,6 +245,7 @@ public static function message_compose_should_set_state_provider(): array
),
// Identity behavior "exact" returns matched identity with no sender on exact match
array(
'uid',
array('to' => '[email protected]'),
array(self::IDENTITY => 'exact'),
array(self::SUBJECT => 'always'),
Expand All @@ -229,17 +254,27 @@ public static function message_compose_should_set_state_provider(): array
),
// Identity behavior "exact" returns no identity and sender with recipient name on prefix match
array(
'uid',
array('to' => 'SomeName <[email protected]>'),
array(),
array(self::IDENTITY => 'exact', self::SUBJECT => 'prefix'),
null,
'SomeName <[email protected]>'
),
// Header "from" should be selected when composing a draft
array(
'draft_uid',
array('from' => '[email protected]', 'to' => '[email protected]'),
array(self::RULES => 'from=d;to=e'),
array(),
'2',
'Bob <[email protected]>'
)
);
}

#[DataProvider('message_compose_should_set_state_provider')]
public function test_message_compose_should_set_state($message, $config_values, $user_prefs, $expected_identity, $expected_sender): void
public function test_message_compose_should_set_state($uid_key, $message, $config_values, $user_prefs, $expected_identity, $expected_sender): void
{
$identity1 = array('identity_id' => '1', 'email' => '[email protected]', 'name' => 'Alice', 'standard' => '0');
$identity2 = array('identity_id' => '2', 'email' => '[email protected]', 'name' => 'Bob', 'standard' => '1');
Expand All @@ -253,7 +288,7 @@ public function test_message_compose_should_set_state($message, $config_values,
$rcmail->mock_user(array($identity1, $identity2, $identity3), $user_prefs);

$plugin = self::create_plugin();
$plugin->message_compose(array('id' => $compose_id, 'param' => array('uid' => $message_id)));
$plugin->message_compose(array('id' => $compose_id, 'param' => array($uid_key => $message_id)));

$state = self::get_state($plugin, $compose_id);

Expand Down

0 comments on commit e600398

Please sign in to comment.