#!/usr/bin/perl use Getopt::Std; use IO::Socket; use Net::hostent; # for OO version of gethostbyaddr use strict; use Carp; our( $opt_h, $opt_s, $opt_c ); getopts("hsc:"); printHelp() if $opt_h; printHelp() if scalar @ARGV > 2; getopts("hc:"); my @valid_input = qw( temp weather weather-icon physstat xmms: ); my $EOL = "\015\012"; my $port = "4564"; my $client = $opt_c if defined $opt_c; my $server = 1; if($opt_c){ my $query = join " ", @ARGV; # print "opening connection to $opt_c\n"; my $remote = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $client, PeerPort => "$port", ); unless ($remote) { die "cannot connect to daemon on $client" } $remote->autoflush(1); print $remote "$query\n"; my $result = join "", <$remote>; #chomp $count; close ($remote) || die "close: $!"; print $result; exit; }elsif($server){ my $lock = "/var/lock/statusd.pid"; if( -e $lock ){ die "Lockfile exists in $lock. Is another instance running?"; } open LOCK,">$lock" or die "Error opening lockfile $lock: $!"; print LOCK "$$\n"; close LOCK; sub catch_zap { my $signame = shift; unlink( $lock ) && die; } $SIG{TERM} = \&catch_zap; $SIG{HUP} = \&catch_zap; $SIG{INT} = \&catch_zap; print "starting status daemon on port $port.\n"; my $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $port, Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; print "[Server $0 accepting clients]\n"; while ($client = $server->accept()) { $client->autoflush(1); my $hostinfo = gethostbyaddr($client->peeraddr); my $query = <$client>; chomp $query; if( $query eq "temp" ){ print $client &get_ibutton()."\n"; }elsif( $query eq "physstat" ){ print $client &get_physstat()."\n"; }elsif( $query eq "weather-icon" ){ print $client &get_weather_icon()."\n"; }elsif( $query eq "weather" ){ print $client &get_weather()."\n"; }elsif( $query =~ /^xmms(?:\:\s*(\d+))?/i ){ my $id = defined $1 ? $1 : 0; print $client &get_xmms($id)."\n"; } close $client; } } sub get_x10{ # blah. } sub get_xmms{ my( $id ) = @_; my $base = "/tmp/xmms-info."; my $xmms_stat = ""; if( open XMMS, "$base$id" ){ while(){ $xmms_stat .= $_; } close XMMS; }else{ warn "cannot open: $base$id for reading: $!"; } return $xmms_stat; } sub get_physstat{ use PhysStat; # set this if you want logs to be anywhere but your homedir my $logbase = "/xanatos/text/.logs"; my $user = "steve"; set_user( $user ); set_logfile( "$logbase/$user.physlog" ); return scalar get_status; } sub get_ibutton{ use IButton::Temp; my $ibutton_temp = new IButton::Temp( mode => 'c' ); return $ibutton_temp->get_temp(); } sub get_weather_icon{ use utf8; my $cond = ""; my $txtstat = get_weather(); $cond = "\x{2600}" if $txtstat =~ /clear|sun/i; $cond = "\x{2601}" if $txtstat =~ /cloud|overcast/i; $cond = "\x{2602}" if $txtstat =~ /rain|shower/i; $cond = "\x{2603}" if $txtstat =~ /snow|flurry/i; return $cond; } sub get_weather{ my $txtstat = `txtstat %s\\; %t`; return $txtstat; } sub printHelp{ my $accept = join " \n", @valid_input; die "Usage: $0 [-c HOST] [parameters] transmit status information over TCP/IP arguments: -h help -c HOST the host of the server to connect to parameters: one of the following: $accept Steve Pomeroy " }