Annotation of loncom/interface/lonmsg.pm, revision 1.192

1.1       www         1: # The LearningOnline Network with CAPA
1.26      albertel    2: # Routines for messaging
                      3: #
1.192   ! raeburn     4: # $Id: lonmsg.pm,v 1.191 2006/12/24 22:13:19 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: 
                     31: use strict;
1.140     albertel   32: use Apache::lonnet;
1.47      albertel   33: use HTML::TokeParser();
1.180     albertel   34: use Apache::lonlocal;
1.53      www        35: use Mail::Send;
1.187     albertel   36: use LONCAPA qw(:DEFAULT :match);
1.180     albertel   37: 
                     38: {
                     39:     my $uniq;
                     40:     sub get_uniq {
                     41: 	$uniq++;
                     42: 	return $uniq;
                     43:     }
                     44: }
1.65      www        45: 
1.1       www        46: # ===================================================================== Package
                     47: 
1.3       www        48: sub packagemsg {
1.108     www        49:     my ($subject,$message,$citation,$baseurl,$attachmenturl,
1.191     raeburn    50: 	$recuser,$recdomain,$msgid,$type,$crsmsgid,$symb,$error)=@_;
1.96      albertel   51:     $message =&HTML::Entities::encode($message,'<>&"');
                     52:     $citation=&HTML::Entities::encode($citation,'<>&"');
                     53:     $subject =&HTML::Entities::encode($subject,'<>&"');
1.49      albertel   54:     #remove machine specification
                     55:     $baseurl =~ s|^http://[^/]+/|/|;
1.96      albertel   56:     $baseurl =&HTML::Entities::encode($baseurl,'<>&"');
1.51      www        57:     #remove machine specification
                     58:     $attachmenturl =~ s|^http://[^/]+/|/|;
1.96      albertel   59:     $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
1.158     raeburn    60:     my $course_context;
                     61:     if (defined($env{'form.replyid'})) {
                     62:         my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid)=
1.184     www        63:                    split(/\:/,&unescape($env{'form.replyid'}));
1.158     raeburn    64:         $course_context = $origcid;
                     65:     }
1.160     raeburn    66:     foreach my $key (keys(%env)) {
                     67:         if ($key=~/^form\.(rep)?rec\_(.*)$/) {
                     68:             my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) =
1.184     www        69:                                     split(/\:/,&unescape($2));
1.160     raeburn    70:             $course_context = $origcid;
                     71:             last;
                     72:         }
                     73:     }
1.158     raeburn    74:     unless(defined($course_context)) {
                     75:         $course_context = $env{'request.course.id'};
                     76:     }
1.2       www        77:     my $now=time;
1.180     albertel   78:     my $msgcount = &get_uniq();
1.156     raeburn    79:     unless(defined($msgid)) {
1.159     raeburn    80:         $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},
1.191     raeburn    81:                            $msgcount,$course_context,$symb,$error,$$);
1.156     raeburn    82:     }
1.174     raeburn    83:     my $result = '<sendername>'.$env{'user.name'}.'</sendername>'.
1.140     albertel   84:            '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
1.1       www        85:            '<subject>'.$subject.'</subject>'.
1.174     raeburn    86:            '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>';
                     87:     if (defined($crsmsgid)) {
                     88:         $result.= '<courseid>'.$course_context.'</courseid>'.
                     89:                   '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
                     90:                   '<msgid>'.$msgid.'</msgid>'.
                     91:                   '<coursemsgid>'.$crsmsgid.'</coursemsgid>'.
                     92:                   '<message>'.$message.'</message>';
                     93:         return ($msgid,$result);
                     94:     }
                     95:     $result .= '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
1.1       www        96:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
                     97: 	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
1.140     albertel   98: 	   '<browsertype>'.$env{'browser.type'}.'</browsertype>'.
                     99: 	   '<browseros>'.$env{'browser.os'}.'</browseros>'.
                    100: 	   '<browserversion>'.$env{'browser.version'}.'</browserversion>'.
                    101:            '<browsermathml>'.$env{'browser.mathml'}.'</browsermathml>'.
1.1       www       102: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
1.158     raeburn   103: 	   '<courseid>'.$course_context.'</courseid>'.
1.140     albertel  104: 	   '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
                    105: 	   '<role>'.$env{'request.role'}.'</role>'.
                    106: 	   '<resource>'.$env{'request.filename'}.'</resource>'.
