-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.hs
120 lines (109 loc) · 3.3 KB
/
Config.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{-# LANGUAGE OverloadedStrings, TypeSynonymInstances, FlexibleInstances, TypeFamilies, FlexibleContexts, NoMonomorphismRestriction, CPP #-}
module Config where
import Control.Concurrent.STM
import Control.Monad
import Control.Monad.Reader
import Data.ByteString as B
import Data.ByteString.Lazy.Char8 as BLC
import Data.Map.Strict as MS
import Data.String.Class as S
import Data.Text as Text
import Data.Text.Encoding as E
import Network.NineP
import Network.NineP.File
import Types
class Configable a where
readcfg :: BLC.ByteString -> a
showcfg :: a -> BLC.ByteString
instance Configable String where
readcfg = BLC.unpack
showcfg = BLC.pack
instance Configable Text where
readcfg = E.decodeUtf8 . B.concat . toChunks
showcfg = fromChunks . (:[]) . E.encodeUtf8
instance Configable Bool where
readcfg = Prelude.read . BLC.unpack -- TODO
showcfg = BLC.pack . show
instance Configable Integer where
readcfg = Prelude.read . BLC.unpack
showcfg = BLC.pack . show
configTVarReadWith c acc = Just (liftM c . liftIO . readTVarIO . acc =<< ask)
configTVarWriteWith c acc x = liftIO . atomically . flip writeTVar (c x) . acc =<< ask
configTVarReadC = configTVarReadWith showcfg
configTVarWriteC = configTVarWriteWith readcfg
rwf n a b = (n, rwFile n a b)
rwfc n a = rwf n (configTVarReadC a) (Just (configTVarWriteC a))
configDir :: NineFile Hate
configDir = boringDir "config" [
rwfc "server" server,
rwfc "username" username,
rwfc "password" password,
rwfc "resource" resource,
rwfc "muc_default_nick" muc_default_nick,
rwfc "muc_history_request" muc_history_request,
rwfc "status" status,
rwfc "stream_management" streamManagement,
rwfc "permit_all_certs" permitUnsafeCerts
]
trimLn :: (Stringy s, Eq (StringCellChar s)) => s -> s
trimLn s = maybe "" (\c -> if c == (S.fromChar '\n') then S.init s else s) $ S.safeLast s
readVar :: TVar a -> Hate a
readVar = liftIO . readTVarIO
readVarH :: (GlobalState -> IO a) -> Hate a
readVarH acc = do
s <- ask
liftIO $ acc s
readSVar :: (Stringy a, Eq (StringCellChar a)) => TVar a -> Hate a
readSVar = liftM trimLn . readVar
writeVar v = liftIO . atomically . writeTVar v
initState = do
st <- newTVarIO ""
ut <- newTVarIO ""
pt <- newTVarIO ""
priot <- newTVarIO ""
portt <- newTVarIO ""
rt <- newTVarIO ""
jnt <- newTVarIO "hatexmpp3"
jot <- newTVarIO ""
jvt <- newTVarIO ""
mdnt <- newTVarIO "hatexmpp3"
showt <- newTVarIO SNone
statust <- newTVarIO ""
streamManagementt <- newTVarIO False
permitUnsafeCertst <- newTVarIO False
mhrst <- newTVarIO (-1)
sesst <- newTVarIO undefined
featureStreamManagement3t <- newTVarIO False
logst <- newTVarIO MS.empty
mucst <- newTVarIO MS.empty
chatst <- newTVarIO MS.empty
#ifdef UI_GTK
amt <- newTVarIO undefined
aut <- newTVarIO undefined
#endif
return $ GlobalState
{ server = st
, username = ut
, password = pt
, priority = priot
, port = portt
, resource = rt
, jiv_name = jnt
, jiv_os = jot
, jiv_version = jvt
, muc_default_nick = mdnt
, showst = showt
, status = statust
, streamManagement = streamManagementt
, permitUnsafeCerts = permitUnsafeCertst
, muc_history_request = mhrst
, sess = sesst
, featureStreamManagement3 = featureStreamManagement3t
, logs = logst
, mucs = mucst
, chats = chatst
#ifdef UI_GTK
, addMUCToRosterWindow = amt
, addUserToRosterWindow = aut
#endif
}