#!/usr/bin/perl
# addact.cgi
# Add Action for STTA Monitor
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL 

require './stta-lib.pl';
if ( ! $access{'monitor'} )   { &redirect("index.cgi"); }
if ( $config{'has_monitor'} ne "true" )  { &redirect("index.cgi"); }
&ReadParse();
if ( !$in{aliasAct} ) { &redirect("mon_actdef.cgi"); }
if ( !$in{monTarget} ) { &redirect("mon_actdef.cgi"); }
if ( !$in{monCondition} ) { &redirect("mon_actdef.cgi"); }
if ( !$in{monAction} ) { &redirect("mon_actdef.cgi"); }
do "ldap-lib.pl";
&headerstta($text{'index_monitor_act'}, "", "monitor_act" ,undef,1,1,$text{headpage_rigth} ); 
$aliasAct=$in{aliasAct};
$monTarget=$in{monTarget};
$monCondition=$in{monCondition};
$monAction=$in{monAction};
$monConditionValue=$in{monConditionValue};
$monScript=$in{monScript};
$monHow=$in{monHow};
$descrip=$in{descrip};
$monWhen=$in{monWhen};
print "<table width=100% border>\n";
print "<tr $tb> <td><b>$text{'titidx_monitor_defact'}  </b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";
%h_act=&list_actions();
if ( $h_act{$aliasAct}->{'alias'} eq $aliasAct ) {
         $err_msg=$text{'list_err'};
         &footHere;
         exit;
}
if ( $monHow eq "task" ) { 
    foreach $arr ("mins", "hours", "days", "months", "weekdays") {
	if ($in{"all_$arr"}) {
		# All mins/hrs/etc.. chosen
		$job->{$arr} = "*";
	}
	elsif (defined($in{$arr})) {
		# Need to work out and simplify ranges selected
		undef(@range);
		undef(@newrange);
		@range = split(/\0/, $in{$arr});
		@range = sort { $a <=> $b } @range;
		$start = -1;
		for($i=0; $i<@range; $i++) {
			if ($i && $range[$i]-1 == $range[$i-1]) {
				# ok.. looks like a range
				if ($start < 0) { $start = $i-1; }
			}
			elsif ($start < 0) {
				# Not in a range at all
				push(@newrange, $range[$i]);
			}
			else {
				# End of the range.. add it
				$newrange[@newrange - 1] =
					"$range[$start]-".$range[$i-1];
				push(@newrange, $range[$i]);
				$start = -1;
			}
		}
		if ($start >= 0) {
			# Reached the end while in a range
			$newrange[@newrange - 1] =
				"$range[$start]-".$range[$i-1];
		}
		$job->{$arr} = join(',' , @newrange);
	}
	else {
                print "<tr><p><b>ERROR: ".eval("\$text{'edit_$arr'}")."</b></tr>\n";
                &footHere;
                exit;
	}
    }
 }
 if ( $monTarget eq "user" ) {
    $objectAct=$in{'username'}." ou=".$in{'userou'};
    %ou=&list_ous();
    $oukey=$in{'userou'};
    if  ( $ou{$oukey}->{'alias'} ne $oukey ) {
          print "<hr>\n";
          print "<b> Error $in{username}</b>\n";
          print "<hr>\n";
          &footHere;
          exit;
    }
    $dn_ou=$ou{$oukey}->{'obj'};
    $ens_ou=$ou{$oukey}->{'ens'};
    $organizationname=$ou{$oukey}->{'name'};
    $dn = "cn=$in{username},$dn_ou,".$config{ldap_base};
    $surname="";
    $ldap_server=1;
    &connect ();
    $cur_entry=&sel_ldap_obj($dn,"cn=*");
    if ( !defined($cur_entry)  || $cur_entry->dn ne $dn) {
         $err_msg=" Error: $in{username}\n";
         &html_errshow("ldap");
         &footHere; exit ;
    }
    %entryObj=&get_ldap_obj($dn,$cur_entry);
    $surname = $entryObj{sn};
    $objectAct="cn=$in{username},$dn_ou";
 } elsif  ( $monTarget eq "ou" ) {
     $objectAct=$in{'user_ou'};
 } elsif  ( $monTarget eq "host" ) {
     $objectAct=$in{'hostname'};
 } elsif  ( $monTarget eq "grphosts" ) {
     $objectAct=$in{'usergrphosts'};
 } elsif  ( $monTarget eq "app" ) {
     $objectAct=$in{'appname'};
 } elsif  ( $monTarget eq "grpapps" ) {
     $objectAct=$in{'grpappsname'};
 }
 if ( $monScript eq "none" ) { $monScript="-" };
 if ( $aliasAct ne "" ) {
    $lin_act=$aliasAct."::".$descrip."::".$monTarget."::".
             $objectAct."::".$monCondition."::".$monConditionValue."::".
             $monAction."::".$monScript."::";
    if ( $monHow eq "task" ) { 
           foreach $arr ("mins", "hours", "days", "months", "weekdays") {
                 $lin_act=$lin_act.$job->{$arr}." ";
           }
    } else {
       $lin_act=$lin_act.$monWhen;
    }
    $lin_act=$lin_act."::".$monHow."::";
    &lock_file($config{'act_list'});
    open(FILEACT, ">>$config{'act_list'}");
         print FILEACT "$lin_act\n";
    close (FILEACT);
    $str=$text{'user_made'}.$aliasAct;
    if ( $monTarget eq "user" ) {
      $str=$str." ->  ".$in{username}."  (".$organizationname.")";
    } else {
      $str=$str." ->  ".$objectAct;
    }
    &unlock_file($config{'act_list'});
    &additional_log('addact.cgi', "act", $str);
    &webmin_log($config{'act_list'}, "act", "write",$lin_act);
    print "<tr><p><b>".$str."</b></tr>\n";
    print "<tr></tr>\n";
 }
 &footHere;
 exit;
