Diff for /loncom/interface/lonmsg.pm between versions 1.181 and 1.193

version 1.181, 2006/04/11 14:17:23 version 1.193, 2006/12/27 20:42:12
Line 33  use Apache::lonnet; Line 33  use Apache::lonnet;
 use HTML::TokeParser();  use HTML::TokeParser();
 use Apache::lonlocal;  use Apache::lonlocal;
 use Mail::Send;  use Mail::Send;
   use LONCAPA qw(:DEFAULT :match);
   
 {  {
     my $uniq;      my $uniq;
Line 46  use Mail::Send; Line 47  use Mail::Send;
   
 sub packagemsg {  sub packagemsg {
     my ($subject,$message,$citation,$baseurl,$attachmenturl,      my ($subject,$message,$citation,$baseurl,$attachmenturl,
  $recuser,$recdomain,$msgid,$type,$crsmsgid)=@_;   $recuser,$recdomain,$msgid,$type,$crsmsgid,$symb,$error)=@_;
     $message =&HTML::Entities::encode($message,'<>&"');      $message =&HTML::Entities::encode($message,'<>&"');
     $citation=&HTML::Entities::encode($citation,'<>&"');      $citation=&HTML::Entities::encode($citation,'<>&"');
     $subject =&HTML::Entities::encode($subject,'<>&"');      $subject =&HTML::Entities::encode($subject,'<>&"');
Line 59  sub packagemsg { Line 60  sub packagemsg {
     my $course_context;      my $course_context;
     if (defined($env{'form.replyid'})) {      if (defined($env{'form.replyid'})) {
         my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid)=          my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid)=
                    split(/\:/,&Apache::lonnet::unescape($env{'form.replyid'}));                     split(/\:/,&unescape($env{'form.replyid'}));
         $course_context = $origcid;          $course_context = $origcid;
     }      }
     foreach my $key (keys(%env)) {      foreach my $key (keys(%env)) {
         if ($key=~/^form\.(rep)?rec\_(.*)$/) {          if ($key=~/^form\.(rep)?rec\_(.*)$/) {
             my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) =              my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) =
                                     split(/\:/,&Apache::lonnet::unescape($2));                                      split(/\:/,&unescape($2));
             $course_context = $origcid;              $course_context = $origcid;
             last;              last;
         }          }
Line 77  sub packagemsg { Line 78  sub packagemsg {
     my $msgcount = &get_uniq();      my $msgcount = &get_uniq();
     unless(defined($msgid)) {      unless(defined($msgid)) {
         $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},          $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},
                             $msgcount,$course_context,$$);                             $msgcount,$course_context,$symb,$error,$$);
     }      }
     my $result = '<sendername>'.$env{'user.name'}.'</sendername>'.      my $result = '<sendername>'.$env{'user.name'}.'</sendername>'.
            '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.             '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
Line 108  sub packagemsg { Line 109  sub packagemsg {
         for (my $i=0; $i<@{$recuser}; $i++) {          for (my $i=0; $i<@{$recuser}; $i++) {
             if ($type eq 'dcmail') {              if ($type eq 'dcmail') {
                 my ($username,$email) = split(/:/,$$recuser[$i]);                  my ($username,$email) = split(/:/,$$recuser[$i]);
                 $username = &Apache::lonnet::unescape($username);                  $username = &unescape($username);
                 $email = &Apache::lonnet::unescape($email);                  $email = &unescape($email);
                 $username = &HTML::Entities::encode($username,'<>&"');                  $username = &HTML::Entities::encode($username,'<>&"');
                 $email = &HTML::Entities::encode($email,'<>&"');                  $email = &HTML::Entities::encode($email,'<>&"');
                 $result .= '<recipient username="'.$username.'">'.                  $result .= '<recipient username="'.$username.'">'.
Line 133  sub packagemsg { Line 134  sub packagemsg {
     if (defined($attachmenturl)) {      if (defined($attachmenturl)) {
  $result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';   $result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
     }      }
     return $msgid,$result;      if (defined($symb)) {
           $result.= '<symb>'.$symb.'</symb>';
           if (defined($course_context)) {
               if ($course_context eq $env{'request.course.id'}) {
                   my $resource_title = &Apache::lonnet::gettitle($symb);
                   if (defined($resource_title)) {
                       $result .= '<resource_title>'.$resource_title.'</resource_title>';
                   }
               }
           }
       }
       return ($msgid,$result);
 }  }
   
 # ================================================== Unpack message into a hash  # ================================================== Unpack message into a hash
Line 177  sub unpackagemsg { Line 189  sub unpackagemsg {
 # ======================================================= Get info out of msgid  # ======================================================= Get info out of msgid
   
 sub buildmsgid {  sub buildmsgid {
     my ($now,$subject,$uname,$udom,$msgcount,$course_context,$pid) = @_;      my ($now,$subject,$uname,$udom,$msgcount,$course_context,$symb,$error,$pid) = @_;
     $subject=&Apache::lonnet::escape($subject);      $subject=&escape($subject);
     return(&Apache::lonnet::escape($now.':'.$subject.':'.$uname.':'.      $symb = &escape($symb);
            $udom.':'.$msgcount.':'.$course_context.':'.$pid));      return(&escape($now.':'.$subject.':'.$uname.':'.
              $udom.':'.$msgcount.':'.$course_context.':'.$pid.':'.$symb.':'.$error));
 }  }
   
 sub unpackmsgid {  sub unpackmsgid {
     my ($msgid,$folder,$skipstatus,$status_cache)=@_;      my ($msgid,$folder,$skipstatus,$status_cache)=@_;
     $msgid=&Apache::lonnet::unescape($msgid);      $msgid=&unescape($msgid);
     my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid,      my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid,
                      $processid)=split(/\:/,&Apache::lonnet::unescape($msgid));          $processid,$symb,$error) = split(/\:/,&unescape($msgid));
       $shortsubj = &unescape($shortsubj);
       $shortsubj = &HTML::Entities::decode($shortsubj);
       $symb = &unescape($symb);
     if (!defined($processid)) { $fromcid = ''; }      if (!defined($processid)) { $fromcid = ''; }
     my %status=();      my %status=();
     unless ($skipstatus) {      unless ($skipstatus) {
Line 200  sub unpackmsgid { Line 216  sub unpackmsgid {
  if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }   if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
         unless ($status{$msgid}) { $status{$msgid}='new'; }          unless ($status{$msgid}) { $status{$msgid}='new'; }
     }      }
     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid);      return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid,$symb,$error);
 }  }
   
   
 sub sendemail {  sub sendemail {
     my ($to,$subject,$body)=@_;      my ($to,$subject,$body)=@_;
       my %senderemails=&Apache::loncommon::getemails();
       my $senderaddress='';
       foreach my $type ('notification','permanentemail','critnotification') {
    if ($senderemails{$type}) {
       $senderaddress=$senderemails{$type};
    }
       }
     $body=      $body=
     "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".      "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
     "*** ".&mt('Please do not reply to this address.')."\n\n".$body;      "*** ".($senderaddress?&mt('You can reply to this message'):&mt('Please do not reply to this address.')."\n*** ".
       &mt('A reply will not be received by the recipient!'))."\n\n".$body;
     my $msg = new Mail::Send;      my $msg = new Mail::Send;
     $msg->to($to);      $msg->to($to);
     $msg->subject('[LON-CAPA] '.$subject);      $msg->subject('[LON-CAPA] '.$subject);
       if ($senderaddress) { $msg->add('Reply-to',$senderaddress); $msg->add('From',$senderaddress); }
     if (my $fh = $msg->open()) {      if (my $fh = $msg->open()) {
  print $fh $body;   print $fh $body;
  $fh->close;   $fh->close;
Line 284  sub author_res_msg { Line 309  sub author_res_msg {
        ($msgid,$message)=&packagemsg($filename,$message);         ($msgid,$message)=&packagemsg($filename,$message);
        return &Apache::lonnet::reply('put:'.$domain.':'.$author.         return &Apache::lonnet::reply('put:'.$domain.':'.$author.
          ':nohist_res_msgs:'.           ':nohist_res_msgs:'.
           &Apache::lonnet::escape($filename.'_'.$id).'='.            &escape($filename.'_'.$id).'='.
           &Apache::lonnet::escape($message),$homeserver);            &escape($message),$homeserver);
     }      }
     return 'no_host';      return 'no_host';
 }  }
Line 295  sub author_res_msg { Line 320  sub author_res_msg {
 sub retrieve_author_res_msg {  sub retrieve_author_res_msg {
     my $url=shift;      my $url=shift;
     $url=&Apache::lonnet::declutter($url);      $url=&Apache::lonnet::declutter($url);
     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);      my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
     my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);      my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
     my $msgs='';      my $msgs='';
     foreach (keys %errormsgs) {      foreach (keys %errormsgs) {
Line 315  sub retrieve_author_res_msg { Line 340  sub retrieve_author_res_msg {
 sub del_url_author_res_msg {  sub del_url_author_res_msg {
     my $url=shift;      my $url=shift;
     $url=&Apache::lonnet::declutter($url);      $url=&Apache::lonnet::declutter($url);
     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);      my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
     my @delmsgs=();      my @delmsgs=();
     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {      foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
  if ($_=~/^\Q$url\E\_\d+$/) {   if ($_=~/^\Q$url\E\_\d+$/) {
Line 329  sub del_url_author_res_msg { Line 354  sub del_url_author_res_msg {
 sub clear_author_res_msg {  sub clear_author_res_msg {
     my $url=shift;      my $url=shift;
     $url=&Apache::lonnet::declutter($url);      $url=&Apache::lonnet::declutter($url);
     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);      my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
     my @delmsgs=();      my @delmsgs=();
     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {      foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
  if ($_=~/^\Q$url\E/) {   if ($_=~/^\Q$url\E/) {
Line 350  sub all_url_author_res_msg { Line 375  sub all_url_author_res_msg {
     return %returnhash;      return %returnhash;
 }  }
   
   # ====================================== Add a comment to the User Notes screen
   
   sub store_instructor_comment {
       my ($msg,$uname,$udom) = @_;
       my $cid  = $env{'request.course.id'};
       my $cnum = $env{'course.'.$cid.'.num'};
       my $cdom = $env{'course.'.$cid.'.domain'};
       my $subject= &mt('Record').' ['.$uname.':'.$udom.']';
       my $result = &user_normal_msg_raw($cnum,$cdom,$subject,$msg);
       return $result;
   }
   
 # ================================================== Critical message to a user  # ================================================== Critical message to a user
   
 sub user_crit_msg_raw {  sub user_crit_msg_raw {
     my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;      my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
 # Check if allowed missing  # Check if allowed missing
     my $status='';      my ($status,$packed_message);
     my $msgid='undefined';      my $msgid='undefined';
     unless (($message)&&($user)&&($domain)) { $status='empty'; };      unless (($message)&&($user)&&($domain)) { $status='empty'; };
     my $text=$message;      my $text=$message;
     my $homeserver=&Apache::lonnet::homeserver($user,$domain);      my $homeserver=&Apache::lonnet::homeserver($user,$domain);
     if ($homeserver ne 'no_host') {      if ($homeserver ne 'no_host') {
        ($msgid,$message)=&packagemsg($subject,$message);         ($msgid,$packed_message)=&packagemsg($subject,$message);
        if ($sendback) { $message.='<sendback>true</sendback>'; }         if ($sendback) { $packed_message.='<sendback>true</sendback>'; }
        $status=&Apache::lonnet::critical(         $status=&Apache::lonnet::critical(
            'put:'.$domain.':'.$user.':critical:'.             'put:'.$domain.':'.$user.':critical:'.
            &Apache::lonnet::escape($msgid).'='.             &escape($msgid).'='.
            &Apache::lonnet::escape($message),$homeserver);             &escape($packed_message),$homeserver);
         if (defined($sentmessage)) {          if (defined($sentmessage)) {
             $$sentmessage = $message;              $$sentmessage = $packed_message;
           }
           if ($env{'request.course.id'} eq '') {
               (undef,my $packed_message_no_citation) =
               &packagemsg($subject,$message,undef,undef,undef,$user,$domain,
                           $msgid);
               if ($status eq 'ok' || $status eq 'con_delayed') {
                   &store_sent_mail($msgid,$packed_message_no_citation);
               }
         }          }
     } else {      } else {
        $status='no_host';         $status='no_host';
     }      }
   
 # Notifications  # Notifications
     my %userenv = &Apache::lonnet::get('environment',['critnotification',      my %userenv = &Apache::loncommon::getemails($user,$domain);
                                                       'permanentemail'],  
                                        $domain,$user);  
     if ($userenv{'critnotification'}) {      if ($userenv{'critnotification'}) {
       &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1,        &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1,
  $text);   $text);
Line 404  sub user_crit_msg_raw { Line 448  sub user_crit_msg_raw {
     a critical message $message to the $user at $domain. If $sendback is true,      a critical message $message to the $user at $domain. If $sendback is true,
     a reciept will be sent to the current user when $user recieves the message.      a reciept will be sent to the current user when $user recieves the message.
   
       Additionally it will check if the user has a Forwarding address
       set, and send the message to that address instead
   
       returns 
         - in array context a list of results for each message that was sent
         - in scalar context a space seperated list of results for each 
              message sent
   
 =cut  =cut
   
 sub user_crit_msg {  sub user_crit_msg {
     my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;      my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
     my $status='';      my @status;
     my %userenv = &Apache::lonnet::get('environment',['msgforward'],      my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                                        $domain,$user);                                         $domain,$user);
     my $msgforward=$userenv{'msgforward'};      my $msgforward=$userenv{'msgforward'};
     if ($msgforward) {      if ($msgforward) {
        foreach (split(/\,/,$msgforward)) {         foreach my $addr (split(/\,/,$msgforward)) {
  my ($forwuser,$forwdomain)=split(/\:/,$_);   my ($forwuser,$forwdomain)=split(/\:/,$addr);
          $status.=           push(@status,
    &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,        &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
                 $sendback,$toperm,$sentmessage).' ';   $sendback,$toperm,$sentmessage));
        }         }
     } else {       } else { 
  $status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage);   push(@status,
        &user_crit_msg_raw($user,$domain,$subject,$message,$sendback,
    $toperm,$sentmessage));
     }      }
     return $status;      if (wantarray) {
    return @status;
       }
       return join(' ',@status);
 }  }
   
 # =================================================== Critical message received  # =================================================== Critical message received
Line 455  sub user_crit_received { Line 512  sub user_crit_received {
   
 sub user_normal_msg_raw {  sub user_normal_msg_raw {
     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,      my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
  $toperm,$currid,$newid,$sentmessage,$crsmsgid)=@_;          $toperm,$currid,$newid,$sentmessage,$crsmsgid,$symb,$restitle,
           $error)=@_;
 # Check if allowed missing  # Check if allowed missing
     my ($status,$packed_message);      my ($status,$packed_message);
     my $msgid='undefined';      my $msgid='undefined';
Line 466  sub user_normal_msg_raw { Line 524  sub user_normal_msg_raw {
        ($msgid,$packed_message)=         ($msgid,$packed_message)=
                  &packagemsg($subject,$message,$citation,$baseurl,                   &packagemsg($subject,$message,$citation,$baseurl,
                                      $attachmenturl,$user,$domain,$currid,                                       $attachmenturl,$user,$domain,$currid,
                                                          undef,$crsmsgid);                                       undef,$crsmsgid,$symb,$error);
   
 # Store in user folder  # Store in user folder
        $status=&Apache::lonnet::critical(         $status=&Apache::lonnet::critical(
            'put:'.$domain.':'.$user.':nohist_email:'.             'put:'.$domain.':'.$user.':nohist_email:'.
            &Apache::lonnet::escape($msgid).'='.             &escape($msgid).'='.
            &Apache::lonnet::escape($packed_message),$homeserver);             &escape($packed_message),$homeserver);
 # Save new message received time  # Save new message received time
        &Apache::lonnet::put         &Apache::lonnet::put
                          ('email_status',{'recnewemail'=>time},$domain,$user);                           ('email_status',{'recnewemail'=>time},$domain,$user);
Line 483  sub user_normal_msg_raw { Line 541  sub user_normal_msg_raw {
                (&Apache::lonnet::allowed('srm',$env{'request.course.id'})                 (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
  || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.   || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
     '/'.$env{'request.course.sec'})))) {      '/'.$env{'request.course.sec'})))) {
            (undef, my $packed_message_no_citation)=             (undef,my $packed_message_no_citation) =
                              &packagemsg($subject,$message,undef     ,$baseurl,                 &packagemsg($subject,$message,undef,$baseurl,$attachmenturl,
                                           $attachmenturl,$user,$domain,$currid,                             $user,$domain,$currid,undef,$crsmsgid,$symb,$error);
                                                               undef,$crsmsgid);             if ($status eq 'ok' || $status eq 'con_delayed') {
                  &store_sent_mail($msgid,$packed_message_no_citation);
            $status .= &store_sent_mail($msgid,$packed_message_no_citation);             }
        }         }
     } else {      } else {
        $status='no_host';         $status='no_host';
Line 522  sub user_normal_msg_raw { Line 580  sub user_normal_msg_raw {
   
 =pod  =pod
   
 =item * B<user_normal_msg($user, $domain, $subject, $message,  =item * B<user_normal_msg($user, $domain, $subject, $message, $citation,
     $citation, $baseurl, $attachmenturl)>: Sends a message to the         $baseurl, $attachmenturl, $toperm, $sentmessage, $symb, $restitle, $error)>:
     $user at $domain, with subject $subject and message $message.   Sends a message to the  $user at $domain, with subject $subject and message $message.
   
 =cut  =cut
   
 sub user_normal_msg {  sub user_normal_msg {
     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,      my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
  $toperm,$sentmessage)=@_;   $toperm,$sentmessage,$symb,$restitle,$error)=@_;
     my $status='';      my $status='';
     my %userenv = &Apache::lonnet::get('environment',['msgforward'],      my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                                        $domain,$user);                                         $domain,$user);
Line 541  sub user_normal_msg { Line 599  sub user_normal_msg {
     $status.=      $status.=
         &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,          &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
      $citation,$baseurl,$attachmenturl,$toperm,       $citation,$baseurl,$attachmenturl,$toperm,
      undef,undef,$sentmessage).' ';       undef,undef,$sentmessage,undef,$symb,$restitle,$error).' ';
         }          }
     } else {       } else {
  $status=&user_normal_msg_raw($user,$domain,$subject,$message,   $status=&user_normal_msg_raw($user,$domain,$subject,$message,
      $citation,$baseurl,$attachmenturl,$toperm,       $citation,$baseurl,$attachmenturl,$toperm,
      undef,undef,$sentmessage);       undef,undef,$sentmessage,undef,$symb,$restitle,$error);
     }      }
     return $status;      return $status;
 }  }
Line 556  sub store_sent_mail { Line 614  sub store_sent_mail {
     my $status =' '.&Apache::lonnet::critical(      my $status =' '.&Apache::lonnet::critical(
                'put:'.$env{'user.domain'}.':'.$env{'user.name'}.                 'put:'.$env{'user.domain'}.':'.$env{'user.name'}.
                                           ':nohist_email_sent:'.                                            ':nohist_email_sent:'.
                &Apache::lonnet::escape($msgid).'='.                 &escape($msgid).'='.
                &Apache::lonnet::escape($message),$env{'user.home'});                 &escape($message),$env{'user.home'});
     return $status;      return $status;
 }  }
   
Line 566  sub store_sent_mail { Line 624  sub store_sent_mail {
 sub foldersuffix {  sub foldersuffix {
     my $folder=shift;      my $folder=shift;
     unless ($folder) { return ''; }      unless ($folder) { return ''; }
     return '_'.&Apache::lonnet::escape($folder);      my $suffix;
       my %folderhash = &get_user_folders($folder);
       if (ref($folderhash{$folder}) eq 'HASH') {
           $suffix = '_'.&escape($folderhash{$folder}{'id'});
       } else {
           $suffix = '_'.&escape($folder);
       }
       return $suffix;
   }
   
   # ========================================================= User-defined folders 
   
   sub get_user_folders {
       my ($folder) = @_;
       my %userfolders = 
             &Apache::lonnet::dump('email_folders',undef,undef,$folder);
       my $lock = "\0".'lock_counter'; # locks db while counter incremented
       my $counter = "\0".'idcount';   # used in suffix for email db files
       if (defined($userfolders{$lock})) {
           delete($userfolders{$lock});
       }
       if (defined($userfolders{$counter})) {
           delete($userfolders{$counter});
       }
       return %userfolders;
 }  }
   
 1;  1;

Removed from v.1.181  
changed lines
  Added in v.1.193


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>