Clean up imports
[cloogle-irc.git] / IRCBot.icl
1 implementation module IRCBot
2
3 import StdEnv
4
5 import Data.Either
6 import Data.Func
7 import Data.Maybe
8 import Data.Tuple
9 import Text
10
11 import IRC
12
13 import TCPServer.Connection
14
15 bot :: (String, Int) [IRCMessage] [IRCMessage] .a (IRCMessage -> (.a -> *(*World -> *(Maybe [IRCMessage], .a, *World)))) !*World -> *(Maybe String, .a, !*World)
16 bot (host, port) start end state bot w = appSnd3 snd $ connect host port
17 { emptyConnection
18 & onConnect = onConnect
19 , onData = onData
20 } ("", state) w
21 where
22 onConnect s w = (Just (concat (map toString start)), connectionResponse s, w)
23 onData d (acc, s) w = case split "\r\n" (acc +++ d) of
24 [m,rest:xs]
25 = case parseIRCMessage $ m +++ "\r\n" of
26 // Do something with the error
27 (Left err) = (Nothing, {connectionResponse ("", s) & stop=True}, w)// (Error $ "IRC Parsing error: " +++ join "\n" err, chan, state, w)
28 (Right msg)
29 # acc = join "\r\n" [rest:xs]
30 # (mircc, s, w) = bot msg s w
31 | isNothing mircc = (Just (concat (map toString end)), {connectionResponse (acc, s) & stop=True}, w)
32 # tosendthis = concat (map toString (fromJust mircc))
33 # (tosend, cr, w) = onData "" (acc, s) w
34 = (Just (maybe tosendthis ((+++) tosendthis) tosend), cr, w)
35 [m] = (Nothing, connectionResponse (m, s), w)