dev'
[cloogle-irc.git] / IRC.icl
diff --git a/IRC.icl b/IRC.icl
index 645edbd..9d03305 100644 (file)
--- a/IRC.icl
+++ b/IRC.icl
@@ -7,41 +7,230 @@ import Data.Maybe
 import Data.Either
 import StdFunc
 import StdString
-from Text import class Text(..), instance Text String
+import StdChar
+import StdBool
+import _SystemArray
+
+import Text.Parsers.Simple.Core
+import Text.Parsers.Simple.Chars
+import Data.Tuple
+import Control.Monad
+import Control.Applicative
+from Data.Functor import <$>
+
+from Data.Func import $
+from Text import class Text(ltrim,indexOf,concat), instance Text String
+import qualified Text
 from StdMisc import undef
+import StdDebug
+
+jon :== 'Text'.join
+
+derive gPrint IRCCommand, IRCReplies, IRCErrors, (,), Maybe, (), Either
+
+//Start = runParser parseMessage $ fromString ":frobnicator!~frobnicat@92.110.128.124 QUIT\r\n"
+//Start = runParser parseMessage $ fromString ":frobnicator!~frobnicat@92.110.128.124 AWAY test\r\n"
+//Start = runParser parseMessage $ fromString ":frobnicator!~frobnicat@92.110.128.124 AWAY :test with spaces\r\n"
+//Start = runParser parseMessage $ fromString ":cherryh.freenode.net NOTICE * :*** Found your hostname\r\n"
+//Start = runParser parseMessage $ fromString ":cherryh.freenode.net QUIT :hoi hoi\r\n"
+Start = parseIRCMessage ":clooglebot!~cloogle@dhcp-077-249-221-037.chello.nl QUIT\r\n"
+
+(<+) infixr 5 :: a b -> String | toString a & toString b
+(<+) a b = toString a +++ toString b
+
+parseIRCMessage :: String -> Either [Error] IRCMessage
+parseIRCMessage s = case runParser parsePrefix (fromString s) of
+       ([(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]
+
+
+parseCmd :: [Char] -> Either Error IRCCommand
+parseCmd cs
+| not (trace_tn $ toString cs) = undef
+= processParse $ argfun $ 'Text'.split " " $ toString cs
+       where
+               argfun :: [String] -> [String]
+               argfun [] = []
+               argfun [x:xs]
+               # x = 'Text'.trim x
+               | x.[0] == ':' = ['Text'.join " " [x:xs]]
+               | otherwise = [x:argfun xs]
+
+               command0 :: IRCCommand [String] -> Either Error IRCCommand
+               command0 c [] = Right c
+               command0 c x = Left $ toString c +++ " doesn't have arguments"
+
+               processParse :: [String] -> Either Error IRCCommand
+               processParse [] = Left "Empty list of arguments"
+               processParse [cmd:args] = case cmd of
+                       //"ADMIN" = (Maybe String)
+                       //"AWAY" = String
+                       //"CONNECT" = String (Maybe (Int, Maybe String))
+                       "DIE" = command0 DIE args
+                       //"ERROR" = String
+                       //"INFO" = (Maybe String)
+                       //"INVITE" = String String
+                       //"ISON" = [String]
+                       //"JOIN" = [(String, Maybe String)]
+                       //"KICK" = String String (Maybe String)
+                       //"KILL" = String String
+                       //"LINKS" = (Maybe (Maybe String, String))
+                       //"LIST" = (Maybe ([String], Maybe String))
+                       //"LUSERS" = (Maybe (String, Maybe String))
+                       //"MODE" = String String (Maybe String) (Maybe String) (Maybe String)
+                       //"MOTD" = (Maybe String)
+                       //"NAMES" = [String]
+                       //"NICK" = String (Maybe String)
+                       "NJOIN" = command0 NJOIN args
+                       //"NOTICE" = String String
+                       //"OPER" = String String 
+                       //"PART" = [String]
+                       //"PASS" = String
+                       //"PING" = String (Maybe String)
+                       //"PONG" = String (Maybe String)
+                       //"PRIVMSG" = [String] String
+                       "QUIT" = case args of
+                               [_,_:_] = Left $ "QUIT has too many arguments"
+                               x = Right $ QUIT $ listToMaybe x
+                       "REHASH" = command0 REHASH args
+                       "RESTART" = command0 REHASH args
+                       "SERVER" = command0 REHASH args
+                       //"SERVICE" = String String String String
+                       //"SERVLIST" = (Maybe (String, Maybe String))
+                       //"SQUERY" = String String
+                       "SQUIRT" = command0 REHASH args
+                       //"SQUIT" = String String
+                       //"STATS" = (Maybe (String, Maybe String))
+                       //"SUMMON" = String (Maybe (String, Maybe String))
+                       //"TIME" = (Maybe String)
+                       //"TOPIC" = String (Maybe String)
+                       //"TRACE" = (Maybe String)
+                       //"USER" = String String String
+                       //"USERHOST" = [String]
+                       //"USERS" = (Maybe String)
+                       //"VERSION" = (Maybe String)
+                       //"WALLOPS" = String
+                       //"WHO" = (Maybe String)
+                       //"WHOIS" = (Maybe String) [String]
+                       //"WHOWAS" = (Maybe String) [String]
+                       _ = Left $ "Unknown command: " +++ cmd
+
+parsePrefix :: Parser Char (Maybe (Either String IRCUser))
+parsePrefix = optional (parseEither parseHost parseUser) <* spaceParser
+
+parseReply :: Parser Char IRCNumReply
+parseReply = (toString <$> pSome pDigit)
+       >>= \rep->pMiddle
+       >>= \recipient->spaceParser >>| (toString <$> pSome (pNoneOf illegal))
+       >>= \msg->pure {IRCNumReply|irc_reply=fs rep,irc_recipient=recipient,irc_message=msg}
+       where
+               fs :: String -> IRCReplies
+               fs s = fromInt $ toInt s
+
+spaceParser :: Parser Char [Char]
+spaceParser = pMany $ pToken ' '
+
+parseServer :: Parser Char String
+parseServer = pFail
+
+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, (), Either
+parseUser :: Parser Char IRCUser
+parseUser = pToken ':' >>| parseNick
+               >>= \nick->optional (pToken '!' >>| parseUsr)
+               >>= \muser->optional (pToken '@' >>| parseHost)
+               >>= \mhost->pure {IRCUser | irc_nick=nick, irc_user=muser, irc_host=mhost}
 
-instance toString IRCCommands where
-       toString r = flip (+++) "\r\n" case r of
-       //ADMIN (Maybe String)
+parseUsr :: Parser Char String
+parseUsr = toString <$> pSome (pNoneOf [' ', '@':illegal])
+
+parseNick :: Parser Char String
+parseNick = pAlpha >>= \c->pMany (pAlpha <|> pDigit <|> pSpecial)
+       >>= \cs->pure (toString [c:cs])
+
+pSpecial :: Parser Char Char
+pSpecial = pOneOf ['-', '[', ']', '\\', '\`', '^', '{', '}']
+
+parseHost :: Parser Char String
+parseHost = pToken ':' >>| (concat <$> pSepBy parseName (pToken '.'))
+       where
+               parseName :: Parser Char String
+               parseName = toString <$> pSome (pAlpha <|> pDigit <|> pOneOf ['-'])
+
+instance toString IRCNumReply where
+       toString m = toInt m.irc_reply <+ " " <+ m.irc_recipient <+ " " <+ formatMSG 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
+
+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']
+
+instance toString IRCCommand where
+       toString r = jon " " (print r) +++ "\r\n"
+
+print :: IRCCommand -> [String]
+print r = case r of
+               ADMIN mm = ["ADMIN":maybeToList mm]
        //AWAY String
-       //CONNECT String Int (Maybe String)
+       //CONNECT String (Maybe (Int, Maybe String))
        //DIE 
        //ERROR String
        //INFO (Maybe String)
        //INVITE String String
        //ISON [String]
-               JOIN chs = "JOIN " +++ (if (isEmpty chs) "0"
-                       (join ", " [join " " [ch:maybeToList mk]\\(ch, mk)<-chs]))
+               JOIN chs = ["JOIN",if (isEmpty chs) "0"
+                       (jon ", " [jon " " [ch:maybeToList mk]\\(ch, mk)<-chs])]
        //KICK String String (Maybe String)
        //KILL String String
        //LINKS (Maybe (Maybe String, String))
-       //LIST [String]
+       //LIST (Maybe ([String], Maybe String))
        //LUSERS (Maybe (String, Maybe String))
-       //MODE String
+       //MODE String String (Maybe String) (Maybe String) (Maybe String)
        //MOTD (Maybe String)
        //NAMES [String]
-               NICK n = join " " ["NICK", n]
+               NICK n ms = ["NICK", n]
        //NJOIN 
        //NOTICE String String
        //OPER String String 
        //PART [String]
        //PASS String
-       //PING [String]
-       //PONG [String]
-               PRIVMSG dest msg = join " " ["PRIVMSG", dest, ":"+++msg]
-               QUIT msg = join " " ["QUIT":maybeToList msg]
+               PING a mb = ["PING",a:maybeToList mb]
+               PONG a mb = ["PONG",a:maybeToList mb]
+               PRIVMSG dest msg = ["PRIVMSG",jon "," dest,formatMSG msg]
+               QUIT msg = ["QUIT":maybeToList msg]
        //REHASH 
        //RESTART 
        //SERVER 
@@ -55,7 +244,7 @@ instance toString IRCCommands where
        //TIME (Maybe String)
        //TOPIC String (Maybe String)
        //TRACE (Maybe String)
-               USER login mode rn = join " " ["USER", login, toString mode, "*", ":"+++rn]
+               USER login mode rn = ["USER", login, mode, "*", ":"+++rn]
        //USERHOST [String]
        //USERS (Maybe String)
        //VERSION (Maybe String)
@@ -63,7 +252,10 @@ instance toString IRCCommands where
        //WHO (Maybe String)
        //WHOIS (Maybe String) [String]
        //WHOWAS (Maybe String) [String]
-               _ = printToString r
+               _ = [printToString r]
+
+formatMSG :: String -> String
+formatMSG s = if (indexOf " " s > 0 || indexOf " " s > 0) (":" +++ s) s
 
 
 instance toString IRCReplies where toString r = printToString r