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

1.1       www         1: # The LearningOnline Network with CAPA
1.26      albertel    2: # Routines for messaging
                      3: #
1.51    ! www         4: # $Id: lonmsg.pm,v 1.50 2003/03/17 17:02:38 albertel 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.51    ! www        59:     my ($subject,$message,$citation,$baseurl,$attachmenturl)=@_;
1.47      albertel   60:     $message =&HTML::Entities::encode($message);
                     61:     $citation=&HTML::Entities::encode($citation);
                     62:     $subject =&HTML::Entities::encode($subject);
1.49      albertel   63:     #remove machine specification
                     64:     $baseurl =~ s|^http://[^/]+/|/|;
                     65:     $baseurl =&HTML::Entities::encode($baseurl);
1.51    ! www        66:     #remove machine specification
        !            67:     $attachmenturl =~ s|^http://[^/]+/|/|;
        !            68:     $attachmenturl =&HTML::Entities::encode($baseurl);
        !            69: 
1.2       www        70:     my $now=time;
                     71:     $msgcount++;
1.6       www        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.':'.$$);
1.49      albertel   77:     my $result='<sendername>'.$ENV{'user.name'}.'</sendername>'.
1.1       www        78:            '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
                     79:            '<subject>'.$subject.'</subject>'.
1.2       www        80: 	   '<time>'.localtime($now).'</time>'.
1.1       www        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>'.
1.2       www        92:            '<msgid>'.$msgid.'</msgid>'.
1.49      albertel   93: 	   '<message>'.$message.'</message>';
                     94:     if (defined($citation)) {
                     95: 	$result.='<citation>'.$citation.'</citation>';
                     96:     }
                     97:     if (defined($baseurl)) {
                     98: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
                     99:     }
1.51    ! www       100:     if (defined($attachmenturl)) {
        !           101: 	$result.= '<attachmenturl>'.$baseurl.'</attachmenturl>';
        !           102:     }
1.49      albertel  103:     return $msgid,$result;
1.1       www       104: }
                    105: 
1.2       www       106: # ================================================== Unpack message into a hash
                    107: 
1.3       www       108: sub unpackagemsg {
1.2       www       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: 
1.6       www       123: # ======================================================= Get info out of msgid
                    124: 
                    125: sub unpackmsgid {
1.7       www       126:     my $msgid=&Apache::lonnet::unescape(shift);
1.6       www       127:     my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
1.7       www       128:                           &Apache::lonnet::unescape($msgid));
1.8       albertel  129:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
1.6       www       130:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
                    131:     unless ($status{$msgid}) { $status{$msgid}='new'; }
                    132:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
                    133: } 
                    134: 
1.40      www       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: 
1.1       www       146: # =============================== Automated message to the author of a resource
                    147: 
                    148: sub author_res_msg {
                    149:     my ($filename,$message)=@_;
1.2       www       150:     unless ($message) { return 'empty'; }
1.1       www       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);
1.2       www       156:        my $msgid;
1.3       www       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);
1.1       www       162:     }
1.2       www       163:     return 'no_host';
1.1       www       164: }
                    165: 
                    166: # ================================================== Critical message to a user
                    167: 
1.38      www       168: sub user_crit_msg_raw {
1.24      www       169:     my ($user,$domain,$subject,$message,$sendback)=@_;
1.2       www       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') {
1.3       www       176:        ($msgid,$message)=&packagemsg($subject,$message);
1.24      www       177:        if ($sendback) { $message.='<sendback>true</sendback>'; }
1.4       www       178:        $status=&Apache::lonnet::critical(
                    179:            'put:'.$domain.':'.$user.':critical:'.
                    180:            &Apache::lonnet::escape($msgid).'='.
                    181:            &Apache::lonnet::escape($message),$homeserver);
1.45      www       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:        }
1.2       www       189:     } else {
                    190:        $status='no_host';
                    191:     }
                    192:     &Apache::lonnet::logthis(
1.4       www       193:       'Sending critical email '.$msgid.
1.2       www       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: '
1.4       www       198:       .$status));
1.2       www       199:     return $status;
                    200: }
                    201: 
1.38      www       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: 
1.2       www       223: # =================================================== Critical message received
                    224: 
                    225: sub user_crit_received {
1.12      www       226:     my $msgid=shift;
                    227:     my %message=&Apache::lonnet::get('critical',[$msgid]);
                    228:     my %contents=&unpackagemsg($message{$msgid});
1.24      www       229:     my $status='rec: '.($contents{'sendback'}?
1.5       www       230:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
1.4       www       231:                      'Receipt: '.$ENV{'user.name'}.' at '.$ENV{'user.domain'},
                    232:                      'User '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.42      www       233:                      ' acknowledged receipt of message'."\n".'   "'.
                    234:                      $contents{'subject'}.'"'."\n".'dated '.
                    235:                      $contents{'time'}.".\n"
                    236:                      ):'no msg req');
1.5       www       237:     $status.=' trans: '.
1.12      www       238:      &Apache::lonnet::put(
                    239:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
1.5       www       240:     $status.=' del: '.
1.9       albertel  241:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.5       www       242:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    243:                          $ENV{'user.home'},'Received critical message '.
                    244:                          $contents{'msgid'}.
                    245:                          ', '.$status);
1.12      www       246:     return $status;
1.2       www       247: }
                    248: 
                    249: # ======================================================== Normal communication
                    250: 
1.38      www       251: sub user_normal_msg_raw {
1.51    ! www       252:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
1.2       www       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') {
1.51    ! www       259:        ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl,
        !           260:                                      $attachmenturl);
1.4       www       261:        $status=&Apache::lonnet::critical(
                    262:            'put:'.$domain.':'.$user.':nohist_email:'.
                    263:            &Apache::lonnet::escape($msgid).'='.
                    264:            &Apache::lonnet::escape($message),$homeserver);
1.40      www       265:        &Apache::lonnet::put
                    266:                          ('email_status',{'recnewemail'=>time},$domain,$user);
1.2       www       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: }
1.38      www       275: 
                    276: # New routine that respects "forward" and calls old routine
                    277: 
                    278: sub user_normal_msg {
1.49      albertel  279:     my ($user,$domain,$subject,$message,$citation,$baseurl)=@_;
1.38      www       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,
1.49      albertel  289: 			       $citation,$baseurl).' ';
1.38      www       290:        }
                    291:     } else { 
1.49      albertel  292: 	$status=&user_normal_msg_raw($user,$domain,$subject,$message,
                    293: 				     $citation,$baseurl);
1.38      www       294:     }
                    295:     return $status;
                    296: }
                    297: 
1.2       www       298: 
1.7       www       299: # =============================================================== Status Change
                    300: 
                    301: sub statuschange {
                    302:     my ($msgid,$newstatus)=@_;
1.8       albertel  303:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
1.7       www       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')) {
1.10      albertel  308: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
1.7       www       309:     }
1.14      www       310:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
                    311: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
                    312:     }
1.7       www       313: }
1.14      www       314: 
1.17      www       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: 
1.19      www       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: 
1.17      www       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>
1.19      www       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;
1.17      www       358: <input type=button onClick="uncheckall()" value="Check for None">
                    359: <p>
                    360: ENDDISHEADER
1.28      harris41  361:     foreach (sort keys %courselist) {
1.17      www       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);
1.19      www       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.'&&&_'.$_.'"> '.
1.17      www       374: 		      $reply{'firstname'}.' '. 
                    375:                       $reply{'middlename'}.' '.
                    376:                       $reply{'lastname'}.' '.
                    377:                       $reply{'generation'}.
1.19      www       378:                       ' ('.$_.') '.$section);
1.17      www       379:         } 
1.28      harris41  380:     }
1.17      www       381: }
                    382: 
1.13      www       383: # ==================================================== Display Critical Message
1.5       www       384: 
1.12      www       385: sub discrit {
                    386:     my $r=shift;
1.30      matthew   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;
1.37      www       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'}.
1.36      www       402:             '<br><blockquote>'.
                    403:               &Apache::lontexconvert::msgtexconverted($content{'message'}).
                    404:             '</blockquote>'.
1.30      matthew   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>');
1.12      www       417: }
                    418: 
1.13      www       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'})) {
1.35      bowersj2  432: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
1.13      www       433:          $dispcrit=
1.35      bowersj2  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>';
1.13      www       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>
1.37      www       443: <textarea name=message cols=84 rows=10 wrap=hard>
1.13      www       444: $quotemsg
                    445: </textarea><p>
                    446: $dispcrit
                    447: <input type=submit value="Send Reply">
                    448: </form>
                    449: ENDREPLY
                    450: }
                    451: 
1.15      www       452: # ======================================================== Display all messages
                    453: 
