--- loncom/interface/lonchatfetch.pm	2002/08/08 13:44:17	1.1
+++ loncom/interface/lonchatfetch.pm	2005/02/17 08:29:42	1.15
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Chat Fetching
 #
-# $Id: lonchatfetch.pm,v 1.1 2002/08/08 13:44:17 www Exp $
+# $Id: lonchatfetch.pm,v 1.15 2005/02/17 08:29:42 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -29,11 +29,27 @@
 package Apache::lonchatfetch;
 
 use strict;
-use Apache::Constants qw(:common);
+use Apache::Constants qw(:common :http);
 use Apache::lontexconvert;
+use Apache::loncommon;
+use Apache::lonnet;
 
 sub handler {
     my $r = shift;
+
+    if (! &Apache::lonnet::allowed('pch',$ENV{'request.course.id'}.
+             ($ENV{'request.course.sec'}?'/'.$ENV{'request.course.sec'}:''))
+        ) {
+        return HTTP_NOT_ACCEPTABLE;
+    }
+
+    my $loaderror=&Apache::lonnet::overloaderror($r);
+    if ($loaderror) { return $loaderror; }
+    $loaderror=
+       &Apache::lonnet::overloaderror($r,
+         $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
+    if ($loaderror) { return $loaderror; }
+
     $r->content_type('text/html');
     $r->send_http_header;
     return OK if $r->header_only;
@@ -43,39 +59,100 @@ sub handler {
     my $cnum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
     my $cdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
     my $chome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
+
     my @entries=split(/\:/,
-                       &Apache::lonnet::reply("chatretr:$cdom:$cnum",$chome));
-    my ($lastid)=($entries[$#entries]=~/^(\w+)/);
-    my ($thentime,$idnum)=split(/\_/,$lastid);
+       &Apache::lonnet::reply(
+        "chatretr:$cdom:$cnum:$ENV{'user.domain'}:$ENV{'user.name'}",$chome));
+# Figure out what the last valid entry-id is
+    my ($lastid,$thentime,$idnum);
+    foreach (@entries) {
+	$_=~/^(\w+)/;
+        if ($1 ne 'active_participant') {
+	    $lastid=$1;
+            ($thentime,$idnum)=split(/\_/,$lastid);
+	}
+    }
+# ----------------------------------------------------------- Can see identity?
+    my $crs='/'.$ENV{'request.course.id'};
+    if ($ENV{'request.course.sec'}) {
+       $crs.='_'.$ENV{'request.course.sec'};
+    }                 
+    $crs=~s/\_/\//g;
+    my $seeid=&Apache::lonnet::allowed('rin',$crs);
 # -------------------------------------------------------- see which ones apply
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['lastid']);
     my $include=0;
     my $newstuff='';
-    unless ($ENV{'form.lastid'}) { $include=1; }
+    my $bottomid='';
+    unless ($ENV{'form.lastid'}) { 
+       $include=1; 
+       my $html=&Apache::lonxml::xmlbegin();
+       $newstuff=$html.'<head></head><body bgcolor="#FFFFFF">';
+    }
+    my @participants=();
     foreach (@entries) {
-	if ($include) {
-            my ($id,$msg)=split(/\:/,&Apache::lonnet::unescape($_));
-            chomp($msg);
-            my ($msgtime,$msgnum)=split(/\_/,$id);
-            my ($sdom,$snum,$anon,$contrib)=split(/\:/,
-                                              &Apache::lonnet::unescape($msg));
-            $contrib=&Apache::lonnet::unescape($contrib);
-            $contrib=~s/\n/\<br \/\>/g;
-	    $contrib=&Apache::lontexconvert::msgtexconverted($contrib);
-            $contrib=~s/\n/ /g;
-            $contrib=~s/\'/\&\#39\;/g;
-            my $sender=$snum.' at '.$sdom;
-            if ($anon) {
-            }
-	    $newstuff.='<b>'.$sender.'</b> ('.localtime($msgtime).'): '.
-                       $contrib."<br>";
-        } else {
-            $_=~/^(\w+)/;
-            if ($1 eq $ENV{'form.lastid'}) { $include=1; }
-        }
+	my ($id,$msg,$udom)=split(/\:/,&Apache::lonnet::unescape($_));
+ 	if ($id eq 'active_participant') {
+           chomp($udom);
+	   my $participant= &Apache::loncommon::nickname($msg,$udom);
+	   unless ($participant=~/\w/) { $participant=$msg.'@'.$udom; }
+	   $participants[$#participants+1]=$participant;
+	} elsif ($include) {
+	    chomp($msg);
+	    my ($msgtime,$msgnum)=split(/\_/,$id);
+	    my ($sdom,$snum,$anon,$contrib)=split(/\:/,
+					     &Apache::lonnet::unescape($msg));
+	    $contrib=&Apache::lonnet::unescape($contrib);
+	    $contrib=~s/\n/\<br \/\>/g;
+	    ($contrib,my $errors)=&Apache::lontexconvert::msgtexconverted($contrib);
+	    if ($errors) { $contrib.="[Message not fully displayed due to incorrect embedded TeX]"; }
+	    if ($errors && $snum eq $ENV{'user.name'} &&
+		$sdom eq $ENV{'user.domain'} ) {
+		$contrib.="<br />[TeX error message: $errors]";
+	    }
+	    $contrib=~s/\n/ /g;
+	    $contrib=~s/\'/\&\#39\;/g;
+	    my $sender='';
+	    if ($seeid) {
+		$sender=&Apache::loncommon::plainname($snum,$sdom);
+		my $nick=&Apache::loncommon::nickname($snum,$sdom);
+		if (($nick) && ($nick ne $sender)) {
+		    $sender.=' '.$nick;
+		}
+		unless ($sender) { $sender=$snum.'@'.$sdom; }
+		if ($anon) { $sender.=' [Anon]' };
+	    } elsif (!$anon) {
+		$sender=&Apache::loncommon::nickname($snum,$sdom);
+		unless ($sender) { $sender=$snum.'@'.$sdom; }
+	    } else {
+		$sender=&Apache::loncommon::screenname($snum,$sdom);
+		unless ($sender) { $sender="Anonymous"; }
+	    }
+	    $sender=~s/\'/\&\#39\;/g;
+	    my $color=$sender;
+	    $color=~tr/a-j/0-9/;
+	    $color=~tr/A-J/0-9/;
+	    $color=~tr/k-t/0-9/;
+	    $color=~tr/K-T/0-9/;
+	    $color=~tr/u-z/0-5/;
+	    $color=~tr/U-Z/0-5/;
+	    $color=~s/\D//g;
+	    $color=substr($color,0,6);
+	    my $timestamp=localtime($msgtime);
+	    my ($mhour,$mmin,$msec)=($timestamp=~/(\d\d)\:(\d\d)\:(\d\d)/);
+	    $newstuff.='<font color="#'.$color.'"><a name="'.$id.'"><b>'.
+		$sender.'</b> ('.$mhour.':'.$mmin.':'.$msec.'): '.
+		$contrib."</font><br>";
+	    $bottomid=$id;
+	} else {
+	    $_=~/^(\w+)/;
+	    if ($1 eq $ENV{'form.lastid'}) { $include=1; }
+	}
     }
+    my $participant_output=join('<br />',sort @participants);
+    my $html=&Apache::lonxml::xmlbegin();
     $r->print(<<ENDDOCUMENT);
-<html>
+$html
 <head>
 <title>The LearningOnline Network with CAPA</title>
   <meta HTTP-EQUIV="Refresh" CONTENT="5; url=/adm/chatfetch?lastid=$lastid">
@@ -83,7 +160,9 @@ sub handler {
 <body bgcolor="#FFFFFF">
 <script>
 parent.chatout.document.writeln('$newstuff');
+parent.chatout.scroll(0,10000000);
 </script>
+$participant_output
 </body>
 </html>
 ENDDOCUMENT