File:  [LON-CAPA] / loncom / interface / lonmsg.pm
Revision 1.54: download - view: text, annotated - select for diffs
Wed Apr 2 14:57:32 2003 UTC (21 years, 2 months ago) by www
Branches: MAIN
CVS tags: version_0_99_2, version_0_99_1, version_0_99_0, conference_2003, HEAD
Direct notification email directly to login screen.

    1: # The LearningOnline Network with CAPA
    2: # Routines for messaging
    3: #
    4: # $Id: lonmsg.pm,v 1.54 2003/04/02 14:57:32 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: use strict;
   48: use Apache::lonnet();
   49: use vars qw($msgcount);
   50: use HTML::TokeParser();
   51: use Apache::Constants qw(:common);
   52: use Apache::loncommon();
   53: use Apache::lontexconvert();
   54: use HTML::Entities();
   55: use Mail::Send;
   56: 
   57: # ===================================================================== Package
   58: 
   59: sub packagemsg {
   60:     my ($subject,$message,$citation,$baseurl,$attachmenturl)=@_;
   61:     $message =&HTML::Entities::encode($message);
   62:     $citation=&HTML::Entities::encode($citation);
   63:     $subject =&HTML::Entities::encode($subject);
   64:     #remove machine specification
   65:     $baseurl =~ s|^http://[^/]+/|/|;
   66:     $baseurl =&HTML::Entities::encode($baseurl);
   67:     #remove machine specification
   68:     $attachmenturl =~ s|^http://[^/]+/|/|;
   69:     $attachmenturl =&HTML::Entities::encode($attachmenturl);
   70: 
   71:     my $now=time;
   72:     $msgcount++;
   73:     my $partsubj=$subject;
   74:     $partsubj=&Apache::lonnet::escape($partsubj);
   75:     my $msgid=&Apache::lonnet::escape(
   76:            $now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
   77:            $ENV{'user.domain'}.':'.$msgcount.':'.$$);
   78:     my $result='<sendername>'.$ENV{'user.name'}.'</sendername>'.
   79:            '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
   80:            '<subject>'.$subject.'</subject>'.
   81: 	   '<time>'.localtime($now).'</time>'.
   82: 	   '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
   83:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
   84: 	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
   85: 	   '<browsertype>'.$ENV{'browser.type'}.'</browsertype>'.
   86: 	   '<browseros>'.$ENV{'browser.os'}.'</browseros>'.
   87: 	   '<browserversion>'.$ENV{'browser.version'}.'</browserversion>'.
   88:            '<browsermathml>'.$ENV{'browser.mathml'}.'</browsermathml>'.
   89: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
   90: 	   '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
   91: 	   '<role>'.$ENV{'request.role'}.'</role>'.
   92: 	   '<resource>'.$ENV{'request.filename'}.'</resource>'.
   93:            '<msgid>'.$msgid.'</msgid>'.
   94: 	   '<message>'.$message.'</message>';
   95:     if (defined($citation)) {
   96: 	$result.='<citation>'.$citation.'</citation>';
   97:     }
   98:     if (defined($baseurl)) {
   99: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
  100:     }
  101:     if (defined($attachmenturl)) {
  102: 	$result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
  103:     }
  104:     return $msgid,$result;
  105: }
  106: 
  107: # ================================================== Unpack message into a hash
  108: 
  109: sub unpackagemsg {
  110:     my ($message,$notoken)=@_;
  111:     my %content=();
  112:     my $parser=HTML::TokeParser->new(\$message);
  113:     my $token;
  114:     while ($token=$parser->get_token) {
  115:        if ($token->[0] eq 'S') {
  116: 	   my $entry=$token->[1];
  117:            my $value=$parser->get_text('/'.$entry);
  118:            $content{$entry}=$value;
  119:        }
  120:     }
  121:     if ($content{'attachmenturl'}) {
  122:        my ($fname,$ft)=($content{'attachmenturl'}=~/\/(\w+)\.(\w+)$/);
  123:        if ($notoken) {
  124: 	   $content{'message'}.='<p>Attachment: <tt>'.$fname.'.'.$ft.'</tt>';
  125:        } else {
  126: 	   $content{'message'}.='<p>Attachment: <a href="'.
  127: 	       &Apache::lonnet::tokenwrapper($content{'attachmenturl'}).
  128: 	       '"><tt>'.$fname.'.'.$ft.'</tt></a>';
  129:        }
  130:     }
  131:     return %content;
  132: }
  133: 
  134: # ======================================================= Get info out of msgid
  135: 
  136: sub unpackmsgid {
  137:     my $msgid=&Apache::lonnet::unescape(shift);
  138:     my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
  139:                           &Apache::lonnet::unescape($msgid));
  140:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
  141:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  142:     unless ($status{$msgid}) { $status{$msgid}='new'; }
  143:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
  144: } 
  145: 
  146: 
  147: sub sendemail {
  148:     my ($to,$subject,$body)=@_;
  149:     $body=
  150:     "*** This is an automatic message generated by the LON-CAPA system.\n".
  151:     "*** Please do not reply to this address.\n\n".$body;
  152:     my $msg = new Mail::Send;
  153:     $msg->to($to);
  154:     $msg->subject('[LON-CAPA] '.$subject);
  155:     my $fh = $msg->open('smtp',Server => 'localhost');
  156:     print $fh $body;
  157:     $fh->close;
  158: }
  159: 
  160: # ==================================================== Send notification emails
  161: 
  162: sub sendnotification {
  163:     my ($to,$touname,$toudom,$subj,$crit)=@_;
  164:     my $sender=$ENV{'environment.firstname'}.' '.$ENV{'environment.lastname'};
  165:     my $critical=($crit?' critical':'');
  166:     my $url='http://'.
  167:       $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
  168:       '/adm/email?username='.$touname.'&domain='.$toudom;
  169:     my $body=(<<ENDMSG);
  170: You received a$critical message from $sender in LON-CAPA. The subject is
  171: 
  172:  $subj
  173: 
  174: Use
  175: 
  176:  $url
  177: 
  178: to access this message.
  179: ENDMSG
  180:     &sendemail($to,'New'.$critical.' message from '.$sender,$body);
  181: }
  182: # ============================================================= Check for email
  183: 
  184: sub newmail {
  185:     if ((time-$ENV{'user.mailcheck.time'})>300) {
  186:         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
  187:         &Apache::lonnet::appenv('user.mailcheck.time'=>time);
  188:         if ($what{'recnewemail'}>0) { return 1; }
  189:     }
  190:     return 0;
  191: }
  192: 
  193: # =============================== Automated message to the author of a resource
  194: 
  195: sub author_res_msg {
  196:     my ($filename,$message)=@_;
  197:     unless ($message) { return 'empty'; }
  198:     $filename=&Apache::lonnet::declutter($filename);
  199:     my ($domain,$author,@dummy)=split(/\//,$filename);
  200:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
  201:     if ($homeserver ne 'no_host') {
  202:        my $id=unpack("%32C*",$message);
  203:        my $msgid;
  204:        ($msgid,$message)=&packagemsg($filename,$message);
  205:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
  206:          ':nohist_res_msgs:'.
  207:           &Apache::lonnet::escape($filename.'_'.$id).'='.
  208:           &Apache::lonnet::escape($message),$homeserver);
  209:     }
  210:     return 'no_host';
  211: }
  212: 
  213: # ================================================== Critical message to a user
  214: 
  215: sub user_crit_msg_raw {
  216:     my ($user,$domain,$subject,$message,$sendback)=@_;
  217: # Check if allowed missing
  218:     my $status='';
  219:     my $msgid='undefined';
  220:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  221:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  222:     if ($homeserver ne 'no_host') {
  223:        ($msgid,$message)=&packagemsg($subject,$message);
  224:        if ($sendback) { $message.='<sendback>true</sendback>'; }
  225:        $status=&Apache::lonnet::critical(
  226:            'put:'.$domain.':'.$user.':critical:'.
  227:            &Apache::lonnet::escape($msgid).'='.
  228:            &Apache::lonnet::escape($message),$homeserver);
  229:        if ($ENV{'request.course.id'}) {
  230:           &user_normal_msg_raw(
  231:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  232:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  233:             'Critical ['.$user.':'.$domain.']',
  234: 	    $message);
  235:        }
  236:     } else {
  237:        $status='no_host';
  238:     }
  239: # Notifications
  240:     my %userenv = &Apache::lonnet::get('environment',['critnotification'],
  241:                                        $domain,$user);
  242:     if ($userenv{'critnotification'}) {
  243:       &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1);
  244:     }
  245: # Log this
  246:     &Apache::lonnet::logthis(
  247:       'Sending critical email '.$msgid.
  248:       ', log status: '.
  249:       &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  250:                          $ENV{'user.home'},
  251:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
  252:       .$status));
  253:     return $status;
  254: }
  255: 
  256: # New routine that respects "forward" and calls old routine
  257: 
  258: sub user_crit_msg {
  259:     my ($user,$domain,$subject,$message,$sendback)=@_;
  260:     my $status='';
  261:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
  262:                                        $domain,$user);
  263:     my $msgforward=$userenv{'msgforward'};
  264:     if ($msgforward) {
  265:        foreach (split(/\,/,$msgforward)) {
  266: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
  267:          $status.=
  268: 	   &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
  269:                 $sendback).' ';
  270:        }
  271:     } else { 
  272: 	$status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback);
  273:     }
  274:     return $status;
  275: }
  276: 
  277: # =================================================== Critical message received
  278: 
  279: sub user_crit_received {
  280:     my $msgid=shift;
  281:     my %message=&Apache::lonnet::get('critical',[$msgid]);
  282:     my %contents=&unpackagemsg($message{$msgid},1);
  283:     my $status='rec: '.($contents{'sendback'}?
  284:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
  285:                      'Receipt: '.$ENV{'user.name'}.' at '.$ENV{'user.domain'},
  286:                      'User '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  287:                      ' acknowledged receipt of message'."\n".'   "'.
  288:                      $contents{'subject'}.'"'."\n".'dated '.
  289:                      $contents{'time'}.".\n"
  290:                      ):'no msg req');
  291:     $status.=' trans: '.
  292:      &Apache::lonnet::put(
  293:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
  294:     $status.=' del: '.
  295:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
  296:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  297:                          $ENV{'user.home'},'Received critical message '.
  298:                          $contents{'msgid'}.
  299:                          ', '.$status);
  300:     return $status;
  301: }
  302: 
  303: # ======================================================== Normal communication
  304: 
  305: sub user_normal_msg_raw {
  306:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
  307: # Check if allowed missing
  308:     my $status='';
  309:     my $msgid='undefined';
  310:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  311:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  312:     if ($homeserver ne 'no_host') {
  313:        ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl,
  314:                                      $attachmenturl);
  315:        $status=&Apache::lonnet::critical(
  316:            'put:'.$domain.':'.$user.':nohist_email:'.
  317:            &Apache::lonnet::escape($msgid).'='.
  318:            &Apache::lonnet::escape($message),$homeserver);
  319:        &Apache::lonnet::put
  320:                          ('email_status',{'recnewemail'=>time},$domain,$user);
  321:     } else {
  322:        $status='no_host';
  323:     }
  324: # Notifications
  325:     my %userenv = &Apache::lonnet::get('environment',['notification'],
  326:                                        $domain,$user);
  327:     if ($userenv{'notification'}) {
  328: 	&sendnotification($userenv{'notification'},$user,$domain,$subject,0);
  329:     }
  330:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  331:                          $ENV{'user.home'},
  332:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
  333:     return $status;
  334: }
  335: 
  336: # New routine that respects "forward" and calls old routine
  337: 
  338: sub user_normal_msg {
  339:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
  340:     my $status='';
  341:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
  342:                                        $domain,$user);
  343:     my $msgforward=$userenv{'msgforward'};
  344:     if ($msgforward) {
  345:        foreach (split(/\,/,$msgforward)) {
  346: 	 my ($forwuser,$forwdomain)=split(/\:/,$_);
  347:          $status.=
  348: 	  &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
  349: 			       $citation,$baseurl,$attachmenturl).' ';
  350:        }
  351:     } else { 
  352: 	$status=&user_normal_msg_raw($user,$domain,$subject,$message,
  353: 				     $citation,$baseurl,$attachmenturl);
  354:     }
  355:     return $status;
  356: }
  357: 
  358: 
  359: # =============================================================== Status Change
  360: 
  361: sub statuschange {
  362:     my ($msgid,$newstatus)=@_;
  363:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
  364:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  365:     unless ($status{$msgid}) { $status{$msgid}='new'; }
  366:     unless (($status{$msgid} eq 'replied') || 
  367:             ($status{$msgid} eq 'forwarded')) {
  368: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
  369:     }
  370:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
  371: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
  372:     }
  373: }
  374: 
  375: # ======================================================= Display a course list
  376: 
  377: sub discourse {
  378:     my $r=shift;
  379:     my %courselist=&Apache::lonnet::dump(
  380:                    'classlist',
  381: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  382: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  383:     my $now=time;
  384:     $r->print(<<ENDDISHEADER);
  385: <input type=hidden name=sendmode value=group>
  386: <script>
  387:     function checkall() {
  388: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
  389:             if 
  390:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
  391: 	      document.forms.compemail.elements[i].checked=true;
  392:             }
  393:         }
  394:     }
  395: 
  396:     function checksec() {
  397: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
  398:             if 
  399:           (document.forms.compemail.elements[i].name.indexOf
  400:            ('send_to_&&&'+document.forms.compemail.chksec.value)==0) {
  401: 	      document.forms.compemail.elements[i].checked=true;
  402:             }
  403:         }
  404:     }
  405: 
  406:     function uncheckall() {
  407: 	for (i=0; i<document.forms.compemail.elements.length; i++) {
  408:             if 
  409:           (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
  410: 	      document.forms.compemail.elements[i].checked=false;
  411:             }
  412:         }
  413:     }
  414: </script>
  415: <input type=button onClick="checkall()" value="Check for All">&nbsp;
  416: <input type=button onClick="checksec()" value="Check for Section/Group">
  417: <input type=text size=5 name=chksec>&nbsp;
  418: <input type=button onClick="uncheckall()" value="Check for None">
  419: <p>
  420: ENDDISHEADER
  421:     foreach (sort keys %courselist) {
  422:         my ($end,$start)=split(/\:/,$courselist{$_});
  423:         my $active=1;
  424:         if (($end) && ($now>$end)) { $active=0; }
  425:         if ($active) {
  426:            my ($sname,$sdom)=split(/\:/,$_);
  427:            my %reply=&Apache::lonnet::get('environment',
  428:               ['firstname','middlename','lastname','generation'],
  429:               $sdom,$sname);
  430:            my $section=&Apache::lonnet::usection
  431: 	       ($sdom,$sname,$ENV{'request.course.id'});
  432:            $r->print(
  433:         '<br><input type=checkbox name="send_to_&&&'.$section.'&&&_'.$_.'"> '.
  434: 		      $reply{'firstname'}.' '. 
  435:                       $reply{'middlename'}.' '.
  436:                       $reply{'lastname'}.' '.
  437:                       $reply{'generation'}.
  438:                       ' ('.$_.') '.$section);
  439:         } 
  440:     }
  441: }
  442: 
  443: # ==================================================== Display Critical Message
  444: 
  445: sub discrit {
  446:     my $r=shift;
  447:     my $header = '<h1><font color=red>Critical Messages</font></h1>'.
  448:         '<form action=/adm/email method=post>'.
  449:         '<input type=hidden name=confirm value=true>';
  450:     my %what=&Apache::lonnet::dump('critical');
  451:     my $result = '';
  452:     foreach (sort keys %what) {
  453:         my %content=&unpackagemsg($what{$_});
  454:         next if ($content{'senderdomain'} eq '');
  455:         $content{'message'}=~s/\n/\<br\>/g;
  456:         $result.='<hr>From: <b>'.
  457: &Apache::loncommon::aboutmewrapper(
  458:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
  459: $content{'sendername'}.'@'.
  460:             $content{'senderdomain'}.') '.$content{'time'}.
  461:             '<br>Subject: '.$content{'subject'}.
  462:             '<br><blockquote>'.
  463:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
  464:             '</blockquote>'.
  465:             '<input type=submit name="rec_'.$_.'" value="Confirm Receipt">'.
  466:             '<input type=submit name="reprec_'.$_.'" '.
  467:                   'value="Confirm Receipt and Reply">';
  468:     }
  469:     # Check to see if there were any messages.
  470:     if ($result eq '') {
  471:         $result = "<h2>You have no critical messages.</h2>";
  472:     } else {
  473:         $r->print($header);
  474:     }
  475:     $r->print($result);
  476:     $r->print('<input type=hidden name="displayedcrit" value="true"></form>');
  477: }
  478: 
  479: # =============================================================== Compose reply
  480: 
  481: sub comprep {
  482:     my ($r,$msgid)=@_;
  483:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  484:       my %content=&unpackagemsg($message{$msgid},1);
  485:       my $quotemsg='> '.$content{'message'};
  486:       $quotemsg=~s/\r/\n/g;
  487:       $quotemsg=~s/\f/\n/g;
  488:       $quotemsg=~s/\n+/\n\> /g;
  489:       my $subject='Re: '.$content{'subject'};
  490:       my $dispcrit='';
  491:       if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  492: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
  493:          $dispcrit=
  494:  '<input type=checkbox name=critmsg> Send as critical message ' . $crithelp . 
  495:  '<br>'.
  496:  '<input type=checkbox name=sendbck> Send as critical message ' .
  497:  ' and return receipt' . $crithelp . '<p>';
  498:       }
  499:       $r->print(<<"ENDREPLY");
  500: <form action="/adm/email" method=post>
  501: <input type=hidden name=sendreply value="$msgid">
  502: Subject: <input type=text size=50 name=subject value="$subject"><p>
  503: <textarea name=message cols=84 rows=10 wrap=hard>
  504: $quotemsg
  505: </textarea><p>
  506: $dispcrit
  507: <input type=submit value="Send Reply">
  508: </form>
  509: ENDREPLY
  510: }
  511: 
  512: # ======================================================== Display all messages
  513: 
  514: sub disall {
  515:     my $r=shift;
  516:      $r->print(<<ENDDISHEADER);
  517: <script>
  518:     function checkall() {
  519: 	for (i=0; i<document.forms.disall.elements.length; i++) {
  520:             if 
  521:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
  522: 	      document.forms.disall.elements[i].checked=true;
  523:             }
  524:         }
  525:     }
  526: 
  527:     function uncheckall() {
  528: 	for (i=0; i<document.forms.disall.elements.length; i++) {
  529:             if 
  530:           (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
  531: 	      document.forms.disall.elements[i].checked=false;
  532:             }
  533:         }
  534:     }
  535: </script>
  536: ENDDISHEADER
  537:    $r->print(
  538:  '<h1>Display All Messages</h1><form method=post name=disall '.
  539:  'action="/adm/email">'.
  540:      '<table border=2><tr><th colspan=2>&nbsp</th><th>Date</th>'.
  541:      '<th>Username</th><th>Domain</th><th>Subject</th><th>Status</th></tr>');
  542:     foreach (sort split(/\&/,&Apache::lonnet::reply('keys:'.
  543: 					$ENV{'user.domain'}.':'.
  544:                                         $ENV{'user.name'}.':nohist_email',
  545:                                         $ENV{'user.home'}))) {
  546:         my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
  547: 	    &Apache::lonmsg::unpackmsgid($_);
  548: 	if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
  549: 	    if ($status eq 'new') {
  550: 		$r->print('<tr bgcolor="#FFBB77">');
  551: 	    } elsif ($status eq 'read') {
  552: 		$r->print('<tr bgcolor="#BBBB77">');
  553: 	    } elsif ($status eq 'replied') {
  554: 		$r->print('<tr bgcolor="#AAAA88">');
  555: 	    } else {
  556: 		$r->print('<tr bgcolor="#99BBBB">');
  557: 	    }
  558: 	    $r->print('<td><a href="/adm/email?display='.$_.
  559: 		      '">Open</a></td><td><a href="/adm/email?markdel='.$_.
  560: 		      '">Delete</a><input type=checkbox name="delmark_'.$_.'"></td>'.
  561: 		      '<td>'.localtime($sendtime).'</td><td>'.
  562: 		      $fromname.'</td><td>'.$fromdomain.'</td><td>'.
  563: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
  564:                       $status.'</td></tr>');
  565: 	}
  566:     }
  567:     $r->print('</table><p>'.
  568:               '<a href="javascript:checkall()">Check All</a>&nbsp;'.
  569:               '<a href="javascript:uncheckall()">Uncheck All</a><p>'.
  570:               '<input type=submit name="markeddel" value="Delete Checked">'.
  571:               '</form></body></html>');
  572: }
  573: 
  574: # ============================================================== Compose output
  575: 
  576: sub compout {
  577:     my ($r,$forwarding,$broadcast)=@_;
  578:       my $dispcrit='';
  579:     my $dissub='';
  580:     my $dismsg='';
  581:     my $func='Send New';
  582:       if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  583: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
  584:          $dispcrit=
  585:  '<input type=checkbox name=critmsg> Send as critical message ' . $crithelp . 
  586:  '<br>'.
  587:  '<input type=checkbox name=sendbck> Send as critical message ' .
  588:  ' and return receipt' . $crithelp . '<p>';
  589:       }
  590:     if ($forwarding) {
  591:        $dispcrit.='<input type=hidden name=forwid value="'.
  592: 	   $forwarding.'">';
  593:        $func='Forward';
  594:       my %message=&Apache::lonnet::get('nohist_email',[$forwarding]);
  595:       my %content=&unpackagemsg($message{$forwarding});
  596: 
  597:        $dissub='Forwarding: '.$content{'subject'};
  598:        $dismsg='Forwarded message from '.
  599: 	   $content{'sendername'}.' at '.$content{'senderdomain'};
  600:     }
  601:     my $defdom=$ENV{'user.domain'};
  602:     if ($ENV{'form.recdom'}) { $defdom=$ENV{'form.recdom'}; }
  603:       $r->print(
  604:                 '<form action="/adm/email"  name="compemail" method="post"'.
  605:                 ' enctype="multipart/form-data">'."\n".
  606:                 '<input type="hidden" name="sendmail" value="on">'."\n".
  607:                 '<table>');
  608:     unless (($broadcast eq 'group') || ($broadcast eq 'upload')) {
  609:         my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
  610:         my $selectlink=&Apache::loncommon::selectstudent_link
  611: 	    ('compemail','recuname','recdomain');
  612:        $r->print(<<"ENDREC");
  613: <table>
  614: <tr><td>Username:</td><td><input type=text size=12 name=recuname value="$ENV{'form.recname'}"></td><td rowspan="2">$selectlink</td></tr>
  615: <tr><td>Domain:</td>
  616: <td>$domform</td></tr>
  617: ENDREC
  618:     }
  619:     if ($broadcast ne 'upload') {
  620:        $r->print(<<"ENDCOMP");
  621: <tr><td>Additional Recipients<br><tt>username\@domain,username\@domain, ...
  622: </tt></td><td>
  623: <input type=text size=50 name=additionalrec></td></tr>
  624: <tr><td>Subject:</td><td><input type=text size=50 name=subject value="$dissub">
  625: </td></tr></table>
  626: <textarea name=message cols=80 rows=10 wrap=hard>$dismsg
  627: </textarea><p>
  628: $dispcrit
  629: <input type=submit value="$func Mail">
  630: ENDCOMP
  631:     } else { # $broadcast is 'upload'
  632: 	$r->print(<<ENDUPLOAD);
  633: <input type=hidden name=sendmode value=upload>
  634: <h3>Generate messages from a file</h3>
  635: <p>
  636: Subject: <input type=text size=50 name=subject>
  637: </p>
  638: <p>General message text<br />
  639: <textarea name=message cols=60 rows=10 wrap=hard>$dismsg
  640: </textarea></p>
  641: <p>
  642: The file format for the uploaded portion of the message is:
  643: <pre>
  644: username1\@domain1: text
  645: username2\@domain2: text
  646: username3\@domain1: text
  647: </pre>
  648: </p>
  649: <p>
  650: The messages will be assembled from all lines with the respective 
  651: <tt>username\@domain</tt>, and appended to the general message text.</p>
  652: <p>
  653: <input type=file name=upfile size=20><p>
  654: $dispcrit
  655: <input type=submit value="Upload and send">
  656: ENDUPLOAD
  657:     }
  658:     if ($broadcast eq 'group') {
  659:        &discourse;
  660:     }
  661:     $r->print('</form>');
  662: }
  663: 
  664: # ---------------------------------------------------- Display all face to face
  665: 
  666: sub disfacetoface {
  667:     my ($r,$user,$domain)=@_;
  668:     unless ($ENV{'request.course.id'}) { return; }
  669:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  670: 	return;
  671:     }
  672:     my %records=&Apache::lonnet::dump('nohist_email',
  673: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  674: 			 $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  675:                          '%255b'.$user.'%253a'.$domain.'%255d');
  676:     my $result='';
  677:     foreach (sort keys %records) {
  678:         my %content=&unpackagemsg($records{$_});
  679:         next if ($content{'senderdomain'} eq '');
  680:         $content{'message'}=~s/\n/\<br\>/g;
  681:         if ($content{'subject'}=~/^Record/) {
  682: 	    $result.='<h3>Record</h3>';
  683:         } else {
  684:             $result.='<h3>Sent Message</h3>';
  685:             %content=&unpackagemsg($content{'message'});
  686:             $content{'message'}=
  687:                 '<b>Subject: '.$content{'subject'}.'</b><br />'.
  688: 		$content{'message'};
  689:         }
  690:         $result.='By: <b>'.
  691: &Apache::loncommon::aboutmewrapper(
  692:  &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
  693: $content{'sendername'}.'@'.
  694:             $content{'senderdomain'}.') '.$content{'time'}.
  695:             '<br><blockquote>'.
  696:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
  697: 	      '</blockquote>';
  698:      }
  699:     # Check to see if there were any messages.
  700:     if ($result eq '') {
  701:         $r->print("<p><b>No notes, face-to-face discussion records, or critical messages in this course.</b></p>");
  702:     } else {
  703:        $r->print($result);
  704:     }
  705: }
  706: 
  707: # ---------------------------------------------------------------- Face to face
  708: 
  709: sub facetoface {
  710:     my ($r,$stage)=@_;
  711:     unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  712: 	return;
  713:     }
  714: # from query string
  715:     if ($ENV{'form.recname'}) { $ENV{'form.recuname'}=$ENV{'form.recname'}; }
  716:     if ($ENV{'form.recdom'}) { $ENV{'form.recdomain'}=$ENV{'form.recdom'}; }
  717: 
  718:     my $defdom=$ENV{'user.domain'};
  719: # already filled in
  720:     if ($ENV{'form.recdomain'}) { $defdom=$ENV{'form.recdomain'}; }
  721: # generate output
  722:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
  723:     my $stdbrws = &Apache::loncommon::selectstudent_link
  724: 	('stdselect','recuname','recdomain');
  725:     $r->print(<<"ENDTREC");
  726: <h3>User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course</h3>
  727: <form method="post" action="/adm/email" name="stdselect">
  728: <input type="hidden" name="recordftf" value="retrieve" />
  729: <table>
  730: <tr><td>Username:</td><td><input type=text size=12 name=recuname value="$ENV{'form.recuname'}"></td>
  731: <td rowspan="2">
  732: $stdbrws
  733: <input type="submit" value="Retrieve discussion and message records"></td>
  734: </tr>
  735: <tr><td>Domain:</td>
  736: <td>$domform</td></tr>
  737: </table>
  738: </form>
  739: ENDTREC
  740:     if (($stage ne 'query') &&
  741:         ($ENV{'form.recdomain'}) && ($ENV{'form.recuname'})) {
  742:         chomp($ENV{'form.newrecord'});
  743:         if ($ENV{'form.newrecord'}) {
  744:            &user_normal_msg_raw(
  745:             $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
  746:             $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  747:             'Record ['.$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}.']',
  748: 	    $ENV{'form.newrecord'});
  749:         }
  750:         $r->print('<h3>'.&Apache::loncommon::plainname($ENV{'form.recuname'},
  751: 				     $ENV{'form.recdomain'}).'</h3>');
  752:         &disfacetoface($r,$ENV{'form.recuname'},$ENV{'form.recdomain'});
  753: 	$r->print(<<ENDRHEAD);
  754: <form method="post" action="/adm/email">
  755: <input name="recdomain" value="$ENV{'form.recdomain'}" type="hidden" />
  756: <input name="recuname" value="$ENV{'form.recuname'}" type="hidden" />
  757: ENDRHEAD
  758:         $r->print(<<ENDBFORM);
  759: <hr />New Record (record is visible to course faculty and staff)<br />
  760: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
  761: <br />
  762: <input type="hidden" name="recordftf" value="post" />
  763: <input type="submit" value="Post this record" />
  764: </form>
  765: ENDBFORM
  766:     }
  767: }
  768: 
  769: # ===================================================================== Handler
  770: 
  771: sub handler {
  772:     my $r=shift;
  773: 
  774: # ----------------------------------------------------------- Set document type
  775: 
  776:   $r->content_type('text/html');
  777:   $r->send_http_header;
  778: 
  779:   return OK if $r->header_only;
  780: 
  781: # --------------------------- Get query string for limited number of parameters
  782:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  783:         ['display','replyto','forward','markread','markdel','markunread',
  784:          'sendreply','compose','sendmail','critical','recname','recdom',
  785:          'recordftf']);
  786: 
  787: # ------------------------------------------------------ They checked for email
  788:   &Apache::lonnet::put('email_status',{'recnewemail'=>0});
  789: # --------------------------------------------------------------- Render Output
  790:   if (!$ENV{'form.display'}) {
  791:       $r->print('<html><head><title>EMail and Messaging</title>'.
  792: 		&Apache::loncommon::studentbrowser_javascript().'</head>'.
  793: 		&Apache::loncommon::bodytag('EMail and Messages'));
  794:   }
  795:   if ($ENV{'form.display'}) {
  796:       my $msgid=$ENV{'form.display'};
  797:       &statuschange($msgid,'read');
  798:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  799:       my %content=&unpackagemsg($message{$msgid});
  800:       $r->print('<html><head><title>EMail and Messaging</title>');
  801:       if (defined($content{'baseurl'})) {
  802: 	  $r->print("<base href=\"http://$ENV{'SERVER_NAME'}/$content{'baseurl'}\" />");
  803:       }
  804:       $r->print(&Apache::loncommon::studentbrowser_javascript().
  805: 		'</head>'.
  806: 		&Apache::loncommon::bodytag('EMail and Messages'));
  807:       $r->print('<b>Subject:</b> '.$content{'subject'}.
  808:              '<br><b>From:</b> '.
  809: &Apache::loncommon::aboutmewrapper(
  810: &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
  811: $content{'sendername'},$content{'senderdomain'}).' ('.
  812:                                  $content{'sendername'}.' at '.
  813:                                  $content{'senderdomain'}.') '.
  814:              '<br><b>Time:</b> '.$content{'time'}.'<p>'.
  815:              '<table border=2><tr bgcolor="#FFFFAA"><td>Functions:</td>'.
  816:            '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).
  817:              '"><b>Reply</b></a></td>'.
  818:            '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).
  819:              '"><b>Forward</b></a></td>'.
  820:         '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).
  821:              '"><b>Mark Unread</b></a></td>'.
  822:         '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).
  823:              '"><b>Delete</b></a></td>'.
  824:         '<td><a href="/adm/email"><b>Display all Messages</b></a></td>'.
  825:              '</tr></table><p><pre>'.
  826:              &Apache::lontexconvert::msgtexconverted($content{'message'}).
  827:              '</pre><hr>'.$content{'citation'});
  828:   } elsif ($ENV{'form.replyto'}) {
  829:       &comprep($r,$ENV{'form.replyto'});
  830:   } elsif ($ENV{'form.sendreply'}) {
  831:       my $msgid=$ENV{'form.sendreply'};
  832:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  833:       my %content=&unpackagemsg($message{$msgid},1);
  834:       &statuschange($msgid,'replied');
  835:       if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
  836:           (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
  837:          $r->print('Sending critical: '.
  838:                 &user_crit_msg($content{'sendername'},
  839:                                  $content{'senderdomain'},
  840:                                  $ENV{'form.subject'},
  841:                                  $ENV{'form.message'},
  842:                                  $ENV{'form.sendbck'}));
  843:       } else {
  844:          $r->print('Sending: '.&user_normal_msg($content{'sendername'},
  845:                                  $content{'senderdomain'},
  846:                                  $ENV{'form.subject'},
  847:                                  $ENV{'form.message'}));
  848:       }
  849:       if ($ENV{'form.displayedcrit'}) {
  850:           &discrit($r);
  851:       } else {
  852: 	  &disall($r);
  853:       }
  854:   } elsif ($ENV{'form.confirm'}) {
  855:       foreach (keys %ENV) {
  856:           if ($_=~/^form\.rec\_(.*)$/) {
  857: 	      $r->print('<b>Confirming Receipt:</b> '.
  858:                         &user_crit_received($1).'<br>');
  859:           }
  860:           if ($_=~/^form\.reprec\_(.*)$/) {
  861:               my $msgid=$1;
  862: 	      $r->print('<b>Confirming Receipt:</b> '.
  863:                         &user_crit_received($msgid).'<br>');
  864:               &comprep($r,$msgid);
  865:           }
  866:       }
  867:       &discrit($r);
  868:   } elsif ($ENV{'form.critical'}) {
  869:       &discrit($r);
  870:   } elsif ($ENV{'form.forward'}) {
  871:       &compout($r,$ENV{'form.forward'});
  872:   } elsif ($ENV{'form.markread'}) {
  873:   } elsif ($ENV{'form.markdel'}) {
  874:       &statuschange($ENV{'form.markdel'},'deleted');
  875:       &disall($r);
  876:   } elsif ($ENV{'form.markeddel'}) {
  877:       my $total=0;
  878:       foreach (keys %ENV) {
  879:           if ($_=~/^form\.delmark_(.*)$/) {
  880: 	      &statuschange(&Apache::lonnet::unescape($1),'deleted');
  881:               $total++;
  882:           }
  883:       }
  884:       $r->print('Deleted '.$total.' message(s)<p>');
  885:       &disall($r);
  886:   } elsif ($ENV{'form.markunread'}) {
  887:       &statuschange($ENV{'form.markunread'},'new');
  888:       &disall($r);
  889:   } elsif ($ENV{'form.compose'}) {
  890:       &compout($r,'',$ENV{'form.compose'});
  891:   } elsif ($ENV{'form.recordftf'}) {
  892:       &facetoface($r,$ENV{'form.recordftf'});
  893:   } elsif ($ENV{'form.sendmail'}) {
  894:       my %content=();
  895:       undef %content;
  896:       if ($ENV{'form.forwid'}) {
  897:         my $msgid=$ENV{'form.forwid'};
  898:         my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  899:         %content=&unpackagemsg($message{$msgid},1);
  900:         &statuschange($msgid,'forwarded');
  901:         $ENV{'form.message'}.="\n\n-- Forwarded message --\n\n".
  902: 	                       $content{'message'};
  903:       }
  904:       my %toaddr=();
  905:       undef %toaddr;
  906:       if ($ENV{'form.sendmode'} eq 'group') {
  907:           foreach (keys %ENV) {
  908: 	      if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
  909: 		  $toaddr{$1}='';
  910:               }
  911:           }
  912:       } elsif ($ENV{'form.sendmode'} eq 'upload') {
  913:           foreach (split(/[\n\r\f]+/,$ENV{'form.upfile'})) {
  914:               my ($rec,$txt)=split(/\s*\:\s*/,$_);
  915:               if ($txt) {
  916: 		  $rec=~s/\@/\:/;
  917:                   $toaddr{$rec}.=$txt."\n";
  918:               }
  919:           }
  920:       } else {
  921: 	  $toaddr{$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}}='';
  922:       }
  923:       if ($ENV{'form.additionalrec'}) {
  924: 	  foreach (split(/\,/,$ENV{'form.additionalrec'})) {
  925:               my ($auname,$audom)=split(/\@/,$_);
  926:               $toaddr{$auname.':'.$audom}='';
  927:           }
  928:       }
  929:     foreach (keys %toaddr) {
  930:       my ($recuname,$recdomain)=split(/\:/,$_);
  931:       my $msgtxt=$ENV{'form.message'};
  932:       if ($toaddr{$_}) { $msgtxt.='<hr>'.$toaddr{$_}; }    
  933:       if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
  934:           (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
  935:          $r->print('Sending critical: '.
  936:                 &user_crit_msg($recuname,$recdomain,
  937:                                  $ENV{'form.subject'},
  938:                                  $msgtxt,
  939:                                  $ENV{'form.sendbck'}));
  940:       } else {
  941:          $r->print('Sending: '.&user_normal_msg($recuname,$recdomain,
  942:                                  $ENV{'form.subject'},
  943:                                  $msgtxt,
  944:                                  $content{'citation'}));
  945:       }
  946:       $r->print('<br>');
  947:     }
  948:       if ($ENV{'form.displayedcrit'}) {
  949:           &discrit($r);
  950:       } else {
  951: 	  &disall($r);
  952:       }
  953:   } else {
  954:       &disall($r);
  955:   }
  956:   $r->print('</body></html>');
  957:   return OK;
  958: 
  959: }
  960: # ================================================= Main program, reset counter
  961: 
  962: BEGIN {
  963:     $msgcount=0;
  964: }
  965: 
  966: 1;
  967: __END__
  968: 
  969: 
  970: 
  971: 
  972: 
  973: 
  974: 

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