1.156     raeburn   107:            '<msgid>'.$msgid.'</msgid>';
                    108:     if (ref($recuser) eq 'ARRAY') {
                    109:         for (my $i=0; $i<@{$recuser}; $i++) {
1.162     raeburn   110:             if ($type eq 'dcmail') {
                    111:                 my ($username,$email) = split(/:/,$$recuser[$i]);
1.184     www       112:                 $username = &unescape($username);
                    113:                 $email = &unescape($email);
1.162     raeburn   114:                 $username = &HTML::Entities::encode($username,'<>&"');
                    115:                 $email = &HTML::Entities::encode($email,'<>&"');
                    116:                 $result .= '<recipient username="'.$username.'">'.
                    117:                                             $email.'</recipient>';
                    118:             } else {
                    119:                 $result .= '<recuser>'.$$recuser[$i].'</recuser>'.
                    120:                            '<recdomain>'.$$recdomain[$i].'</recdomain>';
                    121:             }
1.156     raeburn   122:         }
                    123:     } else {
                    124:         $result .= '<recuser>'.$recuser.'</recuser>'.
                    125:                    '<recdomain>'.$recdomain.'</recdomain>';
                    126:     }
                    127:     $result .= '<message>'.$message.'</message>';
1.49      albertel  128:     if (defined($citation)) {
                    129: 	$result.='<citation>'.$citation.'</citation>';
                    130:     }
                    131:     if (defined($baseurl)) {
                    132: 	$result.= '<baseurl>'.$baseurl.'</baseurl>';
                    133:     }
1.51      www       134:     if (defined($attachmenturl)) {
1.52      www       135: 	$result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
1.51      www       136:     }
1.191     raeburn   137:     if (defined($symb)) {
                    138:         $result.= '<symb>'.$symb.'</symb>';
                    139:         if (defined($course_context)) {
                    140:             if ($course_context eq $env{'request.course.id'}) {
                    141:                 my $resource_title = &Apache::lonnet::gettitle($symb);
                    142:                 if (defined($resource_title)) {
                    143:                     $result .= '<resource_title>'.$resource_title.'</resource_title>';
                    144:                 }
                    145:             }
                    146:         }
                    147:     }
                    148:     return ($msgid,$result);
1.1       www       149: }
                    150: 
1.2       www       151: # ================================================== Unpack message into a hash
                    152: 
1.3       www       153: sub unpackagemsg {
1.52      www       154:     my ($message,$notoken)=@_;
1.2       www       155:     my %content=();
                    156:     my $parser=HTML::TokeParser->new(\$message);
                    157:     my $token;
                    158:     while ($token=$parser->get_token) {
                    159:        if ($token->[0] eq 'S') {
                    160: 	   my $entry=$token->[1];
                    161:            my $value=$parser->get_text('/'.$entry);
1.156     raeburn   162:            if (($entry eq 'recuser') || ($entry eq 'recdomain')) {
                    163:                push(@{$content{$entry}},$value);
1.162     raeburn   164:            } elsif ($entry eq 'recipient') {
                    165:                my $username = $token->[2]{'username'};
                    166:                $username = &HTML::Entities::decode($username,'<>&"');
                    167:                $content{$entry}{$username} = $value;
1.156     raeburn   168:            } else {
                    169:                $content{$entry}=$value;
                    170:            }
1.2       www       171:        }
                    172:     }
1.168     albertel  173:     if (!exists($content{'recuser'})) { $content{'recuser'} = []; }
1.52      www       174:     if ($content{'attachmenturl'}) {
1.100     albertel  175:        my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
1.52      www       176:        if ($notoken) {
1.100     albertel  177: 	   $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';
1.52      www       178:        } else {
1.99      albertel  179: 	   &Apache::lonnet::allowuploaded('/adm/msg',
                    180: 					  $content{'attachmenturl'});
                    181: 	   $content{'message'}.='<p>'.&mt('Attachment').
                    182: 	       ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
1.100     albertel  183: 	       $fname.'</tt></a>';
1.52      www       184:        }
                    185:     }
1.2       www       186:     return %content;
                    187: }
                    188: 
1.6       www       189: # ======================================================= Get info out of msgid
                    190: 
