------------------------------------------------------------------------------- -- | -- Module: Main -- Copyright: (c) 2008 Marco TĂșlio Gontijo e Silva -- License: Simple permissive license (see LICENSE) -- -- Maintainer: Marco TĂșlio Gontijo e Silva -- Stability: unstable -- Portability: unportable -- -- Utility to call iwconfig with open networks. ------------------------------------------------------------------------------- -- base import Control.Applicative import Data.List import Data.Maybe import Data.IORef import GHC.Conc import System.Environment import System.Exit -- mtl import Control.Monad.Reader -- process import System.Cmd -- HSH import HSH.Command -- n-m import Network.Cell dhclient :: String -> IORef (Maybe ThreadId) -> IO () dhclient interface waitThread = do exitCode <- system $ "dhclient " ++ interface when (exitCode == ExitSuccess) $ readIORef waitThread >>= killThread . fromJust main :: IO () main = do interface <- fromMaybe "wlan0" <$> listToMaybe <$> getArgs run ("iwlist " ++ interface ++ " scan") >>= iwconfig interface . getOpenEssids . getCells iwconfig :: String -> [String] -> IO () iwconfig _ [] = return () iwconfig interface (essid : rest) = do iWaitThread <- newIORef Nothing dhclientThread <- liftIO $ system ("iwconfig " ++ interface ++ " essid " ++ essid) >> forkIO (dhclient interface iWaitThread) waitThread <- forkIO $ wait interface dhclientThread rest writeIORef iWaitThread $ Just waitThread wait :: String -> ThreadId -> [String] -> IO () wait interface dhclientThread rest = do char <- liftIO getChar if char == 'n' then liftIO (killThread dhclientThread) >> iwconfig interface rest else wait interface dhclientThread rest