+cons :: a [a] -> [a]
+cons a as = [a:as]
+
+pMiddle :: Parser Char String
+pMiddle = fmap toString $
+ spaceParser >>| liftM2 cons (pNotSatisfy ((==)':')) (pMany $ pNoneOf [' ':illegal])
+
+pTrailing :: Parser Char String
+pTrailing = fmap toString $
+ spaceParser >>| pToken ':' >>| pMany (pNoneOf illegal)
+
+pParam :: Parser Char String
+pParam = pMiddle <|> pTrailing
+
+pNoneOf :: [a] -> Parser a a | Eq a
+pNoneOf l = pSatisfy (not o flip isMember l)
+
+pNotSatisfy :: (a -> Bool) -> Parser a a | Eq a
+pNotSatisfy f = pSatisfy (not o f)
+
+pInt :: Parser Char Int
+pInt = toInt o toString <$> (spaceParser >>| pSome pDigit)
+
+illegal :: [Char]
+illegal = ['\x00','\r','\n']
+
+pCommand :: String -> Parser Char [Char]
+pCommand s = pList (map pToken $ fromString s)
+
+pCommand0 :: String IRCCommand -> Parser Char IRCCommand
+pCommand0 s c = pCommand s >>| pure c
+
+pCommand1 :: String (Parser Char a) (a -> IRCCommand) -> Parser Char IRCCommand
+pCommand1 s p c = pCommand s >>| liftM c p
+
+pCommand2 :: String (Parser Char a) (Parser Char b) (a b -> IRCCommand) -> Parser Char IRCCommand
+pCommand2 s p q c = pCommand s >>| liftM2 c p q
+
+pCommand3 :: String (Parser Char a) (Parser Char b) (Parser Char c) (a b c -> IRCCommand) -> Parser Char IRCCommand
+pCommand3 s p q r c = pCommand s >>| liftM3 c p q r
+
+pCommand4 :: String (Parser Char a) (Parser Char b) (Parser Char c) (Parser Char d) (a b c d -> IRCCommand) -> Parser Char IRCCommand
+pCommand4 s p q r t c = pCommand s >>| liftM4 c p q r t
+
+pCommand5 :: String (Parser Char a) (Parser Char b) (Parser Char c) (Parser Char d) (Parser Char e) (a b c d e -> IRCCommand) -> Parser Char IRCCommand
+pCommand5 s p q r t u c = pCommand s >>| liftM5 c p q r t u
+
+pMode :: Parser Char String
+pMode = toString <$> pSome (pOneOf ['+','-','o','p','i','t','n','b','v','w','s'])
+
+parseCommand :: Parser Char IRCCommand
+parseCommand =
+ pCommand1 "ADMIN" (optional pMiddle) ADMIN
+ <|> pCommand1 "AWAY" pParam AWAY
+ <|> pCommand2 "CONNECT" pParam (optional $ liftM2 tuple pInt (optional pParam)) CONNECT
+ <|> pCommand0 "DIE" DIE
+ <|> pCommand1 "ERROR" pParam ERROR
+ <|> pCommand1 "INFO" (optional pParam) INFO
+ <|> pCommand2 "INVITE" pMiddle pMiddle INVITE
+ <|> pCommand1 "ISON" (pSome pMiddle) ISON
+ <|> pCommand1 "JOIN" (pSepBy (liftM2 tuple pMiddle $ optional pMiddle) pComma) JOIN
+ <|> pCommand3 "KICK" pMiddle pMiddle (optional pParam) KICK
+ <|> pCommand2 "KILL" pMiddle pParam KILL
+ <|> pCommand1 "LINKS" (optional $ liftM2 tuple (optional pMiddle) pMiddle) LINKS
+ <|> pCommand1 "LIST" (optional $ liftM2 tuple (pSepBy pMiddle pComma) $ optional pMiddle) LIST
+ <|> pCommand1 "LUSERS" (optional $ liftM2 tuple pMiddle $ optional pMiddle) LUSERS
+ <|> pCommand5 "MODE" pMiddle pMode (optional pMiddle) (optional pMiddle) (optional pMiddle) MODE
+ <|> pCommand1 "MOTD" (optional pMiddle) MOTD
+ <|> pCommand1 "NAMES" (pSepBy pMiddle pComma) NAMES
+ //NJOIN
+ <|> pCommand2 "NOTICE" pParam pParam NOTICE
+ //OPER String String
+ //PART [String]
+ //PASS String
+ <|> pCommand2 "PING" pMiddle (optional pMiddle) PING
+ <|> pCommand2 "PONG" pMiddle (optional pMiddle) PONG
+ <|> pCommand2 "PRIVMSG" (pSepBy pMiddle pComma) pParam PRIVMSG
+ <|> pCommand1 "QUIT" (optional pParam) QUIT
+ //REHASH
+ //RESTART
+ //SERVER
+ //SERVICE String String String String
+ //SERVLIST (Maybe (String, Maybe String))
+ //SQUERY String String
+ //SQUIRT
+ //SQUIT String String
+ //STATS (Maybe (String, Maybe String))
+ //SUMMON String (Maybe (String, Maybe String))
+ //TIME (Maybe String)
+ //TOPIC String (Maybe String)
+ //TRACE (Maybe String)
+ <|> pCommand3 "USER" pMiddle pMiddle (pMiddle >>| pParam) USER
+ //USERHOST [String]
+ //USERS (Maybe String)
+ //VERSION (Maybe String)
+ //WALLOPS String
+ //WHO (Maybe String)
+ //WHOIS (Maybe String) [String]
+ //WHOWAS (Maybe String) [String]
+
+instance toString IRCCommand where
+ toString r = jon " " (print r) +++ "\r\n"
+
+print :: IRCCommand -> [String]
+print r = case r of
+ ADMIN mm = ["ADMIN":maybeToList mm]