File:
[LON-CAPA] /
loncom /
interface /
lonmsg.pm
Revision
1.206:
download - view:
text,
annotated -
select for diffs
Tue May 8 17:23:10 2007 UTC (17 years, 2 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
Bugs 4647 and 5164
- User can set option for HTML tags to be included in message excerpt sent in notification emails. Default is strip HTML.
- Interface changed for setting of notification email addresses - now set each address to receive all, critical only or non-critical only.
- javascript checks notification e-mail address to see if it is a valid format.
- Internationalization was missing in some places.
1: # The LearningOnline Network with CAPA
2: # Routines for messaging
3: #
4: # $Id: lonmsg.pm,v 1.206 2007/05/08 17:23:10 raeburn 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: =pod
32:
33: =head1 NAME
34:
35: Apache::lonmsg: supports internal messaging
36:
37: =head1 SYNOPSIS
38:
39: lonmsg provides routines for sending messages.
40:
41: Right now, this document will cover just how to send a message, since
42: it is likely you will not need to programmatically read messages,
43: since lonmsg already implements that functionality.
44:
45: The routines used to package messages and unpackage messages are not
46: only used by lonmsg when creating/extracting messages for LON-CAPA's
47: internal messaging system, but also by lonnotify.pm which is available
48: for use by Domain Coordinators to broadcast standard e-mail to specified
49: users in their domain. The XML packaging used in the two cases is very
50: similar. The differences are the use of <recuser>$uname</recuser> and
51: <recdomain>$udom</recdomain> in stored internal messages, compared
52: with <recipient username="$uname:$udom">$email</recipient> in stored
53: Domain Coordinator e-mail for the storage of information about
54: recipients of the message/e-mail.
55:
56: =head1 FUNCTIONS
57:
58: =over 4
59:
60: =cut
61:
62: use strict;
63: use Apache::lonnet;
64: use HTML::TokeParser();
65: use Apache::lonlocal;
66: use Mail::Send;
67: use LONCAPA qw(:DEFAULT :match);
68:
69: {
70: my $uniq;
71: sub get_uniq {
72: $uniq++;
73: return $uniq;
74: }
75: }
76:
77: # ===================================================================== Package
78:
79: sub packagemsg {
80: my ($subject,$message,$citation,$baseurl,$attachmenturl,
81: $recuser,$recdomain,$msgid,$type,$crsmsgid,$symb,$error,$recipid)=@_;
82: $message =&HTML::Entities::encode($message,'<>&"');
83: $citation=&HTML::Entities::encode($citation,'<>&"');
84: $subject =&HTML::Entities::encode($subject,'<>&"');
85: #remove machine specification
86: $baseurl =~ s|^http://[^/]+/|/|;
87: $baseurl =&HTML::Entities::encode($baseurl,'<>&"');
88: #remove machine specification
89: $attachmenturl =~ s|^http://[^/]+/|/|;
90: $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
91: my $course_context = &get_course_context();
92: my $now=time;
93: my $msgcount = &get_uniq();
94: unless(defined($msgid)) {
95: $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},
96: $msgcount,$course_context,$symb,$error,$$);
97: }
98: my $result = '<sendername>'.$env{'user.name'}.'</sendername>'.
99: '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
100: '<subject>'.$subject.'</subject>'.
101: '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>';
102: if (defined($crsmsgid)) {
103: $result.= '<courseid>'.$course_context.'</courseid>'.
104: '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
105: '<msgid>'.$msgid.'</msgid>'.
106: '<coursemsgid>'.$crsmsgid.'</coursemsgid>'.
107: '<message>'.$message.'</message>';
108: return ($msgid,$result);
109: }
110: $result .= '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
111: '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
112: '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
113: '<browsertype>'.$env{'browser.type'}.'</browsertype>'.
114: '<browseros>'.$env{'browser.os'}.'</browseros>'.
115: '<browserversion>'.$env{'browser.version'}.'</browserversion>'.
116: '<browsermathml>'.$env{'browser.mathml'}.'</browsermathml>'.
117: '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
118: '<courseid>'.$course_context.'</courseid>'.
119: '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
120: '<role>'.$env{'request.role'}.'</role>'.
121: '<resource>'.$env{'request.filename'}.'</resource>'.
122: '<msgid>'.$msgid.'</msgid>';
123: if (ref($recuser) eq 'ARRAY') {
124: for (my $i=0; $i<@{$recuser}; $i++) {
125: if ($type eq 'dcmail') {
126: my ($username,$email) = split(/:/,$$recuser[$i]);
127: $username = &unescape($username);
128: $email = &unescape($email);
129: $username = &HTML::Entities::encode($username,'<>&"');
130: $email = &HTML::Entities::encode($email,'<>&"');
131: $result .= '<recipient username="'.$username.'">'.
132: $email.'</recipient>';
133: } else {
134: $result .= '<recuser>'.$$recuser[$i].'</recuser>'.
135: '<recdomain>'.$$recdomain[$i].'</recdomain>';
136: }
137: }
138: } else {
139: $result .= '<recuser>'.$recuser.'</recuser>'.
140: '<recdomain>'.$recdomain.'</recdomain>';
141: }
142: $result .= '<message>'.$message.'</message>';
143: if (defined($citation)) {
144: $result.='<citation>'.$citation.'</citation>';
145: }
146: if (defined($baseurl)) {
147: $result.= '<baseurl>'.$baseurl.'</baseurl>';
148: }
149: if (defined($attachmenturl)) {
150: $result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
151: }
152: if (defined($symb)) {
153: $result.= '<symb>'.$symb.'</symb>';
154: if ($course_context ne '') {
155: if ($course_context eq $env{'request.course.id'}) {
156: my $resource_title = &Apache::lonnet::gettitle($symb);
157: if (defined($resource_title)) {
158: $result .= '<resource_title>'.$resource_title.'</resource_title>';
159: }
160: }
161: }
162: }
163: if (defined($recipid)) {
164: $result.= '<recipid>'.$recipid.'</recipid>';
165: }
166: if ($env{'form.can_reply'} eq 'N') {
167: $result .= '<noreplies>1</noreplies>';
168: }
169: if ($env{'form.reply_to_addr'}) {
170: my ($replytoname,$replytodom) = split(/:/,$env{'form.reply_to_addr'});
171: if (!($replytoname eq $env{'user.name'} && $replytodom eq $env{'user.domain'})) {
172: if (&Apache::lonnet::homeserver($replytoname,$replytodom) ne 'no_host') {
173: $result .= '<replytoaddr>'.$env{'form.reply_to_addr'}.'</replytoaddr>';
174: }
175: }
176: }
177: return ($msgid,$result);
178: }
179:
180: sub get_course_context {
181: my $course_context;
182: if (defined($env{'form.replyid'})) {
183: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid)=
184: split(/\:/,&unescape($env{'form.replyid'}));
185: $course_context = $origcid;
186: }
187: foreach my $key (keys(%env)) {
188: if ($key=~/^form\.(rep)?rec\_(.*)$/) {
189: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) =
190: split(/\:/,&unescape($2));
191: $course_context = $origcid;
192: last;
193: }
194: }
195: if ($course_context eq '') {
196: $course_context = $env{'request.course.id'};
197: }
198: return $course_context;
199: }
200:
201: # ================================================== Unpack message into a hash
202:
203: sub unpackagemsg {
204: my ($message,$notoken)=@_;
205: my %content=();
206: my $parser=HTML::TokeParser->new(\$message);
207: my $token;
208: while ($token=$parser->get_token) {
209: if ($token->[0] eq 'S') {
210: my $entry=$token->[1];
211: my $value=$parser->get_text('/'.$entry);
212: if (($entry eq 'recuser') || ($entry eq 'recdomain')) {
213: push(@{$content{$entry}},$value);
214: } elsif ($entry eq 'recipient') {
215: my $username = $token->[2]{'username'};
216: $username = &HTML::Entities::decode($username,'<>&"');
217: $content{$entry}{$username} = $value;
218: } else {
219: $content{$entry}=$value;
220: }
221: }
222: }
223: if (!exists($content{'recuser'})) { $content{'recuser'} = []; }
224: if ($content{'attachmenturl'}) {
225: my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
226: if ($notoken) {
227: $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';
228: } else {
229: &Apache::lonnet::allowuploaded('/adm/msg',
230: $content{'attachmenturl'});
231: $content{'message'}.='<p>'.&mt('Attachment').
232: ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
233: $fname.'</tt></a>';
234: }
235: }
236: return %content;
237: }
238:
239: # ======================================================= Get info out of msgid
240:
241: sub buildmsgid {
242: my ($now,$subject,$uname,$udom,$msgcount,$course_context,$symb,$error,$pid) = @_;
243: $subject=&escape($subject);
244: $symb = &escape($symb);
245: return(&escape($now.':'.$subject.':'.$uname.':'.
246: $udom.':'.$msgcount.':'.$course_context.':'.$pid.':'.$symb.':'.$error));
247: }
248:
249: sub unpackmsgid {
250: my ($msgid,$folder,$skipstatus,$status_cache)=@_;
251: $msgid=&unescape($msgid);
252: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid,
253: $processid,$symb,$error) = split(/\:/,&unescape($msgid));
254: $shortsubj = &unescape($shortsubj);
255: $shortsubj = &HTML::Entities::decode($shortsubj);
256: $symb = &unescape($symb);
257: if (!defined($processid)) { $fromcid = ''; }
258: my %status=();
259: unless ($skipstatus) {
260: if (ref($status_cache)) {
261: $status{$msgid} = $status_cache->{$msgid};
262: } else {
263: my $suffix=&foldersuffix($folder);
264: %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
265: }
266: if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
267: unless ($status{$msgid}) { $status{$msgid}='new'; }
268: }
269: return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid,$symb,$error);
270: }
271:
272:
273: sub sendemail {
274: my ($to,$subject,$body)=@_;
275: my %senderemails=&Apache::loncommon::getemails();
276: my $senderaddress='';
277: foreach my $type ('notification','permanentemail','critnotification') {
278: if ($senderemails{$type}) {
279: $senderaddress=$senderemails{$type};
280: }
281: }
282: $body=
283: "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
284: "*** ".($senderaddress?&mt('You can reply to this message'):&mt('Please do not reply to this address.')."\n*** ".
285: &mt('A reply will not be received by the recipient!'))."\n\n".$body;
286: my $msg = new Mail::Send;
287: $msg->to($to);
288: $msg->subject('[LON-CAPA] '.$subject);
289: if ($senderaddress) { $msg->add('Reply-to',$senderaddress); $msg->add('From',$senderaddress); }
290: if (my $fh = $msg->open()) {
291: print $fh $body;
292: $fh->close;
293: }
294: }
295:
296: # ==================================================== Send notification emails
297:
298: sub sendnotification {
299: my ($to,$touname,$toudom,$subj,$crit,$text,$msgid)=@_;
300: my $sender=$env{'environment.firstname'}.' '.$env{'environment.lastname'};
301: unless ($sender=~/\w/) {
302: $sender=$env{'user.name'}.'@'.$env{'user.domain'};
303: }
304: my $critical=($crit?' critical':'');
305: $text=~s/\<\;/\</gs;
306: $text=~s/\>\;/\>/gs;
307: my $url='http://'.
308: &Apache::lonnet::hostname(&Apache::lonnet::homeserver($touname,$toudom)).
309: '/adm/email?username='.$touname.'&domain='.$toudom;
310: my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid,
311: $symb,$error) = &Apache::lonmsg::unpackmsgid($msgid);
312: my ($coursetext,$body,$bodystart,$bodyend);
313: if ($fromcid ne '') {
314: $coursetext = "\n".&mt('Course').': ';
315: if ($env{'course.'.$fromcid.'.description'} ne '') {
316: $coursetext .= $env{'course.'.$fromcid.'.description'};
317: } else {
318: my %coursehash = &Apache::lonnet::coursedescription($fromcid,);
319: if ($coursehash{'description'} ne '') {
320: $coursetext .= $coursehash{'description'};
321: }
322: }
323: $coursetext .= "\n\n";
324: }
325: my @recipients = split(/,/,$to);
326: $bodystart = $coursetext.
327: &mt('You received a'.$critical.' message from [_1] in LON-CAPA.',$sender).' '.&mt('The subject is
328:
329: [_1]
330:
331: ',$subj)."\n".
332: '=== '.&mt('Excerpt')." ============================================================
333: ";
334: $bodyend = "
335: ========================================================================
336:
337: ".&mt('Use
338:
339: [_1]
340:
341: to access the full message.',$url);
342: my %userenv = &Apache::lonnet::get('environment',['notifywithhtml'],$toudom,$touname);
343: my $subject = &mt("'New' $critical message from ").$sender;
344: if ($userenv{'notifywithhtml'} ne '') {
345: my @htmlexcerpt = split(/,/,$userenv{'notifywithhtml'});
346: foreach my $addr (@recipients) {
347: my $sendtext = $text;
348: if (!grep/^\Q$addr\E/,@htmlexcerpt) {
349: $sendtext =~ s/\<\/*[^\>]+\>//gs;
350: }
351: $body = $bodystart.$sendtext.$bodyend;
352: &sendemail($addr,$subject,$body);
353: }
354: } else {
355: $body = $bodystart.$text.$bodyend;
356: $text =~ s/\<\/*[^\>]+\>//gs;
357: &sendemail($to,$subject,$body);
358: }
359: }
360: # ============================================================= Check for email
361:
362: sub newmail {
363: if ((time-$env{'user.mailcheck.time'})>300) {
364: my %what=&Apache::lonnet::get('email_status',['recnewemail']);
365: &Apache::lonnet::appenv('user.mailcheck.time'=>time);
366: if ($what{'recnewemail'}>0) { return 1; }
367: }
368: return 0;
369: }
370:
371: # =============================== Automated message to the author of a resource
372:
373: =pod
374:
375: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
376: of the resource with the URI $filename.
377:
378: =cut
379:
380: sub author_res_msg {
381: my ($filename,$message)=@_;
382: unless ($message) { return 'empty'; }
383: $filename=&Apache::lonnet::declutter($filename);
384: my ($domain,$author,@dummy)=split(/\//,$filename);
385: my $homeserver=&Apache::lonnet::homeserver($author,$domain);
386: if ($homeserver ne 'no_host') {
387: my $id=unpack("%32C*",$message);
388: $message .= " <p>This error occurred on machine ".
389: $Apache::lonnet::perlvar{'lonHostID'}."</p>";
390: my $msgid;
391: ($msgid,$message)=&packagemsg($filename,$message);
392: return &Apache::lonnet::reply('put:'.$domain.':'.$author.
393: ':nohist_res_msgs:'.
394: &escape($filename.'_'.$id).'='.
395: &escape($message),$homeserver);
396: }
397: return 'no_host';
398: }
399:
400: # =========================================== Retrieve author resource messages
401:
402: sub retrieve_author_res_msg {
403: my $url=shift;
404: $url=&Apache::lonnet::declutter($url);
405: my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
406: my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
407: my $msgs='';
408: foreach (keys %errormsgs) {
409: if ($_=~/^\Q$url\E\_\d+$/) {
410: my %content=&unpackagemsg($errormsgs{$_});
411: $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
412: $content{'time'}.'</b>: '.$content{'message'}.
413: '<br /></p>';
414: }
415: }
416: return $msgs;
417: }
418:
419:
420: # =============================== Delete all author messages related to one URL
421:
422: sub del_url_author_res_msg {
423: my $url=shift;
424: $url=&Apache::lonnet::declutter($url);
425: my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
426: my @delmsgs=();
427: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
428: if ($_=~/^\Q$url\E\_\d+$/) {
429: push (@delmsgs,$_);
430: }
431: }
432: return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
433: }
434: # =================================== Clear out all author messages in URL path
435:
436: sub clear_author_res_msg {
437: my $url=shift;
438: $url=&Apache::lonnet::declutter($url);
439: my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
440: my @delmsgs=();
441: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
442: if ($_=~/^\Q$url\E/) {
443: push (@delmsgs,$_);
444: }
445: }
446: return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
447: }
448: # ================= Return hash with URLs for which there is a resource message
449:
450: sub all_url_author_res_msg {
451: my ($author,$domain)=@_;
452: my %returnhash=();
453: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
454: $_=~/^(.+)\_\d+/;
455: $returnhash{$1}=1;
456: }
457: return %returnhash;
458: }
459:
460: # ====================================== Add a comment to the User Notes screen
461:
462: sub store_instructor_comment {
463: my ($msg,$uname,$udom) = @_;
464: my $cid = $env{'request.course.id'};
465: my $cnum = $env{'course.'.$cid.'.num'};
466: my $cdom = $env{'course.'.$cid.'.domain'};
467: my $subject= &mt('Record').' ['.$uname.':'.$udom.']';
468: my $result = &user_normal_msg_raw($cnum,$cdom,$subject,$msg);
469: if ($result eq 'ok' || $result eq 'con_delayed') {
470:
471: }
472: return $result;
473: }
474:
475: # ================================================== Critical message to a user
476:
477: sub user_crit_msg_raw {
478: my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage,
479: $nosentstore,$recipid)=@_;
480: # Check if allowed missing
481: my ($status,$packed_message);
482: my $msgid='undefined';
483: unless (($message)&&($user)&&($domain)) { $status='empty'; };
484: my $text=$message;
485: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
486: if ($homeserver ne 'no_host') {
487: ($msgid,$packed_message)=&packagemsg($subject,$message,undef,undef,
488: undef,undef,undef,undef,undef,undef,undef,
489: undef,$recipid);
490: if ($sendback) { $packed_message.='<sendback>true</sendback>'; }
491: $status=&Apache::lonnet::critical(
492: 'put:'.$domain.':'.$user.':critical:'.
493: &escape($msgid).'='.
494: &escape($packed_message),$homeserver);
495: if (defined($sentmessage)) {
496: $$sentmessage = $packed_message;
497: }
498: if (!$nosentstore) {
499: (undef,my $packed_message_no_citation) =
500: &packagemsg($subject,$message,undef,undef,undef,$user,$domain,
501: $msgid);
502: if ($status eq 'ok' || $status eq 'con_delayed') {
503: &store_sent_mail($msgid,$packed_message_no_citation);
504: }
505: }
506: } else {
507: $status='no_host';
508: }
509:
510: # Notifications
511: my %userenv = &Apache::loncommon::getemails($user,$domain);
512: if ($userenv{'critnotification'}) {
513: &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1,
514: $text,$msgid);
515: }
516: if ($toperm && $userenv{'permanentemail'}) {
517: &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,1,
518: $text,$msgid);
519: }
520: # Log this
521: &Apache::lonnet::logthis(
522: 'Sending critical email '.$msgid.
523: ', log status: '.
524: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
525: $env{'user.home'},
526: 'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
527: .$status));
528: return $status;
529: }
530:
531: # New routine that respects "forward" and calls old routine
532:
533: =pod
534:
535: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback, $nosentstore,$recipid)>:
536: Sends a critical message $message to the $user at $domain. If $sendback
537: is true, a receipt will be sent to the current user when $user receives
538: the message.
539:
540: Additionally it will check if the user has a Forwarding address
541: set, and send the message to that address instead
542:
543: returns
544: - in array context a list of results for each message that was sent
545: - in scalar context a space seperated list of results for each
546: message sent
547:
548: =cut
549:
550: sub user_crit_msg {
551: my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage,
552: $nosentstore,$recipid)=@_;
553: my @status;
554: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
555: $domain,$user);
556: my $msgforward=$userenv{'msgforward'};
557: if ($msgforward) {
558: foreach my $addr (split(/\,/,$msgforward)) {
559: my ($forwuser,$forwdomain)=split(/\:/,$addr);
560: push(@status,
561: &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
562: $sendback,$toperm,$sentmessage,$nosentstore,
563: $recipid));
564: }
565: } else {
566: push(@status,
567: &user_crit_msg_raw($user,$domain,$subject,$message,$sendback,
568: $toperm,$sentmessage,$nosentstore,$recipid));
569: }
570: if (wantarray) {
571: return @status;
572: }
573: return join(' ',@status);
574: }
575:
576: # =================================================== Critical message received
577:
578: sub user_crit_received {
579: my $msgid=shift;
580: my %message=&Apache::lonnet::get('critical',[$msgid]);
581: my %contents=&unpackagemsg($message{$msgid},1);
582: my $destname = $contents{'sendername'};
583: my $destdom = $contents{'senderdomain'};
584: if ($contents{'replytoaddr'}) {
585: my ($repname,$repdom) = split(/:/,$contents{'replytoaddr'});
586: if (&Apache::lonnet::homeserver($repname,$repdom) ne 'no_host') {
587: $destname = $repname;
588: $destdom = $repdom;
589: }
590: }
591: my $status='rec: '.($contents{'sendback'}?
592: &user_normal_msg($destname,$destdom,&mt('Receipt').': '.$env{'user.name'}.
593: ' '.&mt('at').' '.$env{'user.domain'}.', '.
594: $contents{'subject'},&mt('User').' '.$env{'user.name'}.
595: ' '.&mt('at').' '.$env{'user.domain'}.
596: ' acknowledged receipt of message'."\n".' "'.
597: $contents{'subject'}.'"'."\n".&mt('dated').' '.
598: $contents{'time'}.".\n"
599: ):'no msg req');
600: $status.=' trans: '.
601: &Apache::lonnet::put(
602: 'nohist_email',{$contents{'msgid'} => $message{$msgid}});
603: $status.=' del: '.
604: &Apache::lonnet::del('critical',[$contents{'msgid'}]);
605: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
606: $env{'user.home'},'Received critical message '.
607: $contents{'msgid'}.
608: ', '.$status);
609: return $status;
610: }
611:
612: # ======================================================== Normal communication
613:
614: sub user_normal_msg_raw {
615: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
616: $toperm,$currid,$newid,$sentmessage,$crsmsgid,$symb,$restitle,
617: $error,$nosentstore,$recipid)=@_;
618: # Check if allowed missing
619: my ($status,$packed_message);
620: my $msgid='undefined';
621: my $text=$message;
622: unless (($message)&&($user)&&($domain)) { $status='empty'; };
623: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
624: if ($homeserver ne 'no_host') {
625: ($msgid,$packed_message)=
626: &packagemsg($subject,$message,$citation,$baseurl,
627: $attachmenturl,$user,$domain,$currid,
628: undef,$crsmsgid,$symb,$error,$recipid);
629:
630: # Store in user folder
631: $status=&Apache::lonnet::critical(
632: 'put:'.$domain.':'.$user.':nohist_email:'.
633: &escape($msgid).'='.
634: &escape($packed_message),$homeserver);
635: # Save new message received time
636: &Apache::lonnet::put
637: ('email_status',{'recnewemail'=>time},$domain,$user);
638: # Into sent-mail folder if sent mail storage required
639: if (!$nosentstore) {
640: (undef,my $packed_message_no_citation) =
641: &packagemsg($subject,$message,undef,$baseurl,$attachmenturl,
642: $user,$domain,$currid,undef,$crsmsgid,$symb,$error);
643: if ($status eq 'ok' || $status eq 'con_delayed') {
644: &store_sent_mail($msgid,$packed_message_no_citation);
645: }
646: }
647: if (ref($newid) eq 'SCALAR') {
648: $$newid = $msgid;
649: }
650: if (ref($sentmessage) eq 'SCALAR') {
651: $$sentmessage = $packed_message;
652: }
653: # Notifications
654: my %userenv = &Apache::loncommon::getemails($user,$domain);
655: if ($userenv{'notification'}) {
656: &sendnotification($userenv{'notification'},$user,$domain,$subject,0,
657: $text,$msgid);
658: }
659: if ($toperm && $userenv{'permanentemail'}) {
660: &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,0,
661: $text,$msgid);
662: }
663: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
664: $env{'user.home'},
665: 'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
666: } else {
667: $status='no_host';
668: }
669: return $status;
670: }
671:
672: # New routine that respects "forward" and calls old routine
673:
674: =pod
675:
676: =item * B<user_normal_msg($user, $domain, $subject, $message, $citation,
677: $baseurl, $attachmenturl, $toperm, $sentmessage, $symb, $restitle,
678: $error,$nosentstore,$recipid)>:
679: Sends a message to the $user at $domain, with subject $subject and message $message.
680:
681: Additionally it will check if the user has a Forwarding address
682: set, and send the message to that address instead
683:
684: returns
685: - in array context a list of results for each message that was sent
686: - in scalar context a space seperated list of results for each
687: message sent
688:
689: =cut
690:
691: sub user_normal_msg {
692: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
693: $toperm,$sentmessage,$symb,$restitle,$error,$nosentstore,$recipid)=@_;
694: my @status;
695: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
696: $domain,$user);
697: my $msgforward=$userenv{'msgforward'};
698: if ($msgforward) {
699: foreach (split(/\,/,$msgforward)) {
700: my ($forwuser,$forwdomain)=split(/\:/,$_);
701: push(@status,
702: &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
703: $citation,$baseurl,$attachmenturl,$toperm,
704: undef,undef,$sentmessage,undef,$symb,
705: $restitle,$error,$nosentstore,$recipid));
706: }
707: } else {
708: push(@status,&user_normal_msg_raw($user,$domain,$subject,$message,
709: $citation,$baseurl,$attachmenturl,$toperm,
710: undef,undef,$sentmessage,undef,$symb,
711: $restitle,$error,$nosentstore,$recipid));
712: }
713: if (wantarray) {
714: return @status;
715: }
716: return join(' ',@status);
717: }
718:
719: sub process_sent_mail {
720: my ($msgsubj,$subj_prefix,$numsent,$stamp,$msgname,$msgdom,$msgcount,$context,$pid,$savemsg,$recusers,$recudoms,$baseurl,$attachmenturl,$symb,$error,$senderuname,$senderdom,$senderhome) = @_;
721: my $sentsubj;
722: if ($numsent > 1) {
723: $sentsubj = $subj_prefix.' ('.$numsent.' sent) '.$msgsubj;
724: } else {
725: if ($subj_prefix) {
726: $sentsubj = $subj_prefix.' ';
727: }
728: $sentsubj .= $msgsubj;
729: }
730: $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"');
731: my $sentmsgid =
732: &buildmsgid($stamp,$sentsubj,$msgname,$msgdom,$msgcount,$context,$pid);
733: (undef,my $sentmessage) =
734: &packagemsg($msgsubj,$savemsg,undef,$baseurl,$attachmenturl,$recusers,
735: $recudoms,$sentmsgid,undef,undef,$symb,$error);
736: my $status = &store_sent_mail($sentmsgid,$sentmessage,$senderuname,
737: $senderdom,$senderhome);
738: return $status;
739: }
740:
741: sub store_sent_mail {
742: my ($msgid,$message,$senderuname,$senderdom,$senderhome) = @_;
743: if ($senderuname eq '') {
744: $senderuname = $env{'user.name'};
745: }
746: if ($senderdom eq '') {
747: $senderdom = $env{'user.domain'};
748: }
749: if ($senderhome eq '') {
750: $senderhome = $env{'user.home'};
751: }
752: my $status =' '.&Apache::lonnet::critical(
753: 'put:'.$senderdom.':'.$senderuname.':nohist_email_sent:'.
754: &escape($msgid).'='.&escape($message),$senderhome);
755: return $status;
756: }
757:
758: sub store_recipients {
759: my ($subject,$sendername,$senderdom,$reciphash) = @_;
760: my $context = &get_course_context();
761: my $now = time();
762: my $msgcount = &get_uniq();
763: my $recipid =
764: &buildmsgid($now,$subject,$sendername,$senderdom,$msgcount,$context,$$);
765: my %recipinfo = (
766: $recipid => $reciphash,
767: );
768: my $status = &Apache::lonnet::put('nohist_emailrecip',\%recipinfo,
769: $senderdom,$sendername);
770: if ($status eq 'ok') {
771: return ($recipid,$status);
772: } else {
773: return (undef,$status);
774: }
775: }
776:
777: # =============================================================== Folder suffix
778:
779: sub foldersuffix {
780: my $folder=shift;
781: unless ($folder) { return ''; }
782: my $suffix;
783: my %folderhash = &get_user_folders($folder);
784: if (ref($folderhash{$folder}) eq 'HASH') {
785: $suffix = '_'.&escape($folderhash{$folder}{'id'});
786: } else {
787: $suffix = '_'.&escape($folder);
788: }
789: return $suffix;
790: }
791:
792: # ========================================================= User-defined folders
793:
794: sub get_user_folders {
795: my ($folder) = @_;
796: my %userfolders =
797: &Apache::lonnet::dump('email_folders',undef,undef,$folder);
798: my $lock = "\0".'lock_counter'; # locks db while counter incremented
799: my $counter = "\0".'idcount'; # used in suffix for email db files
800: if (defined($userfolders{$lock})) {
801: delete($userfolders{$lock});
802: }
803: if (defined($userfolders{$counter})) {
804: delete($userfolders{$counter});
805: }
806: return %userfolders;
807: }
808:
809: sub secapply {
810: my $rec=shift;
811: my $defaultflag=shift;
812: $rec=~s/\s+//g;
813: $rec=~s/\@/\:/g;
814: my ($adr,$sections_or_groups)=($rec=~/^([^\(]+)\(([^\)]+)\)/);
815: if ($sections_or_groups) {
816: foreach my $item (split(/\;/,$sections_or_groups)) {
817: if (($item eq $env{'request.course.sec'}) ||
818: ($defaultflag && ($item eq '*'))) {
819: return $adr;
820: } elsif ($env{'request.course.groups'}) {
821: my @usersgroups = split(/:/,$env{'request.course.groups'});
822: if (grep(/^\Q$item\E$/,@usersgroups)) {
823: return $adr;
824: }
825: }
826: }
827: } else {
828: return $rec;
829: }
830: return '';
831: }
832:
833: =pod
834:
835: =item * B<decide_receiver($feedurl,$author,$question,$course,$policy,$defaultflag)>:
836:
837: Arguments
838: $feedurl - /res/ url of resource (only need if $author is true)
839: $author,$question,$course,$policy - all true/false parameters
840: if true will attempt to find the addresses of user that should receive
841: this type of feedback (author - feedback to author of resource $feedurl,
842: $question 'Resource Content Questions', $course 'Course Content Question',
843: $policy 'Course Policy')
844: (Additionally it also checks $env for whether the corresponding form.<name>
845: element exists, for ease of use in a html response context)
846:
847: $defaultflag - (internal should be left blank) if true gather addresses
848: that aren't for a section even if I have a section
849: (used for reccursion internally, first we look for
850: addresses for our specific section then we recurse
851: and look for non section addresses)
852:
853: Returns
854: $typestyle - string of html text, describing what addresses were found
855: %to - a hash, which keys are addresses of users to send messages to
856: the keys will look like name:domain
857:
858: =cut
859:
860: sub decide_receiver {
861: my ($feedurl,$author,$question,$course,$policy,$defaultflag) = @_;
862: &Apache::lonenc::check_decrypt(\$feedurl);
863: my $typestyle='';
864: my %to=();
865: if ($env{'form.discuss'} eq 'author' ||$author) {
866: $typestyle.='Submitting as Author Feedback<br />';
867: $feedurl=~ m{^/res/($LONCAPA::domain_re)/($LONCAPA::username_re)/};
868: $to{$2.':'.$1}=1;
869: }
870: my $cid = $env{'request.course.id'};
871: if ($env{'form.discuss'} eq 'question' ||$question) {
872: $typestyle.=&mt('Submitting as Question').'<br />';
873: foreach my $item (split(/\,/,$env{'course.'.$cid.'.question.email'})) {
874: my $rec=&secapply($item,$defaultflag);
875: if ($rec) { $to{$rec}=1; }
876: }
877: }
878: if ($env{'form.discuss'} eq 'course' ||$course) {
879: $typestyle.=&mt('Submitting as Comment').'<br />';
880: foreach my $item (split(/\,/,$env{'course.'.$cid.'.comment.email'})) {
881: my $rec=&secapply($item,$defaultflag);
882: if ($rec) { $to{$rec}=1; }
883: }
884: }
885: if ($env{'form.discuss'} eq 'policy' ||$policy) {
886: $typestyle.=&mt('Submitting as Policy Feedback').'<br />';
887: foreach my $item (split(/\,/,$env{'course.'.$cid.'.policy.email'})) {
888: my $rec=&secapply($item,$defaultflag);
889: if ($rec) { $to{$rec}=1; }
890: }
891: }
892: if ((scalar(%to) eq '0') && (!$defaultflag)) {
893: ($typestyle,%to)=
894: &decide_receiver($feedurl,$author,$question,$course,$policy,1);
895: }
896: return ($typestyle,%to);
897: }
898:
899: =pod
900:
901: =back
902:
903: =cut
904:
905: 1;
906: __END__
907:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>