| 1 |
{-# LANGUAGE PatternGuards, FlexibleContexts #-} |
| 2 |
-- | Lambdabot base module. Controls message send and receive |
| 3 |
module Lambdabot.Plugin.Core.Base (basePlugin) where |
| 4 |
|
| 5 |
import Lambdabot.Bot |
| 6 |
import Lambdabot.Command |
| 7 |
import Lambdabot.Config.Core |
| 8 |
import Lambdabot.IRC |
| 9 |
import Lambdabot.Logging |
| 10 |
import Lambdabot.Message |
| 11 |
import Lambdabot.Module |
| 12 |
import Lambdabot.Monad |
| 13 |
import Lambdabot.Nick |
| 14 |
import Lambdabot.Plugin |
| 15 |
import Lambdabot.Util |
| 16 |
|
| 17 |
import Control.Applicative |
| 18 |
import Control.Exception.Lifted as E |
| 19 |
import Control.Monad |
| 20 |
import Control.Monad.Reader |
| 21 |
import Control.Monad.State |
| 22 |
import Data.Char |
| 23 |
import Data.List |
| 24 |
import Data.List.Split |
| 25 |
import qualified Data.Map as M |
| 26 |
import Text.EditDistance |
| 27 |
import Text.Regex.TDFA |
| 28 |
|
| 29 |
type BaseState = GlobalPrivate () () |
| 30 |
type Base = ModuleT BaseState LB |
| 31 |
|
| 32 |
basePlugin :: Module (GlobalPrivate () ()) |
| 33 |
basePlugin = newModule |
| 34 |
{ moduleDefState = return $ mkGlobalPrivate 20 () |
| 35 |
, moduleInit = do |
| 36 |
registerOutputFilter cleanOutput |
| 37 |
registerOutputFilter lineify |
| 38 |
registerOutputFilter cleanOutput |
| 39 |
|
| 40 |
registerCallback "PING" doPING |
| 41 |
registerCallback "NOTICE" doNOTICE |
| 42 |
registerCallback "PART" doPART |
| 43 |
registerCallback "KICK" doKICK |
| 44 |
registerCallback "JOIN" doJOIN |
| 45 |
registerCallback "NICK" doNICK |
| 46 |
registerCallback "MODE" doMODE |
| 47 |
registerCallback "TOPIC" doTOPIC |
| 48 |
registerCallback "QUIT" doQUIT |
| 49 |
registerCallback "PRIVMSG" doPRIVMSG |
| 50 |
registerCallback "001" doRPL_WELCOME |
| 51 |
|
| 52 |
-- registerCallback "002" doRPL_YOURHOST |
| 53 |
-- registerCallback "003" doRPL_CREATED |
| 54 |
-- registerCallback "004" doRPL_MYINFO |
| 55 |
|
| 56 |
registerCallback "005" doRPL_BOUNCE |
| 57 |
|
| 58 |
-- registerCallback "250" doRPL_STATSCONN |
| 59 |
-- registerCallback "251" doRPL_LUSERCLIENT |
| 60 |
-- registerCallback "252" doRPL_LUSEROP |
| 61 |
-- registerCallback "253" doRPL_LUSERUNKNOWN |
| 62 |
-- registerCallback "254" doRPL_LUSERCHANNELS |
| 63 |
-- registerCallback "255" doRPL_LUSERME |
| 64 |
-- registerCallback "265" doRPL_LOCALUSERS |
| 65 |
-- registerCallback "266" doRPL_GLOBALUSERS |
| 66 |
|
| 67 |
registerCallback "332" doRPL_TOPIC |
| 68 |
|
| 69 |
-- registerCallback "353" doRPL_NAMRELY |
| 70 |
-- registerCallback "366" doRPL_ENDOFNAMES |
| 71 |
-- registerCallback "372" doRPL_MOTD |
| 72 |
-- registerCallback "375" doRPL_MOTDSTART |
| 73 |
-- registerCallback "376" doRPL_ENDOFMOTD |
| 74 |
} |
| 75 |
|
| 76 |
doIGNORE :: IrcMessage -> Base () |
| 77 |
doIGNORE = debugM . show |
| 78 |
|
| 79 |
doPING :: IrcMessage -> Base () |
| 80 |
doPING = noticeM . showPingMsg |
| 81 |
where showPingMsg msg = "PING! <" ++ ircMsgServer msg ++ (':' : ircMsgPrefix msg) ++ |
| 82 |
"> [" ++ ircMsgCommand msg ++ "] " ++ show (ircMsgParams msg) |
| 83 |
|
| 84 |
-- If this is a "TIME" then we need to pass it over to the localtime plugin |
| 85 |
-- otherwise, dump it to stdout |
| 86 |
doNOTICE :: IrcMessage -> Base () |
| 87 |
doNOTICE msg |
| 88 |
| isCTCPTimeReply = doPRIVMSG (timeReply msg) |
| 89 |
-- TODO: need to say which module to run the privmsg in |
| 90 |
| otherwise = noticeM (show body) |
| 91 |
where |
| 92 |
body = ircMsgParams msg |
| 93 |
isCTCPTimeReply = ":\SOHTIME" `isPrefixOf` (last body) |
| 94 |
|
| 95 |
doJOIN :: IrcMessage -> Base () |
| 96 |
doJOIN msg |
| 97 |
| lambdabotName msg /= nick msg = doIGNORE msg |
| 98 |
| otherwise = do |
| 99 |
let msgArg = concat (take 1 (ircMsgParams msg)) |
| 100 |
chan = case dropWhile (/= ':') msgArg of |
| 101 |
[] -> msgArg |
| 102 |
aloc -> aloc |
| 103 |
loc = Nick (server msg) (dropWhile (== ':') chan) |
| 104 |
|
| 105 |
-- the empty topic causes problems |
| 106 |
-- TODO: find out what they are and fix them properly |
| 107 |
lb . modify $ \s -> s |
| 108 |
{ ircChannels = M.insert (mkCN loc) "[currently unknown]" (ircChannels s)} |
| 109 |
lb . send $ getTopic loc -- initialize topic |
| 110 |
where |
| 111 |
|
| 112 |
doPART :: IrcMessage -> Base () |
| 113 |
doPART msg |
| 114 |
= when (lambdabotName msg == nick msg) $ do |
| 115 |
let body = ircMsgParams msg |
| 116 |
loc = Nick (server msg) (head body) |
| 117 |
lb . modify $ \s -> s |
| 118 |
{ ircChannels = M.delete (mkCN loc) (ircChannels s) } |
| 119 |
|
| 120 |
doKICK :: IrcMessage -> Base () |
| 121 |
doKICK msg |
| 122 |
= do let body = ircMsgParams msg |
| 123 |
loc = Nick (server msg) (body !! 0) |
| 124 |
who = Nick (server msg) (body !! 1) |
| 125 |
when (lambdabotName msg == who) $ do |
| 126 |
noticeM $ fmtNick "" (nick msg) ++ " KICK " ++ fmtNick (server msg) loc ++ " " ++ show (drop 2 body) |
| 127 |
lift $ modify $ \s -> |
| 128 |
s { ircChannels = M.delete (mkCN loc) (ircChannels s) } |
| 129 |
|
| 130 |
doNICK :: IrcMessage -> Base () |
| 131 |
doNICK msg |
| 132 |
= doIGNORE msg |
| 133 |
|
| 134 |
doMODE :: IrcMessage -> Base () |
| 135 |
doMODE msg |
| 136 |
= doIGNORE msg |
| 137 |
|
| 138 |
|
| 139 |
doTOPIC :: IrcMessage -> Base () |
| 140 |
doTOPIC msg = lb . modify $ \s -> s |
| 141 |
{ ircChannels = M.insert (mkCN loc) (tail $ head $ tail $ ircMsgParams msg) (ircChannels s) } |
| 142 |
where loc = Nick (server msg) (head (ircMsgParams msg)) |
| 143 |
|
| 144 |
doRPL_WELCOME :: IrcMessage -> Base () |
| 145 |
doRPL_WELCOME msg = lb $ do |
| 146 |
modify $ \state' -> |
| 147 |
let persists = if M.findWithDefault True (server msg) (ircPersists state') |
| 148 |
then ircPersists state' |
| 149 |
else M.delete (server msg) $ ircPersists state' |
| 150 |
in state' { ircPersists = persists } |
| 151 |
chans <- gets ircChannels |
| 152 |
forM_ (M.keys chans) $ \chan -> do |
| 153 |
let cn = getCN chan |
| 154 |
when (nTag cn == server msg) $ do |
| 155 |
modify $ \state' -> state' { ircChannels = M.delete chan $ ircChannels state' } |
| 156 |
lb $ send $ joinChannel cn |
| 157 |
|
| 158 |
doQUIT :: IrcMessage -> Base () |
| 159 |
doQUIT msg = doIGNORE msg |
| 160 |
|
| 161 |
doRPL_BOUNCE :: IrcMessage -> Base () |
| 162 |
doRPL_BOUNCE _msg = debugM "BOUNCE!" |
| 163 |
|
| 164 |
doRPL_TOPIC :: IrcMessage -> Base () |
| 165 |
doRPL_TOPIC msg -- nearly the same as doTOPIC but has our nick on the front of body |
| 166 |
= do let body = ircMsgParams msg |
| 167 |
loc = Nick (server msg) (body !! 1) |
| 168 |
lb . modify $ \s -> s |
| 169 |
{ ircChannels = M.insert (mkCN loc) (tail $ last body) (ircChannels s) } |
| 170 |
|
| 171 |
doPRIVMSG :: IrcMessage -> Base () |
| 172 |
doPRIVMSG msg = do |
| 173 |
ignored <- lift $ checkIgnore msg |
| 174 |
commands <- getConfig commandPrefixes |
| 175 |
|
| 176 |
if ignored |
| 177 |
then doIGNORE msg |
| 178 |
else mapM_ (doPRIVMSG' commands (lambdabotName msg) msg) targets |
| 179 |
where |
| 180 |
alltargets = head (ircMsgParams msg) |
| 181 |
targets = map (parseNick (ircMsgServer msg)) $ splitOn "," alltargets |
| 182 |
|
| 183 |
-- |
| 184 |
-- | What does the bot respond to? |
| 185 |
-- |
| 186 |
doPRIVMSG' :: [String] -> Nick -> IrcMessage -> Nick -> Base () |
| 187 |
doPRIVMSG' commands myname msg target |
| 188 |
| myname == target |
| 189 |
= let (cmd, params) = splitFirstWord text |
| 190 |
in doPersonalMsg commands msg target text cmd params |
| 191 |
|
| 192 |
| flip any ":," $ \c -> (fmtNick (ircMsgServer msg) myname ++ [c]) `isPrefixOf` text |
| 193 |
= let Just wholeCmd = maybeCommand (fmtNick (ircMsgServer msg) myname) text |
| 194 |
(cmd, params) = splitFirstWord wholeCmd |
| 195 |
in doPublicMsg commands msg target cmd params |
| 196 |
|
| 197 |
| (commands `arePrefixesOf` text) |
| 198 |
&& length text > 1 |
| 199 |
&& (text !! 1 /= ' ') -- elem of prefixes |
| 200 |
&& (not (commands `arePrefixesOf` [text !! 1]) || |
| 201 |
(length text > 2 && text !! 2 == ' ')) -- ignore @@ prefix, but not the @@ command itself |
| 202 |
= let (cmd, params) = splitFirstWord (dropWhile (==' ') text) |
| 203 |
in doPublicMsg commands msg target cmd params |
| 204 |
|
| 205 |
| otherwise = doContextualMsg msg target target text |
| 206 |
|
| 207 |
where |
| 208 |
text = tail (head (tail (ircMsgParams msg))) |
| 209 |
|
| 210 |
doPersonalMsg :: [String] -> IrcMessage -> Nick -> String -> String -> String -> Base () |
| 211 |
doPersonalMsg commands msg target text s r |
| 212 |
| commands `arePrefixesOf` s = doMsg msg (tail s) r who |
| 213 |
| otherwise = doContextualMsg msg target who text |
| 214 |
where |
| 215 |
who = nick msg |
| 216 |
|
| 217 |
doPublicMsg :: [String] -> IrcMessage -> Nick -> String -> String -> Base () |
| 218 |
doPublicMsg commands msg target s r |
| 219 |
| commands `arePrefixesOf` s = doMsg msg (tail s) r target |
| 220 |
| otherwise = doIGNORE msg |
| 221 |
|
| 222 |
-- |
| 223 |
-- normal commands. |
| 224 |
-- |
| 225 |
-- check privledges, do any spell correction, dispatch, handling |
| 226 |
-- possible timeouts. |
| 227 |
-- |
| 228 |
-- todo, refactor |
| 229 |
-- |
| 230 |
doMsg :: IrcMessage -> String -> String -> Nick -> Base () |
| 231 |
doMsg msg cmd rest towhere = do |
| 232 |
let ircmsg = ircPrivmsg towhere |
| 233 |
allcmds <- lift (gets (M.keys . ircCommands)) |
| 234 |
let ms = filter (isPrefixOf cmd) allcmds |
| 235 |
e <- getConfig editDistanceLimit |
| 236 |
case ms of |
| 237 |
[s] -> docmd msg towhere rest s -- a unique prefix |
| 238 |
_ | cmd `elem` ms -> docmd msg towhere rest cmd -- correct command (usual case) |
| 239 |
_ | otherwise -> case closests cmd allcmds of |
| 240 |
(n,[s]) | n < e , ms == [] -> docmd msg towhere rest s -- unique edit match |
| 241 |
(n,ss) | n < e || ms /= [] -- some possibilities |
| 242 |
-> lift . ircmsg $ "Maybe you meant: "++showClean(nub(ms++ss)) |
| 243 |
_ -> docmd msg towhere rest cmd -- no prefix, edit distance too far |
| 244 |
|
| 245 |
docmd :: IrcMessage -> Nick -> [Char] -> String -> Base () |
| 246 |
docmd msg towhere rest cmd' = withPS towhere $ \_ _ -> do |
| 247 |
withCommand cmd' -- Important. |
| 248 |
(ircPrivmsg towhere "Unknown command, try @list") |
| 249 |
(\theCmd -> do |
| 250 |
name' <- asks moduleName |
| 251 |
|
| 252 |
hasPrivs <- lb (checkPrivs msg) |
| 253 |
|
| 254 |
-- TODO: handle disabled commands earlier |
| 255 |
-- users should probably see no difference between a |
| 256 |
-- command that is disabled and one that doesn't exist. |
| 257 |
disabled <- elem cmd' <$> getConfig disabledCommands |
| 258 |
let ok = not disabled && (not (privileged theCmd) || hasPrivs) |
| 259 |
|
| 260 |
response <- if not ok |
| 261 |
then return ["Not enough privileges"] |
| 262 |
else runCommand theCmd msg towhere cmd' rest |
| 263 |
`E.catch` \exc@SomeException{} -> |
| 264 |
return ["Plugin `" ++ name' ++ "' failed with: " ++ show exc] |
| 265 |
|
| 266 |
-- send off our response strings |
| 267 |
-- TODO: expandTab here should probably be an OutputFilter |
| 268 |
lift $ mapM_ (ircPrivmsg towhere . expandTab 8) response |
| 269 |
) |
| 270 |
|
| 271 |
-- |
| 272 |
-- contextual messages are all input that isn't an explicit command. |
| 273 |
-- they're passed to all modules (todo, sounds inefficient) for |
| 274 |
-- scanning, and any that implement 'contextual' will reply. |
| 275 |
-- |
| 276 |
-- we try to run the contextual functions from all modules, on every |
| 277 |
-- non-command. better hope this is efficient. |
| 278 |
-- |
| 279 |
-- Note how we catch any plugin errors here, rather than letting |
| 280 |
-- them bubble back up to the mainloop |
| 281 |
-- |
| 282 |
doContextualMsg :: IrcMessage -> Nick -> Nick -> [Char] -> Base () |
| 283 |
doContextualMsg msg target towhere r = lb (withAllModules (withHandler invokeContextual)) |
| 284 |
where |
| 285 |
withHandler x = E.catch x $ \e@SomeException{} -> do |
| 286 |
mName <- asks moduleName |
| 287 |
debugM ("Module " ++ show mName ++ " failed in contextual handler: " ++ show e) |
| 288 |
|
| 289 |
invokeContextual = do |
| 290 |
m <- asks theModule |
| 291 |
reply <- execCmd (contextual m r) msg target "contextual" |
| 292 |
lb $ mapM_ (ircPrivmsg towhere) reply |
| 293 |
|
| 294 |
------------------------------------------------------------------------ |
| 295 |
|
| 296 |
closests :: String -> [String] -> (Int,[String]) |
| 297 |
closests pat ss = M.findMin m |
| 298 |
where |
| 299 |
m = M.fromListWith (++) ls |
| 300 |
ls = [ (levenshteinDistance defaultEditCosts pat s, [s]) | s <- ss ] |
| 301 |
|
| 302 |
maybeCommand :: String -> String -> Maybe String |
| 303 |
maybeCommand nm text = mrAfter <$> matchM re text |
| 304 |
where |
| 305 |
re :: Regex |
| 306 |
re = makeRegex (nm ++ "[.:,]*[[:space:]]*") |
| 307 |
|
| 308 |
-- |
| 309 |
-- And stuff we don't care about |
| 310 |
-- |
| 311 |
|
| 312 |
{- |
| 313 |
doRPL_YOURHOST :: IrcMessage -> LB () |
| 314 |
doRPL_YOURHOST _msg = return () |
| 315 |
|
| 316 |
doRPL_CREATED :: IrcMessage -> LB () |
| 317 |
doRPL_CREATED _msg = return () |
| 318 |
|
| 319 |
doRPL_MYINFO :: IrcMessage -> LB () |
| 320 |
doRPL_MYINFO _msg = return () |
| 321 |
|
| 322 |
doRPL_STATSCONN :: IrcMessage -> LB () |
| 323 |
doRPL_STATSCONN _msg = return () |
| 324 |
|
| 325 |
doRPL_LUSERCLIENT :: IrcMessage -> LB () |
| 326 |
doRPL_LUSERCLIENT _msg = return () |
| 327 |
|
| 328 |
doRPL_LUSEROP :: IrcMessage -> LB () |
| 329 |
doRPL_LUSEROP _msg = return () |
| 330 |
|
| 331 |
doRPL_LUSERUNKNOWN :: IrcMessage -> LB () |
| 332 |
doRPL_LUSERUNKNOWN _msg = return () |
| 333 |
|
| 334 |
doRPL_LUSERCHANNELS :: IrcMessage -> LB () |
| 335 |
doRPL_LUSERCHANNELS _msg = return () |
| 336 |
|
| 337 |
doRPL_LUSERME :: IrcMessage -> LB () |
| 338 |
doRPL_LUSERME _msg = return () |
| 339 |
|
| 340 |
doRPL_LOCALUSERS :: IrcMessage -> LB () |
| 341 |
doRPL_LOCALUSERS _msg = return () |
| 342 |
|
| 343 |
doRPL_GLOBALUSERS :: IrcMessage -> LB () |
| 344 |
doRPL_GLOBALUSERS _msg = return () |
| 345 |
|
| 346 |
doUNKNOWN :: IrcMessage -> Base () |
| 347 |
doUNKNOWN msg |
| 348 |
= debugM $ "UNKNOWN> <" ++ msgPrefix msg ++ |
| 349 |
"> [" ++ msgCommand msg ++ "] " ++ show (body msg) |
| 350 |
|
| 351 |
doRPL_NAMREPLY :: IrcMessage -> LB () |
| 352 |
doRPL_NAMREPLY _msg = return () |
| 353 |
|
| 354 |
doRPL_ENDOFNAMES :: IrcMessage -> LB () |
| 355 |
doRPL_ENDOFNAMES _msg = return () |
| 356 |
|
| 357 |
doRPL_MOTD :: IrcMessage -> LB () |
| 358 |
doRPL_MOTD _msg = return () |
| 359 |
|
| 360 |
doRPL_MOTDSTART :: IrcMessage -> LB () |
| 361 |
doRPL_MOTDSTART _msg = return () |
| 362 |
|
| 363 |
doRPL_ENDOFMOTD :: IrcMessage -> LB () |
| 364 |
doRPL_ENDOFMOTD _msg = return () |
| 365 |
-} |
| 366 |
|
| 367 |
-- Initial output filters |
| 368 |
|
| 369 |
-- | For now, this just checks for duplicate empty lines. |
| 370 |
cleanOutput :: Monad m => a -> [String] -> m [String] |
| 371 |
cleanOutput _ msg = return $ remDups True msg' |
| 372 |
where |
| 373 |
remDups True ([]:xs) = remDups True xs |
| 374 |
remDups False ([]:xs) = []:remDups True xs |
| 375 |
remDups _ (x: xs) = x: remDups False xs |
| 376 |
remDups _ [] = [] |
| 377 |
msg' = map (dropFromEnd isSpace) msg |
| 378 |
|
| 379 |
-- | wrap long lines. |
| 380 |
lineify :: MonadConfig m => a -> [String] -> m [String] |
| 381 |
lineify _ msg = do |
| 382 |
w <- getConfig textWidth |
| 383 |
return (lines (unlines msg) >>= mbreak w) |
| 384 |
where |
| 385 |
-- | break into lines |
| 386 |
mbreak w xs |
| 387 |
| null bs = [as] |
| 388 |
| otherwise = (as++cs) : filter (not . null) (mbreak w ds) |
| 389 |
where |
| 390 |
(as,bs) = splitAt (w-n) xs |
| 391 |
breaks = filter (not . isAlphaNum . last . fst) $ drop 1 $ |
| 392 |
take n $ zip (inits bs) (tails bs) |
| 393 |
(cs,ds) = last $ (take n bs, drop n bs): breaks |
| 394 |
n = 10 |