#!/usr/bin/perl # fifobot - open a FIFO to read messages line by line, and send to a Jabber group chat # Copyright (C) 2006-7 Toby Thain # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA use strict; use warnings; use Net::Jabber; $#ARGV >= 4 or die "usage: fifo_path room_name user pass jabber_server [nick]\n"; my($fifo,$room,$user,$pass,$jserver,$nick) = @ARGV; my $con = new Net::Jabber::Client(); #$con->{DEBUG}->Init(level=>3, file=>"stdout"); # optional debugging switch my $recip = $room.'@conference.'.$jserver; #$con->SetCallBacks(message=>\&InMessage, presence=>\&InPresence, iq=>\&InIQ); sub Msg($) { chomp($_[0]); $con->MessageSend(to=>$recip, type=>"groupchat", body=>$_[0]); } sub Stop { Msg("/me got $_[0], leaving"); unlink $fifo; # remove FIFO - see remarks above sleep(1); # give the last message a chance $con->Disconnect(); if(defined $_[0]){ die $_[0] } exit(1); } # IMPORTANT: if your server requires particular settings, you may need to # add 'port=>NUMBER' and/or 'tls=>1' to these parameters. my $status = $con->Connect(hostname=>$jserver); defined $status or die "can't connect ($!)"; $SIG{HUP} = $SIG{TERM} = $SIG{INT} = \&Stop; my @result = $con->AuthSend(username=>$user, password=>$pass, resource=>"$0-$$"); # some servers may need AuthIQAuth() above instead of AuthSend() (Wildfire?) $result[0] eq "ok" or Stop("$result[0] $result[1]"); # create FIFO on the fly - see remarks above # with these permissions, this script must run as same effective user or group as # the writing process (usually a post-commit hook, run as web server user) system("mkfifo -m 660 $fifo") == 0 || die "can't create fifo"; sysopen(FIFO, $fifo, Fcntl::O_RDONLY|Fcntl::O_NONBLOCK) or Stop("can't open $fifo"); $con->RosterGet(); # tell server to send presence info $con->PresenceSend(); # tell world that we are logged in # if the room has a password, add 'password=>"xxx"' below $con->MUCJoin(room=>$room, server=>"conference.$jserver", nick=>($nick||$user)); Msg("hello, I'm watching $fifo on ".`hostname -f`); while(defined($con->Process(1))) { my $line = ; if(defined $line) { chomp($line); Msg($line); } }