aboutsummaryrefslogtreecommitdiffstats
path: root/src/Reaktor/Plugins/System/Internal.hs
blob: 2ed923d57e0eb7e2a4e0652a53bdcfda04266d9a (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
77
78
79
{-# LANGUAGE OverloadedStrings #-}
module Reaktor.Plugins.System.Internal where

import Prelude.Extended
import Data.Aeson
import qualified Data.ByteString.Char8.Extended as BS
import qualified Data.Map as M
import Reaktor ()


-- TODO this needs better names :)
data CaptureOr a = Capture Integer | CaptureOr a
  deriving Show -- TODO killme

instance FromJSON a => FromJSON (CaptureOr a) where
  parseJSON o@(Number _) = Capture <$> parseJSON o -- TODO don't parse twice
  parseJSON o = CaptureOr <$> parseJSON o

-- TODO query means via direct privmsg and <nick>:
data Activate = Always | Match | Query

instance FromJSON Activate where
  parseJSON (String "always") = pure Always
  parseJSON (String "match") = pure Match
  parseJSON (String "query") = pure Query
  parseJSON _ = undefined

data Config = Config {
  cDefaultWorkDir :: Maybe FilePath,
  -- TODO IrcCommand as key for map
  cHooks :: M.Map BS.ByteString [SystemParams]
}

instance Default Config where
  def = Config Nothing mempty

instance FromJSON Config where
  parseJSON (Object v) =
    Config
      <$> v .:? "workdir"
      <*> v .:? "hooks" .!= M.empty
  parseJSON _ = pure undefined

data SystemParams = SystemParams {
  activate :: Activate,
  pattern :: Maybe BS.ByteString, -- TODO RE
  command :: CaptureOr SystemCommand,
  arguments :: [CaptureOr BS.ByteString],
  workDir :: Maybe FilePath,
  commands :: M.Map BS.ByteString SystemCommand
}

instance FromJSON SystemParams where
  parseJSON (Object v) =
      SystemParams
        <$> v .:? "activate" .!= Query
        <*> v .:? "pattern"
        <*> v .: "command"
        <*> v .:? "arguments" .!= []
        <*> v .:? "workdir"
        <*> v .:? "commands" .!= M.empty
  parseJSON _ = pure undefined


data SystemCommand = SystemCommand {
    commandPath :: FilePath,
    commandWorkDir :: Maybe FilePath,
    commandEnv :: Maybe (M.Map String String)
  }
  deriving Show -- TODO killme

instance FromJSON SystemCommand where
  parseJSON (Object v) =
      SystemCommand
        <$> v .: "filename"
        <*> v .:? "workdir"
        <*> v .:? "env"
  parseJSON _ = pure undefined