File:  [LON-CAPA] / loncom / interface / lonmsg.pm
Revision 1.16: download - view: text, annotated - select for diffs
Mon Aug 6 11:41:11 2001 UTC (22 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
Can forward and send message to individual user

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Routines for messaging
    4: #
    5: # (Routines to control the menu
    6: #
    7: # (TeX Conversion Module
    8: #
    9: # 05/29/00,05/30 Gerd Kortemeyer)
   10: #
   11: # 10/05 Gerd Kortemeyer)
   12: #
   13: # 10/19,10/20,10/30,
   14: # 02/06/01 Gerd Kortemeyer
   15: # 07/27 Guy Albertelli
   16: # 07/27,07/28,07/30,08/03,08/06 Gerd Kortemeyer
   17: 
   18: package Apache::lonmsg;
   19: 
   20: use strict;
   21: use Apache::lonnet();
   22: use vars qw($msgcount);
   23: use HTML::TokeParser;
   24: use Apache::Constants qw(:common);
   25: 
   26: # ===================================================================== Package
   27: 
   28: sub packagemsg {
   29:     my ($subject,$message,$citation)=@_;
   30:     $message=~s/\</\&lt\;/g;
   31:     $message=~s/\>/\&gt\;/g;
   32:     $citation=~s/\</\&lt\;/g;
   33:     $citation=~s/\>/\&gt\;/g;
   34:     $subject=~s/\</\&lt\;/g;
   35:     $subject=~s/\>/\&gt\;/g;
   36:     my $now=time;
   37:     $msgcount++;
   38:     my $partsubj=$subject;
   39:     $partsubj=&Apache::lonnet::escape($partsubj);
   40:     $partsubj=substr($partsubj,0,50);
   41:     my $msgid=&Apache::lonnet::escape(
   42:            $now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
   43:            $ENV{'user.domain'}.':'.$msgcount.':'.$$);
   44:     return $msgid,
   45:            '<sendername>'.$ENV{'user.name'}.'</sendername>'.
   46:            '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
   47:            '<subject>'.$subject.'</subject>'.
   48: 	   '<time>'.localtime($now).'</time>'.
   49: 	   '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
   50:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
   51: 	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
   52: 	   '<browsertype>'.$ENV{'browser.type'}.'</browsertype>'.
   53: 	   '<browseros>'.$ENV{'browser.os'}.'</browseros>'.
   54: 	   '<browserversion>'.$ENV{'browser.version'}.'</browserversion>'.
   55:            '<browsermathml>'.$ENV{'browser.mathml'}.'</browsermathml>'.
   56: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
   57: 	   '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
   58: 	   '<role>'.$ENV{'request.role'}.'</role>'.
   59: 	   '<resource>'.$ENV{'request.filename'}.'</resource>'.
   60:            '<msgid>'.$msgid.'</msgid>'.
   61: 	   '<message>'.$message.'</message>'.
   62: 	   '<citation>'.$citation.'</citation>';
   63: }
   64: 
   65: # ================================================== Unpack message into a hash
   66: 
   67: sub unpackagemsg {
   68:     my $message=shift;
   69:     my %content=();
   70:     my $parser=HTML::TokeParser->new(\$message);
   71:     my $token;
   72:     while ($token=$parser->get_token) {
   73:        if ($token->[0] eq 'S') {
   74: 	   my $entry=$token->[1];
   75:            my $value=$parser->get_text('/'.$entry);
   76:            $content{$entry}=$value;
   77:        }
   78:     }
   79:     return %content;
   80: }
   81: 
   82: # ======================================================= Get info out of msgid
   83: 
   84: sub unpackmsgid {
   85:     my $msgid=&Apache::lonnet::unescape(shift);
   86:     my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
   87:                           &Apache::lonnet::unescape($msgid));
   88:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
   89:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
   90:     unless ($status{$msgid}) { $status{$msgid}='new'; }
   91:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
   92: } 
   93: 
   94: # =============================== Automated message to the author of a resource
   95: 
   96: sub author_res_msg {
   97:     my ($filename,$message)=@_;
   98:     unless ($message) { return 'empty'; }
   99:     $filename=&Apache::lonnet::declutter($filename);
  100:     my ($domain,$author,@dummy)=split(/\//,$filename);
  101:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
  102:     if ($homeserver ne 'no_host') {
  103:        my $id=unpack("%32C*",$message);
  104:        my $msgid;
  105:        ($msgid,$message)=&packagemsg($filename,$message);
  106:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
  107:          ':nohist_res_msgs:'.
  108:           &Apache::lonnet::escape($filename.'_'.$id).'='.
  109:           &Apache::lonnet::escape($message),$homeserver);
  110:     }
  111:     return 'no_host';
  112: }
  113: 
  114: # ================================================== Critical message to a user
  115: 
  116: sub user_crit_msg {
  117:     my ($user,$domain,$subject,$message)=@_;
  118: # Check if allowed missing
  119:     my $status='';
  120:     my $msgid='undefined';
  121:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  122:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  123:     if ($homeserver ne 'no_host') {
  124:        my $msgid;
  125:        ($msgid,$message)=&packagemsg($subject,$message);
  126:        $status=&Apache::lonnet::critical(
  127:            'put:'.$domain.':'.$user.':critical:'.
  128:            &Apache::lonnet::escape($msgid).'='.
  129:            &Apache::lonnet::escape($message),$homeserver);
  130:     } else {
  131:        $status='no_host';
  132:     }
  133:     &Apache::lonnet::logthis(
  134:       'Sending critical email '.$msgid.
  135:       ', log status: '.
  136:       &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  137:                          $ENV{'user.home'},
  138:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
  139:       .$status));
  140:     return $status;
  141: }
  142: 
  143: # =================================================== Critical message received
  144: 
  145: sub user_crit_received {
  146:     my $msgid=shift;
  147:     my %message=&Apache::lonnet::get('critical',[$msgid]);
  148:     my %contents=&unpackagemsg($message{$msgid});
  149:     my $status='rec: '.
  150:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
  151:                      'Receipt: '.$ENV{'user.name'}.' at '.$ENV{'user.domain'},
  152:                      'User '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  153:                      ' acknowledged receipt of message "'.
  154:                      $contents{'subject'}.'" dated '.$contents{'time'}.".\n\n"
  155:                      .'Message ID: '.$contents{'msgid'});
  156:     $status.=' trans: '.
  157:      &Apache::lonnet::put(
  158:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
  159:     $status.=' del: '.
  160:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
  161:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  162:                          $ENV{'user.home'},'Received critical message '.
  163:                          $contents{'msgid'}.
  164:                          ', '.$status);
  165:     return $status;
  166: }
  167: 
  168: # ======================================================== Normal communication
  169: 
  170: sub user_normal_msg {
  171:     my ($user,$domain,$subject,$message,$citation)=@_;
  172: # Check if allowed missing
  173:     my $status='';
  174:     my $msgid='undefined';
  175:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  176:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  177:     if ($homeserver ne 'no_host') {
  178:        my $msgid;
  179:        ($msgid,$message)=&packagemsg($subject,$message,$citation);
  180:        $status=&Apache::lonnet::critical(
  181:            'put:'.$domain.':'.$user.':nohist_email:'.
  182:            &Apache::lonnet::escape($msgid).'='.
  183:            &Apache::lonnet::escape($message),$homeserver);
  184:     } else {
  185:        $status='no_host';
  186:     }
  187:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  188:                          $ENV{'user.home'},
  189:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
  190:     return $status;
  191: }
  192: 
  193: # =============================================================== Status Change
  194: 
  195: sub statuschange {
  196:     my ($msgid,$newstatus)=@_;
  197:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
  198:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  199:     unless ($status{$msgid}) { $status{$msgid}='new'; }
  200:     unless (($status{$msgid} eq 'replied') || 
  201:             ($status{$msgid} eq 'forwarded')) {
  202: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
  203:     }
  204:     if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
  205: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
  206:     }
  207: }
  208: 
  209: # ==================================================== Display Critical Message
  210: 
  211: sub discrit {
  212:     my $r=shift;
  213:       $r->print('<h1><font color=red>Critical Messages</font></h1>'.
  214:          '<form action=/adm/email method=post>'.
  215:          '<input type=hidden name=confirm value=true>');
  216:       my %what=&Apache::lonnet::dump('critical');
  217:       map {
  218:           my %content=&unpackagemsg($what{$_});
  219:           $content{'message'}=~s/\n/\<br\>/g;
  220: 	  $r->print('<hr>From: <b>'.$content{'sendername'}.'@'.
  221:                     $content{'senderdomain'}.'</b> ('.$content{'time'}.
  222:                     ')<br><blockquote>'.$content{'message'}.'</blockquote>'.
  223:   '<input type=submit name="rec_'.$_.'" value="Confirm Receipt">'.
  224:  '<input type=submit name="reprec_'.$_.'" value="Confirm Receipt and Reply">');
  225:       } sort keys %what;
  226:       $r->print(
  227:           '<input type=hidden name="displayedcrit" value="true"></form>');
  228: }
  229: 
  230: # =============================================================== Compose reply
  231: 
  232: sub comprep {
  233:     my ($r,$msgid)=@_;
  234:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  235:       my %content=&unpackagemsg($message{$msgid});
  236:       my $quotemsg='> '.$content{'message'};
  237:       $quotemsg=~s/\r/\n/g;
  238:       $quotemsg=~s/\f/\n/g;
  239:       $quotemsg=~s/\n+/\n\> /g;
  240:       my $subject='Re: '.$content{'subject'};
  241:       my $dispcrit='';
  242:       if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  243:          $dispcrit=
  244: 	     '<input type=checkbox name=critmsg> Send as critical message<p>';
  245:       }
  246:       $r->print(<<"ENDREPLY");
  247: <form action="/adm/email" method=post>
  248: <input type=hidden name=sendreply value="$msgid">
  249: Subject: <input type=text size=50 name=subject value="$subject"><p>
  250: <textarea name=message cols=60 rows=10>
  251: $quotemsg
  252: </textarea><p>
  253: $dispcrit
  254: <input type=submit value="Send Reply">
  255: </form>
  256: ENDREPLY
  257: }
  258: 
  259: # ======================================================== Display all messages
  260: 
  261: sub disall {
  262:     my $r=shift;
  263:     $r->print('<h1>Display All Messages</h1>'.
  264:      '<table border=2><tr><th colspan=2>&nbsp</th><th>Date</th>'.
  265:      '<th>Username</th><th>Domain</th><th>Subject</th><th>Status</th></tr>');
  266:     map {
  267:         my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
  268: 	    &Apache::lonmsg::unpackmsgid($_);
  269:        unless ($status eq 'deleted') {
  270:         if ($status eq 'new') {
  271: 	    $r->print('<tr bgcolor="#FFBB77">');
  272:         } elsif ($status eq 'read') {
  273: 	    $r->print('<tr bgcolor="#BBBB77">');
  274:         } elsif ($status eq 'replied') {
  275: 	    $r->print('<tr bgcolor="#AAAA88">');
  276: 	} else {
  277: 	    $r->print('<tr bgcolor="#99BBBB">');
  278:         }
  279:         $r->print('<td><a href="/adm/email?display='.$_.
  280:                   '">Open</a></td><td><a href="/adm/email?markdel='.$_.
  281:                   '">Delete</a></td><td>'.localtime($sendtime).'</td><td>'.
  282:                   $fromname.'</td><td>'.$fromdomain.'</td><td>'.
  283: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
  284:                       $status.'</td></tr>');
  285:        }
  286:     } sort split(/\&/,&Apache::lonnet::reply('keys:'.
  287: 					$ENV{'user.domain'}.':'.
  288:                                         $ENV{'user.name'}.':nohist_email',
  289:                                         $ENV{'user.home'}));
  290:     $r->print('</table></body></html>');
  291: }
  292: 
  293: # ============================================================== Compose output
  294: 
  295: sub compout {
  296:     my ($r,$forwarding)=@_;
  297:       my $dispcrit='';
  298:     my $dissub='';
  299:     my $dismsg='';
  300:     my $func='Send New';
  301:       if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  302:          $dispcrit=
  303: 	     '<input type=checkbox name=critmsg> Send as critical message<p>';
  304:       }
  305:     if ($forwarding) {
  306:        $dispcrit.='<input type=hidden name=forwid value="'.
  307: 	   $forwarding.'">';
  308:        $func='Forward';
  309:       my %message=&Apache::lonnet::get('nohist_email',[$forwarding]);
  310:       my %content=&unpackagemsg($message{$forwarding});
  311: 
  312:        $dissub='Forwarding: '.$content{'subject'};
  313:        $dismsg='Forwarded message from '.
  314: 	   $content{'sendername'}.' at '.$content{'senderdomain'};
  315:     }
  316:     my $defdom=$ENV{'user.domain'};
  317:       $r->print(<<"ENDCOMP");
  318: <form action="/adm/email" method=post>
  319: <input type=hidden name=sendmail value=on>
  320: <table>
  321: <tr><td>Username:</td><td><input type=text size=12 name=recuname></td></tr>
  322: <tr><td>Domain:</td>
  323: <td><input type=text size=12 name=recdomain value="$defdom"></td></tr>
  324: <tr><td>Subject:</td><td><input type=text size=50 name=subject value="$dissub">
  325: </td></tr></table>
  326: <textarea name=message cols=60 rows=10>$dismsg
  327: </textarea><p>
  328: $dispcrit
  329: <input type=submit value="$func Mail">
  330: </form>
  331: ENDCOMP
  332: }
  333: 
  334: # ===================================================================== Handler
  335: 
  336: sub handler {
  337:     my $r=shift;
  338: 
  339: # ----------------------------------------------------------- Set document type
  340: 
  341:   $r->content_type('text/html');
  342:   $r->send_http_header;
  343: 
  344:   return OK if $r->header_only;
  345: 
  346: # --------------------------- Get query string for limited number of parameters
  347: 
  348:     map {
  349:        my ($name, $value) = split(/=/,$_);
  350:        $value =~ tr/+/ /;
  351:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  352:        if (($name eq 'display') || ($name eq 'replyto') || 
  353:            ($name eq 'forward') || ($name eq 'markread') ||
  354:            ($name eq 'markdel') || ($name eq 'markunread') ||
  355:            ($name eq 'sendreply') || ($name eq 'compose') ||
  356:            ($name eq 'sendmail') || ($name eq 'critical')) {
  357:            unless ($ENV{'form.'.$name}) {
  358:               $ENV{'form.'.$name}=$value;
  359: 	   }
  360:        }
  361:     } (split(/&/,$ENV{'QUERY_STRING'}));
  362: 
  363: # --------------------------------------------------------------- Render Output
  364:   
  365:   $r->print('<html><head><title>EMail and Messaging</title></head>');
  366:   $r->print(
  367:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  368:   $r->print('<h1>EMail</h1>');
  369:   if ($ENV{'form.display'}) {
  370:       my $msgid=$ENV{'form.display'};
  371:       &statuschange($msgid,'read');
  372:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  373:       my %content=&unpackagemsg($message{$msgid});
  374:       $r->print('<b>Subject:</b> '.$content{'subject'}.
  375:              '<br><b>From:</b> '.$content{'sendername'}.' at '.
  376:                                  $content{'senderdomain'}.
  377:              '<br><b>Time:</b> '.$content{'time'}.'<p>'.
  378:              '<table border=2><tr bgcolor="#FFFFAA"><td>Functions:</td>'.
  379:            '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).
  380:              '"><b>Reply</b></a></td>'.
  381:            '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).
  382:              '"><b>Forward</b></a></td>'.
  383:         '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).
  384:              '"><b>Mark Unread</b></a></td>'.
  385:         '<td><a href="/adm/email"><b>Display all Messages</b></a></td>'.
  386:              '</tr></table><p><pre>'.
  387:              $content{'message'}.'</pre><hr>'.$content{'citation'});
  388:   } elsif ($ENV{'form.replyto'}) {
  389:       &comprep($r,$ENV{'form.replyto'});
  390:   } elsif ($ENV{'form.sendreply'}) {
  391:       my $msgid=$ENV{'form.sendreply'};
  392:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  393:       my %content=&unpackagemsg($message{$msgid});
  394:       &statuschange($msgid,'replied');
  395:       if (($ENV{'form.critmsg'}) && 
  396:           (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
  397:          $r->print('Sending critical: '.
  398:                 &user_crit_msg($content{'sendername'},
  399:                                  $content{'senderdomain'},
  400:                                  $ENV{'form.subject'},
  401:                                  $ENV{'form.message'}));
  402:       } else {
  403:          $r->print('Sending: '.&user_normal_msg($content{'sendername'},
  404:                                  $content{'senderdomain'},
  405:                                  $ENV{'form.subject'},
  406:                                  $ENV{'form.message'}));
  407:       }
  408:       if ($ENV{'form.displayedcrit'}) {
  409:           &discrit($r);
  410:       } else {
  411: 	  &disall($r);
  412:       }
  413:   } elsif ($ENV{'form.confirm'}) {
  414:       map {
  415:           if ($_=~/^form\.rec\_(.*)$/) {
  416: 	      $r->print('<b>Confirming Receipt:</b> '.
  417:                         &user_crit_received($1).'<br>');
  418:           }
  419:           if ($_=~/^form\.reprec\_(.*)$/) {
  420:               my $msgid=$1;
  421: 	      $r->print('<b>Confirming Receipt:</b> '.
  422:                         &user_crit_received($msgid).'<br>');
  423:               &comprep($r,$msgid);
  424:           }
  425:       } keys %ENV;
  426:       &discrit($r);
  427:   } elsif ($ENV{'form.critical'}) {
  428:       &discrit($r);
  429:   } elsif ($ENV{'form.forward'}) {
  430:       &compout($r,$ENV{'form.forward'});
  431:   } elsif ($ENV{'form.markread'}) {
  432:   } elsif ($ENV{'form.markdel'}) {
  433:       &statuschange($ENV{'form.markdel'},'deleted');
  434:       &disall($r);
  435:   } elsif ($ENV{'form.markunread'}) {
  436:       &statuschange($ENV{'form.markunread'},'new');
  437:       &disall($r);
  438:   } elsif ($ENV{'form.compose'}) {
  439:       &compout($r);
  440:   } elsif ($ENV{'form.sendmail'}) {
  441:       my %content=();
  442:       undef %content;
  443:       if ($ENV{'form.forwid'}) {
  444:         my $msgid=$ENV{'form.forwid'};
  445:         my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  446:         %content=&unpackagemsg($message{$msgid});
  447:         &statuschange($msgid,'forwarded');
  448:         $ENV{'form.message'}.="\n\n-- Forwarded message --\n\n".
  449: 	                       $content{'message'};
  450:       }
  451:       if (($ENV{'form.critmsg'}) && 
  452:           (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
  453:          $r->print('Sending critical: '.
  454:                 &user_crit_msg($ENV{'form.recuname'},
  455:                                  $ENV{'form.recdomain'},
  456:                                  $ENV{'form.subject'},
  457:                                  $ENV{'form.message'},
  458:                                  $content{'citation'}));
  459:       } else {
  460:          $r->print('Sending: '.&user_normal_msg($ENV{'form.recuname'},
  461:                                  $ENV{'form.recdomain'},
  462:                                  $ENV{'form.subject'},
  463:                                  $ENV{'form.message'},
  464:                                  $content{'citation'}));
  465:       }
  466:       if ($ENV{'form.displayedcrit'}) {
  467:           &discrit($r);
  468:       } else {
  469: 	  &disall($r);
  470:       }
  471:   } else {
  472:       &disall($r);
  473:   }
  474:   $r->print('</body></html>');
  475:   return OK;
  476: 
  477: }
  478: # ================================================= Main program, reset counter
  479: 
  480: sub BEGIN {
  481:     $msgcount=0;
  482: }
  483: 
  484: 1;
  485: __END__
  486: 
  487: 
  488: 
  489: 
  490: 
  491: 
  492: 

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