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

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

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