3d5c15660b748f6fba739fc19864ce2af66709f9
[cloogle-irc.git] / IRCBot.icl
1 implementation module IRCBot
2
3 from Data.Func import $
4 import Data.Either
5 import Data.Error
6 import Data.Maybe
7 import IRC
8 from Text import class Text(concat,split,join), instance Text String
9 import StdList, StdString
10 import TCPServer.Connection
11
12 bot :: (String,Int) [IRCMessage] [IRCMessage] a (IRCMessage -> a -> .(*World -> *(Maybe [IRCMessage],a,*World))) *World -> *(Maybe String,a,*World)
13 //bot :: (String, Int) [IRCMessage] [IRCMessage] .a (IRCMessage -> (.a -> .(*World -> *(Maybe [IRCMessage], .a, *World)))) *World -> *(Maybe String, .a, *World)
14 bot (host, port) start end state bot w = case connect host port
15 { emptyConnection
16 & onConnect = onConnect
17 , onData = onData
18 } ("", state) w of
19 (Error e, w) = (Just e, state, w)
20 (Ok (acc, state), w) = (Nothing, 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, state, w) = bot msg state 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)