85 lines
2.5 KiB
Haskell
85 lines
2.5 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
module Krobotkin.Handlers where
|
|
|
|
import Krobotkin.Monad
|
|
import Krobotkin.Muc
|
|
import Krobotkin.Types
|
|
|
|
import Data.Maybe
|
|
import System.Random
|
|
|
|
import Network.Xmpp
|
|
|
|
import Control.Monad.Reader
|
|
import Control.Monad.State
|
|
|
|
import qualified Data.Text as T
|
|
|
|
dispatchStanza :: PackedStanza -> BotMonad ()
|
|
dispatchStanza (BotPresence fromUser ptype o) =
|
|
handlePresence fromUser ptype o
|
|
dispatchStanza (BotMessage toUser fromUser content o) =
|
|
handleMessage toUser fromUser content o
|
|
dispatchStanza _ = liftIO $ return ()
|
|
|
|
handlePresence :: Jid -> PresenceType -> Presence -> BotMonad ()
|
|
handlePresence fromUser Subscribe _ = approveSubscription fromUser
|
|
handlePresence _ _ _ = liftIO $ return ()
|
|
|
|
approveSubscription :: Jid -> BotMonad ()
|
|
approveSubscription user = do
|
|
sess <- asks envXmppSess
|
|
_ <- liftIO $ sendPresence (presenceSubscribed user) sess
|
|
liftIO $ putStrLn $ "subscribed to " ++ (show user)
|
|
|
|
handleMessage :: Jid -> Jid -> String -> Message -> BotMonad ()
|
|
handleMessage _ fromUser "PING" _ =
|
|
sendMucMessage
|
|
"pong"
|
|
(toBare fromUser)
|
|
handleMessage _ fromUser "BING" _ =
|
|
sendMucMessage
|
|
"cannabis smoking device"
|
|
(toBare fromUser)
|
|
handleMessage _ fromUser ('.':'8':'B':'A':'L':'L':' ':_) o = do
|
|
rng <- gets stateRNG
|
|
let (x,rng') = randomR (0,(length responses)-1) rng
|
|
modify (\s -> s { stateRNG = rng' })
|
|
sendMucMessage
|
|
(T.pack ("> "
|
|
++ (drop 7 (T.unpack (fromMaybe ";0;" (getMucMessageBody o))))
|
|
++ "\n" ++ responses !! x))
|
|
(toBare fromUser)
|
|
where
|
|
responses =
|
|
[ "it is certain"
|
|
, "without a doubt"
|
|
, "you may rely on it"
|
|
, "yes definitely"
|
|
, "it is decidedly so"
|
|
, "as i see it, yes"
|
|
, "most likely"
|
|
, "yes"
|
|
, "outlook good"
|
|
, "signs point to yes"
|
|
, "reply hazy try again"
|
|
, "better not tell you now"
|
|
, "ask again later"
|
|
, "cannot predict now"
|
|
, "concentrate and ask again"
|
|
, "dont count on it"
|
|
, "outlook not so good"
|
|
, "my sources say no"
|
|
, "very doubtful"
|
|
, "my reply is no"
|
|
]
|
|
handleMessage _ fromUser _ _ | T.toUpper (fromMaybe "no" (resourcepart fromUser)) == "ELFIE" = do
|
|
rng <- gets stateRNG
|
|
let (x,rng') = randomR (0,21) rng :: (Int,StdGen)
|
|
modify (\s -> s { stateRNG = rng' })
|
|
if x == 13
|
|
then sendMucMessage "shut up smellfie" (toBare fromUser)
|
|
else liftIO $ return ()
|
|
handleMessage _ _ _ _ = liftIO $ return ()
|