简体   繁体   中英

Perl: IRC Notice Printing

Having a few issues with the below code..

my $file=File::Tail->new("/var/log/messages");
while (defined(my $line=$file->read)) {
print $sock "NOTICE #logs $line";
}

As you can see I'm tailing the servers message logs (which works) and printing it into an IRC socket as a NOTICE, but for some reason it's only printing out the first word of each line into the channel - for example, it's only printing out 'Jan' as that's the month.

Can anyone help with this?

[06:55:48]   IRCBOT (~IRCBOT@10.1.0.4) joined the channel.
[06:56:00] -IRCBOT-  Jan
[06:56:00] -IRCBOT-  Jan
[06:56:00] -IRCBOT-  Jan
[06:56:00] -IRCBOT-  Jan
[06:56:00] -IRCBOT-  Jan
[06:56:02] -IRCBOT-  Jan

Many Thanks in Advance!

EDIT: Just in case it matters, this is how I'm connecting to the IRC server..

use IO::Socket;
use File::Tail;

my $file=File::Tail->new("/var/log/messages");

my $server = "irc.example.co.uk";
my $nick = "IRCBOT";
my $login = "IRCBOT";
my $channel = "#logs";
my $sock = new IO::Socket::INET(PeerAddr => $server,
                                PeerPort => 6667,
                                Proto => 'tcp') or
                                    die "Can't connect\n";

You need to prefix the text string with a : character,

print $sock "NOTICE #logs :$line";

If you want to escape the "$line", you can do something like:

print $sock "NOTICE #logs :\x01$line\x01";

By default, the IRC protocol separates parameters by spaces, you need to include the leading semicolon before a text string to indicate it is the trailing parameter and should not be separated.

NOTICE #logs Jan 1st 2014

is treated as a command plus 4 parameters,

NOTICE #logs :Jan 1st 2014 ... more stuff ... long line

is treated as a command plus one parameter that extends to CR LF (possibly including trailing whitespace)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM