--- loncom/interface/lonmsg.pm 2005/11/28 20:14:43 1.158 +++ loncom/interface/lonmsg.pm 2005/12/10 00:46:34 1.164 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Routines for messaging # -# $Id: lonmsg.pm,v 1.158 2005/11/28 20:14:43 raeburn Exp $ +# $Id: lonmsg.pm,v 1.164 2005/12/10 00:46:34 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -97,6 +97,17 @@ Right now, this document will cover just it is likely you will not need to programmatically read messages, since lonmsg already implements that functionality. +The routines used to package messages and unpackage messages are not +only used by lonmsg when creating/extracting messages for LON-CAPA's +internal messaging system, but also by lonnotify.pm which is available +for use by Domain Coordinators to broadcast standard e-mail to specified +users in their domain. The XML packaging used in the two cases is very +similar. The differences are the use of $uname and +$udom in stored internal messages, compared +with $email in stored +Domain Coordinator e-mail for the storage of information about +recipients of the message/e-mail. + =head1 FUNCTIONS =over 4 @@ -126,7 +137,7 @@ my $interdis; sub packagemsg { my ($subject,$message,$citation,$baseurl,$attachmenturl, - $recuser,$recdomain,$msgid)=@_; + $recuser,$recdomain,$msgid,$type)=@_; $message =&HTML::Entities::encode($message,'<>&"'); $citation=&HTML::Entities::encode($citation,'<>&"'); $subject =&HTML::Entities::encode($subject,'<>&"'); @@ -142,18 +153,22 @@ sub packagemsg { split(/\:/,&Apache::lonnet::unescape($env{'form.replyid'})); $course_context = $origcid; } + foreach my $key (keys(%env)) { + if ($key=~/^form\.(rep)?rec\_(.*)$/) { + my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) = + split(/\:/,&Apache::lonnet::unescape($2)); + $course_context = $origcid; + last; + } + } unless(defined($course_context)) { $course_context = $env{'request.course.id'}; } my $now=time; $msgcount++; - my $partsubj=$subject; - $partsubj=&Apache::lonnet::escape($partsubj); unless(defined($msgid)) { - $msgid=&Apache::lonnet::escape( - $now.':'.$partsubj.':'.$env{'user.name'}.':'. - $env{'user.domain'}.':'.$msgcount.':'. - $course_context.':'.$$); + $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'}, + $msgcount,$course_context,$$); } my $result=''.$env{'user.name'}.''. ''.$env{'user.domain'}.''. @@ -174,8 +189,18 @@ sub packagemsg { ''.$msgid.''; if (ref($recuser) eq 'ARRAY') { for (my $i=0; $i<@{$recuser}; $i++) { - $result .= ''.$$recuser[$i].''. - ''.$$recdomain[$i].''; + if ($type eq 'dcmail') { + my ($username,$email) = split(/:/,$$recuser[$i]); + $username = &Apache::lonnet::unescape($username); + $email = &Apache::lonnet::unescape($email); + $username = &HTML::Entities::encode($username,'<>&"'); + $email = &HTML::Entities::encode($email,'<>&"'); + $result .= ''. + $email.''; + } else { + $result .= ''.$$recuser[$i].''. + ''.$$recdomain[$i].''; + } } } else { $result .= ''.$recuser.''. @@ -207,6 +232,10 @@ sub unpackagemsg { my $value=$parser->get_text('/'.$entry); if (($entry eq 'recuser') || ($entry eq 'recdomain')) { push(@{$content{$entry}},$value); + } elsif ($entry eq 'recipient') { + my $username = $token->[2]{'username'}; + $username = &HTML::Entities::decode($username,'<>&"'); + $content{$entry}{$username} = $value; } else { $content{$entry}=$value; } @@ -229,15 +258,25 @@ sub unpackagemsg { # ======================================================= Get info out of msgid +sub buildmsgid { + my ($now,$subject,$uname,$udom,$msgcount,$course_context,$pid) = @_; + $subject=&Apache::lonnet::escape($subject); + return(&Apache::lonnet::escape($now.':'.$subject.':'.$uname.':'. + $udom.':'.$msgcount.':'.$course_context.':'.$pid)); +} + sub unpackmsgid { - my ($msgid,$folder)=@_; + my ($msgid,$folder,$skipstatus)=@_; $msgid=&Apache::lonnet::unescape($msgid); - my $suffix=&foldersuffix($folder); my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid)=split(/\:/, &Apache::lonnet::unescape($msgid)); - my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]); - if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; } - unless ($status{$msgid}) { $status{$msgid}='new'; } + my %status=(); + unless ($skipstatus) { + my $suffix=&foldersuffix($folder); + &Apache::lonnet::get('email_status'.$suffix,[$msgid]); + if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; } + unless ($status{$msgid}) { $status{$msgid}='new'; } + } return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid); } @@ -389,7 +428,7 @@ sub all_url_author_res_msg { # ================================================== Critical message to a user sub user_crit_msg_raw { - my ($user,$domain,$subject,$message,$sendback,$toperm)=@_; + my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_; # Check if allowed missing my $status=''; my $msgid='undefined'; @@ -403,13 +442,9 @@ sub user_crit_msg_raw { 'put:'.$domain.':'.$user.':critical:'. &Apache::lonnet::escape($msgid).'='. &Apache::lonnet::escape($message),$homeserver); - if ($env{'request.course.id'}) { - &user_normal_msg_raw( - $env{'course.'.$env{'request.course.id'}.'.num'}, - $env{'course.'.$env{'request.course.id'}.'.domain'}, - 'Critical ['.$user.':'.$domain.']', - $message); - } + if (defined($sentmessage)) { + $$sentmessage = $message; + } } else { $status='no_host'; } @@ -447,7 +482,7 @@ sub user_crit_msg_raw { =cut sub user_crit_msg { - my ($user,$domain,$subject,$message,$sendback,$toperm)=@_; + my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_; my $status=''; my %userenv = &Apache::lonnet::get('environment',['msgforward'], $domain,$user); @@ -457,10 +492,10 @@ sub user_crit_msg { my ($forwuser,$forwdomain)=split(/\:/,$_); $status.= &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message, - $sendback,$toperm).' '; + $sendback,$toperm,$sentmessage).' '; } } else { - $status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback,$toperm); + $status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage); } return $status; } @@ -495,7 +530,7 @@ sub user_crit_received { sub user_normal_msg_raw { my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl, - $toperm,$newid)=@_; + $toperm,$currid,$newid,$sentmessage)=@_; # Check if allowed missing my $status=''; my $msgid='undefined'; @@ -504,7 +539,7 @@ sub user_normal_msg_raw { my $homeserver=&Apache::lonnet::homeserver($user,$domain); if ($homeserver ne 'no_host') { ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl, - $attachmenturl,$user,$domain); + $attachmenturl,$user,$domain,$currid); # Store in user folder $status=&Apache::lonnet::critical( 'put:'.$domain.':'.$user.':nohist_email:'. @@ -513,8 +548,11 @@ sub user_normal_msg_raw { # Save new message received time &Apache::lonnet::put ('email_status',{'recnewemail'=>time},$domain,$user); -# Into sent-mail folder unless a broadcast message - unless (($env{'request.course.id'}) && ($env{'form.sendmode'} eq 'group')) { +# Into sent-mail folder unless a broadcast message or critical message + unless (($env{'request.course.id'}) && + (($env{'form.sendmode'} eq 'group') || + (($env{'form.critmsg'}) || ($env{'form.sendbck'})) && + (&Apache::lonnet::allowed('srm',$env{'request.course.id'})))) { $status .= &store_sent_mail($msgid,$message); } } else { @@ -523,6 +561,10 @@ sub user_normal_msg_raw { if (defined($newid)) { $$newid = $msgid; } + if (defined($sentmessage)) { + $$sentmessage = $message; + } + # Notifications my %userenv = &Apache::lonnet::get('environment',['notification', 'permanentemail'], @@ -553,7 +595,7 @@ sub user_normal_msg_raw { sub user_normal_msg { my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl, - $toperm)=@_; + $toperm,$sentmessage)=@_; my $status=''; my %userenv = &Apache::lonnet::get('environment',['msgforward'], $domain,$user); @@ -563,11 +605,11 @@ sub user_normal_msg { my ($forwuser,$forwdomain)=split(/\:/,$_); $status.= &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message, - $citation,$baseurl,$attachmenturl,$toperm).' '; + $citation,$baseurl,$attachmenturl,$toperm,undef,undef,$sentmessage).' '; } } else { $status=&user_normal_msg_raw($user,$domain,$subject,$message, - $citation,$baseurl,$attachmenturl,$toperm); + $citation,$baseurl,$attachmenturl,$toperm,undef,undef,$sentmessage); } return $status; } @@ -1175,8 +1217,8 @@ sub compout { &mt('and return receipt') . '' . $crithelp . '

