我正在尝试构建一个IRC机器人,它在私有通道中告诉我我想知道的每个提交消息。但是我很难得到per
#!/bin/bash
REPOS="$1"
REV="$2"
# call bot with arguments reposname, revison and commit message in one string
/usr/bin/perl /home/user/repo/svn_irc_bot.pl "$REPOS" "$REV"
# all checks passed, so allow the commit
exit 0然后,调用Perl-Skript:
#!/usr/bin/perl -w
# see http://www.javalinux.it/wordpress/2009/10/15/writing-an-irc-bot-for-svn-commit-notification/
# see http://oreilly.com/pub/h/1964
use strict;
# We will use a raw socket to connect to the IRC server.
use IO::Socket;
my $repos = $ARGV[0];
my $rev = $ARGV[1];
my $commit = `/usr/bin/svnlook log $repos`;
my $user = `whoami`;
# The server to connect to and our details.
my $server = "irc.server.com";
my $nick = "bot2";
my $login = "bot2";
# The channel which the bot will join.
# my $channel = "#channel";
# Connect to the IRC server.
my $sock = new IO::Socket::INET(PeerAddr => $server,
PeerPort => 6667,
Proto => 'tcp') or
die "Can't connect\n";
# Log on to the server.
print $sock "NICK $nick\r\n";
print $sock "USER $login 8 * :Perl IRC Hacks Robot\r\n";
# Read lines from the server until it tells us we have connected.
while (my $input = <$sock>) {
# Check the numerical responses from the server.
if ($input =~ /004/) {
# We are now logged in.
print $sock "PRIVMSG mynick : $user: $repos r$rev -- $commit\n";
last;
}
elsif ($input =~ /433/) {
die "Nickname is already in use.";
}
}
sleep(5);
print $sock "QUIT bye... \n";
sleep(5);
close($sock);所以,我的机器人确实可以连接,并且可以和我对话...
如果我手动启动shell脚本,只会发送一个单词( $user中的字符串,甚至连下面的冒号都不会发送)。
如果脚本由SVN通过提交调用,则看起来$user和$commit字符串为空,$user和$repos被传输...
我想是我的用法出了点问题...但我想不出来。也许有人能给我点提示?
发布于 2010-02-09 08:58:03
您只得到没有前面冒号的"$user“的原因是因为您正在捕获来自whoami的输出,并且该输出包括一个换行符。该换行符被解释为要发送的字符串的末尾。在使用$user之前,尝试使用chomp $user去掉换行符。
如果脚本被SVN通过提交调用,看起来$user和$commit字符串是空的,$user和$repos被传输...
我假设您的意思是通过SVN传输$user和$commit为空,但传输的是$rev和$repos,因为这是有意义的……
来自svnlook的$commit也会遇到同样的问题,但是因为提交是在消息的末尾进行的,所以只有在消息中有换行符时才会有问题。例如,如果消息的第一行是换行符,您将看不到任何内容。为此,我建议删除消息中的所有换行符,可能使用y/\n//。
至于在钩子中保留$user为空,这取决于您如何使用svn。whoami完全有可能找不到用户id,例如,如果运行钩子的进程没有与任何登录相关联。在这种情况下,您可能需要另一种确定用户的方法,例如svnlook info的第一行输出。
发布于 2010-02-09 03:48:46
您只使用了whoami,而不是命令的完整路径,但是不能保证当脚本被SVN调用时,$PATH环境变量将包含与您的shell相同的目录。
您应该检查的另一件事是,SVN在其下运行脚本的uid是否具有使用svnlook和访问存储库的权限。
我不确定你的问题是否源于此,但这肯定是一个很好的开始。
发布于 2010-02-09 03:50:42
我不太确定,但试着做两件事。
首先检查您正在运行的文件的权限。如果他们没有权限运行whoami和svnlookup,那么你就大错特错了。其次,只要给qx(cmd)一次机会,而不是cmd。
https://stackoverflow.com/questions/2224323
复制相似问题