File:  [LON-CAPA] / loncom / interface / lonmsg.pm
Revision 1.9: download - view: text, annotated - select for diffs
Thu Jul 26 21:40:27 2001 UTC (22 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- modified eget and del in lonnet to take optiona domain and user args
- modified lonmsg usr_crit_received to use new semantics
- retrofitted devalidate to use the new more powerful del

# The LearningOnline Network with CAPA
#
# Routines for messaging
#
# (Routines to control the menu
#
# (TeX Conversion Module
#
# 05/29/00,05/30 Gerd Kortemeyer)
#
# 10/05 Gerd Kortemeyer)
#
# 10/19,10/20,10/30,
# 02/06/01 Gerd Kortemeyer

package Apache::lonmsg;

use strict;
use Apache::lonnet();
use vars qw($msgcount);
use HTML::TokeParser;
use Apache::Constants qw(:common);

# ===================================================================== Package

sub packagemsg {
    my ($subject,$message,$citation)=@_;
    $message=~s/\</\&lt\;/g;
    $message=~s/\>/\&gt\;/g;
    $citation=~s/\</\&lt\;/g;
    $citation=~s/\>/\&gt\;/g;
    $subject=~s/\</\&lt\;/g;
    $subject=~s/\>/\&gt\;/g;
    my $now=time;
    $msgcount++;
    my $partsubj=$subject;
    $partsubj=&Apache::lonnet::escape($partsubj);
    $partsubj=substr($partsubj,0,50);
    my $msgid=&Apache::lonnet::escape(
           $now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
           $ENV{'user.domain'}.':'.$msgcount.':'.$$);
    return $msgid,
           '<sendername>'.$ENV{'user.name'}.'</sendername>'.
           '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
           '<subject>'.$subject.'</subject>'.
	   '<time>'.localtime($now).'</time>'.
	   '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
           '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
	   '<browsertype>'.$ENV{'browser.type'}.'</browsertype>'.
	   '<browseros>'.$ENV{'browser.os'}.'</browseros>'.
	   '<browserversion>'.$ENV{'browser.version'}.'</browserversion>'.
           '<browsermathml>'.$ENV{'browser.mathml'}.'</browsermathml>'.
	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
	   '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
	   '<role>'.$ENV{'request.role'}.'</role>'.
	   '<resource>'.$ENV{'request.filename'}.'</resource>'.
           '<msgid>'.$msgid.'</msgid>'.
	   '<message>'.$message.'</message>'.
	   '<citation>'.$citation.'</citation>';
}

# ================================================== Unpack message into a hash

sub unpackagemsg {
    my $message=shift;
    my %content=();
    my $parser=HTML::TokeParser->new(\$message);
    my $token;
    while ($token=$parser->get_token) {
       if ($token->[0] eq 'S') {
	   my $entry=$token->[1];
           my $value=$parser->get_text('/'.$entry);
           $content{$entry}=$value;
       }
    }
    return %content;
}

# ======================================================= Get info out of msgid

sub unpackmsgid {
    my $msgid=&Apache::lonnet::unescape(shift);
    my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
                          &Apache::lonnet::unescape($msgid));
    my %status=&Apache::lonnet::get('email_status',[$msgid]);
    if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
    unless ($status{$msgid}) { $status{$msgid}='new'; }
    return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
} 

# =============================== Automated message to the author of a resource

sub author_res_msg {
    my ($filename,$message)=@_;
    unless ($message) { return 'empty'; }
    $filename=&Apache::lonnet::declutter($filename);
    my ($domain,$author,@dummy)=split(/\//,$filename);
    my $homeserver=&Apache::lonnet::homeserver($author,$domain);
    if ($homeserver ne 'no_host') {
       my $id=unpack("%32C*",$message);
       my $msgid;
       ($msgid,$message)=&packagemsg($filename,$message);
       return &Apache::lonnet::reply('put:'.$domain.':'.$author.
         ':nohist_res_msgs:'.
          &Apache::lonnet::escape($filename.'_'.$id).'='.
          &Apache::lonnet::escape($message),$homeserver);
    }
    return 'no_host';
}

# ================================================== Critical message to a user

sub user_crit_msg {
    my ($user,$domain,$subject,$message)=@_;
# Check if allowed missing
    my $status='';
    my $msgid='undefined';
    unless (($message)&&($user)&&($domain)) { $status='empty'; };
    my $homeserver=&Apache::lonnet::homeserver($user,$domain);
    if ($homeserver ne 'no_host') {
       my $msgid;
       ($msgid,$message)=&packagemsg($subject,$message);
       $status=&Apache::lonnet::critical(
           'put:'.$domain.':'.$user.':critical:'.
           &Apache::lonnet::escape($msgid).'='.
           &Apache::lonnet::escape($message),$homeserver);
    } else {
       $status='no_host';
    }
    &Apache::lonnet::logthis(
      'Sending critical email '.$msgid.
      ', log status: '.
      &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                         $ENV{'user.home'},
      'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
      .$status));
    return $status;
}

# =================================================== Critical message received

sub user_crit_received {
    my $message=shift;
    my %contents=&unpackagemsg($message);
    my $status='rec: '.
     &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
                     'Receipt: '.$ENV{'user.name'}.' at '.$ENV{'user.domain'},
                     'User '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                     ' acknowledged receipt of message "'.
                     $contents{'subject'}.'" dated '.$contents{'time'}.".\n\n"
                     .'Message ID: '.$contents{'msgid'});
    $status.=' trans: '.
     &Apache::lonnet::put('nohist_email',$contents{'msgid'} => $message);
    $status.=' del: '.
     &Apache::lonnet::del('critical',[$contents{'msgid'}]);
    &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                         $ENV{'user.home'},'Received critical message '.
                         $contents{'msgid'}.
                         ', '.$status);
}

