Annotation of loncom/interface/lonmsg.pm, revision 1.239.2.3

1.1       www         1: # The LearningOnline Network with CAPA
1.26      albertel    2: # Routines for messaging
                      3: #
1.239.2.3! raeburn     4: # $Id: lonmsg.pm,v 1.239.2.2 2021/01/04 03:44:12 raeburn Exp $
1.26      albertel    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/
1.1       www        27: #
1.75      www        28: 
1.1       www        29: package Apache::lonmsg;
                     30: 
1.199     raeburn    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: 
1.215     jms        56: =head1 SUBROUTINES
1.199     raeburn    57: 
1.215     jms        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: 
1.238     raeburn   123: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback, $nosentstore, $recipid, $attachmenturl, $permresults)>: 
1.215     jms       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,
1.238     raeburn   151:        $error,$nosentstore,$recipid,$permresults)>:
1.215     jms       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
1.199     raeburn   198: 
                    199: =cut
                    200: 
1.1       www       201: use strict;
1.140     albertel  202: use Apache::lonnet;
1.47      albertel  203: use HTML::TokeParser();
1.180     albertel  204: use Apache::lonlocal;
1.53      www       205: use Mail::Send;
1.226     raeburn   206: use HTML::Entities;
                    207: use Encode;
1.187     albertel  208: use LONCAPA qw(:DEFAULT :match);
1.180     albertel  209: 
                    210: {
                    211:     my $uniq;
                    212:     sub get_uniq {
                    213: 	$uniq++;
                    214: 	return $uniq;
                    215:     }
                    216: }
1.65      www       217: 
1.215     jms       218: 
1.1       www       219: 
1.3       www       220: sub packagemsg {
1.108     www       221:     my ($subject,$message,$citation,$baseurl,$attachmenturl,
1.202     raeburn   222: 	$recuser,$recdomain,$msgid,$type,$crsmsgid,$symb,$error,$recipid)=@_;
1.96      albertel  223:     $message =&HTML::Entities::encode($message,'<>&"');
                    224:     $citation=&HTML::Entities::encode($citation,'<>&"');
                    225:     $subject =&HTML::Entities::encode($subject,'<>&"');
1.49      albertel  226:     #remove machine specification
1.218     raeburn   227:     $baseurl =~ s|^https?://[^/]+/|/|;
1.96      albertel  228:     $baseurl =&HTML::Entities::encode($baseurl,'<>&"');
1.51      www       229:     #remove machine specification
1.218     raeburn   230:     $attachmenturl =~ s|^https?://[^/]+/|/|;
1.96      albertel  231:     $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
1.201     raeburn   232:     my $course_context = &get_course_context();
1.2       www       233:     my $now=time;
1.239.2.2  raeburn   234:     my $ip = &Apache::lonnet::get_requestor_ip();
1.180     albertel  235:     my $msgcount = &get_uniq();
1.156     raeburn   236:     unless(defined($msgid)) {
1.159     raeburn   237:         $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},
1.191     raeburn   238:                            $msgcount,$course_context,$symb,$error,$$);
1.156     raeburn   239:     }
1.174     raeburn   240:     my $result = '<sendername>'.$env{'user.name'}.'</sendername>'.
1.140     albertel  241:            '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
1.1       www       242:            '<subject>'.$subject.'</subject>'.
1.174     raeburn   243:            '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>';
                    244:     if (defined($crsmsgid)) {
                    245:         $result.= '<courseid>'.$course_context.'</courseid>'.
                    246:                   '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
                    247:                   '<msgid>'.$msgid.'</msgid>'.
                    248:                   '<coursemsgid>'.$crsmsgid.'</coursemsgid>'.
                    249:                   '<message>'.$message.'</message>';
                    250:         return ($msgid,$result);
                    251:     }
                    252:     $result .= '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
1.1       www       253:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
1.239.2.2  raeburn   254: 	   '<client>'.$ip.'</client>'.
1.140     albertel  255: 	   '<browsertype>'.$env{'browser.type'}.'</browsertype>'.
                    256: 	   '<browseros>'.$env{'browser.os'}.'</browseros>'.
                    257: 	   '<browserversion>'.$env{'browser.version'}.'</browserversion>'.
                    258:            '<browsermathml>'.$env{'browser.mathml'}.'</browsermathml>'.
1.1       www       259: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
1.158     raeburn   260: 	   '<courseid>'.$course_context.'</courseid>'.
1.140     albertel  261: 	   '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
                    262: 	   '<role>'.$env{'request.role'}.'</role>'.
                    263: 	   '<resource>'.$env{'request.filename'}.'</resource>'.
1.156     raeburn   264:            '<msgid>'.$msgid.'</msgid>';
1.214     raeburn   265:     if (defined($env{'form.group'})) {
                    266:         $result .= '<group>'.$env{'form.group'}.'</group>';
                    267:     }
1.156     raeburn   268:     if (ref($recuser) eq 'ARRAY') {
                    269:         for (my $i=0; $i<@{$recuser}; $i++) {
1.162     raeburn   270:             if ($type eq 'dcmail') {
                    271:                 my ($username,$email) = split(/:/,$$recuser[$i]);
1.184     www       272:                 $username = &unescape($username);
                    273:                 $email = &unescape($email);
1.162     raeburn   274:                 $username = &HTML::Entities::encode($username,'<>&"');
                    275:                 $email = &HTML::Entities::encode($email,'<>&"');
                    276:                 $result .= '<recipient username="'.$username.'">'.
                    277:                                             $email.'</recipient>';
                    278:             } else {
                    279:                 $result .= '<recuser>'.$$recuser[$i].'</recuser>'.
                    280:                            '<recdomain>'.$$recdomain[$i].'</recdomain>';
                    281:             }
1.156     raeburn   282:         }
                    283:     } else {
                    284:         $result .= '<recuser>'.$recuser.'</recuser>'.
                    285:                    '<recdomain>'.$recdomain.'</recdomain>';
                    286:     }
                    287:     $result .= '<message>'.$message.'</message>';
1.49      albertel  288:     if (defined($citation)) {
                    289: 	$result.='<citation>'.$citation.'</citation>';
                    290:     }
                    291:     if (defined($baseurl)) {
                    292: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
                    293:     }
