Annotation of loncom/interface/lonmsg.pm, revision 1.11
1.1 www 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: #
1.6 www 13: # 10/19,10/20,10/30,
14: # 02/06/01 Gerd Kortemeyer
1.11 ! www 15: # 07/27 Guy Albertelli
! 16: # 07/27 Gerd Kortemeyer
1.1 www 17:
18: package Apache::lonmsg;
19:
20: use strict;
21: use Apache::lonnet();
1.2 www 22: use vars qw($msgcount);
23: use HTML::TokeParser;
1.5 www 24: use Apache::Constants qw(:common);
1.1 www 25:
26: # ===================================================================== Package
27:
1.3 www 28: sub packagemsg {
1.7 www 29: my ($subject,$message,$citation)=@_;
1.1 www 30: $message=~s/\</\<\;/g;
31: $message=~s/\>/\>\;/g;
1.7 www 32: $citation=~s/\</\<\;/g;
33: $citation=~s/\>/\>\;/g;
1.1 www 34: $subject=~s/\</\<\;/g;
35: $subject=~s/\>/\>\;/g;
1.2 www 36: my $now=time;
37: $msgcount++;
1.6 www 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.':'.$$);
1.2 www 44: return $msgid,
45: '<sendername>'.$ENV{'user.name'}.'</sendername>'.
1.1 www 46: '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
47: '<subject>'.$subject.'</subject>'.
1.2 www 48: '<time>'.localtime($now).'</time>'.
1.1 www 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>'.
1.2 www 60: '<msgid>'.$msgid.'</msgid>'.
1.7 www 61: '<message>'.$message.'</message>'.
62: '<citation>'.$citation.'</citation>';
1.1 www 63: }
64:
1.2 www 65: # ================================================== Unpack message into a hash
66:
1.3 www 67: sub unpackagemsg {
1.2 www 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:
1.6 www 82: # ======================================================= Get info out of msgid
83:
84: sub unpackmsgid {
1.7 www 85: my $msgid=&Apache::lonnet::unescape(shift);
1.6 www 86: my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
1.7 www 87: &Apache::lonnet::unescape($msgid));
1.8 albertel 88: my %status=&Apache::lonnet::get('email_status',[$msgid]);
1.6 www 89: if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
90: unless ($status{$msgid}) { $status{$msgid}='new'; }
91: return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
92: }
93:
1.1 www 94: # =============================== Automated message to the author of a resource
95:
96: sub author_res_msg {
97: my ($filename,$message)=@_;
1.2 www 98: unless ($message) { return 'empty'; }
1.1 www 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);
1.2 www 104: my $msgid;
1.3 www 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);
1.1 www 110: }
1.2 www 111: return 'no_host';
1.1 www 112: }
113:
114: # ================================================== Critical message to a user
115:
116: sub user_crit_msg {
117: my ($user,$domain,$subject,$message)=@_;
1.2 www 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;
1.3 www 125: ($msgid,$message)=&packagemsg($subject,$message);
1.4 www 126: $status=&Apache::lonnet::critical(
127: 'put:'.$domain.':'.$user.':critical:'.
128: &Apache::lonnet::escape($msgid).'='.
129: &Apache::lonnet::escape($message),$homeserver);
1.2 www 130: } else {
131: $status='no_host';
132: }
133: &Apache::lonnet::logthis(
1.4 www 134: 'Sending critical email '.$msgid.
1.2 www 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: '
1.4 www 139: .$status));
1.2 www 140: return $status;
141: }
142:
143: # =================================================== Critical message received
144:
145: sub user_crit_received {
146: my $message=shift;
1.4 www 147: my %contents=&unpackagemsg($message);
1.5 www 148: my $status='rec: '.
149: &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
1.4 www 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'});
1.5 www 155: $status.=' trans: '.
1.10 albertel 156: &Apache::lonnet::put('nohist_email',{$contents{'msgid'} => $message});
1.5 www 157: $status.=' del: '.
1.9 albertel 158: &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.5 www 159: &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
160: $ENV{'user.home'},'Received critical message '.
161: $contents{'msgid'}.
162: ', '.$status);
1.2 www 163: }
164:
165: # ======================================================== Normal communication
166:
167: sub user_normal_msg {
1.7 www 168: my ($user,$domain,$subject,$message,$citation)=@_;
1.2 www 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;
1.7 www 176: ($msgid,$message)=&packagemsg($subject,$message,$citation);
1.4 www 177: $status=&Apache::lonnet::critical(
178: 'put:'.$domain.':'.$user.':nohist_email:'.
179: &Apache::lonnet::escape($msgid).'='.
180: &Apache::lonnet::escape($message),$homeserver);
1.2 www 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:
1.7 www 190: # =============================================================== Status Change
191:
192: sub statuschange {
193: my ($msgid,$newstatus)=@_;
1.8 albertel 194: my %status=&Apache::lonnet::get('email_status',[$msgid]);
1.7 www 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')) {
1.10 albertel 199: &Apache::lonnet::put('email_status',{$msgid => $newstatus});
1.7 www 200: }
201: }
1.5 www 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:
1.6 www 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') ||
1.7 www 221: ($name eq 'forward') || ($name eq 'mark') ||
1.11 ! www 222: ($name eq 'sendreply') || ($name eq 'compose')) {
1.6 www 223: unless ($ENV{'form.'.$name}) {
224: $ENV{'form.'.$name}=$value;
225: }
226: }
227: } (split(/&/,$ENV{'QUERY_STRING'}));
228:
1.5 www 229: # --------------------------------------------------------------- Render Output
230:
231: $r->print('<html><head><title>EMail and Messaging</title></head>');
1.7 www 232: $r->print(
233: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1.5 www 234: $r->print('<h1>EMail</h1>');
1.6 www 235: if ($ENV{'form.display'}) {
1.7 www 236: my $msgid=$ENV{'form.display'};
237: &statuschange($msgid,'read');
1.8 albertel 238: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
1.7 www 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'});
1.6 www 247: } elsif ($ENV{'form.replyto'}) {
1.7 www 248: my $msgid=$ENV{'form.replyto'};
1.8 albertel 249: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
1.7 www 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'};
1.11 ! www 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: }
1.7 www 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>
1.11 ! www 268: $dispcrit
1.7 www 269: <input type=submit value="Send Reply">
270: </form>
271: ENDREPLY
272: } elsif ($ENV{'form.sendreply'}) {
273: my $msgid=$ENV{'form.sendreply'};
1.8 albertel 274: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
1.7 www 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'}));
1.6 www 281: } elsif ($ENV{'form.forward'}) {
282: } elsif ($ENV{'form.mark'}) {
1.11 ! www 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'}) {
1.6 www 300: } else {
301: $r->print('<table border=2><tr><th> </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') {
1.7 www 311: $r->print('<tr bgcolor="#AAAA88">');
1.6 www 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: }
1.5 www 327: $r->print('</body></html>');
328: return OK;
329:
330: }
1.2 www 331: # ================================================= Main program, reset counter
332:
333: sub BEGIN {
334: $msgcount=0;
1.1 www 335: }
336:
337: 1;
338: __END__
339:
340:
341:
342:
343:
344:
345:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>