1.159     raeburn   191: sub buildmsgid {
1.191     raeburn   192:     my ($now,$subject,$uname,$udom,$msgcount,$course_context,$symb,$error,$pid) = @_;
1.184     www       193:     $subject=&escape($subject);
1.192   ! raeburn   194:     $symb = &escape($symb);
1.184     www       195:     return(&escape($now.':'.$subject.':'.$uname.':'.
1.191     raeburn   196:            $udom.':'.$msgcount.':'.$course_context.':'.$pid.':'.$symb.':'.$error));
1.159     raeburn   197: }
                    198: 
1.6       www       199: sub unpackmsgid {
1.169     albertel  200:     my ($msgid,$folder,$skipstatus,$status_cache)=@_;
1.184     www       201:     $msgid=&unescape($msgid);
1.167     raeburn   202:     my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid,
1.191     raeburn   203:         $processid,$symb,$error) = split(/\:/,&unescape($msgid));
1.184     www       204:     $shortsubj = &unescape($shortsubj);
1.182     albertel  205:     $shortsubj = &HTML::Entities::decode($shortsubj);
1.192   ! raeburn   206:     $symb = &unescape($symb);
1.167     raeburn   207:     if (!defined($processid)) { $fromcid = ''; }
1.164     raeburn   208:     my %status=();
                    209:     unless ($skipstatus) {
1.169     albertel  210: 	if (ref($status_cache)) {
                    211: 	    $status{$msgid} = $status_cache->{$msgid};
                    212: 	} else {
                    213: 	    my $suffix=&foldersuffix($folder);
                    214: 	    %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
                    215: 	}
                    216: 	if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
1.164     raeburn   217:         unless ($status{$msgid}) { $status{$msgid}='new'; }
                    218:     }
