#!/usr/bin/php4 -q
<?

/*
RTGUJE by Elita (rozanski@sergiusz.com)
based on JPT written by: Jan Starzak <jstar@starzaki.eu.org>, Piotr Filipiak
*/

set_time_limit(0);

global $USRARRAY;
require("class.jabber.php");

$HOME = getenv("HOME");
$JABBER = new Jabber;

require("rtguje.config.php");

$filename=sprintf ("%s/%s",$HOME,$pipename);
if (!file_exists($filename))
    posix_mkfifo($filename,0700);
$pipe=fopen ($filename,"r",NONBLOCK) or die ("Pipe problem :(\n");

echo "RTGUJE - RTG mikro jabber engine\n";
echo "Homedir: ".$HOME."\n";

while(1) {

if( ! $JABBER->connected) {
    echo "Connect: ";
    $JABBER->Connect();
    
    if($JABBER->connected) {
	echo "ok\nAuth: ";
	$JABBER->SendAuth();
	echo "ok\n";
    
	$JABBER->SendPresence(NULL, NULL, "online");
	go_roster();
	$li="";
	}
    else {
	echo "false\n";
	save_roster();
	sleep(10);
	}
    }

while($JABBER->connected) {
    go_roster();
    save_roster();
    for ($j = 0;  $j < 15 ; $j++) {
	$linia=fread($pipe,4096);
	if($linia) {
	    for ($k=0 ; $k<strlen($linia); $k++) {
		if($linia[$k]!="\n")
		    $li.=$linia[$k];
		else {
		    $res=split(" ",$li);

		    $cmd=$res[0];
		    $to=$res[1];

		    $body="";

		    for($ia=2; $ia < count($res); $ia++) {
    			if($ia==2)
	    		    $body=$res[$ia];
			else
		            $body.=" ".$res[$ia];
			}
		    
		    printf("[%s][%s][%s]\n",$cmd,$to,$body);
		    if($cmd=="chat") {
			$JABBER->SendMessage($to, "chat", NULL, array("body" => $body));
			}
		    if($cmd=="msgsbj") {
			$subject=$body;
			}		    
		    if($cmd=="msgsnd") {
			$JABBER->SendMessage($to, "normal", NULL, 
			    array("body" => $body, "subject" => $subject));
			}
		    if($cmd=="add") {
			$JABBER->RosterAddUser($to, NULL, $body);
			go_roster();
			save_roster();
			}
		    if($cmd=="del") {
			$JABBER->RosterRemoveUser($to, "a");
			del_local_roster($to);
			save_roster();
			}
		    if($cmd=="sub") {
			$JABBER->Subscribe($to);
			}
		    if($cmd=="uns") {
			$JABBER->Unsubscribe($to);
			}
		    if($cmd=="sar") {
			$JABBER->SubscriptionAcceptRequest($to);
			}
		    if($cmd=="sdr") {
		        $JABBER->SubscriptionDenyRequest($to);
			}
		    if($cmd=="stt") {
			if( ($to != "online") &&
			    ($to != "offline") &&
			    ($to != "away") &&
			    ($to != "xa") &&
			    ($to != "chat") &&
			    ($to != "dnd") )
				$to="online";
			$JABBER->SendPresence(NULL, NULL, $body ,$to);
			echo "Moj status: $to $body\n";
			}
		    $li="";
		    }
		}
	    }
	$JABBER->CruiseControl(2);
	}
    } //while connected
} // while 1

$JABBER->Disconnect();

function save_roster() {
    global $HOME;
    global $USRARRAY;
    global $JABBER;
    
    $filename=sprintf ("%s/.gg/wap-roster",$HOME);
    if(($file=fopen ($filename,"w"))) {
	if($JABBER->connected)
	    fputs($file,"C\n");
	else
	    fputs($file,"D\n");
	for ($i = 0; $i < count($USRARRAY); $i++) {
	    if ($USRARRAY[$i]["id"] != "") {
		$line=sprintf("%s;%s;%s;%s;%s\n",
		    $USRARRAY[$i]["id"],
		    $USRARRAY[$i]["name"],
		    $USRARRAY[$i]["show"],
		    $USRARRAY[$i]["group"],
		    $USRARRAY[$i]["status"]
		    );
		fputs($file,$line);
		printf("save: %s",$line);
		}
	    }
	fclose($file);
	}
    }

