summaryrefslogtreecommitdiff
path: root/Hsbot/Irc/Server.hs
blob: 3c20e6d4c777d72f8eb309192a40b05013f20201 (plain)
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
module Hsbot.Irc.Server
    ( initServerConnection
    , sendIrcMsg
    ) where

import Control.Concurrent.Chan
import Control.Monad.State

import Hsbot.Irc.Config
import Hsbot.Irc.Message
import Hsbot.Irc.Types

-- | Setup a newly connected server by sending nick and join stuff
initServerConnection :: IrcConfig -> IrcServer ()
initServerConnection config = do
    sendIrcMsg $ IrcMsg Nothing "NICK" [(ircConfigNickname config)]
    sendIrcMsg $ IrcMsg Nothing "USER" [(ircConfigNickname config), "0", "*", (ircConfigRealname config)]
    when (not . null $ ircConfigPassword config) $ do
        sendIrcMsg $ IrcMsg Nothing "PRIVMSG" ["nickserv", "identify", (ircConfigPassword config)]
    mapM_ joinChan (ircConfigChannels config)

-- | Joins a chan
joinChan :: String -> IrcServer ()
joinChan channel = do
    ircServer <- get
    let oldChannels = ircServerChannels ircServer
    sendIrcMsg $ IrcMsg Nothing "JOIN" [channel]
    put $ ircServer { ircServerChannels = channel : oldChannels }

-- | Sends an IrcMsg
sendIrcMsg :: IrcMsg -> IrcServer ()
sendIrcMsg ircMsg = do
    chan <- gets ircServerChan
    liftIO $ writeChan chan (OutIrcMsg ircMsg)