[LON-CAPA-cvs] cvs: modules /sharrison/hosts_tab manage_hosts.pl

harris41 lon-capa-cvs@mail.lon-capa.org
Sun, 25 Aug 2002 18:09:24 -0000


harris41		Sun Aug 25 14:09:24 2002 EDT

  Modified files:              
    /modules/sharrison/hosts_tab	manage_hosts.pl 
  Log:
  successfully merging e-mail record with hosts.tab information
  
  
Index: modules/sharrison/hosts_tab/manage_hosts.pl
diff -u modules/sharrison/hosts_tab/manage_hosts.pl:1.1 modules/sharrison/hosts_tab/manage_hosts.pl:1.2
--- modules/sharrison/hosts_tab/manage_hosts.pl:1.1	Sun Aug 25 10:54:04 2002
+++ modules/sharrison/hosts_tab/manage_hosts.pl	Sun Aug 25 14:09:24 2002
@@ -25,8 +25,18 @@
 =cut
 
 # ==================================================================== Modules.
-# Probably CGI.
-# Mail parser.
+use CGI; # Common Gateway Interface module.
+use Mail::Box::Mbox; # Handles the mbox file format (i.e. folder is in a file).
+
+# Version check here.
+if ($CGI::VERSION < 2.752)
+  {
+    &error("CGI module is a version lower than 2.752");
+  }
+if ($Mail::Box::Mbox::VERSION < 2.021)
+  {
+    &error("Mail::Box::Mbox is a version lower than 2.021");
+  }
 
 # ===================================== Determine if production or development.
 # Time-out (when the user could choose "development") and then automatically
@@ -37,16 +47,116 @@
 # If in https:// mode with the right htpasswd user, then allow for edits.
 
 # ============================================= Read in the relevant hosts.tab.
-open(IN,'<hosts.tab');
+my %entries;
+my %replace_entries;
+my %insert_entries;
+my $mode = 'production';
+if ($mode eq 'production')
+  {
+    open(IN,'<production_hosts.tab');
+  }
+else
+  {
+    open(IN,'<development_hosts.tab');
+  }
 while(<IN>)
   {
-    chomp; # remove new-line
-    push(@entries,$_); # build array with hosts.tab entries
+    chomp; # Remove new-line.
+    my @fields = split(/\:/,$_,6); # Parse colon-separated fields foreach line.
+    my $hostid = shift(@fields); # Get lonHostID (unique identifier).
+    $entries{$hostid} = join(':',@fields); # Build hash with hosts.tab entries.
+#    print "HOSTID: $hostid\n";
   }
 close(IN);
 
+# ==================================== Output view and allow for text download.
+
+
+# ======== If only a "user", then return, otherwise, go onto manager functions.
+
+
+# ========================================================== MANAGER FUNCTIONS.
+
 # ========== Scan in the e-mail and query the hosts.tab manager about response.
 
+$ENV{'MAIL'} = 'installrecord';
+# Open mailbox with read-write access.
+my $folder = Mail::Box::Mbox->new(folder => $ENV{'MAIL'}, access => 'rw');
+
+foreach my $message ($folder->messages)
+  {
+    if ($message->isMultipart) # Delete weird e-mails.
+      {
+	$message->delete(); # Delete it.
+        next; # Okay, now look at the next e-mail in the mailbox.
+      }
+    if ($message->size > 10000) # Delete SPAM.
+      {
+        $message->delete(); # Delete it.
+        next; # Okay, now look at the next e-mail in the mailbox.
+      }
+    # Parse useful information.
+    my $subject = $message->get('Subject'); # Subject of an e-mail.
+    my $body = $message->body; # Body of an e-mail.
+    my $date = $message->date; # Date of an e-mail.
+    my $from = $message->from->format; # E-mail address that was sent from.
+
+    # Store for processing in next step.
+# DEBUGGING
+#    print "Subject: $subject\n";
+#    print "Body: $body\n";
+#    print "Date: $date\n";
+#    print "From: ".$from->format."\n";
+    $subject=~s/^\s+//; $subject=~s/\s+$//;
+    $body=~s/^\s+//; $body=~s/\s+$//;
+    my @subject_info=split(/\:/,$subject);
+    my @body_info=split(/\:/,$body);
+
+    # What action was requested?
+    my $action=$subject_info[0];
+    my $cluster=$subject_info[1];
+    my $cluster2=$body_info[1];
+
+    # Determine machine id name.
+    my $hostid=$subject_info[2];
+    # Make sure that the 
+    if (($hostid ne $body_info[2]) or
+	($hostid ne $body_info[4]))
+      {
+        $message->delete(); # Delete it.
+        next; # Okay, now look at the next e-mail in the mailbox.
+      }
+    if ((($action eq 'REPLACE') or ($action eq 'INSERT')) and
+	($cluster eq $mode))
+      {
+        $replace_entries{$hostid} = "[$date~~$from]".$body
+	    if $action eq 'REPLACE';
+        $insert_entries{$hostid} = "[$date~~$from]".$body
+	    if $action eq 'INSERT';
+      }
+  }
+$folder->close();
+
+# Output information in concert with HTML form elements, and original
+# hosts.tab information.
+
+my %keylist;
+
+foreach my $entry ((keys %entries),(keys %replace_entries),
+		   (keys %insert_entries))
+  {
+    $keylist{$entry}=1;
+  }
+
+foreach my $hostid (keys %keylist)
+  {
+    print "**** HOSTID: $hostid ****\n";
+    print "PRESENT INFORMATION\n";
+    print "CURRENT: $entries{$hostid}\n";
+    print "INSERT REQUEST: $insert_entries{$hostid}\n";
+    print "REPLACE REQUEST: $replace_entries{$hostid}\n";
+  }
+
 # The manager can either:
 #            "okay", "postpone", "delete" or "hand-edit" the hosts.tab request.
 
@@ -56,3 +166,5 @@
 open(OUT,'>hosts_new.tab');
 
 close(OUT);
+
+# === E-mail all LON-CAPA system administrators with the new updated hosts.tab.