1: # The LearningOnline Network with CAPA
2: # Routines for messaging
3: #
4: # $Id: lonmsg.pm,v 1.181 2006/04/11 14:17:23 albertel 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::lonmsg;
30:
31: use strict;
32: use Apache::lonnet;
33: use HTML::TokeParser();
34: use Apache::lonlocal;
35: use Mail::Send;
36:
37: {
38: my $uniq;
39: sub get_uniq {
40: $uniq++;
41: return $uniq;
42: }
43: }
44:
45: # ===================================================================== Package
46:
47: sub packagemsg {
48: my ($subject,$message,$citation,$baseurl,$attachmenturl,
49: $recuser,$recdomain,$msgid,$type,$crsmsgid)=@_;
50: $message =&HTML::Entities::encode($message,'<>&"');
51: $citation=&HTML::Entities::encode($citation,'<>&"');
52: $subject =&HTML::Entities::encode($subject,'<>&"');
53: #remove machine specification
54: $baseurl =~ s|^http://[^/]+/|/|;
55: $baseurl =&HTML::Entities::encode($baseurl,'<>&"');
56: #remove machine specification
57: $attachmenturl =~ s|^http://[^/]+/|/|;
58: $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
59: my $course_context;
60: if (defined($env{'form.replyid'})) {
61: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid)=
62: split(/\:/,&Apache::lonnet::unescape($env{'form.replyid'}));
63: $course_context = $origcid;
64: }
65: foreach my $key (keys(%env)) {
66: if ($key=~/^form\.(rep)?rec\_(.*)$/) {
67: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) =
68: split(/\:/,&Apache::lonnet::unescape($2));
69: $course_context = $origcid;
70: last;
71: }
72: }
73: unless(defined($course_context)) {
74: $course_context = $env{'request.course.id'};
75: }
76: my $now=time;
77: my $msgcount = &get_uniq();
78: unless(defined($msgid)) {
79: $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},
80: $msgcount,$course_context,$$);
81: }
82: my $result = '<sendername>'.$env{'user.name'}.'</sendername>'.
83: '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
84: '<subject>'.$subject.'</subject>'.
85: '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>';
86: if (defined($crsmsgid)) {
87: $result.= '<courseid>'.$course_context.'</courseid>'.
88: '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
89: '<msgid>'.$msgid.'</msgid>'.
90: '<coursemsgid>'.$crsmsgid.'</coursemsgid>'.
91: '<message>'.$message.'</message>';
92: return ($msgid,$result);
93: }
94: $result .= '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
95: '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
96: '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
97: '<browsertype>'.$env{'browser.type'}.'</browsertype>'.
98: '<browseros>'.$env{'browser.os'}.'</browseros>'.
99: '<browserversion>'.$env{'browser.version'}.'</browserversion>'.
100: '<browsermathml>'.$env{'browser.mathml'}.'</browsermathml>'.
101: '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
102: '<courseid>'.$course_context.'</courseid>'.
103: '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
104: '<role>'.$env{'request.role'}.'</role>'.
105: '<resource>'.$env{'request.filename'}.'</resource>'.
106: '<msgid>'.$msgid.'</msgid>';
107: if (ref($recuser) eq 'ARRAY') {
108: for (my $i=0; $i<@{$recuser}; $i++) {
109: if ($type eq 'dcmail') {
110: my ($username,$email) = split(/:/,$$recuser[$i]);
111: $username = &Apache::lonnet::unescape($username);
112: $email = &Apache::lonnet::unescape($email);
113: $username = &HTML::Entities::encode($username,'<>&"');
114: $email = &HTML::Entities::encode($email,'<>&"');
115: $result .= '<recipient username="'.$username.'">'.
116: $email.'</recipient>';
117: } else {
118: $result .= '<recuser>'.$$recuser[$i].'</recuser>'.
119: '<recdomain>'.$$recdomain[$i].'</recdomain>';
120: }
121: }
122: } else {
123: $result .= '<recuser>'.$recuser.'</recuser>'.
124: '<recdomain>'.$recdomain.'</recdomain>';
125: }
126: $result .= '<message>'.$message.'</message>';
127: if (defined($citation)) {
128: $result.='<citation>'.$citation.'</citation>';
129: }
130: if (defined($baseurl)) {
131: $result.= '<baseurl>'.$baseurl.'</baseurl>';
132: }
133: if (defined($attachmenturl)) {
134: $result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
135: }
136: return $msgid,$result;
137: }
138:
139: # ================================================== Unpack message into a hash
140:
141: sub unpackagemsg {
142: my ($message,$notoken)=@_;
143: my %content=();
144: my $parser=HTML::TokeParser->new(\$message);
145: my $token;
146: while ($token=$parser->get_token) {
147: if ($token->[0] eq 'S') {
148: my $entry=$token->[1];
149: my $value=$parser->get_text('/'.$entry);
150: if (($entry eq 'recuser') || ($entry eq 'recdomain')) {
151: push(@{$content{$entry}},$value);
152: } elsif ($entry eq 'recipient') {
153: my $username = $token->[2]{'username'};
154: $username = &HTML::Entities::decode($username,'<>&"');
155: $content{$entry}{$username} = $value;
156: } else {
157: $content{$entry}=$value;
158: }
159: }
160: }
161: if (!exists($content{'recuser'})) { $content{'recuser'} = []; }
162: if ($content{'attachmenturl'}) {
163: my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
164: if ($notoken) {
165: $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';
166: } else {
167: &Apache::lonnet::allowuploaded('/adm/msg',
168: $content{'attachmenturl'});
169: $content{'message'}.='<p>'.&mt('Attachment').
170: ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
171: $fname.'</tt></a>';
172: }
173: }
174: return %content;
175: }
176:
177: # ======================================================= Get info out of msgid
178:
179: sub buildmsgid {
180: my ($now,$subject,$uname,$udom,$msgcount,$course_context,$pid) = @_;
181: $subject=&Apache::lonnet::escape($subject);
182: return(&Apache::lonnet::escape($now.':'.$subject.':'.$uname.':'.
183: $udom.':'.$msgcount.':'.$course_context.':'.$pid));
184: }
185:
186: sub unpackmsgid {
187: my ($msgid,$folder,$skipstatus,$status_cache)=@_;
188: $msgid=&Apache::lonnet::unescape($msgid);
189: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid,
190: $processid)=split(/\:/,&Apache::lonnet::unescape($msgid));
191: if (!defined($processid)) { $fromcid = ''; }
192: my %status=();
193: unless ($skipstatus) {
194: if (ref($status_cache)) {
195: $status{$msgid} = $status_cache->{$msgid};
196: } else {
197: my $suffix=&foldersuffix($folder);
198: %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
199: }
200: if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
201: unless ($status{$msgid}) { $status{$msgid}='new'; }
202: }
203: return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid);
204: }
205:
206:
207: sub sendemail {
208: my ($to,$subject,$body)=@_;
209: $body=
210: "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
211: "*** ".&mt('Please do not reply to this address.')."\n\n".$body;
212: my $msg = new Mail::Send;
213: $msg->to($to);
214: $msg->subject('[LON-CAPA] '.$subject);
215: if (my $fh = $msg->open()) {
216: print $fh $body;
217: $fh->close;
218: }
219: }
220:
221: # ==================================================== Send notification emails
222:
223: sub sendnotification {
224: my ($to,$touname,$toudom,$subj,$crit,$text)=@_;
225: my $sender=$env{'environment.firstname'}.' '.$env{'environment.lastname'};
226: unless ($sender=~/\w/) {
227: $sender=$env{'user.name'}.'@'.$env{'user.domain'};
228: }
229: my $critical=($crit?' critical':'');
230: $text=~s/\<\;/\</gs;
231: $text=~s/\>\;/\>/gs;
232: $text=~s/\<\/*[^\>]+\>//gs;
233: my $url='http://'.
234: $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
235: '/adm/email?username='.$touname.'&domain='.$toudom;
236: my $body=(<<ENDMSG);
237: You received a$critical message from $sender in LON-CAPA. The subject is
238:
239: $subj
240:
241: === Excerpt ============================================================
242: $text
243: ========================================================================
244:
245: Use
246:
247: $url
248:
249: to access the full message.
250: ENDMSG
251: &sendemail($to,'New'.$critical.' message from '.$sender,$body);
252: }
253: # ============================================================= Check for email
254:
255: sub newmail {
256: if ((time-$env{'user.mailcheck.time'})>300) {
257: my %what=&Apache::lonnet::get('email_status',['recnewemail']);
258: &Apache::lonnet::appenv('user.mailcheck.time'=>time);
259: if ($what{'recnewemail'}>0) { return 1; }
260: }
261: return 0;
262: }
263:
264: # =============================== Automated message to the author of a resource
265:
266: =pod
267:
268: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
269: of the resource with the URI $filename.
270:
271: =cut
272:
273: sub author_res_msg {
274: my ($filename,$message)=@_;
275: unless ($message) { return 'empty'; }
276: $filename=&Apache::lonnet::declutter($filename);
277: my ($domain,$author,@dummy)=split(/\//,$filename);
278: my $homeserver=&Apache::lonnet::homeserver($author,$domain);
279: if ($homeserver ne 'no_host') {
280: my $id=unpack("%32C*",$message);
281: $message .= " <p>This error occurred on machine ".
282: $Apache::lonnet::perlvar{'lonHostID'}."</p>";
283: my $msgid;
284: ($msgid,$message)=&packagemsg($filename,$message);
285: return &Apache::lonnet::reply('put:'.$domain.':'.$author.
286: ':nohist_res_msgs:'.
287: &Apache::lonnet::escape($filename.'_'.$id).'='.
288: &Apache::lonnet::escape($message),$homeserver);
289: }
290: return 'no_host';
291: }
292:
293: # =========================================== Retrieve author resource messages
294:
295: sub retrieve_author_res_msg {
296: my $url=shift;
297: $url=&Apache::lonnet::declutter($url);
298: my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
299: my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
300: my $msgs='';
301: foreach (keys %errormsgs) {
302: if ($_=~/^\Q$url\E\_\d+$/) {
303: my %content=&unpackagemsg($errormsgs{$_});
304: $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
305: $content{'time'}.'</b>: '.$content{'message'}.
306: '<br /></p>';
307: }
308: }
309: return $msgs;
310: }
311:
312:
313: # =============================== Delete all author messages related to one URL
314:
315: sub del_url_author_res_msg {
316: my $url=shift;
317: $url=&Apache::lonnet::declutter($url);
318: my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
319: my @delmsgs=();
320: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
321: if ($_=~/^\Q$url\E\_\d+$/) {
322: push (@delmsgs,$_);
323: }
324: }
325: return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
326: }
327: # =================================== Clear out all author messages in URL path
328:
329: sub clear_author_res_msg {
330: my $url=shift;
331: $url=&Apache::lonnet::declutter($url);
332: my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
333: my @delmsgs=();
334: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
335: if ($_=~/^\Q$url\E/) {
336: push (@delmsgs,$_);
337: }
338: }
339: return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
340: }
341: # ================= Return hash with URLs for which there is a resource message
342:
343: sub all_url_author_res_msg {
344: my ($author,$domain)=@_;
345: my %returnhash=();
346: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
347: $_=~/^(.+)\_\d+/;
348: $returnhash{$1}=1;
349: }
350: return %returnhash;
351: }
352:
353: # ================================================== Critical message to a user
354:
355: sub user_crit_msg_raw {
356: my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
357: # Check if allowed missing
358: my $status='';
359: my $msgid='undefined';
360: unless (($message)&&($user)&&($domain)) { $status='empty'; };
361: my $text=$message;
362: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
363: if ($homeserver ne 'no_host') {
364: ($msgid,$message)=&packagemsg($subject,$message);
365: if ($sendback) { $message.='<sendback>true</sendback>'; }
366: $status=&Apache::lonnet::critical(
367: 'put:'.$domain.':'.$user.':critical:'.
368: &Apache::lonnet::escape($msgid).'='.
369: &Apache::lonnet::escape($message),$homeserver);
370: if (defined($sentmessage)) {
371: $$sentmessage = $message;
372: }
373: } else {
374: $status='no_host';
375: }
376: # Notifications
377: my %userenv = &Apache::lonnet::get('environment',['critnotification',
378: 'permanentemail'],
379: $domain,$user);
380: if ($userenv{'critnotification'}) {
381: &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1,
382: $text);
383: }
384: if ($toperm && $userenv{'permanentemail'}) {
385: &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,1,
386: $text);
387: }
388: # Log this
389: &Apache::lonnet::logthis(
390: 'Sending critical email '.$msgid.
391: ', log status: '.
392: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
393: $env{'user.home'},
394: 'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
395: .$status));
396: return $status;
397: }
398:
399: # New routine that respects "forward" and calls old routine
400:
401: =pod
402:
403: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback)>: Sends
404: a critical message $message to the $user at $domain. If $sendback is true,
405: a reciept will be sent to the current user when $user recieves the message.
406:
407: =cut
408:
409: sub user_crit_msg {
410: my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
411: my $status='';
412: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
413: $domain,$user);
414: my $msgforward=$userenv{'msgforward'};
415: if ($msgforward) {
416: foreach (split(/\,/,$msgforward)) {
417: my ($forwuser,$forwdomain)=split(/\:/,$_);
418: $status.=
419: &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
420: $sendback,$toperm,$sentmessage).' ';
421: }
422: } else {
423: $status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage);
424: }
425: return $status;
426: }
427:
428: # =================================================== Critical message received
429:
430: sub user_crit_received {
431: my $msgid=shift;
432: my %message=&Apache::lonnet::get('critical',[$msgid]);
433: my %contents=&unpackagemsg($message{$msgid},1);
434: my $status='rec: '.($contents{'sendback'}?
435: &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
436: &mt('Receipt').': '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.', '.$contents{'subject'},
437: &mt('User').' '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.
438: ' acknowledged receipt of message'."\n".' "'.
439: $contents{'subject'}.'"'."\n".&mt('dated').' '.
440: $contents{'time'}.".\n"
441: ):'no msg req');
442: $status.=' trans: '.
443: &Apache::lonnet::put(
444: 'nohist_email',{$contents{'msgid'} => $message{$msgid}});
445: $status.=' del: '.
446: &Apache::lonnet::del('critical',[$contents{'msgid'}]);
447: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
448: $env{'user.home'},'Received critical message '.
449: $contents{'msgid'}.
450: ', '.$status);
451: return $status;
452: }
453:
454: # ======================================================== Normal communication
455:
456: sub user_normal_msg_raw {
457: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
458: $toperm,$currid,$newid,$sentmessage,$crsmsgid)=@_;
459: # Check if allowed missing
460: my ($status,$packed_message);
461: my $msgid='undefined';
462: my $text=$message;
463: unless (($message)&&($user)&&($domain)) { $status='empty'; };
464: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
465: if ($homeserver ne 'no_host') {
466: ($msgid,$packed_message)=
467: &packagemsg($subject,$message,$citation,$baseurl,
468: $attachmenturl,$user,$domain,$currid,
469: undef,$crsmsgid);
470:
471: # Store in user folder
472: $status=&Apache::lonnet::critical(
473: 'put:'.$domain.':'.$user.':nohist_email:'.
474: &Apache::lonnet::escape($msgid).'='.
475: &Apache::lonnet::escape($packed_message),$homeserver);
476: # Save new message received time
477: &Apache::lonnet::put
478: ('email_status',{'recnewemail'=>time},$domain,$user);
479: # Into sent-mail folder unless a broadcast message or critical message
480: unless (($env{'request.course.id'}) &&
481: (($env{'form.sendmode'} eq 'group') ||
482: (($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
483: (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
484: || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
485: '/'.$env{'request.course.sec'})))) {
486: (undef, my $packed_message_no_citation)=
487: &packagemsg($subject,$message,undef ,$baseurl,
488: $attachmenturl,$user,$domain,$currid,
489: undef,$crsmsgid);
490:
491: $status .= &store_sent_mail($msgid,$packed_message_no_citation);
492: }
493: } else {
494: $status='no_host';
495: }
496: if (defined($newid)) {
497: $$newid = $msgid;
498: }
499: if (defined($sentmessage)) {
500: $$sentmessage = $packed_message;
501: }
502:
503: # Notifications
504: my %userenv = &Apache::lonnet::get('environment',['notification',
505: 'permanentemail'],
506: $domain,$user);
507: if ($userenv{'notification'}) {
508: &sendnotification($userenv{'notification'},$user,$domain,$subject,0,
509: $text);
510: }
511: if ($toperm && $userenv{'permanentemail'}) {
512: &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,0,
513: $text);
514: }
515: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
516: $env{'user.home'},
517: 'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
518: return $status;
519: }
520:
521: # New routine that respects "forward" and calls old routine
522:
523: =pod
524:
525: =item * B<user_normal_msg($user, $domain, $subject, $message,
526: $citation, $baseurl, $attachmenturl)>: Sends a message to the
527: $user at $domain, with subject $subject and message $message.
528:
529: =cut
530:
531: sub user_normal_msg {
532: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
533: $toperm,$sentmessage)=@_;
534: my $status='';
535: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
536: $domain,$user);
537: my $msgforward=$userenv{'msgforward'};
538: if ($msgforward) {
539: foreach (split(/\,/,$msgforward)) {
540: my ($forwuser,$forwdomain)=split(/\:/,$_);
541: $status.=
542: &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
543: $citation,$baseurl,$attachmenturl,$toperm,
544: undef,undef,$sentmessage).' ';
545: }
546: } else {
547: $status=&user_normal_msg_raw($user,$domain,$subject,$message,
548: $citation,$baseurl,$attachmenturl,$toperm,
549: undef,undef,$sentmessage);
550: }
551: return $status;
552: }
553:
554: sub store_sent_mail {
555: my ($msgid,$message) = @_;
556: my $status =' '.&Apache::lonnet::critical(
557: 'put:'.$env{'user.domain'}.':'.$env{'user.name'}.
558: ':nohist_email_sent:'.
559: &Apache::lonnet::escape($msgid).'='.
560: &Apache::lonnet::escape($message),$env{'user.home'});
561: return $status;
562: }
563:
564: # =============================================================== Folder suffix
565:
566: sub foldersuffix {
567: my $folder=shift;
568: unless ($folder) { return ''; }
569: return '_'.&Apache::lonnet::escape($folder);
570: }
571:
572: 1;
573: __END__
574:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>