doNameReply :: Identifier -> [ByteString] -> IrcConnection -> Logic IrcConnection
doNameReply chan xs conn =
  do msg <- getMessage
     case msg of
       RplNameReply _ _ x -> doNameReply chan (x++xs) conn
       RplEndOfNames _ -> return
                     $ learnAllHosts
                     $ set (connChannels . ix chan . chanUsers)
                           users
                           conn
       _ -> fail "Expected end of names"
       where
       modeMap = view (connChanModeTypes . modesPrefixModes) conn

       splitNames :: [(UserInfo, String)]
       splitNames = map (splitNamesReplyName modeMap) xs

       users :: Map Identifier String
       users = Map.fromList (map (over _1 userNick) splitNames)

       learnAllHosts x = foldl' (flip learnUserInfo) x (map fst splitNames)

-- | Compute the nickname and channel modes from an entry in
-- a NAMES reply. The leading channel prefixes are translated
-- into the appropriate modes.
doNameReply :: Identifier -> [ByteString] -> IrcConnection -> Logic IrcConnection
doNameReply chan xs conn =
  do msg <- getMessage
     case msg of
       RplNameReply _ _ x -> doNameReply chan (x++xs) conn
       RplEndOfNames _ -> return
                     $ learnAllHosts
                     $ set (connChannels . ix chan . chanUsers)
                           users
                           conn
       _ -> fail "Expected end of names"
       where
       modeMap = view (connChanModeTypes . modesPrefixModes) conn

       splitNames :: [(UserInfo, String)]
       splitNames = map (splitNamesReplyName modeMap) xs

       users :: Map Identifier String
       users = Map.fromList (map (over _1 userNick) splitNames)

       learnAllHosts x = foldl' (flip learnUserInfo) x (map fst splitNames)

-- | Compute the nickname and channel modes from an entry in
-- a NAMES reply. The leading channel prefixes are translated
-- into the appropriate modes.
doNameReply chan xs conn =
  do msg <- getMessage
     case msg of
       RplNameReply _ _ x -> doNameReply chan (x++xs) conn
       RplEndOfNames _ -> return
                     $ learnAllHosts
                     $ set (connChannels . ix chan . chanUsers)
                           users
                           conn
       _ -> fail "Expected end of names"
       where
       modeMap = view (connChanModeTypes . modesPrefixModes) conn

       splitNames :: [(UserInfo, String)]
       splitNames = map (splitNamesReplyName modeMap) xs

       users :: Map Identifier String
       users = Map.fromList (map (over _1 userNick) splitNames)

       learnAllHosts x = foldl' (flip learnUserInfo) x (map fst splitNames)

-- | Compute the nickname and channel modes from an entry in
-- a NAMES reply. The leading channel prefixes are translated
-- into the appropriate modes.