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

1.1       www         1: # The LearningOnline Network with CAPA
1.26      albertel    2: # Routines for messaging
                      3: #
1.193   ! raeburn     4: # $Id: lonmsg.pm,v 1.192 2006/12/27 18:00:00 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.193   ! raeburn   410:         if ($env{'request.course.id'} eq '') {
        !           411:             (undef,my $packed_message_no_citation) =
1.190     raeburn   412:             &packagemsg($subject,$message,undef,undef,undef,$user,$domain,
                    413:                         $msgid);
1.193   ! raeburn   414:             if ($status eq 'ok' || $status eq 'con_delayed') {
        !           415:                 &store_sent_mail($msgid,$packed_message_no_citation);
        !           416:             }
        !           417:         }
1.2       www       418:     } else {
                    419:        $status='no_host';
                    420:     }
1.190     raeburn   421: 
1.53      www       422: # Notifications
1.186     www       423:     my %userenv = &Apache::loncommon::getemails($user,$domain);
1.53      www       424:     if ($userenv{'critnotification'}) {
1.131     www       425:       &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1,
                    426: 			$text);
1.53      www       427:     }
1.132     www       428:     if ($toperm && $userenv{'permanentemail'}) {
                    429:       &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,1,
                    430: 			$text);
                    431:     }
1.53      www       432: # Log this
1.2       www       433:     &Apache::lonnet::logthis(
1.4       www       434:       'Sending critical email '.$msgid.
1.2       www       435:       ', log status: '.
1.140     albertel  436:       &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    437:                          $env{'user.home'},
1.2       www       438:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
1.4       www       439:       .$status));
1.2       www       440:     return $status;
                    441: }
                    442: 
1.38      www       443: # New routine that respects "forward" and calls old routine
                    444: 
1.58      bowersj2  445: =pod
                    446: 
                    447: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback)>: Sends
                    448:     a critical message $message to the $user at $domain. If $sendback is true,
                    449:     a reciept will be sent to the current user when $user recieves the message.
                    450: 
1.183     albertel  451:     Additionally it will check if the user has a Forwarding address
                    452:     set, and send the message to that address instead
                    453: 
                    454:     returns 
                    455:       - in array context a list of results for each message that was sent
                    456:       - in scalar context a space seperated list of results for each 
                    457:            message sent
                    458: 
1.58      bowersj2  459: =cut
                    460: 
1.38      www       461: sub user_crit_msg {
1.159     raeburn   462:     my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
1.183     albertel  463:     my @status;
1.38      www       464:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    465:                                        $domain,$user);
                    466:     my $msgforward=$userenv{'msgforward'};
                    467:     if ($msgforward) {
1.183     albertel  468:        foreach my $addr (split(/\,/,$msgforward)) {
                    469: 	 my ($forwuser,$forwdomain)=split(/\:/,$addr);
                    470:          push(@status,
                    471: 	      &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
                    472: 				 $sendback,$toperm,$sentmessage));
1.38      www       473:        }
                    474:     } else { 
1.183     albertel  475: 	push(@status,
                    476: 	     &user_crit_msg_raw($user,$domain,$subject,$message,$sendback,
                    477: 				$toperm,$sentmessage));
1.38      www       478:     }
1.183     albertel  479:     if (wantarray) {
                    480: 	return @status;
                    481:     }
                    482:     return join(' ',@status);
1.38      www       483: }
                    484: 
1.2       www       485: # =================================================== Critical message received
                    486: 
                    487: sub user_crit_received {
1.12      www       488:     my $msgid=shift;
                    489:     my %message=&Apache::lonnet::get('critical',[$msgid]);
1.52      www       490:     my %contents=&unpackagemsg($message{$msgid},1);
1.24      www       491:     my $status='rec: '.($contents{'sendback'}?
1.5       www       492:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
1.140     albertel  493:                      &mt('Receipt').': '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.', '.$contents{'subject'},
                    494:                      &mt('User').' '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.
1.42      www       495:                      ' acknowledged receipt of message'."\n".'   "'.
1.67      www       496:                      $contents{'subject'}.'"'."\n".&mt('dated').' '.
1.42      www       497:                      $contents{'time'}.".\n"
                    498:                      ):'no msg req');
1.5       www       499:     $status.=' trans: '.
1.12      www       500:      &Apache::lonnet::put(
                    501:      'nohist_email',{$contents{'msgid'} => $message{$msgid}});
1.5       www       502:     $status.=' del: '.
1.9       albertel  503:      &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.140     albertel  504:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    505:                          $env{'user.home'},'Received critical message '.
1.5       www       506:                          $contents{'msgid'}.
                    507:                          ', '.$status);
1.12      www       508:     return $status;
1.2       www       509: }
                    510: 
                    511: # ======================================================== Normal communication
                    512: 
1.38      www       513: sub user_normal_msg_raw {
1.132     www       514:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.191     raeburn   515:         $toperm,$currid,$newid,$sentmessage,$crsmsgid,$symb,$restitle,
                    516:         $error)=@_;
1.2       www       517: # Check if allowed missing
1.173     albertel  518:     my ($status,$packed_message);
1.2       www       519:     my $msgid='undefined';
1.131     www       520:     my $text=$message;
1.2       www       521:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
                    522:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    523:     if ($homeserver ne 'no_host') {
1.173     albertel  524:        ($msgid,$packed_message)=
                    525: 	                 &packagemsg($subject,$message,$citation,$baseurl,
1.174     raeburn   526:                                      $attachmenturl,$user,$domain,$currid,
1.191     raeburn   527:                                      undef,$crsmsgid,$symb,$error);
1.174     raeburn   528: 
1.108     www       529: # Store in user folder
1.4       www       530:        $status=&Apache::lonnet::critical(
                    531:            'put:'.$domain.':'.$user.':nohist_email:'.
1.184     www       532:            &escape($msgid).'='.
                    533:            &escape($packed_message),$homeserver);
1.108     www       534: # Save new message received time
1.40      www       535:        &Apache::lonnet::put
                    536:                          ('email_status',{'recnewemail'=>time},$domain,$user);
1.159     raeburn   537: # Into sent-mail folder unless a broadcast message or critical message
                    538:        unless (($env{'request.course.id'}) && 
                    539:                (($env{'form.sendmode'} eq 'group')  || 
                    540:                (($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
1.170     albertel  541:                (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
                    542: 		|| &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
                    543: 					    '/'.$env{'request.course.sec'})))) {
1.190     raeburn   544:            (undef,my $packed_message_no_citation) =
                    545:                &packagemsg($subject,$message,undef,$baseurl,$attachmenturl,
1.191     raeburn   546:                            $user,$domain,$currid,undef,$crsmsgid,$symb,$error);
1.193   ! raeburn   547:            if ($status eq 'ok' || $status eq 'con_delayed') {
        !           548:                &store_sent_mail($msgid,$packed_message_no_citation);
        !           549:            }
1.156     raeburn   550:        }
1.2       www       551:     } else {
                    552:        $status='no_host';
1.53      www       553:     }
1.156     raeburn   554:     if (defined($newid)) {
                    555:         $$newid = $msgid;
                    556:     }
1.159     raeburn   557:     if (defined($sentmessage)) {
1.173     albertel  558:         $$sentmessage = $packed_message;
1.159     raeburn   559:     }
                    560: 
1.53      www       561: # Notifications
1.132     www       562:     my %userenv = &Apache::lonnet::get('environment',['notification',
                    563:                                                       'permanentemail'],
1.53      www       564:                                        $domain,$user);
                    565:     if ($userenv{'notification'}) {
1.131     www       566: 	&sendnotification($userenv{'notification'},$user,$domain,$subject,0,
                    567: 			  $text);
1.2       www       568:     }
1.132     www       569:     if ($toperm && $userenv{'permanentemail'}) {
1.180     albertel  570: 	&sendnotification($userenv{'permanentemail'},$user,$domain,$subject,0,
                    571: 			  $text);
1.132     www       572:     }
1.140     albertel  573:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
                    574:                          $env{'user.home'},
1.2       www       575:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
                    576:     return $status;
                    577: }
1.38      www       578: 
                    579: # New routine that respects "forward" and calls old routine
                    580: 
1.58      bowersj2  581: =pod
                    582: 
1.191     raeburn   583: =item * B<user_normal_msg($user, $domain, $subject, $message, $citation,
                    584:        $baseurl, $attachmenturl, $toperm, $sentmessage, $symb, $restitle, $error)>:
                    585:  Sends a message to the  $user at $domain, with subject $subject and message $message.
1.58      bowersj2  586: 
                    587: =cut
                    588: 
1.38      www       589: sub user_normal_msg {
1.132     www       590:     my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.191     raeburn   591: 	$toperm,$sentmessage,$symb,$restitle,$error)=@_;
1.38      www       592:     my $status='';
                    593:     my %userenv = &Apache::lonnet::get('environment',['msgforward'],
                    594:                                        $domain,$user);
                    595:     my $msgforward=$userenv{'msgforward'};
                    596:     if ($msgforward) {
1.171     banghart  597:         foreach (split(/\,/,$msgforward)) {
1.172     albertel  598: 	    my ($forwuser,$forwdomain)=split(/\:/,$_);
                    599: 	    $status.=
1.171     banghart  600: 	        &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
1.172     albertel  601: 				     $citation,$baseurl,$attachmenturl,$toperm,
1.191     raeburn   602: 				     undef,undef,$sentmessage,undef,$symb,$restitle,$error).' ';
1.171     banghart  603:         }
1.191     raeburn   604:     } else {
1.172     albertel  605: 	$status=&user_normal_msg_raw($user,$domain,$subject,$message,
                    606: 				     $citation,$baseurl,$attachmenturl,$toperm,
1.191     raeburn   607: 				     undef,undef,$sentmessage,undef,$symb,$restitle,$error);
1.38      www       608:     }
                    609:     return $status;
                    610: }
                    611: 
1.156     raeburn   612: sub store_sent_mail {
                    613:     my ($msgid,$message) = @_;
1.171     banghart  614:     my $status =' '.&Apache::lonnet::critical(
                    615:                'put:'.$env{'user.domain'}.':'.$env{'user.name'}.
                    616:                                           ':nohist_email_sent:'.
1.184     www       617:                &escape($msgid).'='.
                    618:                &escape($message),$env{'user.home'});
1.156     raeburn   619:     return $status;
                    620: }
1.2       www       621: 
1.106     www       622: # =============================================================== Folder suffix
                    623: 
                    624: sub foldersuffix {
                    625:     my $folder=shift;
                    626:     unless ($folder) { return ''; }
1.189     raeburn   627:     my $suffix;
                    628:     my %folderhash = &get_user_folders($folder);
                    629:     if (ref($folderhash{$folder}) eq 'HASH') {
                    630:         $suffix = '_'.&escape($folderhash{$folder}{'id'});
                    631:     } else {
                    632:         $suffix = '_'.&escape($folder);
                    633:     }
                    634:     return $suffix;
                    635: }
                    636: 
                    637: # ========================================================= User-defined folders 
                    638: 
                    639: sub get_user_folders {
                    640:     my ($folder) = @_;
                    641:     my %userfolders = 
                    642:           &Apache::lonnet::dump('email_folders',undef,undef,$folder);
                    643:     my $lock = "\0".'lock_counter'; # locks db while counter incremented
                    644:     my $counter = "\0".'idcount';   # used in suffix for email db files
                    645:     if (defined($userfolders{$lock})) {
                    646:         delete($userfolders{$lock});
                    647:     }
                    648:     if (defined($userfolders{$counter})) {
                    649:         delete($userfolders{$counter});
                    650:     }
                    651:     return %userfolders;
1.106     www       652: }
                    653: 
1.180     albertel  654: 1;
1.1       www       655: __END__
                    656: 

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