From: Mart Lubbers Date: Wed, 12 Jul 2017 11:08:13 +0000 (+0200) Subject: fix parsing and printing of numeric replies X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=3df957122a6a6644ff8c8b720d2578010f54087e;p=cloogle-irc.git fix parsing and printing of numeric replies --- diff --git a/.gitmodules b/.gitmodules index e69de29..681a9b6 100644 --- a/.gitmodules +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libcloogle"] + path = libcloogle + url = https://github.com/clean-cloogle/libcloogle.git diff --git a/IRC.icl b/IRC.icl index 48542ff..5d3ba4f 100644 --- a/IRC.icl +++ b/IRC.icl @@ -14,16 +14,18 @@ import Data.Tuple import Text.Parsers.Simple.Chars import Text.Parsers.Simple.Core +import StdDebug + from Data.Functor import <$> from Data.Func import $ -from StdMisc import undef -from Text - import class Text(trim,rtrim,split,indexOf,concat), instance Text String +from StdMisc import undef, abort +from Text import class Text(lpad,trim,rtrim,split,indexOf,concat), + instance Text String import qualified Text jon :== 'Text'.join -derive gPrint IRCErrors, IRCReplies +derive gPrint IRCErrors, IRCReplies, Maybe, Either, IRCUser, IRCNumReply Start = (map (fmap toString) msgs, msgs) where @@ -37,11 +39,14 @@ where , 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 :: String -> Either [Error] IRCMessage parseIRCMessage s = case runParser parsePrefix (fromString s) of - ([(prefix, rest):_], _) = case parse parseReply rest of + ([(prefix, rest):_], _) + = case parse parseReply rest of Left e = case parseCmd rest of Left e2 = Left [e2:e] Right cmd @@ -91,18 +96,19 @@ parseCmd cs = fst $ gIRCParse{|*|} $ argfun $ split " " $ toString cs | otherwise = [x:argfun xs] //Reply + parseReply :: Parser Char IRCNumReply -parseReply = (toString <$> pSome pDigit) - >>= \rep->pMiddle - >>= \recipient->spaceParser >>| (toString <$> pSome (pNoneOf illegal)) +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 rep - , irc_recipient=recipient,irc_message=msg} + | irc_reply = fromInt $ toInt $ toString rep + , irc_recipient = rec + , irc_message = msg % (if (msg.[0] == ':') 1 0, size msg) + } + <* pToken '\r' <* pToken '\n' where - pMiddle :: Parser Char String - pMiddle = fmap toString $ spaceParser >>| liftM2 (\x xs->[x:xs]) - (pSatisfy (not o ((==)':'))) (pMany $ pNoneOf [' ':illegal]) - spaceParser :: Parser Char [Char] spaceParser = pMany $ pToken ' ' @@ -114,7 +120,7 @@ illegal :: [Char] illegal = ['\x00','\r','\n'] instance toString IRCNumReply where - toString m = toInt m.irc_reply <+ " " <+ + 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 @@ -174,7 +180,7 @@ instance fromInt IRCReplies where 383 = RPL_YOURESERVICE; 391 = RPL_TIME; 392 = RPL_USERSSTART; 393 = RPL_USERS; 394 = RPL_ENDOFUSERS; 395 = RPL_NOUSERS; - _ = undef + _ = abort $ "fromInt IRCReplies: " +++ toString r +++ " undef\n" instance toInt IRCReplies where toInt r = case r of diff --git a/Makefile b/Makefile index 31a4240..5b95983 100644 --- a/Makefile +++ b/Makefile @@ -13,9 +13,9 @@ CLMLIBS:=\ -I $(CLEAN_HOME)/lib/Generics\ -I $(CLEAN_HOME)/lib/TCPIP\ -I $(CLEAN_HOME)/lib/Dynamics\ - -I ./cloogle-sub/backend + -I ./libcloogle -BINARIES:=test IRC +BINARIES:=test IRC cloogle all: $(BINARIES) diff --git a/cloogle.icl b/cloogle.icl index 9d8b2db..b2272af 100644 --- a/cloogle.icl +++ b/cloogle.icl @@ -2,7 +2,6 @@ module cloogle import Cloogle import GenPrint -import IRC import StdEnv import Data.Functor @@ -27,12 +26,14 @@ import Data.Functor import Data.Tuple import TCPIP +import IRC +import IRCBot commands :: [String] commands = map toString [NICK "clooglebot" Nothing ,USER "cloogle" "0" "cloogle" "Cloogle bot" - ,JOIN (CSepList ["#cloogle"]) + ,JOIN (CSepList ["#cloogle"]) Nothing ] TIMEOUT :== Just 10000 @@ -149,8 +150,8 @@ cloogle data w processResult (ClassResult (br, {class_name,class_funs})) = "Class in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ class_name +++ " with " +++ toString (length class_funs) +++ " class functions" - processResult (MacroResult (br, {macro_name})) - = "Macro in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ macro_name + //processResult (MacroResult (br, {macro_name})) + // = "Macro in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ macro_name processResult (ModuleResult (br, _)) = "Module in " +++ br.library +++ ": " +++ br.modul @@ -193,9 +194,9 @@ Start w = bot ("irc.freenode.net", 6667) startup shutdown () process w toPrefix c = {irc_prefix=Nothing,irc_command=Right c} startup = map toPrefix [NICK "clooglebot" Nothing - ,USER "cloogle" "0" "Cloogle bot" - ,JOIN [("#cloogle", Nothing)]] - shutdown = map toPrefix [QUIT (Just "Bye")] + ,USER "cloogle" "cloogle" "cloogle" "Cloogle bot" + ,JOIN (CSepList ["#cloogle"]) Nothing] + shutdown = map toPrefix [QUIT $ Just "Bye"] process :: IRCMessage () *World -> (Maybe [IRCMessage], (), *World) process im s w = case im.irc_command of diff --git a/libcloogle b/libcloogle new file mode 160000 index 0000000..d85bd3c --- /dev/null +++ b/libcloogle @@ -0,0 +1 @@ +Subproject commit d85bd3c5ccf6fdb82c8f62a08228cc8b6a0ac84f