0

我非常了解 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 ++ "\r\n"
    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 客户端工作正常。帮助表示赞赏。

4

3 回答 3

2

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

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

尝试确保在代码发送它所做的三行之前等待 IRC 服务器开始发送数据。

于 2012-08-25T00:51:26.660 回答
1

使用hPutStr而不是hPrint这样,您\r\n将正确呈现并发送到服务器。hPrint将为您提供与 相同的输出show,这不是您想要的。我最近也按照 Roll Your Own IRC Bot 页面制作了一个机器人,我坚持使用hPrintf.

hPutStrLn自动附加到您的字符串,因此如果这不是您想要的,则\n无需将其放入您的字符串中。write

此外,您应该更改此位。

return $ Bot { socket = h }

您的函数应该返回 aHandle但您返回的是Bot尚未定义的 a 。只需将其更改为return h,您应该就可以了。(既然你已经编译好了,我假设你已经修复了它。)

于 2012-08-26T05:51:19.487 回答
-1

嗯,这里的问题是它连接的端口应该是 irc.freenode.net 的 8001 而不是 irc.freenode.org 的 6667。

于 2013-01-27T07:57:38.257 回答