{-# LANGUAGE OverloadedStrings #-}
module Network.IRC.Bot.Part.Hello where

import Control.Monad            (void)
import Control.Monad.Trans      (liftIO)
import Data.Maybe               (fromMaybe)
import Data.ByteString          (ByteString)
import Data.Monoid              ((<>))
import Network.IRC.Bot.Log      (LogLevel(Debug))
import Network.IRC.Bot.BotMonad (BotMonad(..), maybeZero)
import Network.IRC.Bot.Commands (PrivMsg(..),askSenderNickName, replyTo, sendCommand)
import Network.IRC.Bot.Parsec   (botPrefix, parsecPart)
import System.Random            (randomRIO)
import Text.Parsec              (ParsecT, (<|>), string, try)

helloPart :: (BotMonad m) => m ()
helloPart :: m ()
helloPart = ParsecT ByteString () m () -> m ()
forall (m :: * -> *) a.
BotMonad m =>
ParsecT ByteString () m a -> m a
parsecPart ParsecT ByteString () m ()
forall (m :: * -> *). BotMonad m => ParsecT ByteString () m ()
helloCommand

helloCommand :: (BotMonad m) => ParsecT ByteString () m ()
helloCommand :: ParsecT ByteString () m ()
helloCommand =
    do ParsecT ByteString () m String -> ParsecT ByteString () m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT ByteString () m String -> ParsecT ByteString () m ())
-> ParsecT ByteString () m String -> ParsecT ByteString () m ()
forall a b. (a -> b) -> a -> b
$ ParsecT ByteString () m String -> ParsecT ByteString () m String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT ByteString () m String -> ParsecT ByteString () m String)
-> ParsecT ByteString () m String -> ParsecT ByteString () m String
forall a b. (a -> b) -> a -> b
$ ParsecT ByteString () m ()
forall (m :: * -> *). BotMonad m => ParsecT ByteString () m ()
botPrefix ParsecT ByteString () m ()
-> ParsecT ByteString () m String -> ParsecT ByteString () m String
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> String -> ParsecT ByteString () m String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"hello"
       LogLevel -> ByteString -> ParsecT ByteString () m ()
forall (m :: * -> *). BotMonad m => LogLevel -> ByteString -> m ()
logM LogLevel
Debug ByteString
"helloPart"
       ByteString
target <- Maybe ByteString -> ParsecT ByteString () m ByteString
forall (m :: * -> *) a. MonadPlus m => Maybe a -> m a
maybeZero (Maybe ByteString -> ParsecT ByteString () m ByteString)
-> ParsecT ByteString () m (Maybe ByteString)
-> ParsecT ByteString () m ByteString
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ParsecT ByteString () m (Maybe ByteString)
forall (m :: * -> *). BotMonad m => m (Maybe ByteString)
replyTo
       LogLevel -> ByteString -> ParsecT ByteString () m ()
forall (m :: * -> *). BotMonad m => LogLevel -> ByteString -> m ()
logM LogLevel
Debug (ByteString -> ParsecT ByteString () m ())
-> ByteString -> ParsecT ByteString () m ()
forall a b. (a -> b) -> a -> b
$ ByteString
"target: " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
target
       Maybe ByteString
mNick <- ParsecT ByteString () m (Maybe ByteString)
forall (m :: * -> *). BotMonad m => m (Maybe ByteString)
askSenderNickName
       let greetings :: [ByteString]
greetings = [ByteString
"Hello", ByteString
"Howdy", ByteString
"Greetings", ByteString
"Word up"]
       Int
n <- IO Int -> ParsecT ByteString () m Int
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Int -> ParsecT ByteString () m Int)
-> IO Int -> ParsecT ByteString () m Int
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> IO Int
forall a (m :: * -> *). (Random a, MonadIO m) => (a, a) -> m a
randomRIO (Int
0, [ByteString] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ByteString]
greetings Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
       let message :: ByteString
message = [ByteString]
greetings[ByteString] -> Int -> ByteString
forall a. [a] -> Int -> a
!!Int
n ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
", " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> (ByteString -> Maybe ByteString -> ByteString
forall a. a -> Maybe a -> a
fromMaybe ByteString
"stranger" Maybe ByteString
mNick)
       PrivMsg -> ParsecT ByteString () m ()
forall c (m :: * -> *).
(ToMessage c, BotMonad m, Functor m) =>
c -> m ()
sendCommand (Maybe Prefix -> [ByteString] -> ByteString -> PrivMsg
PrivMsg Maybe Prefix
forall a. Maybe a
Nothing [ByteString
target] ByteString
message)
    ParsecT ByteString () m ()
-> ParsecT ByteString () m () -> ParsecT ByteString () m ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> () -> ParsecT ByteString () m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()