1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
module Hsbot.Core
( Bot(..)
, Config(..)
, IrcServer(..)
, isConnected
, newbot
, sendstr
, saveServersStates
) where
import qualified Data.Map as M
import System.IO (Handle)
import Text.Printf (hPrintf)
-- | An IRC Bot server state (socket handles)
data Bot = Bot
{ joinedServers :: M.Map IrcServer Handle -- servers we are connected to
} deriving (Eq, Show)
-- | Configuration data type
data Config = Config {
commandPrefixes :: String, -- command prefixes, for example @[\'>\',\'@\',\'?\']@
ircServers :: [IrcServer] -- list of 'Server's to connect to
} deriving (Eq,Show)
-- | An IRC server
data IrcServer = IrcServer
{ address :: String -- the server's address
, port :: Int -- the server's port
, channels :: [String] -- a list of channels to join
, nickname :: String -- the hsbot's nickname
, password :: String -- the hsbot's password, optional
, realname :: String -- the hsbot's real name, optional
, administrators :: [String] -- bot admins nicknames
} deriving (Eq, Ord, Show)
-- | Returns a new, empty bot
newbot :: Bot
newbot = Bot (M.empty)
-- | Send a string over handle
sendstr :: Handle -> String -> IO ()
sendstr handle str = hPrintf handle "%s\r\n" str
-- | Are we already connected to this server?
isConnected :: Bot -> IrcServer -> Bool
isConnected (Bot bot) ircServer = ircServer `M.member` bot
saveServerState :: Handle -> IrcServer -> Bot -> Bot
saveServerState handle ircServer x@(Bot bot) =
if ircServer `M.member` bot then x
else (Bot $ M.insert ircServer handle bot)
saveServersStates :: [(IrcServer,Handle)] -> Bot -> Bot
saveServersStates liste bot = foldr (\(ircServer,handle) bot' -> saveServerState handle ircServer bot') bot liste
|