1.14      www       454: sub disall {
                    455:     my $r=shift;
1.29      www       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">'.
1.14      www       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>');
1.27      www       482:     foreach (sort split(/\&/,&Apache::lonnet::reply('keys:'.
                    483: 					$ENV{'user.domain'}.':'.
                    484:                                         $ENV{'user.name'}.':nohist_email',
                    485:                                         $ENV{'user.home'}))) {
1.14      www       486:         my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
                    487: 	    &Apache::lonmsg::unpackmsgid($_);
1.39      albertel  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>'.
1.14      www       503: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
                    504:                       $status.'</td></tr>');
1.39      albertel  505: 	}
1.27      www       506:     }
1.25      www       507:     $r->print('</table><p>'.
1.29      www       508:               '<a href="javascript:checkall()">Check All</a>&nbsp;'.
                    509:               '<a href="javascript:uncheckall()">Uncheck All</a><p>'.
1.25      www       510:               '<input type=submit name="markeddel" value="Delete Checked">'.
                    511:               '</form></body></html>');
1.14      www       512: }
                    513: 
1.15      www       514: # ============================================================== Compose output
                    515: 
                    516: sub compout {
1.17      www       517:     my ($r,$forwarding,$broadcast)=@_;
1.15      www       518:       my $dispcrit='';
                    519:     my $dissub='';
                    520:     my $dismsg='';
                    521:     my $func='Send New';
                    522:       if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
1.35      bowersj2  523: 	 my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
1.15      www       524:          $dispcrit=
1.35      bowersj2  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>';
1.15      www       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'};
1.37      www       542:     if ($ENV{'form.recdom'}) { $defdom=$ENV{'form.recdom'}; }
1.22      www       543:       $r->print(
1.31      matthew   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>');
1.22      www       548:     unless (($broadcast eq 'group') || ($broadcast eq 'upload')) {
1.31      matthew   549:         my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
1.46      www       550:         my $selectlink=&Apache::loncommon::selectstudent_link
                    551: 	    ('compemail','recuname','recdomain');
1.17      www       552:        $r->print(<<"ENDREC");
1.15      www       553: <table>
1.46      www       554: <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       555: <tr><td>Domain:</td>
1.31      matthew   556: <td>$domform</td></tr>
1.17      www       557: ENDREC
                    558:     }
1.31      matthew   559:     if ($broadcast ne 'upload') {
1.22      www       560:        $r->print(<<"ENDCOMP");
1.20      www       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>
1.15      www       564: <tr><td>Subject:</td><td><input type=text size=50 name=subject value="$dissub">
                    565: </td></tr></table>
1.37      www       566: <textarea name=message cols=80 rows=10 wrap=hard>$dismsg
1.15      www       567: </textarea><p>
                    568: $dispcrit
                    569: <input type=submit value="$func Mail">
                    570: ENDCOMP
1.31      matthew   571:     } else { # $broadcast is 'upload'
1.22      www       572: 	$r->print(<<ENDUPLOAD);
                    573: <input type=hidden name=sendmode value=upload>
                    574: <h3>Generate messages from a file</h3>
1.31      matthew   575: <p>
1.22      www       576: Subject: <input type=text size=50 name=subject>
1.31      matthew   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:
1.22      www       583: <pre>
                    584: username1\@domain1: text
                    585: username2\@domain2: text
1.31      matthew   586: username3\@domain1: text
1.22      www       587: </pre>
1.31      matthew   588: </p>
                    589: <p>
1.22      www       590: The messages will be assembled from all lines with the respective 
1.31      matthew   591: <tt>username\@domain</tt>, and appended to the general message text.</p>
                    592: <p>
1.22      www       593: <input type=file name=upfile size=20><p>
                    594: $dispcrit
                    595: <input type=submit value="Upload and send">
                    596: ENDUPLOAD
                    597:     }
1.17      www       598:     if ($broadcast eq 'group') {
                    599:        &discourse;
                    600:     }
                    601:     $r->print('</form>');
1.15      www       602: }
                    603: 
1.45      www       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 '') {
1.46      www       641:         $r->print("<p><b>No notes, face-to-face discussion records, or critical messages in this course.</b></p>");
1.45      www       642:     } else {
                    643:        $r->print($result);
                    644:     }
                    645: }
                    646: 
1.44      www       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:     }
1.46      www       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: 
1.44      www       658:     my $defdom=$ENV{'user.domain'};
1.46      www       659: # already filled in
1.44      www       660:     if ($ENV{'form.recdomain'}) { $defdom=$ENV{'form.recdomain'}; }
1.46      www       661: # generate output
1.44      www       662:     my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
1.46      www       663:     my $stdbrws = &Apache::loncommon::selectstudent_link
                    664: 	('stdselect','recuname','recdomain');