1.51      www       294:     if (defined($attachmenturl)) {
1.52      www       295: 	$result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
1.51      www       296:     }
1.191     raeburn   297:     if (defined($symb)) {
                    298:         $result.= '<symb>'.$symb.'</symb>';
1.201     raeburn   299:         if ($course_context ne '') {
1.191     raeburn   300:             if ($course_context eq $env{'request.course.id'}) {
                    301:                 my $resource_title = &Apache::lonnet::gettitle($symb);
                    302:                 if (defined($resource_title)) {
                    303:                     $result .= '<resource_title>'.$resource_title.'</resource_title>';
                    304:                 }
                    305:             }
                    306:         }
                    307:     }
1.202     raeburn   308:     if (defined($recipid)) {
                    309:         $result.= '<recipid>'.$recipid.'</recipid>';
                    310:     }
1.204     raeburn   311:     if ($env{'form.can_reply'} eq 'N') {
                    312:         $result .= '<noreplies>1</noreplies>';
                    313:     }
                    314:     if ($env{'form.reply_to_addr'}) {
                    315:         my ($replytoname,$replytodom) = split(/:/,$env{'form.reply_to_addr'});
                    316:         if (!($replytoname eq $env{'user.name'} && $replytodom eq $env{'user.domain'})) {
                    317:             if (&Apache::lonnet::homeserver($replytoname,$replytodom) ne 'no_host') {
                    318:                 $result .= '<replytoaddr>'.$env{'form.reply_to_addr'}.'</replytoaddr>';
                    319:             }
                    320:         }
                    321:     }
1.191     raeburn   322:     return ($msgid,$result);
1.1       www       323: }
                    324: 
1.201     raeburn   325: sub get_course_context {
                    326:     my $course_context;
1.212     raeburn   327:     my $msgkey;
1.201     raeburn   328:     if (defined($env{'form.replyid'})) {
1.212     raeburn   329:         $msgkey = $env{'form.replyid'};
                    330:     } elsif (defined($env{'form.forwid'})) {
                    331:         $msgkey = $env{'form.forwid'}
                    332:     } elsif (defined($env{'form.multiforwid'})) {
                    333:         $msgkey = $env{'form.multiforwid'};
                    334:     }
                    335:     if ($msgkey ne '') {
1.201     raeburn   336:         my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid)=
1.212     raeburn   337:                    split(/\:/,&unescape($msgkey));
1.201     raeburn   338:         $course_context = $origcid;
                    339:     }
                    340:     foreach my $key (keys(%env)) {
                    341:         if ($key=~/^form\.(rep)?rec\_(.*)$/) {
                    342:             my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) =
                    343:                                     split(/\:/,&unescape($2));
                    344:             $course_context = $origcid;
                    345:             last;
                    346:         }
                    347:     }
                    348:     if ($course_context eq '') {
                    349:         $course_context = $env{'request.course.id'};
                    350:     }
                    351:     return $course_context;
                    352: }
                    353: 
1.2       www       354: 
1.3       www       355: sub unpackagemsg {
1.212     raeburn   356:     my ($message,$notoken,$noattachmentlink)=@_;
1.2       www       357:     my %content=();
                    358:     my $parser=HTML::TokeParser->new(\$message);
                    359:     my $token;
                    360:     while ($token=$parser->get_token) {
                    361:        if ($token->[0] eq 'S') {
                    362: 	   my $entry=$token->[1];
                    363:            my $value=$parser->get_text('/'.$entry);
1.156     raeburn   364:            if (($entry eq 'recuser') || ($entry eq 'recdomain')) {
                    365:                push(@{$content{$entry}},$value);
1.162     raeburn   366:            } elsif ($entry eq 'recipient') {
                    367:                my $username = $token->[2]{'username'};
                    368:                $username = &HTML::Entities::decode($username,'<>&"');
                    369:                $content{$entry}{$username} = $value;
1.156     raeburn   370:            } else {
                    371:                $content{$entry}=$value;
                    372:            }
1.2       www       373:        }
                    374:     }
1.168     albertel  375:     if (!exists($content{'recuser'})) { $content{'recuser'} = []; }
1.212     raeburn   376:     if (($content{'attachmenturl'}) && (!$noattachmentlink)) {
1.100     albertel  377:        my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
1.52      www       378:        if ($notoken) {
1.100     albertel  379: 	   $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';
1.52      www       380:        } else {
1.99      albertel  381: 	   &Apache::lonnet::allowuploaded('/adm/msg',
                    382: 					  $content{'attachmenturl'});
                    383: 	   $content{'message'}.='<p>'.&mt('Attachment').
                    384: 	       ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
1.100     albertel  385: 	       $fname.'</tt></a>';
1.52      www       386:        }
                    387:     }
1.2       www       388:     return %content;
                    389: }
                    390: 
1.6       www       391: 
1.159     raeburn   392: sub buildmsgid {
1.191     raeburn   393:     my ($now,$subject,$uname,$udom,$msgcount,$course_context,$symb,$error,$pid) = @_;
1.184     www       394:     $subject=&escape($subject);
1.192     raeburn   395:     $symb = &escape($symb);
1.184     www       396:     return(&escape($now.':'.$subject.':'.$uname.':'.
1.191     raeburn   397:            $udom.':'.$msgcount.':'.$course_context.':'.$pid.':'.$symb.':'.$error));
1.159     raeburn   398: }
                    399: 
1.6       www       400: sub unpackmsgid {
1.239.2.1  raeburn   401:     my ($msgid,$folder,$skipstatus,$status_cache,$onlycid)=@_;
1.184     www       402:     $msgid=&unescape($msgid);
1.167     raeburn   403:     my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid,
1.191     raeburn   404:         $processid,$symb,$error) = split(/\:/,&unescape($msgid));
1.239.2.1  raeburn   405:     if (!defined($processid)) { $fromcid = ''; }
                    406:     if (($onlycid) && ($onlycid ne $fromcid)) {
                    407:         return ($sendtime,'',$fromname,$fromdomain,'',$fromcid,'',$error);
                    408:     }
