尝试编写 IRC 客户端,但很难找到有关代码和连接协议的优质资源
我已经看过 RFC 但我仍然在挣扎。我已经用 C# 编写了一个基本客户端,但找不到有关如何正确连接的文档。
一旦我连接并传输NICK和USER信息,我就需要加入一个频道。如果我立即执行 JOIN,则不会发生任何事情 - 大概是因为太早了。我必须推迟它,但我不知道我需要等待哪个命令才能知道可以继续。
我收到类似以下内容:
:irc.fish.net NOTICE AUTH :Looking up your hostname...
和
:irc.fish.net 001 FishBot :Welcome
以及带有代码 002、003、005、251、252 等的内容。但我在网上找不到任何地方可以告诉我这些是什么。
所以我的两个基本问题是:您发送 JOIN 来响应什么?我在哪里可以找到上面的 IRC 代码对应的内容? RFC文档没用!
I've looked at the RFC but I am still struggling. I've written a basic client in C# but I cannot find documentation for how to connect properly.
Once I connect and transmit NICK and USER information, I need to join a channel. If I do a JOIN straight away, nothing happens - presumably because it's too soon. I have to delay it, but I don't know which command I need to wait for to know it's okay to go ahead.
I get stuff like:
:irc.fish.net NOTICE AUTH :looking up your hostname...
and
:irc.fish.net 001 FishBot :Welcome
as well as stuff with codes 002, 003, 005, 251, 252, etc. but I can't find anywhere online that shows me what these are.
So my basic 2 questions are: What do you send a JOIN in response to, and where can I find what the IRC codes above correspond to? The RFC document was useless!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
RFC文档当然不是无用的!您是正确的,您需要发送
USER
,然后发送NICK
。您收到的通知
是IRC服务器正在尝试通过名为IDENTD
的协议连接回您的PC。这是一个相对简单的协议,但其结果是它想知道连接到服务器的主机上的程序正在使用服务器拥有的本地/远程端口。很可能,您的防火墙阻止了这种情况(并且您可能没有运行 IDENTD 服务器)。这不是一个大问题,尽管成熟的 IRC 客户端会实现它。您可以在此处了解更多信息。这涉及到更多细节。实施起来相对简单。
如果无法连接到您,大多数 IRC 服务器都会放弃,而且我已经忘记了这种情况的具体副作用(已经有一段时间了),但是您要注意的下一条消息是 MOTD_START/MOTD/MOTD_END 和 ERR_NOMOTD。只有您收到当天消息的结尾或处理了 ERR_NOMOTD(没有)之后,您才能使用
JOIN
加入频道。顺便说一句,这是一个很好的正则表达式,用于匹配来自 IRC 服务器的输入:
IRC RFC 列出了所有可能的代码及其含义。我不知道为什么你认为它们毫无用处。您参考了哪些?
编辑
我查找了 IRC 的旧 C++ 代码,这样我可以提供更多帮助。连接后,它进入协商阶段(我已标记):
协商阶段:
PASS mypassword
。USER
命令。ERR_NOMOTD
、END_OFMOTD
。在其中之一出现之前,你们还没有“正式连接”。协商昵称阶段:
在连接过程中,完全有可能您要使用的昵称已被使用。因此,客户端应该:
NICK
命令ERR_NICKINUSE
响应,请再次发出该命令。如果您没有更多昵称可以使用,您可以退出或提示用户使用另一个昵称。其他需要考虑的事情:
PING
命令。服务器会在您空闲时发送此信息。将其作为高优先级处理,并使用服务器提供给您的数据返回PONG
。如果不这样做,将确保您断开连接,并且当您测试 IRC 客户端时,这可能会给后端带来麻烦。额外乐趣
这是我的 IRC 命令枚举,您应该能够轻松地将其放入 C# 中:
The RFC documents are certainly not useless! You are correct that you need to send
USER
followed byNICK
. TheNOTICE
you are getting is that the IRC server is attempting to connect back to your PC via a protocol calledIDENTD
. It's a relatively simple protocol but the upshot of it is that it wants to know that a program on the host that is connected to the server, is using the local/remote ports that the server has.Chances are, your firewall is preventing this (and you're probably not running an IDENTD server). This is not a huge problem, though a fully-fledged IRC client will implement it. You can find out more here. That goes in to much more detail. It's relatively simple to implement.
Most IRC servers will give up if it cannot connect to you, and I've forgotten the exact side-effect of this (it has been a while), but the next messages you want to look out for are the MOTD_START/MOTD/MOTD_END and ERR_NOMOTD. Only after you have received the end of the Message of the day, or handled the ERR_NOMOTD (there isn't one), can you then use
JOIN
to join channels.Incidentally, this is a good RegEx for matching input from an IRC Server:
The IRC RFCs list all the possible codes and what they mean. I'm not sure why you think they are useless. Which ones have you been referencing?
EDIT
I looked up my old C++ code for IRC so I could be a bit more helpful. After connecting, it enters a stage (that I have labelled) negotiating:
Negotiating Stage:
PASS mypassword
.USER
command.ERR_NOMOTD
,END_OFMOTD
. Until one of these comes, you're not "officially connected".Negotiate Nickname Stage:
It's entirely possible that during connection, the nickname you want to use is already in use. Therefore the client should:
NICK
commandERR_NICKINUSE
response, issue it again. If you have no more nicknames to use, you can either bailout or prompt the user for another one.Some other thing to consider:
PING
command. The server will send this when you're idle. Handle this as high-priority and returnPONG
with the data the server gave you. Failure to do this will ensure you get disconnected and when you're testing an IRC client, this can be a pain in the rear-end.Bonus Fun
This is my enum for the IRC commands, you should be able to put this in to C# easily enough:
也许您查看的是旧版本 (RFC 1459),而不是当前版本 (< a href="https://www.rfc-editor.org/rfc/rfc2812" rel="nofollow noreferrer">RFC第2812章
后一个列出了第 5 节“回复”中的数字代码:(
这应该回答您的第二个问题;不幸的是,我对协议不够熟悉,无法回答您的第一个问题。让您走上正确轨道的一个简单解决方案可能是使用一些数据包嗅探器来跟踪现有IRC客户端的连接。)
Maybe you looked at an old version (RFC 1459) instead of the current version (RFC 2812) of the standard?
The latter one lists the numeric codes in Section 5 "Replies":
(That should answer your second question; unfortunately, I'm not familiar enough with the protocol to answer your first. A simple solution to get you on the right track might be to trace the connection of an existing IRC client using some packet sniffer.)
代码可以在本文档中找到,您指定的代码是:
The codes can be found in this document, the ones you specified are: