stuartfreeman has asked for the wisdom of the Perl Monks concerning the following question:

I searched through the site and found one person trying to do something similar to this but the discussion didn't really apply. What I'm trying to do is pull a funny quote from a file of quotes by a user and say it when they join a channel. I use an external call to fortune to actually grab the quote. Everything works great so far, except that the script only seems to run every other time a user joins (1st, 3rd, 5th...). Does anyone see something I don't that may be causing this? If so, what?

use strict; use vars qw($NAME $VERSION %IRSSI); use Irssi qw(command_bind signal_add_first); $VERSION = "1.03"; %IRSSI = ( authors => "Stuart Freeman", contact => "stuart\@tyro.homelinux.com", name => "greet", description => "Greets your friends with a message from a file nam +ed after them.", license => "GPL" ); sub greet ($server, $channel, $nick, $address) { my ($server, $channel, $nick, $address) = @_; $address =~ s/\~(.*)\@.*/$1/; my $filename = glob( "~/.quotes/$address"); if (-r $filename) { my @quote=split(/\n/,`/usr/games/fortune $filename`); foreach my $quote (@quote) { $server->command('msg '.$channel.' '.$nick.'> +'.$quote); } } } signal_add_first 'message join' => 'greet';

Replies are listed 'Best First'.
Re: irssi greeter
by Juerd (Abbot) on Dec 10, 2004 at 19:28 UTC

    sub greet ($server, $channel, $nick, $address)

    Well, that's exactly equivalent to sub greet ($$$$), with the exception that it will break as soon as other symbols are added to the list of meaningful prototype characters. I don't understand why you use this at all. Irssi certainly doesn't require you to use a prototype, and if it does anything, I don't see how it could make anything better.

    $address =~ s/\~(.*)\@.*/$1/;

    Realise that you will use this with a system call and eventually as a file name. Do not remove what you want gone, but instead throw EVERYTHING away, except what is known to be safe. You should make the ~ optional, by the way. And there is no need to escape it. Try something like:

    my ($fn) = $address =~ /^\W?(\w+)/; return unless defined $fn and length $fn; # I would not test -f or -r or -e manually. # If open failed, the reason is in $!. open my $fh, "~/.quotes/$fn" or do { warn "Cannot open $fn for reading ($!).\n"; return; } local $/ = "\n%\n"; my $quote; rand($.) < 1 && ($quote = $_) while <>; chomp $quote; $window->command("msg $channel $nick> $quote") for split /\n/, $quote;
    Note that this is untested. See perldoc -q random\ line for the rand($.) trick and perlvar for $/.

    I have no idea why your code would greet only every other time. It could have to do with the ~, but that's not very likely.

    Please note that you should really LEARN PERL BEFORE WRITING PROGRAMS THAT OTHER PEOPLE CAN CONTROL (for example, by joining a channel, visiting a web page or connecting to a daemon you wrote). Several things indicate that you do not know Perl well enough.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      Thank you for your help. I realize why it's bad to allow others to control my scripts when I'm still learning. In my defence I'm only using this on a small channel where I have a high level of trust for the people I chat with (we are a group of friends who know each other IRL).

      Thank you again,
      Stu
Re: irssi greeter
by Animator (Hermit) on Dec 10, 2004 at 19:32 UTC

    If you preview your text, and you look before the from then you read the following thing:

    Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided....

    I strongly suggest you read it and use the proper tag, instead of messing up the layout of this page!