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

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

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