stephan: devel/kolab dirservnotify,1.1.1.1,1.2

cvs at intevation.de cvs at intevation.de
Wed May 12 10:35:26 CEST 2004


Author: stephan

Update of /kolabrepository/devel/kolab
In directory doto:/tmp/cvs-serv7979

Modified Files:
	dirservnotify 
Log Message:
Extended functionality: debug and verbose mode. The script is able to send only 
changes that have been made after a certain timestamp. (--debug, --verbose, --timestamp)


Index: dirservnotify
===================================================================
RCS file: /kolabrepository/devel/kolab/dirservnotify,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- dirservnotify	3 May 2004 14:04:04 -0000	1.1.1.1
+++ dirservnotify	12 May 2004 08:35:24 -0000	1.2
@@ -2,23 +2,38 @@
 use strict;
 use Getopt::Long;
 use URI;
+use IO::File;
 use Net::LDAP;
 use Net::LDAP::LDIF;
 use Net::LDAP::Entry;
 use Kolab;
 use Kolab::DirServ;
 use Kolab::LDAP;
-use vars qw($name $mode);
+use Kolab::Util;
+use vars qw($name $mode $verbose $debug $timestamp);
 
 GetOptions(
     "name=s"    => \$name,
-    "mode=s"    => \$mode
+    "mode=s"    => \$mode,
+    "verbose"    => \$verbose,
+    "debug" => \$debug,
+    "timestamp" => \$timestamp
 );
 
-exit 0 if !defined $mode;
+if (!defined $mode) { 
+   print "No mode defined! Please specify update/delete/new.\n";
+   exit 1;
+}
 
-my $ldap;
+if (!defined $name) { 
+	print "Collecting all entries in ".$Kolab::config{'base_dn'}."\n";
+	$name = "*";
+} else {
+	print "Query filter for LDAP CN entries: ".$name."\n";
+
+}
 
+my $ldap;
 $ldap = Kolab::LDAP::create(
     $Kolab::config{'ldap_ip'},
     $Kolab::config{'ldap_port'},
@@ -26,25 +41,91 @@
     $Kolab::config{'bind_pw'}
 ) || die;
 
+my $queryfilter = "(&(mail=*)(cn=$name)";
+my $lastmodify;
+my $lastmodifyepoch;
+my $filename = $Kolab::config{'prefix'}."/var/kolab/dirserv_lastupdate";
+
+if ($timestamp) {
+	#Get the old timestamp
+        my $FILE;
+        if (!($FILE = IO::File->new($filename, 'r'))) {
+	   $lastmodify = "20000000000000Z";
+	} else {
+	   while (<$FILE>) {
+		$lastmodify = $_ if !defined $lastmodify;
+		chomp($lastmodify);
+	   }
+           $FILE->close;
+	   undef $FILE;
+	}
+				
+        print "Last modifyTimestamp  = $lastmodify\n";
+	$lastmodifyepoch = ldapDateToEpoch($lastmodify);
+	$queryfilter .= "(modifyTimestamp>=$lastmodify))";
+} else {
+	$queryfilter .= ")";
+}
+
+$verbose && print $queryfilter."\n";
+
 my $mesg = $ldap->search(
     base    => $Kolab::config{'base_dn'},
     scope   => 'one',
-    filter  => "(&(cn=*$name*)(mail=*))"
+    filter  => $queryfilter,
+    attrs => ['*', 'modifyTimestamp']
 );
 $mesg->code && die $mesg->error;
 
+sub dumpentry
+{
+   my $entry = shift;
+   print $entry->dn(),"\n";
+   foreach my $attr ( $entry->attributes ) {
+      print "   ",$attr," : ",join( " ", $entry->get_value( $attr ) ), "\n";
+   }
+}
+
 my $max = $mesg->count;
 $max || die "Nothing to do!";
 
 for (my $i = 0; $i < $max; $i++) {
     my $entry = $mesg->entry($i);
+    my $modifyTimestamp = $entry->get_value( 'modifyTimestamp' );
+    my $modifiedtime = ldapDateToEpoch($modifyTimestamp);
+    if ($max == 1 && $lastmodify eq $modifyTimestamp) {
+         die "Nothing to do, this entry has already been sent. Adjust $filename if you want...";
+    }
+    
+    if ($timestamp && $modifiedtime > $lastmodifyepoch) {
+       $lastmodifyepoch = $modifiedtime;
+       $lastmodify = $modifyTimestamp;
+    }
+
+    $timestamp && $entry->delete ( 'modifyTimestamp' );
+   
+    $verbose && dumpentry($entry);
+
     if ($mode =~ /add/) {
-        Kolab::DirServ::notifyNew($entry);
+        $debug || Kolab::DirServ::notifyNew($entry);
     }
     if ($mode =~ /del/) {
-        Kolab::DirServ::notifyRemove($entry);
+        $debug || Kolab::DirServ::notifyRemove($entry);
     }
     if ($mode =~ /upd/) {
-        Kolab::DirServ::notifyModify($entry);
+        $debug || Kolab::DirServ::notifyModify($entry);
+
     }
+}
+
+if ($timestamp) {
+  print "Lastchange: $lastmodify\n";
+  my $FILE;
+  if (!($FILE = IO::File->new($filename, O_RDWR|O_CREAT, 0644))) {
+     print "Unable to write to file `$filename'\n";
+     exit 1;
+  } else {
+     print $FILE $lastmodify;
+     close $FILE;
+  }
 }





More information about the commits mailing list