--- loncom/interface/lonmsg.pm	2007/05/02 20:26:08	1.203
+++ loncom/interface/lonmsg.pm	2008/03/12 02:45:07	1.211
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Routines for messaging
 #
-# $Id: lonmsg.pm,v 1.203 2007/05/02 20:26:08 albertel Exp $
+# $Id: lonmsg.pm,v 1.211 2008/03/12 02:45:07 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -163,6 +163,17 @@ sub packagemsg {
     if (defined($recipid)) {
         $result.= '<recipid>'.$recipid.'</recipid>';
     }
+    if ($env{'form.can_reply'} eq 'N') {
+        $result .= '<noreplies>1</noreplies>';
+    }
+    if ($env{'form.reply_to_addr'}) {
+        my ($replytoname,$replytodom) = split(/:/,$env{'form.reply_to_addr'});
+        if (!($replytoname eq $env{'user.name'} && $replytodom eq $env{'user.domain'})) {
+            if (&Apache::lonnet::homeserver($replytoname,$replytodom) ne 'no_host') {
+                $result .= '<replytoaddr>'.$env{'form.reply_to_addr'}.'</replytoaddr>';
+            }
+        }
+    }
     return ($msgid,$result);
 }
 
@@ -288,18 +299,18 @@ sub sendnotification {
     my ($to,$touname,$toudom,$subj,$crit,$text,$msgid)=@_;
     my $sender=$env{'environment.firstname'}.' '.$env{'environment.lastname'};
     unless ($sender=~/\w/) { 
-	$sender=$env{'user.name'}.'@'.$env{'user.domain'};
+	$sender=$env{'user.name'}.':'.$env{'user.domain'};
     }
     my $critical=($crit?' critical':'');
+
     $text=~s/\&lt\;/\</gs;
     $text=~s/\&gt\;/\>/gs;
-    $text=~s/\<\/*[^\>]+\>//gs;
     my $url='http://'.
 	&Apache::lonnet::hostname(&Apache::lonnet::homeserver($touname,$toudom)).
       '/adm/email?username='.$touname.'&domain='.$toudom;
     my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid,
         $symb,$error) = &Apache::lonmsg::unpackmsgid($msgid);
-    my $coursetext;
+    my ($coursetext,$body,$bodybegin,$bodysubj,$bodyend);
     if ($fromcid ne '') {
         $coursetext = "\n".&mt('Course').': ';
         if ($env{'course.'.$fromcid.'.description'} ne '') {
@@ -312,14 +323,17 @@ sub sendnotification {
         }
         $coursetext .= "\n\n";
     }
-    my $body = $coursetext. 
-               &mt('You received a'.$critical.' message from [_1] in LON-CAPA.',$sender).' '.&mt('The subject is 
+    my @recipients = split(/,/,$to);
+    $bodybegin = $coursetext. 
+               &mt('You received a'.$critical.' message from [_1] in LON-CAPA.',$sender).' ';
+    $bodysubj = &mt('The subject is 
 
  [_1]
 
 ',$subj)."\n".
 '=== '.&mt('Excerpt')." ============================================================
-$text
+";
+    $bodyend = "
 ========================================================================
 
 ".&mt('Use 
@@ -327,14 +341,51 @@ $text
  [_1]
 
 to access the full message.',$url);
-    &sendemail($to,'New'.$critical.' message from '.$sender,$body);
+    my %userenv = &Apache::lonnet::get('environment',['notifywithhtml'],$toudom,$touname);
+    my $subject = &mt("'New' $critical message from ").$sender;
+ 
+    my ($blocked,$blocktext);
+    if (!$crit) {
+        my %setters;
+        my ($startblock,$endblock) = 
+            &Apache::loncommon::blockcheck(\%setters,'com',$touname,$toudom);
+        if ($startblock && $endblock) {
+            $blocked = 1;
+            my $showstart = &Apache::lonlocal::locallocaltime($startblock);
+            my $showend = &Apache::lonlocal::locallocaltime($endblock);
+            $blocktext = &mt('LON-CAPA messages sent to you between [_1] and [_2] will be inaccessible until the end of this time period, because you are a student in a course with an active communications block.',$showstart,$showend);
+        }
+    }
+    if ($userenv{'notifywithhtml'} ne '') {
+        my @htmlexcerpt = split(/,/,$userenv{'notifywithhtml'});
+        foreach my $addr (@recipients) {
+            if ($blocked) {
+                $body = $bodybegin."\n".$blocktext."\n".$bodyend;
+            } else {
+                my $sendtext = $text;
+                if (!grep/^\Q$addr\E/,@htmlexcerpt) {
+                    $sendtext =~ s/\<\/*[^\>]+\>//gs;
+                }
+                $body = $bodybegin.$bodysubj.$sendtext.$bodyend;
+            }
+            &sendemail($addr,$subject,$body);
+        }
+    } else {
+        if ($blocked) {
+            $body = $bodybegin."\n".$blocktext."\n".$bodyend;
+        } else {
+            $text =~ s/\<\/*[^\>]+\>//gs;
+            $body = $bodybegin.$bodysubj.$text.$bodyend;
+        }
+        &sendemail($to,$subject,$body);
+    }
 }
 # ============================================================= Check for email
 
 sub newmail {
     if ((time-$env{'user.mailcheck.time'})>300) {
         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
-        &Apache::lonnet::appenv('user.mailcheck.time'=>time);
+        &Apache::lonnet::appenv({'user.mailcheck.time'=>time});
         if ($what{'recnewemail'}>0) { return 1; }
     }
     return 0;
@@ -460,10 +511,8 @@ sub user_crit_msg_raw {
                                   undef,undef,undef,undef,undef,undef,undef,
                                   undef,$recipid);
        if ($sendback) { $packed_message.='<sendback>true</sendback>'; }
-       $status=&Apache::lonnet::critical(
-           'put:'.$domain.':'.$user.':critical:'.
-           &escape($msgid).'='.
-           &escape($packed_message),$homeserver);
+       $status=&Apache::lonnet::cput('critical', {$msgid => $packed_message},
+				     $domain,$user);
         if (defined($sentmessage)) {
             $$sentmessage = $packed_message;
         }
@@ -551,14 +600,24 @@ sub user_crit_received {
     my $msgid=shift;
     my %message=&Apache::lonnet::get('critical',[$msgid]);
     my %contents=&unpackagemsg($message{$msgid},1);
+    my $destname = $contents{'sendername'};
+    my $destdom = $contents{'senderdomain'};
+    if ($contents{'replytoaddr'}) {
+        my ($repname,$repdom) = split(/:/,$contents{'replytoaddr'});
+        if (&Apache::lonnet::homeserver($repname,$repdom) ne 'no_host') {
+            $destname = $repname;
+            $destdom = $repdom;    
+        }
+    }
     my $status='rec: '.($contents{'sendback'}?
-     &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
-                     &mt('Receipt').': '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.', '.$contents{'subject'},
-                     &mt('User').' '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.
-                     ' acknowledged receipt of message'."\n".'   "'.
-                     $contents{'subject'}.'"'."\n".&mt('dated').' '.
-                     $contents{'time'}.".\n"
-                     ):'no msg req');
+     &user_normal_msg($destname,$destdom,&mt('Receipt').': '.$env{'user.name'}.
+                      ' '.&mt('at').' '.$env{'user.domain'}.', '.
+                      $contents{'subject'},&mt('User').' '.$env{'user.name'}.
+                      ' '.&mt('at').' '.$env{'user.domain'}.
+                      ' acknowledged receipt of message'."\n".'   "'.
+                      $contents{'subject'}.'"'."\n".&mt('dated').' '.
+                      $contents{'time'}.".\n"
+                      ):'no msg req');
     $status.=' trans: '.
      &Apache::lonnet::put(
      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
@@ -590,10 +649,9 @@ sub user_normal_msg_raw {
                                      undef,$crsmsgid,$symb,$error,$recipid);
 
 # Store in user folder
-       $status=&Apache::lonnet::critical(
-           'put:'.$domain.':'.$user.':nohist_email:'.
-           &escape($msgid).'='.
-           &escape($packed_message),$homeserver);
+       $status=
+	   &Apache::lonnet::cput('nohist_email',{$msgid => $packed_message},
+				 $domain,$user);
 # Save new message received time
        &Apache::lonnet::put
                          ('email_status',{'recnewemail'=>time},$domain,$user);
@@ -613,9 +671,7 @@ sub user_normal_msg_raw {
 	   $$sentmessage = $packed_message;
        }
 # Notifications
-       my %userenv = &Apache::lonnet::get('environment',['notification',
-							 'permanentemail'],
-					  $domain,$user);
+       my %userenv = &Apache::loncommon::getemails($user,$domain);
        if ($userenv{'notification'}) {
 	   &sendnotification($userenv{'notification'},$user,$domain,$subject,0,
 			     $text,$msgid);
@@ -681,10 +737,15 @@ sub user_normal_msg {
 }
 
 sub process_sent_mail {
-    my ($msgsubj,$subj_prefix,$numsent,$stamp,$msgname,$msgdom,$msgcount,$context,$pid,$savemsg,$recusers,$recudoms,$baseurl,$attachmenturl,$symb,$error,$senderuname,$senderdom,$senderhome) = @_;
+    my ($msgsubj,$subj_prefix,$numsent,$stamp,$msgname,$msgdom,$msgcount,$context,$pid,$savemsg,$recusers,$recudoms,$baseurl,$attachmenturl,$symb,$error,$senderuname,$senderdom) = @_;
     my $sentsubj;
     if ($numsent > 1) {
         $sentsubj = $subj_prefix.' ('.$numsent.' sent) '.$msgsubj;
+    } else {
+        if ($subj_prefix) {
+            $sentsubj = $subj_prefix.' ';
+        }
+        $sentsubj .= $msgsubj;
     }
     $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"');
     my $sentmsgid = 
@@ -693,24 +754,21 @@ sub process_sent_mail {
         &packagemsg($msgsubj,$savemsg,undef,$baseurl,$attachmenturl,$recusers,
                     $recudoms,$sentmsgid,undef,undef,$symb,$error);
     my $status = &store_sent_mail($sentmsgid,$sentmessage,$senderuname,
-                                  $senderdom,$senderhome);
+                                  $senderdom);
     return $status;
 }
 
 sub store_sent_mail {
-    my ($msgid,$message,$senderuname,$senderdom,$senderhome) = @_;
+    my ($msgid,$message,$senderuname,$senderdom) = @_;
     if ($senderuname eq '') {
         $senderuname = $env{'user.name'};
     }
     if ($senderdom eq '') {
         $senderdom = $env{'user.domain'};
     }
-    if ($senderhome eq '') {
-        $senderhome = $env{'user.home'};
-    }
-    my $status =' '.&Apache::lonnet::critical(
-               'put:'.$senderdom.':'.$senderuname.':nohist_email_sent:'.
-               &escape($msgid).'='.&escape($message),$senderhome);
+    my $status =' '.&Apache::lonnet::cput('nohist_email_sent',
+					  {$msgid => $message},
+					  $senderdom,$senderuname);
     return $status;
 }