Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

- **Added: "Create a filter from this email" in the message menus.** Right-click
a message in the list or in the reader and the filter editor opens with the
sender's address and the subject already filled in, without any leading
"Re:" or "Fwd:", so a whole thread matches. Pick where the mail goes, or
which tag it gets, and save.
- **Changed: Hylki's icons look the same on every desktop.** Every icon is
drawn from copies built into the app, including those GTK and libadwaita
draw for their own buttons, arrows and checkmarks; the desktop's icon theme
Expand Down
3 changes: 3 additions & 0 deletions po/hylki.pot
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,9 @@ msgstr ""
msgid "Add Sender to Contacts"
msgstr ""

msgid "Create a filter from this email"
msgstr ""

msgid "Block Remote Content"
msgstr ""

Expand Down
16 changes: 15 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7083,6 +7083,17 @@ impl SimpleComponent for AppModel {
RowAction::AddContact => {
self.show_add_contact_dialog(&m.from_name, &m.from_addr, &sender);
}
RowAction::CreateFilter => {
let Some(email) = self.email_of(m.account_id) else { return };
let rule = config::FilterRule::draft_from_message(&email, &m.from_addr, &m.subject);
self.open_settings_window(&sender, true, false);
if let Some(p) = &self.prefs {
p.emit(PrefInput::ShowPageById("filters".to_string()));
}
if let Some(acc) = &self.accounts_win {
acc.emit(crate::ui::accounts::AccountsInput::AddFilterFrom(rule));
}
}
}
}