# ======================================================== Normal communication

sub user_normal_msg {
    my ($user,$domain,$subject,$message,$citation)=@_;
# Check if allowed missing
    my $status='';
    my $msgid='undefined';
    unless (($message)&&($user)&&($domain)) { $status='empty'; };
    my $homeserver=&Apache::lonnet::homeserver($user,$domain);
    if ($homeserver ne 'no_host') {
       my $msgid;
       ($msgid,$message)=&packagemsg($subject,$message,$citation);
       $status=&Apache::lonnet::critical(
           'put:'.$domain.':'.$user.':nohist_email:'.
           &Apache::lonnet::escape($msgid).'='.
           &Apache::lonnet::escape($message),$homeserver);
    } else {
       $status='no_host';
    }
    &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                         $ENV{'user.home'},
      'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
    return $status;
}

# =============================================================== Status Change

sub statuschange {
    my ($msgid,$newstatus)=@_;
    my %status=&Apache::lonnet::get('email_status',[$msgid]);
    if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
    unless ($status{$msgid}) { $status{$msgid}='new'; }
    unless (($status{$msgid} eq 'replied') || 
            ($status{$msgid} eq 'forwarded')) {
	&Apache::lonnet::put('email_status',($msgid => $newstatus));
    }
}
# ===================================================================== Handler

sub handler {
    my $r=shift;

# ----------------------------------------------------------- Set document type

  $r->content_type('text/html');
  $r->send_http_header;

  return OK if $r->header_only;

# --------------------------- Get query string for limited number of parameters

    map {
       my ($name, $value) = split(/=/,$_);
       $value =~ tr/+/ /;
       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
       if (($name eq 'display') || ($name eq 'replyto') || 
           ($name eq 'forward') || ($name eq 'mark') ||
           ($name eq 'sendreply')) {
           unless ($ENV{'form.'.$name}) {
              $ENV{'form.'.$name}=$value;
	   }
       }
    } (split(/&/,$ENV{'QUERY_STRING'}));

# --------------------------------------------------------------- Render Output
  
  $r->print('<html><head><title>EMail and Messaging</title></head>');
  $r->print(
   '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
  $r->print('<h1>EMail</h1>');
  if ($ENV{'form.display'}) {
      my $msgid=$ENV{'form.display'};
      &statuschange($msgid,'read');
      my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
      my %content=&unpackagemsg($message{$msgid});
      $r->print('<b>Subject:</b> '.$content{'subject'}.
             '<br><b>From:</b> '.$content{'sendername'}.' at '.
                                 $content{'senderdomain'}.
             '<br><b>Time:</b> '.$content{'time'}.'<hr>Functions: '.
             '<a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).
             '"><b>Reply</b></a><hr><pre>'.
             $content{'message'}.'</pre><hr>'.$content{'citation'});
  } elsif ($ENV{'form.replyto'}) {
      my $msgid=$ENV{'form.replyto'};
      my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
      my %content=&unpackagemsg($message{$msgid});
      my $quotemsg='> '.$content{'message'};
      $quotemsg=~s/\r/\n/g;
      $quotemsg=~s/\f/\n/g;
      $quotemsg=~s/\n+/\n\> /g;
      my $subject='Re: '.$content{'subject'};
      $r->print(<<"ENDREPLY");
<form action="/adm/email" method=post>
<input type=hidden name=sendreply value="$msgid">
Subject: <input type=text size=50 name=subject value="$subject"><p>
<textarea name=message cols=60 rows=10>
$quotemsg
</textarea><p>
<input type=submit value="Send Reply">
</form>
ENDREPLY
  } elsif ($ENV{'form.sendreply'}) {
      my $msgid=$ENV{'form.sendreply'};
      my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
      my %content=&unpackagemsg($message{$msgid});
      &statuschange($msgid,'replied');
      $r->print('Sending: '.&user_normal_msg($content{'sendername'},
                                 $content{'senderdomain'},
                                 $ENV{'form.subject'},
                                 $ENV{'form.message'}));
  } elsif ($ENV{'form.forward'}) {
  } elsif ($ENV{'form.mark'}) {
  } else {
    $r->print('<table border=2><tr><th>&nbsp</th><th>Date</th>'.
     '<th>Username</th><th>Domain</th><th>Subject</th><th>Status</th></tr>');
    map {
        my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
	    &Apache::lonmsg::unpackmsgid($_);
        if ($status eq 'new') {
	    $r->print('<tr bgcolor="#FFBB77">');
        } elsif ($status eq 'read') {
	    $r->print('<tr bgcolor="#BBBB77">');
        } elsif ($status eq 'replied') {
	    $r->print('<tr bgcolor="#AAAA88">');
	} else {
	    $r->print('<tr bgcolor="#99BBBB">');
        }
        $r->print('<td><a href="/adm/email?display='.$_.
                  '">Open</a></td><td>'.localtime($sendtime).'</td><td>'.
                  $fromname.'</td><td>'.$fromdomain.'</td><td>'.
		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
                      $status.'</td></tr>');
    } sort split(/\&/,&Apache::lonnet::reply('keys:'.
					$ENV{'user.domain'}.':'.
                                        $ENV{'user.name'}.':nohist_email',
                                        $ENV{'user.home'}));
    $r->print('</table></body></html>');

  }
  $r->print('</body></html>');
  return OK;

}
# ================================================= Main program, reset counter

sub BEGIN {
    $msgcount=0;
}

1;
__END__








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