diff --git a/modules/programs/thunderbird.nix b/modules/programs/thunderbird.nix index c15a946ec..7de219ceb 100644 --- a/modules/programs/thunderbird.nix +++ b/modules/programs/thunderbird.nix @@ -124,6 +124,28 @@ let '') prefs)} ${extraPrefs} ''; + + filterIniHeader = '' + version="9" + logging="no" + ''; + + mkFilterToIniStr = f: '' + name="${f.name}" + enabled="${if f.enabled then "yes" else "no"}" + type="${f.type}" + action="${f.action}" + actionValue="${f.actionValue}" + condition="${f.condition}" + ''; + + mkFilterListToIni = filters: + filterIniHeader + concatStrings (map (f: mkFilterToIniStr f) filters); + + getAccountsForProfile = profileName: accounts: + (filter (a: + a.thunderbird.profiles == [ ] + || any (p: p == profileName) a.thunderbird.profiles) accounts); in { meta.maintainers = with hm.maintainers; [ d-dervishi jkarlson ]; @@ -295,6 +317,28 @@ in { argument is an automatically generated identifier. ''; }; + + msgFilters = mkOption { + type = (types.listOf (types.attrs)); + default = [ ]; + defaultText = literalExpression "[ ]"; + example = literalExpression '' + [ + { + name = "Junk Status is: Junk"; + enabled = true; + type = "48"; + action = "Move to folder"; + actionValue = "imap://joe%40example.com@mail.example.com/Junk"; + condition = "AND (junk status,is,2)"; + } + ] + ''; + description = '' + A list of thunderbird message filters which will be added to + the account. + ''; + }; }; }); }; @@ -353,10 +397,7 @@ in { mkIf (profile.userContent != "") { text = profile.userContent; }; "${thunderbirdProfilesPath}/${name}/user.js" = let - accounts = filter (a: - a.thunderbird.profiles == [ ] - || any (p: p == name) a.thunderbird.profiles) enabledAccountsWithId; - + accounts = getAccountsForProfile name enabledAccountsWithId; smtp = filter (a: a.smtp != null) accounts; in { text = mkUserJs (builtins.foldl' (a: b: a // b) { } ([ @@ -378,6 +419,14 @@ in { ] ++ (map (a: toThunderbirdAccount a profile) accounts))) profile.extraConfig; }; - })); + }) ++ (mapAttrsToList (name: profile: + let + accountsWithFilters = (filter (a: a.thunderbird.msgFilters != [ ]) + (getAccountsForProfile name enabledAccountsWithId)); + in (builtins.listToAttrs (map (a: { + name = + "${thunderbirdConfigPath}/${name}/ImapMail/${a.id}/msgFilterRules.dat"; + value = { text = mkFilterListToIni a.thunderbird.msgFilters; }; + }) accountsWithFilters))) cfg.profiles)); }; } diff --git a/tests/modules/programs/thunderbird/thunderbird-expected-msgFilterRules.dat b/tests/modules/programs/thunderbird/thunderbird-expected-msgFilterRules.dat new file mode 100644 index 000000000..f6d107f82 --- /dev/null +++ b/tests/modules/programs/thunderbird/thunderbird-expected-msgFilterRules.dat @@ -0,0 +1,8 @@ +version="9" +logging="no" +name="test filter" +enabled="yes" +type="32" +action="ACTION_NAME" +actionValue="//imap:" +condition="AND " diff --git a/tests/modules/programs/thunderbird/thunderbird.nix b/tests/modules/programs/thunderbird/thunderbird.nix index f65f80bdf..10f2a118c 100644 --- a/tests/modules/programs/thunderbird/thunderbird.nix +++ b/tests/modules/programs/thunderbird/thunderbird.nix @@ -6,6 +6,14 @@ thunderbird = { enable = true; profiles = [ "first" ]; + msgFilters = [{ + name = "test filter"; + enabled = true; + type = "32"; + action = "ACTION_NAME"; + actionValue = "//imap:"; + condition = "AND "; + }]; }; aliases = [ "home-manager@example.com" ]; @@ -84,5 +92,13 @@ assertFileExists home-files/.thunderbird/first/chrome/userContent.css assertFileContent home-files/.thunderbird/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} ''; }