[LON-CAPA-cvs] cvs: loncom / lond

foxr lon-capa-cvs@mail.lon-capa.org
Tue, 14 Sep 2004 11:45:04 -0000


foxr		Tue Sep 14 07:45:04 2004 EDT

  Modified files:              
    /loncom	lond 
  Log:
  - Factor out common get/eget code.
  - Fix dispatch to get_profile_entry_encrypted (had not changed the name correctly.
  
  
Index: loncom/lond
diff -u loncom/lond:1.254 loncom/lond:1.255
--- loncom/lond:1.254	Tue Sep 14 06:27:22 2004
+++ loncom/lond	Tue Sep 14 07:45:04 2004
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # lond "LON Daemon" Server (port "LOND" 5663)
 #
-# $Id: lond,v 1.254 2004/09/14 10:27:22 foxr Exp $
+# $Id: lond,v 1.255 2004/09/14 11:45:04 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -57,7 +57,7 @@
 my $status='';
 my $lastlog='';
 
-my $VERSION='$Revision: 1.254 $'; #' stupid emacs
+my $VERSION='$Revision: 1.255 $'; #' stupid emacs
 my $remoteVERSION;
 my $currenthostid="default";
 my $currentdomainid;
@@ -1065,6 +1065,50 @@
     
 }
 
+#   read_profile
+#
+#   Returns a set of specific entries from a user's profile file.
+#   this is a utility function that is used by both get_profile_entry and
+#   get_profile_entry_encrypted.
+#
+# Parameters:
+#    udom       - Domain in which the user exists.
+#    uname      - User's account name (loncapa account)
+#    namespace  - The profile namespace to open.
+#    what       - A set of & separated queries.
+# Returns:
+#    If all ok: - The string that needs to be shipped back to the user.
+#    If failure - A string that starts with error: followed by the failure
+#                 reason.. note that this probabyl gets shipped back to the
+#                 user as well.
+#
+sub read_profile {
+    my ($udom, $uname, $namespace, $what) = @_;
+    
+    my $hashref = &tie_user_hash($udom, $uname, $namespace,
+				 &GDBM_READER());
+    if ($hashref) {
+        my @queries=split(/\&/,$what);
+        my $qresult='';
+	
+	for (my $i=0;$i<=$#queries;$i++) {
+	    $qresult.="$hashref->{$queries[$i]}&";    # Presumably failure gives empty string.
+	}
+	$qresult=~s/\&$//;              # Remove trailing & from last lookup.
+	if (untie %$hashref) {
+	    return $qresult;
+	} else {
+	    return "error: ".($!+0)." untie (GDBM) Failed";
+	}
+    } else {
+	if ($!+0 == 2) {
+	    return "error:No such file or GDBM reported bad block error";
+	} else {
+	    return "error: ".($!+0)." tie (GDBM) Failed";
+	}
+    }
+
+}
 #--------------------- Request Handlers --------------------------------------------
 #
 #   By convention each request handler registers itself prior to the sub 
@@ -2493,32 +2537,17 @@
    
     my ($udom,$uname,$namespace,$what) = split(/:/,$tail);
     chomp($what);
-    my $hashref = &tie_user_hash($udom, $uname, $namespace,
-				 &GDBM_READER());
-    if ($hashref) {
-        my @queries=split(/\&/,$what);
-        my $qresult='';
-	
-	for (my $i=0;$i<=$#queries;$i++) {
-	    $qresult.="$hashref->{$queries[$i]}&";    # Presumably failure gives empty string.
-	}
-	$qresult=~s/\&$//;              # Remove trailing & from last lookup.
-	if (untie(%$hashref)) {
-	    &Reply($client, "$qresult\n", $userinput);
-	} else {
-	    &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
-		    "while attempting get\n", $userinput);
-	}
+
+    my $replystring = read_profile($udom, $uname, $namespace, $what);
+    my ($first) = split(/:/,$replystring);
+    if($first ne "error") {
+	&Reply($client, "$replystring\n", $userinput);
     } else {
-	if ($!+0 == 2) {               # +0 coerces errno -> number 2 is ENOENT
-	    &Failure($client, "error:No such file or ".
-		    "GDBM reported bad block error\n", $userinput);
-	} else {                        # Some other undifferentiated err.
-	    &Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
-		    "while attempting get\n", $userinput);
-	}
+	&Failure($client, $replystring." while attempting get\n", $userinput);
     }
     return 1;
+
+
 }
 &register_handler("get", \&get_profile_entry, 0,1,0);
 
@@ -2548,42 +2577,32 @@
    
     my ($cmd,$udom,$uname,$namespace,$what) = split(/:/,$userinput);
     chomp($what);
-    my $hashref = &tie_user_hash($udom, $uname, $namespace,
-				 &GDBM_READER());
-    if ($hashref) {
-        my @queries=split(/\&/,$what);
-        my $qresult='';
-	for (my $i=0;$i<=$#queries;$i++) {
-	    $qresult.="$hashref->{$queries[$i]}&";
-	}
-	if (untie(%$hashref)) {
-	    $qresult=~s/\&$//;
-	    if ($cipher) {
-		my $cmdlength=length($qresult);
-		$qresult.="         ";
-		my $encqresult='';
-		for(my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
-		    $encqresult.= unpack("H16", 
-					 $cipher->encrypt(substr($qresult,
-								 $encidx,
-								 8)));
-		}
-		&Reply( $client, "enc:$cmdlength:$encqresult\n", $userinput);
-	    } else {
-		&Failure( $client, "error:no_key\n", $userinput);
+    my $qresult = read_profile($udom, $uname, $namespace, $what);
+    my ($first) = split(/:/, $qresult);
+    if($first ne "error") {
+	
+	if ($cipher) {
+	    my $cmdlength=length($qresult);
+	    $qresult.="         ";
+	    my $encqresult='';
+	    for(my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
+		$encqresult.= unpack("H16", 
+				     $cipher->encrypt(substr($qresult,
+							     $encidx,
+							     8)));
 	    }
+	    &Reply( $client, "enc:$cmdlength:$encqresult\n", $userinput);
 	} else {
-	    &Failure($client, "error: ".($!+0)." untie(GDBM) Failed ".
-		    "while attempting eget\n", $userinput);
-	}
+		&Failure( $client, "error:no_key\n", $userinput);
+	    }
     } else {
-	&Failure($client, "error: ".($!+0)." tie(GDBM) Failed ".
-		"while attempting eget\n", $userinput);
+	&Failure($client, "$qresult while attempting eget\n", $userinput);
+
     }
     
     return 1;
 }
-&register_handler("eget", \&GetProfileEntryEncrypted, 0, 1, 0);
+&register_handler("eget", \&get_profile_entry_encrypted, 0, 1, 0);
 #
 #   Deletes a key in a user profile database.
 #