File:
[LON-CAPA] /
loncom /
interface /
lonmsg.pm
Revision
1.180:
download - view:
text,
annotated -
select for diffs
Sat Apr 8 06:53:37 2006 UTC (18 years, 2 months ago) by
albertel
Branches:
MAIN
CVS tags:
HEAD
- moving most of lonmsg into a new lonmsgdisplay module
- lonmsg now only contains the actual functions to send messages in the system and can now be ewasily used by now mod_pelr routines
1: # The LearningOnline Network with CAPA
2: # Routines for messaging
3: #
4: # $Id: lonmsg.pm,v 1.180 2006/04/08 06:53:37 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: my $msgid;
282: ($msgid,$message)=&packagemsg($filename,$message);
283: return &Apache::lonnet::reply('put:'.$domain.':'.$author.
284: ':nohist_res_msgs:'.
285: &Apache::lonnet::escape($filename.'_'.$id).'='.
286: &Apache::lonnet::escape($message),$homeserver);
287: }
288: return 'no_host';
289: }
290:
291: # =========================================== Retrieve author resource messages
292:
293: sub retrieve_author_res_msg {
294: my $url=shift;
295: $url=&Apache::lonnet::declutter($url);
296: my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
297: my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
298: my $msgs='';
299: foreach (keys %errormsgs) {
300: if ($_=~/^\Q$url\E\_\d+$/) {
301: my %content=&unpackagemsg($errormsgs{$_});
302: $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
303: $content{'time'}.'</b>: '.$content{'message'}.
304: '<br /></p>';
305: }
306: }
307: return $msgs;
308: }
309:
310:
311: # =============================== Delete all author messages related to one URL
312:
313: sub del_url_author_res_msg {
314: my $url=shift;
315: $url=&Apache::lonnet::declutter($url);
316: my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
317: my @delmsgs=();
318: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
319: if ($_=~/^\Q$url\E\_\d+$/) {
320: push (@delmsgs,$_);
321: }
322: }
323: return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
324: }
325: # =================================== Clear out all author messages in URL path
326:
327: sub clear_author_res_msg {
328: my $url=shift;
329: $url=&Apache::lonnet::declutter($url);
330: my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//);
331: my @delmsgs=();
332: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
333: if ($_=~/^\Q$url\E/) {
334: push (@delmsgs,$_);
335: }
336: }
337: return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
338: }
339: # ================= Return hash with URLs for which there is a resource message
340:
341: sub all_url_author_res_msg {
342: my ($author,$domain)=@_;
343: my %returnhash=();
344: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
345: $_=~/^(.+)\_\d+/;
346: $returnhash{$1}=1;
347: }
348: return %returnhash;
349: }
350:
351: # ================================================== Critical message to a user
352:
353: sub user_crit_msg_raw {
354: my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
355: # Check if allowed missing
356: my $status='';
357: my $msgid='undefined';
358: unless (($message)&&($user)&&($domain)) { $status='empty'; };
359: my $text=$message;
360: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
361: if ($homeserver ne 'no_host') {
362: ($msgid,$message)=&packagemsg($subject,$message);
363: if ($sendback) { $message.='<sendback>true</sendback>'; }
364: $status=&Apache::lonnet::critical(
365: 'put:'.$domain.':'.$user.':critical:'.
366: &Apache::lonnet::escape($msgid).'='.
367: &Apache::lonnet::escape($message),$homeserver);
368: if (defined($sentmessage)) {
369: $$sentmessage = $message;
370: }
371: } else {
372: $status='no_host';
373: }
374: # Notifications
375: my %userenv = &Apache::lonnet::get('environment',['critnotification',
376: 'permanentemail'],
377: $domain,$user);
378: if ($userenv{'critnotification'}) {
379: &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1,
380: $text);
381: }
382: if ($toperm && $userenv{'permanentemail'}) {
383: &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,1,
384: $text);
385: }
386: # Log this
387: &Apache::lonnet::logthis(
388: 'Sending critical email '.$msgid.
389: ', log status: '.
390: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
391: $env{'user.home'},
392: 'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
393: .$status));
394: return $status;
395: }
396:
397: # New routine that respects "forward" and calls old routine
398:
399: =pod
400:
401: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback)>: Sends
402: a critical message $message to the $user at $domain. If $sendback is true,
403: a reciept will be sent to the current user when $user recieves the message.
404:
405: =cut
406:
407: sub user_crit_msg {
408: my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
409: my $status='';
410: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
411: $domain,$user);
412: my $msgforward=$userenv{'msgforward'};
413: if ($msgforward) {
414: foreach (split(/\,/,$msgforward)) {
415: my ($forwuser,$forwdomain)=split(/\:/,$_);
416: $status.=
417: &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
418: $sendback,$toperm,$sentmessage).' ';
419: }
420: } else {
421: $status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage);
422: }
423: return $status;
424: }
425:
426: # =================================================== Critical message received
427:
428: sub user_crit_received {
429: my $msgid=shift;
430: my %message=&Apache::lonnet::get('critical',[$msgid]);
431: my %contents=&unpackagemsg($message{$msgid},1);
432: my $status='rec: '.($contents{'sendback'}?
433: &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
434: &mt('Receipt').': '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.', '.$contents{'subject'},
435: &mt('User').' '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.
436: ' acknowledged receipt of message'."\n".' "'.
437: $contents{'subject'}.'"'."\n".&mt('dated').' '.
438: $contents{'time'}.".\n"
439: ):'no msg req');
440: $status.=' trans: '.
441: &Apache::lonnet::put(
442: 'nohist_email',{$contents{'msgid'} => $message{$msgid}});
443: $status.=' del: '.
444: &Apache::lonnet::del('critical',[$contents{'msgid'}]);
445: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
446: $env{'user.home'},'Received critical message '.
447: $contents{'msgid'}.
448: ', '.$status);
449: return $status;
450: }
451:
452: # ======================================================== Normal communication
453:
454: sub user_normal_msg_raw {
455: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
456: $toperm,$currid,$newid,$sentmessage,$crsmsgid)=@_;
457: # Check if allowed missing
458: my ($status,$packed_message);
459: my $msgid='undefined';
460: my $text=$message;
461: unless (($message)&&($user)&&($domain)) { $status='empty'; };
462: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
463: if ($homeserver ne 'no_host') {
464: ($msgid,$packed_message)=
465: &packagemsg($subject,$message,$citation,$baseurl,
466: $attachmenturl,$user,$domain,$currid,
467: undef,$crsmsgid);
468:
469: # Store in user folder
470: $status=&Apache::lonnet::critical(
471: 'put:'.$domain.':'.$user.':nohist_email:'.
472: &Apache::lonnet::escape($msgid).'='.
473: &Apache::lonnet::escape($packed_message),$homeserver);
474: # Save new message received time
475: &Apache::lonnet::put
476: ('email_status',{'recnewemail'=>time},$domain,$user);
477: # Into sent-mail folder unless a broadcast message or critical message
478: unless (($env{'request.course.id'}) &&
479: (($env{'form.sendmode'} eq 'group') ||
480: (($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
481: (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
482: || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
483: '/'.$env{'request.course.sec'})))) {
484: (undef, my $packed_message_no_citation)=
485: &packagemsg($subject,$message,undef ,$baseurl,
486: $attachmenturl,$user,$domain,$currid,
487: undef,$crsmsgid);
488:
489: $status .= &store_sent_mail($msgid,$packed_message_no_citation);
490: }
491: } else {
492: $status='no_host';
493: }
494: if (defined($newid)) {
495: $$newid = $msgid;
496: }
497: if (defined($sentmessage)) {
498: $$sentmessage = $packed_message;
499: }
500:
501: # Notifications
502: my %userenv = &Apache::lonnet::get('environment',['notification',
503: 'permanentemail'],
504: $domain,$user);
505: if ($userenv{'notification'}) {
506: &sendnotification($userenv{'notification'},$user,$domain,$subject,0,
507: $text);
508: }
509: if ($toperm && $userenv{'permanentemail'}) {
510: &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,0,
511: $text);
512: }
513: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
514: $env{'user.home'},
515: 'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
516: return $status;
517: }
518:
519: # New routine that respects "forward" and calls old routine
520:
521: =pod
522:
523: =item * B<user_normal_msg($user, $domain, $subject, $message,
524: $citation, $baseurl, $attachmenturl)>: Sends a message to the
525: $user at $domain, with subject $subject and message $message.
526:
527: =cut
528:
529: sub user_normal_msg {
530: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
531: $toperm,$sentmessage)=@_;
532: my $status='';
533: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
534: $domain,$user);
535: my $msgforward=$userenv{'msgforward'};
536: if ($msgforward) {
537: foreach (split(/\,/,$msgforward)) {
538: my ($forwuser,$forwdomain)=split(/\:/,$_);
539: $status.=
540: &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
541: $citation,$baseurl,$attachmenturl,$toperm,
542: undef,undef,$sentmessage).' ';
543: }
544: } else {
545: $status=&user_normal_msg_raw($user,$domain,$subject,$message,
546: $citation,$baseurl,$attachmenturl,$toperm,
547: undef,undef,$sentmessage);
548: }
549: return $status;
550: }
551:
552: sub store_sent_mail {
553: my ($msgid,$message) = @_;
554: my $status =' '.&Apache::lonnet::critical(
555: 'put:'.$env{'user.domain'}.':'.$env{'user.name'}.
556: ':nohist_email_sent:'.
557: &Apache::lonnet::escape($msgid).'='.
558: &Apache::lonnet::escape($message),$env{'user.home'});
559: return $status;
560: }
561:
562: # =============================================================== Folder suffix
563:
564: sub foldersuffix {
565: my $folder=shift;
566: unless ($folder) { return ''; }
567: return '_'.&Apache::lonnet::escape($folder);
568: }
569:
570: 1;
571: __END__
572:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>