File:  [LON-CAPA] / loncom / interface / lonmsg.pm
Revision 1.84: download - view: text, annotated - select for diffs
Sat Jan 31 02:42:44 2004 UTC (20 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #2666: Explanation of what "Confirm Receipt" does.

    1: # The LearningOnline Network with CAPA
    2: # Routines for messaging
    3: #
    4: # $Id: lonmsg.pm,v 1.84 2004/01/31 02:42:44 www Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: 
   30: package Apache::lonmsg;
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::lonmsg: supports internal messaging
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: lonmsg provides routines for sending messages, receiving messages, and
   41: a handler to allow users to read, send, and delete messages.
   42: 
   43: =head1 OVERVIEW
   44: 
   45: =head2 Messaging Overview
   46: 
   47: X<messages>LON-CAPA provides an internal messaging system similar to
   48: email, but customized for LON-CAPA's usage. LON-CAPA implements its
   49: own messaging system, rather then building on top of email, because of
   50: the features LON-CAPA messages can offer that conventional e-mail can
   51: not:
   52: 
   53: =over 4
   54: 
   55: =item * B<Critical messages>: A message the recipient B<must>
   56: acknowlegde receipt of before they are allowed to continue using the
   57: system, preventing a user from claiming they never got a message
   58: 
   59: =item * B<Receipts>: LON-CAPA can reliably send reciepts informing the
   60: sender that it has been read; again, useful for preventing students
   61: from claiming they did not see a message. (While conventional e-mail
   62: has some reciept support, it's sporadic, e-mail client-specific, and
   63: generally the receiver can opt to not send one, making it useless in
   64: this case.)
   65: 
   66: =item * B<Context>: LON-CAPA knows about the sender, such as where
   67: they are in a course. When a student mails an instructor asking for
   68: help on the problem, the instructor receives not just the student's
   69: question, but all submissions the student has made up to that point,
   70: the user's rendering of the problem, and the complete view the student
   71: saw of the resource, including discussion up to that point. Finally,
   72: the instructor is reading all of this inside of LON-CAPA, not their
   73: email program, so they have full access to LON-CAPA's grading
   74: interface, or other features they may wish to use in response to the
   75: student's query.
   76: 
   77: =back
   78: 
   79: Users can ask LON-CAPA to forward messages to conventional e-mail
   80: addresses on their B<PREF> screen, but generally, LON-CAPA messages
   81: are much more useful then traditional email can be made to be, even
   82: with HTML support.
   83: 
   84: Right now, this document will cover just how to send a message, since
   85: it is likely you will not need to programmatically read messages,
   86: since lonmsg already implements that functionality.
   87: 
   88: =head1 FUNCTIONS
   89: 
   90: =over 4
   91: 
   92: =cut
   93: 
   94: use strict;
   95: use Apache::lonnet();
   96: use vars qw($msgcount);
   97: use HTML::TokeParser();
   98: use Apache::Constants qw(:common);
   99: use Apache::loncommon();
  100: use Apache::lontexconvert();
  101: use HTML::Entities();
  102: use Mail::Send;
  103: use Apache::lonlocal;
  104: 
  105: # Querystring component with sorting type
  106: my $sqs;
  107: 
  108: # ===================================================================== Package
  109: 
  110: sub packagemsg {
  111:     my ($subject,$message,$citation,$baseurl,$attachmenturl)=@_;
  112:     $message =&HTML::Entities::encode($message);
  113:     $citation=&HTML::Entities::encode($citation);
  114:     $subject =&HTML::Entities::encode($subject);
  115:     #remove machine specification
  116:     $baseurl =~ s|^http://[^/]+/|/|;
  117:     $baseurl =&HTML::Entities::encode($baseurl);
  118:     #remove machine specification
  119:     $attachmenturl =~ s|^http://[^/]+/|/|;
  120:     $attachmenturl =&HTML::Entities::encode($attachmenturl);
  121: 
  122:     my $now=time;
  123:     $msgcount++;
  124:     my $partsubj=$subject;
  125:     $partsubj=&Apache::lonnet::escape($partsubj);
  126:     my $msgid=&Apache::lonnet::escape(
  127:            $now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
  128:            $ENV{'user.domain'}.':'.$msgcount.':'.$$);
  129:     my $result='<sendername>'.$ENV{'user.name'}.'</sendername>'.
  130:            '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
  131:            '<subject>'.$subject.'</subject>'.
  132: 	   '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>'.
  133: 	   '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
  134:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
  135: 	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
  136: 	   '<browsertype>'.$ENV{'browser.type'}.'</browsertype>'.
  137: 	   '<browseros>'.$ENV{'browser.os'}.'</browseros>'.
  138: 	   '<browserversion>'.$ENV{'browser.version'}.'</browserversion>'.
  139:            '<browsermathml>'.$ENV{'browser.mathml'}.'</browsermathml>'.
  140: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
  141: 	   '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
  142: 	   '<role>'.$ENV{'request.role'}.'</role>'.
  143: 	   '<resource>'.$ENV{'request.filename'}.'</resource>'.
  144:            '<msgid>'.$msgid.'</msgid>'.
  145: 	   '<message>'.$message.'</message>';
  146:     if (defined($citation)) {
  147: 	$result.='<citation>'.$citation.'</citation>';
  148:     }
  149:     if (defined($baseurl)) {
  150: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
  151:     }
  152:     if (defined($attachmenturl)) {
  153: 	$result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
  154:     }
  155:     return $msgid,$result;
  156: }
  157: 
  158: # ================================================== Unpack message into a hash
  159: 
  160: sub unpackagemsg {
  161:     my ($message,$notoken)=@_;
  162:     my %content=();
  163:     my $parser=HTML::TokeParser->new(\$message);
  164:     my $token;
  165:     while ($token=$parser->get_token) {
  166:        if ($token->[0] eq 'S') {
  167: 	   my $entry=$token->[1];
  168:            my $value=$parser->get_text('/'.$entry);
  169:            $content{$entry}=$value;
  170:        }
  171:     }
  172:     if ($content{'attachmenturl'}) {
  173:        my ($fname,$ft)=($content{'attachmenturl'}=~/\/(\w+)\.(\w+)$/);
  174:        if ($notoken) {
  175: 	   $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'.'.$ft.'</tt>';
  176:        } else {
  177: 	   $content{'message'}.='<p>'.&mt('Attachment').': <a href="'.
  178: 	       &Apache::lonnet::tokenwrapper($content{'attachmenturl'}).
  179: 	       '"><tt>'.$fname.'.'.$ft.'</tt></a>';
  180:        }
  181:     }
  182:     return %content;
  183: }
  184: 
  185: # ======================================================= Get info out of msgid
  186: 
  187: sub unpackmsgid {
  188:     my $msgid=&Apache::lonnet::unescape(shift);
  189:     my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
  190:                           &Apache::lonnet::unescape($msgid));
  191:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
  192:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  193:     unless ($status{$msgid}) { $status{$msgid}='new'; }
  194:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
  195: } 
  196: 
  197: 
  198: sub sendemail {
  199:     my ($to,$subject,$body)=@_;
  200:     $body=
  201:     "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
  202:     "*** ".&mt('Please do not reply to this address.')."\n\n".$body;
  203:     my $msg = new Mail::Send;
  204:     $msg->to($to);
  205:     $msg->subject('[LON-CAPA] '.$subject);
  206:     if (my $fh = $msg->open('smtp',Server => 'localhost')) {
  207: 	print $fh $body;
  208: 	$fh->close;
  209:     }
  210: }
  211: 
  212: # ==================================================== Send notification emails
  213: 
  214: sub sendnotification {
  215:     my ($to,$touname,$toudom,$subj,$crit)=@_;
  216:     my $sender=$ENV{'environment.firstname'}.' '.$ENV{'environment.lastname'};
  217:     my $critical=($crit?' critical':'');
  218:     my $url='http://'.
  219:       $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
  220:       '/adm/email?username='.$touname.'&domain='.$toudom;
  221:     my $body=(<<ENDMSG);
  222: You received a$critical message from $sender in LON-CAPA. The subject is
  223: 
  224:  $subj
  225: 
  226: Use
  227: 
  228:  $url
  229: 
  230: to access this message.
  231: ENDMSG
  232:     &sendemail($to,'New'.$critical.' message from '.$sender,$body);
  233: }
  234: # ============================================================= Check for email
  235: 
  236: sub newmail {
  237:     if ((time-$ENV{'user.mailcheck.time'})>300) {
  238:         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
  239:         &Apache::lonnet::appenv('user.mailcheck.time'=>time);
  240:         if ($what{'recnewemail'}>0) { return 1; }
  241:     }
  242:     return 0;
  243: }
  244: 
  245: # =============================== Automated message to the author of a resource
  246: 
  247: =pod
  248: 
  249: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
  250:     of the resource with the URI $filename.
  251: 
  252: =cut
  253: 
  254: sub author_res_msg {
  255:     my ($filename,$message)=@_;
  256:     unless ($message) { return 'empty'; }
  257:     $filename=&Apache::lonnet::declutter($filename);
  258:     my ($domain,$author,@dummy)=split(/\//,$filename);
  259:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
  260:     if ($homeserver ne 'no_host') {
  261:        my $id=unpack("%32C*",$message);
  262:        my $msgid;
  263:        ($msgid,$message)=&packagemsg($filename,$message);
  264:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
  265:          ':nohist_res_msgs:'.
  266:           &Apache::lonnet::escape($filename.'_'.$id).'='.
  267:           &Apache::lonnet::escape($message),$homeserver);
  268:     }
  269:     return 'no_host';
  270: }
  271: 
  272: # =========================================== Retrieve author resource messages
  273: 
  274: sub retrieve_author_res_msg {
  275:     my $url=shift;
  276:     $url=&Apache::lonnet::declutter($url);
  277:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
  278:     my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
  279:     my $msgs='';
  280:     foreach (keys %errormsgs) {
  281: 	if ($_=~/^\Q$url\E\_\d+$/) {
  282: 	    my %content=&unpackagemsg($errormsgs{$_});
  283: 	    $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
  284: 		$content{'time'}.'</b>: '.$content{'message'}.
  285: 		'<br /></p>';
  286: 	}
  287:     } 
  288:     return $msgs;     
  289: }
  290: 
  291: 
  292: # =============================== Delete all author messages related to one URL
  293: 
  294: sub del_url_author_res_msg {
  295:     my $url=shift;
  296:     $url=&Apache::lonnet::declutter($url);
  297:     my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
  298:     my @delmsgs=();
  299:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
  300: 	if ($_=~/^\Q$url\E\_\d+$/) {
  301: 	    push (@delmsgs,$_);
  302: 	}
  303:     }
  304:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
  305: }
  306: 
  307: # ================= Return hash with URLs for which there is a resource message
  308: 
  309: sub all_url_author_res_msg {
  310:     my ($author,$domain)=@_;
  311:     my %returnhash=();
  312:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
  313: 	$_=~/^(.+)\_\d+/;
  314: 	$returnhash{$1}=1;
  315:     }
  316:     return %returnhash;
  317: }
  318: 
  319: # ================================================== Critical message to a user
  320: 
  321: sub user_crit_msg_raw {
  322:     my ($user,$domain,$subject,$message,$sendback)=@_;
  323: # Check if allowed missing
  324:     my $status='';
  325:     my $msgid='undefined';
  326:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  327:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  328:     if ($homeserver ne 'no_host') {
  329:        ($msgid,$message)=&packagemsg($subject,$message);
  330:        if ($sendback) { $message.='<sendback>true</sendback>'; }
  331:        $status=&Apache::lonnet::critical(
  332:            'put:'.$domain.':'.$user.':critical:'.
  333:            &Apache::lonnet::escape($msgid).'='.
  334:            &Apache::lonnet::escape($message),$homeserver);
  335:        if ($ENV{'request.course.id'}) {
  336:           &user_normal_msg_raw(
  337:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  338:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  339:             'Critical ['.$user.':'.$domain.']',
  340: 	    $message);
  341:        }
  342:     } else {
  343:        $status='no_host';
  344:     }
  345: # Notifications
  346:     my %userenv = &Apache::lonnet::get('environment',['critnotification'],
  347:                                        $domain,$user);
  348:     if ($userenv{'critnotification'}) {
  349:       &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1);
  350:     }
  351: # Log this
  352:     &Apache::lonnet::logthis(
  353:       'Sending critical email '.$msgid.
  354:       ', log status: '.
  355:       &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  356:                          $ENV{'user.home'},
  357:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
  358:       .$status));
  359:     return $status;
  360: }
  361: 
  362: # New routine that respects "forward" and calls old routine
  363: 
  364: =pod
  365: 
  366: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback)>: Sends
  367:     a critical message $message to the $user at $domain. If $sendback is true,
  368:     a reciept will be sent to the current user when $user recieves the message.
  369: 
  370: =cut
  371: 
  372: sub user_crit_msg {
  373:     my ($user,$domain,$subject,$message,$sendback)=@_;
  374:     my $status='';
  375:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
  376:                                        $domain,$user);
  377:     my $msgforward=$userenv{'msgforward'};
  378:     if ($msgforward) {
  379:        foreach (split(/\,/,$msgforward)) {
  380: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
  381:          $status.=
  382: 	   &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
  383:                 $sendback).' ';
  384:        }
  385:     } else { 
  386: 	$status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback);
  387:     }
  388:     return $status;
  389: }
  390: 
  391: # =================================================== Critical message received
  392: 
  393: sub user_crit_received {
  394:     my $msgid=shift;
  395:     my %message=&Apache::lonnet::get('critical',[$msgid]);
  396:     my %contents=&unpackagemsg($message{$msgid},1);
  397:     my $status='rec: '.($contents{'sendback'}?
  398:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
  399:                      &mt('Receipt').': '.$ENV{'user.name'}.' '.&mt('at').' '.$ENV{'user.domain'}.', '.$contents{'subject'},
  400:                      &mt('User').' '.$ENV{'user.name'}.' '.&mt('at').' '.$ENV{'user.domain'}.
  401:                      ' acknowledged receipt of message'."\n".'   "'.
  402:                      $contents{'subject'}.'"'."\n".&mt('dated').' '.
  403:                      $contents{'time'}.".\n"
  404:                      ):'no msg req');
  405:     $status.=' trans: '.
  406:      &Apache::lonnet::put(
  407:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
  408:     $status.=' del: '.
  409:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
  410:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  411:                          $ENV{'user.home'},'Received critical message '.
  412:                          $contents{'msgid'}.
  413:                          ', '.$status);
  414:     return $status;
  415: }
  416: 
  417: # ======================================================== Normal communication
  418: 
  419: sub user_normal_msg_raw {
  420:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
  421: # Check if allowed missing
  422:     my $status='';
  423:     my $msgid='undefined';
  424:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  425:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  426:     if ($homeserver ne 'no_host') {
  427:        ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl,
  428:                                      $attachmenturl);
  429:        $status=&Apache::lonnet::critical(
  430:            'put:'.$domain.':'.$user.':nohist_email:'.
  431:            &Apache::lonnet::escape($msgid).'='.
  432:            &Apache::lonnet::escape($message),$homeserver);
  433:        &Apache::lonnet::put
  434:                          ('email_status',{'recnewemail'=>time},$domain,$user);
  435:     } else {
  436:        $status='no_host';
  437:     }
  438: # Notifications
  439:     my %userenv = &Apache::lonnet::get('environment',['notification'],
  440:                                        $domain,$user);
  441:     if ($userenv{'notification'}) {
  442: 	&sendnotification($userenv{'notification'},$user,$domain,$subject,0);
  443:     }
  444:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  445:                          $ENV{'user.home'},
  446:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
  447:     return $status;
  448: }
  449: 
  450: # New routine that respects "forward" and calls old routine
  451: 
  452: =pod
  453: 
  454: =item * B<user_normal_msg($user, $domain, $subject, $message,
  455:     $citation, $baseurl, $attachmenturl)>: Sends a message to the
  456:     $user at $domain, with subject $subject and message $message.
  457: 
  458: =cut
  459: 
  460: sub user_normal_msg {
  461:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
  462:     my $status='';
  463:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
  464:                                        $domain,$user);
  465:     my $msgforward=$userenv{'msgforward'};
  466:     if ($msgforward) {
  467:        foreach (split(/\,/,$msgforward)) {
  468: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
  469:          $status.=
  470: 	  &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
  471: 			       $citation,$baseurl,$attachmenturl).' ';
  472:        }
  473:     } else { 
  474: 	$status=&user_normal_msg_raw($user,$domain,$subject,$message,
  475: 				     $citation,$baseurl,$attachmenturl);
  476:     }
  477:     return $status;
  478: }
  479: 
  480: 
  481: # =============================================================== Status Change
  482: 
  483: sub statuschange {
  484:     my ($msgid,$newstatus)=@_;
  485:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
  486:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  487:     unless ($status{$msgid}) { $status{$msgid}='new'; }
  488:     unless (($status{$msgid} eq 'replied') || 
  489:             ($status{$msgid} eq 'forwarded')) {
  490: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
  491:     }
  492:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
  493: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
  494:     }
  495: }
  496: 
  497: # ======================================================= Display a course list
  498: 
  499: sub discourse {
  500:     my $r=shift;
  501:     my %courselist=&Apache::lonnet::dump(
  502:                    'classlist',
  503: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  504: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  505:     my $now=time;
  506:     my %lt=&Apache::lonlocal::texthash('cfa' => 'Check for All',
  507:             'cfs' => 'Check for Section/Group',
  508:             'cfn' => 'Check for None');
  509:     $r->print(<<ENDDISHEADER);
  510: <input type=hidden name=sendmode value=group>
  511: <script>
  512:     function checkall() {
  513: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
  514:             if 
  515:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
  516: 	      document.forms.compemail.elements[i].checked=true;
  517:             }
  518:         }
  519:     }
  520: 
  521:     function checksec() {
  522: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
  523:             if 
  524:           (document.forms.compemail.elements[i].name.indexOf
  525:            ('send_to_&&&'+document.forms.compemail.chksec.value)==0) {
  526: 	      document.forms.compemail.elements[i].checked=true;
  527:             }
  528:         }
  529:     }
  530: 
  531:     function uncheckall() {
  532: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
  533:             if 
  534:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
  535: 	      document.forms.compemail.elements[i].checked=false;
  536:             }
  537:         }
  538:     }
  539: </script>
  540: <input type=button onClick="checkall()" value="$lt{'cfa'}">&nbsp;
  541: <input type=button onClick="checksec()" value="$lt{'cfs'}">
  542: <input type=text size=5 name=chksec>&nbsp;
  543: <input type=button onClick="uncheckall()" value="$lt{'cfn'}">
  544: <p>
  545: ENDDISHEADER
  546:     my %coursepersonnel=
  547:        &Apache::lonnet::get_course_adv_roles();
  548:     foreach my $role (sort keys %coursepersonnel) {
  549:        foreach (split(/\,/,$coursepersonnel{$role})) {
  550: 	   my ($puname,$pudom)=split(/\:/,$_);
  551: 	   $r->print(
  552:              '<br /><input type="checkbox" name="send_to_&&&&&&_'.
  553:              $puname.':'.$pudom.'" /> '.
  554: 		     &Apache::loncommon::plainname($puname,
  555:                           $pudom).' ('.$_.'), <i>'.$role.'</i>');
  556: 	}
  557:     }
  558: 
  559:     foreach (sort keys %courselist) {
  560:         my ($end,$start)=split(/\:/,$courselist{$_});
  561:         my $active=1;
  562:         if (($end) && ($now>$end)) { $active=0; }
  563:         if ($active) {
  564:            my ($sname,$sdom)=split(/\:/,$_);
  565:            my %reply=&Apache::lonnet::get('environment',
  566:               ['firstname','middlename','lastname','generation'],
  567:               $sdom,$sname);
  568:            my $section=&Apache::lonnet::usection
  569: 	       ($sdom,$sname,$ENV{'request.course.id'});
  570:            $r->print(
  571:         '<br><input type=checkbox name="send_to_&&&'.$section.'&&&_'.$_.'"> '.
  572: 		      $reply{'firstname'}.' '. 
  573:                       $reply{'middlename'}.' '.
  574:                       $reply{'lastname'}.' '.
  575:                       $reply{'generation'}.
  576:                       ' ('.$_.') '.$section);
  577:         } 
  578:     }
  579: }
  580: 
  581: # ==================================================== Display Critical Message
  582: 
  583: sub discrit {
  584:     my $r=shift;
  585:     my $header = '<h1><font color=red>'.&mt('Critical Messages').'</font></h1>'.
  586:         '<form action=/adm/email method=post>'.
  587:         '<input type=hidden name=confirm value=true>';
  588:     my %what=&Apache::lonnet::dump('critical');
  589:     my $result = '';
  590:     foreach (sort keys %what) {
  591:         my %content=&unpackagemsg($what{$_});
  592:         next if ($content{'senderdomain'} eq '');
  593:         $content{'message'}=~s/\n/\<br\>/g;
  594:         $result.='<hr>'.&mt('From').': <b>'.
  595: &Apache::loncommon::aboutmewrapper(
  596:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
  597: $content{'sendername'}.'@'.
  598:             $content{'senderdomain'}.') '.$content{'time'}.
  599:             '<br>'.&mt('Subject').': '.$content{'subject'}.
  600:             '<br><blockquote>'.
  601:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
  602:             '</blockquote><small>'.
  603: &mt('You have to confirm that you received this message. After confirmation, this message will be moved to your regular inbox').
  604:             '</small><br />'.
  605:             '<input type=submit name="rec_'.$_.'" value="'.&mt('Confirm Receipt').'">'.
  606:             '<input type=submit name="reprec_'.$_.'" '.
  607:                   'value="'.&mt('Confirm Receipt and Reply').'">';
  608:     }
  609:     # Check to see if there were any messages.
  610:     if ($result eq '') {
  611:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
  612: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a>';
  613:     } else {
  614:         $r->print($header);
  615:     }
  616:     $r->print($result);
  617:     $r->print('<input type=hidden name="displayedcrit" value="true"></form>');
  618: }
  619: 
  620: # =============================================================== Compose reply
  621: 
  622: sub comprep {
  623:     my ($r,$msgid)=@_;
  624:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  625:       my %content=&unpackagemsg($message{$msgid},1);
  626:       my $quotemsg='> '.$content{'message'};
  627:       $quotemsg=~s/\r/\n/g;
  628:       $quotemsg=~s/\f/\n/g;
  629:       $quotemsg=~s/\n+/\n\> /g;
  630:       my $torepl=&Apache::loncommon::aboutmewrapper(
  631:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
  632: $content{'sendername'}.'@'.
  633:             $content{'senderdomain'}.')';
  634:       my $subject=&mt('Re').': '.$content{'subject'};
  635:       my $dispcrit='';
  636:       if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  637: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
  638:          $dispcrit=
  639:  '<input type=checkbox name=critmsg> '.&mt('Send as critical message').' ' . $crithelp . 
  640:  '<br>'.
  641:  '<input type=checkbox name=sendbck> '.&mt('Send as critical message').' ' .
  642:  &mt('and return receipt') . $crithelp . '<p>';
  643:       }
  644:     my %lt=&Apache::lonlocal::texthash(
  645: 				   'to' => 'To',
  646: 				   'sb' => 'Subject',
  647: 				   'sr' => 'Send Reply',
  648: 				   'ca' => 'Cancel'
  649: 				   );
  650:       $r->print(<<"ENDREPLY");
  651: <form action="/adm/email" method="post">
  652: <input type="hidden" name="sendreply" value="$msgid">
  653: $lt{'to'}: $torepl<br />
  654: $lt{'sb'}: <input type="text" size=50 name="subject" value="$subject"><p>
  655: <textarea name="message" cols="84" rows="10" wrap="hard">
  656: $quotemsg
  657: </textarea></p><br />
  658: $dispcrit
  659: <input type="submit" name="send" value="$lt{'sr'}" />
  660: <input type="submit" name="cancel" value="$lt{'ca'}"/ >
  661: </form>
  662: ENDREPLY
  663: }
  664: 
  665: sub sortedmessages {
  666:     my @messages = &Apache::lonnet::getkeys('nohist_email');
  667:     #unpack the varibles and repack into temp for sorting
  668:     my @temp;
  669:     foreach (@messages) {
  670: 	my $msgid=&Apache::lonnet::escape($_);
  671: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
  672: 	    &Apache::lonmsg::unpackmsgid($msgid);
  673: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
  674: 		     $msgid);
  675: 	push @temp ,\@temp1;
  676:     }
  677:     #default sort
  678:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
  679:     if ($ENV{'form.sortedby'} eq "date"){
  680:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
  681:     }
  682:     if ($ENV{'form.sortedby'} eq "revdate"){
  683:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
  684:     }
  685:     if ($ENV{'form.sortedby'} eq "user"){
  686: 	@temp = sort  {lc($a->[2]) cmp lc($b->[2])} @temp;
  687:     }
  688:     if ($ENV{'form.sortedby'} eq "revuser"){
  689: 	@temp = sort  {lc($b->[2]) cmp lc($a->[2])} @temp;
  690:     }
  691:     if ($ENV{'form.sortedby'} eq "domain"){
  692:         @temp = sort  {$a->[3] cmp $b->[3]} @temp;
  693:     }
  694:     if ($ENV{'form.sortedby'} eq "revdomain"){
  695:         @temp = sort  {$b->[3] cmp $a->[3]} @temp;
  696:     }
  697:     if ($ENV{'form.sortedby'} eq "subject"){
  698:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
  699:     }
  700:     if ($ENV{'form.sortedby'} eq "revsubject"){
  701:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
  702:     }
  703:     if ($ENV{'form.sortedby'} eq "status"){
  704:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
  705:     }
  706:     if ($ENV{'form.sortedby'} eq "revstatus"){
  707:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
  708:     }
  709:     return @temp;
  710: }
  711: 
  712: # ======================================================== Display all messages
  713: 
  714: sub disall {
  715:     my $r=shift;
  716:      $r->print(<<ENDDISHEADER);
  717: <script>
  718:     function checkall() {
  719: 	for (i=0; i<document.forms.disall.elements.length; i++) {
  720:             if 
  721:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
  722: 	      document.forms.disall.elements[i].checked=true;
  723:             }
  724:         }
  725:     }
  726: 
  727:     function uncheckall() {
  728: 	for (i=0; i<document.forms.disall.elements.length; i++) {
  729:             if 
  730:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
  731: 	      document.forms.disall.elements[i].checked=false;
  732:             }
  733:         }
  734:     }
  735: </script>
  736: ENDDISHEADER
  737:     $r->print('<h1>'.&mt('Display All Messages').'</h1><form method=post name=disall '.
  738: 	      'action="/adm/email">'.
  739: 	      '<table border=2><tr><th colspan=2>&nbsp</th><th>');
  740:     if ($ENV{'form.sortedby'} eq "revdate") {
  741: 	$r->print('<a href = "?sortedby=date">'.&mt('Date').'</a></th>');
  742:     } else {
  743: 	$r->print('<a href = "?sortedby=revdate">'.&mt('Date').'</a></th>');
  744:     }
  745:     $r->print('<th>');
  746:     if ($ENV{'form.sortedby'} eq "revuser") {
  747: 	$r->print('<a href = "?sortedby=user">'.&mt('Username').'</a>');
  748:     } else {
  749: 	$r->print('<a href = "?sortedby=revuser">'.&mt('Username').'</a>');
  750:     }
  751:     $r->print('</th><th>');
  752:     if ($ENV{'form.sortedby'} eq "revdomain") {
  753: 	$r->print('<a href = "?sortedby=domain">'.&mt('Domain').'</a>');
  754:     } else {
  755: 	$r->print('<a href = "?sortedby=revdomain">'.&mt('Domain').'</a>');
  756:     }
  757:     $r->print('</th><th>');
  758:     if ($ENV{'form.sortedby'} eq "revsubject") {
  759: 	$r->print('<a href = "?sortedby=subject">'.&mt('Subject').'</a>');
  760:     } else {
  761:     	$r->print('<a href = "?sortedby=revsubject">'.&mt('Subject').'</a>');
  762:     }
  763:     $r->print('</th><th>');
  764:     if ($ENV{'form.sortedby'} eq "revstatus") {
  765: 	$r->print('<a href = "?sortedby=status">'.&mt('Status').'</th>');
  766:     } else {
  767:      	$r->print('<a href = "?sortedby=revstatus">'.&mt('Status').'</th>');
  768:     }
  769:     $r->print('</tr>');
  770:     my @temp=sortedmessages();
  771:     foreach (@temp){
  772: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID)= @$_;
  773: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
  774: 	    if ($status eq 'new') {
  775: 		$r->print('<tr bgcolor="#FFBB77">');
  776: 	    } elsif ($status eq 'read') {
  777: 		$r->print('<tr bgcolor="#BBBB77">');
  778: 	    } elsif ($status eq 'replied') {
  779: 		$r->print('<tr bgcolor="#AAAA88">'); 
  780: 	    } else {
  781: 		$r->print('<tr bgcolor="#99BBBB">');
  782: 	    }
  783: 	    $r->print('<td><a href="/adm/email?display='.$origID.$sqs. 
  784: 		      '">'.&mt('Open').'</a></td><td><a href="/adm/email?markdel='.$origID.$sqs.
  785: 		      '">'.&mt('Delete').'</a><input type=checkbox name="delmark_'.$origID.'"></td>'.
  786: 		      '<td>'.&Apache::lonlocal::locallocaltime($sendtime).'</td><td>'.
  787: 		      $fromname.'</td><td>'.$fromdomain.'</td><td>'.
  788: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
  789:                       $status.'</td></tr>');
  790: 	}
  791:     }   
  792:     $r->print('</table><p>'.
  793:               '<a href="javascript:checkall()">'.&mt('Check All').'</a>&nbsp;'.
  794:               '<a href="javascript:uncheckall()">'.&mt('Uncheck All').'</a><p>'.
  795: 	      '<input type="hidden" name="sortedby" value="'.$ENV{'form.sortedby'}.'" />'.
  796:               '<input type=submit name="markeddel" value="'.&mt('Delete Checked').'">'.
  797:               '</form></body></html>');
  798: }
  799: 
  800: # ============================================================== Compose output
  801: 
  802: sub compout {
  803:     my ($r,$forwarding,$broadcast)=@_;
  804:       my $dispcrit='';
  805:     my $dissub='';
  806:     my $dismsg='';
  807:     my $func=&mt('Send New');
  808:     my %lt=&Apache::lonlocal::texthash('us' => 'Username',
  809: 				       'do' => 'Domain',
  810: 				       'ad' => 'Additional Recipients',
  811: 				       'sb' => 'Subject',
  812: 				       'ca' => 'Cancel',
  813: 				       'ma' => 'Mail');
  814: 
  815:     if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  816: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
  817:          $dispcrit=
  818:  '<input type="checkbox" name="critmsg"> '.&mt('Send as critical message').' ' . $crithelp . 
  819:  '<br>'.
  820:  '<input type="checkbox" name="sendbck"> '.&mt('Send as critical message').'  ' .
  821:  &mt('and return receipt') . $crithelp . '<p>';
  822:       }
  823:     if ($forwarding) {
  824:        $dispcrit.='<input type="hidden" name="forwid" value="'.
  825: 	   $forwarding.'">';
  826:        $func=&mt('Forward');
  827:       my %message=&Apache::lonnet::get('nohist_email',[$forwarding]);
  828:       my %content=&unpackagemsg($message{$forwarding});
  829: 
  830:        $dissub=&mt('Forwarding').': '.$content{'subject'};
  831:        $dismsg=&mt('Forwarded message from').' '.
  832: 	   $content{'sendername'}.' '.&mt('at').' '.$content{'senderdomain'};
  833:     }
  834:     my $defdom=$ENV{'user.domain'};
  835:     if ($ENV{'form.recdom'}) { $defdom=$ENV{'form.recdom'}; }
  836:       $r->print(
  837:                 '<form action="/adm/email"  name="compemail" method="post"'.
  838:                 ' enctype="multipart/form-data">'."\n".
  839:                 '<input type="hidden" name="sendmail" value="on">'."\n".
  840:                 '<table>');
  841:     unless (($broadcast eq 'group') || ($broadcast eq 'upload')) {
  842:         my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
  843:         my $selectlink=&Apache::loncommon::selectstudent_link
  844: 	    ('compemail','recuname','recdomain');
  845:        $r->print(<<"ENDREC");
  846: <table>
  847: <tr><td>$lt{'us'}:</td><td><input type="text" size="12" name="recuname" value="$ENV{'form.recname'}"></td><td rowspan="2">$selectlink</td></tr>
  848: <tr><td>$lt{'do'}:</td>
  849: <td>$domform</td></tr>
  850: ENDREC
  851:     }
  852:     my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
  853:     if ($broadcast ne 'upload') {
  854:        $r->print(<<"ENDCOMP");
  855: <tr><td>$lt{'ad'}<br /><tt>username\@domain,username\@domain, ...
  856: </tt></td><td>
  857: <input type="text" size="50" name="additionalrec"></td></tr>
  858: <tr><td>$lt{'sb'}:</td><td><input type="text" size="50" name="subject" value="$dissub">
  859: </td></tr></table>
  860: $latexHelp
  861: <textarea name="message" cols="80" rows="10" wrap="hard">$dismsg
  862: </textarea></p><br />
  863: $dispcrit
  864: <input type="submit" name="send" value="$func $lt{'ma'}" />
  865: <input type="submit" name="cancel" value="$lt{'ca'}" />
  866: ENDCOMP
  867:     } else { # $broadcast is 'upload'
  868: 	$r->print(<<ENDUPLOAD);
  869: <input type=hidden name=sendmode value=upload>
  870: <h3>Generate messages from a file</h3>
  871: <p>
  872: Subject: <input type=text size=50 name=subject>
  873: </p>
  874: <p>General message text<br />
  875: <textarea name=message cols=60 rows=10 wrap=hard>$dismsg
  876: </textarea></p>
  877: <p>
  878: The file format for the uploaded portion of the message is:
  879: <pre>
  880: username1\@domain1: text
  881: username2\@domain2: text
  882: username3\@domain1: text
  883: </pre>
  884: </p>
  885: <p>
  886: The messages will be assembled from all lines with the respective 
  887: <tt>username\@domain</tt>, and appended to the general message text.</p>
  888: <p>
  889: <input type=file name=upfile size=20><p>
  890: $dispcrit
  891: <input type=submit value="Upload and send">
  892: ENDUPLOAD
  893:     }
  894:     if ($broadcast eq 'group') {
  895:        &discourse;
  896:     }
  897:     $r->print('</form>');
  898: }
  899: 
  900: # ---------------------------------------------------- Display all face to face
  901: 
  902: sub disfacetoface {
  903:     my ($r,$user,$domain)=@_;
  904:     unless ($ENV{'request.course.id'}) { return; }
  905:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  906: 	return;
  907:     }
  908:     my %records=&Apache::lonnet::dump('nohist_email',
  909: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  910: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  911:                          '%255b'.$user.'%253a'.$domain.'%255d');
  912:     my $result='';
  913:     foreach (sort keys %records) {
  914:         my %content=&unpackagemsg($records{$_});
  915:         next if ($content{'senderdomain'} eq '');
  916:         $content{'message'}=~s/\n/\<br\>/g;
  917:         if ($content{'subject'}=~/^Record/) {
  918: 	    $result.='<h3>'.&mt('Record').'</h3>';
  919:         } else {
  920:             $result.='<h3>'.&mt('Sent Message').'</h3>';
  921:             %content=&unpackagemsg($content{'message'});
  922:             $content{'message'}=
  923:                 '<b>Subject: '.$content{'subject'}.'</b><br />'.
  924: 		$content{'message'};
  925:         }
  926:         $result.=&mt('By').': <b>'.
  927: &Apache::loncommon::aboutmewrapper(
  928:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
  929: $content{'sendername'}.'@'.
  930:             $content{'senderdomain'}.') '.$content{'time'}.
  931:             '<br><blockquote>'.
  932:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
  933: 	      '</blockquote>';
  934:      }
  935:     # Check to see if there were any messages.
  936:     if ($result eq '') {
  937:         $r->print("<p><b>No notes, face-to-face discussion records, or critical messages in this course.</b></p>");
  938:     } else {
  939:        $r->print($result);
  940:     }
  941: }
  942: 
  943: # ---------------------------------------------------------------- Face to face
  944: 
  945: sub facetoface {
  946:     my ($r,$stage)=@_;
  947:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  948: 	return;
  949:     }
  950: # from query string
  951:     if ($ENV{'form.recname'}) { $ENV{'form.recuname'}=$ENV{'form.recname'}; }
  952:     if ($ENV{'form.recdom'}) { $ENV{'form.recdomain'}=$ENV{'form.recdom'}; }
  953: 
  954:     my $defdom=$ENV{'user.domain'};
  955: # already filled in
  956:     if ($ENV{'form.recdomain'}) { $defdom=$ENV{'form.recdomain'}; }
  957: # generate output
  958:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
  959:     my $stdbrws = &Apache::loncommon::selectstudent_link
  960: 	('stdselect','recuname','recdomain');
  961:     $r->print(<<"ENDTREC");
  962: <h3>User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course</h3>
  963: <form method="post" action="/adm/email" name="stdselect">
  964: <input type="hidden" name="recordftf" value="retrieve" />
  965: <table>
  966: <tr><td>Username:</td><td><input type=text size=12 name=recuname value="$ENV{'form.recuname'}"></td>
  967: <td rowspan="2">
  968: $stdbrws
  969: <input type="submit" value="Retrieve discussion and message records"></td>
  970: </tr>
  971: <tr><td>Domain:</td>
  972: <td>$domform</td></tr>
  973: </table>
  974: </form>
  975: ENDTREC
  976:     if (($stage ne 'query') &&
  977:         ($ENV{'form.recdomain'}) && ($ENV{'form.recuname'})) {
  978:         chomp($ENV{'form.newrecord'});
  979:         if ($ENV{'form.newrecord'}) {
  980:            &user_normal_msg_raw(
  981:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  982:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  983:             'Record ['.$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}.']',
  984: 	    $ENV{'form.newrecord'});
  985:         }
  986:         $r->print('<h3>'.&Apache::loncommon::plainname($ENV{'form.recuname'},
  987: 				     $ENV{'form.recdomain'}).'</h3>');
  988:         &disfacetoface($r,$ENV{'form.recuname'},$ENV{'form.recdomain'});
  989: 	$r->print(<<ENDRHEAD);
  990: <form method="post" action="/adm/email">
  991: <input name="recdomain" value="$ENV{'form.recdomain'}" type="hidden" />
  992: <input name="recuname" value="$ENV{'form.recuname'}" type="hidden" />
  993: ENDRHEAD
  994:         $r->print(<<ENDBFORM);
  995: <hr />New Record (record is visible to course faculty and staff)<br />
  996: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
  997: <br />
  998: <input type="hidden" name="recordftf" value="post" />
  999: <input type="submit" value="Post this record" />
 1000: </form>
 1001: ENDBFORM
 1002:     }
 1003: }
 1004: 
 1005: # ===================================================================== Handler
 1006: 
 1007: sub handler {
 1008:     my $r=shift;
 1009: 
 1010: # ----------------------------------------------------------- Set document type
 1011: 
 1012:   &Apache::loncommon::content_type($r,'text/html');
 1013:   $r->send_http_header;
 1014: 
 1015:   return OK if $r->header_only;
 1016: 
 1017: # --------------------------- Get query string for limited number of parameters
 1018:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1019:         ['display','replyto','forward','markread','markdel','markunread',
 1020:          'sendreply','compose','sendmail','critical','recname','recdom',
 1021:          'recordftf','sortedby']);
 1022:     $sqs='&sortedby='.$ENV{'form.sortedby'};
 1023: # ------------------------------------------------------ They checked for email
 1024:   &Apache::lonnet::put('email_status',{'recnewemail'=>0});
 1025: # --------------------------------------------------------------- Render Output
 1026:   if (!$ENV{'form.display'}) {
 1027:       $r->print('<html><head><title>EMail and Messaging</title>'.
 1028: 		&Apache::loncommon::studentbrowser_javascript().'</head>'.
 1029: 		&Apache::loncommon::bodytag('EMail and Messages').
 1030: 	     &Apache::loncommon::help_open_faq(12).
 1031: 	     &Apache::loncommon::help_open_bug('Communication Tools'));
 1032:   }
 1033:   if ($ENV{'form.display'}) {
 1034:       my $msgid=$ENV{'form.display'};
 1035:       &statuschange($msgid,'read');
 1036:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
 1037:       my %content=&unpackagemsg($message{$msgid});
 1038: # info to generate "next" and "previous" buttons
 1039:       my @messages=&sortedmessages();
 1040:       my $counter=0;
 1041:       $r->print('<pre>');
 1042:       my $escmsgid=&Apache::lonnet::escape($msgid);
 1043:       foreach (@messages) {
 1044:  	  if ($_->[5] eq $escmsgid){
 1045:  	      last;
 1046:  	  }
 1047:  	  $counter++;
 1048:       }
 1049:       $r->print('</pre>');
 1050:       my $number_of_messages = scalar(@messages); #subtract 1 for last index
 1051: # start output
 1052:       $r->print('<html><head><title>EMail and Messaging</title>');
 1053:       if (defined($content{'baseurl'})) {
 1054: 	  $r->print("<base href=\"http://$ENV{'SERVER_NAME'}/$content{'baseurl'}\" />");
 1055:       }
 1056:       $r->print(&Apache::loncommon::studentbrowser_javascript().
 1057: 		'</head>'.
 1058: 		&Apache::loncommon::bodytag('EMail and Messages').
 1059: 	     &Apache::loncommon::help_open_faq(12).
 1060: 	     &Apache::loncommon::help_open_bug('Communication Tools'));
 1061:       $r->print('<b>'.&mt('Subject').':</b> '.$content{'subject'}.
 1062:              '<br><b>'.&mt('From').':</b> '.
 1063: &Apache::loncommon::aboutmewrapper(
 1064: &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
 1065: $content{'sendername'},$content{'senderdomain'}).' ('.
 1066:                                  $content{'sendername'}.' at '.
 1067:                                  $content{'senderdomain'}.') '.
 1068:              '<br><b>'.&mt('Time').':</b> '.$content{'time'}.'<p>'.
 1069:              '<table border=2><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
 1070:            '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).$sqs.
 1071:              '"><b>'.&mt('Reply').'</b></a></td>'.
 1072:            '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).$sqs.
 1073:              '"><b>'.&mt('Forward').'</b></a></td>'.
 1074:         '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).$sqs.
 1075:              '"><b>'.&mt('Mark Unread').'</b></a></td>'.
 1076:         '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).$sqs.
 1077:              '"><b>Delete</b></a></td>'.
 1078: 		'<td><a href="/adm/email?sortedby='.$ENV{'form.sortedby'}.
 1079: 		'"><b>'.&mt('Display all Messages').'</b></a></td>');
 1080:       if ($counter > 0){
 1081:  	  $r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
 1082:            '"><b>'.&mt('Previous').'</b></a></td>');
 1083:        }
 1084:        if ($counter < $number_of_messages - 1){
 1085:  	  $r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
 1086:            '"><b>'.&mt('Next').'</b></a></td>');
 1087:        }
 1088:        $r->print('</tr></table><p><pre>'.
 1089:              &Apache::lontexconvert::msgtexconverted($content{'message'},1).
 1090:              '</pre><hr>'.$content{'citation'});
 1091:   } elsif ($ENV{'form.replyto'}) {
 1092:       &comprep($r,$ENV{'form.replyto'});
 1093:   } elsif ($ENV{'form.sendreply'}) {
 1094:       if ($ENV{'form.send'}) {
 1095: 	  my $msgid=$ENV{'form.sendreply'};
 1096: 	  my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
 1097: 	  my %content=&unpackagemsg($message{$msgid},1);
 1098: 	  &statuschange($msgid,'replied');
 1099: 	  if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
 1100: 	      (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
 1101: 	      $r->print(&mt('Sending critical message').': '.
 1102: 			&user_crit_msg($content{'sendername'},
 1103: 				       $content{'senderdomain'},
 1104: 				       &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1105: 				       &Apache::lonfeedback::clear_out_html($ENV{'form.message'}),
 1106: 				       $ENV{'form.sendbck'}));
 1107: 	  } else {
 1108: 	      $r->print(&mt('Sending').': '.&user_normal_msg($content{'sendername'},
 1109: 							     $content{'senderdomain'},
 1110: 							     &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1111: 							     &Apache::lonfeedback::clear_out_html($ENV{'form.message'})));
 1112: 	  }
 1113:       }
 1114:       if ($ENV{'form.displayedcrit'}) {
 1115:           &discrit($r);
 1116:       } else {
 1117: 	  &disall($r);
 1118:       }
 1119:   } elsif ($ENV{'form.confirm'}) {
 1120:       foreach (keys %ENV) {
 1121:           if ($_=~/^form\.rec\_(.*)$/) {
 1122: 	      $r->print('<b>Confirming Receipt:</b> '.
 1123:                         &user_crit_received($1).'<br>');
 1124:           }
 1125:           if ($_=~/^form\.reprec\_(.*)$/) {
 1126:               my $msgid=$1;
 1127: 	      $r->print('<b>Confirming Receipt:</b> '.
 1128:                         &user_crit_received($msgid).'<br>');
 1129:               &comprep($r,$msgid);
 1130:           }
 1131:       }
 1132:       &discrit($r);
 1133:   } elsif ($ENV{'form.critical'}) {
 1134:       &discrit($r);
 1135:   } elsif ($ENV{'form.forward'}) {
 1136:       &compout($r,$ENV{'form.forward'});
 1137:   } elsif ($ENV{'form.markread'}) {
 1138:   } elsif ($ENV{'form.markdel'}) {
 1139:       &statuschange($ENV{'form.markdel'},'deleted');
 1140:       &disall($r);
 1141:   } elsif ($ENV{'form.markeddel'}) {
 1142:       my $total=0;
 1143:       foreach (keys %ENV) {
 1144:           if ($_=~/^form\.delmark_(.*)$/) {
 1145: 	      &statuschange(&Apache::lonnet::unescape($1),'deleted');
 1146:               $total++;
 1147:           }
 1148:       }
 1149:       $r->print('Deleted '.$total.' message(s)<p>');
 1150:       &disall($r);
 1151:   } elsif ($ENV{'form.markunread'}) {
 1152:       &statuschange($ENV{'form.markunread'},'new');
 1153:       &disall($r);
 1154:   } elsif ($ENV{'form.compose'}) {
 1155:       &compout($r,'',$ENV{'form.compose'});
 1156:   } elsif ($ENV{'form.recordftf'}) {
 1157:       &facetoface($r,$ENV{'form.recordftf'});
 1158:   } elsif ($ENV{'form.sendmail'}) {
 1159:       my $sendstatus='';
 1160:       if ($ENV{'form.send'}) {
 1161: 	  my %content=();
 1162: 	  undef %content;
 1163: 	  if ($ENV{'form.forwid'}) {
 1164: 	      my $msgid=$ENV{'form.forwid'};
 1165: 	      my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
 1166: 	      %content=&unpackagemsg($message{$msgid},1);
 1167: 	      &statuschange($msgid,'forwarded');
 1168: 	      $ENV{'form.message'}.="\n\n-- Forwarded message --\n\n".
 1169: 		  $content{'message'};
 1170: 	  }
 1171: 	  my %toaddr=();
 1172: 	  undef %toaddr;
 1173: 	  if ($ENV{'form.sendmode'} eq 'group') {
 1174: 	      foreach (keys %ENV) {
 1175: 		  if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
 1176: 		      $toaddr{$1}='';
 1177: 		  }
 1178: 	      }
 1179: 	  } elsif ($ENV{'form.sendmode'} eq 'upload') {
 1180: 	      foreach (split(/[\n\r\f]+/,$ENV{'form.upfile'})) {
 1181: 		  my ($rec,$txt)=split(/\s*\:\s*/,$_);
 1182: 		  if ($txt) {
 1183: 		      $rec=~s/\@/\:/;
 1184: 		      $toaddr{$rec}.=$txt."\n";
 1185: 		  }
 1186: 	      }
 1187: 	  } else {
 1188: 	      $toaddr{$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}}='';
 1189: 	  }
 1190: 	  if ($ENV{'form.additionalrec'}) {
 1191: 	      foreach (split(/\,/,$ENV{'form.additionalrec'})) {
 1192: 		  my ($auname,$audom)=split(/\@/,$_);
 1193: 		  $toaddr{$auname.':'.$audom}='';
 1194: 	      }
 1195: 	  }
 1196: 	  foreach (keys %toaddr) {
 1197: 	      my ($recuname,$recdomain)=split(/\:/,$_);
 1198: 	      my $msgtxt=&Apache::lonfeedback::clear_out_html($ENV{'form.message'});
 1199: 	      if ($toaddr{$_}) { $msgtxt.='<hr>'.$toaddr{$_}; }    
 1200: 	      if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
 1201: 		  (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
 1202: 		  $r->print(&mt('Sending critical message').' ...');
 1203:                   $sendstatus.=' '.&user_crit_msg($recuname,$recdomain,
 1204: 					   &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1205: 					   $msgtxt,
 1206: 					   $ENV{'form.sendbck'});
 1207: 	      } else {
 1208: 		  $r->print(&mt('Sending').' ...');
 1209:                   $sendstatus.=' '.&user_normal_msg($recuname,$recdomain,
 1210: 				                         &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1211: 							 $msgtxt,
 1212: 							 $content{'citation'});
 1213: 	      }
 1214: 	      $r->print('<br />');
 1215: 	  }
 1216:       }
 1217:       if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
 1218: 	  $r->print('<br /><font color="green">'.&mt('Completed.').'</font>');
 1219: 	  if ($ENV{'form.displayedcrit'}) {
 1220: 	      &discrit($r);
 1221: 	  } else {
 1222: 	      &disall($r);
 1223: 	  }
 1224:       } else {
 1225: 	  $r->print(
 1226:   '<h2><font color="red">'.&mt('Could not deliver message').'</font></h2>'.
 1227:   &mt('Please use the browser "Back" button and correct the recipient addresses')
 1228: 		    );
 1229:       }
 1230:   } else {
 1231:       &disall($r);
 1232:   }
 1233:   $r->print('</body></html>');
 1234:   return OK;
 1235: 
 1236: }
 1237: # ================================================= Main program, reset counter
 1238: 
 1239: BEGIN {
 1240:     $msgcount=0;
 1241: }
 1242: 
 1243: =pod
 1244: 
 1245: =back
 1246: 
 1247: =cut
 1248: 
 1249: 1; 
 1250: 
 1251: __END__
 1252: 
 1253: 
 1254: 
 1255: 
 1256: 
 1257: 
 1258: 

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