Merge branch 'interface-improvements' of github.com:clean-cloogle/clean-irc into...
[cloogle-irc.git] / cloogleirc.icl
index 596f8c4..a504883 100644 (file)
@@ -15,6 +15,7 @@ import Internet.HTTP
 import Text.JSON
 
 import Text.URI
+import System.Time
 
 import Control.Applicative
 import qualified Control.Monad as CM
@@ -32,8 +33,7 @@ import TCPIP
 import IRC
 import IRCBot
 
-TIMEOUT :== Just 10000
-SERVER :== "irc.freenode.net"
+import StdMisc, StdDebug
 
 shorten :: String *World -> (String, *World)
 shorten s w 
@@ -56,7 +56,7 @@ shorten s w
 
 cloogle :: String *World -> (String, *World)
 cloogle data w
-# (mer, w) = doHTTPRequestL
+# (mer, w) = doHTTPRequestFollowRedirects
                { newHTTPRequest
                & req_path = "/api.php"
                , req_query = "?str=" + urlEncode data
@@ -67,6 +67,7 @@ cloogle data w
 # resp = fromOk mer
 = case fromJSON $ fromString resp.HTTPResponse.rsp_data of
        Nothing = ("couldn't parse json", w)
+       Just {return=127} = ("No results for " + data, w)
        Just clr = ("Results for " + data + " -- https://cloogle.org/#" +
                replaceSubString "+" "%20" (urlEncode data) + "\n" +
                processResults clr, w)
@@ -86,6 +87,9 @@ cloogle data w
                                +++ toString (length class_funs) +++ " class functions"
                processResult (ModuleResult (br, _))
                        = "Module in " +++ br.library +++ ": " +++ br.modul
+               processResult (SyntaxResult (br, re))
+                       = "Clean syntax: " +++ re.syntax_title +++ "\n"
+                               +++ concat (intersperse "; " re.syntax_code)
 
                limitResults :: String -> String
                limitResults s
@@ -94,9 +98,7 @@ cloogle data w
                = join "\n" (map maxWidth lines)
                
                maxWidth :: String -> String
-               maxWidth s
-               | size s > 80 = subString 0 77 s + "..."
-               = s
+               maxWidth s = if (size s > 80) (subString 0 77 s + "...") s
 
 :: BotSettings =
                { bs_nick     :: String
@@ -104,25 +106,33 @@ cloogle data w
                , bs_autojoin :: [String]
                , bs_port     :: Int
                , bs_server   :: String
+               , bs_strftime :: String
                }
 
-Start :: *World -> (MaybeErrorString (), *World)
+Start :: *World -> (Maybe String, *World)
 Start w
-# ([arg0:args], w) = getCommandLine w
-# bs = parseCLI args 
-| isError bs = (Error $ "\n" +++ fromError bs +++ "\n", w)
+# ([cmd:args], w) = getCommandLine w
+# (io, w) = stdio w
+# bs = parseCLI cmd args
+| isError bs
+       # io = io <<< fromError bs <<< "\n"
+       = (Nothing, snd $ fclose io w)
 # (Ok bs) = bs
-= bot (bs.bs_server, bs.bs_port) (startup bs) shutdown () process w
+# (merr, io, w) = bot (bs.bs_server, bs.bs_port) (startup bs) shutdown io (process bs.bs_strftime) w
+= (merr, snd $ fclose io w)
        where
-               parseCLI :: [String] -> MaybeErrorString BotSettings
-               parseCLI [] = Ok
+               parseCLI :: String [String] -> MaybeErrorString BotSettings
+               parseCLI [] = Ok
                        { bs_nick     = "clooglebot"
                        , bs_nickserv = Nothing
                        , bs_autojoin = []
                        , bs_port     = 6667
                        , bs_server   = "irc.freenode.net"
+                       , bs_strftime = "%s"
                        }
-               parseCLI [a:as]
+               parseCLI cmd [a:as]
+               | a == "-f" || a == "--strftime"
+                       = arg1 "--strftime" as \a c->{c & bs_strftime=a}
                | a == "-n" || a == "--nick"
                        = arg1 "--nick" as \a c->{c & bs_nick=a}
                | a == "-ns" || a == "--nickserv"
@@ -132,10 +142,11 @@ Start w
                | a == "-p" || a == "--port"
                        = arg1 "--port" as \a c->{c & bs_port=toInt a}
                | a == "-s" || a == "--server"
-                       = arg1 "--port" as \a c->{c & bs_server=a}
+                       = arg1 "--server" as \a c->{c & bs_server=a}
                | a == "-h" || a == "--help" = Error $ join "\n" $
-                       [ "Usage: cloogle [OPTS]"
+                       [ "Usage: " + cmd + " [OPTS]"
                        , "Options:"
+                       , "\t--strftime/-f FORMAT   strftime format used in the output. default: %s\n"
                        , "\t--nick/-n NICKNAME     Use the given nickname instead of clooglebot"
                        , "\t--nickserv/-ns PW      Identify via the given password with NickServ"
                        , "\t--port/-p PORT         Use the given port instead of port 6667"
@@ -145,9 +156,9 @@ Start w
                        , "\t                       has to be escaped in most shells"
                        ]
                = Error $ "Unknown option: " +++ a
-
-               arg1 name [] _ = Error $ name +++ " requires an argument"
-               arg1 name [a:as] f = parseCLI as >>= Ok o f a
+               where
+                       arg1 name [] _ = Error $ name +++ " requires an argument"
+                       arg1 name [a:as] f = parseCLI cmd as >>= Ok o f a
 
                nickserv pw = PRIVMSG (CSepList ["NickServ"]) $ "IDENTIFY " +++ pw
 
@@ -161,24 +172,35 @@ Start w
                                [JOIN (CSepList bs.bs_autojoin) Nothing]
                shutdown = map toPrefix [QUIT $ Just "Bye"]
 
-               process :: IRCMessage () *World -> (Maybe [IRCMessage], (), *World)
-               process im s w = case im.irc_command of
-                       Left numr = (Just [], (), w)
+               process :: String !IRCMessage *File !*World -> (Maybe [IRCMessage], *File, !*World)
+               process strf im io w
+               #! (io, w) = log strf " (r): " im (io, w)
+               = case im.irc_command of
+                       Left numr = (Just [], io, w)
                        Right cmd = case process` im.irc_prefix cmd w of
-                               (Nothing, w) = (Nothing, (), w)
-                               (Just cs, w) = (Just $ map toPrefix cs, (), w)
+                               (Nothing, w) = (Nothing, io, w)
+                               (Just cs, w)
+                               # msgs = map toPrefix cs
+                               #! (io, w) = foldr (log strf " (s): ") (io, w) msgs
+                               = (Just msgs, io, w)
+
+               log :: String String IRCMessage (!*File, !*World) -> (!*File, !*World)
+               log strf pref m (io, w)
+               #! (t, w) = localTime w
+               = (io <<< strfTime strf t <<< pref <<< toString m <<< "\n", w)
 
                process` :: (Maybe (Either IRCUser String)) IRCCommand *World -> (Maybe [IRCCommand], *World)
                process` (Just (Left user)) (PRIVMSG t m) w
                        | m == "!restart" = (Nothing, w)
                        | m.[0] == '!'
                                # (msgs, w) = realProcess (split " " $ m % (1, size m)) w
-                               = (Just $ map (NOTICE recipient) msgs, w)
+                               = (Just $ map reply msgs, w)
+                       | m % (0,4) == "\001PING" = (Just [reply m], w)
                        = (Just [], w)
                where
-                       recipient = case (\(CSepList [t:_]) -> t.[0]) t of
-                               '#' -> t
-                               _   -> CSepList [user.irc_nick]
+                       reply = case (\(CSepList [t:_]) -> t.[0]) t of
+                               '#' -> PRIVMSG t
+                               _   -> NOTICE user.irc_nick
                process` _ (PING t mt) w = (Just [PONG t mt], w)
                process` _ _ w = (Just [], w)