log pongs
[cloogle-irc.git] / cloogle.icl
1 module cloogle
2
3 import GenPrint
4 import IRC
5 import StdEnv
6
7 import Data.Functor
8 import Data.Maybe
9 from Data.Func import $
10 from Text import class Text(..), instance Text String
11
12 import TCPIP
13
14 commands :: [String]
15 commands = map toString
16 [NICK "clooglebot"
17 ,USER "cloogle" 0 "Cloogle bot"
18 ,JOIN [("#cloogle", Nothing)]
19 ]
20
21 TIMEOUT :== Just 10000
22 SERVER :== "irc.freenode.net"
23
24 KEY :== "PRIVMSG #cloogle :!"
25
26 send :: [String] TCP_DuplexChannel *World -> (TCP_DuplexChannel, *World)
27 send [] chan w = (chan, w)
28 send [msg:msgs] {sChannel,rChannel} w
29 # (rpt,i,sChannel,w) = send_MT TIMEOUT (toByteSeq msg) sChannel w
30 | rpt <> TR_Success = abort "Could not send request\n"
31 = send msgs {sChannel=sChannel,rChannel=rChannel} w
32
33 recv :: TCP_DuplexChannel *World -> (Maybe String, TCP_DuplexChannel, *World)
34 recv {sChannel,rChannel} w
35 # (rpt, resp, rChannel, w) = receive_MT TIMEOUT rChannel w
36 | rpt == TR_Expired = (Nothing, {sChannel=sChannel,rChannel=rChannel}, w)
37 | rpt == TR_NoSuccess || isNothing resp = abort "Halp?\n"
38 = (toString <$> resp, {sChannel=sChannel,rChannel=rChannel}, w)
39
40 msg :: (String -> IRCCommands)
41 msg = PRIVMSG "#cloogle"
42
43 process :: *File TCP_DuplexChannel *World -> (*File, TCP_DuplexChannel, *World)
44 process io chan w
45 # (mr, chan, w) = recv chan w
46 | isNothing mr = process io chan w
47 # resp = fromJust mr
48 #! io = io <<< ("Received: " +++ resp +++ "\n")
49 # ind = indexOf KEY resp
50 | ind > 0
51 # cmd = split " " $ rtrim $ subString (ind + size KEY) (size resp) resp
52 #! io = io <<< ("Received command: " +++ printToString cmd +++ "\n")
53 # toSend = case cmd of
54 ["stop":_] = Nothing
55 ["ping":xs] = Just [msg $ "pong " +++ join " " xs]
56 ["help"] = Just
57 [msg "type !help cmd for command specific help"
58 ,msg "available commands: help, ping"]
59 ["help":c:_] = case c of
60 "help" = Just [msg "help [CMD] - I will print general help or the help of CMD"]
61 "ping" = Just [msg "ping [TXT] - I will reply with pong and the optionar TXT"]
62 _ = Just [msg "Unknown command"]
63 [c:_] = Just [msg $ join " " ["unknown command: " , c, ", type !help to get help"]]
64 | isNothing toSend = (io, chan, w)
65 # (chan, w) = send (map toString $ fromJust toSend) chan w
66 = process io chan w
67 | indexOf "PING :" resp > 0
68 # cmd = rtrim $ subString (indexOf "PING :" resp + size "PING :") (size resp) resp
69 #! io <<< (toString $ PONG cmd Nothing) <<< "\n"
70 # (chan, w) = send [toString $ PONG cmd Nothing] chan w
71 = process io chan w
72 = process io chan w
73
74 Start :: *World -> *World
75 Start w
76 # (io, w) = stdio w
77 # (ip, w) = lookupIPAddress SERVER w
78 | isNothing ip = abort $ "DNS lookup for " +++ SERVER +++ " failed\n"
79 # (Just ip) = ip
80 # (rpt,chan,w) = connectTCP_MT TIMEOUT (ip, 6667) w
81 | rpt == TR_Expired = abort $ "Connection to " +++ SERVER +++ " timed out\n"
82 | rpt == TR_NoSuccess = abort $ "Could not connect to " +++ SERVER +++ "\n"
83 # chan = fromJust chan
84 # (chan, w) = send commands chan w
85 # (io, chan, w) = process io chan w
86 # ({sChannel,rChannel}, w) = send [toString $ QUIT Nothing] chan w
87 # (_, w) = fclose io w
88 = closeChannel sChannel (closeRChannel rChannel w)