#!/usr/bin/perl # jann - a simple command line Jabber admin message client # (c) 2001 DJ Adams, April 2001 # # 2005, Petr Pisar # Net::Jabber:EscapeXML replaced by XML::Stream::EscapeXML # Adds support for sending message to the admin of the server by option # "-t admin". use Net::Jabber; use Getopt::Std; use strict; use vars qw($opt_h $opt_q $opt_s $opt_u $opt_p $opt_t); my $optionfile = $ENV{HOME}."/.jann"; getopts('hqs:u:p:t:'); # help, quiet, server, user, password, type # Help if (defined $opt_h) { print usage(); exit; } # Announce resources my %resource = ( online => "/announce/online", motd => "/announce/motd", delete => "/announce/motd/delete", update => "/announce/motd/update", admin => "", ); # Ur-default options my %option = ( server => "localhost:5222", user => "admin", type => "online", # online, motd, delete, update, admin ); # Read basic options from .jann file if (open(JANN, $optionfile)) { while () { s/#.*$//; # remove comments next if /^\s*$/; # and empty lines chomp; my ($parm, $value) = split(/\s+/, $_, 2); $option{$parm} = $value; } } # Options specified on command line override $option{server} = $opt_s if $opt_s; $option{user} = $opt_u if $opt_u; $option{type} = $opt_t if $opt_t; $option{pass} = $opt_p if $opt_p; # Default port if none specified $option{server} = $option{server}.":5222" unless $option{server} =~ m/:/; # Abort if bad admin type die "Bad admin type.\n".usage() unless $option{type} =~ m/^(online|motd|delete|update|admin)$/i; # Abort if we haven't been given a message my $message; my $no_pass_input; unless ($option{type} eq "delete") { unless ($message = shift) { $message = join("", <>); $no_pass_input = 1; } die "Must specify a message\n" unless $message; } # Ask for password if none given unless ($option{pass}) { die "Specify password in .jann file or with -p option\n" if $no_pass_input; print "Password: "; system "stty -echo"; $option{pass} = ; system "stty echo"; chomp $option{pass}; print "\n"; } # Connect to Jabber server my ($host, $port) = split(":", $option{server}, 2); print "Connecting to $host:$port as $option{user}\n" unless $opt_q; my $c = new Net::Jabber::Client; $c->Connect( hostname => $host, port => $port, ) or die "Cannot connect to Jabber server at $option{server}\n"; my @result; eval { @result = $c->AuthSend( username => $option{user}, password => $option{pass}, resource => "jann", ); }; die "Cannot connect to Jabber server at $option{server}\n" if $@; if ($result[0] ne "ok") { die "Authorisation failed ($result[1]) for user $option{user} on $option{server}\n"; } print "Sending $option{type} message\n" unless $opt_q; # Set up announce XML # Magic addresses: # server/announce/online all online users # server/announce/motd motd # server/announce/motd/delete remove motd # server/announce/motd/update update motd # administrator of the server my $to = $host . $resource{$option{type}}; my $xml = qq[]; unless ($option{type} eq "delete") { # Subject line $xml .= qq[] . ($option{type} eq "online" || $option{type} eq "admin" ? "Admin Message" : "MOTD") . qq[]; # The message $xml .= qq[] . XML::Stream::EscapeXML($message) . qq[]; } $xml .= qq[]; # Send it! $c->SendXML($xml); # End tidily print "Disconnecting\n" unless $opt_q; $c->Disconnect; exit; sub usage { <]>] [-u ] [-p ] [-t ] [] where type is one of online/motd/delete/update/admin EO_USAGE } =pod =head1 NAME jann - a simple command-line admin announcement client for Jabber =head1 SYNOPSIS Send an announcement to everyone logged on: jann "I've had enough - I'm going home" jann < announce.txt Set a message-of-the-day (motd): jann -t motd "Jabber Rocks!" motd | jann -t motd Change the motd: jann -t update "Jabber still Rocks" Remove the motd: jann -t delete Send a message to the admin: jann -t admin "Security alert: Someone is doing something nasty" =head1 DESCRIPTION jann is a simple client written in Perl and using Net::Jabber that provides a comfortable command-line interface to the administrative announcement features of a Jabber server. You can send a message to all online users, create a motd, change it or remove it and send a message to the admin of the jabber server. Obviously you need to specify which Jabber server to connect to and your administration JID and password - you can specify these in a file in your home directory called .jann and the contents should look like this (minus the comments ;-): server hostname:port # hostname (and optional port) of Jabber server # (default: localhost:5222) user admin # admin user # (default: admin) pass secret # don't have to specify it here # (no default (!)) type online # specify nothing here or one of 'online' / 'motd' # / 'update' / 'delete' / 'admin' # (default: online) You can override (or specify) these values on the command line using switches: jann [-h] [-q] [-s ]>] [-u ] [-p ] [-t ] The -h switch shows usage help and quits. The -q switch forces jann to be quiet (no output to your terminal). If you just specify a hostname for the -s switch, port 5222 will be assumed. If you invoke jann and pass a message like this (as a string that comes after any switches), you can leave off the -p switch (and leave the 'pass' parameter out of your ~/.jann file) and you'll be prompted for a password. You can also use jann in a script environment where you might want to pipe or echo a message to be sent: echo "something or other" | jann last | head | jann jann -t motd < /etc/motd and so on. Note: In each of these latter cases (where you pipe or redirect the message to jann and don't specify a message on the command line after any switches) you can specify switches, and _must_ have specified a password either in ~/.jann or with the -p switch. =head1 USES Net::Jabber =head1 VERSION 0.2 (Fixed EscapeXML() and admin message added) =head1 DOWNLOAD =head1 AUTHOR DJ Adams, April 2001 Petr Pisar, October 2005 =cut