1.191     raeburn   219:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid,$symb,$error);
1.141     raeburn   220: }
1.6       www       221: 
1.53      www       222: 
                    223: sub sendemail {
                    224:     my ($to,$subject,$body)=@_;
1.186     www       225:     my %senderemails=&Apache::loncommon::getemails();
                    226:     my $senderaddress='';
                    227:     foreach my $type ('notification','permanentemail','critnotification') {
                    228: 	if ($senderemails{$type}) {
                    229: 	    $senderaddress=$senderemails{$type};
                    230: 	}
                    231:     }
1.53      www       232:     $body=
1.67      www       233:     "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
1.186     www       234:     "*** ".($senderaddress?&mt('You can reply to this message'):&mt('Please do not reply to this address.')."\n*** ".
                    235: 	    &mt('A reply will not be received by the recipient!'))."\n\n".$body;
1.53      www       236:     my $msg = new Mail::Send;
                    237:     $msg->to($to);
                    238:     $msg->subject('[LON-CAPA] '.$subject);
1.188     www       239:     if ($senderaddress) { $msg->add('Reply-to',$senderaddress); $msg->add('From',$senderaddress); }
1.97      matthew   240:     if (my $fh = $msg->open()) {
1.172     albertel  241: 	print $fh $body;
                    242: 	$fh->close;
1.68      www       243:     }
1.53      www       244: }
                    245: 
                    246: # ==================================================== Send notification emails
                    247: 
                    248: sub sendnotification {
1.131     www       249:     my ($to,$touname,$toudom,$subj,$crit,$text)=@_;
1.140     albertel  250:     my $sender=$env{'environment.firstname'}.' '.$env{'environment.lastname'};
1.131     www       251:     unless ($sender=~/\w/) { 
1.172     albertel  252: 	$sender=$env{'user.name'}.'@'.$env{'user.domain'};
1.131     www       253:     }
1.53      www       254:     my $critical=($crit?' critical':'');
1.131     www       255:     $text=~s/\&lt\;/\</gs;
                    256:     $text=~s/\&gt\;/\>/gs;
                    257:     $text=~s/\<\/*[^\>]+\>//gs;
1.53      www       258:     my $url='http://'.
                    259:       $Apache::lonnet::hostname{&Apache::lonnet::homeserver($touname,$toudom)}.
1.54      www       260:       '/adm/email?username='.$touname.'&domain='.$toudom;
1.53      www       261:     my $body=(<<ENDMSG);
                    262: You received a$critical message from $sender in LON-CAPA. The subject is
                    263: 
                    264:  $subj
                    265: 
1.131     www       266: === Excerpt ============================================================
                    267: $text
                    268: ========================================================================
                    269: 
1.53      www       270: Use
                    271: 
                    272:  $url
                    273: 
1.131     www       274: to access the full message.
1.53      www       275: ENDMSG
                    276:     &sendemail($to,'New'.$critical.' message from '.$sender,$body);
                    277: }
1.40      www       278: # ============================================================= Check for email
                    279: 
                    280: sub newmail {
1.140     albertel  281:     if ((time-$env{'user.mailcheck.time'})>300) {
1.40      www       282:         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
                    283:         &Apache::lonnet::appenv('user.mailcheck.time'=>time);
                    284:         if ($what{'recnewemail'}>0) { return 1; }
                    285:     }
                    286:     return 0;
                    287: }
                    288: 
1.1       www       289: # =============================== Automated message to the author of a resource
                    290: 
1.58      bowersj2  291: =pod
                    292: 
                    293: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
                    294:     of the resource with the URI $filename.
                    295: 
                    296: =cut
                    297: 
1.1       www       298: sub author_res_msg {
                    299:     my ($filename,$message)=@_;
1.2       www       300:     unless ($message) { return 'empty'; }
1.1       www       301:     $filename=&Apache::lonnet::declutter($filename);
1.72      www       302:     my ($domain,$author,@dummy)=split(/\//,$filename);
1.1       www       303:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
                    304:     if ($homeserver ne 'no_host') {
                    305:        my $id=unpack("%32C*",$message);
1.181     albertel  306:        $message .= " <p>This error occurred on machine ".
                    307: 	   $Apache::lonnet::perlvar{'lonHostID'}."</p>";
1.2       www       308:        my $msgid;
1.72      www       309:        ($msgid,$message)=&packagemsg($filename,$message);
1.3       www       310:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
1.72      www       311:          ':nohist_res_msgs:'.
1.184     www       312:           &escape($filename.'_'.$id).'='.
                    313:           &escape($message),$homeserver);
1.1       www       314:     }
1.2       www       315:     return 'no_host';
1.73      www       316: }
                    317: 
                    318: # =========================================== Retrieve author resource messages
                    319: 
                    320: sub retrieve_author_res_msg {
1.75      www       321:     my $url=shift;
1.73      www       322:     $url=&Apache::lonnet::declutter($url);
1.187     albertel  323:     my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.76      www       324:     my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
1.73      www       325:     my $msgs='';
                    326:     foreach (keys %errormsgs) {
1.80      www       327: 	if ($_=~/^\Q$url\E\_\d+$/) {
1.73      www       328: 	    my %content=&unpackagemsg($errormsgs{$_});
1.74      www       329: 	    $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
                    330: 		$content{'time'}.'</b>: '.$content{'message'}.
                    331: 		'<br /></p>';
1.73      www       332: 	}
                    333:     } 
                    334:     return $msgs;     
                    335: }
                    336: 
                    337: 
                    338: # =============================== Delete all author messages related to one URL
                    339: 
                    340: sub del_url_author_res_msg {
1.75      www       341:     my $url=shift;
1.73      www       342:     $url=&Apache::lonnet::declutter($url);
1.187     albertel  343:     my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.77      www       344:     my @delmsgs=();
                    345:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
                    346: 	if ($_=~/^\Q$url\E\_\d+$/) {
                    347: 	    push (@delmsgs,$_);
                    348: 	}
                    349:     }
                    350:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
1.73      www       351: }
1.152     www       352: # =================================== Clear out all author messages in URL path
1.73      www       353: 
1.152     www       354: sub clear_author_res_msg {
                    355:     my $url=shift;
                    356:     $url=&Apache::lonnet::declutter($url);
1.187     albertel  357:     my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.152     www       358:     my @delmsgs=();
                    359:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
                    360: 	if ($_=~/^\Q$url\E/) {
                    361: 	    push (@delmsgs,$_);
                    362: 	}
                    363:     }
                    364:     return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
                    365: }
1.73      www       366: # ================= Return hash with URLs for which there is a resource message
                    367: 
                    368: sub all_url_author_res_msg {
                    369:     my ($author,$domain)=@_;
1.75      www       370:     my %returnhash=();
1.76      www       371:     foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
1.75      www       372: 	$_=~/^(.+)\_\d+/;
                    373: 	$returnhash{$1}=1;
                    374:     }
                    375:     return %returnhash;
1.1       www       376: }
                    377: 
1.185     albertel  378: # ====================================== Add a comment to the User Notes screen
                    379: 
                    380: sub store_instructor_comment {
                    381:     my ($msg,$uname,$udom) = @_;
                    382:     my $cid  = $env{'request.course.id'};
                    383:     my $cnum = $env{'course.'.$cid.'.num'};
                    384:     my $cdom = $env{'course.'.$cid.'.domain'};
                    385:     my $subject= &mt('Record').' ['.$uname.':'.$udom.']';
                    386:     my $result = &user_normal_msg_raw($cnum,$cdom,$subject,$msg);
                    387:     return $result;
                    388: }
                    389: 
1.1       www       390: # ================================================== Critical message to a user
                    391: 
1.38      www       392: sub user_crit_msg_raw {
1.159     raeburn   393:     my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
1.2       www       394: # Check if allowed missing
1.190     raeburn   395:     my ($status,$packed_message);
1.2       www       396:     my $msgid='undefined';
                    397:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
1.131     www       398:     my $text=$message;
1.2       www       399:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    400:     if ($homeserver ne 'no_host') {
1.190     raeburn   401:        ($msgid,$packed_message)=&packagemsg($subject,$message);
                    402:        if ($sendback) { $packed_message.='<sendback>true</sendback>'; }
1.4       www       403:        $status=&Apache::lonnet::critical(
                    404:            'put:'.$domain.':'.$user.':critical:'.
1.184     www       405:            &escape($msgid).'='.
1.190     raeburn   406:            &escape($packed_message),$homeserver);
1.159     raeburn   407:         if (defined($sentmessage)) {
1.190     raeburn   408:             $$sentmessage = $packed_message;
1.159     raeburn   409:         }
1.190     raeburn   410:         (undef,my $packed_message_no_citation) =
                    411:             &packagemsg($subject,$message,undef,undef,undef,$user,$domain,
                    412:                         $msgid);
                    413:         $status .= &store_sent_mail($msgid,$packed_message_no_citation);
1.2       www       414:     } else {
                    415:        $status='no_host';
                    416:     }
1.190     raeburn   417: 
1.53      www       418: # Notifications
1.186     www       419:     my %userenv = &Apache::loncommon::getemails($user,$domain);
1.53      www       420:     if ($userenv{'critnotification'}) {
1.131     www       421:       &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1,
                    422: 			$text);
1.53      www       423:     }
1.132     www       424:     if ($toperm && $userenv{'permanentemail'}) {
                    425:       &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,1,
                    426: 			$text);
                    427:     }
1.53      www       428: # Log this
1.2       www       429:     &Apache::lonnet::logthis(
1.4       www       430:       'Sending critical email '.$msgid.
1.2       www       431:       ', log status: '.
1.140     albertel  432:       &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    433:                          $env{'user.home'},
1.2       www       434:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
1.4       www       435:       .$status));
1.2       www       436:     return $status;
                    437: }
                    438: 
1.38      www       439: # New routine that respects "forward" and calls old routine
                    440: 
1.58      bowersj2  441: =pod
                    442: 
                    443: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback)>: Sends
                    444:     a critical message $message to the $user at $domain. If $sendback is true,
                    445:     a reciept will be sent to the current user when $user recieves the message.
                    446: 
1.183     albertel  447:     Additionally it will check if the user has a Forwarding address
                    448:     set, and send the message to that address instead
                    449: 
                    450:     returns 
                    451:       - in array context a list of results for each message that was sent
                    452:       - in scalar context a space seperated list of results for each 
                    453:            message sent
                    454: 
1.58      bowersj2  455: =cut
                    456: 
1.38      www       457: sub user_crit_msg {
1.159     raeburn   458:     my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
1.183     albertel  459:     my @status;
1.38      www       460:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    461:                                        $domain,$user);
                    462:     my $msgforward=$userenv{'msgforward'};
                    463:     if ($msgforward) {
1.183     albertel  464:        foreach my $addr (split(/\,/,$msgforward)) {
                    465: 	 my ($forwuser,$forwdomain)=split(/\:/,$addr);
                    466:          push(@status,
                    467: 	      &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
                    468: 				 $sendback,$toperm,$sentmessage));
1.38      www       469:        }
                    470:     } else { 
1.183     albertel  471: 	push(@status,
                    472: 	     &user_crit_msg_raw($user,$domain,$subject,$message,$sendback,
                    473: 				$toperm,$sentmessage));
1.38      www       474:     }
1.183     albertel  475:     if (wantarray) {
                    476: 	return @status;
                    477:     }
                    478:     return join(' ',@status);
1.38      www       479: }
                    480: 
1.2       www       481: # =================================================== Critical message received
                    482: 
                    483: sub user_crit_received {
1.12      www       484:     my $msgid=shift;
                    485:     my %message=&Apache::lonnet::get('critical',[$msgid]);
1.52      www       486:     my %contents=&unpackagemsg($message{$msgid},1);
1.24      www       487:     my $status='rec: '.($contents{'sendback'}?
1.5       www       488:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
1.140     albertel  489:                      &mt('Receipt').': '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.', '.$contents{'subject'},
                    490:                      &mt('User').' '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.
1.42      www       491:                      ' acknowledged receipt of message'."\n".'   "'.
1.67      www       492:                      $contents{'subject'}.'"'."\n".&mt('dated').' '.
1.42      www       493:                      $contents{'time'}.".\n"
                    494:                      ):'no msg req');
1.5       www       495:     $status.=' trans: '.
1.12      www       496:      &Apache::lonnet::put(
                    497:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
1.5       www       498:     $status.=' del: '.
1.9       albertel  499:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.140     albertel  500:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    501:                          $env{'user.home'},'Received critical message '.
1.5       www       502:                          $contents{'msgid'}.
                    503:                          ', '.$status);
1.12      www       504:     return $status;
1.2       www       505: }
                    506: 
                    507: # ======================================================== Normal communication
                    508: 
1.38      www       509: sub user_normal_msg_raw {
1.132     www       510:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.191     raeburn   511:         $toperm,$currid,$newid,$sentmessage,$crsmsgid,$symb,$restitle,
                    512:         $error)=@_;
1.2       www       513: # Check if allowed missing
1.173     albertel  514:     my ($status,$packed_message);
1.2       www       515:     my $msgid='undefined';
1.131     www       516:     my $text=$message;
1.2       www       517:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
                    518:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    519:     if ($homeserver ne 'no_host') {
1.173     albertel  520:        ($msgid,$packed_message)=
                    521: 	                 &packagemsg($subject,$message,$citation,$baseurl,
1.174     raeburn   522:                                      $attachmenturl,$user,$domain,$currid,
1.191     raeburn   523:                                      undef,$crsmsgid,$symb,$error);
1.174     raeburn   524: 
1.108     www       525: # Store in user folder
1.4       www       526:        $status=&Apache::lonnet::critical(
                    527:            'put:'.$domain.':'.$user.':nohist_email:'.
1.184     www       528:            &escape($msgid).'='.
                    529:            &escape($packed_message),$homeserver);
1.108     www       530: # Save new message received time
1.40      www       531:        &Apache::lonnet::put
                    532:                          ('email_status',{'recnewemail'=>time},$domain,$user);
1.159     raeburn   533: # Into sent-mail folder unless a broadcast message or critical message
                    534:        unless (($env{'request.course.id'}) && 
                    535:                (($env{'form.sendmode'} eq 'group')  || 
                    536:                (($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
1.170     albertel  537:                (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    538: 		|| &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    539: 					    '/'.$env{'request.course.sec'})))) {
1.190     raeburn   540:            (undef,my $packed_message_no_citation) =
                    541:                &packagemsg($subject,$message,undef,$baseurl,$attachmenturl,
1.191     raeburn   542:                            $user,$domain,$currid,undef,$crsmsgid,$symb,$error);
1.173     albertel  543:            $status .= &store_sent_mail($msgid,$packed_message_no_citation);
1.156     raeburn   544:        }
1.2       www       545:     } else {
                    546:        $status='no_host';
1.53      www       547:     }
1.156     raeburn   548:     if (defined($newid)) {
                    549:         $$newid = $msgid;
                    550:     }
1.159     raeburn   551:     if (defined($sentmessage)) {
1.173     albertel  552:         $$sentmessage = $packed_message;
1.159     raeburn   553:     }
                    554: 
1.53      www       555: # Notifications
1.132     www       556:     my %userenv = &Apache::lonnet::get('environment',['notification',
                    557:                                                       'permanentemail'],
1.53      www       558:                                        $domain,$user);
                    559:     if ($userenv{'notification'}) {
1.131     www       560: 	&sendnotification($userenv{'notification'},$user,$domain,$subject,0,
                    561: 			  $text);
1.2       www       562:     }
1.132     www       563:     if ($toperm && $userenv{'permanentemail'}) {
1.180     albertel  564: 	&sendnotification($userenv{'permanentemail'},$user,$domain,$subject,0,
                    565: 			  $text);
1.132     www       566:     }
1.140     albertel  567:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    568:                          $env{'user.home'},
1.2       www       569:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
                    570:     return $status;
                    571: }
1.38      www       572: 
                    573: # New routine that respects "forward" and calls old routine
                    574: 
1.58      bowersj2  575: =pod
                    576: 
1.191     raeburn   577: =item * B<user_normal_msg($user, $domain, $subject, $message, $citation,
                    578:        $baseurl, $attachmenturl, $toperm, $sentmessage, $symb, $restitle, $error)>:
                    579:  Sends a message to the  $user at $domain, with subject $subject and message $message.
1.58      bowersj2  580: 
                    581: =cut
                    582: 
1.38      www       583: sub user_normal_msg {
1.132     www       584:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.191     raeburn   585: 	$toperm,$sentmessage,$symb,$restitle,$error)=@_;
1.38      www       586:     my $status='';
                    587:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    588:                                        $domain,$user);
                    589:     my $msgforward=$userenv{'msgforward'};
                    590:     if ($msgforward) {
1.171     banghart  591:         foreach (split(/\,/,$msgforward)) {
1.172     albertel  592: 	    my ($forwuser,$forwdomain)=split(/\:/,$_);
                    593: 	    $status.=
1.171     banghart  594: 	        &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
1.172     albertel  595: 				     $citation,$baseurl,$attachmenturl,$toperm,
1.191     raeburn   596: 				     undef,undef,$sentmessage,undef,$symb,$restitle,$error).' ';
1.171     banghart  597:         }
1.191     raeburn   598:     } else {
1.172     albertel  599: 	$status=&user_normal_msg_raw($user,$domain,$subject,$message,
                    600: 				     $citation,$baseurl,$attachmenturl,$toperm,
1.191     raeburn   601: 				     undef,undef,$sentmessage,undef,$symb,$restitle,$error);
1.38      www       602:     }
                    603:     return $status;
                    604: }
                    605: 
1.156     raeburn   606: sub store_sent_mail {
                    607:     my ($msgid,$message) = @_;
1.171     banghart  608:     my $status =' '.&Apache::lonnet::critical(
                    609:                'put:'.$env{'user.domain'}.':'.$env{'user.name'}.
                    610:                                           ':nohist_email_sent:'.
1.184     www       611:                &escape($msgid).'='.
                    612:                &escape($message),$env{'user.home'});
1.156     raeburn   613:     return $status;
                    614: }
1.2       www       615: 
1.106     www       616: # =============================================================== Folder suffix
                    617: 
                    618: sub foldersuffix {
                    619:     my $folder=shift;
                    620:     unless ($folder) { return ''; }
1.189     raeburn   621:     my $suffix;
                    622:     my %folderhash = &get_user_folders($folder);
                    623:     if (ref($folderhash{$folder}) eq 'HASH') {
                    624:         $suffix = '_'.&escape($folderhash{$folder}{'id'});
                    625:     } else {
                    626:         $suffix = '_'.&escape($folder);
                    627:     }
                    628:     return $suffix;
                    629: }
                    630: 
                    631: # ========================================================= User-defined folders 
                    632: 
                    633: sub get_user_folders {
                    634:     my ($folder) = @_;
                    635:     my %userfolders = 
                    636:           &Apache::lonnet::dump('email_folders',undef,undef,$folder);
                    637:     my $lock = "\0".'lock_counter'; # locks db while counter incremented
                    638:     my $counter = "\0".'idcount';   # used in suffix for email db files
                    639:     if (defined($userfolders{$lock})) {
                    640:         delete($userfolders{$lock});
                    641:     }
                    642:     if (defined($userfolders{$counter})) {
                    643:         delete($userfolders{$counter});
                    644:     }
                    645:     return %userfolders;
1.106     www       646: }
                    647: 
1.180     albertel  648: 1;
1.1       www       649: __END__
                    650: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>