Skip to content

Commit

Permalink
thunderbird: add message filters option
Browse files Browse the repository at this point in the history
Add option to declare account-specific message filters.
  • Loading branch information
347Online committed Mar 1, 2025
1 parent f0b5e7e commit b3325ae
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
88 changes: 84 additions & 4 deletions modules/programs/thunderbird.nix
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,27 @@ let
'') prefs)}
${extraPrefs}
'';

mkFilterToIniString = f:
''
name="${f.name}"
enabled="${if f.enabled then "yes" else "no"}"
type="${f.type}"
condition="${f.condition}"
action="${f.action}"
''
+ optionalString (f.actionValue != null) ''actionValue="${f.actionValue}"'';

mkFilterListToIni = filters:
''
version="9"
logging="no"
'' + concatStrings (map (f: mkFilterToIniString f) filters);

getEmailAccountsForProfile = profileName: accounts:
(filter (a:
a.thunderbird.profiles == [ ]
|| any (p: p == profileName) a.thunderbird.profiles) accounts);
in {
meta.maintainers = with hm.maintainers; [ d-dervishi jkarlson ];

Expand Down Expand Up @@ -408,6 +429,58 @@ in {
argument is an automatically generated identifier.
'';
};

messageFilters = mkOption {
type = with types;
listOf (submodule {
options = {
name = mkOption {
type = str;
description = "Name for the filter.";
};
enabled = mkOption {
type = bool;
default = true;
description = "Whether this filter is currently active.";
};
type = mkOption {
type = str;
description = "Type for this filter.";
};
action = mkOption {
type = str;
description = "Action to perform on matched messages.";
};
actionValue = mkOption {
type = nullOr str;
default = null;
description =
"Argument passed to the filter action, e.g. a folder path.";
};
condition = mkOption {
type = str;
description = "Condition to match messages against.";
};
};
});
default = [ ];
defaultText = literalExpression "[ ]";
example = literalExpression ''
[
{
name = "Mark as Read on Archive";
enabled = true;
type = "128";
action = "Mark read";
condition = "ALL";
}
]
'';
description = ''
List of message filters to add to this Thunderbird account
configuration.
'';
};
};
});
};
Expand Down Expand Up @@ -463,9 +536,7 @@ in {
mkIf (profile.userContent != "") { text = profile.userContent; };

"${thunderbirdProfilesPath}/${name}/user.js" = let
emailAccounts = filter (a:
a.thunderbird.profiles == [ ]
|| any (p: p == name) a.thunderbird.profiles) enabledAccountsWithId;
emailAccounts = getEmailAccountsForProfile name enabledAccountsWithId;

smtp = filter (a: a.smtp != null) emailAccounts;

Expand Down Expand Up @@ -514,6 +585,15 @@ in {
recursive = true;
force = true;
};
}));
}) ++ (mapAttrsToList (name: profile:
let
emailAccountsWithFilters =
(filter (a: a.thunderbird.messageFilters != [ ])
(getEmailAccountsForProfile name enabledAccountsWithId));
in (builtins.listToAttrs (map (a: {
name =
"${thunderbirdConfigPath}/${name}/ImapMail/${a.id}/msgFilterRules.dat";
value = { text = mkFilterListToIni a.thunderbird.messageFilters; };
}) emailAccountsWithFilters))) cfg.profiles));
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version="9"
logging="no"
name="Mark as Read on Archive"
enabled="yes"
type="128"
action="Mark read"
condition="ALL"
15 changes: 15 additions & 0 deletions tests/modules/programs/thunderbird/thunderbird.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
thunderbird = {
enable = true;
profiles = [ "first" ];
messageFilters = [{
name = "Mark as Read on Archive";
enabled = true;
type = "128";
action = "Mark read";
condition = "ALL";
}];
};

aliases = [ "home-manager@example.com" ];
Expand Down Expand Up @@ -99,5 +106,13 @@
assertFileExists home-files/${profilesDir}/first/chrome/userContent.css
assertFileContent home-files/${profilesDir}/first/chrome/userContent.css \
<(echo "* { color: red !important; }")
assertFileExists home-files/.thunderbird/first/ImapMail/${
builtins.hashString "sha256" "hm@example.com"
}/msgFilterRules.dat
assertFileContent home-files/.thunderbird/first/ImapMail/${
builtins.hashString "sha256" "hm@example.com"
}/msgFilterRules.dat \
${./thunderbird-expected-msgFilterRules.dat}
'';
}

0 comments on commit b3325ae

Please sign in to comment.