summaryrefslogtreecommitdiff
path: root/Hsbot/IRC.hs
blob: 76853e2e926d98c9c7c6c59b4c3fe1023111bb53 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module Hsbot.IRC
    ( IrcLine(..)
    , connectServer
    , initServer
    , parseIrcMsg
    , ping
    , pong
    , sendPrivmsg
    )where

import Control.Monad
import Data.List(isPrefixOf)
import Data.Maybe
import Network
import qualified Network.IRC as IRC
import System.IO

import Hsbot.Core
import Hsbot.IRCParser

type User    = String
type Channel = String
type Command = String
type Args    = [String]

-- | An IRC line
data IrcLine = Privmsg (String, [String])   -- statement (chan, sentence...)
               | Quit (IrcServer, Handle)   -- a quit message from a server
               | Join (IrcServer, Channel)  -- joined a channel
               | Part (IrcServer, Channel)  -- parted the channel
               | Ping (String)              -- pinged by the server
               | Reboot                     -- reboot message sent
               | Nil                        -- signifies thread death, only happens after reboot
    deriving (Eq,Show)

-- | Parses an IrcInput
parseIrcMsg :: String -> IrcLine
parseIrcMsg str = 
    case (ircParser str) of
        Left err -> Nil
        Right x  -> eval x
    where
        eval :: IrcMsg -> IrcLine
        eval x@(IrcMsg statement cmd stuff)
            | cmd == "PING" = Ping $ head stuff
            | cmd == "PRIVMSG" =
		    case statement of
		        Nothing -> Nil
			Just statement' -> if stuff!!1 == "reboot" then Reboot
                                              else  Privmsg $ (statement', stuff)
            | otherwise = Nil

-- | 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 ()

-- | Check if a message is a PING
ping :: String -> Bool
ping = isPrefixOf "PING :"

-- | Send a pong message given a ping message
pong :: Handle -> String -> IO ()
pong handle str = sendstr handle $ "PONG " ++ (drop 5 str)

sendPrivmsg :: (IrcServer, Handle) -> [String] -> IO ()
sendPrivmsg (server, handle) stuff' = sendstr handle (IRC.encode $ IRC.privmsg (head stuff') (unwords . tail $ stuff'))