Annotation of loncom/interface/lonmsg.pm, revision 1.206
1.1 www 1: # The LearningOnline Network with CAPA
1.26 albertel 2: # Routines for messaging
3: #
1.206 ! raeburn 4: # $Id: lonmsg.pm,v 1.205 2007/05/08 16:50:02 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:
56: =head1 FUNCTIONS
57:
58: =over 4
59:
60: =cut
61:
1.1 www 62: use strict;
1.140 albertel 63: use Apache::lonnet;
1.47 albertel 64: use HTML::TokeParser();
1.180 albertel 65: use Apache::lonlocal;
1.53 www 66: use Mail::Send;
1.187 albertel 67: use LONCAPA qw(:DEFAULT :match);
1.180 albertel 68:
69: {
70: my $uniq;
71: sub get_uniq {
72: $uniq++;
73: return $uniq;
74: }
75: }
1.65 www 76:
1.1 www 77: # ===================================================================== Package
78:
1.3 www 79: sub packagemsg {
1.108 www 80: my ($subject,$message,$citation,$baseurl,$attachmenturl,
1.202 raeburn 81: $recuser,$recdomain,$msgid,$type,$crsmsgid,$symb,$error,$recipid)=@_;
1.96 albertel 82: $message =&HTML::Entities::encode($message,'<>&"');
83: $citation=&HTML::Entities::encode($citation,'<>&"');
84: $subject =&HTML::Entities::encode($subject,'<>&"');
1.49 albertel 85: #remove machine specification
86: $baseurl =~ s|^http://[^/]+/|/|;
1.96 albertel 87: $baseurl =&HTML::Entities::encode($baseurl,'<>&"');
1.51 www 88: #remove machine specification
89: $attachmenturl =~ s|^http://[^/]+/|/|;
1.96 albertel 90: $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
1.201 raeburn 91: my $course_context = &get_course_context();
1.2 www 92: my $now=time;
1.180 albertel 93: my $msgcount = &get_uniq();
1.156 raeburn 94: unless(defined($msgid)) {
1.159 raeburn 95: $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},
1.191 raeburn 96: $msgcount,$course_context,$symb,$error,$$);
1.156 raeburn 97: }
1.174 raeburn 98: my $result = '<sendername>'.$env{'user.name'}.'</sendername>'.
1.140 albertel 99: '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
1.1 www 100: '<subject>'.$subject.'</subject>'.
1.174 raeburn 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>'.
1.1 www 111: '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
112: '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
1.140 albertel 113: '<browsertype>'.$env{'browser.type'}.'</browsertype>'.
114: '<browseros>'.$env{'browser.os'}.'</browseros>'.
115: '<browserversion>'.$env{'browser.version'}.'</browserversion>'.
116: '<browsermathml>'.$env{'browser.mathml'}.'</browsermathml>'.
1.1 www 117: '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
1.158 raeburn 118: '<courseid>'.$course_context.'</courseid>'.
1.140 albertel 119: '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
120: '<role>'.$env{'request.role'}.'</role>'.
121: '<resource>'.$env{'request.filename'}.'</resource>'.
1.156 raeburn 122: '<msgid>'.$msgid.'</msgid>';
123: if (ref($recuser) eq 'ARRAY') {
124: for (my $i=0; $i<@{$recuser}; $i++) {
1.162 raeburn 125: if ($type eq 'dcmail') {
126: my ($username,$email) = split(/:/,$$recuser[$i]);
1.184 www 127: $username = &unescape($username);
128: $email = &unescape($email);
1.162 raeburn 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: }
1.156 raeburn 137: }
138: } else {
139: $result .= '<recuser>'.$recuser.'</recuser>'.
140: '<recdomain>'.$recdomain.'</recdomain>';
141: }
142: $result .= '<message>'.$message.'</message>';
1.49 albertel 143: if (defined($citation)) {
144: $result.='<citation>'.$citation.'</citation>';
145: }
146: if (defined($baseurl)) {
147: $result.= '<baseurl>'.$baseurl.'</baseurl>';
148: }
1.51 www 149: if (defined($attachmenturl)) {
1.52 www 150: $result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
1.51 www 151: }
1.191 raeburn 152: if (defined($symb)) {
153: $result.= '<symb>'.$symb.'</symb>';
1.201 raeburn 154: if ($course_context ne '') {
1.191 raeburn 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: }
1.202 raeburn 163: if (defined($recipid)) {
164: $result.= '<recipid>'.$recipid.'</recipid>';
165: }
1.204 raeburn 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: }
1.191 raeburn 177: return ($msgid,$result);
1.1 www 178: }
179:
1.201 raeburn 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:
1.2 www 201: # ================================================== Unpack message into a hash
202:
1.3 www 203: sub unpackagemsg {
1.52 www 204: my ($message,$notoken)=@_;
1.2 www 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);
1.156 raeburn 212: if (($entry eq 'recuser') || ($entry eq 'recdomain')) {
213: push(@{$content{$entry}},$value);
1.162 raeburn 214: } elsif ($entry eq 'recipient') {
215: my $username = $token->[2]{'username'};
216: $username = &HTML::Entities::decode($username,'<>&"');
217: $content{$entry}{$username} = $value;
1.156 raeburn 218: } else {
219: $content{$entry}=$value;
220: }
1.2 www 221: }
222: }
1.168 albertel 223: if (!exists($content{'recuser'})) { $content{'recuser'} = []; }
1.52 www 224: if ($content{'attachmenturl'}) {
1.100 albertel 225: my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
1.52 www 226: if ($notoken) {
1.100 albertel 227: $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';
1.52 www 228: } else {
1.99 albertel 229: &Apache::lonnet::allowuploaded('/adm/msg',
230: $content{'attachmenturl'});
231: $content{'message'}.='<p>'.&mt('Attachment').
232: ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
1.100 albertel 233: $fname.'</tt></a>';
1.52 www 234: }
235: }
1.2 www 236: return %content;
237: }
238:
1.6 www 239: # ======================================================= Get info out of msgid
240:
1.159 raeburn 241: sub buildmsgid {
1.191 raeburn 242: my ($now,$subject,$uname,$udom,$msgcount,$course_context,$symb,$error,$pid) = @_;
1.184 www 243: $subject=&escape($subject);
1.192 raeburn 244: $symb = &escape($symb);
1.184 www 245: return(&escape($now.':'.$subject.':'.$uname.':'.
1.191 raeburn 246: $udom.':'.$msgcount.':'.$course_context.':'.$pid.':'.$symb.':'.$error));
1.159 raeburn 247: }
248:
1.6 www 249: sub unpackmsgid {
1.169 albertel 250: my ($msgid,$folder,$skipstatus,$status_cache)=@_;
1.184 www 251: $msgid=&unescape($msgid);
1.167 raeburn 252: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid,
1.191 raeburn 253: $processid,$symb,$error) = split(/\:/,&unescape($msgid));
1.184 www 254: $shortsubj = &unescape($shortsubj);
1.182 albertel 255: $shortsubj = &HTML::Entities::decode($shortsubj);
1.192 raeburn 256: $symb = &unescape($symb);
1.167 raeburn 257: if (!defined($processid)) { $fromcid = ''; }
1.164 raeburn 258: my %status=();
259: unless ($skipstatus) {
1.169 albertel 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}=''; }
1.164 raeburn 267: unless ($status{$msgid}) { $status{$msgid}='new'; }
268: }
1.191 raeburn 269: return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid,$symb,$error);
1.141 raeburn 270: }
1.6 www 271:
1.53 www 272:
273: sub sendemail {
274: my ($to,$subject,$body)=@_;
1.186 www 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: }
1.53 www 282: $body=
1.67 www 283: "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
1.186 www 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;
1.53 www 286: my $msg = new Mail::Send;
287: $msg->to($to);
288: $msg->subject('[LON-CAPA] '.$subject);
1.188 www 289: if ($senderaddress) { $msg->add('Reply-to',$senderaddress); $msg->add('From',$senderaddress); }
1.97 matthew 290: if (my $fh = $msg->open()) {
1.172 albertel 291: print $fh $body;
292: $fh->close;
1.68 www 293: }
1.53 www 294: }
295:
296: # ==================================================== Send notification emails
297:
298: sub sendnotification {
1.194 raeburn 299: my ($to,$touname,$toudom,$subj,$crit,$text,$msgid)=@_;
1.140 albertel 300: my $sender=$env{'environment.firstname'}.' '.$env{'environment.lastname'};
1.131 www 301: unless ($sender=~/\w/) {
1.172 albertel 302: $sender=$env{'user.name'}.'@'.$env{'user.domain'};
1.131 www 303: }
1.53 www 304: my $critical=($crit?' critical':'');
1.131 www 305: $text=~s/\<\;/\</gs;
306: $text=~s/\>\;/\>/gs;
1.53 www 307: my $url='http://'.
1.198 albertel 308: &Apache::lonnet::hostname(&Apache::lonnet::homeserver($touname,$toudom)).
1.54 www 309: '/adm/email?username='.$touname.'&domain='.$toudom;
1.194 raeburn 310: my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid,
311: $symb,$error) = &Apache::lonmsg::unpackmsgid($msgid);
1.206 ! raeburn 312: my ($coursetext,$body,$bodystart,$bodyend);
1.194 raeburn 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: }
1.206 ! raeburn 325: my @recipients = split(/,/,$to);
! 326: $bodystart = $coursetext.
1.195 raeburn 327: &mt('You received a'.$critical.' message from [_1] in LON-CAPA.',$sender).' '.&mt('The subject is
1.53 www 328:
1.195 raeburn 329: [_1]
1.53 www 330:
1.195 raeburn 331: ',$subj)."\n".
1.194 raeburn 332: '=== '.&mt('Excerpt')." ============================================================
1.206 ! raeburn 333: ";
! 334: $bodyend = "
1.131 www 335: ========================================================================
336:
1.195 raeburn 337: ".&mt('Use
1.53 www 338:
1.195 raeburn 339: [_1]
1.53 www 340:
1.195 raeburn 341: to access the full message.',$url);
1.206 ! raeburn 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: }
1.53 www 359: }
1.40 www 360: # ============================================================= Check for email
361:
362: sub newmail {
1.140 albertel 363: if ((time-$env{'user.mailcheck.time'})>300) {
1.40 www 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:
1.1 www 371: # =============================== Automated message to the author of a resource
372:
1.58 bowersj2 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:
1.1 www 380: sub author_res_msg {
381: my ($filename,$message)=@_;
1.2 www 382: unless ($message) { return 'empty'; }
1.1 www 383: $filename=&Apache::lonnet::declutter($filename);
1.72 www 384: my ($domain,$author,@dummy)=split(/\//,$filename);
1.1 www 385: my $homeserver=&Apache::lonnet::homeserver($author,$domain);
386: if ($homeserver ne 'no_host') {
387: my $id=unpack("%32C*",$message);
1.181 albertel 388: $message .= " <p>This error occurred on machine ".
389: $Apache::lonnet::perlvar{'lonHostID'}."</p>";
1.2 www 390: my $msgid;
1.72 www 391: ($msgid,$message)=&packagemsg($filename,$message);
1.3 www 392: return &Apache::lonnet::reply('put:'.$domain.':'.$author.
1.72 www 393: ':nohist_res_msgs:'.
1.184 www 394: &escape($filename.'_'.$id).'='.
395: &escape($message),$homeserver);
1.1 www 396: }
1.2 www 397: return 'no_host';
1.73 www 398: }
399:
400: # =========================================== Retrieve author resource messages
401:
402: sub retrieve_author_res_msg {
1.75 www 403: my $url=shift;
1.73 www 404: $url=&Apache::lonnet::declutter($url);
1.187 albertel 405: my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.76 www 406: my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
1.73 www 407: my $msgs='';
408: foreach (keys %errormsgs) {
1.80 www 409: if ($_=~/^\Q$url\E\_\d+$/) {
1.73 www 410: my %content=&unpackagemsg($errormsgs{$_});
1.74 www 411: $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
412: $content{'time'}.'</b>: '.$content{'message'}.
413: '<br /></p>';
1.73 www 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 {
1.75 www 423: my $url=shift;
1.73 www 424: $url=&Apache::lonnet::declutter($url);
1.187 albertel 425: my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.77 www 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);
1.73 www 433: }
1.152 www 434: # =================================== Clear out all author messages in URL path
1.73 www 435:
1.152 www 436: sub clear_author_res_msg {
437: my $url=shift;
438: $url=&Apache::lonnet::declutter($url);
1.187 albertel 439: my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.152 www 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: }
1.73 www 448: # ================= Return hash with URLs for which there is a resource message
449:
450: sub all_url_author_res_msg {
451: my ($author,$domain)=@_;
1.75 www 452: my %returnhash=();
1.76 www 453: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
1.75 www 454: $_=~/^(.+)\_\d+/;
455: $returnhash{$1}=1;
456: }
457: return %returnhash;
1.1 www 458: }
459:
1.185 albertel 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);
1.201 raeburn 469: if ($result eq 'ok' || $result eq 'con_delayed') {
470:
471: }
1.185 albertel 472: return $result;
473: }
474:
1.1 www 475: # ================================================== Critical message to a user
476:
1.38 www 477: sub user_crit_msg_raw {
1.201 raeburn 478: my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage,
1.202 raeburn 479: $nosentstore,$recipid)=@_;
1.2 www 480: # Check if allowed missing
1.190 raeburn 481: my ($status,$packed_message);
1.2 www 482: my $msgid='undefined';
483: unless (($message)&&($user)&&($domain)) { $status='empty'; };
1.131 www 484: my $text=$message;
1.2 www 485: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
486: if ($homeserver ne 'no_host') {
1.202 raeburn 487: ($msgid,$packed_message)=&packagemsg($subject,$message,undef,undef,
488: undef,undef,undef,undef,undef,undef,undef,
489: undef,$recipid);
1.190 raeburn 490: if ($sendback) { $packed_message.='<sendback>true</sendback>'; }
1.4 www 491: $status=&Apache::lonnet::critical(
492: 'put:'.$domain.':'.$user.':critical:'.
1.184 www 493: &escape($msgid).'='.
1.190 raeburn 494: &escape($packed_message),$homeserver);
1.159 raeburn 495: if (defined($sentmessage)) {
1.190 raeburn 496: $$sentmessage = $packed_message;
1.159 raeburn 497: }
1.201 raeburn 498: if (!$nosentstore) {
1.193 raeburn 499: (undef,my $packed_message_no_citation) =
1.190 raeburn 500: &packagemsg($subject,$message,undef,undef,undef,$user,$domain,
501: $msgid);
1.193 raeburn 502: if ($status eq 'ok' || $status eq 'con_delayed') {
503: &store_sent_mail($msgid,$packed_message_no_citation);
504: }
505: }
1.2 www 506: } else {
507: $status='no_host';
508: }
1.190 raeburn 509:
1.53 www 510: # Notifications
1.186 www 511: my %userenv = &Apache::loncommon::getemails($user,$domain);
1.53 www 512: if ($userenv{'critnotification'}) {
1.131 www 513: &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1,
1.194 raeburn 514: $text,$msgid);
1.53 www 515: }
1.132 www 516: if ($toperm && $userenv{'permanentemail'}) {
517: &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,1,
1.194 raeburn 518: $text,$msgid);
1.132 www 519: }
1.53 www 520: # Log this
1.2 www 521: &Apache::lonnet::logthis(
1.4 www 522: 'Sending critical email '.$msgid.
1.2 www 523: ', log status: '.
1.140 albertel 524: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
525: $env{'user.home'},
1.2 www 526: 'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
1.4 www 527: .$status));
1.2 www 528: return $status;
529: }
530:
1.38 www 531: # New routine that respects "forward" and calls old routine
532:
1.58 bowersj2 533: =pod
534:
1.202 raeburn 535: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback, $nosentstore,$recipid)>:
1.201 raeburn 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.
1.58 bowersj2 539:
1.183 albertel 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:
1.58 bowersj2 548: =cut
549:
1.38 www 550: sub user_crit_msg {
1.201 raeburn 551: my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage,
1.202 raeburn 552: $nosentstore,$recipid)=@_;
1.183 albertel 553: my @status;
1.38 www 554: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
555: $domain,$user);
556: my $msgforward=$userenv{'msgforward'};
557: if ($msgforward) {
1.183 albertel 558: foreach my $addr (split(/\,/,$msgforward)) {
559: my ($forwuser,$forwdomain)=split(/\:/,$addr);
560: push(@status,
561: &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
1.202 raeburn 562: $sendback,$toperm,$sentmessage,$nosentstore,
563: $recipid));
1.38 www 564: }
565: } else {
1.183 albertel 566: push(@status,
567: &user_crit_msg_raw($user,$domain,$subject,$message,$sendback,
1.202 raeburn 568: $toperm,$sentmessage,$nosentstore,$recipid));
1.38 www 569: }
1.183 albertel 570: if (wantarray) {
571: return @status;
572: }
573: return join(' ',@status);
1.38 www 574: }
575:
1.2 www 576: # =================================================== Critical message received
577:
578: sub user_crit_received {
1.12 www 579: my $msgid=shift;
580: my %message=&Apache::lonnet::get('critical',[$msgid]);
1.52 www 581: my %contents=&unpackagemsg($message{$msgid},1);
1.204 raeburn 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: }
1.24 www 591: my $status='rec: '.($contents{'sendback'}?
1.204 raeburn 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');
1.5 www 600: $status.=' trans: '.
1.12 www 601: &Apache::lonnet::put(
602: 'nohist_email',{$contents{'msgid'} => $message{$msgid}});
1.5 www 603: $status.=' del: '.
1.9 albertel 604: &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.140 albertel 605: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
606: $env{'user.home'},'Received critical message '.
1.5 www 607: $contents{'msgid'}.
608: ', '.$status);
1.12 www 609: return $status;
1.2 www 610: }
611:
612: # ======================================================== Normal communication
613:
1.38 www 614: sub user_normal_msg_raw {
1.132 www 615: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.191 raeburn 616: $toperm,$currid,$newid,$sentmessage,$crsmsgid,$symb,$restitle,
1.202 raeburn 617: $error,$nosentstore,$recipid)=@_;
1.2 www 618: # Check if allowed missing
1.173 albertel 619: my ($status,$packed_message);
1.2 www 620: my $msgid='undefined';
1.131 www 621: my $text=$message;
1.2 www 622: unless (($message)&&($user)&&($domain)) { $status='empty'; };
623: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
624: if ($homeserver ne 'no_host') {
1.173 albertel 625: ($msgid,$packed_message)=
626: &packagemsg($subject,$message,$citation,$baseurl,
1.174 raeburn 627: $attachmenturl,$user,$domain,$currid,
1.202 raeburn 628: undef,$crsmsgid,$symb,$error,$recipid);
1.174 raeburn 629:
1.108 www 630: # Store in user folder
1.4 www 631: $status=&Apache::lonnet::critical(
632: 'put:'.$domain.':'.$user.':nohist_email:'.
1.184 www 633: &escape($msgid).'='.
634: &escape($packed_message),$homeserver);
1.108 www 635: # Save new message received time
1.40 www 636: &Apache::lonnet::put
637: ('email_status',{'recnewemail'=>time},$domain,$user);
1.201 raeburn 638: # Into sent-mail folder if sent mail storage required
639: if (!$nosentstore) {
1.190 raeburn 640: (undef,my $packed_message_no_citation) =
641: &packagemsg($subject,$message,undef,$baseurl,$attachmenturl,
1.191 raeburn 642: $user,$domain,$currid,undef,$crsmsgid,$symb,$error);
1.193 raeburn 643: if ($status eq 'ok' || $status eq 'con_delayed') {
644: &store_sent_mail($msgid,$packed_message_no_citation);
645: }
1.156 raeburn 646: }
1.201 raeburn 647: if (ref($newid) eq 'SCALAR') {
1.196 www 648: $$newid = $msgid;
649: }
1.201 raeburn 650: if (ref($sentmessage) eq 'SCALAR') {
1.196 www 651: $$sentmessage = $packed_message;
652: }
653: # Notifications
1.206 ! raeburn 654: my %userenv = &Apache::loncommon::getemails($user,$domain);
1.196 www 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 {
1.2 www 667: $status='no_host';
1.196 www 668: }
1.2 www 669: return $status;
670: }
1.38 www 671:
672: # New routine that respects "forward" and calls old routine
673:
1.58 bowersj2 674: =pod
675:
1.191 raeburn 676: =item * B<user_normal_msg($user, $domain, $subject, $message, $citation,
1.201 raeburn 677: $baseurl, $attachmenturl, $toperm, $sentmessage, $symb, $restitle,
1.202 raeburn 678: $error,$nosentstore,$recipid)>:
1.191 raeburn 679: Sends a message to the $user at $domain, with subject $subject and message $message.
1.58 bowersj2 680:
1.199 raeburn 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:
1.58 bowersj2 689: =cut
690:
1.38 www 691: sub user_normal_msg {
1.132 www 692: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.202 raeburn 693: $toperm,$sentmessage,$symb,$restitle,$error,$nosentstore,$recipid)=@_;
1.199 raeburn 694: my @status;
1.38 www 695: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
696: $domain,$user);
697: my $msgforward=$userenv{'msgforward'};
698: if ($msgforward) {
1.171 banghart 699: foreach (split(/\,/,$msgforward)) {
1.172 albertel 700: my ($forwuser,$forwdomain)=split(/\:/,$_);
1.199 raeburn 701: push(@status,
1.171 banghart 702: &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
1.172 albertel 703: $citation,$baseurl,$attachmenturl,$toperm,
1.201 raeburn 704: undef,undef,$sentmessage,undef,$symb,
1.202 raeburn 705: $restitle,$error,$nosentstore,$recipid));
1.171 banghart 706: }
1.191 raeburn 707: } else {
1.199 raeburn 708: push(@status,&user_normal_msg_raw($user,$domain,$subject,$message,
1.172 albertel 709: $citation,$baseurl,$attachmenturl,$toperm,
1.201 raeburn 710: undef,undef,$sentmessage,undef,$symb,
1.202 raeburn 711: $restitle,$error,$nosentstore,$recipid));
1.199 raeburn 712: }
713: if (wantarray) {
714: return @status;
1.38 www 715: }
1.199 raeburn 716: return join(' ',@status);
1.38 www 717: }
718:
1.201 raeburn 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;
1.205 raeburn 724: } else {
725: if ($subj_prefix) {
726: $sentsubj = $subj_prefix.' ';
727: }
728: $sentsubj .= $msgsubj;
1.201 raeburn 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:
1.156 raeburn 741: sub store_sent_mail {
1.201 raeburn 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: }
1.171 banghart 752: my $status =' '.&Apache::lonnet::critical(
1.201 raeburn 753: 'put:'.$senderdom.':'.$senderuname.':nohist_email_sent:'.
754: &escape($msgid).'='.&escape($message),$senderhome);
1.156 raeburn 755: return $status;
756: }
1.2 www 757:
1.202 raeburn 758: sub store_recipients {
759: my ($subject,$sendername,$senderdom,$reciphash) = @_;
760: my $context = &get_course_context();
1.203 albertel 761: my $now = time();
1.202 raeburn 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:
1.106 www 777: # =============================================================== Folder suffix
778:
779: sub foldersuffix {
780: my $folder=shift;
781: unless ($folder) { return ''; }
1.189 raeburn 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;
1.106 www 807: }
808:
1.197 albertel 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:
1.199 raeburn 835: =item * B<decide_receiver($feedurl,$author,$question,$course,$policy,$defaultflag)>:
1.197 albertel 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:
1.199 raeburn 899: =pod
900:
901: =back
902:
903: =cut
904:
1.180 albertel 905: 1;
1.1 www 906: __END__
907:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>