File:  [LON-CAPA] / loncom / interface / lonmsg.pm
Revision 1.251: download - view: text, annotated - select for diffs
Thu Feb 8 03:02:12 2024 UTC (3 months, 1 week ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Typo

    1: # The LearningOnline Network with CAPA
    2: # Routines for messaging
    3: #
    4: # $Id: lonmsg.pm,v 1.251 2024/02/08 03:02:12 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::lonmsg;
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::lonmsg: supports internal messaging
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: lonmsg provides routines for sending messages.
   40: 
   41: Right now, this document will cover just how to send a message, since
   42: it is likely you will not need to programmatically read messages,
   43: since lonmsg already implements that functionality.
   44: 
   45: The routines used to package messages and unpackage messages are not
   46: only used by lonmsg when creating/extracting messages for LON-CAPA's
   47: internal messaging system, but also by lonnotify.pm which is available
   48: for use by Domain Coordinators to broadcast standard e-mail to specified
   49: users in their domain.  The XML packaging used in the two cases is very
   50: similar.  The differences are the use of <recuser>$uname</recuser> and
   51: <recdomain>$udom</recdomain> in stored internal messages, compared
   52: with <recipient username="$uname:$udom">$email</recipient> in stored
   53: Domain Coordinator e-mail for the storage of information about
   54: recipients of the message/e-mail.
   55: 
   56: =head1 SUBROUTINES
   57: 
   58: =over
   59: 
   60: =pod
   61: 
   62: =item packagemsg()
   63: 
   64: Package
   65: 
   66: =item get_course_context()
   67: 
   68: =item unpackagemsg()
   69: 
   70: Unpack message into a hash
   71: 
   72: =item buildmsgid()
   73: 
   74: Get info out of msgid
   75: 
   76: =item unpackmsgid()
   77: 
   78: =item sendemail()
   79: 
   80: =item sendnotification()
   81: 
   82: Send notification emails
   83: 
   84: =item newmail()
   85: 
   86: Check for email
   87: 
   88: =item author_res_msg()
   89: 
   90: Automated message to the author of a resource
   91: 
   92: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
   93:     of the resource with the URI $filename.
   94: 
   95: =item retrieve_author_res_msg()
   96: 
   97: Retrieve author resource messages
   98: 
   99: =item del_url_author_res_msg()
  100: 
  101: Delete all author messages related to one URL
  102: 
  103: =item clear_author_res_msg()
  104: 
  105: Clear out all author messages in URL path
  106: 
  107: =item all_url_author_res_msg()
  108: 
  109: Return hash with URLs for which there is a resource message
  110: 
  111: =item store_instructor_comment()
  112: 
  113: Add a comment to the User Notes screen
  114: 
  115: =item user_crit_msg_raw()
  116: 
  117: Critical message to a user
  118: 
  119: =item user_crit_msg()
  120: 
  121: New routine that respects "forward" and calls old routine
  122: 
  123: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback, $toperm, $sentmessage, $nosentstore, $recipid, $attachmenturl, $permresults, $senthide)>: 
  124:     Sends a critical message $message to the $user at $domain.  If $sendback
  125:     is true,  a receipt will be sent to the current user when $user receives 
  126:     the message.
  127: 
  128:     Additionally it will check if the user has a Forwarding address
  129:     set, and send the message to that address instead
  130: 
  131:     returns 
  132:       - in array context a list of results for each message that was sent
  133:       - in scalar context a space seperated list of results for each 
  134:            message sent
  135: 
  136: 
  137: =item user_crit_received()
  138: 
  139: Critical message received
  140: 
  141: =item user_normal_msg_raw()
  142: 
  143: Normal communication
  144: 
  145: =item user_normal_msg()
  146: 
  147: New routine that respects "forward" and calls old routine
  148: 
  149: =item * B<user_normal_msg($user, $domain, $subject, $message, $citation,
  150:        $baseurl, $attachmenturl, $toperm, $sentmessage, $symb, $restitle,
  151:        $error,$nosentstore,$recipid,$permresults,$senthide)>:
  152:  Sends a message to the  $user at $domain, with subject $subject and message $message.
  153: 
  154:     Additionally it will check if the user has a Forwarding address
  155:     set, and send the message to that address instead
  156: 
  157:     returns
  158:       - in array context a list of results for each message that was sent
  159:       - in scalar context a space seperated list of results for each
  160:            message sent
  161: 
  162: =item store_sent_mail()
  163: 
  164: =item store_recipients()
  165: 
  166: =item foldersuffix()
  167: 
  168: =item get_user_folders()
  169: 
  170: User-defined folders 
  171: 
  172: =item secapply()
  173: 
  174: =item B<decide_receiver($feedurl,$author,$question,$course,$policy,$defaultflag)>:
  175: 
  176: Arguments
  177:   $feedurl - /res/ url of resource (only need if $author is true)
  178:   $author,$question,$course,$policy - all true/false parameters
  179:     if true will attempt to find the addresses of user that should receive
  180:     this type of feedback (author - feedback to author of resource $feedurl,
  181:     $question 'Resource Content Questions', $course 'Course Content Question',
  182:     $policy 'Course Policy')
  183:     (Additionally it also checks $env for whether the corresponding form.<name>
  184:     element exists, for ease of use in a html response context)
  185:    
  186:   $defaultflag - (internal should be left blank) if true gather addresses 
  187:                  that aren't for a section even if I have a section
  188:                  (used for reccursion internally, first we look for
  189:                  addresses for our specific section then we recurse
  190:                  and look for non section addresses)
  191: 
  192: Returns
  193:   $typestyle - string of html text, describing what addresses were found
  194:   %to - a hash, which keys are addresses of users to send messages to
  195:         the keys will look like   name:domain
  196: 
  197: =back
  198: 
  199: =cut
  200: 
  201: use strict;
  202: use Apache::lonnet;
  203: use Apache::loncommon;
  204: use HTML::TokeParser();
  205: use Apache::lonlocal;
  206: use HTML::Entities;
  207: use Encode;
  208: use LONCAPA qw(:DEFAULT :match);
  209: 
  210: {
  211:     my $uniq;
  212:     sub get_uniq {
  213: 	$uniq++;
  214: 	return $uniq;
  215:     }
  216: }
  217: 
  218: 
  219: 
  220: sub packagemsg {
  221:     my ($subject,$message,$citation,$baseurl,$attachmenturl,$recuser,$recdomain,
  222: 	$msgid,$type,$crsmsgid,$symb,$error,$recipid,$senthide,$origmsgid)=@_;
  223:     $message =&HTML::Entities::encode($message,'<>&"');
  224:     $citation=&HTML::Entities::encode($citation,'<>&"');
  225:     $subject =&HTML::Entities::encode($subject,'<>&"');
  226:     #remove machine specification
  227:     $baseurl =~ s|^https?://[^/]+/|/|;
  228:     $baseurl =&HTML::Entities::encode($baseurl,'<>&"');
  229:     #remove machine specification
  230:     $attachmenturl =~ s|^https?://[^/]+/|/|;
  231:     $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
  232:     if ($senthide) {
  233:         foreach my $item ($subject,$message) {
  234:             if ($item ne '') {
  235:                 $item = 'Not shown due to IP block';
  236:             }
  237:         }
  238:         if ($attachmenturl ne '') {
  239:             $attachmenturl = '';
  240:         }
  241:         if ($citation ne '') {
  242:             $citation = '';
  243:         }
  244:         if ($msgid ne '') {
  245:             $msgid = '';
  246:         }
  247:     }
  248:     my $course_context = &get_course_context();
  249:     my $now=time;
  250:     my $ip = &Apache::lonnet::get_requestor_ip();
  251:     my $msgcount = &get_uniq();
  252:     unless(defined($msgid)) {
  253:         $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},
  254:                            $msgcount,$course_context,$symb,$error,$$);
  255:     }
  256:     my $result = '<sendername>'.$env{'user.name'}.'</sendername>'.
  257:            '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
  258:            '<subject>'.$subject.'</subject>'.
  259:            '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>';
  260:     if (defined($crsmsgid)) {
  261:         $result.= '<courseid>'.$course_context.'</courseid>'.
  262:                   '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
  263:                   '<msgid>'.$msgid.'</msgid>'.
  264:                   '<coursemsgid>'.$crsmsgid.'</coursemsgid>'.
  265:                   '<message>'.$message.'</message>';
  266:         return ($msgid,$result);
  267:     }
  268:     $result .= '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
  269:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
  270: 	   '<client>'.$ip.'</client>'.
  271: 	   '<browsertype>'.$env{'browser.type'}.'</browsertype>'.
  272: 	   '<browseros>'.$env{'browser.os'}.'</browseros>'.
  273: 	   '<browserversion>'.$env{'browser.version'}.'</browserversion>'.
  274:            '<browsermathml>'.$env{'browser.mathml'}.'</browsermathml>'.
  275: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
  276: 	   '<courseid>'.$course_context.'</courseid>'.
  277: 	   '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
  278: 	   '<role>'.$env{'request.role'}.'</role>'.
  279: 	   '<resource>'.$env{'request.filename'}.'</resource>'.
  280:            '<msgid>'.$msgid.'</msgid>';
  281:     if (defined($env{'form.group'})) {
  282:         $result .= '<group>'.$env{'form.group'}.'</group>';
  283:     }
  284:     if (ref($recuser) eq 'ARRAY') {
  285:         for (my $i=0; $i<@{$recuser}; $i++) {
  286:             if ($type eq 'dcmail') {
  287:                 my ($username,$email) = split(/:/,$$recuser[$i]);
  288:                 $username = &unescape($username);
  289:                 $email = &unescape($email);
  290:                 $username = &HTML::Entities::encode($username,'<>&"');
  291:                 $email = &HTML::Entities::encode($email,'<>&"');
  292:                 $result .= '<recipient username="'.$username.'">'.
  293:                                             $email.'</recipient>';
  294:             } else {
  295:                 $result .= '<recuser>'.$$recuser[$i].'</recuser>'.
  296:                            '<recdomain>'.$$recdomain[$i].'</recdomain>';
  297:             }
  298:         }
  299:     } else {
  300:         $result .= '<recuser>'.$recuser.'</recuser>'.
  301:                    '<recdomain>'.$recdomain.'</recdomain>';
  302:     }
  303:     $result .= '<message>'.$message.'</message>';
  304:     if (defined($citation)) {
  305: 	$result.='<citation>'.$citation.'</citation>';
  306:     }
  307:     if (defined($baseurl)) {
  308: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
  309:     }
  310:     if (defined($attachmenturl)) {
  311: 	$result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
  312:     }
  313:     if (defined($symb)) {
  314:         $result.= '<symb>'.$symb.'</symb>';
  315:         if ($course_context ne '') {
  316:             if ($course_context eq $env{'request.course.id'}) {
  317:                 my $resource_title = &Apache::lonnet::gettitle($symb);
  318:                 if (defined($resource_title)) {
  319:                     $result .= '<resource_title>'.$resource_title.'</resource_title>';
  320:                 }
  321:             }
  322:         }
  323:     }
  324:     if (defined($recipid)) {
  325:         $result.= '<recipid>'.$recipid.'</recipid>';
  326:     }
  327:     if ($env{'form.can_reply'} eq 'N') {
  328:         $result .= '<noreplies>1</noreplies>';
  329:     }
  330:     if ($env{'form.reply_to_addr'}) {
  331:         my ($replytoname,$replytodom) = split(/:/,$env{'form.reply_to_addr'});
  332:         if (!($replytoname eq $env{'user.name'} && $replytodom eq $env{'user.domain'})) {
  333:             if (&Apache::lonnet::homeserver($replytoname,$replytodom) ne 'no_host') {
  334:                 $result .= '<replytoaddr>'.$env{'form.reply_to_addr'}.'</replytoaddr>';
  335:             }
  336:         }
  337:     }
  338:     if ($senthide) {
  339:         $result .= '<senthide>$origmsgid</senthide>';
  340:     }
  341:     return ($msgid,$result);
  342: }
  343: 
  344: sub get_course_context {
  345:     my $course_context;
  346:     my $msgkey;
  347:     if (defined($env{'form.replyid'})) {
  348:         $msgkey = $env{'form.replyid'};
  349:     } elsif (defined($env{'form.forwid'})) {
  350:         $msgkey = $env{'form.forwid'}
  351:     } elsif (defined($env{'form.multiforwid'})) {
  352:         $msgkey = $env{'form.multiforwid'};
  353:     }
  354:     if ($msgkey ne '') {
  355:         my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid)=
  356:                    split(/\:/,&unescape($msgkey));
  357:         $course_context = $origcid;
  358:     }
  359:     foreach my $key (keys(%env)) {
  360:         if ($key=~/^form\.(rep)?rec\_(.*)$/) {
  361:             my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) =
  362:                                     split(/\:/,&unescape($2));
  363:             $course_context = $origcid;
  364:             last;
  365:         }
  366:     }
  367:     if ($course_context eq '') {
  368:         $course_context = $env{'request.course.id'};
  369:     }
  370:     return $course_context;
  371: }
  372: 
  373: 
  374: sub unpackagemsg {
  375:     my ($message,$notoken,$noattachmentlink)=@_;
  376:     my %content=();
  377:     my $parser=HTML::TokeParser->new(\$message);
  378:     my $token;
  379:     while ($token=$parser->get_token) {
  380:        if ($token->[0] eq 'S') {
  381: 	   my $entry=$token->[1];
  382:            my $value=$parser->get_text('/'.$entry);
  383:            if (($entry eq 'recuser') || ($entry eq 'recdomain')) {
  384:                push(@{$content{$entry}},$value);
  385:            } elsif ($entry eq 'recipient') {
  386:                my $username = $token->[2]{'username'};
  387:                $username = &HTML::Entities::decode($username,'<>&"');
  388:                $content{$entry}{$username} = $value;
  389:            } else {
  390:                $content{$entry}=$value;
  391:            }
  392:        }
  393:     }
  394:     if (!exists($content{'recuser'})) { $content{'recuser'} = []; }
  395:     if (($content{'attachmenturl'}) && (!$noattachmentlink)) {
  396:        my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
  397:        if ($notoken) {
  398: 	   $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';
  399:        } else {
  400: 	   &Apache::lonnet::allowuploaded('/adm/msg',
  401: 					  $content{'attachmenturl'});
  402: 	   $content{'message'}.='<p>'.&mt('Attachment').
  403: 	       ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
  404: 	       $fname.'</tt></a>';
  405:        }
  406:     }
  407:     return %content;
  408: }
  409: 
  410: 
  411: sub buildmsgid {
  412:     my ($now,$subject,$uname,$udom,$msgcount,$course_context,$symb,$error,$pid) = @_;
  413:     $subject=&escape($subject);
  414:     $symb = &escape($symb);
  415:     return(&escape($now.':'.$subject.':'.$uname.':'.
  416:            $udom.':'.$msgcount.':'.$course_context.':'.$pid.':'.$symb.':'.$error));
  417: }
  418: 
  419: sub unpackmsgid {
  420:     my ($msgid,$folder,$skipstatus,$status_cache,$onlycid)=@_;
  421:     $msgid=&unescape($msgid);
  422:     my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid,
  423:         $processid,$symb,$error) = split(/\:/,&unescape($msgid));
  424:     if (!defined($processid)) { $fromcid = ''; }
  425:     if (($onlycid) && ($onlycid ne $fromcid)) {
  426:         return ($sendtime,'',$fromname,$fromdomain,'',$fromcid,'',$error);
  427:     }
  428:     $shortsubj = &unescape($shortsubj);
  429:     $shortsubj = &HTML::Entities::decode($shortsubj);
  430:     $symb = &unescape($symb);
  431:     my %status=();
  432:     unless ($skipstatus) {
  433: 	if (ref($status_cache)) {
  434: 	    $status{$msgid} = $status_cache->{$msgid};
  435: 	} else {
  436: 	    my $suffix=&foldersuffix($folder);
  437: 	    %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
  438: 	}
  439: 	if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  440:         unless ($status{$msgid}) { $status{$msgid}='new'; }
  441:     }
  442:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid,$symb,$error);
  443: }
  444: 
  445: 
  446: sub sendemail {
  447:     my ($to,$subject,$body,$to_uname,$to_udom,$user_lh,$attachmenturl)=@_;
  448:     my $senderaddress='';
  449:     my $replytoaddress='';
  450:     my $msgsent;
  451:     if ($env{'form.can_reply'} eq 'N') {
  452:         my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
  453:         my $hostname = &Apache::lonnet::hostname($lonhost);
  454:         $replytoaddress = 'do-not-reply@'.$hostname;
  455:     } else {
  456:         my %senderemails;
  457:         my ($have_sender,$setreplyto);
  458:         if ($env{'form.reply_to_addr'}) {
  459:             my ($replytoname,$replytodom) = split(/:/,$env{'form.reply_to_addr'});
  460:             if (($replytoname eq $env{'user.name'} && $replytodom eq $env{'user.domain'})) {
  461:                 $setreplyto = 1;
  462:             } else {
  463:                 if (&Apache::lonnet::homeserver($replytoname,$replytodom) ne 'no_host') {
  464:                     %senderemails = 
  465:                         &Apache::loncommon::getemails($replytoname,$replytodom);
  466:                     $have_sender = 1;
  467:                     $setreplyto = 1;
  468:                 }
  469:             }
  470:         }
  471:         if (!$have_sender) {
  472:             %senderemails=&Apache::loncommon::getemails();
  473:         }
  474:         foreach my $type ('permanentemail','critnotification','notification') {
  475:             if ($senderemails{$type}) {
  476:                 ($senderaddress) = split(/,/,$senderemails{$type});
  477:                 if ($senderaddress) {
  478:                     if ($setreplyto) {
  479:                         $replytoaddress = $senderaddress;
  480:                     }
  481:                     last;
  482:                 }
  483:             }
  484:         }
  485:     }
  486:     $body=
  487:     "*** ".&mt_user($user_lh,'This is an automatic e-mail generated by the LON-CAPA system.')."\n".
  488:     "*** ".($senderaddress?&mt_user($user_lh,'You can reply to this e-mail'):&mt_user($user_lh,'Please do not reply to this address.')."\n*** ".
  489:     &mt_user($user_lh,'A reply will not be received by the recipient!'))."\n\n".$body;
  490:     
  491:     $attachmenturl = &Apache::lonnet::filelocation("",$attachmenturl);
  492:     my $filesize = (stat($attachmenturl))[7];
  493:     if ($filesize > 1048576) {
  494:         # Don't send if it exceeds 1 MB.
  495:         print '<p><span class="LC_error">' 
  496:             .&mt('Email not sent.  Attachment exceeds permitted length.')
  497:             .'</span><br /></p>';
  498:     } else {
  499:         # Otherwise build and send the email
  500:         $subject = '[LON-CAPA] '.$subject;
  501:         &Apache::loncommon::mime_email($senderaddress,$replytoaddress,$to,
  502:                                        $subject,$body,'','',$attachmenturl,'','');
  503:         $msgsent = 1;
  504:     }
  505:     return $msgsent;
  506: }
  507: 
  508: # ==================================================== Send notification emails
  509: 
  510: sub sendnotification {
  511:     my ($to,$touname,$toudom,$subj,$crit,$text,$msgid,$attachmenturl)=@_;
  512:     my $sender=$env{'environment.firstname'}.' '.$env{'environment.lastname'};
  513:     unless ($sender=~/\w/) { 
  514: 	$sender=$env{'user.name'}.':'.$env{'user.domain'};
  515:     }
  516:     my $critical=($crit?' critical':'');
  517:     my $numsent = 0;
  518: 
  519:     $text=~s/\&lt\;/\</gs;
  520:     $text=~s/\&gt\;/\>/gs;
  521:     my $touhome = &Apache::lonnet::homeserver($touname,$toudom);
  522:     my $url = &Apache::lonnet::url_prefix('',$toudom,$touhome,'email').
  523:               '/adm/email?username='.$touname.'&domain='.$toudom.
  524:               '&display='.&escape($msgid);
  525:     my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid,
  526:         $symb,$error) = &Apache::lonmsg::unpackmsgid($msgid);
  527:     my ($coursetext,$body,$bodybegin,$bodysubj,$bodyend);
  528:     my $user_lh = &Apache::loncommon::user_lang($touname,$toudom,$fromcid);
  529:     if ($fromcid ne '') {
  530:         $coursetext = "\n".&mt_user($user_lh,'Course').': ';
  531:         if ($env{'course.'.$fromcid.'.description'} ne '') {
  532:             $coursetext .= $env{'course.'.$fromcid.'.description'};
  533:         } else {
  534:             my %coursehash = &Apache::lonnet::coursedescription($fromcid,);
  535:             if ($coursehash{'description'} ne '') {
  536:                 $coursetext .= $coursehash{'description'};
  537:             }
  538:         }
  539:         $coursetext .= "\n\n";
  540:     }
  541:     my @recipients = split(/,/,$to);
  542:     $bodybegin = $coursetext. 
  543:                &mt_user($user_lh,
  544:                          'You received a'.$critical.' message from [_1] in LON-CAPA.',$sender).' ';
  545:     $bodysubj = &mt_user($user_lh,'The subject is 
  546: 
  547:  [_1]
  548: 
  549: ',$subj)."\n".
  550: '=== '.&mt_user($user_lh,'Excerpt')." ============================================================
  551: ";
  552:     $bodyend = "
  553: ========================================================================
  554: 
  555: ".&mt_user($user_lh,'Use 
  556: 
  557:  [_1]
  558: 
  559: to access the full message.',$url);
  560:     my %userenv = &Apache::lonnet::get('environment',['notifywithhtml'],$toudom,$touname);
  561:     my $subject = &mt_user($user_lh,"'New'$critical message from [_1]",$sender);
  562:     unless ($subj eq '') {
  563:         $subject = $subj;
  564:     }
  565:  
  566:     my ($blocked,$blocktext,$clientip);
  567:     $clientip = &Apache::lonnet::get_requestor_ip();
  568:     if (!$crit) {
  569:         my %setters;
  570:         my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) = 
  571:             &Apache::loncommon::blockcheck(\%setters,'com',$clientip,$touname,$toudom);
  572:         if ($startblock && $endblock) {
  573:             $blocked = 1;
  574:             my $showstart = &Apache::lonlocal::locallocaltime($startblock);
  575:             my $showend = &Apache::lonlocal::locallocaltime($endblock);
  576:             $blocktext = &mt_user($user_lh,'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);
  577:         } elsif ($by_ip) {
  578:             $blocked = 1;
  579:             $blocktext = &mt_user($user_lh,'LON-CAPA messages sent to you will be inaccessible from your IP address [_1], because communication is being blocked for certain IP address(es).',$clientip);
  580:         }
  581:     }
  582:     if ($userenv{'notifywithhtml'} ne '') {
  583:         my @htmlexcerpt = split(/,/,$userenv{'notifywithhtml'});
  584:         my $htmlfree = &make_htmlfree($text);
  585:         foreach my $addr (@recipients) {
  586:             if ($blocked) {
  587:                 $body = $bodybegin."\n".$blocktext."\n".$bodyend;
  588:             } else {
  589:                 my $sendtext;
  590:                 if (!grep/^\Q$addr\E/,@htmlexcerpt) {
  591:                     $sendtext = $htmlfree;
  592:                 } else {
  593:                     $sendtext = $text;
  594:                 }
  595:                 $body = $bodybegin.$bodysubj.$sendtext.$bodyend;
  596:             }
  597:             if (&sendemail($addr,$subject,$body,$touname,$toudom,$user_lh,$attachmenturl)) {
  598:                 $numsent ++;
  599:             }
  600:         }
  601:     } else {
  602:         if ($blocked) {
  603:             $body = $bodybegin."\n".$blocktext."\n".$bodyend;
  604:         } else {
  605:             my $htmlfree = &make_htmlfree($text);
  606:             $body = $bodybegin.$bodysubj.$htmlfree.$bodyend;
  607:         }
  608:         if (&sendemail($to,$subject,$body,$touname,$toudom,$user_lh,$attachmenturl)) {
  609:             $numsent ++;
  610:         }
  611:     }
  612:     return $numsent;
  613: }
  614: 
  615: sub make_htmlfree {
  616:     my ($text) = @_;
  617:     $text =~ s/\<\/*[^\>]+\>//gs;
  618:     $text = &HTML::Entities::decode($text);
  619:     $text = &Encode::encode('utf8',$text);
  620:     return $text;
  621: }
  622: 
  623: sub mynewmail{
  624: 	&newmail();
  625: 	return $env{'user.mailcheck.lastnewmessagetime'} > $env{'user.mailcheck.lastvisit'};
  626: }
  627: 
  628: 
  629: sub newmail {
  630:     if ((time-$env{'user.mailcheck.time'})>300) {
  631:         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
  632:         &Apache::lonnet::appenv({'user.mailcheck.time'=>time});
  633: 		&Apache::lonnet::appenv({'user.mailcheck.lastnewmessagetime'=> $what{'recnewemail'}});
  634:         if ($what{'recnewemail'}>0) { return 1; }
  635:     }
  636:     return 0;
  637: }
  638: 
  639: 
  640: 
  641: sub author_res_msg {
  642:     my ($filename,$message)=@_;
  643:     unless ($message) { return 'empty'; }
  644:     $filename=&Apache::lonnet::declutter($filename);
  645:     my ($domain,$author,@dummy)=split(/\//,$filename);
  646:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
  647:     if ($homeserver ne 'no_host') {
  648:        my $id=unpack("%32C*",$message);
  649:        $message .= " <p>This error occurred on machine ".
  650: 	   $Apache::lonnet::perlvar{'lonHostID'}."</p>";
  651:        my $msgid;
  652:        ($msgid,$message)=&packagemsg($filename,$message);
  653:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
  654:          ':nohist_res_msgs:'.
  655:           &escape($filename.'_'.$id).'='.
  656:           &escape($message),$homeserver);
  657:     }
  658:     return 'no_host';
  659: }
  660: 
  661: 
  662: 
  663: sub retrieve_author_res_msg {
  664:     my $url=shift;
  665:     $url=&Apache::lonnet::declutter($url);
  666:     my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
  667:     my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
  668:     my $msgs='';
  669:     foreach my $msg (keys(%errormsgs)) {
  670: 	if ($msg =~ /^\Q$url\E\_\d+$/) {
  671: 	    my %content=&unpackagemsg($errormsgs{$msg});
  672: 	    $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
  673: 		$content{'time'}.'</b>: '.$content{'message'}.
  674: 		'<br /></p>';
  675: 	}
  676:     } 
  677:     return $msgs;     
  678: }
  679: 
  680: 
  681: 
  682: 
  683: 
  684: sub del_url_author_res_msg {
  685:     my $url=shift;
  686:     $url=&Apache::lonnet::declutter($url);
  687:     my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
  688:     my @delmsgs=();
  689:     foreach my $msg (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
  690: 	if ($msg =~ /^\Q$url\E\_\d+$/) {
  691: 	    push (@delmsgs,$msg);
  692: 	}
  693:     }
  694:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
  695: }
  696: 
  697: 
  698: sub clear_author_res_msg {
  699:     my $url=shift;
  700:     $url=&Apache::lonnet::declutter($url);
  701:     my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
  702:     my @delmsgs=();
  703:     foreach my $msg (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
  704: 	if ($msg =~ /^\Q$url\E/) {
  705: 	    push (@delmsgs,$msg);
  706: 	}
  707:     }
  708:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
  709: }
  710: 
  711: 
  712: 
  713: sub all_url_author_res_msg {
  714:     my ($author,$domain)=@_;
  715:     my %returnhash=();
  716:     foreach my $msg (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
  717: 	$msg =~ /^(.+)\_\d+/;
  718: 	$returnhash{$1}=1;
  719:     }
  720:     return %returnhash;
  721: }
  722: 
  723: 
  724: sub store_instructor_comment {
  725:     my ($msg,$uname,$udom) = @_;
  726:     my $cid  = $env{'request.course.id'};
  727:     my $cnum = $env{'course.'.$cid.'.num'};
  728:     my $cdom = $env{'course.'.$cid.'.domain'};
  729:     my $subject= &mt('Record').' ['.$uname.':'.$udom.']';
  730:     my $result = &user_normal_msg_raw($cnum,$cdom,$subject,$msg);
  731:     if ($result eq 'ok' || $result eq 'con_delayed') {
  732:         
  733:     }
  734:     return $result;
  735: }
  736: 
  737: 
  738: sub user_crit_msg_raw {
  739:     my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage,
  740:         $nosentstore,$recipid,$attachmenturl,$permresults,$senthide)=@_;
  741: # Check if allowed missing
  742:     my ($status,$packed_message);
  743:     my $msgid='undefined';
  744:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  745:     my $text=$message;
  746:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  747:     if ($homeserver ne 'no_host') {
  748:        ($msgid,$packed_message)=&packagemsg($subject,$message,undef,undef,
  749:                                   $attachmenturl,undef,undef,undef,undef,undef,
  750:                                   undef,undef,$recipid);
  751:        if ($sendback) { $packed_message.='<sendback>true</sendback>'; }
  752:        $status=&Apache::lonnet::cput('critical', {$msgid => $packed_message},
  753: 				     $domain,$user);
  754:         if (defined($sentmessage)) {
  755:             $$sentmessage = $packed_message;
  756:         }
  757:         if (!$nosentstore) {
  758:             my ($sentmsgid,$packed_message_no_citation) =
  759:             &packagemsg($subject,$message,undef,undef,$attachmenturl,$user,
  760:                         $domain,$msgid,undef,undef,undef,undef,undef,$senthide,$msgid);
  761:             if ($status eq 'ok' || $status eq 'con_delayed') {
  762:                 if ($senthide && $sentmsgid) {
  763:                     &store_sent_mail($sentmsgid,$packed_message_no_citation);
  764:                 } else {
  765:                     &store_sent_mail($msgid,$packed_message_no_citation);
  766:                 }
  767:             }
  768:         }
  769:     } else {
  770:        $status='no_host';
  771:     }
  772: 
  773: # Notifications
  774:     my %userenv = &Apache::loncommon::getemails($user,$domain);
  775:     my $critnotify = $userenv{'critnotification'};
  776:     my $permemail = $userenv{'permanentemail'};
  777:     my $numcrit = 0;
  778:     my $numperm = 0;
  779:     my $permlogmsgstatus;
  780:     if ($critnotify) {
  781:         $numcrit = &sendnotification($critnotify,$user,$domain,$subject,1,$text,$msgid,$attachmenturl);
  782:     }
  783:     if ($toperm && $permemail) {
  784:         if ($critnotify && $numcrit) {
  785:             if (grep(/^\Q$permemail\E/,split(/,/,$critnotify))) {
  786:                 $numperm = 1;
  787:             }
  788:         }
  789:         unless ($numperm) {
  790:             $numperm = &sendnotification($permemail,$user,$domain,$subject,1,$text,$msgid,$attachmenturl);
  791:         }
  792:     }
  793:     if ($toperm) {
  794:         $permlogmsgstatus = '. Perm. email log status '.
  795:                             &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
  796:                                                  "Perm. e-mail count $numperm for $user at $domain");
  797:         if (ref($permresults) eq 'HASH') {
  798:             $permresults->{"$user:$domain"} = $numperm;
  799:         }
  800:     }
  801: # Log this
  802:     &Apache::lonnet::logthis(
  803:       'Sending critical '.$msgid.
  804:       ', log status: '.
  805:       &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
  806:                          $env{'user.home'},
  807:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status '
  808:       .$status).$permlogmsgstatus);
  809:     return $status;
  810: }
  811: 
  812: 
  813: 
  814: sub user_crit_msg {
  815:     my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage,
  816:         $nosentstore,$recipid,$attachmenturl,$permresults,$senthide)=@_;
  817:     my @status;
  818:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
  819:                                        $domain,$user);
  820:     my $msgforward=$userenv{'msgforward'};
  821:     if ($msgforward) {
  822:        foreach my $addr (split(/\,/,$msgforward)) {
  823: 	 my ($forwuser,$forwdomain)=split(/\:/,$addr);
  824:          push(@status,
  825: 	      &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
  826: 				 $sendback,$toperm,$sentmessage,$nosentstore,
  827:                                  $recipid,$attachmenturl,$permresults,$senthide));
  828:        }
  829:     } else { 
  830: 	push(@status,
  831: 	     &user_crit_msg_raw($user,$domain,$subject,$message,$sendback,
  832: 				$toperm,$sentmessage,$nosentstore,$recipid,
  833:                                 $attachmenturl,$permresults,$senthide));
  834:     }
  835:     if (wantarray) {
  836: 	return @status;
  837:     }
  838:     return join(' ',@status);
  839: }
  840: 
  841: 
  842: sub user_crit_received {
  843:     my $msgid=shift;
  844:     my %message=&Apache::lonnet::get('critical',[$msgid]);
  845:     my %contents=&unpackagemsg($message{$msgid},1);
  846:     my $destname = $contents{'sendername'};
  847:     my $destdom = $contents{'senderdomain'};
  848:     if ($contents{'replytoaddr'}) {
  849:         my ($repname,$repdom) = split(/:/,$contents{'replytoaddr'});
  850:         if (&Apache::lonnet::homeserver($repname,$repdom) ne 'no_host') {
  851:             $destname = $repname;
  852:             $destdom = $repdom;    
  853:         }
  854:     }
  855:     my $status='rec: '.($contents{'sendback'}?
  856:      &user_normal_msg($destname,$destdom,&mt('Receipt').': '.$env{'user.name'}.
  857:                       ' '.&mt('at').' '.$env{'user.domain'}.', '.
  858:                       $contents{'subject'},&mt('User').' '.$env{'user.name'}.
  859:                       ' '.&mt('at').' '.$env{'user.domain'}.
  860:                       ' acknowledged receipt of message'."\n".'   "'.
  861:                       $contents{'subject'}.'"'."\n".&mt('dated').' '.
  862:                       $contents{'time'}.".\n"
  863:                       ):'no msg req');
  864:     $status.=' trans: '.
  865:      &Apache::lonnet::put(
  866:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
  867:     $status.=' del: '.
  868:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
  869:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
  870:                          $env{'user.home'},'Received critical message '.
  871:                          $contents{'msgid'}.
  872:                          ', '.$status);
  873:     return $status;
  874: }
  875: 
  876: 
  877: 
  878: 
  879: sub user_normal_msg_raw {
  880:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
  881:         $toperm,$currid,$newid,$sentmessage,$crsmsgid,$symb,$restitle,
  882:         $error,$nosentstore,$recipid,$permresults,$senthide)=@_;
  883: # Check if allowed missing
  884:     my ($status,$packed_message);
  885:     my $msgid='undefined';
  886:     my $text=$message;
  887:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  888:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  889:     if ($homeserver ne 'no_host') {
  890:        ($msgid,$packed_message)=
  891: 	                 &packagemsg($subject,$message,$citation,$baseurl,
  892:                                      $attachmenturl,$user,$domain,$currid,
  893:                                      undef,$crsmsgid,$symb,$error,$recipid);
  894: 
  895: # Store in user folder
  896:        $status=
  897: 	   &Apache::lonnet::cput('nohist_email',{$msgid => $packed_message},
  898: 				 $domain,$user);
  899: # Save new message received time
  900:        &Apache::lonnet::put
  901:                          ('email_status',{'recnewemail'=>time},$domain,$user);
  902: # Into sent-mail folder if sent mail storage required
  903:        if (!$nosentstore) {
  904:            my ($sentmsgid,$packed_message_no_citation) =
  905:                &packagemsg($subject,$message,undef,$baseurl,$attachmenturl,
  906:                            $user,$domain,$currid,undef,$crsmsgid,$symb,$error,
  907:                            undef,$senthide,$msgid);
  908:            if ($status eq 'ok' || $status eq 'con_delayed') {
  909:                if ($senthide && $sentmsgid) {
  910:                    &store_sent_mail($sentmsgid,$packed_message_no_citation);
  911:                } else {
  912:                    &store_sent_mail($msgid,$packed_message_no_citation);
  913:                }
  914:            }
  915:        }
  916:        if (ref($newid) eq 'SCALAR') {
  917: 	   $$newid = $msgid;
  918:        }
  919:        if (ref($sentmessage) eq 'SCALAR') {
  920: 	   $$sentmessage = $packed_message;
  921:        }
  922: # Notifications
  923:        my %userenv = &Apache::loncommon::getemails($user,$domain);
  924:        my $notify = $userenv{'notification'};
  925:        my $permemail = $userenv{'permanentemail'};
  926:        my $numnotify = 0;
  927:        my $numperm = 0;
  928:        my $permlogmsgstatus;
  929:        if ($notify) {
  930:            $numnotify = &sendnotification($notify,$user,$domain,$subject,0,$text,$msgid,$attachmenturl);
  931:        }
  932:        if ($toperm && $permemail) {
  933:            if ($notify && $numnotify) {
  934:                if (grep(/^\Q$permemail\E/,split(/,/,$notify))) {
  935:                    $numperm = 1;
  936:                }
  937:            }
  938:            unless ($numperm) {
  939:                $numperm = &sendnotification($permemail,$user,$domain,$subject,0,
  940:                                             $text,$msgid,$attachmenturl);
  941:            }
  942:        }
  943:        if ($toperm) {
  944:            $permlogmsgstatus = '. Perm. email log status '.
  945:                          &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
  946:                                               "Perm. e-mail count $numperm for $user at $domain");
  947:            if (ref($permresults) eq 'HASH') {
  948:                $permresults->{"$user:$domain"} = $numperm;
  949:            }
  950:        }
  951:        &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
  952: 			    $env{'user.home'},
  953: 			    'Sending '.$msgid.' to '.$user.' at '.$domain.' with status '.$status.
  954:                             $permlogmsgstatus);
  955:    } else {
  956:        $status='no_host';
  957:    }
  958:     return $status;
  959: }
  960: 
  961: sub user_normal_msg {
  962:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
  963: 	$toperm,$sentmessage,$symb,$restitle,$error,$nosentstore,$recipid,
  964:         $permresults,$senthide)=@_;
  965:     my @status;
  966:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
  967:                                        $domain,$user);
  968:     my $msgforward=$userenv{'msgforward'};
  969:     if ($msgforward) {
  970:         foreach my $fwd (split(/\,/,$msgforward)) {
  971: 	    my ($forwuser,$forwdomain)=split(/\:/,$fwd);
  972: 	    push(@status,
  973: 	        &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
  974: 				     $citation,$baseurl,$attachmenturl,$toperm,
  975: 				     undef,undef,$sentmessage,undef,$symb,
  976:                                      $restitle,$error,$nosentstore,$recipid,
  977:                                      $permresults,$senthide));
  978:         }
  979:     } else {
  980: 	push(@status,&user_normal_msg_raw($user,$domain,$subject,$message,
  981: 				     $citation,$baseurl,$attachmenturl,$toperm,
  982: 				     undef,undef,$sentmessage,undef,$symb,
  983:                                      $restitle,$error,$nosentstore,$recipid,
  984:                                      $permresults,$senthide));
  985:     }
  986:     if (wantarray) {
  987:         return @status;
  988:     }
  989:     return join(' ',@status);
  990: }
  991: 
  992: sub process_sent_mail {
  993:     my ($msgsubj,$subj_prefix,$numsent,$stamp,$msgname,$msgdom,$msgcount,
  994:         $context,$pid,$savemsg,$recusers,$recudoms,$baseurl,$attachmenturl,
  995:         $symb,$error,$senderuname,$senderdom,$recipid) = @_;
  996:     my $sentsubj;
  997:     if ($numsent > 1) {
  998:         $sentsubj = $subj_prefix.' ('.$numsent.' sent) '.$msgsubj;
  999:     } else {
 1000:         if ($subj_prefix) {
 1001:             $sentsubj = $subj_prefix.' ';
 1002:         }
 1003:         $sentsubj .= $msgsubj;
 1004:     }
 1005:     $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"');
 1006:     my $sentmsgid = 
 1007:         &buildmsgid($stamp,$sentsubj,$msgname,$msgdom,$msgcount,$context,$pid);
 1008:     (undef,my $sentmessage) =
 1009:         &packagemsg($msgsubj,$savemsg,undef,$baseurl,$attachmenturl,$recusers,
 1010:                     $recudoms,$sentmsgid,undef,undef,$symb,$error,$recipid);
 1011:     my $status = &store_sent_mail($sentmsgid,$sentmessage,$senderuname,
 1012:                                   $senderdom);
 1013:     return $status;
 1014: }
 1015: 
 1016: sub store_sent_mail {
 1017:     my ($msgid,$message,$senderuname,$senderdom) = @_;
 1018:     if ($senderuname eq '') {
 1019:         $senderuname = $env{'user.name'};
 1020:     }
 1021:     if ($senderdom eq '') {
 1022:         $senderdom = $env{'user.domain'};
 1023:     }
 1024:     my $status =' '.&Apache::lonnet::cput('nohist_email_sent',
 1025: 					  {$msgid => $message},
 1026: 					  $senderdom,$senderuname);
 1027:     return $status;
 1028: }
 1029: 
 1030: sub store_recipients {
 1031:     my ($subject,$sendername,$senderdom,$reciphash) = @_;
 1032:     my $context = &get_course_context();
 1033:     my $now = time();
 1034:     my $msgcount = &get_uniq();
 1035:     my $recipid =
 1036:         &buildmsgid($now,$subject,$sendername,$senderdom,$msgcount,$context,$$);
 1037:     my %recipinfo = (
 1038:                          $recipid => $reciphash,
 1039:                     );
 1040:     my $status = &Apache::lonnet::put('nohist_emailrecip',\%recipinfo,
 1041:                                       $senderdom,$sendername); 
 1042:     if ($status eq 'ok') {
 1043:         return ($recipid,$status);
 1044:     } else {
 1045:         return (undef,$status);
 1046:     }
 1047: }
 1048: 
 1049: 
 1050: sub foldersuffix {
 1051:     my $folder=shift;
 1052:     unless ($folder) { return ''; }
 1053:     my $suffix;
 1054:     my %folderhash = &get_user_folders($folder);
 1055:     if (ref($folderhash{$folder}) eq 'HASH') {
 1056:         $suffix = '_'.&escape($folderhash{$folder}{'id'});
 1057:     } else {
 1058:         $suffix = '_'.&escape($folder);
 1059:     }
 1060:     return $suffix;
 1061: }
 1062: 
 1063: 
 1064: sub get_user_folders {
 1065:     my ($folder) = @_;
 1066:     my %userfolders = 
 1067:           &Apache::lonnet::dump('email_folders',undef,undef,$folder);
 1068:     my $lock = "\0".'lock_counter'; # locks db while counter incremented
 1069:     my $counter = "\0".'idcount';   # used in suffix for email db files
 1070:     if (defined($userfolders{$lock})) {
 1071:         delete($userfolders{$lock});
 1072:     }
 1073:     if (defined($userfolders{$counter})) {
 1074:         delete($userfolders{$counter});
 1075:     }
 1076:     return %userfolders;
 1077: }
 1078: 
 1079: sub secapply {
 1080:     my $rec=shift;
 1081:     my $defaultflag=shift;
 1082:     $rec=~s/\s+//g;
 1083:     unless ($rec =~ /\:/) {
 1084:         $rec=~s/\@/\:/g;
 1085:     }
 1086:     my ($adr,$sections_or_groups)=($rec=~/^([^\(]+)\(([^\)]+)\)/);
 1087:     if ($sections_or_groups) {
 1088: 	foreach my $item (split(/\;/,$sections_or_groups)) {
 1089:             if (($item eq $env{'request.course.sec'}) ||
 1090:                 ($defaultflag && ($item eq '*'))) {
 1091:                 return $adr; 
 1092:             } elsif ($env{'request.course.groups'}) {
 1093:                 my @usersgroups = split(/:/,$env{'request.course.groups'});
 1094:                 if (grep(/^\Q$item\E$/,@usersgroups)) {
 1095:                     return $adr;
 1096:                 }
 1097:             } 
 1098:         }
 1099:     } else {
 1100:        return $rec;
 1101:     }
 1102:     return '';
 1103: }
 1104: 
 1105: sub decide_receiver {
 1106:     my ($feedurl,$author,$question,$course,$policy,$defaultflag) = @_;
 1107:     &Apache::lonenc::check_decrypt(\$feedurl);
 1108:     my $typestyle='';
 1109:     my %to=();
 1110:     if ($env{'form.discuss'} eq 'author' ||$author) {
 1111: 	$typestyle.='Submitting as Author Feedback<br />';
 1112: 	$feedurl=~ m{^/res/($LONCAPA::domain_re)/($LONCAPA::username_re)/};
 1113: 	$to{$2.':'.$1}=1;
 1114:     }
 1115:     my $cid = $env{'request.course.id'};
 1116:     if ($env{'form.discuss'} eq 'question' ||$question) {
 1117: 	$typestyle.=&mt('Submitting as Question').'<br />';
 1118: 	foreach my $item (split(/\,/,$env{'course.'.$cid.'.question.email'})) {
 1119: 	    my $rec=&secapply($item,$defaultflag);
 1120: 	    if ($rec) { $to{$rec}=1; }
 1121: 	} 
 1122:     }
 1123:     if ($env{'form.discuss'} eq 'course' ||$course) {
 1124: 	$typestyle.=&mt('Submitting as Comment').'<br />';
 1125: 	foreach my $item (split(/\,/,$env{'course.'.$cid.'.comment.email'})) {
 1126: 	    my $rec=&secapply($item,$defaultflag);
 1127: 	    if ($rec) { $to{$rec}=1; }
 1128: 	} 
 1129:     }
 1130:     if ($env{'form.discuss'} eq 'policy' ||$policy) {
 1131: 	$typestyle.=&mt('Submitting as Policy Feedback').'<br />';
 1132: 	foreach my $item (split(/\,/,$env{'course.'.$cid.'.policy.email'})) {
 1133: 	    my $rec=&secapply($item,$defaultflag);
 1134: 	    if ($rec) { $to{$rec}=1; }
 1135: 	} 
 1136:     }
 1137:     if ((scalar(%to) eq '0') && (!$defaultflag)) {
 1138: 	($typestyle,%to)=
 1139: 	    &decide_receiver($feedurl,$author,$question,$course,$policy,1);
 1140:     }
 1141:     return ($typestyle,%to);
 1142: }
 1143: 
 1144: 1;
 1145: __END__
 1146: 

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