File:  [LON-CAPA] / loncom / interface / lonmsg.pm
Revision 1.74: download - view: text, annotated - select for diffs
Tue Dec 30 20:39:30 2003 UTC (20 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Work on Bug #2444: retrieval of bombs possible again.

    1: # The LearningOnline Network with CAPA
    2: # Routines for messaging
    3: #
    4: # $Id: lonmsg.pm,v 1.74 2003/12/30 20:39:30 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: # (Routines to control the menu
   30: #
   31: # (TeX Conversion Module
   32: #
   33: # 05/29/00,05/30 Gerd Kortemeyer)
   34: #
   35: # 10/05 Gerd Kortemeyer)
   36: #
   37: # 10/19,10/20,10/30,
   38: # 02/06/01 Gerd Kortemeyer
   39: # 07/27 Guy Albertelli
   40: # 07/27,07/28,07/30,08/03,08/06,08/08,08/09,08/10,8/13,8/15,
   41: # 10/1,11/5 Gerd Kortemeyer
   42: # YEAR=2002
   43: # 1/1,3/18 Gerd Kortemeyer
   44: #
   45: package Apache::lonmsg;
   46: 
   47: =pod
   48: 
   49: =head1 NAME
   50: 
   51: Apache::lonmsg: supports internal messaging
   52: 
   53: =head1 SYNOPSIS
   54: 
   55: lonmsg provides routines for sending messages, receiving messages, and
   56: a handler to allow users to read, send, and delete messages.
   57: 
   58: =head1 OVERVIEW
   59: 
   60: =head2 Messaging Overview
   61: 
   62: X<messages>LON-CAPA provides an internal messaging system similar to
   63: email, but customized for LON-CAPA's usage. LON-CAPA implements its
   64: own messaging system, rather then building on top of email, because of
   65: the features LON-CAPA messages can offer that conventional e-mail can
   66: not:
   67: 
   68: =over 4
   69: 
   70: =item * B<Critical messages>: A message the recipient B<must>
   71: acknowlegde receipt of before they are allowed to continue using the
   72: system, preventing a user from claiming they never got a message
   73: 
   74: =item * B<Receipts>: LON-CAPA can reliably send reciepts informing the
   75: sender that it has been read; again, useful for preventing students
   76: from claiming they did not see a message. (While conventional e-mail
   77: has some reciept support, it's sporadic, e-mail client-specific, and
   78: generally the receiver can opt to not send one, making it useless in
   79: this case.)
   80: 
   81: =item * B<Context>: LON-CAPA knows about the sender, such as where
   82: they are in a course. When a student mails an instructor asking for
   83: help on the problem, the instructor receives not just the student's
   84: question, but all submissions the student has made up to that point,
   85: the user's rendering of the problem, and the complete view the student
   86: saw of the resource, including discussion up to that point. Finally,
   87: the instructor is reading all of this inside of LON-CAPA, not their
   88: email program, so they have full access to LON-CAPA's grading
   89: interface, or other features they may wish to use in response to the
   90: student's query.
   91: 
   92: =back
   93: 
   94: Users can ask LON-CAPA to forward messages to conventional e-mail
   95: addresses on their B<PREF> screen, but generally, LON-CAPA messages
   96: are much more useful then traditional email can be made to be, even
   97: with HTML support.
   98: 
   99: Right now, this document will cover just how to send a message, since
  100: it is likely you will not need to programmatically read messages,
  101: since lonmsg already implements that functionality.
  102: 
  103: =head1 FUNCTIONS
  104: 
  105: =over 4
  106: 
  107: =cut
  108: 
  109: use strict;
  110: use Apache::lonnet();
  111: use vars qw($msgcount);
  112: use HTML::TokeParser();
  113: use Apache::Constants qw(:common);
  114: use Apache::loncommon();
  115: use Apache::lontexconvert();
  116: use HTML::Entities();
  117: use Mail::Send;
  118: use Apache::lonlocal;
  119: 
  120: # Querystring component with sorting type
  121: my $sqs;
  122: 
  123: # ===================================================================== Package
  124: 
  125: sub packagemsg {
  126:     my ($subject,$message,$citation,$baseurl,$attachmenturl)=@_;
  127:     $message =&HTML::Entities::encode($message);
  128:     $citation=&HTML::Entities::encode($citation);
  129:     $subject =&HTML::Entities::encode($subject);
  130:     #remove machine specification
  131:     $baseurl =~ s|^http://[^/]+/|/|;
  132:     $baseurl =&HTML::Entities::encode($baseurl);
  133:     #remove machine specification
  134:     $attachmenturl =~ s|^http://[^/]+/|/|;
  135:     $attachmenturl =&HTML::Entities::encode($attachmenturl);
  136: 
  137:     my $now=time;
  138:     $msgcount++;
  139:     my $partsubj=$subject;
  140:     $partsubj=&Apache::lonnet::escape($partsubj);
  141:     my $msgid=&Apache::lonnet::escape(
  142:            $now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
  143:            $ENV{'user.domain'}.':'.$msgcount.':'.$$);
  144:     my $result='<sendername>'.$ENV{'user.name'}.'</sendername>'.
  145:            '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
  146:            '<subject>'.$subject.'</subject>'.
  147: 	   '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>'.
  148: 	   '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
  149:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
  150: 	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
  151: 	   '<browsertype>'.$ENV{'browser.type'}.'</browsertype>'.
  152: 	   '<browseros>'.$ENV{'browser.os'}.'</browseros>'.
  153: 	   '<browserversion>'.$ENV{'browser.version'}.'</browserversion>'.
  154:            '<browsermathml>'.$ENV{'browser.mathml'}.'</browsermathml>'.
  155: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
  156: 	   '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
  157: 	   '<role>'.$ENV{'request.role'}.'</role>'.
  158: 	   '<resource>'.$ENV{'request.filename'}.'</resource>'.
  159:            '<msgid>'.$msgid.'</msgid>'.
  160: 	   '<message>'.$message.'</message>';
  161:     if (defined($citation)) {
  162: 	$result.='<citation>'.$citation.'</citation>';
  163:     }
  164:     if (defined($baseurl)) {
  165: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
  166:     }
  167:     if (defined($attachmenturl)) {
  168: 	$result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
  169:     }
  170:     return $msgid,$result;
  171: }
  172: 
  173: # ================================================== Unpack message into a hash
  174: 
  175: sub unpackagemsg {
  176:     my ($message,$notoken)=@_;
  177:     my %content=();
  178:     my $parser=HTML::TokeParser->new(\$message);
  179:     my $token;
  180:     while ($token=$parser->get_token) {
  181:        if ($token->[0] eq 'S') {
  182: 	   my $entry=$token->[1];
  183:            my $value=$parser->get_text('/'.$entry);
  184:            $content{$entry}=$value;
  185:        }
  186:     }
  187:     if ($content{'attachmenturl'}) {
  188:        my ($fname,$ft)=($content{'attachmenturl'}=~/\/(\w+)\.(\w+)$/);
  189:        if ($notoken) {
  190: 	   $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'.'.$ft.'</tt>';
  191:        } else {
  192: 	   $content{'message'}.='<p>'.&mt('Attachment').': <a href="'.
  193: 	       &Apache::lonnet::tokenwrapper($content{'attachmenturl'}).
  194: 	       '"><tt>'.$fname.'.'.$ft.'</tt></a>';
  195:        }
  196:     }
  197:     return %content;
  198: }
  199: 
  200: # ======================================================= Get info out of msgid
  201: 
  202: sub unpackmsgid {
  203:     my $msgid=&Apache::lonnet::unescape(shift);
  204:     my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
  205:                           &Apache::lonnet::unescape($msgid));
  206:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
  207:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  208:     unless ($status{$msgid}) { $status{$msgid}='new'; }
  209:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
  210: } 
  211: 
  212: 
  213: sub sendemail {
  214:     my ($to,$subject,$body)=@_;
  215:     $body=
  216:     "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
  217:     "*** ".&mt('Please do not reply to this address.')."\n\n".$body;
  218:     my $msg = new Mail::Send;
  219:     $msg->to($to);
  220:     $msg->subject('[LON-CAPA] '.$subject);
  221:     if (my $fh = $msg->open('smtp',Server => 'localhost')) {
  222: 	print $fh $body;
  223: 	$fh->close;
  224:     }
  225: }
  226: 
  227: # ==================================================== Send notification emails
  228: 
  229: sub sendnotification {
  230:     my ($to,$touname,$toudom,$subj,$crit)=@_;
  231:     my $sender=$ENV{'environment.firstname'}.' '.$ENV{'environment.lastname'};
  232:     my $critical=($crit?' critical':'');
  233:     my $url='http://'.
  234:       $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
  235:       '/adm/email?username='.$touname.'&domain='.$toudom;
  236:     my $body=(<<ENDMSG);
  237: You received a$critical message from $sender in LON-CAPA. The subject is
  238: 
  239:  $subj
  240: 
  241: Use
  242: 
  243:  $url
  244: 
  245: to access this message.
  246: ENDMSG
  247:     &sendemail($to,'New'.$critical.' message from '.$sender,$body);
  248: }
  249: # ============================================================= Check for email
  250: 
  251: sub newmail {
  252:     if ((time-$ENV{'user.mailcheck.time'})>300) {
  253:         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
  254:         &Apache::lonnet::appenv('user.mailcheck.time'=>time);
  255:         if ($what{'recnewemail'}>0) { return 1; }
  256:     }
  257:     return 0;
  258: }
  259: 
  260: # =============================== Automated message to the author of a resource
  261: 
  262: =pod
  263: 
  264: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
  265:     of the resource with the URI $filename.
  266: 
  267: =cut
  268: 
  269: sub author_res_msg {
  270:     my ($filename,$message)=@_;
  271:     unless ($message) { return 'empty'; }
  272:     $filename=&Apache::lonnet::declutter($filename);
  273:     my ($domain,$author,@dummy)=split(/\//,$filename);
  274:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
  275:     if ($homeserver ne 'no_host') {
  276:        my $id=unpack("%32C*",$message);
  277:        my $msgid;
  278:        ($msgid,$message)=&packagemsg($filename,$message);
  279:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
  280:          ':nohist_res_msgs:'.
  281:           &Apache::lonnet::escape($filename.'_'.$id).'='.
  282:           &Apache::lonnet::escape($message),$homeserver);
  283:     }
  284:     return 'no_host';
  285: }
  286: 
  287: # =========================================== Retrieve author resource messages
  288: 
  289: sub retrieve_author_res_msg {
  290:     my ($author,$domain,$url)=@_;
  291:     $url=&Apache::lonnet::declutter($url);
  292:     my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$1,$2);
  293:     my $msgs='';
  294:     foreach (keys %errormsgs) {
  295: 	if ($_=~/^\Q$url\E\_\d+$/) {
  296: 	    my %content=&unpackagemsg($errormsgs{$_});
  297: 	    $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
  298: 		$content{'time'}.'</b>: '.$content{'message'}.
  299: 		'<br /></p>';
  300: 	}
  301:     } 
  302:     return $msgs;     
  303: }
  304: 
  305: 
  306: # =============================== Delete all author messages related to one URL
  307: 
  308: sub del_url_author_res_msg {
  309:     my ($author,$domain,$url)=@_;
  310:     $url=&Apache::lonnet::declutter($url);
  311: }
  312: 
  313: # ================= Return hash with URLs for which there is a resource message
  314: 
  315: sub all_url_author_res_msg {
  316:     my ($author,$domain)=@_;
  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'}.' at '.$ENV{'user.domain'},
  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>'.
  603:             '<input type=submit name="rec_'.$_.'" value="'.&mt('Confirm Receipt').'">'.
  604:             '<input type=submit name="reprec_'.$_.'" '.
  605:                   'value="'.&mt('Confirm Receipt and Reply').'">';
  606:     }
  607:     # Check to see if there were any messages.
  608:     if ($result eq '') {
  609:         $result = "<h2>".&mt('You have no critical messages.')."</h2>".
  610: 	    '<a href="/adm/roles">'.&mt('Select a course').'</a>';
  611:     } else {
  612:         $r->print($header);
  613:     }
  614:     $r->print($result);
  615:     $r->print('<input type=hidden name="displayedcrit" value="true"></form>');
  616: }
  617: 
  618: # =============================================================== Compose reply
  619: 
  620: sub comprep {
  621:     my ($r,$msgid)=@_;
  622:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  623:       my %content=&unpackagemsg($message{$msgid},1);
  624:       my $quotemsg='> '.$content{'message'};
  625:       $quotemsg=~s/\r/\n/g;
  626:       $quotemsg=~s/\f/\n/g;
  627:       $quotemsg=~s/\n+/\n\> /g;
  628:       my $torepl=&Apache::loncommon::aboutmewrapper(
  629:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
  630: $content{'sendername'}.'@'.
  631:             $content{'senderdomain'}.')';
  632:       my $subject=&mt('Re').': '.$content{'subject'};
  633:       my $dispcrit='';
  634:       if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  635: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
  636:          $dispcrit=
  637:  '<input type=checkbox name=critmsg> '.&mt('Send as critical message').' ' . $crithelp . 
  638:  '<br>'.
  639:  '<input type=checkbox name=sendbck> '.&mt('Send as critical message').' ' .
  640:  &mt('and return receipt') . $crithelp . '<p>';
  641:       }
  642:     my %lt=&Apache::lonlocal::texthash(
  643: 				   'to' => 'To',
  644: 				   'sb' => 'Subject',
  645: 				   'sr' => 'Send Reply',
  646: 				   'ca' => 'Cancel'
  647: 				   );
  648:       $r->print(<<"ENDREPLY");
  649: <form action="/adm/email" method="post">
  650: <input type="hidden" name="sendreply" value="$msgid">
  651: $lt{'to'}: $torepl<br />
  652: $lt{'sb'}: <input type="text" size=50 name="subject" value="$subject"><p>
  653: <textarea name="message" cols="84" rows="10" wrap="hard">
  654: $quotemsg
  655: </textarea></p><br />
  656: $dispcrit
  657: <input type="submit" name="send" value="$lt{'sr'}" />
  658: <input type="submit" name="cancel" value="$lt{'ca'}"/ >
  659: </form>
  660: ENDREPLY
  661: }
  662: 
  663: sub sortedmessages {
  664:     my @messages = &Apache::lonnet::getkeys('nohist_email');
  665:     #unpack the varibles and repack into temp for sorting
  666:     my @temp;
  667:     foreach (@messages) {
  668: 	my $msgid=&Apache::lonnet::escape($_);
  669: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
  670: 	    &Apache::lonmsg::unpackmsgid($msgid);
  671: 	my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
  672: 		     $msgid);
  673: 	push @temp ,\@temp1;
  674:     }
  675:     #default sort
  676:     @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
  677:     if ($ENV{'form.sortedby'} eq "date"){
  678:         @temp = sort  {$a->[0] <=> $b->[0]} @temp;    
  679:     }
  680:     if ($ENV{'form.sortedby'} eq "revdate"){
  681:     	@temp = sort  {$b->[0] <=> $a->[0]} @temp; 
  682:     }
  683:     if ($ENV{'form.sortedby'} eq "user"){
  684: 	@temp = sort  {lc($a->[2]) cmp lc($b->[2])} @temp;
  685:     }
  686:     if ($ENV{'form.sortedby'} eq "revuser"){
  687: 	@temp = sort  {lc($b->[2]) cmp lc($a->[2])} @temp;
  688:     }
  689:     if ($ENV{'form.sortedby'} eq "domain"){
  690:         @temp = sort  {$a->[3] cmp $b->[3]} @temp;
  691:     }
  692:     if ($ENV{'form.sortedby'} eq "revdomain"){
  693:         @temp = sort  {$b->[3] cmp $a->[3]} @temp;
  694:     }
  695:     if ($ENV{'form.sortedby'} eq "subject"){
  696:         @temp = sort  {lc($a->[1]) cmp lc($b->[1])} @temp;
  697:     }
  698:     if ($ENV{'form.sortedby'} eq "revsubject"){
  699:         @temp = sort  {lc($b->[1]) cmp lc($a->[1])} @temp;
  700:     }
  701:     if ($ENV{'form.sortedby'} eq "status"){
  702:         @temp = sort  {$a->[4] cmp $b->[4]} @temp;
  703:     }
  704:     if ($ENV{'form.sortedby'} eq "revstatus"){
  705:         @temp = sort  {$b->[4] cmp $a->[4]} @temp;
  706:     }
  707:     return @temp;
  708: }
  709: 
  710: # ======================================================== Display all messages
  711: 
  712: sub disall {
  713:     my $r=shift;
  714:      $r->print(<<ENDDISHEADER);
  715: <script>
  716:     function checkall() {
  717: 	for (i=0; i<document.forms.disall.elements.length; i++) {
  718:             if 
  719:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
  720: 	      document.forms.disall.elements[i].checked=true;
  721:             }
  722:         }
  723:     }
  724: 
  725:     function uncheckall() {
  726: 	for (i=0; i<document.forms.disall.elements.length; i++) {
  727:             if 
  728:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
  729: 	      document.forms.disall.elements[i].checked=false;
  730:             }
  731:         }
  732:     }
  733: </script>
  734: ENDDISHEADER
  735:     $r->print('<h1>'.&mt('Display All Messages').'</h1><form method=post name=disall '.
  736: 	      'action="/adm/email">'.
  737: 	      '<table border=2><tr><th colspan=2>&nbsp</th><th>');
  738:     if ($ENV{'form.sortedby'} eq "revdate") {
  739: 	$r->print('<a href = "?sortedby=date">'.&mt('Date').'</a></th>');
  740:     } else {
  741: 	$r->print('<a href = "?sortedby=revdate">'.&mt('Date').'</a></th>');
  742:     }
  743:     $r->print('<th>');
  744:     if ($ENV{'form.sortedby'} eq "revuser") {
  745: 	$r->print('<a href = "?sortedby=user">'.&mt('Username').'</a>');
  746:     } else {
  747: 	$r->print('<a href = "?sortedby=revuser">'.&mt('Username').'</a>');
  748:     }
  749:     $r->print('</th><th>');
  750:     if ($ENV{'form.sortedby'} eq "revdomain") {
  751: 	$r->print('<a href = "?sortedby=domain">'.&mt('Domain').'</a>');
  752:     } else {
  753: 	$r->print('<a href = "?sortedby=revdomain">'.&mt('Domain').'</a>');
  754:     }
  755:     $r->print('</th><th>');
  756:     if ($ENV{'form.sortedby'} eq "revsubject") {
  757: 	$r->print('<a href = "?sortedby=subject">'.&mt('Subject').'</a>');
  758:     } else {
  759:     	$r->print('<a href = "?sortedby=revsubject">'.&mt('Subject').'</a>');
  760:     }
  761:     $r->print('</th><th>');
  762:     if ($ENV{'form.sortedby'} eq "revstatus") {
  763: 	$r->print('<a href = "?sortedby=status">'.&mt('Status').'</th>');
  764:     } else {
  765:      	$r->print('<a href = "?sortedby=revstatus">'.&mt('Status').'</th>');
  766:     }
  767:     $r->print('</tr>');
  768:     my @temp=sortedmessages();
  769:     foreach (@temp){
  770: 	my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID)= @$_;
  771: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
  772: 	    if ($status eq 'new') {
  773: 		$r->print('<tr bgcolor="#FFBB77">');
  774: 	    } elsif ($status eq 'read') {
  775: 		$r->print('<tr bgcolor="#BBBB77">');
  776: 	    } elsif ($status eq 'replied') {
  777: 		$r->print('<tr bgcolor="#AAAA88">'); 
  778: 	    } else {
  779: 		$r->print('<tr bgcolor="#99BBBB">');
  780: 	    }
  781: 	    $r->print('<td><a href="/adm/email?display='.$origID.$sqs. 
  782: 		      '">'.&mt('Open').'</a></td><td><a href="/adm/email?markdel='.$origID.$sqs.
  783: 		      '">'.&mt('Delete').'</a><input type=checkbox name="delmark_'.$origID.'"></td>'.
  784: 		      '<td>'.&Apache::lonlocal::locallocaltime($sendtime).'</td><td>'.
  785: 		      $fromname.'</td><td>'.$fromdomain.'</td><td>'.
  786: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
  787:                       $status.'</td></tr>');
  788: 	}
  789:     }   
  790:     $r->print('</table><p>'.
  791:               '<a href="javascript:checkall()">'.&mt('Check All').'</a>&nbsp;'.
  792:               '<a href="javascript:uncheckall()">'.&mt('Uncheck All').'</a><p>'.
  793: 	      '<input type="hidden" name="sortedby" value="'.$ENV{'form.sortedby'}.'" />'.
  794:               '<input type=submit name="markeddel" value="'.&mt('Delete Checked').'">'.
  795:               '</form></body></html>');
  796: }
  797: 
  798: # ============================================================== Compose output
  799: 
  800: sub compout {
  801:     my ($r,$forwarding,$broadcast)=@_;
  802:       my $dispcrit='';
  803:     my $dissub='';
  804:     my $dismsg='';
  805:     my $func=&mt('Send New');
  806:     my %lt=&Apache::lonlocal::texthash('us' => 'Username',
  807: 				       'do' => 'Domain',
  808: 				       'ad' => 'Additional Recipients',
  809: 				       'sb' => 'Subject',
  810: 				       'ca' => 'Cancel',
  811: 				       'ma' => 'Mail');
  812: 
  813:     if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  814: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
  815:          $dispcrit=
  816:  '<input type="checkbox" name="critmsg"> '.&mt('Send as critical message').' ' . $crithelp . 
  817:  '<br>'.
  818:  '<input type="checkbox" name="sendbck"> '.&mt('Send as critical message').'  ' .
  819:  &mt('and return receipt') . $crithelp . '<p>';
  820:       }
  821:     if ($forwarding) {
  822:        $dispcrit.='<input type="hidden" name="forwid" value="'.
  823: 	   $forwarding.'">';
  824:        $func=&mt('Forward');
  825:       my %message=&Apache::lonnet::get('nohist_email',[$forwarding]);
  826:       my %content=&unpackagemsg($message{$forwarding});
  827: 
  828:        $dissub=&mt('Forwarding').': '.$content{'subject'};
  829:        $dismsg=&mt('Forwarded message from').' '.
  830: 	   $content{'sendername'}.' '.&mt('at').' '.$content{'senderdomain'};
  831:     }
  832:     my $defdom=$ENV{'user.domain'};
  833:     if ($ENV{'form.recdom'}) { $defdom=$ENV{'form.recdom'}; }
  834:       $r->print(
  835:                 '<form action="/adm/email"  name="compemail" method="post"'.
  836:                 ' enctype="multipart/form-data">'."\n".
  837:                 '<input type="hidden" name="sendmail" value="on">'."\n".
  838:                 '<table>');
  839:     unless (($broadcast eq 'group') || ($broadcast eq 'upload')) {
  840:         my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
  841:         my $selectlink=&Apache::loncommon::selectstudent_link
  842: 	    ('compemail','recuname','recdomain');
  843:        $r->print(<<"ENDREC");
  844: <table>
  845: <tr><td>$lt{'us'}:</td><td><input type="text" size="12" name="recuname" value="$ENV{'form.recname'}"></td><td rowspan="2">$selectlink</td></tr>
  846: <tr><td>$lt{'do'}:</td>
  847: <td>$domform</td></tr>
  848: ENDREC
  849:     }
  850:     my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
  851:     if ($broadcast ne 'upload') {
  852:        $r->print(<<"ENDCOMP");
  853: <tr><td>$lt{'ad'}<br /><tt>username\@domain,username\@domain, ...
  854: </tt></td><td>
  855: <input type="text" size="50" name="additionalrec"></td></tr>
  856: <tr><td>$lt{'sb'}:</td><td><input type="text" size="50" name="subject" value="$dissub">
  857: </td></tr></table>
  858: $latexHelp
  859: <textarea name="message" cols="80" rows="10" wrap="hard">$dismsg
  860: </textarea></p><br />
  861: $dispcrit
  862: <input type="submit" name="send" value="$func $lt{'ma'}" />
  863: <input type="submit" name="cancel" value="$lt{'ca'}" />
  864: ENDCOMP
  865:     } else { # $broadcast is 'upload'
  866: 	$r->print(<<ENDUPLOAD);
  867: <input type=hidden name=sendmode value=upload>
  868: <h3>Generate messages from a file</h3>
  869: <p>
  870: Subject: <input type=text size=50 name=subject>
  871: </p>
  872: <p>General message text<br />
  873: <textarea name=message cols=60 rows=10 wrap=hard>$dismsg
  874: </textarea></p>
  875: <p>
  876: The file format for the uploaded portion of the message is:
  877: <pre>
  878: username1\@domain1: text
  879: username2\@domain2: text
  880: username3\@domain1: text
  881: </pre>
  882: </p>
  883: <p>
  884: The messages will be assembled from all lines with the respective 
  885: <tt>username\@domain</tt>, and appended to the general message text.</p>
  886: <p>
  887: <input type=file name=upfile size=20><p>
  888: $dispcrit
  889: <input type=submit value="Upload and send">
  890: ENDUPLOAD
  891:     }
  892:     if ($broadcast eq 'group') {
  893:        &discourse;
  894:     }
  895:     $r->print('</form>');
  896: }
  897: 
  898: # ---------------------------------------------------- Display all face to face
  899: 
  900: sub disfacetoface {
  901:     my ($r,$user,$domain)=@_;
  902:     unless ($ENV{'request.course.id'}) { return; }
  903:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  904: 	return;
  905:     }
  906:     my %records=&Apache::lonnet::dump('nohist_email',
  907: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  908: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  909:                          '%255b'.$user.'%253a'.$domain.'%255d');
  910:     my $result='';
  911:     foreach (sort keys %records) {
  912:         my %content=&unpackagemsg($records{$_});
  913:         next if ($content{'senderdomain'} eq '');
  914:         $content{'message'}=~s/\n/\<br\>/g;
  915:         if ($content{'subject'}=~/^Record/) {
  916: 	    $result.='<h3>'.&mt('Record').'</h3>';
  917:         } else {
  918:             $result.='<h3>'.&mt('Sent Message').'</h3>';
  919:             %content=&unpackagemsg($content{'message'});
  920:             $content{'message'}=
  921:                 '<b>Subject: '.$content{'subject'}.'</b><br />'.
  922: 		$content{'message'};
  923:         }
  924:         $result.=&mt('By').': <b>'.
  925: &Apache::loncommon::aboutmewrapper(
  926:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
  927: $content{'sendername'}.'@'.
  928:             $content{'senderdomain'}.') '.$content{'time'}.
  929:             '<br><blockquote>'.
  930:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
  931: 	      '</blockquote>';
  932:      }
  933:     # Check to see if there were any messages.
  934:     if ($result eq '') {
  935:         $r->print("<p><b>No notes, face-to-face discussion records, or critical messages in this course.</b></p>");
  936:     } else {
  937:        $r->print($result);
  938:     }
  939: }
  940: 
  941: # ---------------------------------------------------------------- Face to face
  942: 
  943: sub facetoface {
  944:     my ($r,$stage)=@_;
  945:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  946: 	return;
  947:     }
  948: # from query string
  949:     if ($ENV{'form.recname'}) { $ENV{'form.recuname'}=$ENV{'form.recname'}; }
  950:     if ($ENV{'form.recdom'}) { $ENV{'form.recdomain'}=$ENV{'form.recdom'}; }
  951: 
  952:     my $defdom=$ENV{'user.domain'};
  953: # already filled in
  954:     if ($ENV{'form.recdomain'}) { $defdom=$ENV{'form.recdomain'}; }
  955: # generate output
  956:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
  957:     my $stdbrws = &Apache::loncommon::selectstudent_link
  958: 	('stdselect','recuname','recdomain');
  959:     $r->print(<<"ENDTREC");
  960: <h3>User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course</h3>
  961: <form method="post" action="/adm/email" name="stdselect">
  962: <input type="hidden" name="recordftf" value="retrieve" />
  963: <table>
  964: <tr><td>Username:</td><td><input type=text size=12 name=recuname value="$ENV{'form.recuname'}"></td>
  965: <td rowspan="2">
  966: $stdbrws
  967: <input type="submit" value="Retrieve discussion and message records"></td>
  968: </tr>
  969: <tr><td>Domain:</td>
  970: <td>$domform</td></tr>
  971: </table>
  972: </form>
  973: ENDTREC
  974:     if (($stage ne 'query') &&
  975:         ($ENV{'form.recdomain'}) && ($ENV{'form.recuname'})) {
  976:         chomp($ENV{'form.newrecord'});
  977:         if ($ENV{'form.newrecord'}) {
  978:            &user_normal_msg_raw(
  979:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  980:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  981:             'Record ['.$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}.']',
  982: 	    $ENV{'form.newrecord'});
  983:         }
  984:         $r->print('<h3>'.&Apache::loncommon::plainname($ENV{'form.recuname'},
  985: 				     $ENV{'form.recdomain'}).'</h3>');
  986:         &disfacetoface($r,$ENV{'form.recuname'},$ENV{'form.recdomain'});
  987: 	$r->print(<<ENDRHEAD);
  988: <form method="post" action="/adm/email">
  989: <input name="recdomain" value="$ENV{'form.recdomain'}" type="hidden" />
  990: <input name="recuname" value="$ENV{'form.recuname'}" type="hidden" />
  991: ENDRHEAD
  992:         $r->print(<<ENDBFORM);
  993: <hr />New Record (record is visible to course faculty and staff)<br />
  994: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
  995: <br />
  996: <input type="hidden" name="recordftf" value="post" />
  997: <input type="submit" value="Post this record" />
  998: </form>
  999: ENDBFORM
 1000:     }
 1001: }
 1002: 
 1003: # ===================================================================== Handler
 1004: 
 1005: sub handler {
 1006:     my $r=shift;
 1007: 
 1008: # ----------------------------------------------------------- Set document type
 1009: 
 1010:   &Apache::loncommon::content_type($r,'text/html');
 1011:   $r->send_http_header;
 1012: 
 1013:   return OK if $r->header_only;
 1014: 
 1015: # --------------------------- Get query string for limited number of parameters
 1016:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1017:         ['display','replyto','forward','markread','markdel','markunread',
 1018:          'sendreply','compose','sendmail','critical','recname','recdom',
 1019:          'recordftf','sortedby']);
 1020:     $sqs='&sortedby='.$ENV{'form.sortedby'};
 1021: # ------------------------------------------------------ They checked for email
 1022:   &Apache::lonnet::put('email_status',{'recnewemail'=>0});
 1023: # --------------------------------------------------------------- Render Output
 1024:   if (!$ENV{'form.display'}) {
 1025:       $r->print('<html><head><title>EMail and Messaging</title>'.
 1026: 		&Apache::loncommon::studentbrowser_javascript().'</head>'.
 1027: 		&Apache::loncommon::bodytag('EMail and Messages'));
 1028:   }
 1029:   if ($ENV{'form.display'}) {
 1030:       my $msgid=$ENV{'form.display'};
 1031:       &statuschange($msgid,'read');
 1032:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
 1033:       my %content=&unpackagemsg($message{$msgid});
 1034: # info to generate "next" and "previous" buttons
 1035:       my @messages=&sortedmessages();
 1036:       my $counter=0;
 1037:       $r->print('<pre>');
 1038:       my $escmsgid=&Apache::lonnet::escape($msgid);
 1039:       foreach (@messages) {
 1040:  	  if ($_->[5] eq $escmsgid){
 1041:  	      last;
 1042:  	  }
 1043:  	  $counter++;
 1044:       }
 1045:       $r->print('</pre>');
 1046:       my $number_of_messages = scalar(@messages); #subtract 1 for last index
 1047: # start output
 1048:       $r->print('<html><head><title>EMail and Messaging</title>');
 1049:       if (defined($content{'baseurl'})) {
 1050: 	  $r->print("<base href=\"http://$ENV{'SERVER_NAME'}/$content{'baseurl'}\" />");
 1051:       }
 1052:       $r->print(&Apache::loncommon::studentbrowser_javascript().
 1053: 		'</head>'.
 1054: 		&Apache::loncommon::bodytag('EMail and Messages'));
 1055:       $r->print('<b>'.&mt('Subject').':</b> '.$content{'subject'}.
 1056:              '<br><b>'.&mt('From').':</b> '.
 1057: &Apache::loncommon::aboutmewrapper(
 1058: &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
 1059: $content{'sendername'},$content{'senderdomain'}).' ('.
 1060:                                  $content{'sendername'}.' at '.
 1061:                                  $content{'senderdomain'}.') '.
 1062:              '<br><b>'.&mt('Time').':</b> '.$content{'time'}.'<p>'.
 1063:              '<table border=2><tr bgcolor="#FFFFAA"><td>'.&mt('Functions').':</td>'.
 1064:            '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).$sqs.
 1065:              '"><b>'.&mt('Reply').'</b></a></td>'.
 1066:            '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).$sqs.
 1067:              '"><b>'.&mt('Forward').'</b></a></td>'.
 1068:         '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).$sqs.
 1069:              '"><b>'.&mt('Mark Unread').'</b></a></td>'.
 1070:         '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).$sqs.
 1071:              '"><b>Delete</b></a></td>'.
 1072: 		'<td><a href="/adm/email?sortedby='.$ENV{'form.sortedby'}.
 1073: 		'"><b>'.&mt('Display all Messages').'</b></a></td>');
 1074:       if ($counter > 0){
 1075:  	  $r->print('<td><a href="/adm/email?display='.$messages[$counter-1]->[5].$sqs.
 1076:            '"><b>'.&mt('Previous').'</b></a></td>');
 1077:        }
 1078:        if ($counter < $number_of_messages - 1){
 1079:  	  $r->print('<td><a href="/adm/email?display='.$messages[$counter+1]->[5].$sqs.
 1080:            '"><b>'.&mt('Next').'</b></a></td>');
 1081:        }
 1082:        $r->print('</tr></table><p><pre>'.
 1083:              &Apache::lontexconvert::msgtexconverted($content{'message'}).
 1084:              '</pre><hr>'.$content{'citation'});
 1085:   } elsif ($ENV{'form.replyto'}) {
 1086:       &comprep($r,$ENV{'form.replyto'});
 1087:   } elsif ($ENV{'form.sendreply'}) {
 1088:       if ($ENV{'form.send'}) {
 1089: 	  my $msgid=$ENV{'form.sendreply'};
 1090: 	  my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
 1091: 	  my %content=&unpackagemsg($message{$msgid},1);
 1092: 	  &statuschange($msgid,'replied');
 1093: 	  if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
 1094: 	      (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
 1095: 	      $r->print(&mt('Sending critical message').': '.
 1096: 			&user_crit_msg($content{'sendername'},
 1097: 				       $content{'senderdomain'},
 1098: 				       &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1099: 				       &Apache::lonfeedback::clear_out_html($ENV{'form.message'}),
 1100: 				       $ENV{'form.sendbck'}));
 1101: 	  } else {
 1102: 	      $r->print(&mt('Sending').': '.&user_normal_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: 	  }
 1107:       }
 1108:       if ($ENV{'form.displayedcrit'}) {
 1109:           &discrit($r);
 1110:       } else {
 1111: 	  &disall($r);
 1112:       }
 1113:   } elsif ($ENV{'form.confirm'}) {
 1114:       foreach (keys %ENV) {
 1115:           if ($_=~/^form\.rec\_(.*)$/) {
 1116: 	      $r->print('<b>Confirming Receipt:</b> '.
 1117:                         &user_crit_received($1).'<br>');
 1118:           }
 1119:           if ($_=~/^form\.reprec\_(.*)$/) {
 1120:               my $msgid=$1;
 1121: 	      $r->print('<b>Confirming Receipt:</b> '.
 1122:                         &user_crit_received($msgid).'<br>');
 1123:               &comprep($r,$msgid);
 1124:           }
 1125:       }
 1126:       &discrit($r);
 1127:   } elsif ($ENV{'form.critical'}) {
 1128:       &discrit($r);
 1129:   } elsif ($ENV{'form.forward'}) {
 1130:       &compout($r,$ENV{'form.forward'});
 1131:   } elsif ($ENV{'form.markread'}) {
 1132:   } elsif ($ENV{'form.markdel'}) {
 1133:       &statuschange($ENV{'form.markdel'},'deleted');
 1134:       &disall($r);
 1135:   } elsif ($ENV{'form.markeddel'}) {
 1136:       my $total=0;
 1137:       foreach (keys %ENV) {
 1138:           if ($_=~/^form\.delmark_(.*)$/) {
 1139: 	      &statuschange(&Apache::lonnet::unescape($1),'deleted');
 1140:               $total++;
 1141:           }
 1142:       }
 1143:       $r->print('Deleted '.$total.' message(s)<p>');
 1144:       &disall($r);
 1145:   } elsif ($ENV{'form.markunread'}) {
 1146:       &statuschange($ENV{'form.markunread'},'new');
 1147:       &disall($r);
 1148:   } elsif ($ENV{'form.compose'}) {
 1149:       &compout($r,'',$ENV{'form.compose'});
 1150:   } elsif ($ENV{'form.recordftf'}) {
 1151:       &facetoface($r,$ENV{'form.recordftf'});
 1152:   } elsif ($ENV{'form.sendmail'}) {
 1153:       my $sendstatus='';
 1154:       if ($ENV{'form.send'}) {
 1155: 	  my %content=();
 1156: 	  undef %content;
 1157: 	  if ($ENV{'form.forwid'}) {
 1158: 	      my $msgid=$ENV{'form.forwid'};
 1159: 	      my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
 1160: 	      %content=&unpackagemsg($message{$msgid},1);
 1161: 	      &statuschange($msgid,'forwarded');
 1162: 	      $ENV{'form.message'}.="\n\n-- Forwarded message --\n\n".
 1163: 		  $content{'message'};
 1164: 	  }
 1165: 	  my %toaddr=();
 1166: 	  undef %toaddr;
 1167: 	  if ($ENV{'form.sendmode'} eq 'group') {
 1168: 	      foreach (keys %ENV) {
 1169: 		  if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
 1170: 		      $toaddr{$1}='';
 1171: 		  }
 1172: 	      }
 1173: 	  } elsif ($ENV{'form.sendmode'} eq 'upload') {
 1174: 	      foreach (split(/[\n\r\f]+/,$ENV{'form.upfile'})) {
 1175: 		  my ($rec,$txt)=split(/\s*\:\s*/,$_);
 1176: 		  if ($txt) {
 1177: 		      $rec=~s/\@/\:/;
 1178: 		      $toaddr{$rec}.=$txt."\n";
 1179: 		  }
 1180: 	      }
 1181: 	  } else {
 1182: 	      $toaddr{$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}}='';
 1183: 	  }
 1184: 	  if ($ENV{'form.additionalrec'}) {
 1185: 	      foreach (split(/\,/,$ENV{'form.additionalrec'})) {
 1186: 		  my ($auname,$audom)=split(/\@/,$_);
 1187: 		  $toaddr{$auname.':'.$audom}='';
 1188: 	      }
 1189: 	  }
 1190: 	  foreach (keys %toaddr) {
 1191: 	      my ($recuname,$recdomain)=split(/\:/,$_);
 1192: 	      my $msgtxt=&Apache::lonfeedback::clear_out_html($ENV{'form.message'});
 1193: 	      if ($toaddr{$_}) { $msgtxt.='<hr>'.$toaddr{$_}; }    
 1194: 	      if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
 1195: 		  (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
 1196: 		  $r->print(&mt('Sending critical message').' ...');
 1197:                   $sendstatus.=' '.&user_crit_msg($recuname,$recdomain,
 1198: 					   &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1199: 					   $msgtxt,
 1200: 					   $ENV{'form.sendbck'});
 1201: 	      } else {
 1202: 		  $r->print(&mt('Sending').' ...');
 1203:                   $sendstatus.=' '.&user_normal_msg($recuname,$recdomain,
 1204: 				                         &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
 1205: 							 $msgtxt,
 1206: 							 $content{'citation'});
 1207: 	      }
 1208: 	      $r->print('<br />');
 1209: 	  }
 1210:       }
 1211:       if ($sendstatus=~/^(\s*(?:ok|con_delayed)\s*)*$/) {
 1212: 	  if ($ENV{'form.displayedcrit'}) {
 1213: 	      &discrit($r);
 1214: 	  } else {
 1215: 	      &disall($r);
 1216: 	  }
 1217:       } else {
 1218: 	  $r->print(
 1219:   '<h2><font color="red">'.&mt('Could not deliver message').'</font></h2>'.
 1220:   &mt('Please use the browser "Back" button and correct the recipient addresses')
 1221: 		    );
 1222:       }
 1223:   } else {
 1224:       &disall($r);
 1225:   }
 1226:   $r->print('</body></html>');
 1227:   return OK;
 1228: 
 1229: }
 1230: # ================================================= Main program, reset counter
 1231: 
 1232: BEGIN {
 1233:     $msgcount=0;
 1234: }
 1235: 
 1236: =pod
 1237: 
 1238: =back
 1239: 
 1240: =cut
 1241: 
 1242: 1; 
 1243: 
 1244: __END__
 1245: 
 1246: 
 1247: 
 1248: 
 1249: 
 1250: 
 1251: 

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