10 from Data.Func import $, mapSt
11 from Text import class Text(..), instance Text String, instance + String
20 import Control.Applicative
21 import qualified Control.Monad as CM
22 import qualified Data.Map as DM
23 from Control.Monad import class Monad, instance Monad Maybe, >>=
24 from Text.Encodings.UrlEncoding import urlEncode
25 import System.CommandLine
36 shorten :: String *World -> (String, *World)
38 # s = if (startsWith "http://" s) s (if (startsWith "https://" s) s ("http://" + s))
39 # data = "type=regular&url="+urlEncode s+"&token=a"
40 # (mer, w) = doHTTPRequest
42 & req_method = HTTP_POST
44 , server_name = "cloo.gl"
46 , req_headers = 'DM'.fromList
47 [("Content-Type", "application/x-www-form-urlencoded")
48 ,("Content-Length", toString $ size data)
50 , req_data = data} 10000 w
51 | isError mer = ("request failed: " + fromError mer, w)
55 cloogle :: String *World -> (String, *World)
57 # (mer, w) = doHTTPRequestL
59 & req_path = "/api.php"
60 , req_query = "?str=" + urlEncode data
61 , req_headers = 'DM'.fromList [("User-Agent", "cloogle-irc")]
62 , server_name = "cloogle.org"
63 , server_port = 80} 10000 10 w
64 | isError mer = ("request failed: " + fromError mer, w)
66 = case fromJSON $ fromString resp.HTTPResponse.rsp_data of
67 Nothing = ("couldn't parse json", w)
68 Just clr = ("Results for " + data + " -- https://cloogle.org/#" +
69 replaceSubString "+" "%20" (urlEncode data) + "\n" +
70 processResults clr, w)
72 processResults :: Response -> String
74 | resp.return > 127 = "Failed: return code: " + toString resp.return + ", " + resp.msg
75 = join "\n" $ map processResult $ take 3 resp.data
77 processResult :: Result -> String
78 processResult (FunctionResult (br, {func}))
79 = "Function in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ func
80 processResult (TypeResult (br, {type}))
81 = "Type in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ limitResults type
82 processResult (ClassResult (br, {class_name,class_funs}))
83 = "Class in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ class_name +++ " with "
84 +++ toString (length class_funs) +++ " class functions"
85 processResult (ModuleResult (br, _))
86 = "Module in " +++ br.library +++ ": " +++ br.modul
88 limitResults :: String -> String
90 # lines = split "\n" s
91 | length lines > 4 = limitResults (join "\n" (take 3 lines) + "\n...")
92 = join "\n" (map maxWidth lines)
94 maxWidth :: String -> String
95 maxWidth s = if (size s > 80) (subString 0 77 s + "...") s
99 , bs_nickserv :: Maybe String
100 , bs_autojoin :: [String]
102 , bs_server :: String
103 , bs_strftime :: String
106 Start :: *World -> (Maybe String, *World)
108 # ([arg0:args], w) = getCommandLine w
111 //| isError bs = (Just $ "\n" +++ fromError bs +++ "\n", snd $ fclose io w)
113 # (merr, io, w) = bot (bs.bs_server, bs.bs_port) (startup bs) shutdown io (process bs.bs_strftime) w
114 = (Nothing, w)//= (merr, snd $ fclose io w)
116 parseCLI :: [String] -> MaybeErrorString BotSettings
118 { bs_nick = "clooglebot"
119 , bs_nickserv = Nothing
122 , bs_server = "irc.freenode.net"
126 | a == "-f" || a == "--strftime"
127 = arg1 "--strftime" as \a c->{c & bs_strftime=a}
128 | a == "-n" || a == "--nick"
129 = arg1 "--nick" as \a c->{c & bs_nick=a}
130 | a == "-ns" || a == "--nickserv"
131 = arg1 "--nickserv" as \a c->{c & bs_nickserv=Just a}
132 | a == "-a" || a == "--autojoin"
133 = arg1 "--autojoin" as \a c->{c & bs_autojoin=c.bs_autojoin ++ [a]}
134 | a == "-p" || a == "--port"
135 = arg1 "--port" as \a c->{c & bs_port=toInt a}
136 | a == "-s" || a == "--server"
137 = arg1 "--server" as \a c->{c & bs_server=a}
138 | a == "-h" || a == "--help" = Error $ join "\n" $
139 [ "Usage: cloogle [OPTS]"
141 , "\t--strftime/-f FORMAT strftime format used in the output. default: %s\n"
142 , "\t--nick/-n NICKNAME Use the given nickname instead of clooglebot"
143 , "\t--nickserv/-ns PW Identify via the given password with NickServ"
144 , "\t--port/-p PORT Use the given port instead of port 6667"
145 , "\t--server/-s SERVER Use the given server instead of irc.freenode.net"
146 , "\t--autojoin/-a CHANNEL Add CHANNEL to the autojoin list. This command "
147 , "\t can be called multiple times. Beware that #"
148 , "\t has to be escaped in most shells"
150 = Error $ "Unknown option: " +++ a
152 arg1 name [] _ = Error $ name +++ " requires an argument"
153 arg1 name [a:as] f = parseCLI as >>= Ok o f a
155 nickserv pw = PRIVMSG (CSepList ["NickServ"]) $ "IDENTIFY " +++ pw
157 toPrefix c = {irc_prefix=Nothing,irc_command=Right c}
159 startup bs = map toPrefix $
160 [ NICK bs.bs_nick Nothing
161 , USER "cloogle" "cloogle" "cloogle" "Cloogle bot"
162 ]++ maybe [] (pure o nickserv) bs.bs_nickserv
163 ++ if (isEmpty bs.bs_autojoin) []
164 [JOIN (CSepList bs.bs_autojoin) Nothing]
165 shutdown = map toPrefix [QUIT $ Just "Bye"]
167 process :: String !IRCMessage *File !*World -> (Maybe [IRCMessage], *File, !*World)
169 # (io, w) = log strf " (r): " im (io, w)
170 = case im.irc_command of
171 Left numr = (Just [], io, w)
172 Right cmd = case process` im.irc_prefix cmd w of
173 (Nothing, w) = (Nothing, io, w)
175 # msgs = map toPrefix cs
176 # (io, w) = foldr (log strf " (s): ") (io, w) msgs
179 log :: String String IRCMessage (*File, *World) -> (*File, *World)
180 log strf pref m (io, w) = (io, w)
181 // # (t, w) = localTime w
182 // = (io <<< strfTime strf t <<< pref <<< toString m, w)
184 process` :: (Maybe (Either IRCUser String)) IRCCommand *World -> (Maybe [IRCCommand], *World)
185 process` (Just (Left user)) (PRIVMSG t m) w
186 | m == "!restart" = (Nothing, w)
188 # (msgs, w) = realProcess (split " " $ m % (1, size m)) w
189 = (Just $ map reply msgs, w)
192 reply = case (\(CSepList [t:_]) -> t.[0]) t of
194 _ -> NOTICE user.irc_nick
195 process` _ (PING t mt) w = (Just [PONG t mt], w)
196 process` _ _ w = (Just [], w)
198 realProcess :: [String] *World -> ([String], *World)
199 realProcess ["help",x:xs] w = ((case x of
201 [ "Usage: !help [ARG]"
202 , "Show this help, or the specific help of the argument"]
204 [ "Usage: !ping [ARG [ARG ...]]"
205 , "Ping the bot, it will pong the arguments back"]
207 [ "Usage: !shorten URL [URL [URL ...]]"
208 , "Shorten the given urls with the cloo.gl url shortener"]
210 [ "Usage: !query QUERY"
211 , "Query QUERY in cloogle and return the results"]
215 x = ["Unknown command: " +++ x]
217 realProcess ["help"] w = (
218 ["Type !help cmd for command specific help"
219 ,"available commands: help, ping, shorten, query, restart"], w)
220 realProcess ["ping":xs] w = (["pong " +++ join " " xs], w)
221 realProcess ["shorten":xs] w = case xs of
222 [] = (["shorten requires at least one argument"], w)
223 xs = mapSt shorten xs w
224 realProcess ["query":xs] w = case xs of
225 [] = (["query requires one or more arguments"], w)
226 xs = appFst (split "\n") $ cloogle (join " " xs) w
227 realProcess ["restart":_] w = (["restart takes no arguments"], w)
228 realProcess [c:_] w = ([join " " [
229 "Unknown cmd: ", c, ", type !help to get help"]], w)