summaryrefslogtreecommitdiffstats
path: root/src/Reaktor/Plugins/System.hs
blob: 88b8d846c237a93205a34879866d41bea7df12e0 (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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Reaktor.Plugins.System (new) where

--import Prelude.Extended
import Blessings
import Control.Applicative
import Control.Concurrent (forkIO)
import Control.Exception (finally)
--import Data.Aeson
import Data.ByteString.Char8.Extended (ByteString)
import qualified Data.ByteString.Char8.Extended as BS
import qualified Data.Map as M
import Reaktor
import System.Environment (getEnvironment)
import System.FilePath.Posix (takeBaseName)
import System.IO (BufferMode(LineBuffering),hSetBuffering)
import System.IO (Handle,hClose,hPutStr,hIsEOF)
import Reaktor.Plugins.System.Internal -- TODO rename to Reaktor.Plugins.System again
import System.Process (StdStream(CreatePipe),waitForProcess)
import System.Process (createProcess,CreateProcess(..),proc)
import qualified Text.Regex.PCRE.Heavy as RE
import qualified Text.Regex.PCRE.Light as RE


new :: Config -> Actions -> IO (Message -> IO ())
new config@Config{..} actions@Actions{..} = do
    pure $ \case
        Message (Just prefix) "PRIVMSG" (msgtarget:text:[]) -> do

          nick_ <- aGetNick
          let hs = maybe [] id (M.lookup "PRIVMSG" cHooks)
          mapM_ (\h -> run1 config actions nick_ h prefix msgtarget text) hs

        Message (Just prefix) "JOIN" (channel:[]) -> do
          nick_ <- aGetNick
          let hs = maybe [] id (M.lookup "JOIN" cHooks)
          mapM_ (\h -> run1 config actions nick_ h prefix channel "") hs

        _ -> pure ()


run1 :: Config -> Actions -> ByteString -> SystemParams -> ByteString -> ByteString -> ByteString -> IO ()
run1 config@Config{..} actions@Actions{..} nick_ params prefix msgtarget text = do
    let
        isActivated =
          case activate params of
            Always -> Just ""
            Match ->
              case pattern params of
                Nothing -> Nothing
                Just pat ->
                  let
                      result = RE.scan patternRE text
                      patternRE = RE.compile pat []
                  in
                    if null result
                      then Nothing
                      else Just ""
            Query ->
              if
                | BS.isPrefixOf (nick_ <> ":") text ->
                  Just (nick_ <> ":")
                | BS.isPrefixOf "*:" text ->
                  Just "*:"
                | isQuery ->
                  Just ""
                | otherwise ->
                  Nothing

        audience = if isQuery then from else msgtarget

        -- TODO check if msgtarget is one of our channels?
        --      what if our nick has changed?
        isQuery = msgtarget == nick_

        from = BS.takeWhile (/='!') prefix
          --maybe prefix (flip BS.take prefix) $ BS.findIndex (=='!') prefix

    case isActivated of
      Just trigger -> do
        let cmdline = BS.dropWhile (==' ') $ BS.drop (BS.length trigger) text
            resultPrefix = if isQuery then [] else [from <> ":"]

            parseCommandLine' pat s =
                if null result then [] else snd (head result)
              where
                result = RE.scan patternRE s
                patternRE = RE.compile pat []

            parse' =
              case pattern params of
                Nothing -> [] -- TODO everything
                Just pat -> parseCommandLine' pat cmdline

            headMaybe x = if null x then Nothing else Just (head x)

            -- TODO rename "command" to something like "commandSpec"
            command' = case command params of
              Capture i ->
                case headMaybe (drop (fromIntegral i - 1) parse') of
                  Nothing -> Nothing
                  Just k -> M.lookup k (commands params)

              CaptureOr c -> Just c

            cmdName = case command params of
              Capture i ->
                case headMaybe (drop (fromIntegral i - 1) parse') of
                  Nothing -> "<CMDERP>"
                  Just k -> k

              CaptureOr c -> BS.pack (takeBaseName $ commandPath c)

            args' =
                map BS.unpack $
                map (maybe "" id) $
                reverse $
                dropWhile (==Nothing) $
                reverse $
                map f (arguments params)
              where
                f arg = case arg of
                  Capture i ->
                    case headMaybe (drop (fromIntegral i - 1) parse') of
                      Nothing -> Nothing
                      Just k -> Just k

                  CaptureOr x -> Just x

        case command' of
          Just c -> do
            -- aSend <- gets s_sendMsg
            -- putLog_ <- gets s_putLog
            let onErrLine s =
                  aLog $ SGR [31,1] $
                    Plain (BS.pack (takeBaseName $ commandPath c) <> ": "<> s)

                onOutLine s =
                  aSend (privmsg audience [s])

                extraEnv = [("_prefix", BS.unpack prefix),
                            ("_from", BS.unpack from)]

            fork config actions c args' (Just extraEnv) "" onOutLine onErrLine

          Nothing -> do
            aSend (privmsg audience (resultPrefix <> [cmdName <> ": command not found"]))

      Nothing -> return ()

fork :: Config
     -> Actions
     -> SystemCommand
     -> [String]
     -> Maybe [(String, String)]
     -> String
     -> (ByteString -> IO ())
     -> (ByteString -> IO ())
     -> IO ()
fork Config{..} Actions{..} cmd args extraEnv input onOutLine onErrLine = do

    baseEnv <- getEnvironment

    let procEnv = M.toList $ mconcat [
                    maybe mempty M.fromList extraEnv,
                    maybe mempty id (commandEnv cmd),
                    M.fromList baseEnv
                  ]

    (inh, outh, errh) <- do
      (Just inh, Just outh, Just errh, ph) <-
        createProcess (proc (commandPath cmd) args) {
          cwd = commandWorkDir cmd <|> cDefaultWorkDir,
          env = Just procEnv,
          std_in = CreatePipe,
          std_out = CreatePipe,
          std_err = CreatePipe,
          close_fds = True,
          create_group = True,
          new_session = True
        }
      _ <- forkIO $ waitForProcess ph >> return ()
      return (inh, outh, errh)

    mapM_ forkIO [
        hPutStr inh input `finally` hClose inh,
        hWithLines outh onOutLine,
        hWithLines errh onErrLine
      ]


hWithLines :: Handle -> (ByteString -> IO ()) -> IO ()
hWithLines h f = do
    hSetBuffering h LineBuffering
    go `finally` hClose h
  where
    go =
      hIsEOF h >>= \case
        True -> return ()
        False -> BS.hGetLine h >>= f >> go