Added IRC connection and initialisation stuff.
This commit is contained in:
parent
45ebb68aa0
commit
b9c8e4d404
4 changed files with 34 additions and 5 deletions
|
@ -10,7 +10,7 @@ import Hsbot.Core
|
||||||
kro = IrcServer
|
kro = IrcServer
|
||||||
{ address = "kro.corp"
|
{ address = "kro.corp"
|
||||||
, port = 6667
|
, port = 6667
|
||||||
, channels = ["#geek"]
|
, channels = ["#geek", "#shbot"]
|
||||||
, nickname = "hsbot"
|
, nickname = "hsbot"
|
||||||
, password = ""
|
, password = ""
|
||||||
, realname = "The One True bot, with it's haskell soul."
|
, realname = "The One True bot, with it's haskell soul."
|
||||||
|
|
|
@ -3,10 +3,12 @@ module Hsbot.Core
|
||||||
, Config(..)
|
, Config(..)
|
||||||
, IrcServer(..)
|
, IrcServer(..)
|
||||||
, newbot
|
, newbot
|
||||||
|
, sendstr
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
import System.IO (Handle)
|
import System.IO (Handle)
|
||||||
|
import Text.Printf (hPrintf)
|
||||||
|
|
||||||
-- | An IRC Bot server state (socket handles)
|
-- | An IRC Bot server state (socket handles)
|
||||||
data Bot = Bot
|
data Bot = Bot
|
||||||
|
@ -34,3 +36,6 @@ data IrcServer = IrcServer
|
||||||
newbot :: Bot
|
newbot :: Bot
|
||||||
newbot = Bot (M.empty)
|
newbot = Bot (M.empty)
|
||||||
|
|
||||||
|
-- | Send a string over handle
|
||||||
|
sendstr handle str = hPrintf handle "%s\r\n" str
|
||||||
|
|
||||||
|
|
27
Hsbot/IRC.hs
27
Hsbot/IRC.hs
|
@ -1,11 +1,15 @@
|
||||||
module Hsbot.IRC
|
module Hsbot.IRC
|
||||||
( IrcInput(..)
|
( IrcInput(..)
|
||||||
, IrcOutput(..)
|
, IrcOutput(..)
|
||||||
|
, connectServer
|
||||||
|
, initServer
|
||||||
, parseIrcMsg
|
, parseIrcMsg
|
||||||
)where
|
)where
|
||||||
|
|
||||||
import qualified Network.IRC as Irc
|
import Control.Monad
|
||||||
import System.IO (Handle)
|
import Network
|
||||||
|
import qualified Network.IRC as IRC
|
||||||
|
import System.IO
|
||||||
|
|
||||||
import Hsbot.Core
|
import Hsbot.Core
|
||||||
|
|
||||||
|
@ -32,3 +36,22 @@ data IrcOutput = Str String -- a regular string
|
||||||
parseIrcMsg :: String -> IrcInput
|
parseIrcMsg :: String -> IrcInput
|
||||||
parseIrcMsg _ = Err "Parsing not yet implemented"
|
parseIrcMsg _ = Err "Parsing not yet implemented"
|
||||||
|
|
||||||
|
-- | Connects to a server
|
||||||
|
connectServer :: IrcServer -> IO (IrcServer, Handle)
|
||||||
|
connectServer server = do
|
||||||
|
let name = address server
|
||||||
|
port_number = port server
|
||||||
|
handle <- connectTo name (PortNumber $ fromIntegral port_number)
|
||||||
|
hSetBuffering handle NoBuffering
|
||||||
|
return (server, handle)
|
||||||
|
|
||||||
|
-- | Setup a newly connected server by sending nick and join stuff
|
||||||
|
initServer :: (IrcServer, Handle) -> IO ()
|
||||||
|
initServer (server, handle) = do
|
||||||
|
sendstr handle (IRC.encode $ IRC.nick (nickname server))
|
||||||
|
sendstr handle (IRC.encode $ IRC.user (nickname server) "0" "*" (realname server))
|
||||||
|
when (not . null $ (password server)) $ do
|
||||||
|
sendstr handle (IRC.encode $ IRC.privmsg "nickserv" ("identify" ++ (password server)))
|
||||||
|
mapM_ (sendstr handle . IRC.encode . IRC.joinChan) (channels server)
|
||||||
|
return ()
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ module Hsbot.Main
|
||||||
(imain
|
(imain
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Network
|
|
||||||
import System.IO
|
import System.IO
|
||||||
import System.Plugins
|
import System.Plugins
|
||||||
|
|
||||||
|
@ -19,5 +18,7 @@ imain modul' reboot = imain' modul' reboot newbot
|
||||||
-- | Bot's main entry point
|
-- | Bot's main entry point
|
||||||
imain' :: Module -> Reboot -> Bot -> IO ()
|
imain' :: Module -> Reboot -> Bot -> IO ()
|
||||||
imain' modul' reboot bot = do
|
imain' modul' reboot bot = do
|
||||||
print C.config
|
servers' <- mapM connectServer (ircServers C.config)
|
||||||
|
mapM_ initServer servers'
|
||||||
|
return ()
|
||||||
|
|
||||||
|
|
Reference in a new issue