1 -- | This module contains the 'Site' data type representing one
2 -- blacklist with its associated return codes and weight. For
3 -- example, in Postfix's main.cf you might have,
5 -- postscreen_dnsbl_sites = bl.mailspike.net=127.0.0.[2;10;11]*2, ...
7 -- Here, the blacklist (a 'UserDomain') is \"bl.mailspike.net\", the
8 -- return code pattern is \"127.0.0.[2;10;11]\", and the weight is
11 module Network.DNS.RBL.Site (
18 import Data.List ( intercalate )
19 import Test.Tasty ( TestTree, testGroup )
20 import Test.Tasty.HUnit ( (@?=), testCase )
34 import Text.Parsec.String ( Parser )
35 import Text.Read ( readMaybe )
37 import Network.DNS.RBL.Domain ( UserDomain, user_domain )
38 import Network.DNS.RBL.IPv4Pattern ( IPv4Pattern, v4pattern )
39 import Network.DNS.RBL.Pretty ( Pretty(..) )
42 newtype Weight = Weight Int deriving (Eq, Show)
44 instance Pretty Weight where
45 pretty_show (Weight w) = show w
48 -- | Parse the weight multiplier at the end of a site.
52 -- >>> import Text.Parsec ( parseTest )
54 -- Negative, zero, and positive integers are all supported:
56 -- >>> parseTest weight "*-5"
59 -- >>> parseTest weight "*0"
62 -- >>> parseTest weight "*17"
65 -- If the weight is empty, it defaults to @1@:
67 -- >>> parseTest weight ""
70 -- The default is used whenever parsing fails:
72 -- >>> parseTest weight "*hello"
75 -- The 'Pretty' instance works as intended:
77 -- >>> import Text.Parsec ( parse )
78 -- >>> pretty_print $ parse weight "" "*3"
81 weight :: Parser Weight
82 weight = try parse_weight <|> return (Weight 1)
86 sign <- (char '-') <|> (option '+' (char '+'))
88 case ( readMaybe w :: Maybe Int ) of
89 -- If "many1 digit" gives us a list of digits, we should be able
90 -- to convert that to an Int! It will overflow rather than fail
91 -- if the input is too big/small, so it should really always
93 Nothing -> unexpected "weight: readMaybe failed on a sequence of digits!"
94 Just k -> return $ Weight (if sign == '-' then negate k else k)
98 -- | A DNSBL as it would be input into postfix. It has a blacklist
99 -- (DNS) name, a pattern of addresses to use for a \"hit\", and a
100 -- weight multiplier.
102 data Site = Site UserDomain (Maybe IPv4Pattern) Weight
105 -- | Pretty print DNSBL sites. This is straightforward except for the
106 -- weight. We default to a weight of @1@, but this leaves us with a
107 -- choice. If the user leaves off the weight, do we want to
108 -- pretty-print it as @1@? How about if we explicitly writes the
109 -- \"*1\" multiplier?
111 -- The pretty-printing isn't user-facing, really, so it makes sense
112 -- to just choose one of these behaviors rather than pass around a
113 -- @Maybe Weight@. We always print the multiplier, even when it's @1@.
115 instance Pretty Site where
116 pretty_show (Site d p w) =
117 (pretty_show d) ++ pattern_string ++ "*" ++ (pretty_show w)
119 pattern_string = case p of
121 Just pat -> "=" ++ pretty_show pat
124 -- | Parse a single 'Site'.
128 -- >>> import Text.Parsec ( parse )
130 -- >>> let spamhaus = "zen.spamhaus.org*3"
131 -- >>> pretty_print $ parse site "" spamhaus
132 -- zen.spamhaus.org*3
134 -- >>> let mailspike = "bl.mailspike.net=127.0.0.[2;10;11]*2"
135 -- >>> pretty_print $ parse site "" mailspike
136 -- bl.mailspike.net=127.0.0.[2;10;11]*2
138 -- If the weight is left unspecified, it defaults to \"1\" which is
141 -- >>> let hostkarma = "hostkarma.junkemailfilter.com=127.0.0.2"
142 -- >>> pretty_print $ parse site "" hostkarma
143 -- hostkarma.junkemailfilter.com=127.0.0.2*1
145 -- >>> let ubl = "ubl.unsubscore.com"
146 -- >>> pretty_print $ parse site "" ubl
147 -- ubl.unsubscore.com*1
152 return_codes <- optionMaybe $ char '=' >> v4pattern
154 return $ Site d return_codes w
157 -- | Parse more than one 'Site', separated by commas and/or
162 -- >>> import Text.Parsec ( parse )
164 -- Any combination of comma/spaces can be used as a separator:
166 -- >>> let spamhaus = "zen.spamhaus.org*3"
167 -- >>> let mailspike = "bl.mailspike.net=127.0.0.[2;10;11]*2"
168 -- >>> let bl_list = spamhaus ++ "," ++ mailspike
169 -- >>> pretty_print $ parse sites "" bl_list
170 -- ["zen.spamhaus.org*3","bl.mailspike.net=127.0.0.[2;10;11]*2"]
171 -- >>> let bl_list = spamhaus ++ " , " ++ mailspike
172 -- >>> pretty_print $ parse sites "" bl_list
173 -- ["zen.spamhaus.org*3","bl.mailspike.net=127.0.0.[2;10;11]*2"]
174 -- >>> let bl_list = spamhaus ++ " " ++ mailspike
175 -- >>> pretty_print $ parse sites "" bl_list
176 -- ["zen.spamhaus.org*3","bl.mailspike.net=127.0.0.[2;10;11]*2"]
178 -- Any whitespace, in fact, should work:
180 -- >>> let spamhaus = "zen.spamhaus.org*3"
181 -- >>> let mailspike = "bl.mailspike.net=127.0.0.[2;10;11]*2"
182 -- >>> let bl_list = spamhaus ++ "\n,\t \t\r" ++ mailspike
183 -- >>> pretty_print $ parse sites "" bl_list
184 -- ["zen.spamhaus.org*3","bl.mailspike.net=127.0.0.[2;10;11]*2"]
186 sites :: Parser [Site]
187 sites = site `sepBy1` many1 (choice [char ',', space])
193 site_tests :: TestTree
197 [ test_full_maincf_sites_parsed ]
200 -- | This is a sample \"postscreen_dnsbl_sites\" from a real main.cf.
201 -- We should be able to parse it as a list of 'Site's.
203 test_full_maincf_sites_parsed :: TestTree
204 test_full_maincf_sites_parsed =
205 testCase "a full main.cf list of postscreen_dnsbl_sites is parsed" $ do
206 -- Whatever, it's a test.
207 let actual = pretty_show $ parse sites "" input
210 input = intercalate ",\n\t" [
211 "zen.spamhaus.org*3",
212 "b.barracudacentral.org*3",
213 "sip.invaluement.invalid*3",
214 "jerks.viabit.com*3",
215 "bl.mailspike.net=127.0.0.[2;10;11]*2",
217 "psbl.surriel.com*2",
218 "bl.mailspike.net=127.0.0.12*2",
219 "bl.spameatingmonkey.net*2",
222 "dnsbl-1.uceprotect.net",
223 "hostkarma.junkemailfilter.com=127.0.0.2",
224 "ubl.unsubscore.com",
227 -- We expect the "one" multipliers to have been added, and the
228 -- quotation marks to be added...
230 intercalate "\",\"" [
231 "zen.spamhaus.org*3",
232 "b.barracudacentral.org*3",
233 "sip.invaluement.invalid*3",
234 "jerks.viabit.com*3",
235 "bl.mailspike.net=127.0.0.[2;10;11]*2",
237 "psbl.surriel.com*2",
238 "bl.mailspike.net=127.0.0.12*2",
239 "bl.spameatingmonkey.net*2",
242 "dnsbl-1.uceprotect.net*1",
243 "hostkarma.junkemailfilter.com=127.0.0.2*1",
244 "ubl.unsubscore.com*1",
245 "dnsbl.zapbl.net*1" ]