2. IRC 规范
2.1. 概述
本文所述的协议既用于服务器到服务器的连接, 也用于客户端到服务器的连接. 然而, 对客户端连接的限制比对服务器连接的限制更多 (客户端连接被视为不可信).
2.2. 字符代码
未指定特定字符集. 该协议基于由八 (8) 位组成的一组代码, 即一个八位字节. 每条消息可以由任意数量的这类八位字节组成; 但是, 某些八位字节值用于控制代码, 充当消息分隔符.
尽管这是一个 8 位协议, 但其分隔符和关键字使得该协议大多可以在 USASCII 终端和 telnet 连接上使用.
由于 IRC 起源于斯堪的纳维亚, 字符 |{} 分别被视为字符 []\ 的小写等价形式. 在确定两个昵称是否等价时, 这是一个关键问题.
2.3. 消息
服务器和客户端相互发送消息, 这些消息可能会也可能不会产生回复. 如果消息包含后文所述的有效命令, 客户端应当期望得到规定的回复, 但不建议无限期等待回复; 客户端到服务器以及服务器到服务器之间的通信本质上是异步的.
每条 IRC 消息最多可由三个主要部分组成: 前缀 (可选), 命令, 以及命令参数 (最多可有 15 个). 前缀, 命令和所有参数之间用一个或多个 ASCII 空格字符 (0x20) 分隔.
前缀的存在通过一个前导 ASCII 冒号字符 (':', 0x3b) 表示, 它必须是消息本身的第一个字符. 冒号与前缀之间不得有间隙 (空白). 服务器使用前缀来指示消息的真正来源. 如果消息中缺少前缀, 则假定它来自接收该消息的连接. 客户端在发送自己的消息时不应使用前缀; 如果使用前缀, 唯一有效的前缀是与该客户端关联的已注册昵称. 如果无法从服务器的内部数据库中找到前缀所标识的源, 或者该源是从与消息到达所用的链路不同的链路注册的, 则服务器必须静默忽略该消息.
命令必须是有效的 IRC 命令, 或者是以 ASCII 文本表示的三 (3) 位数字.
IRC 消息始终是以 CR-LF (回车 - 换行) 对结尾的字符行, 这些消息的长度不得超过 512 个字符, 包括末尾的 CR-LF 在内的所有字符都计算在内. 因此, 命令及其参数最多允许 510 个字符. 没有对续行消息的规定. 有关当前实现的更多细节, 请参见第 7 节.
2.3.1. 'pseudo' BNF 中的消息格式
必须从连续的八位字节流中提取协议消息. 当前的解决方案是指定两个字符 CR 和 LF 作为消息分隔符. 空消息会被静默忽略, 这允许在消息之间使用 CR-LF 序列而不会产生额外问题.
提取出的消息被解析为
其 BNF 表示如下:
<message> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
<prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
<command> ::= <letter> { <letter> } | <number> <number> <number>
<SPACE> ::= ' ' { ' ' }
<params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
<middle> ::= <Any *non-empty* sequence of octets not including SPACE
or NUL or CR or LF, the first of which may not be ':'>
<trailing> ::= <Any, possibly *empty*, sequence of octets not including
NUL or CR or LF>
<crlf> ::= CR LF
NOTES:
1) <SPACE> is consists only of SPACE character(s) (0x20).
Specially notice that TABULATION, and all other control
characters are considered NON-WHITE-SPACE.
2) After extracting the parameter list, all parameters are equal,
whether matched by <middle> or <trailing>. <Trailing> is just
a syntactic trick to allow SPACE within parameter.
3) The fact that CR and LF cannot appear in parameter strings is
just artifact of the message framing. This might change later.
4) The NUL character is not special in message framing, and
basically could end up inside a parameter, but as it would
cause extra complexities in normal C string handling. Therefore
NUL is not allowed within messages.
5) The last parameter may be an empty string.
6) Use of the extended prefix (['!' <user> ] ['@' <host> ]) must
not be used in server to server communications and is only
intended for server to client messages in order to provide
clients with more useful information about who a message is
from without the need for additional queries.
大多数协议消息会根据提取出的参数字符串在列表中的位置, 为其规定额外的语义和语法. 例如, 许多服务器命令会假定命令后的第一个参数是目标列表, 可描述为:
<target> ::= <to> [ "," <target> ]
<to> ::= <channel> | <user> '@' <servername> | <nick> | <mask>
<channel> ::= ('#' | '&') <chstring>
<servername> ::= <host>
<host> ::= see RFC 952 [DNS:4] for details on allowed hostnames
<nick> ::= <letter> { <letter> | <number> | <special> }
<mask> ::= ('#' | '$') <chstring>
<chstring> ::= <any 8bit code except SPACE, BELL, NUL, CR, LF and
comma (',')>
Other parameter syntaxes are:
<user> ::= <nonwhite> { <nonwhite> }
<letter> ::= 'a' ... 'z' | 'A' ... 'Z'
<number> ::= '0' ... '9'
<special> ::= '-' | '[' | ']' | '\' | '`' | '^' | '{' | '}'
<nonwhite> ::= <any 8bit code except SPACE (0x20), NUL (0x0), CR
(0xd), and LF (0xa)>
2.4. 数字回复
发送到服务器的大多数消息都会产生某种回复. 最常见的回复是数字回复, 它既用于错误, 也用于正常回复. 数字回复必须作为一条消息发送, 该消息由发送者前缀, 三位数字以及回复目标组成. 数字回复不允许源自客户端; 服务器收到的任何此类消息都会被静默丢弃. 在其他所有方面, 数字回复与普通消息一样, 只是关键字由 3 个数字字符组成, 而不是字母字符串. 第 6 节提供了不同回复的列表.