-
Notifications
You must be signed in to change notification settings - Fork 2
/
TelnetHandler.hs
35 lines (31 loc) · 1014 Bytes
/
TelnetHandler.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module TelnetHandler where
import System.IO
import Network.Socket
import Control.Monad
import Paths_harlson (version)
import Data.Version (showVersion)
import Data.Char
handleTelnet :: (String -> IO String) -> SockAddr -> Handle -> IO ()
handleTelnet runCmd sa h = do
hPutStrLn h "\n"
hPutStrLn h ("harlson " ++ showVersion version)
handleTelnet' runCmd sa h
handleTelnet' :: (String -> IO String) -> SockAddr -> Handle -> IO ()
handleTelnet' runCmd sa h = do
hPutStrLn h "Ctrl-D to quit"
hPutStr h "> "
hFlush h
c <- hLookAhead h
unless (c == chr 4) $
do
cmd <- fmap strip $ hGetLine h
unless (cmd == ":q" || cmd == [chr 4]) $
do
out <- runCmd cmd
hPutStrLn h out
hPutStrLn h ""
handleTelnet' runCmd sa h
strip :: String -> String
strip = lstrip . rstrip where
lstrip = dropWhile (`elem` " \t\r\n")
rstrip = reverse . lstrip . reverse