01 #!/usr/bin/perl 02 ########################################### 03 # agent - Jabber agent behind firewall 04 # Mike Schilli, 2004 (m@perlmeister.com) 05 ########################################### 06 use warnings; 07 use strict; 08 09 use Net::Jabber qw(Client); 10 use Log::Log4perl qw(:easy); 11 use LWP::Simple; 12 13 Log::Log4perl->easy_init( 14 { level => $DEBUG, 15 file => '>>/tmp/agent.log' }); 16 17 my $JABBER_USER = 'mikes-agent-receiver'; 18 my $JABBER_PASSWD = "*****"; 19 my $JABBER_SERVER = "jabber.org"; 20 my $JABBER_PORT = 5222; 21 22 our %ROSTER; 23 24 my $c = Net::Jabber::Client->new(); 25 26 $c->SetCallBacks( 27 28 message => sub { 29 my $msg = $_[1]; 30 31 DEBUG "Message '", $msg->GetBody(), 32 "' from ", $msg->GetFrom(); 33 34 if(! exists 35 $ROSTER{$msg->GetFrom()}) { 36 INFO "Denied (not in roster)"; 37 return; 38 } 39 40 DEBUG "Running ", $msg->GetBody(); 41 my $rep = run_cmd($msg->GetBody()); 42 chomp $rep; 43 DEBUG "Result: ", $rep; 44 45 $c->Send($msg->Reply(body => $rep)); 46 }, 47 48 onauth => sub { 49 DEBUG "Auth"; 50 %ROSTER = $c->RosterGet(); 51 $c->PresenceSend(); 52 }, 53 54 presence => sub { 55 # Ignore all subscription requests 56 }, 57 ); 58 59 DEBUG "Connecting ..."; 60 61 $c->Execute( 62 hostname => $JABBER_SERVER, 63 username => $JABBER_USER, 64 password => $JABBER_PASSWD, 65 resource => 'Script', 66 ); 67 68 $c->Disconnect(); 69 70 ########################################### 71 sub run_cmd { 72 ########################################### 73 my($cmd) = @_; 74 75 # Find out external IP 76 if($cmd eq "ip") { 77 return LWP::Simple::get( 78 "http://perlmeister.com/cgi/whatsmyip" 79 ); 80 } 81 82 # Print Load 83 if($cmd eq "load") { 84 return `/usr/bin/uptime`; 85 } 86 87 # Switch bedroom light on/off 88 if($cmd =~ /^lamp\s+(on|off)$/) { 89 my $rc = system("/usr/bin/lamp $1"); 90 return $rc == 0 ? "ok" : 91 "not ok ($rc)"; 92 } 93 94 return "Unknown Command"; 95 }