-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathircbot.hs
79 lines (66 loc) · 1.76 KB
/
ircbot.hs
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
import Network
import System.IO
import Control.Concurrent
import Data.Time
import Control.Applicative
import Data.Maybe
import Control.Exception
import System.Exit
import Data.Sequence hiding (replicate)
import Control.Monad
import Data.ByteString.Char8 hiding (replicate, putStrLn, hPutStr, hGetLine)
import Network.IRC
host :: HostName
host = "127.0.0.1"
port :: PortID
port = PortNumber 6667
hExistsInput :: Handle -> IO Bool
hExistsInput h = catch (hWaitForInput h 500) handler
where
handler :: IOException -> IO Bool
handler e = do
let msg = "A connection problem occured: " ++ show e
pprint $ Just msg
exitFailure
hGetReadyLine :: Handle -> IO (Maybe String)
hGetReadyLine h = hExistsInput h >>= hRead
where
hRead True = Just <$> hGetLine h
hRead False = return Nothing
pprint :: Maybe String -> IO ()
pprint line
| isJust line && isJust msg = getCurrentTime >>= \t -> (putStrLn $ (show t) ++ " " ++ (show . fromJust $ msg))
| otherwise = return ()
where
msg = (decode . pack . fromJust $ line)
getInput :: Handle -> IO ()
getInput h = hGetReadyLine h >>= pprint
write :: Handle -> String -> IO ()
write h msg = do
let debugMsg = "Sending to server: " ++ msg
pprint $ Just debugMsg
hPutStr h (msg ++ "\r\n")
readServer :: Handle -> Int -> IO ()
readServer h n = do
let lines = replicate n (getInput h)
sequence_ lines
ircLogin :: Handle -> IO ()
ircLogin h = do
write h "NICK hbot"
write h "USER hbot localhost localhost :Hbot"
write h "JOIN #mychannel"
main = do
h <- connectTo host port
hSetBuffering h NoBuffering
-- --------------
let rs2 = readServer h 2
let rs30 = readServer h 30
let login = ircLogin h
rs2
login
threadDelay 5000000
forever rs30
-- threadDelay 5000000
-- --------------
hClose h
print "the end"