Annotation of loncom/interface/lonmsg.pm, revision 1.221
1.1 www 1: # The LearningOnline Network with CAPA
1.26 albertel 2: # Routines for messaging
3: #
1.221 ! raeburn 4: # $Id: lonmsg.pm,v 1.220 2009/01/04 16:20:27 raeburn Exp $
1.26 albertel 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/
1.1 www 27: #
1.75 www 28:
1.1 www 29: package Apache::lonmsg;
30:
1.199 raeburn 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:
1.215 jms 56: =head1 SUBROUTINES
1.199 raeburn 57:
1.215 jms 58: =over
59:
60: =pod
61:
62: =item packagemsg()
63:
64: Package
65:
66: =item get_course_context()
67:
68: =item unpackagemsg()
69:
70: Unpack message into a hash
71:
72: =item buildmsgid()
73:
74: Get info out of msgid
75:
76: =item unpackmsgid()
77:
78: =item sendemail()
79:
80: =item sendnotification()
81:
82: Send notification emails
83:
84: =item newmail()
85:
86: Check for email
87:
88: =item author_res_msg()
89:
90: Automated message to the author of a resource
91:
92: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
93: of the resource with the URI $filename.
94:
95: =item retrieve_author_res_msg()
96:
97: Retrieve author resource messages
98:
99: =item del_url_author_res_msg()
100:
101: Delete all author messages related to one URL
102:
103: =item clear_author_res_msg()
104:
105: Clear out all author messages in URL path
106:
107: =item all_url_author_res_msg()
108:
109: Return hash with URLs for which there is a resource message
110:
111: =item store_instructor_comment()
112:
113: Add a comment to the User Notes screen
114:
115: =item user_crit_msg_raw()
116:
117: Critical message to a user
118:
119: =item user_crit_msg()
120:
121: New routine that respects "forward" and calls old routine
122:
123: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback, $nosentstore,$recipid,$attachmenturl)>:
124: Sends a critical message $message to the $user at $domain. If $sendback
125: is true, a receipt will be sent to the current user when $user receives
126: the message.
127:
128: Additionally it will check if the user has a Forwarding address
129: set, and send the message to that address instead
130:
131: returns
132: - in array context a list of results for each message that was sent
133: - in scalar context a space seperated list of results for each
134: message sent
135:
136:
137: =item user_crit_received()
138:
139: Critical message received
140:
141: =item user_normal_msg_raw()
142:
143: Normal communication
144:
145: =item user_normal_msg()
146:
147: New routine that respects "forward" and calls old routine
148:
149: =item * B<user_normal_msg($user, $domain, $subject, $message, $citation,
150: $baseurl, $attachmenturl, $toperm, $sentmessage, $symb, $restitle,
151: $error,$nosentstore,$recipid)>:
152: Sends a message to the $user at $domain, with subject $subject and message $message.
153:
154: Additionally it will check if the user has a Forwarding address
155: set, and send the message to that address instead
156:
157: returns
158: - in array context a list of results for each message that was sent
159: - in scalar context a space seperated list of results for each
160: message sent
161:
162: =item store_sent_mail()
163:
164: =item store_recipients()
165:
166: =item foldersuffix()
167:
168: =item get_user_folders()
169:
170: User-defined folders
171:
172: =item secapply()
173:
174: =item B<decide_receiver($feedurl,$author,$question,$course,$policy,$defaultflag)>:
175:
176: Arguments
177: $feedurl - /res/ url of resource (only need if $author is true)
178: $author,$question,$course,$policy - all true/false parameters
179: if true will attempt to find the addresses of user that should receive
180: this type of feedback (author - feedback to author of resource $feedurl,
181: $question 'Resource Content Questions', $course 'Course Content Question',
182: $policy 'Course Policy')
183: (Additionally it also checks $env for whether the corresponding form.<name>
184: element exists, for ease of use in a html response context)
185:
186: $defaultflag - (internal should be left blank) if true gather addresses
187: that aren't for a section even if I have a section
188: (used for reccursion internally, first we look for
189: addresses for our specific section then we recurse
190: and look for non section addresses)
191:
192: Returns
193: $typestyle - string of html text, describing what addresses were found
194: %to - a hash, which keys are addresses of users to send messages to
195: the keys will look like name:domain
196:
197: =item user_lang()
198:
199: =back
1.199 raeburn 200:
201: =cut
202:
1.1 www 203: use strict;
1.140 albertel 204: use Apache::lonnet;
1.47 albertel 205: use HTML::TokeParser();
1.180 albertel 206: use Apache::lonlocal;
1.53 www 207: use Mail::Send;
1.187 albertel 208: use LONCAPA qw(:DEFAULT :match);
1.180 albertel 209:
210: {
211: my $uniq;
212: sub get_uniq {
213: $uniq++;
214: return $uniq;
215: }
216: }
1.65 www 217:
1.215 jms 218:
1.1 www 219:
1.3 www 220: sub packagemsg {
1.108 www 221: my ($subject,$message,$citation,$baseurl,$attachmenturl,
1.202 raeburn 222: $recuser,$recdomain,$msgid,$type,$crsmsgid,$symb,$error,$recipid)=@_;
1.96 albertel 223: $message =&HTML::Entities::encode($message,'<>&"');
224: $citation=&HTML::Entities::encode($citation,'<>&"');
225: $subject =&HTML::Entities::encode($subject,'<>&"');
1.49 albertel 226: #remove machine specification
1.218 raeburn 227: $baseurl =~ s|^https?://[^/]+/|/|;
1.96 albertel 228: $baseurl =&HTML::Entities::encode($baseurl,'<>&"');
1.51 www 229: #remove machine specification
1.218 raeburn 230: $attachmenturl =~ s|^https?://[^/]+/|/|;
1.96 albertel 231: $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
1.201 raeburn 232: my $course_context = &get_course_context();
1.2 www 233: my $now=time;
1.180 albertel 234: my $msgcount = &get_uniq();
1.156 raeburn 235: unless(defined($msgid)) {
1.159 raeburn 236: $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},
1.191 raeburn 237: $msgcount,$course_context,$symb,$error,$$);
1.156 raeburn 238: }
1.174 raeburn 239: my $result = '<sendername>'.$env{'user.name'}.'</sendername>'.
1.140 albertel 240: '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
1.1 www 241: '<subject>'.$subject.'</subject>'.
1.174 raeburn 242: '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>';
243: if (defined($crsmsgid)) {
244: $result.= '<courseid>'.$course_context.'</courseid>'.
245: '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
246: '<msgid>'.$msgid.'</msgid>'.
247: '<coursemsgid>'.$crsmsgid.'</coursemsgid>'.
248: '<message>'.$message.'</message>';
249: return ($msgid,$result);
250: }
251: $result .= '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
1.1 www 252: '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
253: '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
1.140 albertel 254: '<browsertype>'.$env{'browser.type'}.'</browsertype>'.
255: '<browseros>'.$env{'browser.os'}.'</browseros>'.
256: '<browserversion>'.$env{'browser.version'}.'</browserversion>'.
257: '<browsermathml>'.$env{'browser.mathml'}.'</browsermathml>'.
1.1 www 258: '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
1.158 raeburn 259: '<courseid>'.$course_context.'</courseid>'.
1.140 albertel 260: '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
261: '<role>'.$env{'request.role'}.'</role>'.
262: '<resource>'.$env{'request.filename'}.'</resource>'.
1.156 raeburn 263: '<msgid>'.$msgid.'</msgid>';
1.214 raeburn 264: if (defined($env{'form.group'})) {
265: $result .= '<group>'.$env{'form.group'}.'</group>';
266: }
1.156 raeburn 267: if (ref($recuser) eq 'ARRAY') {
268: for (my $i=0; $i<@{$recuser}; $i++) {
1.162 raeburn 269: if ($type eq 'dcmail') {
270: my ($username,$email) = split(/:/,$$recuser[$i]);
1.184 www 271: $username = &unescape($username);
272: $email = &unescape($email);
1.162 raeburn 273: $username = &HTML::Entities::encode($username,'<>&"');
274: $email = &HTML::Entities::encode($email,'<>&"');
275: $result .= '<recipient username="'.$username.'">'.
276: $email.'</recipient>';
277: } else {
278: $result .= '<recuser>'.$$recuser[$i].'</recuser>'.
279: '<recdomain>'.$$recdomain[$i].'</recdomain>';
280: }
1.156 raeburn 281: }
282: } else {
283: $result .= '<recuser>'.$recuser.'</recuser>'.
284: '<recdomain>'.$recdomain.'</recdomain>';
285: }
286: $result .= '<message>'.$message.'</message>';
1.49 albertel 287: if (defined($citation)) {
288: $result.='<citation>'.$citation.'</citation>';
289: }
290: if (defined($baseurl)) {
291: $result.= '<baseurl>'.$baseurl.'</baseurl>';
292: }
1.51 www 293: if (defined($attachmenturl)) {
1.52 www 294: $result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
1.51 www 295: }
1.191 raeburn 296: if (defined($symb)) {
297: $result.= '<symb>'.$symb.'</symb>';
1.201 raeburn 298: if ($course_context ne '') {
1.191 raeburn 299: if ($course_context eq $env{'request.course.id'}) {
300: my $resource_title = &Apache::lonnet::gettitle($symb);
301: if (defined($resource_title)) {
302: $result .= '<resource_title>'.$resource_title.'</resource_title>';
303: }
304: }
305: }
306: }
1.202 raeburn 307: if (defined($recipid)) {
308: $result.= '<recipid>'.$recipid.'</recipid>';
309: }
1.204 raeburn 310: if ($env{'form.can_reply'} eq 'N') {
311: $result .= '<noreplies>1</noreplies>';
312: }
313: if ($env{'form.reply_to_addr'}) {
314: my ($replytoname,$replytodom) = split(/:/,$env{'form.reply_to_addr'});
315: if (!($replytoname eq $env{'user.name'} && $replytodom eq $env{'user.domain'})) {
316: if (&Apache::lonnet::homeserver($replytoname,$replytodom) ne 'no_host') {
317: $result .= '<replytoaddr>'.$env{'form.reply_to_addr'}.'</replytoaddr>';
318: }
319: }
320: }
1.191 raeburn 321: return ($msgid,$result);
1.1 www 322: }
323:
1.201 raeburn 324: sub get_course_context {
325: my $course_context;
1.212 raeburn 326: my $msgkey;
1.201 raeburn 327: if (defined($env{'form.replyid'})) {
1.212 raeburn 328: $msgkey = $env{'form.replyid'};
329: } elsif (defined($env{'form.forwid'})) {
330: $msgkey = $env{'form.forwid'}
331: } elsif (defined($env{'form.multiforwid'})) {
332: $msgkey = $env{'form.multiforwid'};
333: }
334: if ($msgkey ne '') {
1.201 raeburn 335: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid)=
1.212 raeburn 336: split(/\:/,&unescape($msgkey));
1.201 raeburn 337: $course_context = $origcid;
338: }
339: foreach my $key (keys(%env)) {
340: if ($key=~/^form\.(rep)?rec\_(.*)$/) {
341: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) =
342: split(/\:/,&unescape($2));
343: $course_context = $origcid;
344: last;
345: }
346: }
347: if ($course_context eq '') {
348: $course_context = $env{'request.course.id'};
349: }
350: return $course_context;
351: }
352:
1.2 www 353:
1.3 www 354: sub unpackagemsg {
1.212 raeburn 355: my ($message,$notoken,$noattachmentlink)=@_;
1.2 www 356: my %content=();
357: my $parser=HTML::TokeParser->new(\$message);
358: my $token;
359: while ($token=$parser->get_token) {
360: if ($token->[0] eq 'S') {
361: my $entry=$token->[1];
362: my $value=$parser->get_text('/'.$entry);
1.156 raeburn 363: if (($entry eq 'recuser') || ($entry eq 'recdomain')) {
364: push(@{$content{$entry}},$value);
1.162 raeburn 365: } elsif ($entry eq 'recipient') {
366: my $username = $token->[2]{'username'};
367: $username = &HTML::Entities::decode($username,'<>&"');
368: $content{$entry}{$username} = $value;
1.156 raeburn 369: } else {
370: $content{$entry}=$value;
371: }
1.2 www 372: }
373: }
1.168 albertel 374: if (!exists($content{'recuser'})) { $content{'recuser'} = []; }
1.212 raeburn 375: if (($content{'attachmenturl'}) && (!$noattachmentlink)) {
1.100 albertel 376: my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
1.52 www 377: if ($notoken) {
1.100 albertel 378: $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';
1.52 www 379: } else {
1.99 albertel 380: &Apache::lonnet::allowuploaded('/adm/msg',
381: $content{'attachmenturl'});
382: $content{'message'}.='<p>'.&mt('Attachment').
383: ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
1.100 albertel 384: $fname.'</tt></a>';
1.52 www 385: }
386: }
1.2 www 387: return %content;
388: }
389:
1.6 www 390:
1.159 raeburn 391: sub buildmsgid {
1.191 raeburn 392: my ($now,$subject,$uname,$udom,$msgcount,$course_context,$symb,$error,$pid) = @_;
1.184 www 393: $subject=&escape($subject);
1.192 raeburn 394: $symb = &escape($symb);
1.184 www 395: return(&escape($now.':'.$subject.':'.$uname.':'.
1.191 raeburn 396: $udom.':'.$msgcount.':'.$course_context.':'.$pid.':'.$symb.':'.$error));
1.159 raeburn 397: }
398:
1.6 www 399: sub unpackmsgid {
1.169 albertel 400: my ($msgid,$folder,$skipstatus,$status_cache)=@_;
1.184 www 401: $msgid=&unescape($msgid);
1.167 raeburn 402: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid,
1.191 raeburn 403: $processid,$symb,$error) = split(/\:/,&unescape($msgid));
1.184 www 404: $shortsubj = &unescape($shortsubj);
1.182 albertel 405: $shortsubj = &HTML::Entities::decode($shortsubj);
1.192 raeburn 406: $symb = &unescape($symb);
1.167 raeburn 407: if (!defined($processid)) { $fromcid = ''; }
1.164 raeburn 408: my %status=();
409: unless ($skipstatus) {
1.169 albertel 410: if (ref($status_cache)) {
411: $status{$msgid} = $status_cache->{$msgid};
412: } else {
413: my $suffix=&foldersuffix($folder);
414: %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
415: }
416: if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
1.164 raeburn 417: unless ($status{$msgid}) { $status{$msgid}='new'; }
418: }
1.191 raeburn 419: return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid,$symb,$error);
1.141 raeburn 420: }
1.6 www 421:
1.53 www 422:
423: sub sendemail {
1.213 raeburn 424: my ($to,$subject,$body,$to_uname,$to_udom,$user_lh)=@_;
1.186 www 425: my $senderaddress='';
1.220 raeburn 426: my $replytoaddress='';
427: if ($env{'form.can_reply'} eq 'N') {
428: my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
429: my $hostname = &Apache::lonnet::hostname($lonhost);
430: $replytoaddress = 'do-not-reply@'.$hostname;
431: } else {
432: my %senderemails;
433: my $have_sender;
434: if ($env{'form.reply_to_addr'}) {
435: my ($replytoname,$replytodom) = split(/:/,$env{'form.reply_to_addr'});
436: if (!($replytoname eq $env{'user.name'} && $replytodom eq $env{'user.domain'})) {
437: if (&Apache::lonnet::homeserver($replytoname,$replytodom) ne 'no_host') {
438: %senderemails =
439: &Apache::loncommon::getemails($replytoname,$replytodom);
440: $have_sender = 1;
441: }
442: }
443: }
444: if (!$have_sender) {
445: %senderemails=&Apache::loncommon::getemails();
446: }
447: foreach my $type ('permanentemail','critnotification','notification') {
448: if ($senderemails{$type}) {
449: ($senderaddress) = split(/,/,$senderemails{$type});
450: last if ($senderaddress);
451: }
452: }
1.186 www 453: }
1.53 www 454: $body=
1.217 schafran 455: "*** ".&mt_user($user_lh,'This is an automatic e-mail generated by the LON-CAPA system.')."\n".
456: "*** ".($senderaddress?&mt_user($user_lh,'You can reply to this e-mail'):&mt_user($user_lh,'Please do not reply to this address.')."\n*** ".
1.220 raeburn 457: &mt_user($user_lh,'A reply will not be received by the recipient!'))."\n\n".$body;
1.53 www 458: my $msg = new Mail::Send;
459: $msg->to($to);
460: $msg->subject('[LON-CAPA] '.$subject);
1.220 raeburn 461: if ($replytoaddress) {
462: $msg->add('Reply-to',$replytoaddress);
463: }
464: if ($senderaddress) {
465: $msg->add('From',$senderaddress);
466: }
1.97 matthew 467: if (my $fh = $msg->open()) {
1.172 albertel 468: print $fh $body;
469: $fh->close;
1.68 www 470: }
1.53 www 471: }
472:
473: # ==================================================== Send notification emails
474:
475: sub sendnotification {
1.194 raeburn 476: my ($to,$touname,$toudom,$subj,$crit,$text,$msgid)=@_;
1.140 albertel 477: my $sender=$env{'environment.firstname'}.' '.$env{'environment.lastname'};
1.131 www 478: unless ($sender=~/\w/) {
1.208 raeburn 479: $sender=$env{'user.name'}.':'.$env{'user.domain'};
1.131 www 480: }
1.53 www 481: my $critical=($crit?' critical':'');
1.208 raeburn 482:
1.131 www 483: $text=~s/\<\;/\</gs;
484: $text=~s/\>\;/\>/gs;
1.218 raeburn 485: my $homeserver = &Apache::lonnet::homeserver($touname,$toudom);
486: my $protocol = $Apache::lonnet::protocol{$homeserver};
487: $protocol = 'http' if ($protocol ne 'https');
488: my $url = $protocol.'://'.&Apache::lonnet::hostname($homeserver).
489: '/adm/email?username='.$touname.'&domain='.$toudom;
1.194 raeburn 490: my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid,
491: $symb,$error) = &Apache::lonmsg::unpackmsgid($msgid);
1.208 raeburn 492: my ($coursetext,$body,$bodybegin,$bodysubj,$bodyend);
1.213 raeburn 493: my $user_lh = &user_lang($touname,$toudom,$fromcid);
1.194 raeburn 494: if ($fromcid ne '') {
1.213 raeburn 495: $coursetext = "\n".&mt_user($user_lh,'Course').': ';
1.194 raeburn 496: if ($env{'course.'.$fromcid.'.description'} ne '') {
497: $coursetext .= $env{'course.'.$fromcid.'.description'};
498: } else {
499: my %coursehash = &Apache::lonnet::coursedescription($fromcid,);
500: if ($coursehash{'description'} ne '') {
501: $coursetext .= $coursehash{'description'};
502: }
503: }
504: $coursetext .= "\n\n";
505: }
1.206 raeburn 506: my @recipients = split(/,/,$to);
1.208 raeburn 507: $bodybegin = $coursetext.
1.213 raeburn 508: &mt_user($user_lh,
509: 'You received a'.$critical.' message from [_1] in LON-CAPA.',$sender).' ';
510: $bodysubj = &mt_user($user_lh,'The subject is
1.53 www 511:
1.195 raeburn 512: [_1]
1.53 www 513:
1.195 raeburn 514: ',$subj)."\n".
1.213 raeburn 515: '=== '.&mt_user($user_lh,'Excerpt')." ============================================================
1.206 raeburn 516: ";
517: $bodyend = "
1.131 www 518: ========================================================================
519:
1.213 raeburn 520: ".&mt_user($user_lh,'Use
1.53 www 521:
1.195 raeburn 522: [_1]
1.53 www 523:
1.195 raeburn 524: to access the full message.',$url);
1.206 raeburn 525: my %userenv = &Apache::lonnet::get('environment',['notifywithhtml'],$toudom,$touname);
1.213 raeburn 526: my $subject = &mt_user($user_lh,"'New' $critical message from ").$sender;
1.208 raeburn 527:
528: my ($blocked,$blocktext);
529: if (!$crit) {
530: my %setters;
531: my ($startblock,$endblock) =
532: &Apache::loncommon::blockcheck(\%setters,'com',$touname,$toudom);
533: if ($startblock && $endblock) {
534: $blocked = 1;
1.209 albertel 535: my $showstart = &Apache::lonlocal::locallocaltime($startblock);
536: my $showend = &Apache::lonlocal::locallocaltime($endblock);
1.213 raeburn 537: $blocktext = &mt_user($user_lh,'LON-CAPA messages sent to you between [_1] and [_2] will be inaccessible until the end of this time period, because you are a student in a course with an active communications block.',$showstart,$showend);
1.208 raeburn 538: }
539: }
1.206 raeburn 540: if ($userenv{'notifywithhtml'} ne '') {
541: my @htmlexcerpt = split(/,/,$userenv{'notifywithhtml'});
542: foreach my $addr (@recipients) {
1.208 raeburn 543: if ($blocked) {
544: $body = $bodybegin."\n".$blocktext."\n".$bodyend;
545: } else {
546: my $sendtext = $text;
547: if (!grep/^\Q$addr\E/,@htmlexcerpt) {
548: $sendtext =~ s/\<\/*[^\>]+\>//gs;
549: }
550: $body = $bodybegin.$bodysubj.$sendtext.$bodyend;
1.206 raeburn 551: }
1.213 raeburn 552: &sendemail($addr,$subject,$body,$touname,$toudom,$user_lh);
1.206 raeburn 553: }
554: } else {
1.208 raeburn 555: if ($blocked) {
556: $body = $bodybegin."\n".$blocktext."\n".$bodyend;
557: } else {
558: $text =~ s/\<\/*[^\>]+\>//gs;
559: $body = $bodybegin.$bodysubj.$text.$bodyend;
560: }
1.213 raeburn 561: &sendemail($to,$subject,$body,$touname,$toudom,$user_lh);
1.206 raeburn 562: }
1.53 www 563: }
1.215 jms 564:
1.40 www 565:
566: sub newmail {
1.140 albertel 567: if ((time-$env{'user.mailcheck.time'})>300) {
1.40 www 568: my %what=&Apache::lonnet::get('email_status',['recnewemail']);
1.211 raeburn 569: &Apache::lonnet::appenv({'user.mailcheck.time'=>time});
1.40 www 570: if ($what{'recnewemail'}>0) { return 1; }
571: }
572: return 0;
573: }
574:
1.1 www 575:
1.58 bowersj2 576:
1.1 www 577: sub author_res_msg {
578: my ($filename,$message)=@_;
1.2 www 579: unless ($message) { return 'empty'; }
1.1 www 580: $filename=&Apache::lonnet::declutter($filename);
1.72 www 581: my ($domain,$author,@dummy)=split(/\//,$filename);
1.1 www 582: my $homeserver=&Apache::lonnet::homeserver($author,$domain);
583: if ($homeserver ne 'no_host') {
584: my $id=unpack("%32C*",$message);
1.181 albertel 585: $message .= " <p>This error occurred on machine ".
586: $Apache::lonnet::perlvar{'lonHostID'}."</p>";
1.2 www 587: my $msgid;
1.72 www 588: ($msgid,$message)=&packagemsg($filename,$message);
1.3 www 589: return &Apache::lonnet::reply('put:'.$domain.':'.$author.
1.72 www 590: ':nohist_res_msgs:'.
1.184 www 591: &escape($filename.'_'.$id).'='.
592: &escape($message),$homeserver);
1.1 www 593: }
1.2 www 594: return 'no_host';
1.73 www 595: }
596:
1.215 jms 597:
1.73 www 598:
599: sub retrieve_author_res_msg {
1.75 www 600: my $url=shift;
1.73 www 601: $url=&Apache::lonnet::declutter($url);
1.187 albertel 602: my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.76 www 603: my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
1.73 www 604: my $msgs='';
605: foreach (keys %errormsgs) {
1.80 www 606: if ($_=~/^\Q$url\E\_\d+$/) {
1.73 www 607: my %content=&unpackagemsg($errormsgs{$_});
1.74 www 608: $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
609: $content{'time'}.'</b>: '.$content{'message'}.
610: '<br /></p>';
1.73 www 611: }
612: }
613: return $msgs;
614: }
615:
616:
1.215 jms 617:
618:
1.73 www 619:
620: sub del_url_author_res_msg {
1.75 www 621: my $url=shift;
1.73 www 622: $url=&Apache::lonnet::declutter($url);
1.187 albertel 623: my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.77 www 624: my @delmsgs=();
625: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
626: if ($_=~/^\Q$url\E\_\d+$/) {
627: push (@delmsgs,$_);
628: }
629: }
630: return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
1.73 www 631: }
1.215 jms 632:
1.73 www 633:
1.152 www 634: sub clear_author_res_msg {
635: my $url=shift;
636: $url=&Apache::lonnet::declutter($url);
1.187 albertel 637: my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.152 www 638: my @delmsgs=();
639: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
640: if ($_=~/^\Q$url\E/) {
641: push (@delmsgs,$_);
642: }
643: }
644: return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
645: }
1.215 jms 646:
647:
1.73 www 648:
649: sub all_url_author_res_msg {
650: my ($author,$domain)=@_;
1.75 www 651: my %returnhash=();
1.76 www 652: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
1.75 www 653: $_=~/^(.+)\_\d+/;
654: $returnhash{$1}=1;
655: }
656: return %returnhash;
1.1 www 657: }
658:
1.185 albertel 659:
660: sub store_instructor_comment {
661: my ($msg,$uname,$udom) = @_;
662: my $cid = $env{'request.course.id'};
663: my $cnum = $env{'course.'.$cid.'.num'};
664: my $cdom = $env{'course.'.$cid.'.domain'};
665: my $subject= &mt('Record').' ['.$uname.':'.$udom.']';
666: my $result = &user_normal_msg_raw($cnum,$cdom,$subject,$msg);
1.201 raeburn 667: if ($result eq 'ok' || $result eq 'con_delayed') {
668:
669: }
1.185 albertel 670: return $result;
671: }
672:
1.1 www 673:
1.38 www 674: sub user_crit_msg_raw {
1.201 raeburn 675: my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage,
1.212 raeburn 676: $nosentstore,$recipid,$attachmenturl)=@_;
1.2 www 677: # Check if allowed missing
1.190 raeburn 678: my ($status,$packed_message);
1.2 www 679: my $msgid='undefined';
680: unless (($message)&&($user)&&($domain)) { $status='empty'; };
1.131 www 681: my $text=$message;
1.2 www 682: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
683: if ($homeserver ne 'no_host') {
1.202 raeburn 684: ($msgid,$packed_message)=&packagemsg($subject,$message,undef,undef,
1.212 raeburn 685: $attachmenturl,undef,undef,undef,undef,undef,
686: undef,undef,$recipid);
1.190 raeburn 687: if ($sendback) { $packed_message.='<sendback>true</sendback>'; }
1.210 albertel 688: $status=&Apache::lonnet::cput('critical', {$msgid => $packed_message},
689: $domain,$user);
1.159 raeburn 690: if (defined($sentmessage)) {
1.190 raeburn 691: $$sentmessage = $packed_message;
1.159 raeburn 692: }
1.201 raeburn 693: if (!$nosentstore) {
1.193 raeburn 694: (undef,my $packed_message_no_citation) =
1.212 raeburn 695: &packagemsg($subject,$message,undef,undef,$attachmenturl,$user,
696: $domain,$msgid);
1.193 raeburn 697: if ($status eq 'ok' || $status eq 'con_delayed') {
698: &store_sent_mail($msgid,$packed_message_no_citation);
699: }
700: }
1.2 www 701: } else {
702: $status='no_host';
703: }
1.190 raeburn 704:
1.53 www 705: # Notifications
1.186 www 706: my %userenv = &Apache::loncommon::getemails($user,$domain);
1.53 www 707: if ($userenv{'critnotification'}) {
1.131 www 708: &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1,
1.194 raeburn 709: $text,$msgid);
1.53 www 710: }
1.132 www 711: if ($toperm && $userenv{'permanentemail'}) {
712: &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,1,
1.194 raeburn 713: $text,$msgid);
1.132 www 714: }
1.53 www 715: # Log this
1.2 www 716: &Apache::lonnet::logthis(
1.4 www 717: 'Sending critical email '.$msgid.
1.2 www 718: ', log status: '.
1.140 albertel 719: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
720: $env{'user.home'},
1.2 www 721: 'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
1.4 www 722: .$status));
1.2 www 723: return $status;
724: }
725:
1.38 www 726:
1.58 bowersj2 727:
1.38 www 728: sub user_crit_msg {
1.201 raeburn 729: my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage,
1.212 raeburn 730: $nosentstore,$recipid,$attachmenturl)=@_;
1.183 albertel 731: my @status;
1.38 www 732: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
733: $domain,$user);
734: my $msgforward=$userenv{'msgforward'};
735: if ($msgforward) {
1.183 albertel 736: foreach my $addr (split(/\,/,$msgforward)) {
737: my ($forwuser,$forwdomain)=split(/\:/,$addr);
738: push(@status,
739: &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
1.202 raeburn 740: $sendback,$toperm,$sentmessage,$nosentstore,
1.212 raeburn 741: $recipid,$attachmenturl));
1.38 www 742: }
743: } else {
1.183 albertel 744: push(@status,
745: &user_crit_msg_raw($user,$domain,$subject,$message,$sendback,
1.212 raeburn 746: $toperm,$sentmessage,$nosentstore,$recipid,
747: $attachmenturl));
1.38 www 748: }
1.183 albertel 749: if (wantarray) {
750: return @status;
751: }
752: return join(' ',@status);
1.38 www 753: }
754:
1.2 www 755:
756: sub user_crit_received {
1.12 www 757: my $msgid=shift;
758: my %message=&Apache::lonnet::get('critical',[$msgid]);
1.52 www 759: my %contents=&unpackagemsg($message{$msgid},1);
1.204 raeburn 760: my $destname = $contents{'sendername'};
761: my $destdom = $contents{'senderdomain'};
762: if ($contents{'replytoaddr'}) {
763: my ($repname,$repdom) = split(/:/,$contents{'replytoaddr'});
764: if (&Apache::lonnet::homeserver($repname,$repdom) ne 'no_host') {
765: $destname = $repname;
766: $destdom = $repdom;
767: }
768: }
1.24 www 769: my $status='rec: '.($contents{'sendback'}?
1.204 raeburn 770: &user_normal_msg($destname,$destdom,&mt('Receipt').': '.$env{'user.name'}.
771: ' '.&mt('at').' '.$env{'user.domain'}.', '.
772: $contents{'subject'},&mt('User').' '.$env{'user.name'}.
773: ' '.&mt('at').' '.$env{'user.domain'}.
774: ' acknowledged receipt of message'."\n".' "'.
775: $contents{'subject'}.'"'."\n".&mt('dated').' '.
776: $contents{'time'}.".\n"
777: ):'no msg req');
1.5 www 778: $status.=' trans: '.
1.12 www 779: &Apache::lonnet::put(
780: 'nohist_email',{$contents{'msgid'} => $message{$msgid}});
1.5 www 781: $status.=' del: '.
1.9 albertel 782: &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.140 albertel 783: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
784: $env{'user.home'},'Received critical message '.
1.5 www 785: $contents{'msgid'}.
786: ', '.$status);
1.12 www 787: return $status;
1.2 www 788: }
789:
1.215 jms 790:
791:
1.2 www 792:
1.38 www 793: sub user_normal_msg_raw {
1.132 www 794: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.191 raeburn 795: $toperm,$currid,$newid,$sentmessage,$crsmsgid,$symb,$restitle,
1.202 raeburn 796: $error,$nosentstore,$recipid)=@_;
1.2 www 797: # Check if allowed missing
1.173 albertel 798: my ($status,$packed_message);
1.2 www 799: my $msgid='undefined';
1.131 www 800: my $text=$message;
1.2 www 801: unless (($message)&&($user)&&($domain)) { $status='empty'; };
802: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
803: if ($homeserver ne 'no_host') {
1.173 albertel 804: ($msgid,$packed_message)=
805: &packagemsg($subject,$message,$citation,$baseurl,
1.174 raeburn 806: $attachmenturl,$user,$domain,$currid,
1.202 raeburn 807: undef,$crsmsgid,$symb,$error,$recipid);
1.174 raeburn 808:
1.108 www 809: # Store in user folder
1.210 albertel 810: $status=
811: &Apache::lonnet::cput('nohist_email',{$msgid => $packed_message},
812: $domain,$user);
1.108 www 813: # Save new message received time
1.40 www 814: &Apache::lonnet::put
815: ('email_status',{'recnewemail'=>time},$domain,$user);
1.201 raeburn 816: # Into sent-mail folder if sent mail storage required
817: if (!$nosentstore) {
1.190 raeburn 818: (undef,my $packed_message_no_citation) =
819: &packagemsg($subject,$message,undef,$baseurl,$attachmenturl,
1.191 raeburn 820: $user,$domain,$currid,undef,$crsmsgid,$symb,$error);
1.193 raeburn 821: if ($status eq 'ok' || $status eq 'con_delayed') {
822: &store_sent_mail($msgid,$packed_message_no_citation);
823: }
1.156 raeburn 824: }
1.201 raeburn 825: if (ref($newid) eq 'SCALAR') {
1.196 www 826: $$newid = $msgid;
827: }
1.201 raeburn 828: if (ref($sentmessage) eq 'SCALAR') {
1.196 www 829: $$sentmessage = $packed_message;
830: }
831: # Notifications
1.206 raeburn 832: my %userenv = &Apache::loncommon::getemails($user,$domain);
1.196 www 833: if ($userenv{'notification'}) {
834: &sendnotification($userenv{'notification'},$user,$domain,$subject,0,
835: $text,$msgid);
836: }
837: if ($toperm && $userenv{'permanentemail'}) {
1.214 raeburn 838: if ((!$userenv{'notification'}) || ($userenv{'notification'} ne $userenv{'permanentemail'})) {
839: &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,0,
840: $text,$msgid);
841: }
1.196 www 842: }
843: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
844: $env{'user.home'},
845: 'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
846: } else {
1.2 www 847: $status='no_host';
1.196 www 848: }
1.2 www 849: return $status;
850: }
1.38 www 851:
852: sub user_normal_msg {
1.132 www 853: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.202 raeburn 854: $toperm,$sentmessage,$symb,$restitle,$error,$nosentstore,$recipid)=@_;
1.199 raeburn 855: my @status;
1.38 www 856: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
857: $domain,$user);
858: my $msgforward=$userenv{'msgforward'};
859: if ($msgforward) {
1.171 banghart 860: foreach (split(/\,/,$msgforward)) {
1.172 albertel 861: my ($forwuser,$forwdomain)=split(/\:/,$_);
1.199 raeburn 862: push(@status,
1.171 banghart 863: &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
1.172 albertel 864: $citation,$baseurl,$attachmenturl,$toperm,
1.201 raeburn 865: undef,undef,$sentmessage,undef,$symb,
1.202 raeburn 866: $restitle,$error,$nosentstore,$recipid));
1.171 banghart 867: }
1.191 raeburn 868: } else {
1.199 raeburn 869: push(@status,&user_normal_msg_raw($user,$domain,$subject,$message,
1.172 albertel 870: $citation,$baseurl,$attachmenturl,$toperm,
1.201 raeburn 871: undef,undef,$sentmessage,undef,$symb,
1.202 raeburn 872: $restitle,$error,$nosentstore,$recipid));
1.199 raeburn 873: }
874: if (wantarray) {
875: return @status;
1.38 www 876: }
1.199 raeburn 877: return join(' ',@status);
1.38 www 878: }
879:
1.201 raeburn 880: sub process_sent_mail {
1.214 raeburn 881: my ($msgsubj,$subj_prefix,$numsent,$stamp,$msgname,$msgdom,$msgcount,$context,$pid,$savemsg,$recusers,$recudoms,$baseurl,$attachmenturl,$symb,$error,$senderuname,$senderdom,$recipid) = @_;
1.201 raeburn 882: my $sentsubj;
883: if ($numsent > 1) {
884: $sentsubj = $subj_prefix.' ('.$numsent.' sent) '.$msgsubj;
1.205 raeburn 885: } else {
886: if ($subj_prefix) {
887: $sentsubj = $subj_prefix.' ';
888: }
889: $sentsubj .= $msgsubj;
1.201 raeburn 890: }
891: $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"');
892: my $sentmsgid =
893: &buildmsgid($stamp,$sentsubj,$msgname,$msgdom,$msgcount,$context,$pid);
894: (undef,my $sentmessage) =
895: &packagemsg($msgsubj,$savemsg,undef,$baseurl,$attachmenturl,$recusers,
1.214 raeburn 896: $recudoms,$sentmsgid,undef,undef,$symb,$error,$recipid);
1.201 raeburn 897: my $status = &store_sent_mail($sentmsgid,$sentmessage,$senderuname,
1.210 albertel 898: $senderdom);
1.201 raeburn 899: return $status;
900: }
901:
1.156 raeburn 902: sub store_sent_mail {
1.210 albertel 903: my ($msgid,$message,$senderuname,$senderdom) = @_;
1.201 raeburn 904: if ($senderuname eq '') {
905: $senderuname = $env{'user.name'};
906: }
907: if ($senderdom eq '') {
908: $senderdom = $env{'user.domain'};
909: }
1.210 albertel 910: my $status =' '.&Apache::lonnet::cput('nohist_email_sent',
911: {$msgid => $message},
912: $senderdom,$senderuname);
1.156 raeburn 913: return $status;
914: }
1.2 www 915:
1.202 raeburn 916: sub store_recipients {
917: my ($subject,$sendername,$senderdom,$reciphash) = @_;
918: my $context = &get_course_context();
1.203 albertel 919: my $now = time();
1.202 raeburn 920: my $msgcount = &get_uniq();
921: my $recipid =
922: &buildmsgid($now,$subject,$sendername,$senderdom,$msgcount,$context,$$);
923: my %recipinfo = (
924: $recipid => $reciphash,
925: );
926: my $status = &Apache::lonnet::put('nohist_emailrecip',\%recipinfo,
927: $senderdom,$sendername);
928: if ($status eq 'ok') {
929: return ($recipid,$status);
930: } else {
931: return (undef,$status);
932: }
933: }
934:
1.106 www 935:
936: sub foldersuffix {
937: my $folder=shift;
938: unless ($folder) { return ''; }
1.189 raeburn 939: my $suffix;
940: my %folderhash = &get_user_folders($folder);
941: if (ref($folderhash{$folder}) eq 'HASH') {
942: $suffix = '_'.&escape($folderhash{$folder}{'id'});
943: } else {
944: $suffix = '_'.&escape($folder);
945: }
946: return $suffix;
947: }
948:
949:
950: sub get_user_folders {
951: my ($folder) = @_;
952: my %userfolders =
953: &Apache::lonnet::dump('email_folders',undef,undef,$folder);
954: my $lock = "\0".'lock_counter'; # locks db while counter incremented
955: my $counter = "\0".'idcount'; # used in suffix for email db files
956: if (defined($userfolders{$lock})) {
957: delete($userfolders{$lock});
958: }
959: if (defined($userfolders{$counter})) {
960: delete($userfolders{$counter});
961: }
962: return %userfolders;
1.106 www 963: }
964:
1.197 albertel 965: sub secapply {
966: my $rec=shift;
967: my $defaultflag=shift;
968: $rec=~s/\s+//g;
969: $rec=~s/\@/\:/g;
970: my ($adr,$sections_or_groups)=($rec=~/^([^\(]+)\(([^\)]+)\)/);
971: if ($sections_or_groups) {
972: foreach my $item (split(/\;/,$sections_or_groups)) {
973: if (($item eq $env{'request.course.sec'}) ||
974: ($defaultflag && ($item eq '*'))) {
975: return $adr;
976: } elsif ($env{'request.course.groups'}) {
977: my @usersgroups = split(/:/,$env{'request.course.groups'});
978: if (grep(/^\Q$item\E$/,@usersgroups)) {
979: return $adr;
980: }
981: }
982: }
983: } else {
984: return $rec;
985: }
986: return '';
987: }
988:
989: sub decide_receiver {
990: my ($feedurl,$author,$question,$course,$policy,$defaultflag) = @_;
991: &Apache::lonenc::check_decrypt(\$feedurl);
992: my $typestyle='';
993: my %to=();
994: if ($env{'form.discuss'} eq 'author' ||$author) {
995: $typestyle.='Submitting as Author Feedback<br />';
996: $feedurl=~ m{^/res/($LONCAPA::domain_re)/($LONCAPA::username_re)/};
997: $to{$2.':'.$1}=1;
998: }
999: my $cid = $env{'request.course.id'};
1000: if ($env{'form.discuss'} eq 'question' ||$question) {
1001: $typestyle.=&mt('Submitting as Question').'<br />';
1002: foreach my $item (split(/\,/,$env{'course.'.$cid.'.question.email'})) {
1003: my $rec=&secapply($item,$defaultflag);
1004: if ($rec) { $to{$rec}=1; }
1005: }
1006: }
1007: if ($env{'form.discuss'} eq 'course' ||$course) {
1008: $typestyle.=&mt('Submitting as Comment').'<br />';
1009: foreach my $item (split(/\,/,$env{'course.'.$cid.'.comment.email'})) {
1010: my $rec=&secapply($item,$defaultflag);
1011: if ($rec) { $to{$rec}=1; }
1012: }
1013: }
1014: if ($env{'form.discuss'} eq 'policy' ||$policy) {
1015: $typestyle.=&mt('Submitting as Policy Feedback').'<br />';
1016: foreach my $item (split(/\,/,$env{'course.'.$cid.'.policy.email'})) {
1017: my $rec=&secapply($item,$defaultflag);
1018: if ($rec) { $to{$rec}=1; }
1019: }
1020: }
1021: if ((scalar(%to) eq '0') && (!$defaultflag)) {
1022: ($typestyle,%to)=
1023: &decide_receiver($feedurl,$author,$question,$course,$policy,1);
1024: }
1025: return ($typestyle,%to);
1026: }
1027:
1.213 raeburn 1028: sub user_lang {
1029: my ($touname,$toudom,$fromcid) = @_;
1030: my @userlangs;
1031: if (($fromcid ne '') && ($env{'course.'.$fromcid.'.languages'} ne '')) {
1032: @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
1033: $env{'course.'.$fromcid.'.languages'}));
1034: } else {
1.221 ! raeburn 1035: my %langhash = &Apache::loncommon::getlangs($touname,$toudom);
1.213 raeburn 1036: if ($langhash{'languages'} ne '') {
1.221 ! raeburn 1037: @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
1.213 raeburn 1038: } else {
1039: my %domdefs = &Apache::lonnet::get_domain_defaults($toudom);
1040: if ($domdefs{'lang_def'} ne '') {
1041: @userlangs = ($domdefs{'lang_def'});
1042: }
1043: }
1044: }
1.216 raeburn 1045: my @languages=&Apache::lonlocal::get_genlanguages(@userlangs);
1.213 raeburn 1046: my $user_lh = Apache::localize->get_handle(@languages);
1047: return $user_lh;
1048: }
1049:
1.180 albertel 1050: 1;
1.1 www 1051: __END__
1052:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>