1.184     www       409:     $shortsubj = &unescape($shortsubj);
1.182     albertel  410:     $shortsubj = &HTML::Entities::decode($shortsubj);
1.192     raeburn   411:     $symb = &unescape($symb);
1.167     raeburn   412:     if (!defined($processid)) { $fromcid = ''; }
1.164     raeburn   413:     my %status=();
                    414:     unless ($skipstatus) {
1.169     albertel  415: 	if (ref($status_cache)) {
                    416: 	    $status{$msgid} = $status_cache->{$msgid};
                    417: 	} else {
                    418: 	    my $suffix=&foldersuffix($folder);
                    419: 	    %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
                    420: 	}
                    421: 	if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
1.164     raeburn   422:         unless ($status{$msgid}) { $status{$msgid}='new'; }
                    423:     }
1.191     raeburn   424:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid,$symb,$error);
1.141     raeburn   425: }
1.6       www       426: 
1.53      www       427: 
                    428: sub sendemail {
1.213     raeburn   429:     my ($to,$subject,$body,$to_uname,$to_udom,$user_lh)=@_;
1.186     www       430:     my $senderaddress='';
1.220     raeburn   431:     my $replytoaddress='';
1.238     raeburn   432:     my $msgsent;
1.220     raeburn   433:     if ($env{'form.can_reply'} eq 'N') {
                    434:         my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
                    435:         my $hostname = &Apache::lonnet::hostname($lonhost);
                    436:         $replytoaddress = 'do-not-reply@'.$hostname;
                    437:     } else {
                    438:         my %senderemails;
                    439:         my $have_sender;
                    440:         if ($env{'form.reply_to_addr'}) {
                    441:             my ($replytoname,$replytodom) = split(/:/,$env{'form.reply_to_addr'});
                    442:             if (!($replytoname eq $env{'user.name'} && $replytodom eq $env{'user.domain'})) {
                    443:                 if (&Apache::lonnet::homeserver($replytoname,$replytodom) ne 'no_host') {
                    444:                     %senderemails = 
                    445:                         &Apache::loncommon::getemails($replytoname,$replytodom);
                    446:                     $have_sender = 1;
                    447:                 }
                    448:             }
                    449:         }
                    450:         if (!$have_sender) {
                    451:             %senderemails=&Apache::loncommon::getemails();
                    452:         }
                    453:         foreach my $type ('permanentemail','critnotification','notification') {
                    454:             if ($senderemails{$type}) {
                    455:                 ($senderaddress) = split(/,/,$senderemails{$type});
                    456:                 last if ($senderaddress);
                    457:             }
                    458:         }
1.186     www       459:     }
1.53      www       460:     $body=
1.217     schafran  461:     "*** ".&mt_user($user_lh,'This is an automatic e-mail generated by the LON-CAPA system.')."\n".
                    462:     "*** ".($senderaddress?&mt_user($user_lh,'You can reply to this e-mail'):&mt_user($user_lh,'Please do not reply to this address.')."\n*** ".
1.220     raeburn   463:     &mt_user($user_lh,'A reply will not be received by the recipient!'))."\n\n".$body;
1.225     jms       464:     
1.53      www       465:     my $msg = new Mail::Send;
                    466:     $msg->to($to);
                    467:     $msg->subject('[LON-CAPA] '.$subject);
1.220     raeburn   468:     if ($replytoaddress) {
                    469:         $msg->add('Reply-to',$replytoaddress);
                    470:     }
                    471:     if ($senderaddress) {
                    472:         $msg->add('From',$senderaddress);
                    473:     }
1.230     bisitz    474:     $msg->add('Content-type','text/plain; charset=UTF-8');
1.97      matthew   475:     if (my $fh = $msg->open()) {
1.172     albertel  476: 	print $fh $body;
                    477: 	$fh->close;
1.238     raeburn   478:         $msgsent = 1;
1.68      www       479:     }
1.238     raeburn   480:     return $msgsent;
1.53      www       481: }
                    482: 
                    483: # ==================================================== Send notification emails
                    484: 
                    485: sub sendnotification {
1.194     raeburn   486:     my ($to,$touname,$toudom,$subj,$crit,$text,$msgid)=@_;
1.140     albertel  487:     my $sender=$env{'environment.firstname'}.' '.$env{'environment.lastname'};
1.131     www       488:     unless ($sender=~/\w/) { 
1.208     raeburn   489: 	$sender=$env{'user.name'}.':'.$env{'user.domain'};
1.131     www       490:     }
1.53      www       491:     my $critical=($crit?' critical':'');
1.238     raeburn   492:     my $numsent = 0;
1.208     raeburn   493: 
1.131     www       494:     $text=~s/\&lt\;/\</gs;
                    495:     $text=~s/\&gt\;/\>/gs;
1.218     raeburn   496:     my $homeserver = &Apache::lonnet::homeserver($touname,$toudom);
                    497:     my $protocol = $Apache::lonnet::protocol{$homeserver};
                    498:     $protocol = 'http' if ($protocol ne 'https');
                    499:     my $url = $protocol.'://'.&Apache::lonnet::hostname($homeserver).
1.226     raeburn   500:               '/adm/email?username='.$touname.'&domain='.$toudom.
                    501:               '&display='.&escape($msgid);
1.194     raeburn   502:     my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid,
                    503:         $symb,$error) = &Apache::lonmsg::unpackmsgid($msgid);
1.208     raeburn   504:     my ($coursetext,$body,$bodybegin,$bodysubj,$bodyend);
1.222     raeburn   505:     my $user_lh = &Apache::loncommon::user_lang($touname,$toudom,$fromcid);
1.194     raeburn   506:     if ($fromcid ne '') {
1.213     raeburn   507:         $coursetext = "\n".&mt_user($user_lh,'Course').': ';
1.194     raeburn   508:         if ($env{'course.'.$fromcid.'.description'} ne '') {
                    509:             $coursetext .= $env{'course.'.$fromcid.'.description'};
                    510:         } else {
                    511:             my %coursehash = &Apache::lonnet::coursedescription($fromcid,);
                    512:             if ($coursehash{'description'} ne '') {
                    513:                 $coursetext .= $coursehash{'description'};
                    514:             }
                    515:         }
                    516:         $coursetext .= "\n\n";
                    517:     }
