-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ba697c
commit 9823d39
Showing
7 changed files
with
81 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
module Filter(Filter(..), FilterAction(..), FilterActions) where | ||
module Filter(Filter(..), FilterAction(..), FilterActions(..), shouldAck, shouldCopy) where | ||
|
||
import Message | ||
|
||
type FilterActions = [FilterAction] | ||
newtype FilterActions = MkFilterActions [FilterAction] | ||
|
||
instance Semigroup FilterActions where | ||
(MkFilterActions l) <> (MkFilterActions r) = MkFilterActions (l <> r) | ||
|
||
instance Monoid FilterActions where | ||
mempty = MkFilterActions [] | ||
|
||
data FilterAction = | ||
Ack | ||
| Copy | ||
deriving (Eq, Show) | ||
|
||
shouldAck :: FilterActions -> Bool | ||
shouldAck (MkFilterActions actions) = elem Ack actions | ||
|
||
shouldCopy :: FilterActions -> Bool | ||
shouldCopy (MkFilterActions actions) = elem Copy actions | ||
|
||
class Monad m => Filter m where | ||
filterAction :: Message -> m FilterActions | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,36 @@ | ||
module ScriptFilter(scriptFilter, defaultFilter) where | ||
module ScriptFilter(scriptFilter, defaultFilter, parseAction) where | ||
|
||
import Filter | ||
import Message | ||
import System.Process | ||
|
||
scriptFilter :: FilePath -> Message -> IO FilterAction | ||
scriptFilter _ _ = return CopyAndAck | ||
import qualified Data.ByteString.Char8 as BSC(null, breakSubstring, hGetLine, hPutStrLn, ByteString) | ||
import System.IO(hFlush) | ||
|
||
defaultFilter :: Message -> IO FilterAction | ||
defaultFilter _ = return CopyAndAck | ||
contains :: BSC.ByteString -> BSC.ByteString -> Bool | ||
contains text pattern = let (_, after) = BSC.breakSubstring pattern text | ||
in not $ BSC.null after | ||
|
||
actionChecker :: BSC.ByteString -> FilterAction -> BSC.ByteString -> FilterActions | ||
actionChecker pattern action line = let actions = if line `contains` pattern then [action] else [] | ||
in MkFilterActions actions | ||
|
||
actionCheckers :: [BSC.ByteString -> FilterActions] | ||
actionCheckers = [actionChecker "ack" Ack, actionChecker "copy" Copy] | ||
|
||
parseAction :: BSC.ByteString -> FilterActions | ||
parseAction line = mconcat $ actionCheckers <*> [line] | ||
|
||
serializeMessage :: Message -> BSC.ByteString | ||
serializeMessage (MkMessage _ _ msg) = msg | ||
|
||
scriptFilter :: FilePath -> Message -> IO FilterActions | ||
scriptFilter path msg = do | ||
(Just inHandle, Just outHandle, _, _) <- createProcess (proc path []){ std_in = CreatePipe, std_out = CreatePipe } | ||
_ <- BSC.hPutStrLn inHandle (serializeMessage msg) | ||
hFlush inHandle | ||
line <- BSC.hGetLine outHandle | ||
return $ parseAction line | ||
|
||
defaultFilter :: Message -> IO FilterActions | ||
defaultFilter _ = return $ MkFilterActions [Copy, Ack] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters