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