'. -'

'; } +''; } my %message; my %content; my $defdom=$env{'user.domain'}; @@ -1346,6 +1388,12 @@ sub disfacetoface { $result.='

'.&mt('Record').'

'; } elsif ($content{'subject'}=~/^Broadcast/) { $result .='

'.&mt('Broadcast Message').'

'; + if ($content{'subject'}=~/^Broadcast\./) { + %content=&unpackagemsg($content{'message'}); + $content{'message'}= + ''.&mt('Subject').': '.$content{'subject'}.'
'. + $content{'message'}; + } } else { $result.='

'.&mt('Critical Message').'

'; %content=&unpackagemsg($content{'message'}); @@ -1421,12 +1469,13 @@ ENDTREC ($env{'form.recdomain'}) && ($env{'form.recuname'})) { chomp($env{'form.newrecord'}); if ($env{'form.newrecord'}) { + my $recordtxt = $env{'form.newrecord'}; &user_normal_msg_raw( $env{'course.'.$env{'request.course.id'}.'.num'}, $env{'course.'.$env{'request.course.id'}.'.domain'}, &mt('Record'). ' ['.$env{'form.recuname'}.':'.$env{'form.recdomain'}.']', - $env{'form.newrecord'}); + $recordtxt); } $r->print('

'.&Apache::loncommon::plainname($env{'form.recuname'}, $env{'form.recdomain'}).'

