1
set botlisten(port) "3333"
set botlisten(password) "123456"
set botlisten(channel) "#chan"
listen $botlisten(port) script botlisten
proc botlisten {idx} {
    control $idx botlisten2
}
proc botlisten2 {idx args} {
global botlisten newTorrentChannel
set args [join $args]
set botlisten(pass) [lindex [split $args] 0]
set botlisten(message) [join [lrange [split $args] 1 end]]
if {[string match $botlisten(pass) $botlisten(password)]} then {
   putquick "PRIVMSG $botlisten(channel) :$botlisten(message)"
 } else {
  putlog "Unauthorized person tried to connect to the bot"
  }
}  

假设消息有这些字符:ąčęėįšųūž 所以机器人输出奇怪的字符。所以,在我看来,解决方案是添加 utf-8 支持。

4

1 回答 1

6

十多年来,Tcl 已经完全集成了 UTF-8 支持(从 Tcl 8.1 开始,尽管没有人再理智地使用该版本,因为有单调更好的版本)。

然而,一般来说,有必要告诉 Tcl 在与外界的特定通信通道上使用什么编码(使用fconfigure's-encoding选项)。Tcl 使用依赖于系统的默认猜测;在我的系统上,它实际上是 UTF-8,但在其他系统上是 ISO 8859-1 或 -15 或适当的 Windows 代码页。(顺便说一句,Tcl 擅长进行默认猜测。)在套接字上更尴尬,因为编码实际上是协议级别的决定(某些协议指定了特定的编码 - SMTP 确实如此,IIRC - 一些在协议操作期间切换编码– HTTP 是一个典型的例子 – 有些根本没有指定 – IRC 是典型的例子)。在某些情况下,encoding命令是必需的,以便脚本可以手动控制字节序列和字符之间的转换。不过比较少见。

当然,如果正在使用的代码只是获取 Tcl 的字符串并使用低级网络(hellooo,eggdrop!)盲目地将它们推送到网络上,那么一般的 Tcl 级别实际上并不能做那么多。在这种情况下,解决方法是构建 eggdrop 以使用不同的编码(正如Zero 在他的评论中的链接所说),或者用于encoding进行 munging,如下所示:

将 UTF-8 转换为编码形式:

set encoded [encoding convertto utf-8 $normalString]

将编码的 UTF-8 转换回普通字符串:

set normalString [encoding convertfrom utf-8 $encoded]
于 2011-04-15T09:34:55.743 回答