无效的命令名称“”

invalid command name ""

我无法让这个脚本接受例如https://youtu.be/HPP0yB-_blA, https://www.youtube.com/watch?v=HPP0yB-_blA 虽然有效。第一个例子只是导致 invalid command name "".

# URL title parse script for Eggdrop.
#
# Based on https://github.com/teeli/urltitle by teel.
#
# Version log:
#   0.11    Minor site specific tweaks.
#   0.1     First version.
#
# Usage:
#   .chanset #channelname +urltitle  ;# Enable script.

namespace eval urltitle {
    # Configuration variables.
    set delay 1  ;# Minimum number of seconds to wait between uses.
    set length 5  ;# Minimum character length of URL to trigger usage.
    set timeout 5000  ;# Geturl timeout in milliseconds (1/1000ths of a second).

    # Internal variables.
    set ignoredSites {apina.biz}  ;# Sites to ignore when parsing URLs.
    set last 1  ;# Stores time of last usage.
    set scriptVersion 0.11  ;# Script version number.

    # Binds/Hooks.
    bind pubm - "*://*" urltitle::handler
    setudef flag urltitle  ;# Channel flag to enable script.

    # Required packages.
    package require http
    package require tdom
    package require tls

    proc socket {args} {
        set opts [lrange $args 0 end-2]
        set host [lindex $args end-1]
        set port [lindex $args end]

        ::tls::socket -autoservername true {*}$opts $host $port
    }

    proc handler {nick host user chan text} {
        set time [clock seconds]
        variable delay
        variable ignoredSites
        variable last
        variable length

        if {[channel get $chan urltitle] && ($time - $delay) > $last} {
            foreach word [split $text] {
                if {[string length $word] >= $length && [regexp {^(f|ht)tp(s|)://} $word] && \
                    ![regexp {://([^/:]*:([^/]*@|\d+(/|$))|.*/\.)} $word]} {
                    foreach site $ignoredSites {
                        if {![string match *$site* $word]} {
                            set last $time

                            # Enable HTTPS support.
                            ::http::register https 443 [list urltitle::socket]
                            set title [urltitle::parse $word]

                            # Disable HTTPS support.
                            ::http::unregister https

                            # Sends text to the server, like 'putserv', but it uses a different queue intended for sending messages to channels or people.
                            puthelp "PRIVMSG $chan :$title"

                            break
                        }
                    }
                }
            }
        }
        return 1
    }

    proc parse {url} {
        set title ""
        variable timeout

        if {[info exists url] && [string length $url]} {
            if {[catch {set http [::http::geturl $url -timeout $timeout]} results]} {
                putlog "Connection to $url failed"
            } else {
                if {[::http::status $http] == "ok" } {
                    set data [::http::data $http]

                    if {[catch {set doc [dom parse -html -simple $data]} results]} {
                        # Remove HTML comments.
                        regsub -all {<!--.*?-->} $data {} data

                        # Remove everything except <head></head> content.
                        regexp -nocase {<head>.*?</head>} $data match
                        #regsub -nocase {.*?<head>} $data {} data
                        #regsub -nocase {</head>.*?} $data {} data

                        regexp -nocase {<title>(.*?)</title>} $data match title
                        #set title [regsub -all -nocase {\s+} $title " "]
                        set title [string trim $title]
                    } else {
                        set root [$doc documentElement]
                        set title [string trim [[$root selectNodes {//head/title[1]/text()}] data]]
                        $doc delete
                    }
                } else {
                    putlog "Connection to $url failed"
                }
                http::cleanup $http
            }
        }
        return $title
    }
    putlog "URL title parser v$scriptVersion"
}

有人知道为什么会这样吗?我认为问题出在 set title [urltitle::parse $word] 但我没弄对。

问题正式出现在您未显示的代码中,urltitle::parse,因为您的模式正确匹配两个 URL。确定这是否真的是真的一个好方法是在交互式 shell.

中尝试 运行 少量代码

我猜实际问题是 youtu.be URL 生成一个 HTTP redirect 到另一个 URL (或一个非常喜欢); Tcl 的 http 库不会为您处理重定向——它会是顶层的更高层(如果 thisurltitle 代码的来源,那么我可以看到它没有这样做)——结果导致某些东西以一种令人讨厌的方式窒息。

如果您只想支持这些 youtu.be 网址,您可以在传递 URL 到 urltitle::parse:

    ...
    regsub {^https?//youtu\.be/([^?/]*)$} $word {https://www.youtube.com/watch?} word
    set title [urltitle::parse $word]
    ...

regsub 受到严密保护,因此它不会转换任何不应该转换的内容,但这种方法不可扩展;您不能为每个网站都引入自己的重写规则!相反,它需要为您正确处理各种重定向。这是 urltitle 代码中的实际错误。