#!/usr/bin/perl
#
# echo "message to send" | smb_wall
#
# Write a message to all clients connected to a Samba server.
# The message is read from stdin.
# Based on a script by Keith Farrar <farrar@parc.xerox.com>
#
# Michiel Broek <mbroek@mbse.eu>
#
# This script 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, or (at your option) any
# later version.
#
# This script 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 MB BBS; see the file COPYING.  If not, write to the Free
# Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
#

$smbstatus = "/usr/bin/smbstatus -p";
$smbshout = "/usr/bin/smbclient -M";

open(PCLIST, "$smbstatus |") || die "$smbstatus failed!.\n$!\n";
while(<PCLIST>) {
  last if /^Locked files:/;
  split(' ', $_, 5);
  # do not accept this line if less then six fields
  next unless $_[4];
  # if you have A LOT of clients you may speed things up by
  # checking pid - no need to look further if this pid was already
  # seen;  left as an exercise :-)
  $client = $_[3];
  next if grep($_ eq $client, @clients); # we want this name once
  push(@clients, $client);
}
close(PCLIST);

# keep quiet and read message from stdin
@message = <>;

foreach(@clients) {
##    print "To $_:\n";
    if (open(SENDMSG,"|$smbshout $_ >/dev/null")) {
	print SENDMSG @message;
	close(SENDMSG);
    }
    else {
	warn "Cannot notify $_ with $smbshout:\n$!\n";
    }
}

exit 0;