module
Network.Cell
(
Cell (..)
, getCells
, getQuality
, isOpen
, getValue
, getOpenEssids)
where
import Data.List
newtype Cell = Cell [String] deriving Eq
instance Ord Cell where
compare c d = compare (getQuality c) (getQuality d)
getCells
:: [String]
-> [Cell]
getCells [] = []
getCells iwlist
= case cell of
(_ : _) -> Cell cell : rest
_ -> rest
where
isPrefix = isPrefixOf prefix
cell = map (drop (length prefix)) $ takeWhile isPrefix usable
rest = getCells (dropWhile isPrefix usable)
usable = dropWhile (not . isPrefix) iwlist
getQuality :: Cell -> Int
getQuality = read . takeWhile (/= '/') . getValue "Quality:"
isOpen :: Cell -> Bool
isOpen cell = getValue "Encryption key:" cell == "off"
getValue
:: String
-> Cell
-> String
getValue name (Cell cell)
= case mValue of
(value : _) -> value
_ -> error $ show cell
where
mValue = map (drop (length name)) $ filter (isPrefixOf name) cell
getOpenEssids :: [Cell] -> [String]
getOpenEssids
= nub
. map (getValue "ESSID:")
. reverse
. sort
. filter isOpen
prefix :: String
prefix = replicate 20 ' '