--- loncom/interface/lonmsg.pm	2000/10/19 14:20:17	1.1
+++ loncom/interface/lonmsg.pm	2000/10/20 10:59:02	1.2
@@ -10,13 +10,14 @@
 #
 # 10/05 Gerd Kortemeyer)
 #
-# 10/19 Gerd Kortemeyer
+# 10/19,10/20 Gerd Kortemeyer
 
 package Apache::lonmsg;
 
 use strict;
 use Apache::lonnet();
-
+use vars qw($msgcount);
+use HTML::TokeParser;
 
 # ===================================================================== Package
 
@@ -26,10 +27,15 @@ sub package {
     $message=~s/\>/\&gt\;/g;
     $subject=~s/\</\&lt\;/g;
     $subject=~s/\>/\&gt\;/g;
-    return '<sendername>'.$ENV{'user.name'}.'</sendername>'.
+    my $now=time;
+    $msgcount++;
+    $msgid=$now.'_'.$ENV{'user.name'}.'_'.
+           $ENV{'user.domain'}.'_'.$msgcount.'_'.$$;
+    return $msgid,
+           '<sendername>'.$ENV{'user.name'}.'</sendername>'.
            '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
            '<subject>'.$subject.'</subject>'.
-	   '<time>'.localtime().'</time>'.
+	   '<time>'.localtime($now).'</time>'.
 	   '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
 	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
@@ -41,26 +47,104 @@ sub package {
 	   '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
 	   '<role>'.$ENV{'request.role'}.'</role>'.
 	   '<resource>'.$ENV{'request.filename'}.'</resource>'.
+           '<msgid>'.$msgid.'</msgid>'.
 	   '<message>'.$message.'</message>';
 }
 
+# ================================================== Unpack message into a hash
+
+sub unpackage {
+    my $message=shift;
+    my %content=();
+    my $parser=HTML::TokeParser->new(\$message);
+    my $token;
+    while ($token=$parser->get_token) {
+       if ($token->[0] eq 'S') {
+	   my $entry=$token->[1];
+           my $value=$parser->get_text('/'.$entry);
+           $content{$entry}=$value;
+       }
+    }
+    return %content;
+}
+
 # =============================== Automated message to the author of a resource
 
 sub author_res_msg {
     my ($filename,$message)=@_;
+    unless ($message) { return 'empty'; }
     $filename=&Apache::lonnet::declutter($filename);
     my ($domain,$author,@dummy)=split(/\//,$filename);
     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
     if ($homeserver ne 'no_host') {
        my $id=unpack("%32C*",$message);
-       &Apache::lonnet::put('res_msgs',$filename.'_'.$id => package($message));
+       my $msgid;
+       ($msgid,$message)=package($filename,$message);
+       return &Apache::lonnet::put(
+                              'nohist_res_msgs',$filename.'_'.$id => $message);
     }
+    return 'no_host';
 }
 
 # ================================================== Critical message to a user
 
 sub user_crit_msg {
     my ($user,$domain,$subject,$message)=@_;
+# Check if allowed missing
+    my $status='';
+    my $msgid='undefined';
+    unless (($message)&&($user)&&($domain)) { $status='empty'; };
+    my $homeserver=&Apache::lonnet::homeserver($user,$domain);
+    if ($homeserver ne 'no_host') {
+       my $msgid;
+       ($msgid,$message)=package($filename,$message);
+       $status=&Apache::lonnet::cput('critical',$msgid => $message);
+    } else {
+       $status='no_host';
+    }
+    &Apache::lonnet::logthis(
+      '<font color=yellow>INFO: Sending critical email '.$msgid.
+      ', log status: '.
+      &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
+                         $ENV{'user.home'},
+      'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
+      .$status).'</font>');
+    return $status;
+}
+
+# =================================================== Critical message received
+
+sub user_crit_received {
+    my $message=shift;
+
+}
+
+# ======================================================== Normal communication
+
+sub user_normal_msg {
+    my ($user,$domain,$subject,$message)=@_;
+# Check if allowed missing
+    my $status='';
+    my $msgid='undefined';
+    unless (($message)&&($user)&&($domain)) { $status='empty'; };
+    my $homeserver=&Apache::lonnet::homeserver($user,$domain);
+    if ($homeserver ne 'no_host') {
+       my $msgid;
+       ($msgid,$message)=package($filename,$message);
+       $status=&Apache::lonnet::cput('nohist_email',$msgid => $message);
+    } else {
+       $status='no_host';
+    }
+    &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
+                         $ENV{'user.home'},
+      'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
+    return $status;
+}
+
+# ================================================= Main program, reset counter
+
+sub BEGIN {
+    $msgcount=0;
 }
 
 1;