1 -- This file is part of Intricacy
2 -- Copyright (C) 2013 Martin Bays <mbays@sdf.org>
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of version 3 of the GNU General Public License as
6 -- published by the Free Software Foundation, or any later version.
8 -- You should have received a copy of the GNU General Public License
9 -- along with this program. If not, see http://www.gnu.org/licenses/.
15 import qualified Data
.ByteString
.Lazy
as L
17 import Data
.List
(intersperse)
19 import System
.Environment
22 --import Data.BinaryCom as BC
24 import Text
.Show.Pretty
(ppShow
)
26 import BinaryInstances
33 port
= 27001 -- 27001 == ('i'<<8) + 'y'
34 main
= withStream
(IP
"localhost" port
) handler
36 readAL
:: String -> ActiveLock
37 readAL s
= ActiveLock
(take 3 s
) (ord (s
!!4) - ord 'A
')
39 handler
:: Handle -> IO ()
41 (name
:passwd
:cmd
:args
) <- getArgs
43 "auth" -> return Authenticate
44 "reg" -> return Register
45 "sinfo" -> return GetServerInfo
46 "getlock" -> return $ GetLock
(read $ head args
)
47 "getuinfo" -> return $ GetUserInfo
(head args
)
48 "getsolution" -> return $ GetSolution
(read $ head args
)
49 "gethint" -> return $ GetHint
(read $ head args
)
50 "setlock" -> liftM3 SetLock
(fromJust <$> readReadFile
(head args
)) (return $ read (args
!!1)) (fromJust <$> readReadFile
(args
!!2))
51 "declare" -> liftM4 DeclareSolution
(fromJust <$> readReadFile
(head args
)) (return $ read (args
!!1)) (return $ readAL
(args
!!2)) (return $ read (args
!!3))
52 let request
= ClientRequest protocolVersion
(Just
(Auth name passwd
)) action
53 L
.hPut handle
$ encode request
55 response
<- decode
<$> L
.hGetContents handle
56 putStrLn $ ppShow
(response
:: ServerResponse
)