'); @@ -2017,8 +2066,8 @@ sub sendoffmail { my ($r,$folder)=@_; my $suffix=&foldersuffix($folder); my $sendstatus=''; - my %broadcast_status; - my $numbroadcast = 0; + my %specialmsg_status; + my $numspecial = 0; if ($env{'form.send'}) { &printheader($r,'','Messages being sent.'); $r->rflush(); @@ -2064,19 +2113,20 @@ sub sendoffmail { } } - my $basicmsg; + my $savemsg; my $msgtype; + my %sentmessage; if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && (&Apache::lonnet::allowed('srm',$env{'request.course.id'}))) { - $basicmsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1); - $msgtype = '(critical)'; + $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1); + $msgtype = 'critical'; } else { - $basicmsg=&Apache::lonfeedback::clear_out_html($env{'form.message'}); + $savemsg=&Apache::lonfeedback::clear_out_html($env{'form.message'}); } foreach (keys %toaddr) { my ($recuname,$recdomain)=split(/\:/,$_); - my $msgtxt = $basicmsg; + my $msgtxt = $savemsg; if ($toaddr{$_}) { $msgtxt.='
'.$toaddr{$_}; } my $thismsg; if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && @@ -2085,57 +2135,77 @@ sub sendoffmail { $thismsg=&user_crit_msg($recuname,$recdomain, &Apache::lonfeedback::clear_out_html($env{'form.subject'}), $msgtxt, - $env{'form.sendbck'},$env{'form.permanent'}); + $env{'form.sendbck'},$env{'form.permanent'},\$sentmessage{$_}); } else { $r->print(&mt('Sending').' '.$recuname.'@'.$recdomain.': '); $thismsg=&user_normal_msg($recuname,$recdomain, &Apache::lonfeedback::clear_out_html($env{'form.subject'}), $msgtxt, - $content{'citation'},undef,undef,$env{'form.permanent'}); + $content{'citation'},undef,undef,$env{'form.permanent'},\$sentmessage{$_}); } - if (($env{'request.course.id'}) && - ($env{'form.sendmode'} eq 'group')) { - $broadcast_status{$recuname.':'.$recdomain} = $thismsg; + if (($env{'request.course.id'}) && (($msgtype eq 'critical') || + ($env{'form.sendmode'} eq 'group'))) { + $specialmsg_status{$recuname.':'.$recdomain} = $thismsg; if ($thismsg eq 'ok') { - $numbroadcast ++; + $numspecial ++; } } $r->print($thismsg.'
'); $sendstatus.=' '.$thismsg; } - if (($env{'request.course.id'}) && ($env{'form.sendmode'} eq 'group')) { + if (($env{'request.course.id'}) && (($env{'form.sendmode'} eq 'group') + || ($msgtype eq 'critical'))) { my $subj_prefix; if ($msgtype eq 'critical') { - $subj_prefix = 'Critical broadcast'; + $subj_prefix = 'Critical.'; } else { - $subj_prefix = 'Broadcast'; + $subj_prefix = 'Broadcast.'; } - my ($broadmsgid,$broadresult); - if ($numbroadcast) { - $broadresult = &user_normal_msg_raw( - $env{'course.'.$env{'request.course.id'}.'.num'}, - $env{'course.'.$env{'request.course.id'}.'.domain'}, $subj_prefix.' to: '.$env{'course.'.$env{'request.course.id'}.'.description'}. - ' ('.$numbroadcast.' sent)',$basicmsg,undef,undef,undef, - undef,\$broadmsgid); + my ($specialmsgid,$specialresult); + my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; + my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; + my $course_str = &Apache::lonnet::escape('['.$cnum.':'.$cdom.']'); + + if ($numspecial) { + $specialresult = &user_normal_msg_raw($cnum,$cdom,$subj_prefix. + ' '.$course_str,$savemsg,undef,undef,undef, + undef,undef,\$specialmsgid); + $specialmsgid = &Apache::lonnet::unescape($specialmsgid); } - if ($broadresult eq 'ok') { + if ($specialresult eq 'ok') { my $record_sent; my @recusers = (); my @recudoms = (); + my ($stamp,$msgsubj,$msgname,$msgdom,$msgcount,$context,$pid) = + split(/\:/,&Apache::lonnet::unescape($specialmsgid)); foreach my $recipient (sort(keys(%toaddr))) { - if ($broadcast_status{$recipient} eq 'ok') { + if ($specialmsg_status{$recipient} eq 'ok') { + my $usersubj = $subj_prefix.'['.$recipient.']'; + my $usermsgid = &buildmsgid($stamp,$usersubj,$msgname, + $msgdom,$msgcount,$context,$pid); + &user_normal_msg_raw($cnum,$cdom,$subj_prefix. + ' ['.$recipient.']',$sentmessage{$recipient}, + undef,undef,undef,undef,$usermsgid); my ($uname,$udom) = split/:/,$recipient; push(@recusers,$uname); push(@recudoms,$udom); } } if (@recusers) { - my $broadmessage; - ($broadmsgid,$broadmessage)=&packagemsg(&Apache::lonfeedback::clear_out_html($env{'form.subject'}),$basicmsg,undef,undef,undef,\@recusers,\@recudoms,$broadmsgid); - $record_sent = &store_sent_mail($broadmsgid,$broadmessage); + my $specialmessage; + my $sentsubj = $subj_prefix.' ('.$numspecial.' sent) '. + &Apache::lonfeedback::clear_out_html($env{'form.subject'}); + $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"'); + my $sentmsgid = &buildmsgid($stamp,$sentsubj,$msgname, + $msgdom,$msgcount,$context,$pid); + ($specialmsgid,$specialmessage) = + &packagemsg(&Apache::lonfeedback::clear_out_html( + $env{'form.subject'}),$savemsg,undef,undef,undef, + \@recusers,\@recudoms,$sentmsgid); + $record_sent = &store_sent_mail($specialmsgid,$specialmessage); } } else { - &Apache::lonnet::logthis('Failed to create record of broadcast in '.$env{'course.'.$env{'request.course.id'}.'.num'}.' at '.$env{'course.'.$env{'request.course.id'}.'.domain'}.' - no msgid generated'); + &Apache::lonnet::logthis('Failed to create record of critical message or broadcast in '.$env{'course.'.$env{'request.course.id'}.'.num'}.' at '.$env{'course.'.$env{'request.course.id'}.'.domain'}.' - no msgid generated'); } } } else {