X-Git-Url: https://git.martlubbers.net/?p=cloogle-irc.git;a=blobdiff_plain;f=IRC.icl;h=39e3f5004111d345f997a9c438d405652e7b0991;hp=50a42e903fb2157b3a8c4ce5b3a37164fcb61497;hb=1c65613d8732d28f6c6c11864ccbc761918ac594;hpb=87d709d61dae99cc468f52927e8246b3b6bc9022 diff --git a/IRC.icl b/IRC.icl index 50a42e9..39e3f50 100644 --- a/IRC.icl +++ b/IRC.icl @@ -1,16 +1,134 @@ implementation module IRC -import GenPrint -import StdOverloaded +import StdEnv + +import Control.Applicative +import Control.Monad => qualified join +import Data.Either +import Data.Func import Data.Maybe -from StdMisc import undef +import Text +import Text.GenPrint +import Text.Parsers.Simple.Chars +import Text.Parsers.Simple.Core + +import GenIRC + +derive gPrint IRCErrors, IRCReplies, Maybe, Either, IRCUser, IRCNumReply + +Start = (map (fmap toString) msgs, msgs) +where + msgs = + [ parseIRCMessage ":clooglebot!~cloogle@dhcp-077-249-221-037.chello.nl QUIT\r\n" + , parseIRCMessage ":clooglebot!~cloogle QUIT\r\n" + , parseIRCMessage ":frobnicator!~frobnicat@92.110.128.124 QUIT\r\n" + , parseIRCMessage ":frobnicator!~frobnicat@92.110.128.124 AWAY test\r\n" + , parseIRCMessage ":frobnicator!~frobnicat@92.110.128.124 AWAY :test with spaces\r\n" + , parseIRCMessage ":cherryh.freenode.net NOTICE * :*** Found your hostname\r\n" + , parseIRCMessage ":cherryh.freenode.net QUIT :hoi hoi\r\n" + , 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 ":qbot_v01!~qbot@ip-213-124-170-20.ip.prioritytelecom.net PRIVMSG ##chinees :[link] Cloogle - https://cloogle.org" + ] + +parseIRCMessage :: String -> Either [Error] IRCMessage +parseIRCMessage s = case runParser parsePrefix (fromString s) of + // Prefix is parsed + ([(prefix, rest):_], _) + //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 ' ') +where + parseEither :: (Parser a b) (Parser a c) -> Parser a (Either b c) + parseEither p q = Left <$> p <|> Right <$> q -derive gPrint IRCCommands, IRCReplies, IRCErrors, (,), Maybe, () + parseUser :: Parser Char IRCUser + parseUser = parseNick + >>= \nick->optional (pToken '!' >>| parseUsr) + >>= \muser->optional (pToken '@' >>| parseHost) + >>= \mhost->pure {IRCUser + | irc_nick=nick, irc_user=muser, irc_host=mhost} + + parseUsr :: Parser Char String + parseUsr = toString <$> pSome (pNoneOf [' ', '@':illegal]) + + parseNick :: Parser Char String + parseNick = pAlpha + >>= \c ->pMany (pAlpha <|> pDigit <|> pOneOf (fromString "_-[]\\`^{}")) + >>= \cs->pure (toString [c:cs]) -instance toString IRCCommands where toString r = printToString r + parseHost :: Parser Char String + parseHost = join "." <$> (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 ['-', '/']) + +//Parse Cmd +parseCmd :: [Char] -> Either Error IRCCommand +parseCmd cs = fst $ gIRCParse{|*|} $ argfun $ split " " $ toString cs + where + argfun :: [String] -> [String] + argfun [] = [] + argfun [x:xs] + # x = trim x + | x.[0] == ':' = [join " " $ [x % (1, size x):map rtrim xs]] + | otherwise = [x:argfun xs] + +//Reply +parseReply :: Parser Char IRCNumReply +parseReply = spaceParser + >>| (pMany (pToken '0') >>| pSome pDigit <* spaceParser) + >>= \rep->(toString <$> pSome (pNoneOf [' ':illegal]) <* spaceParser) + >>= \rec->(toString <$> pSome (pNoneOf illegal)) + >>= \msg->pure {IRCNumReply + | irc_reply = fromInt $ toInt $ toString rep + , irc_recipient = rec + , irc_message = msg % (if (msg.[0] == ':') 1 0, size msg) + } + <* pToken '\r' <* pToken '\n' + where + spaceParser :: Parser Char [Char] + spaceParser = pMany $ pToken ' ' + +//Common parsers +pNoneOf :: [a] -> Parser a a | Eq a +pNoneOf l = pSatisfy (not o flip isMember l) + +illegal :: [Char] +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) +instance toString IRCMessage where + toString m = maybe "" (\s->either ((<+) ":") id s <+ " ") m.irc_prefix + <+ either toString toString m.irc_command +instance toString IRCUser where + toString m = m.irc_nick <+ maybe "" ((<+) "!") m.irc_user + <+ maybe "" ((<+) "@") m.irc_host +instance toString IRCCommand where + toString m = join " " (gIRCPrint{|*|} m) +++ "\r\n" instance toString IRCReplies where toString r = printToString r instance toString IRCErrors where toString r = printToString r +(<+) infixr 5 :: a b -> String | toString a & toString b +(<+) a b = toString a +++ toString b + instance fromInt IRCReplies where fromInt r = case r of 1 = RPL_WELCOME @@ -97,7 +215,7 @@ instance fromInt IRCReplies where 393 = RPL_USERS 394 = RPL_ENDOFUSERS 395 = RPL_NOUSERS - _ = undef + _ = RPL_UNKNOWN instance toInt IRCReplies where toInt r = case r of @@ -185,6 +303,7 @@ instance toInt IRCReplies where RPL_USERS = 393 RPL_ENDOFUSERS = 394 RPL_NOUSERS = 395 + RPL_UNKNOWN = 998 instance fromInt IRCErrors where fromInt r = case r of @@ -241,6 +360,7 @@ instance fromInt IRCErrors where 491 = ERR_NOOPERHOST 501 = ERR_UMODEUNKNOWNFLAG 502 = ERR_USERSDONTMATCH + _ = ERR_UNKNOWN instance toInt IRCErrors where toInt r = case r of @@ -297,3 +417,4 @@ instance toInt IRCErrors where ERR_NOOPERHOST = 491 ERR_UMODEUNKNOWNFLAG = 501 ERR_USERSDONTMATCH = 502 + ERR_UNKNOWN = 999