-send :: [String] TCP_DuplexChannel *World -> (TCP_DuplexChannel, *World)
-send [] chan w = (chan, w)
-send [msg:msgs] {sChannel,rChannel} w
-# (rpt,i,sChannel,w) = send_MT TIMEOUT (toByteSeq msg) sChannel w
-| rpt <> TR_Success = abort "Could not send request\n"
-= send msgs {sChannel=sChannel,rChannel=rChannel} w
-
-recv :: TCP_DuplexChannel *World -> (Maybe String, TCP_DuplexChannel, *World)
-recv {sChannel,rChannel} w
-# (rpt, resp, rChannel, w) = receive_MT TIMEOUT rChannel w
-| rpt == TR_Expired = (Nothing, {sChannel=sChannel,rChannel=rChannel}, w)
-| rpt == TR_NoSuccess || isNothing resp = abort "Halp?\n"
-= (toString <$> resp, {sChannel=sChannel,rChannel=rChannel}, w)
-
-msg :: (String -> IRCCommands)
-msg = PRIVMSG "#cloogle"
-
-process :: *File TCP_DuplexChannel *World -> (*File, TCP_DuplexChannel, *World)
-process io chan w
-# (mr, chan, w) = recv chan w
-| isNothing mr = process io chan w
-# resp = fromJust mr
-#! io = io <<< ("Received: " +++ resp +++ "\n")
-# ind = indexOf KEY resp
-| ind >= 0
- # cmd = split " " $ rtrim $ subString (ind + size KEY) (size resp) resp
- #! io = io <<< ("Received command: " +++ printToString cmd +++ "\n")
- # (w, toSend) = case cmd of
+cloogle :: String *World -> (String, *World)
+cloogle data w
+# (mer, w) = doRequestL
+ { newHTTPRequest
+ & req_path = "/api.php"
+ , req_query = "?str=" + urlEncode data
+ , req_headers = 'DM'.fromList [("User-Agent", "cloogle-irc")]
+ , server_name = "cloogle.org"
+ , server_port = 80} 10 w
+| isError mer = ("request failed: " + fromError mer, w)
+# resp = fromOk mer
+= case fromJSON $ fromString resp.HTTPResponse.rsp_data of
+ Nothing = ("couldn't parse json", w)
+ Just clr = ("Results for " + data + " -- https://cloogle.org/#" + replaceSubString "+" "%20" (urlEncode data) + "\n" +
+ processResults clr, w)
+ where
+ processResults :: Response -> String
+ processResults resp
+ | resp.return > 127 = "Failed: return code: " + toString resp.return + ", " + resp.msg
+ = join "\n" $ map processResult $ take 3 resp.data
+
+ processResult :: Result -> String
+ processResult (FunctionResult (br, {func}))
+ = "Function in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ func
+ processResult (TypeResult (br, {type}))
+ = "Type in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ limitResults type
+ 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 (ModuleResult (br, _))
+ = "Module in " +++ br.library +++ ": " +++ br.modul
+
+ limitResults :: String -> String
+ limitResults s
+ # lines = split "\n" s
+ | length lines > 4 = limitResults (join "\n" (take 3 lines) + "\n...")
+ = join "\n" (map maxWidth lines)
+
+ maxWidth :: String -> String
+ maxWidth s
+ | size s > 80 = subString 0 77 s + "..."
+ = s
+
+/*