#!/usr/bin/perl -w 

# copyright Wulf Coulmann <scripts at gpl.coulmann dot de>

# GNU GPL
# http://www.gnu.org/licenses/gpl.html
#
# Download me here: http://gpl.coulmann.de/

# Based on the client example from teh Net::XMPP package.

# Version 0.0.1 (this is basic and proof of concept)

use Net::XMPP;
use strict;

#use Data::Dumper;

# ---------- config ----------------

my $debug = 0;                     # swich to 1, to make the script more verbose
my $server = 'JABBER_SERVER';
my $port = '5222';
my $username = 'USER_NAME';
my $password = 'PASSWORD';
my $osd_cfg = '/home/system/etc/osd.cfg'; 
# this file holds the default config for osd_cat, we read only first line.
# use xfontsel to determine your -f values
# first line schould look similar to (without # at the begining)
#
# /usr/bin/osd_cat -f -*-lucidatypewriter-bold-*-*-*-*-120-*-*-*-*-*-* -p top -c green -d 0 -A center 


# if you like to run autostart via scripts or cron you need propper enviroment to connect your display
# on most systems this should run without changeing the next two lines
$ENV{DISPLAY} = ':0.0';
$ENV{XAUTHORITY} = '/home/'.$ENV{'USER'}.'/.Xauthority';

# ---------- go for it -------------

# read some system settings
open CFG, '<'.$osd_cfg || print 'unable to open '.$osd_cfg;
my @cfg = <CFG>;
my $osd_command = $cfg[0];
$osd_command =~ s/\n//;
close CFG;

open CFG, '</etc/hostname' || print '/etc/hostname ';
@cfg = <CFG>;
my $resource = $cfg[0];
$resource =~ s/\n//;
close CFG;




$SIG{HUP} = \&Stop;
$SIG{KILL} = \&Stop;
$SIG{TERM} = \&Stop;
$SIG{INT} = \&Stop;

my $Connection = new Net::XMPP::Client();

$Connection->SetCallBacks(message=>\&InMessage,
                          # presence=>\&InPresence,
                          # iq=>\&InIQ,
                          );

my $status = $Connection->Connect(hostname=>$server,
                                  port=>$port,
                                  tls=>1 );

if (!(defined($status)))
{
    print "ERROR:  Jabber server is down or connection was not allowed.\n";
    print "        ($!)\n";
    exit(0);
}

my @result = $Connection->AuthSend(username=>$username,
                                   password=>$password,
                                   resource=>$resource);

if ($result[0] ne "ok")
{
    print "ERROR: Authorization failed: $result[0] - $result[1]\n";
    exit(0);
}

print "Logged in to $server:$port...\n" if $debug;

$Connection->RosterGet();

print "Getting Roster to tell server to send presence info...\n" if $debug;

$Connection->PresenceSend();

print "Sending presence to tell world that we are logged in...\n" if $debug;

while(defined($Connection->Process())) { }

print "ERROR: The connection was killed...\n";

exit(0);


sub Stop
{
    print "Exiting...\n";
    $Connection->Disconnect();
    system('echo -n \'osd_client disconnected\' | '.$osd_command.'&');
    exit(0);
}


sub InMessage
{
    my $sid = shift;
    my $message = shift;
    
    my $type = $message->GetType();
    my $fromJID = $message->GetFrom("jid");
    
    my $from = $fromJID->GetUserID();
    my $resource = $fromJID->GetResource();
    my $subject = $message->GetSubject();
    my $body = $message->GetBody();
    if ($debug) {
      print "InMessage\n";
      print "===\n";
      print "Message ($type)\n";
      print "  From: $from ($resource)\n";
      print "  Subject: $subject\n";
      print "  Body: $body\n";
      print "===\n";
      print $message->GetXML(),"\n";
      print "===\n";
    }

    system("killall osd_cat 2>/dev/null");
    system('echo -n \''.$body.'\' | '.$osd_command.'&');
 

}


sub InIQ
{
    my $sid = shift;
    my $iq = shift;
    print "InIQ\n";
    
    my $from = $iq->GetFrom();
    my $type = $iq->GetType();
    my $query = $iq->GetQuery();
    my $xmlns = $query->GetXMLNS();
    print "===\n";
    print "IQ\n";
    print "  From $from\n";
    print "  Type: $type\n";
    print "  XMLNS: $xmlns";
    print "===\n";
    print $iq->GetXML(),"\n";
    print "===\n";
}

sub InPresence
{
    my $sid = shift;
    my $presence = shift;
    
    print "InPresence\n";
    my $from = $presence->GetFrom();
    my $type = $presence->GetType();
    my $status = $presence->GetStatus();
    print "===\n";
    print "Presence\n";
    print "  From $from\n";
    print "  Type: $type\n";
    print "  Status: $status\n";
    print "===\n";
    print $presence->GetXML(),"\n";
    print "===\n";
}