function add_local_roster($jid,$name,$group) {
    global $USRARRAY;
    
    $space=(-1);
    for ($i = 0; $i < count($USRARRAY); $i++) {
	if($jid == $USRARRAY[$i]["id"]) {
	    $USRARRAY[$i]["name"] = $name;
	    $USRARRAY[$i]["group"] = $group;
	    printf("old: %s\n",$jid);
	    return 0;
	    }
	if($USRARRAY[$i]["id"] == "")
	    $space=$i;
	}

    if ( $space != (-1) ) $i = $space;

    $USRARRAY[$i]["id"] = $jid;
    $USRARRAY[$i]["name"] = $name;
    $USRARRAY[$i]["show"] = "offline";
    $USRARRAY[$i]["group"] = $group;	
    printf("add: %s\n",$jid);
    }

function del_local_roster($jid) {
    global $USRARRAY;
    
    for ($i = 0; $i < count($USRARRAY); $i++) {
	if($jid == $USRARRAY[$i]["id"]) {
	    $USRARRAY[$i]["id"] = "";
	    printf("del: %s\n",$jid);
	    return 0;
	    }
	}
    }
    
function go_roster() {
    global $JABBER;
    global $USRARRAY;
    
    $JABBER->RosterUpdate();
    $roster = $JABBER->roster;

    for ($i = 0; $i < count($roster); $i++) {
	printf("Roster:%s\n",$roster[$i]["jid"]);
	add_local_roster($JABBER->StripJID($roster[$i]["jid"]),$roster[$i]["name"],$roster[$i]["group"]);
	}
    }

function Handler_message_normal($message) {
    global $JABBER;
    global $HOME;

    $from = $JABBER->StripJID($JABBER->GetInfoFromMessageFrom($message));
    $body = $JABBER->GetInfoFromMessageBody($message);
    echo "[".$from."]".$body."\n";
    $filename=sprintf ("%s/.gg/wap%s_%s",$HOME,date("H:is"),$from);
    if(($file=fopen ($filename,"w"))) {
	$line=sprintf("%s;%s\n",$from,$body);
	fputs($file,$line);
	fclose($file);
	}
    }

function Handler_message_chat($message) {
    global $JABBER;
    global $HOME;

    $from = $JABBER->StripJID($JABBER->GetInfoFromMessageFrom($message));
    $body = $JABBER->GetInfoFromMessageBody($message);
    echo "[".$from."]".$body."\n";
    $filename=sprintf ("%s/.gg/wap%s_%s",$HOME,date("H:is"),$from);
    if(($file=fopen ($filename,"w"))) {
	$line=sprintf("%s;%s\n",$from,$body);
	fputs($file,$line);
	fclose($file);
	}
    }

function update_status($JABBER, $presence, $when)
  {
    global $USRARRAY;
    $from = $JABBER->StripJID($JABBER->GetInfoFromPresenceFrom($presence));
    $show = $JABBER->GetInfoFromPresenceShow($presence);
    $status = $JABBER->GetInfoFromPresenceStatus($presence);
    $type = $JABBER->GetInfoFromPresenceType($presence);
//    echo "[".$from."]".$show." ".$status."\n";
    for ($i = 0; $i <= count($USRARRAY); $i++) {
//      printf("%s == %s\n",$from,$USRARRAY[$i]["id"]);
      if ($USRARRAY[$i]["id"] == $from) {
        if ($show == "") {
          $USRARRAY[$i]["show"] = "online";
        } else {
          $USRARRAY[$i]["show"] = $show;
        }
        $USRARRAY[$i]["status"] = $status;
        echo "[".$from."]".$USRARRAY[$i]["show"]." ".$status."\n";
      }
    }
  }
  
  function Handler_presence_available($presence)
  {
    global $JABBER;
    update_status($JABBER, $presence, "online");
  }
  
  function Handler_presence_subscribe($presence)
  {
    global $JABBER;
    update_status($JABBER, $presence, "online");
  }
  
  function Handler_presence_unavailable($presence)
  {
    global $JABBER;
    update_status($JABBER, $presence, "offline");
  }
  
  function Handler_presence_unsubscribe($presence)
  {
    global $JABBER;
    update_status($JABBER, $presence, "offline");
  }
  
  function Handler_presence_unsubscribed($presence)
  {
    global $JABBER;
    update_status($JABBER, $presence, "offline");
  }
  
  function Handler_presence_probe($presence)
  {
    global $JABBER;
    update_status($JABBER, $presence, "offline");
  }

?>