1.44      www       665:     $r->print(<<"ENDTREC");
1.46      www       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">
1.44      www       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">
1.46      www       672: $stdbrws
1.44      www       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'}) {
1.45      www       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'});
1.44      www       689:         }
1.46      www       690:         $r->print('<h3>'.&Apache::loncommon::plainname($ENV{'form.recuname'},
                    691: 				     $ENV{'form.recdomain'}).'</h3>');
1.45      www       692:         &disfacetoface($r,$ENV{'form.recuname'},$ENV{'form.recdomain'});
1.44      www       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>
1.45      www       701: <br />
                    702: <input type="hidden" name="recordftf" value="post" />
                    703: <input type="submit" value="Post this record" />
1.44      www       704: </form>
                    705: ENDBFORM
                    706:     }
                    707: }
                    708: 
1.13      www       709: # ===================================================================== Handler
                    710: 
1.5       www       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: 
1.6       www       721: # --------------------------- Get query string for limited number of parameters
1.32      matthew   722:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    723:         ['display','replyto','forward','markread','markdel','markunread',
1.44      www       724:          'sendreply','compose','sendmail','critical','recname','recdom',
                    725:          'recordftf']);
1.6       www       726: 
1.40      www       727: # ------------------------------------------------------ They checked for email
                    728:   &Apache::lonnet::put('email_status',{'recnewemail'=>0});
1.5       www       729: # --------------------------------------------------------------- Render Output
1.49      albertel  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:   }
1.6       www       735:   if ($ENV{'form.display'}) {
1.7       www       736:       my $msgid=$ENV{'form.display'};
                    737:       &statuschange($msgid,'read');
1.8       albertel  738:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
1.7       www       739:       my %content=&unpackagemsg($message{$msgid});
1.49      albertel  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'));
1.7       www       747:       $r->print('<b>Subject:</b> '.$content{'subject'}.
1.37      www       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'}.') '.
1.14      www       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>'.
1.15      www       758:            '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).
1.14      www       759:              '"><b>Forward</b></a></td>'.
1.15      www       760:         '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).
                    761:              '"><b>Mark Unread</b></a></td>'.
1.43      www       762:         '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).
                    763:              '"><b>Delete</b></a></td>'.
1.15      www       764:         '<td><a href="/adm/email"><b>Display all Messages</b></a></td>'.
1.14      www       765:              '</tr></table><p><pre>'.
1.36      www       766:              &Apache::lontexconvert::msgtexconverted($content{'message'}).
                    767:              '</pre><hr>'.$content{'citation'});
