Annotation of loncom/interface/lonmsg.pm, revision 1.13
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
1.12 www 16: # 07/27,07/28,07/30,08/03 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 {
1.12 www 146: my $msgid=shift;
147: my %message=&Apache::lonnet::get('critical',[$msgid]);
148: my %contents=&unpackagemsg($message{$msgid});
1.5 www 149: my $status='rec: '.
150: &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
1.4 www 151: 'Receipt: '.$ENV{'user.name'}.' at '.$ENV{'user.domain'},
152: 'User '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
153: ' acknowledged receipt of message "'.
154: $contents{'subject'}.'" dated '.$contents{'time'}.".\n\n"
155: .'Message ID: '.$contents{'msgid'});
1.5 www 156: $status.=' trans: '.
1.12 www 157: &Apache::lonnet::put(
158: 'nohist_email',{$contents{'msgid'} => $message{$msgid}});
1.5 www 159: $status.=' del: '.
1.9 albertel 160: &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.5 www 161: &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
162: $ENV{'user.home'},'Received critical message '.
163: $contents{'msgid'}.
164: ', '.$status);
1.12 www 165: return $status;
1.2 www 166: }
167:
168: # ======================================================== Normal communication
169:
170: sub user_normal_msg {
1.7 www 171: my ($user,$domain,$subject,$message,$citation)=@_;
1.2 www 172: # Check if allowed missing
173: my $status='';
174: my $msgid='undefined';
175: unless (($message)&&($user)&&($domain)) { $status='empty'; };
176: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
177: if ($homeserver ne 'no_host') {
178: my $msgid;
1.7 www 179: ($msgid,$message)=&packagemsg($subject,$message,$citation);
1.4 www 180: $status=&Apache::lonnet::critical(
181: 'put:'.$domain.':'.$user.':nohist_email:'.
182: &Apache::lonnet::escape($msgid).'='.
183: &Apache::lonnet::escape($message),$homeserver);
1.2 www 184: } else {
185: $status='no_host';
186: }
187: &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
188: $ENV{'user.home'},
189: 'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
190: return $status;
191: }
192:
1.7 www 193: # =============================================================== Status Change
194:
195: sub statuschange {
196: my ($msgid,$newstatus)=@_;
1.8 albertel 197: my %status=&Apache::lonnet::get('email_status',[$msgid]);
1.7 www 198: if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
199: unless ($status{$msgid}) { $status{$msgid}='new'; }
200: unless (($status{$msgid} eq 'replied') ||
201: ($status{$msgid} eq 'forwarded')) {
1.10 albertel 202: &Apache::lonnet::put('email_status',{$msgid => $newstatus});
1.7 www 203: }
204: }
1.13 ! www 205: # ==================================================== Display Critical Message
1.5 www 206:
1.12 www 207: sub discrit {
208: my $r=shift;
209: $r->print('<h1><font color=red>Critical Messages</font></h1>'.
210: '<form action=/adm/email method=post>'.
211: '<input type=hidden name=confirm value=true>');
212: my %what=&Apache::lonnet::dump('critical');
213: map {
214: my %content=&unpackagemsg($what{$_});
215: $content{'message'}=~s/\n/\<br\>/g;
216: $r->print('<hr>From: <b>'.$content{'sendername'}.'@'.
217: $content{'senderdomain'}.'</b> ('.$content{'time'}.
218: ')<br><blockquote>'.$content{'message'}.'</blockquote>'.
1.13 ! www 219: '<input type=submit name="rec_'.$_.'" value="Confirm Receipt">'.
! 220: '<input type=submit name="reprec_'.$_.'" value="Confirm Receipt and Reply">');
1.12 www 221: } sort keys %what;
222: $r->print('</form>');
223: }
224:
1.13 ! www 225: # =============================================================== Compose reply
! 226:
! 227: sub comprep {
! 228: my ($r,$msgid)=@_;
! 229: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
! 230: my %content=&unpackagemsg($message{$msgid});
! 231: my $quotemsg='> '.$content{'message'};
! 232: $quotemsg=~s/\r/\n/g;
! 233: $quotemsg=~s/\f/\n/g;
! 234: $quotemsg=~s/\n+/\n\> /g;
! 235: my $subject='Re: '.$content{'subject'};
! 236: my $dispcrit='';
! 237: if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
! 238: $dispcrit=
! 239: '<input type=checkbox name=critmsg> Send as critical message<p>';
! 240: }
! 241: $r->print(<<"ENDREPLY");
! 242: <form action="/adm/email" method=post>
! 243: <input type=hidden name=sendreply value="$msgid">
! 244: Subject: <input type=text size=50 name=subject value="$subject"><p>
! 245: <textarea name=message cols=60 rows=10>
! 246: $quotemsg
! 247: </textarea><p>
! 248: $dispcrit
! 249: <input type=submit value="Send Reply">
! 250: </form>
! 251: ENDREPLY
! 252: }
! 253:
! 254: # ===================================================================== Handler
! 255:
1.5 www 256: sub handler {
257: my $r=shift;
258:
259: # ----------------------------------------------------------- Set document type
260:
261: $r->content_type('text/html');
262: $r->send_http_header;
263:
264: return OK if $r->header_only;
265:
1.6 www 266: # --------------------------- Get query string for limited number of parameters
267:
268: map {
269: my ($name, $value) = split(/=/,$_);
270: $value =~ tr/+/ /;
271: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
272: if (($name eq 'display') || ($name eq 'replyto') ||
1.7 www 273: ($name eq 'forward') || ($name eq 'mark') ||
1.12 www 274: ($name eq 'sendreply') || ($name eq 'compose') ||
275: ($name eq 'sendmail') || ($name eq 'critical')) {
1.6 www 276: unless ($ENV{'form.'.$name}) {
277: $ENV{'form.'.$name}=$value;
278: }
279: }
280: } (split(/&/,$ENV{'QUERY_STRING'}));
281:
1.5 www 282: # --------------------------------------------------------------- Render Output
283:
284: $r->print('<html><head><title>EMail and Messaging</title></head>');
1.7 www 285: $r->print(
286: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1.5 www 287: $r->print('<h1>EMail</h1>');
1.6 www 288: if ($ENV{'form.display'}) {
1.7 www 289: my $msgid=$ENV{'form.display'};
290: &statuschange($msgid,'read');
1.8 albertel 291: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
1.7 www 292: my %content=&unpackagemsg($message{$msgid});
293: $r->print('<b>Subject:</b> '.$content{'subject'}.
294: '<br><b>From:</b> '.$content{'sendername'}.' at '.
295: $content{'senderdomain'}.
296: '<br><b>Time:</b> '.$content{'time'}.'<hr>Functions: '.
297: '<a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).
298: '"><b>Reply</b></a><hr><pre>'.
299: $content{'message'}.'</pre><hr>'.$content{'citation'});
1.6 www 300: } elsif ($ENV{'form.replyto'}) {
1.13 ! www 301: &comprep($r,$ENV{'form.replyto'});
1.7 www 302: } elsif ($ENV{'form.sendreply'}) {
303: my $msgid=$ENV{'form.sendreply'};
1.8 albertel 304: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
1.7 www 305: my %content=&unpackagemsg($message{$msgid});
306: &statuschange($msgid,'replied');
1.12 www 307: if (($ENV{'form.critmsg'}) &&
308: (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
309: $r->print('Sending critical: '.
310: &user_crit_msg($content{'sendername'},
1.7 www 311: $content{'senderdomain'},
312: $ENV{'form.subject'},
313: $ENV{'form.message'}));
1.12 www 314: } else {
315: $r->print('Sending: '.&user_normal_msg($content{'sendername'},
316: $content{'senderdomain'},
317: $ENV{'form.subject'},
318: $ENV{'form.message'}));
319: }
320: } elsif ($ENV{'form.confirm'}) {
321: map {
322: if ($_=~/^form\.rec\_(.*)$/) {
323: $r->print('<b>Confirming Receipt:</b> '.
324: &user_crit_received($1).'<br>');
1.13 ! www 325: }
! 326: if ($_=~/^form\.reprec\_(.*)$/) {
! 327: my $msgid=$1;
! 328: $r->print('<b>Confirming Receipt:</b> '.
! 329: &user_crit_received($msgid).'<br>');
! 330: &comprep($r,$msgid);
1.12 www 331: }
332: } keys %ENV;
333: &discrit($r);
334: } elsif ($ENV{'form.critical'}) {
335: &discrit($r);
1.6 www 336: } elsif ($ENV{'form.forward'}) {
337: } elsif ($ENV{'form.mark'}) {
1.11 www 338: } elsif ($ENV{'form.compose'}) {
339: my $dispcrit='';
340: if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
341: $dispcrit=
342: '<input type=checkbox name=critmsg> Send as critical message<p>';
343: }
344: $r->print(<<"ENDCOMP");
345: <form action="/adm/email" method=post>
346: <input type=hidden name=sendmail value=on>
347: Subject: <input type=text size=50 name=subject value=""><p>
348: <textarea name=message cols=60 rows=10>
349: </textarea><p>
350: $dispcrit
351: <input type=submit value="Send Mail">
352: </form>
353: ENDCOMP
354: } elsif ($ENV{'form.sendmail'}) {
1.6 www 355: } else {
356: $r->print('<table border=2><tr><th> </th><th>Date</th>'.
357: '<th>Username</th><th>Domain</th><th>Subject</th><th>Status</th></tr>');
358: map {
359: my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
360: &Apache::lonmsg::unpackmsgid($_);
361: if ($status eq 'new') {
362: $r->print('<tr bgcolor="#FFBB77">');
363: } elsif ($status eq 'read') {
364: $r->print('<tr bgcolor="#BBBB77">');
365: } elsif ($status eq 'replied') {
1.7 www 366: $r->print('<tr bgcolor="#AAAA88">');
1.6 www 367: } else {
368: $r->print('<tr bgcolor="#99BBBB">');
369: }
370: $r->print('<td><a href="/adm/email?display='.$_.
371: '">Open</a></td><td>'.localtime($sendtime).'</td><td>'.
372: $fromname.'</td><td>'.$fromdomain.'</td><td>'.
373: &Apache::lonnet::unescape($shortsubj).'</td><td>'.
374: $status.'</td></tr>');
375: } sort split(/\&/,&Apache::lonnet::reply('keys:'.
376: $ENV{'user.domain'}.':'.
377: $ENV{'user.name'}.':nohist_email',
378: $ENV{'user.home'}));
379: $r->print('</table></body></html>');
380:
381: }
1.5 www 382: $r->print('</body></html>');
383: return OK;
384:
385: }
1.2 www 386: # ================================================= Main program, reset counter
387:
388: sub BEGIN {
389: $msgcount=0;
1.1 www 390: }
391:
392: 1;
393: __END__
394:
395:
396:
397:
398:
399:
400:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>