module Network.IRC.Bot.Part.NickUser where

import Control.Monad.Trans        (liftIO)
import Data.ByteString            (ByteString)
import Data.ByteString.Char8      (pack)
import Data.Monoid                ((<>))
import Network.IRC.Bot.BotMonad   (BotMonad(..) )
import Network.IRC.Bot.Types      (User(..))
import Network.IRC.Bot.ErrorCodes
import Network.IRC.Bot.Log        (LogLevel(..))
import           Network.IRC      (Message(..), showMessage)
import qualified Network.IRC      as IRC
import System.Random              (randomRIO)

nickUserPart :: (BotMonad m) => m ()
nickUserPart :: m ()
nickUserPart =
  do Message
msg <- m Message
forall (m :: * -> *). BotMonad m => m Message
askMessage
     let cmd :: Command
cmd = Message -> Command
msg_command Message
msg
     case () of
       () | Command
cmd Command -> Command -> Bool
forall a. Eq a => a -> a -> Bool
== Command
noNicknameGiven  -> LogLevel -> Command -> m ()
forall (m :: * -> *). BotMonad m => LogLevel -> Command -> m ()
logM LogLevel
Important (Message -> Command
showMessage Message
msg)
          | Command
cmd Command -> Command -> Bool
forall a. Eq a => a -> a -> Bool
== Command
erroneusNickname -> LogLevel -> Command -> m ()
forall (m :: * -> *). BotMonad m => LogLevel -> Command -> m ()
logM LogLevel
Important (Message -> Command
showMessage Message
msg)
          | Command
cmd Command -> Command -> Bool
forall a. Eq a => a -> a -> Bool
== Command
nickCollision    -> LogLevel -> Command -> m ()
forall (m :: * -> *). BotMonad m => LogLevel -> Command -> m ()
logM LogLevel
Important (Message -> Command
showMessage Message
msg)
          | Command
cmd Command -> Command -> Bool
forall a. Eq a => a -> a -> Bool
== Command
nicknameInUse    ->
              do LogLevel -> Command -> m ()
forall (m :: * -> *). BotMonad m => LogLevel -> Command -> m ()
logM LogLevel
Important (Message -> Command
showMessage Message
msg)
                 Command
n <- m Command
forall (m :: * -> *). BotMonad m => m Command
whoami
                 Int
i <- IO Int -> m Int
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Int -> m Int) -> IO Int -> 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
1, Int
100 :: Int)
                 Command -> Maybe User -> m ()
forall (m :: * -> *). BotMonad m => Command -> Maybe User -> m ()
changeNickUser (Command
n Command -> Command -> Command
forall a. Semigroup a => a -> a -> a
<> String -> Command
pack (Int -> String
forall a. Show a => a -> String
show Int
i)) Maybe User
forall a. Maybe a
Nothing
          | Bool
otherwise -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

changeNickUser :: (BotMonad m) => ByteString -> Maybe User -> m ()
changeNickUser :: Command -> Maybe User -> m ()
changeNickUser Command
n Maybe User
mUser =
    do Message -> m ()
forall (m :: * -> *). BotMonad m => Message -> m ()
sendMessage (Command -> Message
IRC.nick Command
n)
       case Maybe User
mUser of
         Maybe User
Nothing -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
         (Just User
u) ->
             Message -> m ()
forall (m :: * -> *). BotMonad m => Message -> m ()
sendMessage (Command -> Command -> Command -> Command -> Message
IRC.user (User -> Command
username User
u) (String -> Command
pack (String -> Command) -> String -> Command
forall a b. (a -> b) -> a -> b
$ User -> String
hostname User
u) (String -> Command
pack (String -> Command) -> String -> Command
forall a b. (a -> b) -> a -> b
$ User -> String
servername User
u) (User -> Command
realname User
u))