1: # The LearningOnline Network with CAPA
2: # Routines for messaging
3: #
4: # $Id: lonmsg.pm,v 1.64 2003/08/18 17:37:42 www 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: # (Routines to control the menu
30: #
31: # (TeX Conversion Module
32: #
33: # 05/29/00,05/30 Gerd Kortemeyer)
34: #
35: # 10/05 Gerd Kortemeyer)
36: #
37: # 10/19,10/20,10/30,
38: # 02/06/01 Gerd Kortemeyer
39: # 07/27 Guy Albertelli
40: # 07/27,07/28,07/30,08/03,08/06,08/08,08/09,08/10,8/13,8/15,
41: # 10/1,11/5 Gerd Kortemeyer
42: # YEAR=2002
43: # 1/1,3/18 Gerd Kortemeyer
44: #
45: package Apache::lonmsg;
46:
47: =pod
48:
49: =head1 NAME
50:
51: Apache::lonmsg: supports internal messaging
52:
53: =head1 SYNOPSIS
54:
55: lonmsg provides routines for sending messages, receiving messages, and
56: a handler to allow users to read, send, and delete messages.
57:
58: =head1 OVERVIEW
59:
60: =head2 Messaging Overview
61:
62: X<messages>LON-CAPA provides an internal messaging system similar to
63: email, but customized for LON-CAPA's usage. LON-CAPA implements its
64: own messaging system, rather then building on top of email, because of
65: the features LON-CAPA messages can offer that conventional e-mail can
66: not:
67:
68: =over 4
69:
70: =item * B<Critical messages>: A message the recipient B<must>
71: acknowlegde receipt of before they are allowed to continue using the
72: system, preventing a user from claiming they never got a message
73:
74: =item * B<Receipts>: LON-CAPA can reliably send reciepts informing the
75: sender that it has been read; again, useful for preventing students
76: from claiming they did not see a message. (While conventional e-mail
77: has some reciept support, it's sporadic, e-mail client-specific, and
78: generally the receiver can opt to not send one, making it useless in
79: this case.)
80:
81: =item * B<Context>: LON-CAPA knows about the sender, such as where
82: they are in a course. When a student mails an instructor asking for
83: help on the problem, the instructor receives not just the student's
84: question, but all submissions the student has made up to that point,
85: the user's rendering of the problem, and the complete view the student
86: saw of the resource, including discussion up to that point. Finally,
87: the instructor is reading all of this inside of LON-CAPA, not their
88: email program, so they have full access to LON-CAPA's grading
89: interface, or other features they may wish to use in response to the
90: student's query.
91:
92: =back
93:
94: Users can ask LON-CAPA to forward messages to conventional e-mail
95: addresses on their B<PREF> screen, but generally, LON-CAPA messages
96: are much more useful then traditional email can be made to be, even
97: with HTML support.
98:
99: Right now, this document will cover just how to send a message, since
100: it is likely you will not need to programmatically read messages,
101: since lonmsg already implements that functionality.
102:
103: =head1 FUNCTIONS
104:
105: =over 4
106:
107: =cut
108:
109: use strict;
110: use Apache::lonnet();
111: use vars qw($msgcount);
112: use HTML::TokeParser();
113: use Apache::Constants qw(:common);
114: use Apache::loncommon();
115: use Apache::lontexconvert();
116: use HTML::Entities();
117: use Mail::Send;
118:
119: # ===================================================================== Package
120:
121: sub packagemsg {
122: my ($subject,$message,$citation,$baseurl,$attachmenturl)=@_;
123: $message =&HTML::Entities::encode($message);
124: $citation=&HTML::Entities::encode($citation);
125: $subject =&HTML::Entities::encode($subject);
126: #remove machine specification
127: $baseurl =~ s|^http://[^/]+/|/|;
128: $baseurl =&HTML::Entities::encode($baseurl);
129: #remove machine specification
130: $attachmenturl =~ s|^http://[^/]+/|/|;
131: $attachmenturl =&HTML::Entities::encode($attachmenturl);
132:
133: my $now=time;
134: $msgcount++;
135: my $partsubj=$subject;
136: $partsubj=&Apache::lonnet::escape($partsubj);
137: my $msgid=&Apache::lonnet::escape(
138: $now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
139: $ENV{'user.domain'}.':'.$msgcount.':'.$$);
140: my $result='<sendername>'.$ENV{'user.name'}.'</sendername>'.
141: '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
142: '<subject>'.$subject.'</subject>'.
143: '<time>'.localtime($now).'</time>'.
144: '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
145: '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
146: '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
147: '<browsertype>'.$ENV{'browser.type'}.'</browsertype>'.
148: '<browseros>'.$ENV{'browser.os'}.'</browseros>'.
149: '<browserversion>'.$ENV{'browser.version'}.'</browserversion>'.
150: '<browsermathml>'.$ENV{'browser.mathml'}.'</browsermathml>'.
151: '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
152: '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
153: '<role>'.$ENV{'request.role'}.'</role>'.
154: '<resource>'.$ENV{'request.filename'}.'</resource>'.
155: '<msgid>'.$msgid.'</msgid>'.
156: '<message>'.$message.'</message>';
157: if (defined($citation)) {
158: $result.='<citation>'.$citation.'</citation>';
159: }
160: if (defined($baseurl)) {
161: $result.= '<baseurl>'.$baseurl.'</baseurl>';
162: }
163: if (defined($attachmenturl)) {
164: $result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
165: }
166: return $msgid,$result;
167: }
168:
169: # ================================================== Unpack message into a hash
170:
171: sub unpackagemsg {
172: my ($message,$notoken)=@_;
173: my %content=();
174: my $parser=HTML::TokeParser->new(\$message);
175: my $token;
176: while ($token=$parser->get_token) {
177: if ($token->[0] eq 'S') {
178: my $entry=$token->[1];
179: my $value=$parser->get_text('/'.$entry);
180: $content{$entry}=$value;
181: }
182: }
183: if ($content{'attachmenturl'}) {
184: my ($fname,$ft)=($content{'attachmenturl'}=~/\/(\w+)\.(\w+)$/);
185: if ($notoken) {
186: $content{'message'}.='<p>Attachment: <tt>'.$fname.'.'.$ft.'</tt>';
187: } else {
188: $content{'message'}.='<p>Attachment: <a href="'.
189: &Apache::lonnet::tokenwrapper($content{'attachmenturl'}).
190: '"><tt>'.$fname.'.'.$ft.'</tt></a>';
191: }
192: }
193: return %content;
194: }
195:
196: # ======================================================= Get info out of msgid
197:
198: sub unpackmsgid {
199: my $msgid=&Apache::lonnet::unescape(shift);
200: my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
201: &Apache::lonnet::unescape($msgid));
202: my %status=&Apache::lonnet::get('email_status',[$msgid]);
203: if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
204: unless ($status{$msgid}) { $status{$msgid}='new'; }
205: return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
206: }
207:
208:
209: sub sendemail {
210: my ($to,$subject,$body)=@_;
211: $body=
212: "*** This is an automatic message generated by the LON-CAPA system.\n".
213: "*** Please do not reply to this address.\n\n".$body;
214: my $msg = new Mail::Send;
215: $msg->to($to);
216: $msg->subject('[LON-CAPA] '.$subject);
217: my $fh = $msg->open('smtp',Server => 'localhost');
218: print $fh $body;
219: $fh->close;
220: }
221:
222: # ==================================================== Send notification emails
223:
224: sub sendnotification {
225: my ($to,$touname,$toudom,$subj,$crit)=@_;
226: my $sender=$ENV{'environment.firstname'}.' '.$ENV{'environment.lastname'};
227: my $critical=($crit?' critical':'');
228: my $url='http://'.
229: $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
230: '/adm/email?username='.$touname.'&domain='.$toudom;
231: my $body=(<<ENDMSG);
232: You received a$critical message from $sender in LON-CAPA. The subject is
233:
234: $subj
235:
236: Use
237:
238: $url
239:
240: to access this message.
241: ENDMSG
242: &sendemail($to,'New'.$critical.' message from '.$sender,$body);
243: }
244: # ============================================================= Check for email
245:
246: sub newmail {
247: if ((time-$ENV{'user.mailcheck.time'})>300) {
248: my %what=&Apache::lonnet::get('email_status',['recnewemail']);
249: &Apache::lonnet::appenv('user.mailcheck.time'=>time);
250: if ($what{'recnewemail'}>0) { return 1; }
251: }
252: return 0;
253: }
254:
255: # =============================== Automated message to the author of a resource
256:
257: =pod
258:
259: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
260: of the resource with the URI $filename.
261:
262: =cut
263:
264: sub author_res_msg {
265: my ($filename,$message)=@_;
266: unless ($message) { return 'empty'; }
267: $filename=&Apache::lonnet::declutter($filename);
268: my ($domain,$author,@dummy)=split(/\//,$filename);
269: my $homeserver=&Apache::lonnet::homeserver($author,$domain);
270: if ($homeserver ne 'no_host') {
271: my $id=unpack("%32C*",$message);
272: my $msgid;
273: ($msgid,$message)=&packagemsg($filename,$message);
274: return &Apache::lonnet::reply('put:'.$domain.':'.$author.
275: ':nohist_res_msgs:'.
276: &Apache::lonnet::escape($filename.'_'.$id).'='.
277: &Apache::lonnet::escape($message),$homeserver);
278: }
279: return 'no_host';
280: }
281:
282: # ================================================== Critical message to a user
283:
284: sub user_crit_msg_raw {
285: my ($user,$domain,$subject,$message,$sendback)=@_;
286: # Check if allowed missing
287: my $status='';
288: my $msgid='undefined';
289: unless (($message)&&($user)&&($domain)) { $status='empty'; };
290: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
291: if ($homeserver ne 'no_host') {
292: ($msgid,$message)=&packagemsg($subject,$message);
293: if ($sendback) { $message.='<sendback>true</sendback>'; }
294: $status=&Apache::lonnet::critical(
295: 'put:'.$domain.':'.$user.':critical:'.
296: &Apache::lonnet::escape($msgid).'='.
297: &Apache::lonnet::escape($message),$homeserver);
298: if ($ENV{'request.course.id'}) {
299: &user_normal_msg_raw(
300: $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
301: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
302: 'Critical ['.$user.':'.$domain.']',
303: $message);
304: }
305: } else {
306: $status='no_host';
307: }
308: # Notifications
309: my %userenv = &Apache::lonnet::get('environment',['critnotification'],
310: $domain,$user);
311: if ($userenv{'critnotification'}) {
312: &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1);
313: }
314: # Log this
315: &Apache::lonnet::logthis(
316: 'Sending critical email '.$msgid.
317: ', log status: '.
318: &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
319: $ENV{'user.home'},
320: 'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
321: .$status));
322: return $status;
323: }
324:
325: # New routine that respects "forward" and calls old routine
326:
327: =pod
328:
329: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback)>: Sends
330: a critical message $message to the $user at $domain. If $sendback is true,
331: a reciept will be sent to the current user when $user recieves the message.
332:
333: =cut
334:
335: sub user_crit_msg {
336: my ($user,$domain,$subject,$message,$sendback)=@_;
337: my $status='';
338: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
339: $domain,$user);
340: my $msgforward=$userenv{'msgforward'};
341: if ($msgforward) {
342: foreach (split(/\,/,$msgforward)) {
343: my ($forwuser,$forwdomain)=split(/\:/,$_);
344: $status.=
345: &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
346: $sendback).' ';
347: }
348: } else {
349: $status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback);
350: }
351: return $status;
352: }
353:
354: # =================================================== Critical message received
355:
356: sub user_crit_received {
357: my $msgid=shift;
358: my %message=&Apache::lonnet::get('critical',[$msgid]);
359: my %contents=&unpackagemsg($message{$msgid},1);
360: my $status='rec: '.($contents{'sendback'}?
361: &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
362: 'Receipt: '.$ENV{'user.name'}.' at '.$ENV{'user.domain'},
363: 'User '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
364: ' acknowledged receipt of message'."\n".' "'.
365: $contents{'subject'}.'"'."\n".'dated '.
366: $contents{'time'}.".\n"
367: ):'no msg req');
368: $status.=' trans: '.
369: &Apache::lonnet::put(
370: 'nohist_email',{$contents{'msgid'} => $message{$msgid}});
371: $status.=' del: '.
372: &Apache::lonnet::del('critical',[$contents{'msgid'}]);
373: &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
374: $ENV{'user.home'},'Received critical message '.
375: $contents{'msgid'}.
376: ', '.$status);
377: return $status;
378: }
379:
380: # ======================================================== Normal communication
381:
382: sub user_normal_msg_raw {
383: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
384: # Check if allowed missing
385: my $status='';
386: my $msgid='undefined';
387: unless (($message)&&($user)&&($domain)) { $status='empty'; };
388: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
389: if ($homeserver ne 'no_host') {
390: ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl,
391: $attachmenturl);
392: $status=&Apache::lonnet::critical(
393: 'put:'.$domain.':'.$user.':nohist_email:'.
394: &Apache::lonnet::escape($msgid).'='.
395: &Apache::lonnet::escape($message),$homeserver);
396: &Apache::lonnet::put
397: ('email_status',{'recnewemail'=>time},$domain,$user);
398: } else {
399: $status='no_host';
400: }
401: # Notifications
402: my %userenv = &Apache::lonnet::get('environment',['notification'],
403: $domain,$user);
404: if ($userenv{'notification'}) {
405: &sendnotification($userenv{'notification'},$user,$domain,$subject,0);
406: }
407: &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
408: $ENV{'user.home'},
409: 'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
410: return $status;
411: }
412:
413: # New routine that respects "forward" and calls old routine
414:
415: =pod
416:
417: =item * B<user_normal_msg($user, $domain, $subject, $message,
418: $citation, $baseurl, $attachmenturl)>: Sends a message to the
419: $user at $domain, with subject $subject and message $message.
420:
421: =cut
422:
423: sub user_normal_msg {
424: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
425: my $status='';
426: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
427: $domain,$user);
428: my $msgforward=$userenv{'msgforward'};
429: if ($msgforward) {
430: foreach (split(/\,/,$msgforward)) {
431: my ($forwuser,$forwdomain)=split(/\:/,$_);
432: $status.=
433: &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
434: $citation,$baseurl,$attachmenturl).' ';
435: }
436: } else {
437: $status=&user_normal_msg_raw($user,$domain,$subject,$message,
438: $citation,$baseurl,$attachmenturl);
439: }
440: return $status;
441: }
442:
443:
444: # =============================================================== Status Change
445:
446: sub statuschange {
447: my ($msgid,$newstatus)=@_;
448: my %status=&Apache::lonnet::get('email_status',[$msgid]);
449: if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
450: unless ($status{$msgid}) { $status{$msgid}='new'; }
451: unless (($status{$msgid} eq 'replied') ||
452: ($status{$msgid} eq 'forwarded')) {
453: &Apache::lonnet::put('email_status',{$msgid => $newstatus});
454: }
455: if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
456: &Apache::lonnet::put('email_status',{$msgid => $newstatus});
457: }
458: }
459:
460: # ======================================================= Display a course list
461:
462: sub discourse {
463: my $r=shift;
464: my %courselist=&Apache::lonnet::dump(
465: 'classlist',
466: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
467: $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
468: my $now=time;
469: $r->print(<<ENDDISHEADER);
470: <input type=hidden name=sendmode value=group>
471: <script>
472: function checkall() {
473: for (i=0; i<document.forms.compemail.elements.length; i++) {
474: if
475: (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
476: document.forms.compemail.elements[i].checked=true;
477: }
478: }
479: }
480:
481: function checksec() {
482: for (i=0; i<document.forms.compemail.elements.length; i++) {
483: if
484: (document.forms.compemail.elements[i].name.indexOf
485: ('send_to_&&&'+document.forms.compemail.chksec.value)==0) {
486: document.forms.compemail.elements[i].checked=true;
487: }
488: }
489: }
490:
491: function uncheckall() {
492: for (i=0; i<document.forms.compemail.elements.length; i++) {
493: if
494: (document.forms.compemail.elements[i].name.indexOf('send_to_')==0) {
495: document.forms.compemail.elements[i].checked=false;
496: }
497: }
498: }
499: </script>
500: <input type=button onClick="checkall()" value="Check for All">
501: <input type=button onClick="checksec()" value="Check for Section/Group">
502: <input type=text size=5 name=chksec>
503: <input type=button onClick="uncheckall()" value="Check for None">
504: <p>
505: ENDDISHEADER
506: my %coursepersonnel=
507: &Apache::lonnet::get_course_adv_roles();
508: foreach my $role (sort keys %coursepersonnel) {
509: foreach (split(/\,/,$coursepersonnel{$role})) {
510: my ($puname,$pudom)=split(/\:/,$_);
511: $r->print(
512: '<br /><input type="checkbox" name="send_to_&&&&&&_'.
513: $puname.':'.$pudom.'" /> '.
514: &Apache::loncommon::plainname($puname,
515: $pudom).' ('.$_.'), <i>'.$role.'</i>');
516: }
517: }
518:
519: foreach (sort keys %courselist) {
520: my ($end,$start)=split(/\:/,$courselist{$_});
521: my $active=1;
522: if (($end) && ($now>$end)) { $active=0; }
523: if ($active) {
524: my ($sname,$sdom)=split(/\:/,$_);
525: my %reply=&Apache::lonnet::get('environment',
526: ['firstname','middlename','lastname','generation'],
527: $sdom,$sname);
528: my $section=&Apache::lonnet::usection
529: ($sdom,$sname,$ENV{'request.course.id'});
530: $r->print(
531: '<br><input type=checkbox name="send_to_&&&'.$section.'&&&_'.$_.'"> '.
532: $reply{'firstname'}.' '.
533: $reply{'middlename'}.' '.
534: $reply{'lastname'}.' '.
535: $reply{'generation'}.
536: ' ('.$_.') '.$section);
537: }
538: }
539: }
540:
541: # ==================================================== Display Critical Message
542:
543: sub discrit {
544: my $r=shift;
545: my $header = '<h1><font color=red>Critical Messages</font></h1>'.
546: '<form action=/adm/email method=post>'.
547: '<input type=hidden name=confirm value=true>';
548: my %what=&Apache::lonnet::dump('critical');
549: my $result = '';
550: foreach (sort keys %what) {
551: my %content=&unpackagemsg($what{$_});
552: next if ($content{'senderdomain'} eq '');
553: $content{'message'}=~s/\n/\<br\>/g;
554: $result.='<hr>From: <b>'.
555: &Apache::loncommon::aboutmewrapper(
556: &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
557: $content{'sendername'}.'@'.
558: $content{'senderdomain'}.') '.$content{'time'}.
559: '<br>Subject: '.$content{'subject'}.
560: '<br><blockquote>'.
561: &Apache::lontexconvert::msgtexconverted($content{'message'}).
562: '</blockquote>'.
563: '<input type=submit name="rec_'.$_.'" value="Confirm Receipt">'.
564: '<input type=submit name="reprec_'.$_.'" '.
565: 'value="Confirm Receipt and Reply">';
566: }
567: # Check to see if there were any messages.
568: if ($result eq '') {
569: $result = "<h2>You have no critical messages.</h2>".
570: '<a href="/adm/roles">Select a course</a>';
571: } else {
572: $r->print($header);
573: }
574: $r->print($result);
575: $r->print('<input type=hidden name="displayedcrit" value="true"></form>');
576: }
577:
578: # =============================================================== Compose reply
579:
580: sub comprep {
581: my ($r,$msgid)=@_;
582: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
583: my %content=&unpackagemsg($message{$msgid},1);
584: my $quotemsg='> '.$content{'message'};
585: $quotemsg=~s/\r/\n/g;
586: $quotemsg=~s/\f/\n/g;
587: $quotemsg=~s/\n+/\n\> /g;
588: my $torepl=&Apache::loncommon::aboutmewrapper(
589: &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
590: $content{'sendername'}.'@'.
591: $content{'senderdomain'}.')';
592: my $subject='Re: '.$content{'subject'};
593: my $dispcrit='';
594: if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
595: my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
596: $dispcrit=
597: '<input type=checkbox name=critmsg> Send as critical message ' . $crithelp .
598: '<br>'.
599: '<input type=checkbox name=sendbck> Send as critical message ' .
600: ' and return receipt' . $crithelp . '<p>';
601: }
602: $r->print(<<"ENDREPLY");
603: <form action="/adm/email" method=post>
604: <input type=hidden name=sendreply value="$msgid">
605: To: $torepl<br />
606: Subject: <input type=text size=50 name=subject value="$subject"><p>
607: <textarea name=message cols=84 rows=10 wrap=hard>
608: $quotemsg
609: </textarea><p>
610: $dispcrit
611: <input type=submit value="Send Reply">
612: </form>
613: ENDREPLY
614: }
615:
616: # ======================================================== Display all messages
617:
618: sub disall {
619: my $r=shift;
620: $r->print(<<ENDDISHEADER);
621: <script>
622: function checkall() {
623: for (i=0; i<document.forms.disall.elements.length; i++) {
624: if
625: (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
626: document.forms.disall.elements[i].checked=true;
627: }
628: }
629: }
630:
631: function uncheckall() {
632: for (i=0; i<document.forms.disall.elements.length; i++) {
633: if
634: (document.forms.disall.elements[i].name.indexOf('delmark_')==0) {
635: document.forms.disall.elements[i].checked=false;
636: }
637: }
638: }
639: </script>
640: ENDDISHEADER
641: $r->print('<h1>Display All Messages</h1><form method=post name=disall '.
642: 'action="/adm/email">'.
643: '<table border=2><tr><th colspan=2> </th><th>');
644: if ($ENV{'form.sortedby'} eq "revdate") {
645: $r->print('<a href = "?sortedby=date">Date</a></th>');
646: } else {
647: $r->print('<a href = "?sortedby=revdate">Date</a></th>');
648: }
649: $r->print('<th>');
650: if ($ENV{'form.sortedby'} eq "revuser") {
651: $r->print('<a href = "?sortedby=user">Username</a>');
652: } else {
653: $r->print('<a href = "?sortedby=revuser">Username</a>');
654: }
655: $r->print('</th><th>');
656: if ($ENV{'form.sortedby'} eq "revdomain") {
657: $r->print('<a href = "?sortedby=domain">Domain</a>');
658: } else {
659: $r->print('<a href = "?sortedby=revdomain">Domain</a>');
660: }
661: $r->print('</th><th>');
662: if ($ENV{'form.sortedby'} eq "revsubject") {
663: $r->print('<a href = "?sortedby=subject">Subject</a>');
664: } else {
665: $r->print('<a href = "?sortedby=revsubject">Subject</a>');
666: }
667: $r->print('</th><th>');
668: if ($ENV{'form.sortedby'} eq "revstatus") {
669: $r->print('<a href = "?sortedby=status">Status</th>');
670: } else {
671: $r->print('<a href = "?sortedby=revstatus">Status</th>');
672: }
673: $r->print('</tr>');
674: my @messages = split(/\&/,&Apache::lonnet::reply('keys:'.$ENV{'user.domain'}.':'.$ENV{'user.name'}.':nohist_email',$ENV{'user.home'}));
675: #unpack the varibles and repack into temp for sorting
676: my @temp;
677: foreach (@messages) {
678: my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
679: &Apache::lonmsg::unpackmsgid($_);
680: my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$_);
681: push @temp ,\@temp1;
682: }
683: #default sort
684: @temp = sort {$a->[0] <=> $b->[0]} @temp;
685: if ($ENV{'form.sortedby'} eq "date"){
686: @temp = sort {$a->[0] <=> $b->[0]} @temp;
687: }
688: if ($ENV{'form.sortedby'} eq "revdate"){
689: @temp = sort {$b->[0] <=> $a->[0]} @temp;
690: }
691: if ($ENV{'form.sortedby'} eq "user"){
692: @temp = sort {lc($a->[2]) cmp lc($b->[2])} @temp;
693: }
694: if ($ENV{'form.sortedby'} eq "revuser"){
695: @temp = sort {lc($b->[2]) cmp lc($a->[2])} @temp;
696: }
697: if ($ENV{'form.sortedby'} eq "domain"){
698: @temp = sort {$a->[3] cmp $b->[3]} @temp;
699: }
700: if ($ENV{'form.sortedby'} eq "revdomain"){
701: @temp = sort {$b->[3] cmp $a->[3]} @temp;
702: }
703: if ($ENV{'form.sortedby'} eq "subject"){
704: @temp = sort {lc($a->[1]) cmp lc($b->[1])} @temp;
705: }
706: if ($ENV{'form.sortedby'} eq "revsubject"){
707: @temp = sort {lc($b->[1]) cmp lc($a->[1])} @temp;
708: }
709: if ($ENV{'form.sortedby'} eq "status"){
710: @temp = sort {$a->[4] cmp $b->[4]} @temp;
711: }
712: if ($ENV{'form.sortedby'} eq "revstatus"){
713: @temp = sort {$b->[4] cmp $a->[4]} @temp;
714: }
715: foreach (@temp){
716: my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID)= @$_;
717: if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) {
718: if ($status eq 'new') {
719: $r->print('<tr bgcolor="#FFBB77">');
720: } elsif ($status eq 'read') {
721: $r->print('<tr bgcolor="#BBBB77">');
722: } elsif ($status eq 'replied') {
723: $r->print('<tr bgcolor="#AAAA88">');
724: } else {
725: $r->print('<tr bgcolor="#99BBBB">');
726: }
727: $r->print('<td><a href="/adm/email?display='.$origID.
728: '">Open</a></td><td><a href="/adm/email?markdel='.$origID.
729: '">Delete</a><input type=checkbox name="delmark_'.$origID.'"></td>'.
730: '<td>'.localtime($sendtime).'</td><td>'.
731: $fromname.'</td><td>'.$fromdomain.'</td><td>'.
732: &Apache::lonnet::unescape($shortsubj).'</td><td>'.
733: $status.'</td></tr>');
734: }
735: }
736: $r->print('</table><p>'.
737: '<a href="javascript:checkall()">Check All</a> '.
738: '<a href="javascript:uncheckall()">Uncheck All</a><p>'.
739: '<input type=submit name="markeddel" value="Delete Checked">'.
740: '</form></body></html>');
741: }
742:
743: # ============================================================== Compose output
744:
745: sub compout {
746: my ($r,$forwarding,$broadcast)=@_;
747: my $dispcrit='';
748: my $dissub='';
749: my $dismsg='';
750: my $func='Send New';
751: if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
752: my $crithelp = Apache::loncommon::help_open_topic("Course_Critical_Message");
753: $dispcrit=
754: '<input type=checkbox name=critmsg> Send as critical message ' . $crithelp .
755: '<br>'.
756: '<input type=checkbox name=sendbck> Send as critical message ' .
757: ' and return receipt' . $crithelp . '<p>';
758: }
759: if ($forwarding) {
760: $dispcrit.='<input type=hidden name=forwid value="'.
761: $forwarding.'">';
762: $func='Forward';
763: my %message=&Apache::lonnet::get('nohist_email',[$forwarding]);
764: my %content=&unpackagemsg($message{$forwarding});
765:
766: $dissub='Forwarding: '.$content{'subject'};
767: $dismsg='Forwarded message from '.
768: $content{'sendername'}.' at '.$content{'senderdomain'};
769: }
770: my $defdom=$ENV{'user.domain'};
771: if ($ENV{'form.recdom'}) { $defdom=$ENV{'form.recdom'}; }
772: $r->print(
773: '<form action="/adm/email" name="compemail" method="post"'.
774: ' enctype="multipart/form-data">'."\n".
775: '<input type="hidden" name="sendmail" value="on">'."\n".
776: '<table>');
777: unless (($broadcast eq 'group') || ($broadcast eq 'upload')) {
778: my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
779: my $selectlink=&Apache::loncommon::selectstudent_link
780: ('compemail','recuname','recdomain');
781: $r->print(<<"ENDREC");
782: <table>
783: <tr><td>Username:</td><td><input type=text size=12 name=recuname value="$ENV{'form.recname'}"></td><td rowspan="2">$selectlink</td></tr>
784: <tr><td>Domain:</td>
785: <td>$domform</td></tr>
786: ENDREC
787: }
788: my $latexHelp = Apache::loncommon::helpLatexCheatsheet();
789: if ($broadcast ne 'upload') {
790: $r->print(<<"ENDCOMP");
791: <tr><td>Additional Recipients<br><tt>username\@domain,username\@domain, ...
792: </tt></td><td>
793: <input type=text size=50 name=additionalrec></td></tr>
794: <tr><td>Subject:</td><td><input type=text size=50 name=subject value="$dissub">
795: </td></tr></table>
796: $latexHelp
797: <textarea name=message cols=80 rows=10 wrap=hard>$dismsg
798: </textarea><p>
799: $dispcrit
800: <input type=submit value="$func Mail">
801: ENDCOMP
802: } else { # $broadcast is 'upload'
803: $r->print(<<ENDUPLOAD);
804: <input type=hidden name=sendmode value=upload>
805: <h3>Generate messages from a file</h3>
806: <p>
807: Subject: <input type=text size=50 name=subject>
808: </p>
809: <p>General message text<br />
810: <textarea name=message cols=60 rows=10 wrap=hard>$dismsg
811: </textarea></p>
812: <p>
813: The file format for the uploaded portion of the message is:
814: <pre>
815: username1\@domain1: text
816: username2\@domain2: text
817: username3\@domain1: text
818: </pre>
819: </p>
820: <p>
821: The messages will be assembled from all lines with the respective
822: <tt>username\@domain</tt>, and appended to the general message text.</p>
823: <p>
824: <input type=file name=upfile size=20><p>
825: $dispcrit
826: <input type=submit value="Upload and send">
827: ENDUPLOAD
828: }
829: if ($broadcast eq 'group') {
830: &discourse;
831: }
832: $r->print('</form>');
833: }
834:
835: # ---------------------------------------------------- Display all face to face
836:
837: sub disfacetoface {
838: my ($r,$user,$domain)=@_;
839: unless ($ENV{'request.course.id'}) { return; }
840: unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
841: return;
842: }
843: my %records=&Apache::lonnet::dump('nohist_email',
844: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
845: $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
846: '%255b'.$user.'%253a'.$domain.'%255d');
847: my $result='';
848: foreach (sort keys %records) {
849: my %content=&unpackagemsg($records{$_});
850: next if ($content{'senderdomain'} eq '');
851: $content{'message'}=~s/\n/\<br\>/g;
852: if ($content{'subject'}=~/^Record/) {
853: $result.='<h3>Record</h3>';
854: } else {
855: $result.='<h3>Sent Message</h3>';
856: %content=&unpackagemsg($content{'message'});
857: $content{'message'}=
858: '<b>Subject: '.$content{'subject'}.'</b><br />'.
859: $content{'message'};
860: }
861: $result.='By: <b>'.
862: &Apache::loncommon::aboutmewrapper(
863: &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).'</b> ('.
864: $content{'sendername'}.'@'.
865: $content{'senderdomain'}.') '.$content{'time'}.
866: '<br><blockquote>'.
867: &Apache::lontexconvert::msgtexconverted($content{'message'}).
868: '</blockquote>';
869: }
870: # Check to see if there were any messages.
871: if ($result eq '') {
872: $r->print("<p><b>No notes, face-to-face discussion records, or critical messages in this course.</b></p>");
873: } else {
874: $r->print($result);
875: }
876: }
877:
878: # ---------------------------------------------------------------- Face to face
879:
880: sub facetoface {
881: my ($r,$stage)=@_;
882: unless (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
883: return;
884: }
885: # from query string
886: if ($ENV{'form.recname'}) { $ENV{'form.recuname'}=$ENV{'form.recname'}; }
887: if ($ENV{'form.recdom'}) { $ENV{'form.recdomain'}=$ENV{'form.recdom'}; }
888:
889: my $defdom=$ENV{'user.domain'};
890: # already filled in
891: if ($ENV{'form.recdomain'}) { $defdom=$ENV{'form.recdomain'}; }
892: # generate output
893: my $domform = &Apache::loncommon::select_dom_form($defdom,'recdomain');
894: my $stdbrws = &Apache::loncommon::selectstudent_link
895: ('stdselect','recuname','recdomain');
896: $r->print(<<"ENDTREC");
897: <h3>User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course</h3>
898: <form method="post" action="/adm/email" name="stdselect">
899: <input type="hidden" name="recordftf" value="retrieve" />
900: <table>
901: <tr><td>Username:</td><td><input type=text size=12 name=recuname value="$ENV{'form.recuname'}"></td>
902: <td rowspan="2">
903: $stdbrws
904: <input type="submit" value="Retrieve discussion and message records"></td>
905: </tr>
906: <tr><td>Domain:</td>
907: <td>$domform</td></tr>
908: </table>
909: </form>
910: ENDTREC
911: if (($stage ne 'query') &&
912: ($ENV{'form.recdomain'}) && ($ENV{'form.recuname'})) {
913: chomp($ENV{'form.newrecord'});
914: if ($ENV{'form.newrecord'}) {
915: &user_normal_msg_raw(
916: $ENV{'course.'.$ENV{'request.course.id'}.'.num'},
917: $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
918: 'Record ['.$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}.']',
919: $ENV{'form.newrecord'});
920: }
921: $r->print('<h3>'.&Apache::loncommon::plainname($ENV{'form.recuname'},
922: $ENV{'form.recdomain'}).'</h3>');
923: &disfacetoface($r,$ENV{'form.recuname'},$ENV{'form.recdomain'});
924: $r->print(<<ENDRHEAD);
925: <form method="post" action="/adm/email">
926: <input name="recdomain" value="$ENV{'form.recdomain'}" type="hidden" />
927: <input name="recuname" value="$ENV{'form.recuname'}" type="hidden" />
928: ENDRHEAD
929: $r->print(<<ENDBFORM);
930: <hr />New Record (record is visible to course faculty and staff)<br />
931: <textarea name="newrecord" cols="80" rows="10" wrap="hard"></textarea>
932: <br />
933: <input type="hidden" name="recordftf" value="post" />
934: <input type="submit" value="Post this record" />
935: </form>
936: ENDBFORM
937: }
938: }
939:
940: # ===================================================================== Handler
941:
942: sub handler {
943: my $r=shift;
944:
945: # ----------------------------------------------------------- Set document type
946:
947: $r->content_type('text/html');
948: $r->send_http_header;
949:
950: return OK if $r->header_only;
951:
952: # --------------------------- Get query string for limited number of parameters
953: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
954: ['display','replyto','forward','markread','markdel','markunread',
955: 'sendreply','compose','sendmail','critical','recname','recdom',
956: 'recordftf','sortedby']);
957:
958: # ------------------------------------------------------ They checked for email
959: &Apache::lonnet::put('email_status',{'recnewemail'=>0});
960: # --------------------------------------------------------------- Render Output
961: if (!$ENV{'form.display'}) {
962: $r->print('<html><head><title>EMail and Messaging</title>'.
963: &Apache::loncommon::studentbrowser_javascript().'</head>'.
964: &Apache::loncommon::bodytag('EMail and Messages'));
965: }
966: if ($ENV{'form.display'}) {
967: my $msgid=$ENV{'form.display'};
968: &statuschange($msgid,'read');
969: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
970: my %content=&unpackagemsg($message{$msgid});
971: $r->print('<html><head><title>EMail and Messaging</title>');
972: if (defined($content{'baseurl'})) {
973: $r->print("<base href=\"http://$ENV{'SERVER_NAME'}/$content{'baseurl'}\" />");
974: }
975: $r->print(&Apache::loncommon::studentbrowser_javascript().
976: '</head>'.
977: &Apache::loncommon::bodytag('EMail and Messages'));
978: $r->print('<b>Subject:</b> '.$content{'subject'}.
979: '<br><b>From:</b> '.
980: &Apache::loncommon::aboutmewrapper(
981: &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),
982: $content{'sendername'},$content{'senderdomain'}).' ('.
983: $content{'sendername'}.' at '.
984: $content{'senderdomain'}.') '.
985: '<br><b>Time:</b> '.$content{'time'}.'<p>'.
986: '<table border=2><tr bgcolor="#FFFFAA"><td>Functions:</td>'.
987: '<td><a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).
988: '"><b>Reply</b></a></td>'.
989: '<td><a href="/adm/email?forward='.&Apache::lonnet::escape($msgid).
990: '"><b>Forward</b></a></td>'.
991: '<td><a href="/adm/email?markunread='.&Apache::lonnet::escape($msgid).
992: '"><b>Mark Unread</b></a></td>'.
993: '<td><a href="/adm/email?markdel='.&Apache::lonnet::escape($msgid).
994: '"><b>Delete</b></a></td>'.
995: '<td><a href="/adm/email"><b>Display all Messages</b></a></td>'.
996: '</tr></table><p><pre>'.
997: &Apache::lontexconvert::msgtexconverted($content{'message'}).
998: '</pre><hr>'.$content{'citation'});
999: } elsif ($ENV{'form.replyto'}) {
1000: &comprep($r,$ENV{'form.replyto'});
1001: } elsif ($ENV{'form.sendreply'}) {
1002: my $msgid=$ENV{'form.sendreply'};
1003: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
1004: my %content=&unpackagemsg($message{$msgid},1);
1005: &statuschange($msgid,'replied');
1006: if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) &&
1007: (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
1008: $r->print('Sending critical: '.
1009: &user_crit_msg($content{'sendername'},
1010: $content{'senderdomain'},
1011: &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
1012: &Apache::lonfeedback::clear_out_html($ENV{'form.message'}),
1013: $ENV{'form.sendbck'}));
1014: } else {
1015: $r->print('Sending: '.&user_normal_msg($content{'sendername'},
1016: $content{'senderdomain'},
1017: &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
1018: &Apache::lonfeedback::clear_out_html($ENV{'form.message'})));
1019: }
1020: if ($ENV{'form.displayedcrit'}) {
1021: &discrit($r);
1022: } else {
1023: &disall($r);
1024: }
1025: } elsif ($ENV{'form.confirm'}) {
1026: foreach (keys %ENV) {
1027: if ($_=~/^form\.rec\_(.*)$/) {
1028: $r->print('<b>Confirming Receipt:</b> '.
1029: &user_crit_received($1).'<br>');
1030: }
1031: if ($_=~/^form\.reprec\_(.*)$/) {
1032: my $msgid=$1;
1033: $r->print('<b>Confirming Receipt:</b> '.
1034: &user_crit_received($msgid).'<br>');
1035: &comprep($r,$msgid);
1036: }
1037: }
1038: &discrit($r);
1039: } elsif ($ENV{'form.critical'}) {
1040: &discrit($r);
1041: } elsif ($ENV{'form.forward'}) {
1042: &compout($r,$ENV{'form.forward'});
1043: } elsif ($ENV{'form.markread'}) {
1044: } elsif ($ENV{'form.markdel'}) {
1045: &statuschange($ENV{'form.markdel'},'deleted');
1046: &disall($r);
1047: } elsif ($ENV{'form.markeddel'}) {
1048: my $total=0;
1049: foreach (keys %ENV) {
1050: if ($_=~/^form\.delmark_(.*)$/) {
1051: &statuschange(&Apache::lonnet::unescape($1),'deleted');
1052: $total++;
1053: }
1054: }
1055: $r->print('Deleted '.$total.' message(s)<p>');
1056: &disall($r);
1057: } elsif ($ENV{'form.markunread'}) {
1058: &statuschange($ENV{'form.markunread'},'new');
1059: &disall($r);
1060: } elsif ($ENV{'form.compose'}) {
1061: &compout($r,'',$ENV{'form.compose'});
1062: } elsif ($ENV{'form.recordftf'}) {
1063: &facetoface($r,$ENV{'form.recordftf'});
1064: } elsif ($ENV{'form.sendmail'}) {
1065: my %content=();
1066: undef %content;
1067: if ($ENV{'form.forwid'}) {
1068: my $msgid=$ENV{'form.forwid'};
1069: my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
1070: %content=&unpackagemsg($message{$msgid},1);
1071: &statuschange($msgid,'forwarded');
1072: $ENV{'form.message'}.="\n\n-- Forwarded message --\n\n".
1073: $content{'message'};
1074: }
1075: my %toaddr=();
1076: undef %toaddr;
1077: if ($ENV{'form.sendmode'} eq 'group') {
1078: foreach (keys %ENV) {
1079: if ($_=~/^form\.send\_to\_\&\&\&[^\&]*\&\&\&\_(.+)$/) {
1080: $toaddr{$1}='';
1081: }
1082: }
1083: } elsif ($ENV{'form.sendmode'} eq 'upload') {
1084: foreach (split(/[\n\r\f]+/,$ENV{'form.upfile'})) {
1085: my ($rec,$txt)=split(/\s*\:\s*/,$_);
1086: if ($txt) {
1087: $rec=~s/\@/\:/;
1088: $toaddr{$rec}.=$txt."\n";
1089: }
1090: }
1091: } else {
1092: $toaddr{$ENV{'form.recuname'}.':'.$ENV{'form.recdomain'}}='';
1093: }
1094: if ($ENV{'form.additionalrec'}) {
1095: foreach (split(/\,/,$ENV{'form.additionalrec'})) {
1096: my ($auname,$audom)=split(/\@/,$_);
1097: $toaddr{$auname.':'.$audom}='';
1098: }
1099: }
1100: foreach (keys %toaddr) {
1101: my ($recuname,$recdomain)=split(/\:/,$_);
1102: my $msgtxt=&Apache::lonfeedback::clear_out_html($ENV{'form.message'});
1103: if ($toaddr{$_}) { $msgtxt.='<hr>'.$toaddr{$_}; }
1104: if ((($ENV{'form.critmsg'}) || ($ENV{'form.sendbck'})) &&
1105: (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))) {
1106: $r->print('Sending critical: '.
1107: &user_crit_msg($recuname,$recdomain,
1108: &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
1109: $msgtxt,
1110: $ENV{'form.sendbck'}));
1111: } else {
1112: $r->print('Sending: '.&user_normal_msg($recuname,$recdomain,
1113: &Apache::lonfeedback::clear_out_html($ENV{'form.subject'}),
1114: $msgtxt,
1115: $content{'citation'}));
1116: }
1117: $r->print('<br>');
1118: }
1119: if ($ENV{'form.displayedcrit'}) {
1120: &discrit($r);
1121: } else {
1122: &disall($r);
1123: }
1124: } else {
1125: &disall($r);
1126: }
1127: $r->print('</body></html>');
1128: return OK;
1129:
1130: }
1131: # ================================================= Main program, reset counter
1132:
1133: BEGIN {
1134: $msgcount=0;
1135: }
1136:
1137: =pod
1138:
1139: =back
1140:
1141: =cut
1142:
1143: 1;
1144:
1145: __END__
1146:
1147:
1148:
1149:
1150:
1151:
1152:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>