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

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

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