module Lambdabot.Util.NickEq
( Polynick
, nickMatches
, readPolynick
, showPolynick
, lookupMononickMap
, mononickToPolynick
) where
import Lambdabot.Message
import Lambdabot.Monad
import Lambdabot.Nick
import Data.List (intercalate)
import Data.List.Split (splitOn)
import qualified Data.Map as M
import Data.Maybe (mapMaybe)
data Polynick = Polynick [Nick] deriving (Eq)
nickMatches :: LB (Nick -> Polynick -> Bool)
nickMatches = return m'
where
m' nck (Polynick nck2) = nck `elem` nck2
readPolynick :: Message a => a -> String -> Polynick
readPolynick m = Polynick . map (parseNick (server m)) . splitOn "|"
showPolynick :: Message a => a -> Polynick -> String
showPolynick m (Polynick n) = intercalate "|" $ map (fmtNick (server m)) n
mononickToPolynick :: Nick -> Polynick
mononickToPolynick = Polynick . (:[])
lookupMononickMap :: LB (Polynick -> M.Map Nick a -> [(Nick,a)])
lookupMononickMap = return $ look'
where look' (Polynick ns) m = mapMaybe (\n -> (,) n `fmap` M.lookup n m) ns