File:  [LON-CAPA] / loncom / interface / lonmsg.pm
Revision 1.51: download - view: text, annotated - select for diffs
Sun Mar 30 21:58:17 2003 UTC (21 years, 3 months ago) by www
Branches: MAIN
CVS tags: HEAD
Attachments in discussions work, but not yet in other messages.

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

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