--- | This module contains the 'DnsblSite' data type representing one
--- blacklist with its associated return codes and weight. For example,
--- in Postfix's main.cf you might have,
+-- | This module contains the 'Site' data type representing one
+-- blacklist with its associated return codes and weight. For
+-- example, in Postfix's main.cf you might have,
--
-- postscreen_dnsbl_sites = bl.mailspike.net=127.0.0.[2;10;11]*2, ...
--
-- return code pattern is \"127.0.0.[2;10;11]\", and the weight is
-- \"2".
--
-module Network.DNS.RBL.DnsblSite (
- dnsbl_site_tests,
- dnsbl_sites )
+module Network.DNS.RBL.Site (
+ site_tests,
+ sites )
where
import Data.List ( intercalate )
pretty_show (Weight w) = show w
--- | Parse the weight multiplier at the end of a dnsbl_site.
+-- | Parse the weight multiplier at the end of a site.
--
-- ==== _Examples_
--
-- (DNS) name, a pattern of addresses to use for a \"hit\", and a
-- weight multiplier.
--
-data DnsblSite = DnsblSite UserDomain (Maybe IPv4Pattern) Weight
+data Site = Site UserDomain (Maybe IPv4Pattern) Weight
-- | Pretty print DNSBL sites. This is straightforward except for the
-- to just choose one of these behaviors rather than pass around a
-- @Maybe Weight@. We always print the multiplier, even when it's @1@.
--
-instance Pretty DnsblSite where
- pretty_show (DnsblSite d p w) =
+instance Pretty Site where
+ pretty_show (Site d p w) =
(pretty_show d) ++ pattern_string ++ "*" ++ (pretty_show w)
where
pattern_string = case p of
Just pat -> "=" ++ pretty_show pat
--- | Parse a single 'DnsblSite'.
+-- | Parse a single 'Site'.
--
-- ==== _Examples_
--
-- >>> import Text.Parsec ( parse )
--
-- >>> let spamhaus = "zen.spamhaus.org*3"
--- >>> pretty_print $ parse dnsbl_site "" spamhaus
+-- >>> pretty_print $ parse site "" spamhaus
-- zen.spamhaus.org*3
--
-- >>> let mailspike = "bl.mailspike.net=127.0.0.[2;10;11]*2"
--- >>> pretty_print $ parse dnsbl_site "" mailspike
+-- >>> pretty_print $ parse site "" mailspike
-- bl.mailspike.net=127.0.0.[2;10;11]*2
--
-- If the weight is left unspecified, it defaults to \"1\" which is
-- then printed:
--
-- >>> let hostkarma = "hostkarma.junkemailfilter.com=127.0.0.2"
--- >>> pretty_print $ parse dnsbl_site "" hostkarma
+-- >>> pretty_print $ parse site "" hostkarma
-- hostkarma.junkemailfilter.com=127.0.0.2*1
--
-- >>> let ubl = "ubl.unsubscore.com"
--- >>> pretty_print $ parse dnsbl_site "" ubl
+-- >>> pretty_print $ parse site "" ubl
-- ubl.unsubscore.com*1
--
-dnsbl_site :: Parser DnsblSite
-dnsbl_site = do
+site :: Parser Site
+site = do
d <- user_domain
return_codes <- optionMaybe $ char '=' >> v4pattern
w <- weight
- return $ DnsblSite d return_codes w
+ return $ Site d return_codes w
--- | Parse more than one 'DnsblSite', separated by commas and/or
+-- | Parse more than one 'Site', separated by commas and/or
-- whitespace.
--
-- ==== _Examples_
-- >>> let spamhaus = "zen.spamhaus.org*3"
-- >>> let mailspike = "bl.mailspike.net=127.0.0.[2;10;11]*2"
-- >>> let bl_list = spamhaus ++ "," ++ mailspike
--- >>> pretty_print $ parse dnsbl_sites "" bl_list
+-- >>> pretty_print $ parse sites "" bl_list
-- ["zen.spamhaus.org*3","bl.mailspike.net=127.0.0.[2;10;11]*2"]
-- >>> let bl_list = spamhaus ++ " , " ++ mailspike
--- >>> pretty_print $ parse dnsbl_sites "" bl_list
+-- >>> pretty_print $ parse sites "" bl_list
-- ["zen.spamhaus.org*3","bl.mailspike.net=127.0.0.[2;10;11]*2"]
-- >>> let bl_list = spamhaus ++ " " ++ mailspike
--- >>> pretty_print $ parse dnsbl_sites "" bl_list
+-- >>> pretty_print $ parse sites "" bl_list
-- ["zen.spamhaus.org*3","bl.mailspike.net=127.0.0.[2;10;11]*2"]
--
-- Any whitespace, in fact, should work:
-- >>> let spamhaus = "zen.spamhaus.org*3"
-- >>> let mailspike = "bl.mailspike.net=127.0.0.[2;10;11]*2"
-- >>> let bl_list = spamhaus ++ "\n,\t \t\r" ++ mailspike
--- >>> pretty_print $ parse dnsbl_sites "" bl_list
+-- >>> pretty_print $ parse sites "" bl_list
-- ["zen.spamhaus.org*3","bl.mailspike.net=127.0.0.[2;10;11]*2"]
--
-dnsbl_sites :: Parser [DnsblSite]
-dnsbl_sites = dnsbl_site `sepBy1` many1 (choice [char ',', space])
+sites :: Parser [Site]
+sites = site `sepBy1` many1 (choice [char ',', space])
-- * Tests
-dnsbl_site_tests :: TestTree
-dnsbl_site_tests =
+site_tests :: TestTree
+site_tests =
testGroup
- "DnsblSite tests"
+ "Site tests"
[ test_full_maincf_sites_parsed ]
-- | This is a sample \"postscreen_dnsbl_sites\" from a real main.cf.
--- We should be able to parse it as a list of 'DnsblSite's.
+-- We should be able to parse it as a list of 'Site's.
--
test_full_maincf_sites_parsed :: TestTree
test_full_maincf_sites_parsed =
testCase "a full main.cf list of postscreen_dnsbl_sites is parsed" $ do
-- Whatever, it's a test.
- let actual = pretty_show $ parse dnsbl_sites "" input
+ let actual = pretty_show $ parse sites "" input
actual @?= expected
where
input = intercalate ",\n\t" [