summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-07-31 00:45:19 -0400
committerjoe <joe@jerkface.net>2014-07-31 00:45:19 -0400
commitdf49ab92f3f4d30eb7578cdef3f929e95dfff098 (patch)
treee0c073de7ed06a3b26bc580d760e142d73b107f7
parent914c7404d0add86cd44a044c4aceafb3cdc8db4c (diff)
Hosts.hs tweaks
-rw-r--r--Hosts.hs46
1 files changed, 43 insertions, 3 deletions
diff --git a/Hosts.hs b/Hosts.hs
index 5b117c7..29e54be 100644
--- a/Hosts.hs
+++ b/Hosts.hs
@@ -130,7 +130,7 @@ scrubName f line = line'
130 (a,ws') = splitAt 2 ws 130 (a,ws') = splitAt 2 ws
131 ws'' = f ws' 131 ws'' = f ws'
132 line' = if null ws'' 132 line' = if null ws''
133 then if length a==2 then "# " <> L.concat a <> ign 133 then if length a==2 then "" -- "# " <> L.concat a <> ign
134 else line 134 else line
135 else if length a==2 135 else if length a==2
136 then L.concat (a ++ ws'') <> ign 136 then L.concat (a ++ ws'') <> ign
@@ -147,6 +147,36 @@ scrubName f line = line'
147assignName :: SockAddr -> L.ByteString -> Hosts -> Hosts 147assignName :: SockAddr -> L.ByteString -> Hosts -> Hosts
148assignName addr name hosts = assignName0 False addr name hosts 148assignName addr name hosts = assignName0 False addr name hosts
149 149
150chaddr :: Int -> SockAddr -> Hosts -> Hosts
151chaddr n addr hosts =
152 hosts { addrnum = Map.insert addr n (addrnum hosts)
153 , numline = Map.adjust re n (numline hosts) }
154 where
155 re line = if length a==2
156 then L.pack (inet_ntop addr) <> " " <> L.concat ws' <> ign
157 else line
158 where (x,ign) = L.break (=='#') line
159 ws = L.groupBy ( (==EQ) `oo` comparing isSpace) x
160 where oo = (.) . (.)
161 (a,ws') = splitAt 2 ws
162
163isLonerName line = length ws' <= 2
164 where (x,_) = L.break (=='#') line
165 ws = L.groupBy ( (==EQ) `oo` comparing isSpace) x
166 where oo = (.) . (.)
167 (_,ws') = splitAt 2 ws
168
169scrubTrailingEmpties :: Hosts -> Hosts
170scrubTrailingEmpties hosts =
171 hosts { lineCount = cnt'
172 , numline = foldl' (flip Map.delete) (numline hosts) es
173 }
174 where
175 cnt = lineCount hosts
176 es = takeWhile (\n -> Map.lookup n (numline hosts) == Just "")
177 $ [cnt,cnt-1..]
178 cnt' = cnt - length es
179
150assignName0 :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts 180assignName0 :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts
151assignName0 iscannon addr name hosts = hosts' 181assignName0 iscannon addr name hosts = hosts'
152 where 182 where
@@ -157,9 +187,19 @@ assignName0 iscannon addr name hosts = hosts'
157 then hosts -- address already has name, nothing to do 187 then hosts -- address already has name, nothing to do
158 else 188 else
159 let hosts0 = -- remove name if it's present 189 let hosts0 = -- remove name if it's present
160 maybe hosts (removeName hosts) ns 190 scrubTrailingEmpties $ maybe hosts (removeName hosts) ns
191 ns' = fmap (filter $
192 isLonerName
193 . fromJust
194 . (\n -> Map.lookup n (numline hosts)))
195 ns
196 >>= listToMaybe
161 hosts1 = -- insert name, or add new line 197 hosts1 = -- insert name, or add new line
162 maybe (newLine hosts0) (appendName iscannon name hosts0) a 198 maybe (maybe (newLine hosts0)
199 (\n -> chaddr n addr $ appendName iscannon name hosts0 n)
200 ns')
201 (appendName iscannon name hosts0)
202 a
163 in hosts1 203 in hosts1
164 removeName hosts nums = hosts 204 removeName hosts nums = hosts
165 { namenum = Map.delete name (namenum hosts) 205 { namenum = Map.delete name (namenum hosts)