1.206     raeburn   518:     my @recipients = split(/,/,$to);
1.208     raeburn   519:     $bodybegin = $coursetext. 
1.213     raeburn   520:                &mt_user($user_lh,
                    521:                          'You received a'.$critical.' message from [_1] in LON-CAPA.',$sender).' ';
                    522:     $bodysubj = &mt_user($user_lh,'The subject is 
1.53      www       523: 
1.195     raeburn   524:  [_1]
1.53      www       525: 
1.195     raeburn   526: ',$subj)."\n".
1.213     raeburn   527: '=== '.&mt_user($user_lh,'Excerpt')." ============================================================
1.206     raeburn   528: ";
                    529:     $bodyend = "
1.131     www       530: ========================================================================
                    531: 
1.213     raeburn   532: ".&mt_user($user_lh,'Use 
1.53      www       533: 
1.195     raeburn   534:  [_1]
1.53      www       535: 
1.195     raeburn   536: to access the full message.',$url);
1.206     raeburn   537:     my %userenv = &Apache::lonnet::get('environment',['notifywithhtml'],$toudom,$touname);
1.235     bisitz    538:     my $subject = &mt_user($user_lh,"'New'$critical message from [_1]",$sender);
1.232     raeburn   539:     unless ($subj eq '') {
                    540:         $subject = $subj;
                    541:     }
1.208     raeburn   542:  
1.239.2.3! raeburn   543:     my ($blocked,$blocktext,$clientip);
        !           544:     $clientip = &Apache::lonnet::get_requestor_ip();
1.208     raeburn   545:     if (!$crit) {
                    546:         my %setters;
1.239.2.3! raeburn   547:         my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) = 
        !           548:             &Apache::loncommon::blockcheck(\%setters,'com',$clientip,$touname,$toudom);
1.208     raeburn   549:         if ($startblock && $endblock) {
                    550:             $blocked = 1;
1.209     albertel  551:             my $showstart = &Apache::lonlocal::locallocaltime($startblock);
                    552:             my $showend = &Apache::lonlocal::locallocaltime($endblock);
1.213     raeburn   553:             $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);
1.239.2.3! raeburn   554:         } elsif ($by_ip) {
        !           555:             $blocked = 1;
        !           556:             $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);
1.208     raeburn   557:         }
                    558:     }
