module Lambdabot.Plugin.Novelty.Filter (filterPlugin) where
import Lambdabot.Plugin
import Lambdabot.Util
import Control.Applicative
import Data.Maybe
import System.Directory (findExecutable)
import System.Process
filterPlugin :: Module [(String, FilePath, String)]
filterPlugin :: Module [(String, String, String)]
filterPlugin = Module [(String, String, String)]
forall st. Module st
newModule
{ moduleDefState :: LB [(String, String, String)]
moduleDefState = [Maybe (String, String, String)] -> [(String, String, String)]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe (String, String, String)] -> [(String, String, String)])
-> LB [Maybe (String, String, String)]
-> LB [(String, String, String)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [LB (Maybe (String, String, String))]
-> LB [Maybe (String, String, String)]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
[ do
Maybe String
mbPath <- IO (Maybe String) -> LB (Maybe String)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (String -> IO (Maybe String)
findExecutable String
name)
Maybe (String, String, String)
-> LB (Maybe (String, String, String))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (String, String, String)
-> LB (Maybe (String, String, String)))
-> Maybe (String, String, String)
-> LB (Maybe (String, String, String))
forall a b. (a -> b) -> a -> b
$! do
String
path <- Maybe String
mbPath
(String, String, String) -> Maybe (String, String, String)
forall a. a -> Maybe a
Just (String
name, String
path, String
descr)
| (name :: String
name, descr :: String
descr) <- [(String, String)]
filters
]
, moduleCmds :: ModuleT
[(String, String, String)]
LB
[Command (ModuleT [(String, String, String)] LB)]
moduleCmds = do
[(String, String, String)]
activeFilters <- ModuleT [(String, String, String)] LB [(String, String, String)]
forall (m :: * -> *). MonadLBState m => m (LBState m)
readMS
[Command (ModuleT [(String, String, String)] LB)]
-> ModuleT
[(String, String, String)]
LB
[Command (ModuleT [(String, String, String)] LB)]
forall (m :: * -> *) a. Monad m => a -> m a
return
[ (String -> Command Identity
command String
name)
{ help :: Cmd (ModuleT [(String, String, String)] LB) ()
help = String -> Cmd (ModuleT [(String, String, String)] LB) ()
forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
descr
, process :: String -> Cmd (ModuleT [(String, String, String)] LB) ()
process = \s :: String
s -> do
case String -> [String]
words String
s of
[] -> String -> Cmd (ModuleT [(String, String, String)] LB) ()
forall (m :: * -> *). Monad m => String -> Cmd m ()
say ("usage: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ " <phrase>")
t :: [String]
t -> IO String -> Cmd (ModuleT [(String, String, String)] LB) ()
forall (m :: * -> *). MonadIO m => IO String -> Cmd m ()
ios80 (String -> String -> IO String
runFilter String
path ([String] -> String
unwords [String]
t))
}
| (name :: String
name, path :: String
path, descr :: String
descr) <- [(String, String, String)]
activeFilters
]
}
filters :: [(String, String)]
filters :: [(String, String)]
filters =
[ ("austro", "austro <phrase>. Talk like Ahhhnold")
, ("b1ff", "b1ff <phrase>. B1ff of usenet yore")
, ("brooklyn", "brooklyn <phrase>. Yo")
, ("chef", "chef <phrase>. Bork bork bork")
, ("cockney", "cockney <phrase>. Londoner accent")
, ("drawl", "drawl <phrase>. Southern drawl")
, ("dubya", "dubya <phrase>. Presidential filter")
, ("fudd", "fudd <phrase>. Fudd, Elmer")
, ("funetak", "funetak <phrase>. Southern drawl")
, ("jethro", "jethro <phrase>. Now listen to a story 'bout a man named Jed...")
, ("jive", "jive <phrase>. Slap ma fro")
, ("kraut", "kraut <phrase>. German accent")
, ("pansy", "pansy <phrase>. Effeminate male")
, ("pirate", "pirate <phrase>. Talk like a pirate")
, ("postmodern", "postmodern <phrase>. Feminazi")
, ("redneck", "redneck <phrase>. Deep south")
, ("valspeak", "valley <phrase>. Like, ya know?")
, ("warez", "warez <phrase>. H4x0r")
]
runFilter :: String -> String -> IO String
runFilter :: String -> String -> IO String
runFilter f :: String
f s :: String
s = do
String
out <- String -> [String] -> String -> IO String
readProcess String
f [] String
s
String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> IO String) -> String -> IO String
forall a b. (a -> b) -> a -> b
$ String -> String
result String
out
where result :: String -> String
result [] = "Couldn't run the filter."
result xs :: String
xs = [String] -> String
unlines ([String] -> String) -> (String -> [String]) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (String -> Bool) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==' ')) ([String] -> [String])
-> (String -> [String]) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
xs