--- loncom/interface/lonmsg.pm 2002/08/28 14:11:27 1.37
+++ loncom/interface/lonmsg.pm 2003/07/05 10:07:11 1.57
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Routines for messaging
#
-# $Id: lonmsg.pm,v 1.37 2002/08/28 14:11:27 www Exp $
+# $Id: lonmsg.pm,v 1.57 2003/07/05 10:07:11 www Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -47,21 +47,27 @@ package Apache::lonmsg;
use strict;
use Apache::lonnet();
use vars qw($msgcount);
-use HTML::TokeParser;
+use HTML::TokeParser();
use Apache::Constants qw(:common);
-use Apache::loncommon;
-use Apache::lontexconvert;
+use Apache::loncommon();
+use Apache::lontexconvert();
+use HTML::Entities();
+use Mail::Send;
# ===================================================================== Package
sub packagemsg {
- my ($subject,$message,$citation)=@_;
- $message=~s/\\<\;/g;
- $message=~s/\>/\>\;/g;
- $citation=~s/\\<\;/g;
- $citation=~s/\>/\>\;/g;
- $subject=~s/\\<\;/g;
- $subject=~s/\>/\>\;/g;
+ my ($subject,$message,$citation,$baseurl,$attachmenturl)=@_;
+ $message =&HTML::Entities::encode($message);
+ $citation=&HTML::Entities::encode($citation);
+ $subject =&HTML::Entities::encode($subject);
+ #remove machine specification
+ $baseurl =~ s|^http://[^/]+/|/|;
+ $baseurl =&HTML::Entities::encode($baseurl);
+ #remove machine specification
+ $attachmenturl =~ s|^http://[^/]+/|/|;
+ $attachmenturl =&HTML::Entities::encode($attachmenturl);
+
my $now=time;
$msgcount++;
my $partsubj=$subject;
@@ -69,8 +75,7 @@ sub packagemsg {
my $msgid=&Apache::lonnet::escape(
$now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
$ENV{'user.domain'}.':'.$msgcount.':'.$$);
- return $msgid,
- ''.$ENV{'user.name'}.''.
+ my $result=''.$ENV{'user.name'}.''.
''.$ENV{'user.domain'}.''.
''.$subject.''.
''.
@@ -86,14 +91,23 @@ sub packagemsg {
''.$ENV{'request.role'}.''.
''.$ENV{'request.filename'}.''.
''.$msgid.''.
- ''.$message.''.
- ''.$citation.'';
+ ''.$message.'';
+ if (defined($citation)) {
+ $result.=''.$citation.'';
+ }
+ if (defined($baseurl)) {
+ $result.= ''.$baseurl.'';
+ }
+ if (defined($attachmenturl)) {
+ $result.= ''.$attachmenturl.'';
+ }
+ return $msgid,$result;
}
# ================================================== Unpack message into a hash
sub unpackagemsg {
- my $message=shift;
+ my ($message,$notoken)=@_;
my %content=();
my $parser=HTML::TokeParser->new(\$message);
my $token;
@@ -104,6 +118,16 @@ sub unpackagemsg {
$content{$entry}=$value;
}
}
+ if ($content{'attachmenturl'}) {
+ my ($fname,$ft)=($content{'attachmenturl'}=~/\/(\w+)\.(\w+)$/);
+ if ($notoken) {
+ $content{'message'}.='
Attachment: '.$fname.'.'.$ft.'';
+ } else {
+ $content{'message'}.='
Attachment: '.$fname.'.'.$ft.'';
+ }
+ }
return %content;
}
@@ -119,6 +143,53 @@ sub unpackmsgid {
return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
}
+
+sub sendemail {
+ my ($to,$subject,$body)=@_;
+ $body=
+ "*** This is an automatic message generated by the LON-CAPA system.\n".
+ "*** Please do not reply to this address.\n\n".$body;
+ my $msg = new Mail::Send;
+ $msg->to($to);
+ $msg->subject('[LON-CAPA] '.$subject);
+ my $fh = $msg->open('smtp',Server => 'localhost');
+ print $fh $body;
+ $fh->close;
+}
+
+# ==================================================== Send notification emails
+
+sub sendnotification {
+ my ($to,$touname,$toudom,$subj,$crit)=@_;
+ my $sender=$ENV{'environment.firstname'}.' '.$ENV{'environment.lastname'};
+ my $critical=($crit?' critical':'');
+ my $url='http://'.
+ $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
+ '/adm/email?username='.$touname.'&domain='.$toudom;
+ my $body=(<300) {
+ my %what=&Apache::lonnet::get('email_status',['recnewemail']);
+ &Apache::lonnet::appenv('user.mailcheck.time'=>time);
+ if ($what{'recnewemail'}>0) { return 1; }
+ }
+ return 0;
+}
+
# =============================== Automated message to the author of a resource
sub author_res_msg {
@@ -141,7 +212,7 @@ sub author_res_msg {
# ================================================== Critical message to a user
-sub user_crit_msg {
+sub user_crit_msg_raw {
my ($user,$domain,$subject,$message,$sendback)=@_;
# Check if allowed missing
my $status='';
@@ -155,9 +226,23 @@ sub user_crit_msg {
'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);
+ }
} else {
$status='no_host';
}
+# Notifications
+ my %userenv = &Apache::lonnet::get('environment',['critnotification'],
+ $domain,$user);
+ if ($userenv{'critnotification'}) {
+ &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1);
+ }
+# Log this
&Apache::lonnet::logthis(
'Sending critical email '.$msgid.
', log status: '.
@@ -168,19 +253,41 @@ sub user_crit_msg {
return $status;
}
+# New routine that respects "forward" and calls old routine
+
+sub user_crit_msg {
+ my ($user,$domain,$subject,$message,$sendback)=@_;
+ my $status='';
+ my %userenv = &Apache::lonnet::get('environment',['msgforward'],
+ $domain,$user);
+ my $msgforward=$userenv{'msgforward'};
+ if ($msgforward) {
+ foreach (split(/\,/,$msgforward)) {
+ my ($forwuser,$forwdomain)=split(/\:/,$_);
+ $status.=
+ &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
+ $sendback).' ';
+ }
+ } else {
+ $status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback);
+ }
+ return $status;
+}
+
# =================================================== Critical message received
sub user_crit_received {
my $msgid=shift;
my %message=&Apache::lonnet::get('critical',[$msgid]);
- my %contents=&unpackagemsg($message{$msgid});
+ my %contents=&unpackagemsg($message{$msgid},1);
my $status='rec: '.($contents{'sendback'}?
&user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
'Receipt: '.$ENV{'user.name'}.' at '.$ENV{'user.domain'},
'User '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
- ' acknowledged receipt of message "'.
- $contents{'subject'}.'" dated '.$contents{'time'}.".\n\n"
- .'Message ID: '.$contents{'msgid'}):'no msg req');
+ ' acknowledged receipt of message'."\n".' "'.
+ $contents{'subject'}.'"'."\n".'dated '.
+ $contents{'time'}.".\n"
+ ):'no msg req');
$status.=' trans: '.
&Apache::lonnet::put(
'nohist_email',{$contents{'msgid'} => $message{$msgid}});
@@ -195,28 +302,60 @@ sub user_crit_received {
# ======================================================== Normal communication
-sub user_normal_msg {
- my ($user,$domain,$subject,$message,$citation)=@_;
+sub user_normal_msg_raw {
+ my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
# Check if allowed missing
my $status='';
my $msgid='undefined';
unless (($message)&&($user)&&($domain)) { $status='empty'; };
my $homeserver=&Apache::lonnet::homeserver($user,$domain);
if ($homeserver ne 'no_host') {
- ($msgid,$message)=&packagemsg($subject,$message,$citation);
+ ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl,
+ $attachmenturl);
$status=&Apache::lonnet::critical(
'put:'.$domain.':'.$user.':nohist_email:'.
&Apache::lonnet::escape($msgid).'='.
&Apache::lonnet::escape($message),$homeserver);
+ &Apache::lonnet::put
+ ('email_status',{'recnewemail'=>time},$domain,$user);
} else {
$status='no_host';
}
+# Notifications
+ my %userenv = &Apache::lonnet::get('environment',['notification'],
+ $domain,$user);
+ if ($userenv{'notification'}) {
+ &sendnotification($userenv{'notification'},$user,$domain,$subject,0);
+ }
&Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
$ENV{'user.home'},
'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
return $status;
}
+# New routine that respects "forward" and calls old routine
+
+sub user_normal_msg {
+ my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl)=@_;
+ my $status='';
+ my %userenv = &Apache::lonnet::get('environment',['msgforward'],
+ $domain,$user);
+ my $msgforward=$userenv{'msgforward'};
+ if ($msgforward) {
+ foreach (split(/\,/,$msgforward)) {
+ my ($forwuser,$forwdomain)=split(/\:/,$_);
+ $status.=
+ &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
+ $citation,$baseurl,$attachmenturl).' ';
+ }
+ } else {
+ $status=&user_normal_msg_raw($user,$domain,$subject,$message,
+ $citation,$baseurl,$attachmenturl);
+ }
+ return $status;
+}
+
+
# =============================================================== Status Change
sub statuschange {
@@ -342,11 +481,15 @@ $content{'sendername'}.'@'.
sub comprep {
my ($r,$msgid)=@_;
my %message=&Apache::lonnet::get('nohist_email',[$msgid]);
- my %content=&unpackagemsg($message{$msgid});
+ my %content=&unpackagemsg($message{$msgid},1);
my $quotemsg='> '.$content{'message'};
$quotemsg=~s/\r/\n/g;
$quotemsg=~s/\f/\n/g;
$quotemsg=~s/\n+/\n\> /g;
+ my $torepl=&Apache::loncommon::aboutmewrapper(
+ &Apache::loncommon::plainname($content{'sendername'},$content{'senderdomain'}),$content{'sendername'},$content{'senderdomain'}).' ('.
+$content{'sendername'}.'@'.
+ $content{'senderdomain'}.')';
my $subject='Re: '.$content{'subject'};
my $dispcrit='';
if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
@@ -360,6 +503,7 @@ sub comprep {
$r->print(<<"ENDREPLY");