module Lambdabot.Compat.PackedNick
    ( PackedNick
    , packNick
    , unpackNick
    ) where

import Lambdabot.Nick
import qualified Data.ByteString.Char8 as BS

-- | The type of nicknames
type PackedNick = BS.ByteString

-- Helper functions
upckStr :: String -> String -> Nick
upckStr :: String -> String -> Nick
upckStr String
def String
str | String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
ac   = String -> String -> Nick
Nick String
def String
str
                | Bool
otherwise = String -> String -> Nick
Nick String
bc (String -> String
forall a. [a] -> [a]
tail String
ac)
    where (String
bc, String
ac) = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
':') String
str

pckStr :: Nick -> String
pckStr :: Nick -> String
pckStr Nick
nck = Nick -> String
nTag Nick
nck String -> String -> String
forall a. [a] -> [a] -> [a]
++ Char
':' Char -> String -> String
forall a. a -> [a] -> [a]
: Nick -> String
nName Nick
nck

-- | Pack a nickname into a ByteString.  Note that the resulting strings are
-- not optimally formatted for human consumtion.
packNick :: Nick -> BS.ByteString
packNick :: Nick -> ByteString
packNick = String -> ByteString
BS.pack (String -> ByteString) -> (Nick -> String) -> Nick -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Nick -> String
pckStr

-- | Unpack a nickname packed by 'packNick'.
unpackNick :: BS.ByteString -> Nick
unpackNick :: ByteString -> Nick
unpackNick = String -> String -> Nick
upckStr String
"freenode" (String -> Nick) -> (ByteString -> String) -> ByteString -> Nick
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> String
BS.unpack