{-# Language OverloadedStrings #-}
module Irc.UserInfo
(
UserInfo(..)
, renderUserInfo
, parseUserInfo
, uiNick
, uiName
, uiHost
) where
import Data.Text (Text)
import qualified Data.Text as Text
import Irc.Identifier
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
uiName :: Functor f => (Text -> f Text) -> UserInfo -> f UserInfo
uiName f ui@UserInfo{userName = n} = (\n' -> ui{userName = n'}) <$> f n
uiHost :: Functor f => (Text -> f Text) -> UserInfo -> f UserInfo
uiHost f ui@UserInfo{userHost = n} = (\n' -> ui{userHost = 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