Haskell IRC Bot Stalling Out



我很了解Haskell,我正在为我的机器人编写IRC接口

运行代码后。。

module Main (main) where
import Network
import Data.List
import System.IO
server = "irc.freenode.org"
port   = 6667
chan   = "#tanuki"
nick   = "DuckBot01"
main :: IO ()
main = do
    bot <- connect 
    run bot
connect :: IO Handle
connect = notify $ do
    h <- connectTo server (PortNumber (fromIntegral port))
    hSetBuffering h NoBuffering
    return $ Bot { socket = h }
  where
    notify a = do
        putStrLn ("Connecting to " ++ server ++ " ... ") >> hFlush stdout
        putStrLn "done."
        a
run :: Handle -> IO () 
run h = do
    write h "NICK" nick
    write h "USER" (nick++" 0 * :tutorial bot")
    write h "JOIN" chan
    listen h 
--
-- Process each line from the server
--
listen :: Handle -> IO ()
listen h = forever $ do
    s <- init `fmap` hGetLine h
    putStrLn s
    if ping s then pong s else eval h (clean s)
  where
    forever a = a >> forever a
    clean     = drop 1 . dropWhile (/= ':') . drop 1
    ping x    = "PING :" `isPrefixOf` x
    pong x    = write h "PONG" (':' : drop 6 x)
eval :: Handle -> String -> IO ()
eval     h "!quit"               = write h "QUIT" ":byebye" 
eval     _ _                     = return () -- ignore everything else
privmsg :: Handle -> String -> IO ()
privmsg h s = write h "PRIVMSG" (chan ++ " :" ++ s)
write :: Handle -> String -> String -> IO ()
write handle s t = do
    hPrint handle $ s ++ " " ++ t ++ "rn"
    putStrLn $ "> " ++ s ++ " " ++ t ++ "n"

我在终端中得到以下输出:

Loading package bytestring-0.9.2.1 ... linking ... done.
Loading package transformers-0.2.2.0 ... linking ... done.
Loading package mtl-2.0.1.0 ... linking ... done.
Loading package array-0.4.0.0 ... linking ... done.
Loading package deepseq-1.3.0.0 ... linking ... done.
Loading package text-0.11.1.13 ... linking ... done.
Loading package parsec-3.1.2 ... linking ... done.
Loading package unix-2.5.1.0 ... linking ... done.
Loading package network-2.3.0.10 ... linking ... done.
Connecting to irc.freenode.org ... 
done.
> NICK DuckBot01
> USER DuckBot01 0 * :tutorial bot
> JOIN #tanuki
:cameron.freenode.net NOTICE * :*** Looking up your hostname...
:cameron.freenode.net NOTICE * :*** Checking Ident
:cameron.freenode.net NOTICE * :*** Found your hostname
:cameron.freenode.net NOTICE * :*** No Ident response
ERROR :Closing Link: 127.0.0.1 (Connection timed out)
*** Exception: <socket: 8>: hGetLine: end of file

为什么超时了?这不是我的连接,我的普通IRC客户端运行良好。感谢您的帮助。

不能100%确定,但我怀疑您的客户端在连接时动作太快。

IRC服务器应该响应您发送的命令,但事实并非如此。IRC服务器向您发送ERROR行也表明这不是连接问题,但IRC服务器正在断开您的连接,因为您的客户端没有按照协议运行。

请确保您等待IRC服务器开始发送数据,然后再发送代码所发送的三行数据。

使用hPutStr而不是hPrint,以便正确地渲染和发送rn到服务器。hPrint将为您提供与show相同的输出,这不是您想要的。我最近也在Roll Your Own IRC bot页面上制作了一个机器人,我坚持使用hPrintf

hPutStrLn会自动将n附加到您的字符串中,因此如果您不希望这样做,则不需要将其放入write中。

另外,你应该改变这一点。

return $ Bot { socket = h }

您的函数应该返回一个Handle,但您返回的是一个尚未定义的Bot。只要把它改成return h就可以了。(既然你已经编译好了,我想你已经解决了这个问题。)

ummm这里的问题是它连接到的端口对于irc.greenode.net应该是8001,而对于irc.reenode.org应该是6667。

最新更新