ditch network-fancy client dep
[intricacy.git] / ServerAddr.hs
blobb266c379af5e7c22540f3f2d95ce48befc6226df
1 -- This file is part of Intricacy
2 -- Copyright (C) 2013 Martin Bays <mbays@sdf.org>
3 --
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.
7 --
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/.
11 module ServerAddr where
13 import Control.Applicative
14 import Data.List
15 import Data.Maybe
17 data ServerAddr = ServerAddr {hostname::String, port::Int}
18 deriving (Eq, Ord, Show, Read)
19 nullSaddr (ServerAddr host _) = null host
21 defaultPort=27001 -- == ('i'<<8) + 'y'
22 defaultServerAddr = ServerAddr "i.thegonz.net" defaultPort
23 oldDefaultServerAddrs = [ServerAddr "thegonz.net" defaultPort]
25 updateDefaultSAddr :: ServerAddr -> ServerAddr
26 updateDefaultSAddr saddr | saddr `elem` oldDefaultServerAddrs = defaultServerAddr
27 updateDefaultSAddr saddr = saddr
29 saddrStr (ServerAddr h p) = h ++ if p==defaultPort then "" else ':':show p
31 -- |windows doesn't like ':' in paths, so use '#' instead
32 saddrPath (ServerAddr h p) = h ++ if p==defaultPort then "" else '#':show p
34 strToSaddr str =
35 case elemIndex ':' str of
36 Nothing -> Just $ ServerAddr str defaultPort
37 Just idx -> do
38 let (addr,portstr) = splitAt idx str
39 port <- fst <$> listToMaybe (reads (drop 1 portstr))
40 return $ ServerAddr addr port