-- | GNU Talk Filters
-- needs: http://www.hyperrealm.com/main.php?s=talkfilters
-- Edward Kmett 2006

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

-- State consists of a map from filter name to executable path

filterPlugin :: Module [(String, FilePath, String)]
filterPlugin :: Module [(String, String, String)]
filterPlugin = Module [(String, String, String)]
forall st. Module st
newModule
    { moduleDefState = catMaybes <$> sequence
        [ do
            mbPath <- io (findExecutable name)
            return $! do
                path <- mbPath
                Just (name, path, descr)
        | (name, descr) <- filters
        ]

    , moduleCmds = do
        activeFilters <- readMS
        return
            [ (command name)
                { help = say descr
                , process = \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 (String
"usage: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" <phrase>")
                            [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, path, descr) <- activeFilters
            ]
    }

filters :: [(String, String)]
filters :: [(String, String)]
filters =
    [ (String
"austro",     String
"austro <phrase>. Talk like Ahhhnold")
    , (String
"b1ff",       String
"b1ff <phrase>. B1ff of usenet yore")
    , (String
"brooklyn",   String
"brooklyn <phrase>. Yo")
    , (String
"chef",       String
"chef <phrase>. Bork bork bork")
    , (String
"cockney",    String
"cockney <phrase>. Londoner accent")
    , (String
"drawl",      String
"drawl <phrase>. Southern drawl")
    , (String
"dubya",      String
"dubya <phrase>. Presidential filter")
    , (String
"fudd",       String
"fudd <phrase>. Fudd, Elmer")
    , (String
"funetak",    String
"funetak <phrase>. Southern drawl")
    , (String
"jethro",     String
"jethro <phrase>. Now listen to a story 'bout a man named Jed...")
    , (String
"jive",       String
"jive <phrase>. Slap ma fro")
    , (String
"kraut",      String
"kraut <phrase>. German accent")
    , (String
"pansy",      String
"pansy <phrase>. Effeminate male")
    , (String
"pirate",     String
"pirate <phrase>. Talk like a pirate")
    , (String
"postmodern", String
"postmodern <phrase>. Feminazi")
    , (String
"redneck",    String
"redneck <phrase>. Deep south")
    , (String
"valspeak",   String
"valley <phrase>. Like, ya know?")
    , (String
"warez",      String
"warez <phrase>. H4x0r")
    ]

runFilter :: String -> String -> IO String
runFilter :: String -> String -> IO String
runFilter String
f String
s = do
    String
out <- String -> [String] -> String -> IO String
readProcess String
f [] String
s
    String -> IO String
forall a. a -> IO a
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 [] = String
"Couldn't run the filter."
          result 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
==Char
' ')) ([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