| 1 |
-- | Talk to hot chixxors. |
| 2 |
|
| 3 |
-- (c) Mark Wotton |
| 4 |
-- Serialisation (c) 2007 Don Stewart |
| 5 |
|
| 6 |
module Lambdabot.Plugin.Novelty.Vixen (vixenPlugin) where |
| 7 |
|
| 8 |
import Lambdabot.Plugin |
| 9 |
import Lambdabot.Util |
| 10 |
|
| 11 |
import Control.Arrow ((***)) |
| 12 |
import Control.Monad |
| 13 |
import Data.Binary |
| 14 |
import qualified Data.ByteString.Char8 as P |
| 15 |
import qualified Data.ByteString.Lazy as L |
| 16 |
import System.Directory |
| 17 |
import Text.Regex.TDFA |
| 18 |
|
| 19 |
vixenPlugin :: Module (Bool, String -> IO [Char]) |
| 20 |
vixenPlugin = newModule |
| 21 |
{ moduleCmds = return |
| 22 |
[ (command "vixen") |
| 23 |
{ help = say "vixen <phrase>. Sergeant Curry's lonely hearts club" |
| 24 |
, process = \txt -> say =<< io . ($ txt) . snd =<< readMS |
| 25 |
} |
| 26 |
, (command "vixen-on") |
| 27 |
{ privileged = True |
| 28 |
, help = do |
| 29 |
me <- showNick =<< getLambdabotName |
| 30 |
say ("vixen-on: turn " ++ me ++ " into a chatterbot") |
| 31 |
, process = const $ do |
| 32 |
modifyMS $ \(_,r) -> (True, r) |
| 33 |
say "What's this channel about?" |
| 34 |
} |
| 35 |
, (command "vixen-off") |
| 36 |
{ privileged = True |
| 37 |
, help = do |
| 38 |
me <- showNick =<< getLambdabotName |
| 39 |
say ("vixen-off: shut " ++ me ++ "up") |
| 40 |
, process = const $ do |
| 41 |
modifyMS $ \(_,r) -> (False, r) |
| 42 |
say "Bye!" |
| 43 |
} |
| 44 |
] |
| 45 |
|
| 46 |
-- if vixen-chat is on, we can just respond to anything |
| 47 |
, contextual = \txt -> do |
| 48 |
(alive, k) <- readMS |
| 49 |
if alive then io (k txt) >>= say |
| 50 |
else return () |
| 51 |
|
| 52 |
, moduleDefState = return (False, const (return "<undefined>")) |
| 53 |
|
| 54 |
-- suck in our (read only) regex state from disk |
| 55 |
-- compile it, and stick it in the plugin state |
| 56 |
, moduleSerialize = Just $ readOnly $ \bs -> |
| 57 |
let st = decode (L.fromStrict bs) |
| 58 |
compiled = map (makeRegex *** id) (st :: [(String, WTree)]) |
| 59 |
in (False, vixen (mkResponses compiled)) |
| 60 |
} |
| 61 |
|
| 62 |
------------------------------------------------------------------------ |
| 63 |
|
| 64 |
vixen :: (String -> WTree) -> String -> IO String |
| 65 |
vixen k key = P.unpack `fmap` randomW (k key) |
| 66 |
|
| 67 |
randomW :: WTree -> IO P.ByteString |
| 68 |
randomW (Leaf a) = return a |
| 69 |
randomW (Node ls) = random ls >>= randomW |
| 70 |
|
| 71 |
mkResponses :: RChoice -> String -> WTree |
| 72 |
mkResponses choices them = (\((_,wtree):_) -> wtree) $ |
| 73 |
filter (\(reg,_) -> match reg them) choices |
| 74 |
|
| 75 |
------------------------------------------------------------------------ |
| 76 |
-- serialisation for the vixen state |
| 77 |
-- |
| 78 |
-- The tree of regexes and responses is written in binary form to |
| 79 |
-- State/vixen, and we suck it in on module init, then lazily regexify it all |
| 80 |
|
| 81 |
data WTree = Leaf !P.ByteString | Node ![WTree] |
| 82 |
deriving Show |
| 83 |
|
| 84 |
instance Binary WTree where |
| 85 |
put (Leaf s) = putWord8 0 >> put s |
| 86 |
put (Node ls) = putWord8 1 >> put ls |
| 87 |
get = do |
| 88 |
tag <- getWord8 |
| 89 |
case tag of |
| 90 |
0 -> liftM Leaf get |
| 91 |
1 -> liftM Node get |
| 92 |
_ -> error "Vixen plugin error: unknown tag" |
| 93 |
|
| 94 |
type RChoice = [(Regex, WTree)] -- compiled choices |