views:

444

answers:

3

Hello

I'm trying to build an IRC Bot which tells me in a private channel every commit-message which I want to know about. But I have trouble to get 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

Then, the invoked 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);

So, my Bot does connect, and can talk to me...

If I start the shell Script manually, only one word (the string inside $user, not even the following colon) is sent.

If the script is invoked by SVN trough a commit, is seems like the $user and $commit strings are empty, $user and $repos are transmitted...

I guess that something is wrong with my usage of whoami and svnlook... But I can't figure it out. Maybe someone can give me a hint?

+1  A: 

You're using just whoami instead of the full path to the command, but there's no assurance that when the script is invoked by SVN, the $PATH env variable will contain the same directories as the one your shell has.

Another thing you should check is that the uid under which SVN runs the script has permissions to use svnlook and access the repository.

Not sure if your problem comes from this, but it sure is a good place to start looking at.

Miguel Ventura
Hi, tried the full path for whoami, but that doesn't help... The filesystem permissions for the repo are 775 -- enought, I suppose? I myself can execute /usr/bin/svnlook... How can I check the UID of my script?
marvin2k
A: 

I'm not sure about it but try two things.

First check the permissions of the files you're running. If they don't have permissions to run whoami and svnlookup then you're pretty much screwed. Second just give qx(cmd) a shot instead of cmd.

tsenart
+1  A: 

The reason you only get "$user" with no preceding colon is because you are capturing output from whoami, and that output includes a newline. That newline is interpreted as the end of your string to sent. Try chomp $user to get rid of the newline before using $user.

If the script is invoked by SVN trough a commit, is seems like the $user and $commit strings are empty, $user and $repos are transmitted...

I'm going to assume that you mean through SVN $user and $commit are empty, but $rev and $repos are transmitted, since that would make sense...

You'll have the same problem with $commit from svnlook, but because the commit comes at the end of your message, you'll only have a problem if your message has newlines in it. For example, if the first line of your message is a newline, you won't see anything. For that, I'd recommend removing all of the newlines from the message, probably with y/\n//.

As to $user being blank from within the hook, that depends on how you are using svn. It's entirely possible that whoami isn't able to find the user id, for example, if the process running the hook is not associated with any login. You will probably need another method of determining the user in that case, such as the first line of output from svnlook info.

Adam Bellaire
Ok, thanks a lot, that helps. My main mistake was a missing colon after "PRIVMSG mynick"
marvin2k