1.206     raeburn   559:     if ($userenv{'notifywithhtml'} ne '') {
                    560:         my @htmlexcerpt = split(/,/,$userenv{'notifywithhtml'});
1.226     raeburn   561:         my $htmlfree = &make_htmlfree($text);
1.206     raeburn   562:         foreach my $addr (@recipients) {
1.208     raeburn   563:             if ($blocked) {
                    564:                 $body = $bodybegin."\n".$blocktext."\n".$bodyend;
                    565:             } else {
1.226     raeburn   566:                 my $sendtext;
1.208     raeburn   567:                 if (!grep/^\Q$addr\E/,@htmlexcerpt) {
1.227     raeburn   568:                     $sendtext = $htmlfree;
1.226     raeburn   569:                 } else {
                    570:                     $sendtext = $text;
1.208     raeburn   571:                 }
                    572:                 $body = $bodybegin.$bodysubj.$sendtext.$bodyend;
1.206     raeburn   573:             }
1.238     raeburn   574:             if (&sendemail($addr,$subject,$body,$touname,$toudom,$user_lh)) {
                    575:                 $numsent ++;
                    576:             }
1.206     raeburn   577:         }
                    578:     } else {
1.208     raeburn   579:         if ($blocked) {
                    580:             $body = $bodybegin."\n".$blocktext."\n".$bodyend;
                    581:         } else {
1.226     raeburn   582:             my $htmlfree = &make_htmlfree($text);
                    583:             $body = $bodybegin.$bodysubj.$htmlfree.$bodyend;
1.208     raeburn   584:         }
1.238     raeburn   585:         if (&sendemail($to,$subject,$body,$touname,$toudom,$user_lh)) {
                    586:             $numsent ++;
                    587:         }
1.206     raeburn   588:     }
1.238     raeburn   589:     return $numsent;
1.53      www       590: }
1.215     jms       591: 
1.226     raeburn   592: sub make_htmlfree {
                    593:     my ($text) = @_;
                    594:     $text =~ s/\<\/*[^\>]+\>//gs;
                    595:     $text = &HTML::Entities::decode($text);
                    596:     $text = &Encode::encode('utf8',$text);
                    597:     return $text;
                    598: }
                    599: 
1.223     kaisler   600: sub mynewmail{
                    601: 	&newmail();
1.231     raeburn   602: 	return $env{'user.mailcheck.lastnewmessagetime'} > $env{'user.mailcheck.lastvisit'};
1.223     kaisler   603: }
                    604: 
1.40      www       605: 
                    606: sub newmail {
1.140     albertel  607:     if ((time-$env{'user.mailcheck.time'})>300) {
1.40      www       608:         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
1.211     raeburn   609:         &Apache::lonnet::appenv({'user.mailcheck.time'=>time});
1.231     raeburn   610: 		&Apache::lonnet::appenv({'user.mailcheck.lastnewmessagetime'=> $what{'recnewemail'}});
1.40      www       611:         if ($what{'recnewemail'}>0) { return 1; }
                    612:     }
                    613:     return 0;
                    614: }
                    615: 
1.1       www       616: 
1.58      bowersj2  617: 
1.1       www       618: sub author_res_msg {
                    619:     my ($filename,$message)=@_;
1.2       www       620:     unless ($message) { return 'empty'; }
1.1       www       621:     $filename=&Apache::lonnet::declutter($filename);
1.72      www       622:     my ($domain,$author,@dummy)=split(/\//,$filename);
1.1       www       623:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
                    624:     if ($homeserver ne 'no_host') {
                    625:        my $id=unpack("%32C*",$message);
1.181     albertel  626:        $message .= " <p>This error occurred on machine ".
                    627: 	   $Apache::lonnet::perlvar{'lonHostID'}."</p>";
1.2       www       628:        my $msgid;
1.72      www       629:        ($msgid,$message)=&packagemsg($filename,$message);
1.3       www       630:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
1.72      www       631:          ':nohist_res_msgs:'.
1.184     www       632:           &escape($filename.'_'.$id).'='.
                    633:           &escape($message),$homeserver);
1.1       www       634:     }
1.2       www       635:     return 'no_host';
1.73      www       636: }
                    637: 
1.215     jms       638: 
1.73      www       639: 
                    640: sub retrieve_author_res_msg {
1.75      www       641:     my $url=shift;
1.73      www       642:     $url=&Apache::lonnet::declutter($url);
1.187     albertel  643:     my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.76      www       644:     my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
1.73      www       645:     my $msgs='';
1.239     raeburn   646:     foreach my $msg (keys(%errormsgs)) {
                    647: 	if ($msg =~ /^\Q$url\E\_\d+$/) {
                    648: 	    my %content=&unpackagemsg($errormsgs{$msg});
1.74      www       649: 	    $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
                    650: 		$content{'time'}.'</b>: '.$content{'message'}.
                    651: 		'<br /></p>';
1.73      www       652: 	}
                    653:     } 
                    654:     return $msgs;     
                    655: }
                    656: 
                    657: 
1.215     jms       658: 
                    659: 
1.73      www       660: 
                    661: sub del_url_author_res_msg {
1.75      www       662:     my $url=shift;
1.73      www       663:     $url=&Apache::lonnet::declutter($url);
1.187     albertel  664:     my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.77      www       665:     my @delmsgs=();
1.239     raeburn   666:     foreach my $msg (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
                    667: 	if ($msg =~ /^\Q$url\E\_\d+$/) {
                    668: 	    push (@delmsgs,$msg);
1.77      www       669: 	}
                    670:     }
                    671:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
1.73      www       672: }
1.215     jms       673: 
1.73      www       674: 
1.152     www       675: sub clear_author_res_msg {
                    676:     my $url=shift;
                    677:     $url=&Apache::lonnet::declutter($url);
1.187     albertel  678:     my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.152     www       679:     my @delmsgs=();
1.239     raeburn   680:     foreach my $msg (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
                    681: 	if ($msg =~ /^\Q$url\E/) {
                    682: 	    push (@delmsgs,$msg);
1.152     www       683: 	}
                    684:     }
                    685:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
                    686: }
1.215     jms       687: 
                    688: 
1.73      www       689: 
                    690: sub all_url_author_res_msg {
                    691:     my ($author,$domain)=@_;
1.75      www       692:     my %returnhash=();
1.239     raeburn   693:     foreach my $msg (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
                    694: 	$msg =~ /^(.+)\_\d+/;
1.75      www       695: 	$returnhash{$1}=1;
                    696:     }
                    697:     return %returnhash;
1.1       www       698: }
                    699: 
1.185     albertel  700: 
                    701: sub store_instructor_comment {
                    702:     my ($msg,$uname,$udom) = @_;
                    703:     my $cid  = $env{'request.course.id'};
                    704:     my $cnum = $env{'course.'.$cid.'.num'};
                    705:     my $cdom = $env{'course.'.$cid.'.domain'};
                    706:     my $subject= &mt('Record').' ['.$uname.':'.$udom.']';
                    707:     my $result = &user_normal_msg_raw($cnum,$cdom,$subject,$msg);
1.201     raeburn   708:     if ($result eq 'ok' || $result eq 'con_delayed') {
                    709:         
                    710:     }
1.185     albertel  711:     return $result;
                    712: }
                    713: 
1.1       www       714: 
1.38      www       715: sub user_crit_msg_raw {
1.201     raeburn   716:     my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage,
1.238     raeburn   717:         $nosentstore,$recipid,$attachmenturl,$permresults)=@_;
1.2       www       718: # Check if allowed missing
1.190     raeburn   719:     my ($status,$packed_message);
1.2       www       720:     my $msgid='undefined';
                    721:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
1.131     www       722:     my $text=$message;
1.2       www       723:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    724:     if ($homeserver ne 'no_host') {
1.202     raeburn   725:        ($msgid,$packed_message)=&packagemsg($subject,$message,undef,undef,
1.212     raeburn   726:                                   $attachmenturl,undef,undef,undef,undef,undef,
                    727:                                   undef,undef,$recipid);
1.190     raeburn   728:        if ($sendback) { $packed_message.='<sendback>true</sendback>'; }
1.210     albertel  729:        $status=&Apache::lonnet::cput('critical', {$msgid => $packed_message},
                    730: 				     $domain,$user);
1.159     raeburn   731:         if (defined($sentmessage)) {
1.190     raeburn   732:             $$sentmessage = $packed_message;
1.159     raeburn   733:         }
1.201     raeburn   734:         if (!$nosentstore) {
1.193     raeburn   735:             (undef,my $packed_message_no_citation) =
1.212     raeburn   736:             &packagemsg($subject,$message,undef,undef,$attachmenturl,$user,
                    737:                         $domain,$msgid);
1.193     raeburn   738:             if ($status eq 'ok' || $status eq 'con_delayed') {
                    739:                 &store_sent_mail($msgid,$packed_message_no_citation);
                    740:             }
                    741:         }
1.2       www       742:     } else {
                    743:        $status='no_host';
                    744:     }
1.190     raeburn   745: 
1.53      www       746: # Notifications
1.186     www       747:     my %userenv = &Apache::loncommon::getemails($user,$domain);
1.238     raeburn   748:     my $critnotify = $userenv{'critnotification'};
                    749:     my $permemail = $userenv{'permanentemail'};
                    750:     my $numcrit = 0;
                    751:     my $numperm = 0;
                    752:     my $permlogmsgstatus;
                    753:     if ($critnotify) {
                    754:         $numcrit = &sendnotification($critnotify,$user,$domain,$subject,1,$text,$msgid);
                    755:     }
                    756:     if ($toperm && $permemail) {
                    757:         if ($critnotify && $numcrit) {
                    758:             if (grep(/^\Q$permemail\E/,split(/,/,$critnotify))) {
                    759:                 $numperm = 1;
                    760:             }
                    761:         }
                    762:         unless ($numperm) {
                    763:             $numperm = &sendnotification($permemail,$user,$domain,$subject,1,$text,$msgid);
                    764:         }
                    765:     }
                    766:     if ($toperm) {
                    767:         $permlogmsgstatus = '. Perm. email log status '.
                    768:                             &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
                    769:                                                  "Perm. e-mail count $numperm for $user at $domain");
                    770:         if (ref($permresults) eq 'HASH') {
                    771:             $permresults->{"$user:$domain"} = $numperm;
                    772:         }
1.132     www       773:     }
1.53      www       774: # Log this
1.2       www       775:     &Apache::lonnet::logthis(
1.238     raeburn   776:       'Sending critical '.$msgid.
1.2       www       777:       ', log status: '.
1.140     albertel  778:       &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    779:                          $env{'user.home'},
1.238     raeburn   780:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status '
                    781:       .$status).$permlogmsgstatus);
1.2       www       782:     return $status;
                    783: }
                    784: 
1.38      www       785: 
1.58      bowersj2  786: 
1.38      www       787: sub user_crit_msg {
1.201     raeburn   788:     my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage,
1.238     raeburn   789:         $nosentstore,$recipid,$attachmenturl,$permresults)=@_;
1.183     albertel  790:     my @status;
1.38      www       791:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    792:                                        $domain,$user);
                    793:     my $msgforward=$userenv{'msgforward'};
                    794:     if ($msgforward) {
1.183     albertel  795:        foreach my $addr (split(/\,/,$msgforward)) {
                    796: 	 my ($forwuser,$forwdomain)=split(/\:/,$addr);
                    797:          push(@status,
                    798: 	      &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
1.202     raeburn   799: 				 $sendback,$toperm,$sentmessage,$nosentstore,
1.238     raeburn   800:                                  $recipid,$attachmenturl,$permresults));
1.38      www       801:        }
                    802:     } else { 
1.183     albertel  803: 	push(@status,
                    804: 	     &user_crit_msg_raw($user,$domain,$subject,$message,$sendback,
1.212     raeburn   805: 				$toperm,$sentmessage,$nosentstore,$recipid,
1.238     raeburn   806:                                 $attachmenturl,$permresults));
1.38      www       807:     }
1.183     albertel  808:     if (wantarray) {
                    809: 	return @status;
                    810:     }
                    811:     return join(' ',@status);
1.38      www       812: }
                    813: 
1.2       www       814: 
                    815: sub user_crit_received {
1.12      www       816:     my $msgid=shift;
                    817:     my %message=&Apache::lonnet::get('critical',[$msgid]);
1.52      www       818:     my %contents=&unpackagemsg($message{$msgid},1);
1.204     raeburn   819:     my $destname = $contents{'sendername'};
                    820:     my $destdom = $contents{'senderdomain'};
                    821:     if ($contents{'replytoaddr'}) {
                    822:         my ($repname,$repdom) = split(/:/,$contents{'replytoaddr'});
                    823:         if (&Apache::lonnet::homeserver($repname,$repdom) ne 'no_host') {
                    824:             $destname = $repname;
                    825:             $destdom = $repdom;    
                    826:         }
                    827:     }
1.24      www       828:     my $status='rec: '.($contents{'sendback'}?
1.204     raeburn   829:      &user_normal_msg($destname,$destdom,&mt('Receipt').': '.$env{'user.name'}.
                    830:                       ' '.&mt('at').' '.$env{'user.domain'}.', '.
                    831:                       $contents{'subject'},&mt('User').' '.$env{'user.name'}.
                    832:                       ' '.&mt('at').' '.$env{'user.domain'}.
                    833:                       ' acknowledged receipt of message'."\n".'   "'.
                    834:                       $contents{'subject'}.'"'."\n".&mt('dated').' '.
                    835:                       $contents{'time'}.".\n"
                    836:                       ):'no msg req');
1.5       www       837:     $status.=' trans: '.
1.12      www       838:      &Apache::lonnet::put(
                    839:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
1.5       www       840:     $status.=' del: '.
1.9       albertel  841:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.140     albertel  842:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    843:                          $env{'user.home'},'Received critical message '.
1.5       www       844:                          $contents{'msgid'}.
                    845:                          ', '.$status);
1.12      www       846:     return $status;
1.2       www       847: }
                    848: 
1.215     jms       849: 
                    850: 
1.2       www       851: 
1.38      www       852: sub user_normal_msg_raw {
1.132     www       853:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.191     raeburn   854:         $toperm,$currid,$newid,$sentmessage,$crsmsgid,$symb,$restitle,
1.238     raeburn   855:         $error,$nosentstore,$recipid,$permresults)=@_;
1.2       www       856: # Check if allowed missing
1.173     albertel  857:     my ($status,$packed_message);
1.2       www       858:     my $msgid='undefined';
1.131     www       859:     my $text=$message;
1.2       www       860:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
                    861:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    862:     if ($homeserver ne 'no_host') {
1.173     albertel  863:        ($msgid,$packed_message)=
                    864: 	                 &packagemsg($subject,$message,$citation,$baseurl,
1.174     raeburn   865:                                      $attachmenturl,$user,$domain,$currid,
1.202     raeburn   866:                                      undef,$crsmsgid,$symb,$error,$recipid);
1.174     raeburn   867: 
1.108     www       868: # Store in user folder
1.210     albertel  869:        $status=
                    870: 	   &Apache::lonnet::cput('nohist_email',{$msgid => $packed_message},
                    871: 				 $domain,$user);
1.108     www       872: # Save new message received time
1.40      www       873:        &Apache::lonnet::put
                    874:                          ('email_status',{'recnewemail'=>time},$domain,$user);
1.201     raeburn   875: # Into sent-mail folder if sent mail storage required
                    876:        if (!$nosentstore) {
1.190     raeburn   877:            (undef,my $packed_message_no_citation) =
                    878:                &packagemsg($subject,$message,undef,$baseurl,$attachmenturl,
1.191     raeburn   879:                            $user,$domain,$currid,undef,$crsmsgid,$symb,$error);
1.193     raeburn   880:            if ($status eq 'ok' || $status eq 'con_delayed') {
                    881:                &store_sent_mail($msgid,$packed_message_no_citation);
                    882:            }
1.156     raeburn   883:        }
1.201     raeburn   884:        if (ref($newid) eq 'SCALAR') {
1.196     www       885: 	   $$newid = $msgid;
                    886:        }
1.201     raeburn   887:        if (ref($sentmessage) eq 'SCALAR') {
1.196     www       888: 	   $$sentmessage = $packed_message;
                    889:        }
                    890: # Notifications
1.206     raeburn   891:        my %userenv = &Apache::loncommon::getemails($user,$domain);
1.238     raeburn   892:        my $notify = $userenv{'notification'};
                    893:        my $permemail = $userenv{'permanentemail'};
                    894:        my $numnotify = 0;
                    895:        my $numperm = 0;
                    896:        my $permlogmsgstatus;
                    897:        if ($notify) {
                    898:            $numnotify = &sendnotification($notify,$user,$domain,$subject,0,$text,$msgid);
                    899:        }
                    900:        if ($toperm && $permemail) {
                    901:            if ($notify && $numnotify) {
                    902:                if (grep(/^\Q$permemail\E/,split(/,/,$notify))) {
                    903:                    $numperm = 1;
                    904:                }
                    905:            }
                    906:            unless ($numperm) {
                    907:                $numperm = &sendnotification($permemail,$user,$domain,$subject,0,
                    908:                                             $text,$msgid);
                    909:            }
1.196     www       910:        }
1.238     raeburn   911:        if ($toperm) {
                    912:            $permlogmsgstatus = '. Perm. email log status '.
                    913:                          &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
                    914:                                               "Perm. e-mail count $numperm for $user at $domain");
                    915:            if (ref($permresults) eq 'HASH') {
                    916:                $permresults->{"$user:$domain"} = $numperm;
1.214     raeburn   917:            }
1.196     www       918:        }
                    919:        &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    920: 			    $env{'user.home'},
1.238     raeburn   921: 			    'Sending '.$msgid.' to '.$user.' at '.$domain.' with status '.$status.
                    922:                             $permlogmsgstatus);
1.196     www       923:    } else {
1.2       www       924:        $status='no_host';
1.196     www       925:    }
1.2       www       926:     return $status;
                    927: }
1.38      www       928: 
                    929: sub user_normal_msg {
1.132     www       930:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.238     raeburn   931: 	$toperm,$sentmessage,$symb,$restitle,$error,$nosentstore,$recipid,
                    932:         $permresults)=@_;
1.199     raeburn   933:     my @status;
1.38      www       934:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    935:                                        $domain,$user);
                    936:     my $msgforward=$userenv{'msgforward'};
                    937:     if ($msgforward) {
1.239     raeburn   938:         foreach my $fwd (split(/\,/,$msgforward)) {
                    939: 	    my ($forwuser,$forwdomain)=split(/\:/,$fwd);
1.199     raeburn   940: 	    push(@status,
1.171     banghart  941: 	        &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
1.172     albertel  942: 				     $citation,$baseurl,$attachmenturl,$toperm,
1.201     raeburn   943: 				     undef,undef,$sentmessage,undef,$symb,
1.238     raeburn   944:                                      $restitle,$error,$nosentstore,$recipid,$permresults));
1.171     banghart  945:         }
1.191     raeburn   946:     } else {
1.199     raeburn   947: 	push(@status,&user_normal_msg_raw($user,$domain,$subject,$message,
1.172     albertel  948: 				     $citation,$baseurl,$attachmenturl,$toperm,
1.201     raeburn   949: 				     undef,undef,$sentmessage,undef,$symb,
1.238     raeburn   950:                                      $restitle,$error,$nosentstore,$recipid,$permresults));
1.199     raeburn   951:     }
                    952:     if (wantarray) {
                    953:         return @status;
1.38      www       954:     }
1.199     raeburn   955:     return join(' ',@status);
1.38      www       956: }
                    957: 
1.201     raeburn   958: sub process_sent_mail {
1.236     raeburn   959:     my ($msgsubj,$subj_prefix,$numsent,$stamp,$msgname,$msgdom,$msgcount,
                    960:         $context,$pid,$savemsg,$recusers,$recudoms,$baseurl,$attachmenturl,
                    961:         $symb,$error,$senderuname,$senderdom,$recipid) = @_;
1.201     raeburn   962:     my $sentsubj;
                    963:     if ($numsent > 1) {
                    964:         $sentsubj = $subj_prefix.' ('.$numsent.' sent) '.$msgsubj;
1.205     raeburn   965:     } else {
                    966:         if ($subj_prefix) {
                    967:             $sentsubj = $subj_prefix.' ';
                    968:         }
                    969:         $sentsubj .= $msgsubj;
1.201     raeburn   970:     }
                    971:     $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"');
                    972:     my $sentmsgid = 
                    973:         &buildmsgid($stamp,$sentsubj,$msgname,$msgdom,$msgcount,$context,$pid);
                    974:     (undef,my $sentmessage) =
                    975:         &packagemsg($msgsubj,$savemsg,undef,$baseurl,$attachmenturl,$recusers,
1.214     raeburn   976:                     $recudoms,$sentmsgid,undef,undef,$symb,$error,$recipid);
1.201     raeburn   977:     my $status = &store_sent_mail($sentmsgid,$sentmessage,$senderuname,
1.210     albertel  978:                                   $senderdom);
1.201     raeburn   979:     return $status;
                    980: }
                    981: 
1.156     raeburn   982: sub store_sent_mail {
1.210     albertel  983:     my ($msgid,$message,$senderuname,$senderdom) = @_;
1.201     raeburn   984:     if ($senderuname eq '') {
                    985:         $senderuname = $env{'user.name'};
                    986:     }
                    987:     if ($senderdom eq '') {
                    988:         $senderdom = $env{'user.domain'};
                    989:     }
1.210     albertel  990:     my $status =' '.&Apache::lonnet::cput('nohist_email_sent',
                    991: 					  {$msgid => $message},
                    992: 					  $senderdom,$senderuname);
1.156     raeburn   993:     return $status;
                    994: }
1.2       www       995: 
1.202     raeburn   996: sub store_recipients {
                    997:     my ($subject,$sendername,$senderdom,$reciphash) = @_;
                    998:     my $context = &get_course_context();
1.203     albertel  999:     my $now = time();
1.202     raeburn  1000:     my $msgcount = &get_uniq();
                   1001:     my $recipid =
                   1002:         &buildmsgid($now,$subject,$sendername,$senderdom,$msgcount,$context,$$);
                   1003:     my %recipinfo = (
                   1004:                          $recipid => $reciphash,
                   1005:                     );
                   1006:     my $status = &Apache::lonnet::put('nohist_emailrecip',\%recipinfo,
                   1007:                                       $senderdom,$sendername); 
                   1008:     if ($status eq 'ok') {
                   1009:         return ($recipid,$status);
                   1010:     } else {
                   1011:         return (undef,$status);
                   1012:     }
                   1013: }
                   1014: 
1.106     www      1015: 
                   1016: sub foldersuffix {
                   1017:     my $folder=shift;
                   1018:     unless ($folder) { return ''; }
1.189     raeburn  1019:     my $suffix;
                   1020:     my %folderhash = &get_user_folders($folder);
                   1021:     if (ref($folderhash{$folder}) eq 'HASH') {
                   1022:         $suffix = '_'.&escape($folderhash{$folder}{'id'});
                   1023:     } else {
                   1024:         $suffix = '_'.&escape($folder);
                   1025:     }
                   1026:     return $suffix;
                   1027: }
                   1028: 
                   1029: 
                   1030: sub get_user_folders {
                   1031:     my ($folder) = @_;
                   1032:     my %userfolders = 
                   1033:           &Apache::lonnet::dump('email_folders',undef,undef,$folder);
                   1034:     my $lock = "\0".'lock_counter'; # locks db while counter incremented
                   1035:     my $counter = "\0".'idcount';   # used in suffix for email db files
                   1036:     if (defined($userfolders{$lock})) {
                   1037:         delete($userfolders{$lock});
                   1038:     }
                   1039:     if (defined($userfolders{$counter})) {
                   1040:         delete($userfolders{$counter});
                   1041:     }
                   1042:     return %userfolders;
1.106     www      1043: }
                   1044: 
1.197     albertel 1045: sub secapply {
                   1046:     my $rec=shift;
                   1047:     my $defaultflag=shift;
                   1048:     $rec=~s/\s+//g;
1.229     raeburn  1049:     unless ($rec =~ /\:/) {
                   1050:         $rec=~s/\@/\:/g;
                   1051:     }
1.197     albertel 1052:     my ($adr,$sections_or_groups)=($rec=~/^([^\(]+)\(([^\)]+)\)/);
                   1053:     if ($sections_or_groups) {
                   1054: 	foreach my $item (split(/\;/,$sections_or_groups)) {
                   1055:             if (($item eq $env{'request.course.sec'}) ||
                   1056:                 ($defaultflag && ($item eq '*'))) {
                   1057:                 return $adr; 
                   1058:             } elsif ($env{'request.course.groups'}) {
                   1059:                 my @usersgroups = split(/:/,$env{'request.course.groups'});
                   1060:                 if (grep(/^\Q$item\E$/,@usersgroups)) {
                   1061:                     return $adr;
                   1062:                 }
                   1063:             } 
                   1064:         }
                   1065:     } else {
                   1066:        return $rec;
                   1067:     }
                   1068:     return '';
                   1069: }
                   1070: 
                   1071: sub decide_receiver {
                   1072:     my ($feedurl,$author,$question,$course,$policy,$defaultflag) = @_;
                   1073:     &Apache::lonenc::check_decrypt(\$feedurl);
                   1074:     my $typestyle='';
                   1075:     my %to=();
                   1076:     if ($env{'form.discuss'} eq 'author' ||$author) {
                   1077: 	$typestyle.='Submitting as Author Feedback<br />';
                   1078: 	$feedurl=~ m{^/res/($LONCAPA::domain_re)/($LONCAPA::username_re)/};
                   1079: 	$to{$2.':'.$1}=1;
                   1080:     }
                   1081:     my $cid = $env{'request.course.id'};
                   1082:     if ($env{'form.discuss'} eq 'question' ||$question) {
                   1083: 	$typestyle.=&mt('Submitting as Question').'<br />';
                   1084: 	foreach my $item (split(/\,/,$env{'course.'.$cid.'.question.email'})) {
                   1085: 	    my $rec=&secapply($item,$defaultflag);
                   1086: 	    if ($rec) { $to{$rec}=1; }
                   1087: 	} 
                   1088:     }
                   1089:     if ($env{'form.discuss'} eq 'course' ||$course) {
                   1090: 	$typestyle.=&mt('Submitting as Comment').'<br />';
                   1091: 	foreach my $item (split(/\,/,$env{'course.'.$cid.'.comment.email'})) {
                   1092: 	    my $rec=&secapply($item,$defaultflag);
                   1093: 	    if ($rec) { $to{$rec}=1; }
                   1094: 	} 
                   1095:     }
                   1096:     if ($env{'form.discuss'} eq 'policy' ||$policy) {
                   1097: 	$typestyle.=&mt('Submitting as Policy Feedback').'<br />';
                   1098: 	foreach my $item (split(/\,/,$env{'course.'.$cid.'.policy.email'})) {
                   1099: 	    my $rec=&secapply($item,$defaultflag);
                   1100: 	    if ($rec) { $to{$rec}=1; }
                   1101: 	} 
                   1102:     }
                   1103:     if ((scalar(%to) eq '0') && (!$defaultflag)) {
                   1104: 	($typestyle,%to)=
                   1105: 	    &decide_receiver($feedurl,$author,$question,$course,$policy,1);
                   1106:     }
                   1107:     return ($typestyle,%to);
                   1108: }
                   1109: 
1.180     albertel 1110: 1;
1.1       www      1111: __END__
                   1112: 

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