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

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

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