X-Git-Url: https://git.martlubbers.net/?p=cloogle-irc.git;a=blobdiff_plain;f=IRC.icl;h=f51bc8a2076b5b0fdb527290f644192e3264a912;hp=5d3ba4f3ebfe20e6539eb60c57b703e7061eaae0;hb=93f0540991809d459cfc3061c88fc07d1cfc948c;hpb=3df957122a6a6644ff8c8b720d2578010f54087e diff --git a/IRC.icl b/IRC.icl index 5d3ba4f..f51bc8a 100644 --- a/IRC.icl +++ b/IRC.icl @@ -40,25 +40,28 @@ where , parseIRCMessage ":cherryh.freenode.net JOIN #cha,#ch-b #twilight\r\n" , parseIRCMessage ":cherryh.freenode.net ISON a b c d e f :g h\r\n" , parseIRCMessage ":wilhelm.freenode.net 001 clooglebot :Welcome to the freenode Internet Relay Chat Network clooglebot\r\n" - + , parseIRCMessage "PING :orwell.freenode.net\r\n" + , parseIRCMessage ":ChanServ!ChanServ@services. MODE #cloogle +o frobnicator\r\n" ] parseIRCMessage :: String -> Either [Error] IRCMessage parseIRCMessage s = case runParser parsePrefix (fromString s) of + // Prefix is parsed ([(prefix, rest):_], _) - = case parse parseReply rest of - Left e = case parseCmd rest of - Left e2 = Left [e2:e] - Right cmd - = Right {IRCMessage | irc_prefix=prefix, irc_command=Right cmd} - Right repl - = Right {IRCMessage | irc_prefix=prefix, irc_command=Left repl} - (_, es) = Left ["couldn't parse prefix":es] + //Try parsing a numeric reply + = case parse parseReply rest of + //Try a normal command + Left e = case parseCmd rest of + Left e2 = Left [e2:e] + Right cmd = Right {IRCMessage | irc_prefix=prefix, irc_command=Right cmd} + Right repl = Right {IRCMessage | irc_prefix=prefix, irc_command=Left repl} + // Error parsing prefix + (_, es) = Left ["Error parsing prefix"] //Prefix parsePrefix :: Parser Char (Maybe (Either IRCUser String)) parsePrefix - = optional (pToken ':' >>| parseEither parseUser parseHost) <* pToken ' ' + = optional (pToken ':' >>| parseEither parseUser parseHost <* pToken ' ') where parseEither :: (Parser a b) (Parser a c) -> Parser a (Either b c) parseEither p q = Left <$> p <|> Right <$> q @@ -79,7 +82,8 @@ where >>= \cs->pure (toString [c:cs]) parseHost :: Parser Char String - parseHost = jon "." <$> pSepBy parseName (pToken '.') + parseHost = jon "." <$> (pSepBy parseName (pToken '.')) + >>= \s->optional (pToken '.') >>= pure o maybe s (\p->s+++toString s) where parseName :: Parser Char String parseName = toString <$> pSome (pAlpha <|> pDigit <|> pOneOf ['-']) @@ -121,7 +125,7 @@ illegal = ['\x00','\r','\n'] instance toString IRCNumReply where toString m = lpad (toString $ toInt m.irc_reply) 3 '0' <+ " " <+ - m.irc_recipient <+ concat (gIRCPrint{|*|} m.irc_message) + m.irc_recipient <+ " " <+ concat (gIRCPrint{|*|} m.irc_message) instance toString IRCMessage where toString m = maybe "" (\s->either ((<+) ":") id s <+ " ") m.irc_prefix <+ either toString toString m.irc_command