File:  [LON-CAPA] / loncom / interface / lonmsg.pm
Revision 1.11: download - view: text, annotated - select for diffs
Sat Jul 28 04:16:29 2001 UTC (22 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
More email functions

    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 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 $message=shift;
  147:     my %contents=&unpackagemsg($message);
  148:     my $status='rec: '.
  149:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
  150:                      'Receipt: '.$ENV{'user.name'}.' at '.$ENV{'user.domain'},
  151:                      'User '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  152:                      ' acknowledged receipt of message "'.
  153:                      $contents{'subject'}.'" dated '.$contents{'time'}.".\n\n"
  154:                      .'Message ID: '.$contents{'msgid'});
  155:     $status.=' trans: '.
  156:      &Apache::lonnet::put('nohist_email',{$contents{'msgid'} => $message});
  157:     $status.=' del: '.
  158:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
  159:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  160:                          $ENV{'user.home'},'Received critical message '.
  161:                          $contents{'msgid'}.
  162:                          ', '.$status);
  163: }
  164: 
  165: # ======================================================== Normal communication
  166: 
  167: sub user_normal_msg {
  168:     my ($user,$domain,$subject,$message,$citation)=@_;
  169: # Check if allowed missing
  170:     my $status='';
  171:     my $msgid='undefined';
  172:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
  173:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
  174:     if ($homeserver ne 'no_host') {
  175:        my $msgid;
  176:        ($msgid,$message)=&packagemsg($subject,$message,$citation);
  177:        $status=&Apache::lonnet::critical(
  178:            'put:'.$domain.':'.$user.':nohist_email:'.
  179:            &Apache::lonnet::escape($msgid).'='.
  180:            &Apache::lonnet::escape($message),$homeserver);
  181:     } else {
  182:        $status='no_host';
  183:     }
  184:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
  185:                          $ENV{'user.home'},
  186:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
  187:     return $status;
  188: }
  189: 
  190: # =============================================================== Status Change
  191: 
  192: sub statuschange {
  193:     my ($msgid,$newstatus)=@_;
  194:     my %status=&Apache::lonnet::get('email_status',[$msgid]);
  195:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
  196:     unless ($status{$msgid}) { $status{$msgid}='new'; }
  197:     unless (($status{$msgid} eq 'replied') || 
  198:             ($status{$msgid} eq 'forwarded')) {
  199: 	&Apache::lonnet::put('email_status',{$msgid => $newstatus});
  200:     }
  201: }
  202: # ===================================================================== Handler
  203: 
  204: sub handler {
  205:     my $r=shift;
  206: 
  207: # ----------------------------------------------------------- Set document type
  208: 
  209:   $r->content_type('text/html');
  210:   $r->send_http_header;
  211: 
  212:   return OK if $r->header_only;
  213: 
  214: # --------------------------- Get query string for limited number of parameters
  215: 
  216:     map {
  217:        my ($name, $value) = split(/=/,$_);
  218:        $value =~ tr/+/ /;
  219:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  220:        if (($name eq 'display') || ($name eq 'replyto') || 
  221:            ($name eq 'forward') || ($name eq 'mark') ||
  222:            ($name eq 'sendreply') || ($name eq 'compose')) {
  223:            unless ($ENV{'form.'.$name}) {
  224:               $ENV{'form.'.$name}=$value;
  225: 	   }
  226:        }
  227:     } (split(/&/,$ENV{'QUERY_STRING'}));
  228: 
  229: # --------------------------------------------------------------- Render Output
  230:   
  231:   $r->print('<html><head><title>EMail and Messaging</title></head>');
  232:   $r->print(
  233:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  234:   $r->print('<h1>EMail</h1>');
  235:   if ($ENV{'form.display'}) {
  236:       my $msgid=$ENV{'form.display'};
  237:       &statuschange($msgid,'read');
  238:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  239:       my %content=&unpackagemsg($message{$msgid});
  240:       $r->print('<b>Subject:</b> '.$content{'subject'}.
  241:              '<br><b>From:</b> '.$content{'sendername'}.' at '.
  242:                                  $content{'senderdomain'}.
  243:              '<br><b>Time:</b> '.$content{'time'}.'<hr>Functions: '.
  244:              '<a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).
  245:              '"><b>Reply</b></a><hr><pre>'.
  246:              $content{'message'}.'</pre><hr>'.$content{'citation'});
  247:   } elsif ($ENV{'form.replyto'}) {
  248:       my $msgid=$ENV{'form.replyto'};
  249:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  250:       my %content=&unpackagemsg($message{$msgid});
  251:       my $quotemsg='> '.$content{'message'};
  252:       $quotemsg=~s/\r/\n/g;
  253:       $quotemsg=~s/\f/\n/g;
  254:       $quotemsg=~s/\n+/\n\> /g;
  255:       my $subject='Re: '.$content{'subject'};
  256:       my $dispcrit='';
  257:       if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  258:          $dispcrit=
  259: 	     '<input type=checkbox name=critmsg> Send as critical message<p>';
  260:       }
  261:       $r->print(<<"ENDREPLY");
  262: <form action="/adm/email" method=post>
  263: <input type=hidden name=sendreply value="$msgid">
  264: Subject: <input type=text size=50 name=subject value="$subject"><p>
  265: <textarea name=message cols=60 rows=10>
  266: $quotemsg
  267: </textarea><p>
  268: $dispcrit
  269: <input type=submit value="Send Reply">
  270: </form>
  271: ENDREPLY
  272:   } elsif ($ENV{'form.sendreply'}) {
  273:       my $msgid=$ENV{'form.sendreply'};
  274:       my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
  275:       my %content=&unpackagemsg($message{$msgid});
  276:       &statuschange($msgid,'replied');
  277:       $r->print('Sending: '.&user_normal_msg($content{'sendername'},
  278:                                  $content{'senderdomain'},
  279:                                  $ENV{'form.subject'},
  280:                                  $ENV{'form.message'}));
  281:   } elsif ($ENV{'form.forward'}) {
  282:   } elsif ($ENV{'form.mark'}) {
  283:   } elsif ($ENV{'form.compose'}) {
  284:       my $dispcrit='';
  285:       if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
  286:          $dispcrit=
  287: 	     '<input type=checkbox name=critmsg> Send as critical message<p>';
  288:       }
  289:       $r->print(<<"ENDCOMP");
  290: <form action="/adm/email" method=post>
  291: <input type=hidden name=sendmail value=on>
  292: Subject: <input type=text size=50 name=subject value=""><p>
  293: <textarea name=message cols=60 rows=10>
  294: </textarea><p>
  295: $dispcrit
  296: <input type=submit value="Send Mail">
  297: </form>
  298: ENDCOMP
  299:   } elsif ($ENV{'form.sendmail'}) {
  300:   } else {
  301:     $r->print('<table border=2><tr><th>&nbsp</th><th>Date</th>'.
  302:      '<th>Username</th><th>Domain</th><th>Subject</th><th>Status</th></tr>');
  303:     map {
  304:         my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
  305: 	    &Apache::lonmsg::unpackmsgid($_);
  306:         if ($status eq 'new') {
  307: 	    $r->print('<tr bgcolor="#FFBB77">');
  308:         } elsif ($status eq 'read') {
  309: 	    $r->print('<tr bgcolor="#BBBB77">');
  310:         } elsif ($status eq 'replied') {
  311: 	    $r->print('<tr bgcolor="#AAAA88">');
  312: 	} else {
  313: 	    $r->print('<tr bgcolor="#99BBBB">');
  314:         }
  315:         $r->print('<td><a href="/adm/email?display='.$_.
  316:                   '">Open</a></td><td>'.localtime($sendtime).'</td><td>'.
  317:                   $fromname.'</td><td>'.$fromdomain.'</td><td>'.
  318: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
  319:                       $status.'</td></tr>');
  320:     } sort split(/\&/,&Apache::lonnet::reply('keys:'.
  321: 					$ENV{'user.domain'}.':'.
  322:                                         $ENV{'user.name'}.':nohist_email',
  323:                                         $ENV{'user.home'}));
  324:     $r->print('</table></body></html>');
  325: 
  326:   }
  327:   $r->print('</body></html>');
  328:   return OK;
  329: 
  330: }
  331: # ================================================= Main program, reset counter
  332: 
  333: sub BEGIN {
  334:     $msgcount=0;
  335: }
  336: 
  337: 1;
  338: __END__
  339: 
  340: 
  341: 
  342: 
  343: 
  344: 
  345: 

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