1.6       www       768:   } elsif ($ENV{'form.replyto'}) {
1.13      www       769:       &comprep($r,$ENV{'form.replyto'});
1.7       www       770:   } elsif ($ENV{'form.sendreply'}) {
                    771:       my $msgid=$ENV{'form.sendreply'};
1.8       albertel  772:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
1.7       www       773:       my %content=&unpackagemsg($message{$msgid});
                    774:       &statuschange($msgid,'replied');
1.24      www       775:       if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
1.12      www       776:           (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
                    777:          $r->print('Sending critical: '.
                    778:                 &user_crit_msg($content{'sendername'},
1.7       www       779:                                  $content{'senderdomain'},
                    780:                                  $ENV{'form.subject'},
1.24      www       781:                                  $ENV{'form.message'},
                    782:                                  $ENV{'form.sendbck'}));
1.12      www       783:       } else {
                    784:          $r->print('Sending: '.&user_normal_msg($content{'sendername'},
                    785:                                  $content{'senderdomain'},
                    786:                                  $ENV{'form.subject'},
                    787:                                  $ENV{'form.message'}));
                    788:       }
1.14      www       789:       if ($ENV{'form.displayedcrit'}) {
                    790:           &discrit($r);
                    791:       } else {
                    792: 	  &disall($r);
                    793:       }
1.12      www       794:   } elsif ($ENV{'form.confirm'}) {
1.28      harris41  795:       foreach (keys %ENV) {
1.12      www       796:           if ($_=~/^form\.rec\_(.*)$/) {
                    797: 	      $r->print('<b>Confirming Receipt:</b> '.
                    798:                         &user_crit_received($1).'<br>');
1.13      www       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);
1.12      www       805:           }
1.28      harris41  806:       }
1.12      www       807:       &discrit($r);
                    808:   } elsif ($ENV{'form.critical'}) {
                    809:       &discrit($r);
1.6       www       810:   } elsif ($ENV{'form.forward'}) {
1.15      www       811:       &compout($r,$ENV{'form.forward'});
1.14      www       812:   } elsif ($ENV{'form.markread'}) {
                    813:   } elsif ($ENV{'form.markdel'}) {
                    814:       &statuschange($ENV{'form.markdel'},'deleted');
1.25      www       815:       &disall($r);
                    816:   } elsif ($ENV{'form.markeddel'}) {
                    817:       my $total=0;
1.28      harris41  818:       foreach (keys %ENV) {
1.25      www       819:           if ($_=~/^form\.delmark_(.*)$/) {
                    820: 	      &statuschange(&Apache::lonnet::unescape($1),'deleted');
                    821:               $total++;
                    822:           }
1.28      harris41  823:       }
1.25      www       824:       $r->print('Deleted '.$total.' message(s)<p>');
1.14      www       825:       &disall($r);
                    826:   } elsif ($ENV{'form.markunread'}) {
1.15      www       827:       &statuschange($ENV{'form.markunread'},'new');
                    828:       &disall($r);
1.11      www       829:   } elsif ($ENV{'form.compose'}) {
1.17      www       830:       &compout($r,'',$ENV{'form.compose'});
1.44      www       831:   } elsif ($ENV{'form.recordftf'}) {
                    832:       &facetoface($r,$ENV{'form.recordftf'});
1.11      www       833:   } elsif ($ENV{'form.sendmail'}) {
1.16      www       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:       }
1.18      www       844:       my %toaddr=();
                    845:       undef %toaddr;
                    846:       if ($ENV{'form.sendmode'} eq 'group') {
1.28      harris41  847:           foreach (keys %ENV) {
1.19      www       848: 	      if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
1.22      www       849: 		  $toaddr{$1}='';
1.18      www       850:               }
1.28      harris41  851:           }
1.22      www       852:       } elsif ($ENV{'form.sendmode'} eq 'upload') {
1.28      harris41  853:           foreach (split(/[\n\r\f]+/,$ENV{'form.upfile'})) {
1.22      www       854:               my ($rec,$txt)=split(/\s*\:\s*/,$_);
                    855:               if ($txt) {
                    856: 		  $rec=~s/\@/\:/;
                    857:                   $toaddr{$rec}.=$txt."\n";
                    858:               }
1.28      harris41  859:           }
1.18      www       860:       } else {
1.22      www       861: 	  $toaddr{$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}}='';
1.20      www       862:       }
                    863:       if ($ENV{'form.additionalrec'}) {
1.28      harris41  864: 	  foreach (split(/\,/,$ENV{'form.additionalrec'})) {
1.20      www       865:               my ($auname,$audom)=split(/\@/,$_);
1.22      www       866:               $toaddr{$auname.':'.$audom}='';
1.28      harris41  867:           }
1.18      www       868:       }
1.28      harris41  869:     foreach (keys %toaddr) {
1.18      www       870:       my ($recuname,$recdomain)=split(/\:/,$_);
1.22      www       871:       my $msgtxt=$ENV{'form.message'};
                    872:       if ($toaddr{$_}) { $msgtxt.='<hr>'.$toaddr{$_}; }    
1.24      www       873:       if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) && 
1.16      www       874:           (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
                    875:          $r->print('Sending critical: '.
1.18      www       876:                 &user_crit_msg($recuname,$recdomain,
1.16      www       877:                                  $ENV{'form.subject'},
1.22      www       878:                                  $msgtxt,
1.24      www       879:                                  $ENV{'form.sendbck'}));
1.16      www       880:       } else {
1.18      www       881:          $r->print('Sending: '.&user_normal_msg($recuname,$recdomain,
1.16      www       882:                                  $ENV{'form.subject'},
1.22      www       883:                                  $msgtxt,
1.16      www       884:                                  $content{'citation'}));
                    885:       }
1.18      www       886:       $r->print('<br>');
1.28      harris41  887:     }
1.16      www       888:       if ($ENV{'form.displayedcrit'}) {
                    889:           &discrit($r);
                    890:       } else {
                    891: 	  &disall($r);
                    892:       }
1.6       www       893:   } else {
1.14      www       894:       &disall($r);
1.6       www       895:   }
1.5       www       896:   $r->print('</body></html>');
                    897:   return OK;
                    898: 
                    899: }
1.2       www       900: # ================================================= Main program, reset counter
                    901: 
1.27      www       902: BEGIN {
1.2       www       903:     $msgcount=0;
1.1       www       904: }
                    905: 
                    906: 1;
                    907: __END__
                    908: 
                    909: 
                    910: 
                    911: 
                    912: 
                    913: 
                    914: 

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