Expand Down Expand Up @@ -15127,7 +15138,10 @@ impl AppModel {
acts.push(item(RowAction::Archive, i18n("Archive"), "mail-archive"));
acts.push(item(RowAction::Delete, i18n("Delete"), "user-trash"));
sections.push(acts);
sections.push(vec![item(RowAction::AddContact, i18n("Add Sender to Contacts"), "contact-new")]);
sections.push(vec![
item(RowAction::AddContact, i18n("Add Sender to Contacts"), "contact-new"),
item(RowAction::CreateFilter, i18n("Create a filter from this email"), "filter-folder"),
]);
// Remote content, per message. The banner offers the same thing, but
// it can be switched off (Settings → Privacy) and then there is
// nothing to click — so the choice lives here too, and here it goes
Expand Down
94 changes: 94 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,20 @@ fn is_false(b: &bool) -> bool {
!*b
}

/// The subject with any leading "Re:", "Fwd:" or "Fw:" (any case, repeated)
/// taken off, and the whitespace around it.
fn strip_reply_prefixes(subject: &str) -> &str {
const PREFIXES: [&str; 3] = ["re:", "fwd:", "fw:"];
let mut rest = subject.trim();
while let Some(next) = PREFIXES.iter().find_map(|p| {
let head = rest.get(..p.len())?;
head.eq_ignore_ascii_case(p).then(|| rest[p.len()..].trim_start())
}) {
rest = next;
}
rest.trim()
}

/// One condition of a [`FilterRule`]: what to look at, how, and for what.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(from = "FilterConditionDisk", into = "FilterConditionDisk")]
Expand Down Expand Up @@ -2461,6 +2475,41 @@ fn mailboxes(list: &str) -> Vec<String> {
}

impl FilterRule {
/// The rule the "Create a filter from this email" entry starts the
/// editor with: the sender's address, and the subject without its
/// leading "Re:" / "Fwd:" so a whole thread matches. Both have to match;
/// what the rule does is left for the user to pick. A blank subject
/// leaves the sender as the only condition.
pub fn draft_from_message(account_email: &str, from_addr: &str, subject: &str) -> FilterRule {
let mut conditions = vec![FilterCondition {
field: FilterField::FromAddress,
matcher: FilterMatch::Equals,
value: from_addr.trim().to_string(),
}];
let subject = strip_reply_prefixes(subject);
if !subject.is_empty() {
conditions.push(FilterCondition {
field: FilterField::Subject,
matcher: FilterMatch::Contains,
value: subject.to_string(),
});
}
let mut rule = FilterRule {
name: String::new(),
account_email: account_email.to_string(),
field: FilterField::FromAddress,
matcher: FilterMatch::Equals,
value: String::new(),
more: Vec::new(),
any: false,
dest_path: String::new(),
tag: String::new(),
count_unread: count_unread_default(),
};
rule.set_conditions(conditions);
rule
}

/// Every condition, the first included.
pub fn conditions(&self) -> Vec<FilterCondition> {
let mut all = Vec::with_capacity(1 + self.more.len());
Expand Down Expand Up @@ -4738,6 +4787,51 @@ mod filter_tests {
FilterInput { from_addr, from_name, subject, recipients, ..Default::default() }
}

#[test]
fn draft_filter_matches_sender_and_subject() {
let rule = FilterRule::draft_from_message("me@x.org", "billing@shop.example", "Your invoice");
assert_eq!(rule.account_email, "me@x.org");
assert!(!rule.any, "both conditions have to match");
assert!(rule.dest_path.is_empty() && rule.tag.is_empty(), "the user picks what the rule does");
assert_eq!(
rule.conditions(),
vec![
FilterCondition {
field: FilterField::FromAddress,
matcher: FilterMatch::Equals,
value: "billing@shop.example".into(),
},
FilterCondition {
field: FilterField::Subject,
matcher: FilterMatch::Contains,
value: "Your invoice".into(),
},
]
);
}

#[test]
fn draft_filter_strips_reply_and_forward_prefixes() {
let subject_of = |subject| {
FilterRule::draft_from_message("me@x.org", "a@b.org", subject).conditions()[1].value.clone()
};
assert_eq!(subject_of("Re: Invoice"), "Invoice");
assert_eq!(subject_of("RE: Fwd: re: Invoice"), "Invoice");
assert_eq!(subject_of("Fw: Invoice"), "Invoice");
// Only a leading prefix goes; one inside the subject is the subject's own.
assert_eq!(subject_of("Invoice re: March"), "Invoice re: March");
assert_eq!(subject_of("Reminder: Invoice"), "Reminder: Invoice");
}

#[test]
fn draft_filter_skips_an_empty_subject() {
for subject in ["", " ", "Re: ", "Fwd:"] {
let rule = FilterRule::draft_from_message("me@x.org", "a@b.org", subject);
assert_eq!(rule.conditions().len(), 1, "subject {subject:?} leaves the sender alone");
assert_eq!(rule.field, FilterField::FromAddress);
}
}

#[test]
fn found_keywords_get_names_and_colors() {
// Thunderbird's built-ins keep Thunderbird's names and colors.
Expand Down
22 changes: 16 additions & 6 deletions src/ui/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,9 @@ pub enum AccountsInput {
AddBlacklistText(String),
RemoveBlacklistRow(String),
AddFilter,
/// Open the filter page on a new rule started from this draft, as the
/// "Create a filter from this email" entry does.
AddFilterFrom(crate::config::FilterRule),
/// Run the rules over the mail already in the inboxes (#198).
ApplyFilters,
/// The app says whether that run is still going (#198).
Expand Down Expand Up @@ -3009,7 +3012,10 @@ impl Component for AccountsWindow {
}
}
AccountsInput::AddFilter => {
self.open_filter_page(&widgets.nav, &sender, None);
self.open_filter_page(&widgets.nav, &sender, None, None);
}
AccountsInput::AddFilterFrom(rule) => {
self.open_filter_page(&widgets.nav, &sender, None, Some(rule));
}
AccountsInput::ApplyFilters => {
if !self.filters_applying {
Expand All @@ -3020,7 +3026,7 @@ impl Component for AccountsWindow {
AccountsInput::FiltersApplying(on) => self.set_filters_applying(on),
AccountsInput::EditFilter(i) => {
if let Some(rule) = self.filter_rules.get(i).cloned() {
self.open_filter_page(&widgets.nav, &sender, Some((i, rule)));
self.open_filter_page(&widgets.nav, &sender, Some((i, rule)), None);
}
}
AccountsInput::FilterEdited(i, rule) => {
Expand Down Expand Up @@ -6186,6 +6192,7 @@ impl AccountsWindow {
nav: &adw::NavigationView,
sender: &ComponentSender<Self>,
edit: Option<(usize, crate::config::FilterRule)>,
prefill: Option<crate::config::FilterRule>,
) {
use crate::config::{FilterField, FilterMatch, FilterRule};
let emails: Vec<String> = self.accounts.iter().map(|a| a.email.clone()).collect();
Expand Down Expand Up @@ -6463,9 +6470,12 @@ impl AccountsWindow {
action_group.add(&tag_row);
action_group.add(&count_row);

// Editing: every field starts from the rule as it stands.
// Editing: every field starts from the rule as it stands. A new
// rule started from a message starts from its draft the same way,
// and is added, not saved over another.
let edit_index = edit.as_ref().map(|(i, _)| *i);
if let Some((_, rule)) = &edit {
let source = edit.as_ref().map(|(_, rule)| rule).or(prefill.as_ref());
if let Some(rule) = source {
name_row.set_text(&rule.name);
if let Some(idx) = emails
.iter()
Expand All @@ -6490,8 +6500,8 @@ impl AccountsWindow {
page_box.append(&action_group);
// The condition groups slot in above Add Condition: the rule's own
// when editing, one blank one otherwise.
match &edit {
Some((_, rule)) => {
match source {
Some(rule) => {
for c in rule.conditions() {
add_cond(Some(&c));
}
Expand Down
9 changes: 4 additions & 5 deletions src/ui/message_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2922,11 +2922,10 @@ impl MessageList {
section.push(item(RowAction::Delete, &i18n("Delete"), "user-trash-symbolic"));
section
},
vec![item(
RowAction::AddContact,
&i18n("Add Sender to Contacts"),
"contact-new-symbolic",
)],
vec![
item(RowAction::AddContact, &i18n("Add Sender to Contacts"), "contact-new-symbolic"),
item(RowAction::CreateFilter, &i18n("Create a filter from this email"), "filter-folder-symbolic"),
],
vec![
item(RowAction::ViewSource, &i18n("View Source"), "code-symbolic"),
item(RowAction::SaveAsFile, &i18n("Save as File…"), "document-save-symbolic"),
Expand Down
2 changes: 2 additions & 0 deletions src/ui/message_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub enum RowAction {
/// Save the message whole, as a .eml file (#398).
SaveAsFile,
AddContact,
/// Start a new filter from the message's sender and subject.
CreateFilter,
}

/// A full swipe (#swipe): also `AdwSwipeable`'s reported `distance`, the px
Expand Down