commit 3f7a1eee88b3b787935ab307a5eb41ecea7c3466
Author: Marc Dequenes <duck@Orfeo.duckcorp.org>
Date:   Mon Feb 16 14:39:42 2009 +0100

    [evol] add abstraction level above LDAP layer #1

diff --git a/postman b/postman
index 5d0b101..8fe3735 100755
--- a/postman
+++ b/postman
@@ -25,11 +25,11 @@ logger.outputters = Log4r::StderrOutputter.new('')
 logger.level = Log4r::WARN
 #logger.level = Log4r::DEBUG
 
-class Person < ActiveLdap::Base
+class LdapPerson < ActiveLdap::Base
   ldap_mapping :dn_attribute => 'uid', :prefix => '', :classes => ['person', 'extInetOrgPerson']
 end
 
-class DnsDomain < ActiveLdap::Base
+class LdapDnsDomain < ActiveLdap::Base
   ldap_mapping :dn_attribute => 'cn', :prefix => '', :classes => ['genericDomain']
 
   def managers
@@ -55,6 +55,37 @@ ldap_config = {
 
 ActiveLdap::Base.establish_connection(ldap_config)
 
+class DnsDomain < Delegator
+  attr_reader :name
+
+  def initialize(name)
+    raise "invalid zone name" unless self.is_valid?(name)
+
+    # may not exist (if creating a new one)
+    begin
+      @ldap = LdapDnsDomain.find(name)
+    rescue
+      @ldap = nil
+    end
+  end
+
+  def self.is_valid?(name)
+    name =~ /^[a-z0-9.-]+\.[a-z]{2,4}$/
+  end
+
+  def is_valid?(name)
+    self.class.is_valid?(name)
+  end
+
+  def managed?
+    not @ldap.nil?
+  end
+
+  def __getobj__
+    @ldap
+  end
+end
+
 #Socket.gethostname
 
 #
@@ -72,8 +103,12 @@ class CommandParser
       # stop processing when detecting message signature
       break if line == "-- "
 
-      logger.info "### Executing command: #{sline}"
-      execute_cmd(user, sline)
+      logger.info "Executing command: #{sline}"
+      begin
+        execute_cmd(user, sline)
+      rescue
+        logger.info "Command failed: " + $!
+      end
     end
   end
 
@@ -89,8 +124,8 @@ class CommandParser
       case cmdline.shift.upcase
       when "INFO"
 	if cmdline.empty?
-          list = DnsDomain.find(:all, :attribute => 'manager', :value => user.dn)
-	  logger.info "### User is manager of the following zones: " + list.collect{|z| z.cn }.join(", ")
+          list = LdapDnsDomain.find(:all, :attribute => 'manager', :value => user.dn)
+	  logger.info "User is manager of the following zones: " + list.collect{|z| z.cn }.join(", ")
 	else
 	  ok = false
 	end
@@ -98,20 +133,16 @@ class CommandParser
         case cmdline.shift.upcase
         when "ZONE"
           zone = cmdline.shift.downcase
-          if zone =~ /^[a-z0-9.-]+\.[a-z]{2,4}$/
-	    logger.info "### User requesting zone content for '#{zone}'"
-	    begin
-	      domain = DnsDomain.find(zone)
-	      if domain.managers.include? user.dn
-	        logger.info "### User is manager of the zone"
-	      else
-		logger.info "### User is not allowed to manage the zone"
-	      end
-	    rescue
-	      logger.info "### Zone not managed"
+	  dom = DnsDomain.new(zone)
+	  logger.info "User requesting zone content for '#{zone}'"
+	  if dom.managed?
+	    if dom.managers.include? user.dn
+	      logger.info "User is manager of the zone"
+	    else
+	      logger.info "User is not allowed to manage the zone"
 	    end
 	  else
-            logger.info "### Invalid zone name specified (#{zone})"
+	    logger.info "Zone not managed"
 	  end
 	else
 	  ok = false
@@ -125,7 +156,7 @@ class CommandParser
     end
 
     if not ok
-      logger.info "### Command not recognized: #{cmdstr}"
+      logger.info "Command not recognized: #{cmdstr}"
     end
   end
 end
@@ -176,10 +207,10 @@ module CyborgHood
 	sig_check = mail.verify_pgp_signature()
         if sig_check.status == 0
           logger.info "Mail content was properly signed by key #{sig_check.fingerprint}"
-      	  list = Person.find(:all, :attribute => 'keyFingerPrint', :value => sig_check.fingerprint)
+      	  list = LdapPerson.find(:all, :attribute => 'keyFingerPrint', :value => sig_check.fingerprint)
       	  case list.size
       	  when 0
-      	    logger.info "Mail is from an unknow person"
+      	    logger.info "Mail is from an unknown person"
       	  when 1
       	    user = list.first
       	    logger.info "Mail is from user #{user.uid} (#{user.cn})"
