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

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

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