blob: 8330be9a193c54b1ebaa520250cf8a5c3bd99a37 (
plain)
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
|
{-# LANGUAGE OverloadedStrings #-}
module Reaktor.Config where
import Data.Aeson
import qualified Data.HashMap.Lazy as HML
import qualified Data.Text as T
import qualified Reaktor.Plugins
import Reaktor.Types
instance FromJSON Config where
parseJSON (Object v) = do
p <- v .:? "port" .!= defaultPort
Config
<$> v .: "hostname"
<*> pure p
<*> v .: "nick"
<*> v .:? "useTLS" .!= (p == tlsPort)
<*> v .:? "logTime" .!= True
<*> v .:? "plugins" .!= []
parseJSON _ = pure undefined
data Config = Config {
hostname :: HostName,
port :: ServiceName,
nick :: Nickname,
useTLS :: Bool,
logTime :: Bool,
pluginInstances :: [PluginInstance]
}
addPlugin :: T.Text -> IO Plugin -> Config -> Config
addPlugin name p r =
r { pluginInstances = pluginInstances r <> [PluginInstance name (Left p)] }
defaultPort :: ServiceName
defaultPort = tlsPort
tlsPort :: ServiceName
tlsPort = "6697"
data PluginInstance = PluginInstance {
pi_name :: T.Text,
pi_plugin :: Either (IO Plugin) Plugin
}
instance FromJSON PluginInstance where
parseJSON o@(Object v) =
case HML.lookup "plugin" v of
Just (String name) -> do
let p = Reaktor.Plugins.get name
c = HML.lookupDefault (Object HML.empty) "config" v
pure $ PluginInstance name (Left $ p c)
Just _ -> error ("bad plugin object: " <> show o)
_ -> error ("mising 'plugin' attribute: " <> show o)
parseJSON x =
error ("bad plugin type: " <> show x)
initPlugins :: Config -> IO Config
initPlugins cfg = do
plugins' <- mapM initPlugin (pluginInstances cfg)
return cfg { pluginInstances = plugins' }
where
initPlugin :: PluginInstance -> IO PluginInstance
initPlugin i = do
p <-
case pi_plugin i of
Right p -> return p
Left f -> f
return i { pi_plugin = Right p }
|