Debian Codegraph @

src/Lambdabot/Plugin/Novelty/Quote.hs

raw
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE PatternGuards #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 -- | Support for quotes
5 module Lambdabot.Plugin.Novelty.Quote (quotePlugin) where
6
7 import Lambdabot.Plugin
8 import Lambdabot.Util
9
10 import qualified Data.ByteString.Char8 as P
11 import Data.Char
12 import Data.Fortune
13 import Data.List
14 import qualified Data.Map as M
15 import Data.Maybe
16 import Text.Regex.TDFA
17
18 type Key = P.ByteString
19 type Quotes = M.Map Key [P.ByteString]
20 type Quote = ModuleT Quotes LB
21
22 quotePlugin :: Module (M.Map P.ByteString [P.ByteString])
23 quotePlugin = newModule
24 { moduleSerialize = Just mapListPackedSerial
25 , moduleDefState = return M.empty
26 , moduleInit = modifyMS (M.filter (not . null))
27
28 , moduleCmds = return
29 [ (command "quote")
30 { help = say "quote <nick>: Quote <nick> or a random person if no nick is given"
31 , process = runQuote . strip isSpace
32 }
33 , (command "remember")
34 { help = say "remember <nick> <quote>: Remember that <nick> said <quote>."
35 , process = runRemember . strip isSpace
36 }
37 , (command "forget")
38 { help = say "forget nick quote. Delete a quote"
39 , process = runForget . strip isSpace
40 }
41 , (command "ghc")
42 { help = say "ghc. Choice quotes from GHC."
43 , process = const (fortune ["ghc"])
44 }
45 , (command "fortune")
46 { help = say "fortune. Provide a random fortune"
47 , process = const (fortune [])
48 }
49 , (command "yow")
50 { help = say "yow. The zippy man."
51 , process = const (fortune ["zippy"])
52 }
53 , (command "arr")
54 { help = say "arr. Talk to a pirate"
55 , process = const (fortune ["arr"])
56 }
57 , (command "yarr")
58 { help = say "yarr. Talk to a scurvy pirate"
59 , process = const (fortune ["arr", "yarr"])
60 }
61 , (command "keal")
62 { help = say "keal. Talk like Keal"
63 , process = const (fortune ["keal"])
64 }
65 , (command "b52s")
66 { help = say "b52s. Anyone noticed the b52s sound a lot like zippy?"
67 , process = const (fortune ["b52s"])
68 }
69 , (command "pinky")
70 { help = say "pinky. Pinky and the Brain"
71 , process = \s -> fortune $ if "pondering" `isInfixOf` s
72 then ["pinky-pondering"]
73 else ["pinky-pondering", "pinky"]
74 }
75 , (command "brain")
76 { help = say "brain. Pinky and the Brain"
77 , process = const (fortune ["brain"])
78 }
79 , (command "palomer")
80 { help = say "palomer. Sound a bit like palomer on a good day."
81 , process = const (fortune ["palomer"])
82 }
83 , (command "girl19")
84 { help = say "girl19 wonders what \"discriminating hackers\" are."
85 , process = const (fortune ["girl19"])
86 }
87 , (command "v")
88 { aliases = ["yhjulwwiefzojcbxybbruweejw"]
89 , help = getCmdName >>= \v -> case v of
90 "v" -> say "let v = show v in v"
91 _ -> say "V RETURNS!"
92 , process = const (fortune ["notoriousV"])
93 }
94 , (command "protontorpedo")
95 { help = say "protontorpedo is silly"
96 , process = const (fortune ["protontorpedo"])
97 }
98 , (command "nixon")
99 { help = say "Richard Nixon's finest."
100 , process = const (fortune ["nixon"])
101 }
102 , (command "farber")
103 { help = say "Farberisms in the style of David Farber."
104 , process = const (fortune ["farber"])
105 }
106 ]
107 }
108
109 fortune :: [FilePath] -> Cmd Quote ()
110 fortune xs = io (resolveFortuneFiles All xs >>= randomFortune) >>= say
111
112 ------------------------------------------------------------------------
113
114 -- the @remember command stores away a quotation by a user, for future
115 -- use by @quote
116
117 -- error handling!
118 runRemember :: String -> Cmd Quote ()
119 runRemember str
120 | null rest = say "Incorrect arguments to quote"
121 | otherwise = do
122 withMS $ \fm writer -> do
123 let ss = fromMaybe [] (M.lookup (P.pack nm) fm)
124 fm' = M.insert (P.pack nm) (P.pack q : ss) fm
125 writer fm'
126 say =<< randomSuccessMsg
127 where
128 (nm,rest) = break isSpace str
129 q = drop 1 rest
130
131 -- @forget, to remove a quote
132 runForget :: String -> Cmd Quote ()
133 runForget str
134 | null rest = say "Incorrect arguments to quote"
135 | otherwise = do
136 ss <- withMS $ \fm writer -> do
137 let ss = fromMaybe [] (M.lookup (P.pack nm) fm)
138 fm' = case delete (P.pack q) ss of
139 [] -> M.delete (P.pack nm) fm
140 ss' -> M.insert (P.pack nm) ss' fm
141 writer fm'
142 return ss
143 say $ if P.pack q `elem` ss
144 then "Done."
145 else "No match."
146 where
147 (nm,rest) = break isSpace str
148 q = drop 1 rest
149
150 --
151 -- the @quote command, takes a user nm to choose a random quote from
152 --
153 runQuote :: String -> Cmd Quote ()
154 runQuote str =
155 say =<< search (P.pack nm) (P.pack pat) =<< readMS
156 where (nm, p) = break isSpace str
157 pat = drop 1 p
158
159 search :: Key -> P.ByteString -> Quotes -> Cmd Quote String
160 search key pat db
161 | M.null db = return "No quotes yet."
162
163 | P.null key = do
164 (key', qs) <- random (M.toList db) -- quote a random person
165 fmap (display key') (random qs)
166
167 | P.null pat, Just qs <- mquotes =
168 fmap (display key) (random qs)
169
170 | P.null pat = match' key allquotes
171
172 | Just qs <- mquotes = match' pat (zip (repeat key) qs)
173
174 | otherwise = do
175 r <- randomFailureMsg
176 return $ "No quotes for this person. " ++ r
177
178 where
179 mquotes = M.lookup key db
180 allquotes = concat [ zip (repeat who) qs | (who, qs) <- M.assocs db ]
181
182 match' :: RegexMaker Regex CompOption ExecOption source => source -> [(P.ByteString, P.ByteString)] -> Cmd Quote String
183 match' p ss = do
184 re <- makeRegexOptsM defaultCompOpt {caseSensitive = False, newSyntax = True}
185 defaultExecOpt {captureGroups = False} p
186
187 let rs = filter (match re . snd) ss
188 if null rs
189 then do r <- randomFailureMsg
190 return $ "No quotes match. " ++ r
191 else do (who, saying) <- random rs
192 return $ P.unpack who ++ " says: " ++ P.unpack saying
193
194 display k msg = (if P.null k then " " else who ++ " says: ") ++ saying
195 where saying = P.unpack msg
196 who = P.unpack k