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(bind), >>=
24 from Text.Encodings.UrlEncoding import urlEncode
25 import System.CommandLine
36 import StdMisc, StdDebug
38 shorten :: String *World -> (String, *World)
40 # s = if (startsWith "http://" s) s (if (startsWith "https://" s) s ("http://" + s))
41 # data = "type=regular&url="+urlEncode s+"&token=a"
42 # (mer, w) = doHTTPRequest
44 & req_method = HTTP_POST
46 , server_name = "cloo.gl"
48 , req_headers = 'DM'.fromList
49 [("Content-Type", "application/x-www-form-urlencoded")
50 ,("Content-Length", toString $ size data)
52 , req_data = data} 10000 w
53 | isError mer = ("request failed: " + fromError mer, w)
57 cloogle :: String *World -> (String, *World)
59 # (mer, w) = doHTTPRequestFollowRedirects
61 & req_path = "/api.php"
62 , req_query = "?str=" + urlEncode data
63 , req_headers = 'DM'.fromList [("User-Agent", "cloogle-irc")]
64 , server_name = "cloogle.org"
65 , server_port = 80} 10000 10 w
66 | isError mer = ("request failed: " + fromError mer, w)
68 = case fromJSON $ fromString resp.HTTPResponse.rsp_data of
69 Nothing = ("couldn't parse json", w)
70 Just {return=127} = ("No results for " + data, w)
71 Just clr = ("Results for " + data + " -- https://cloogle.org/#" +
72 replaceSubString "+" "%20" (urlEncode data) + "\n" +
73 processResults clr, w)
75 processResults :: Response -> String
77 | resp.return > 127 = "Failed: return code: " + toString resp.return + ", " + resp.msg
78 = join "\n" $ map processResult $ take 3 resp.data
80 processResult :: Result -> String
81 processResult (FunctionResult (br, {func}))
82 = "Function in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ func
83 processResult (TypeResult (br, {type}))
84 = "Type in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ limitResults type
85 processResult (ClassResult (br, {class_name,class_funs}))
86 = "Class in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ class_name +++ " with "
87 +++ toString (length class_funs) +++ " class functions"
88 processResult (ModuleResult (br, _))
89 = "Module in " +++ br.library +++ ": " +++ br.modul
90 processResult (SyntaxResult (br, re))
91 = "Clean syntax: " +++ re.syntax_title +++ "\n"
92 +++ concat (intersperse "; " re.syntax_code)
94 limitResults :: String -> String
96 # lines = split "\n" s
97 | length lines > 4 = limitResults (join "\n" (take 3 lines) + "\n...")
98 = join "\n" (map maxWidth lines)
100 maxWidth :: String -> String
101 maxWidth s = if (size s > 80) (subString 0 77 s + "...") s
105 , bs_nickserv :: Maybe String
106 , bs_autojoin :: [String]
108 , bs_server :: String
109 , bs_strftime :: String
112 Start :: *World -> (Maybe String, *World)
114 # ([cmd:args], w) = getCommandLine w
116 # bs = parseCLI cmd args
118 # io = io <<< fromError bs <<< "\n"
119 = (Nothing, snd $ fclose io w)
121 # (_, w) = fclose io w
122 # (merr, _, w) = bot (bs.bs_server, bs.bs_port) (startup bs) shutdown () (process bs.bs_strftime) w
125 parseCLI :: String [String] -> MaybeErrorString BotSettings
127 { bs_nick = "clooglebot"
128 , bs_nickserv = Nothing
131 , bs_server = "irc.freenode.net"
135 | a == "-f" || a == "--strftime"
136 = arg1 "--strftime" as \a c->{c & bs_strftime=a}
137 | a == "-n" || a == "--nick"
138 = arg1 "--nick" as \a c->{c & bs_nick=a}
139 | a == "-ns" || a == "--nickserv"
140 = arg1 "--nickserv" as \a c->{c & bs_nickserv=Just a}
141 | a == "-a" || a == "--autojoin"
142 = arg1 "--autojoin" as \a c->{c & bs_autojoin=c.bs_autojoin ++ [a]}
143 | a == "-p" || a == "--port"
144 = arg1 "--port" as \a c->{c & bs_port=toInt a}
145 | a == "-s" || a == "--server"
146 = arg1 "--server" as \a c->{c & bs_server=a}
147 | a == "-h" || a == "--help" = Error $ join "\n" $
148 [ "Usage: " + cmd + " [OPTS]"
150 , "\t--strftime/-f FORMAT strftime format used in the output. default: %s\n"
151 , "\t--nick/-n NICKNAME Use the given nickname instead of clooglebot"
152 , "\t--nickserv/-ns PW Identify via the given password with NickServ"
153 , "\t--port/-p PORT Use the given port instead of port 6667"
154 , "\t--server/-s SERVER Use the given server instead of irc.freenode.net"
155 , "\t--autojoin/-a CHANNEL Add CHANNEL to the autojoin list. This command "
156 , "\t can be called multiple times. Beware that #"
157 , "\t has to be escaped in most shells"
159 = Error $ "Unknown option: " +++ a
161 arg1 name [] _ = Error $ name +++ " requires an argument"
162 arg1 name [a:as] f = parseCLI cmd as >>= Ok o f a
164 nickserv pw = PRIVMSG (CSepList ["NickServ"]) $ "IDENTIFY " +++ pw
166 toPrefix c = {irc_prefix=Nothing,irc_command=Right c}
168 startup bs = map toPrefix $
169 [ NICK bs.bs_nick Nothing
170 , USER "cloogle" "cloogle" "cloogle" "Cloogle bot"
171 ]++ maybe [] (pure o nickserv) bs.bs_nickserv
172 ++ if (isEmpty bs.bs_autojoin) []
173 [JOIN (CSepList bs.bs_autojoin) Nothing]
174 shutdown = map toPrefix [QUIT $ Just "Bye"]
176 process :: String !IRCMessage () !*World -> (Maybe [IRCMessage], (), !*World)
179 # (io, w) = log strf " (r): " im (io, w)
180 # (_, w) = fclose io w
181 = case im.irc_command of
182 Left numr = (Just [], (), w)
183 Right cmd = case process` im.irc_prefix cmd w of
184 (Nothing, w) = (Nothing, (), w)
186 # msgs = map toPrefix cs
189 log :: String String IRCMessage (!*File, !*World) -> (!*File, !*World)
190 log strf pref m (io, w)
191 #! (t, w) = localTime w
192 = (io <<< strfTime strf t <<< pref <<< toString m <<< "\n", w)
194 process` :: (Maybe (Either IRCUser String)) IRCCommand *World -> (Maybe [IRCCommand], *World)
195 process` (Just (Left user)) (PRIVMSG t m) w
196 | m == "!restart" = (Nothing, w)
198 # (msgs, w) = realProcess (split " " $ m % (1, size m)) w
199 = (Just $ map reply msgs, w)
200 | m % (0,4) == "\001PING" = (Just [reply m], w)
203 reply = case (\(CSepList [t:_]) -> t.[0]) t of
205 _ -> NOTICE user.irc_nick
206 process` _ (PING t mt) w = (Just [PONG t mt], w)
207 process` _ _ w = (Just [], w)
209 realProcess :: [String] *World -> ([String], *World)
210 realProcess ["help",x:xs] w = ((case x of
212 [ "Usage: !help [ARG]"
213 , "Show this help, or the specific help of the argument"]
215 [ "Usage: !ping [ARG [ARG ...]]"
216 , "Ping the bot, it will pong the arguments back"]
218 [ "Usage: !shorten URL [URL [URL ...]]"
219 , "Shorten the given urls with the cloo.gl url shortener"]
221 [ "Usage: !query QUERY"
222 , "Query QUERY in cloogle and return the results"]
226 x = ["Unknown command: " +++ x]
228 realProcess ["help"] w = (
229 ["Type !help cmd for command specific help"
230 ,"available commands: help, ping, shorten, query, restart"], w)
231 realProcess ["ping":xs] w = (["pong " +++ join " " xs], w)
232 realProcess ["shorten":xs] w = case xs of
233 [] = (["shorten requires at least one argument"], w)
234 xs = mapSt shorten xs w
235 realProcess ["query":xs] w = case xs of
236 [] = (["query requires one or more arguments"], w)
237 xs = appFst (split "\n") $ cloogle (join " " xs) w
238 realProcess ["restart":_] w = (["restart takes no arguments"], w)
239 realProcess [c:_] w = ([join " " [
240 "Unknown cmd: ", c, ", type !help to get help"]], w)