{-# Language OverloadedStrings #-}
module Irc.UserInfo
( UserInfo(..)
, renderUserInfo
, parseUserInfo
, uiNick
) where
import Data.Text (Text)
import qualified Data.Text as Text
import Irc.Identifier
import Data.Monoid ((<>))
data UserInfo = UserInfo
{ userNick :: {-# UNPACK #-} !Identifier
, userName :: {-# UNPACK #-} !Text
, userHost :: {-# UNPACK #-} !Text
}
deriving (Eq, Read, Show)
uiNick :: Functor f => (Identifier -> f Identifier) -> UserInfo -> f UserInfo
uiNick f ui@UserInfo{userNick = n} = (\n' -> ui{userNick = n'}) <$> f n
renderUserInfo :: UserInfo -> Text
renderUserInfo (UserInfo a b c)
= idText a
<> (if Text.null b then "" else "!" <> b)
<> (if Text.null c then "" else "@" <> c)
parseUserInfo :: Text -> UserInfo
parseUserInfo x = UserInfo
{ userNick = mkId nick
, userName = Text.drop 1 user
, userHost = Text.drop 1 host
}
where
(nickuser,host) = Text.break (=='@') x
(nick,user) = Text.break (=='!') nickuser