1: # The LearningOnline Network
2: # Chat Fetching
3: #
4: # $Id: lonchatfetch.pm,v 1.28 2006/06/28 23:56:09 raeburn Exp $
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/
27: #
28:
29: package Apache::lonchatfetch;
30:
31: use strict;
32: use Apache::Constants qw(:common :http);
33: use Apache::lontexconvert;
34: use Apache::loncommon;
35: use Apache::lonnet;
36: use Apache::longroup;
37: use lib '/home/httpd/lib/perl/';
38: use LONCAPA;
39:
40:
41: sub handler {
42: my $r = shift;
43:
44: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
45: ['lastid','group']);
46: my ($group,$grouptitle);
47: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
48: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
49: if (defined($env{'form.group'})) {
50: $group = $env{'form.group'};
51: if ((! &Apache::lonnet::allowed('pgc',$env{'request.course.id'}.'/'.
52: $group)) &&
53: (! &Apache::lonnet::allowed('vcg',$env{'request.course.id'}))) {
54: return HTTP_NOT_ACCEPTABLE;
55: }
56: my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum,$group);
57: if (%curr_groups) {
58: my %group_info =
59: &Apache::longroup::get_group_settings($curr_groups{$group});
60: $grouptitle =
61: '<b>'.&unescape($group_info{description}).
62: '</b><br />';
63: }
64: } elsif (! &Apache::lonnet::allowed('pch',$env{'request.course.id'}.
65: ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))
66: ) {
67: return HTTP_NOT_ACCEPTABLE;
68: }
69:
70: my $loaderror=&Apache::lonnet::overloaderror($r);
71: if ($loaderror) { return $loaderror; }
72: $loaderror=
73: &Apache::lonnet::overloaderror($r,
74: $env{'course.'.$env{'request.course.id'}.'.home'});
75: if ($loaderror) { return $loaderror; }
76:
77: &Apache::loncommon::content_type($r,'text/html');
78: $r->send_http_header;
79: return OK if $r->header_only;
80:
81: # ------------------------------------------------------------ retrieve entries
82:
83: my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
84:
85: my @entries=split(/\:/,
86: &Apache::lonnet::reply(
87: "chatretr:$cdom:$cnum:$env{'user.domain'}:$env{'user.name'}:$group",
88: $chome));
89: # Figure out what the last valid entry-id is
90: my ($lastid,$thentime,$idnum);
91: foreach (@entries) {
92: $_=~/^(\w+)/;
93: if ($1 ne 'active_participant') {
94: $lastid=$1;
95: ($thentime,$idnum)=split(/\_/,$lastid);
96: }
97: }
98: # ----------------------------------------------------------- Can see identity?
99: my $seeid = &get_seeid_status();
100: # -------------------------------------------------------- see which ones apply
101: my $include=0;
102: my $newstuff='';
103: my $bottomid='';
104: unless ($env{'form.lastid'}) {
105: $include=1;
106: $newstuff .=
107: &Apache::loncommon::start_page(undef,undef,
108: {'only_body' => 1,
109: 'bgcolor' => '#FFFFFF',
110: 'js_ready' => 1,});
111: # removing the CSS reference for now
112: # see BUG# 4839
113: # to fix, need to either write the whole webpage to the bottom frame
114: # everytime or store the page in a js variable somewhere.
115: # although in tryng both of these ideas, the .scroll to
116: # the bottom seems to fail.
117: $newstuff =~ s|(<link.*?/>)||;
118: }
119: my @participants=();
120: foreach (@entries) {
121: my ($id,$msg,$udom)=split(/\:/,&unescape($_));
122: if ($id eq 'active_participant') {
123: chomp($udom);
124: my $participant= &Apache::loncommon::nickname($msg,$udom);
125: unless ($participant=~/\w/) { $participant=$msg.'@'.$udom; }
126: $participants[$#participants+1]=$participant;
127: } elsif ($include) {
128: chomp($msg);
129: my ($msgtime,$msgnum)=split(/\_/,$id);
130: my ($sdom,$snum,$anon,$contrib)=split(/\:/,
131: &unescape($msg));
132: $contrib=&unescape($contrib);
133: &Apache::lonfeedback::newline_to_br(\$contrib);
134: ($contrib,my $errors)=&Apache::lontexconvert::msgtexconverted($contrib);
135: if ($errors) { $contrib.="[Message not fully displayed due to incorrect embedded TeX]"; }
136: if ($errors && $snum eq $env{'user.name'} &&
137: $sdom eq $env{'user.domain'} ) {
138: $contrib.="<br />[TeX error message: $errors]";
139: }
140: $contrib=~s/\n/ /g;
141: $contrib=~s/\'/\&\#39\;/g;
142: my $sender='';
143: if ($seeid) {
144: $sender=&Apache::loncommon::plainname($snum,$sdom);
145: my $nick=&Apache::loncommon::nickname($snum,$sdom);
146: if (($nick) && ($nick ne $sender)) {
147: $sender.=' '.$nick;
148: }
149: unless ($sender) { $sender=$snum.'@'.$sdom; }
150: if ($anon) { $sender.=' [Anon]' };
151: } elsif (!$anon) {
152: $sender=&Apache::loncommon::nickname($snum,$sdom);
153: unless ($sender) { $sender=$snum.'@'.$sdom; }
154: } else {
155: $sender=&Apache::loncommon::screenname($snum,$sdom);
156: unless ($sender) { $sender="Anonymous"; }
157: }
158: $sender=~s/\'/\&\#39\;/g;
159: my $color=$sender;
160: $color=~tr/a-j/0-9/;
161: $color=~tr/A-J/0-9/;
162: $color=~tr/k-t/0-9/;
163: $color=~tr/K-T/0-9/;
164: $color=~tr/u-z/0-5/;
165: $color=~tr/U-Z/0-5/;
166: $color=~s/\D//g;
167: $color=substr($color,0,6);
168: my $timestamp=localtime($msgtime);
169: my ($mhour,$mmin,$msec)=($timestamp=~/(\d\d)\:(\d\d)\:(\d\d)/);
170: $newstuff.='<font color="#'.$color.'"><a name="LC_'.$id.'"></a><b>'.
171: $sender.'</b> ('.$mhour.':'.$mmin.':'.$msec.'): '.
172: $contrib."</font><br />";
173: $bottomid='LC_'.$id;
174: } else {
175: $_=~/^(\w+)/;
176: if ($1 eq $env{'form.lastid'}) { $include=1; }
177: }
178: }
179: my $participant_output=join('<br />',sort @participants);
180: my $refresh_cmd = "/adm/chatfetch?lastid=$lastid";
181: if (defined($group)) {
182: $refresh_cmd .= "&group=$group";
183: }
184: my $start_page =
185: &Apache::loncommon::start_page('Chat',undef,
186: {'redirect' => [5,$refresh_cmd],
187: 'only_body' => 1,});
188: my $end_page = &Apache::loncommon::end_page();
189: $r->print(<<ENDDOCUMENT);
190: $start_page
191: <script type="text/javascript">
192: parent.chatout.document.writeln('$newstuff');
193: parent.chatout.scroll(0,10000000);
194: </script>
195: $grouptitle
196: $participant_output
197: $end_page
198: ENDDOCUMENT
199: return OK;
200: }
201:
202: sub get_seeid_status {
203: my $crs='/'.$env{'request.course.id'};
204: my $seeid;
205: if (exists($env{'form.group'})) {
206: $seeid = &Apache::lonnet::allowed('rci',$crs.'/'.$env{'form.group'});
207: } else {
208: if ($env{'request.course.sec'}) {
209: $crs.='_'.$env{'request.course.sec'};
210: }
211: $crs=~s/\_/\//g;
212: $seeid=&Apache::lonnet::allowed('rin',$crs);
213: }
214: return $seeid;
215: }
216:
217: 1;
218: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>