Annotation of loncom/interface/lonmsg.pm, revision 1.201
1.1 www 1: # The LearningOnline Network with CAPA
1.26 albertel 2: # Routines for messaging
3: #
1.201 ! raeburn 4: # $Id: lonmsg.pm,v 1.200 2007/04/22 13:41:22 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:
1.199 raeburn 31: =pod
32:
33: =head1 NAME
34:
35: Apache::lonmsg: supports internal messaging
36:
37: =head1 SYNOPSIS
38:
39: lonmsg provides routines for sending messages.
40:
41: Right now, this document will cover just how to send a message, since
42: it is likely you will not need to programmatically read messages,
43: since lonmsg already implements that functionality.
44:
45: The routines used to package messages and unpackage messages are not
46: only used by lonmsg when creating/extracting messages for LON-CAPA's
47: internal messaging system, but also by lonnotify.pm which is available
48: for use by Domain Coordinators to broadcast standard e-mail to specified
49: users in their domain. The XML packaging used in the two cases is very
50: similar. The differences are the use of <recuser>$uname</recuser> and
51: <recdomain>$udom</recdomain> in stored internal messages, compared
52: with <recipient username="$uname:$udom">$email</recipient> in stored
53: Domain Coordinator e-mail for the storage of information about
54: recipients of the message/e-mail.
55:
56: =head1 FUNCTIONS
57:
58: =over 4
59:
60: =cut
61:
1.1 www 62: use strict;
1.140 albertel 63: use Apache::lonnet;
1.47 albertel 64: use HTML::TokeParser();
1.180 albertel 65: use Apache::lonlocal;
1.53 www 66: use Mail::Send;
1.187 albertel 67: use LONCAPA qw(:DEFAULT :match);
1.180 albertel 68:
69: {
70: my $uniq;
71: sub get_uniq {
72: $uniq++;
73: return $uniq;
74: }
75: }
1.65 www 76:
1.1 www 77: # ===================================================================== Package
78:
1.3 www 79: sub packagemsg {
1.108 www 80: my ($subject,$message,$citation,$baseurl,$attachmenturl,
1.191 raeburn 81: $recuser,$recdomain,$msgid,$type,$crsmsgid,$symb,$error)=@_;
1.96 albertel 82: $message =&HTML::Entities::encode($message,'<>&"');
83: $citation=&HTML::Entities::encode($citation,'<>&"');
84: $subject =&HTML::Entities::encode($subject,'<>&"');
1.49 albertel 85: #remove machine specification
86: $baseurl =~ s|^http://[^/]+/|/|;
1.96 albertel 87: $baseurl =&HTML::Entities::encode($baseurl,'<>&"');
1.51 www 88: #remove machine specification
89: $attachmenturl =~ s|^http://[^/]+/|/|;
1.96 albertel 90: $attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
1.201 ! raeburn 91: my $course_context = &get_course_context();
1.2 www 92: my $now=time;
1.180 albertel 93: my $msgcount = &get_uniq();
1.156 raeburn 94: unless(defined($msgid)) {
1.159 raeburn 95: $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},
1.191 raeburn 96: $msgcount,$course_context,$symb,$error,$$);
1.156 raeburn 97: }
1.174 raeburn 98: my $result = '<sendername>'.$env{'user.name'}.'</sendername>'.
1.140 albertel 99: '<senderdomain>'.$env{'user.domain'}.'</senderdomain>'.
1.1 www 100: '<subject>'.$subject.'</subject>'.
1.174 raeburn 101: '<time>'.&Apache::lonlocal::locallocaltime($now).'</time>';
102: if (defined($crsmsgid)) {
103: $result.= '<courseid>'.$course_context.'</courseid>'.
104: '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
105: '<msgid>'.$msgid.'</msgid>'.
106: '<coursemsgid>'.$crsmsgid.'</coursemsgid>'.
107: '<message>'.$message.'</message>';
108: return ($msgid,$result);
109: }
110: $result .= '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
1.1 www 111: '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
112: '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
1.140 albertel 113: '<browsertype>'.$env{'browser.type'}.'</browsertype>'.
114: '<browseros>'.$env{'browser.os'}.'</browseros>'.
115: '<browserversion>'.$env{'browser.version'}.'</browserversion>'.
116: '<browsermathml>'.$env{'browser.mathml'}.'</browsermathml>'.
1.1 www 117: '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
1.158 raeburn 118: '<courseid>'.$course_context.'</courseid>'.
1.140 albertel 119: '<coursesec>'.$env{'request.course.sec'}.'</coursesec>'.
120: '<role>'.$env{'request.role'}.'</role>'.
121: '<resource>'.$env{'request.filename'}.'</resource>'.
1.156 raeburn 122: '<msgid>'.$msgid.'</msgid>';
123: if (ref($recuser) eq 'ARRAY') {
124: for (my $i=0; $i<@{$recuser}; $i++) {
1.162 raeburn 125: if ($type eq 'dcmail') {
126: my ($username,$email) = split(/:/,$$recuser[$i]);
1.184 www 127: $username = &unescape($username);
128: $email = &unescape($email);
1.162 raeburn 129: $username = &HTML::Entities::encode($username,'<>&"');
130: $email = &HTML::Entities::encode($email,'<>&"');
131: $result .= '<recipient username="'.$username.'">'.
132: $email.'</recipient>';
133: } else {
134: $result .= '<recuser>'.$$recuser[$i].'</recuser>'.
135: '<recdomain>'.$$recdomain[$i].'</recdomain>';
136: }
1.156 raeburn 137: }
138: } else {
139: $result .= '<recuser>'.$recuser.'</recuser>'.
140: '<recdomain>'.$recdomain.'</recdomain>';
141: }
142: $result .= '<message>'.$message.'</message>';
1.49 albertel 143: if (defined($citation)) {
144: $result.='<citation>'.$citation.'</citation>';
145: }
146: if (defined($baseurl)) {
147: $result.= '<baseurl>'.$baseurl.'</baseurl>';
148: }
1.51 www 149: if (defined($attachmenturl)) {
1.52 www 150: $result.= '<attachmenturl>'.$attachmenturl.'</attachmenturl>';
1.51 www 151: }
1.191 raeburn 152: if (defined($symb)) {
153: $result.= '<symb>'.$symb.'</symb>';
1.201 ! raeburn 154: if ($course_context ne '') {
1.191 raeburn 155: if ($course_context eq $env{'request.course.id'}) {
156: my $resource_title = &Apache::lonnet::gettitle($symb);
157: if (defined($resource_title)) {
158: $result .= '<resource_title>'.$resource_title.'</resource_title>';
159: }
160: }
161: }
162: }
163: return ($msgid,$result);
1.1 www 164: }
165:
1.201 ! raeburn 166: sub get_course_context {
! 167: my $course_context;
! 168: if (defined($env{'form.replyid'})) {
! 169: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid)=
! 170: split(/\:/,&unescape($env{'form.replyid'}));
! 171: $course_context = $origcid;
! 172: }
! 173: foreach my $key (keys(%env)) {
! 174: if ($key=~/^form\.(rep)?rec\_(.*)$/) {
! 175: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) =
! 176: split(/\:/,&unescape($2));
! 177: $course_context = $origcid;
! 178: last;
! 179: }
! 180: }
! 181: if ($course_context eq '') {
! 182: $course_context = $env{'request.course.id'};
! 183: }
! 184: return $course_context;
! 185: }
! 186:
1.2 www 187: # ================================================== Unpack message into a hash
188:
1.3 www 189: sub unpackagemsg {
1.52 www 190: my ($message,$notoken)=@_;
1.2 www 191: my %content=();
192: my $parser=HTML::TokeParser->new(\$message);
193: my $token;
194: while ($token=$parser->get_token) {
195: if ($token->[0] eq 'S') {
196: my $entry=$token->[1];
197: my $value=$parser->get_text('/'.$entry);
1.156 raeburn 198: if (($entry eq 'recuser') || ($entry eq 'recdomain')) {
199: push(@{$content{$entry}},$value);
1.162 raeburn 200: } elsif ($entry eq 'recipient') {
201: my $username = $token->[2]{'username'};
202: $username = &HTML::Entities::decode($username,'<>&"');
203: $content{$entry}{$username} = $value;
1.156 raeburn 204: } else {
205: $content{$entry}=$value;
206: }
1.2 www 207: }
208: }
1.168 albertel 209: if (!exists($content{'recuser'})) { $content{'recuser'} = []; }
1.52 www 210: if ($content{'attachmenturl'}) {
1.100 albertel 211: my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
1.52 www 212: if ($notoken) {
1.100 albertel 213: $content{'message'}.='<p>'.&mt('Attachment').': <tt>'.$fname.'</tt>';
1.52 www 214: } else {
1.99 albertel 215: &Apache::lonnet::allowuploaded('/adm/msg',
216: $content{'attachmenturl'});
217: $content{'message'}.='<p>'.&mt('Attachment').
218: ': <a href="'.$content{'attachmenturl'}.'"><tt>'.
1.100 albertel 219: $fname.'</tt></a>';
1.52 www 220: }
221: }
1.2 www 222: return %content;
223: }
224:
1.6 www 225: # ======================================================= Get info out of msgid
226:
1.159 raeburn 227: sub buildmsgid {
1.191 raeburn 228: my ($now,$subject,$uname,$udom,$msgcount,$course_context,$symb,$error,$pid) = @_;
1.184 www 229: $subject=&escape($subject);
1.192 raeburn 230: $symb = &escape($symb);
1.184 www 231: return(&escape($now.':'.$subject.':'.$uname.':'.
1.191 raeburn 232: $udom.':'.$msgcount.':'.$course_context.':'.$pid.':'.$symb.':'.$error));
1.159 raeburn 233: }
234:
1.6 www 235: sub unpackmsgid {
1.169 albertel 236: my ($msgid,$folder,$skipstatus,$status_cache)=@_;
1.184 www 237: $msgid=&unescape($msgid);
1.167 raeburn 238: my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid,
1.191 raeburn 239: $processid,$symb,$error) = split(/\:/,&unescape($msgid));
1.184 www 240: $shortsubj = &unescape($shortsubj);
1.182 albertel 241: $shortsubj = &HTML::Entities::decode($shortsubj);
1.192 raeburn 242: $symb = &unescape($symb);
1.167 raeburn 243: if (!defined($processid)) { $fromcid = ''; }
1.164 raeburn 244: my %status=();
245: unless ($skipstatus) {
1.169 albertel 246: if (ref($status_cache)) {
247: $status{$msgid} = $status_cache->{$msgid};
248: } else {
249: my $suffix=&foldersuffix($folder);
250: %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
251: }
252: if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
1.164 raeburn 253: unless ($status{$msgid}) { $status{$msgid}='new'; }
254: }
1.191 raeburn 255: return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid,$symb,$error);
1.141 raeburn 256: }
1.6 www 257:
1.53 www 258:
259: sub sendemail {
260: my ($to,$subject,$body)=@_;
1.186 www 261: my %senderemails=&Apache::loncommon::getemails();
262: my $senderaddress='';
263: foreach my $type ('notification','permanentemail','critnotification') {
264: if ($senderemails{$type}) {
265: $senderaddress=$senderemails{$type};
266: }
267: }
1.53 www 268: $body=
1.67 www 269: "*** ".&mt('This is an automatic message generated by the LON-CAPA system.')."\n".
1.186 www 270: "*** ".($senderaddress?&mt('You can reply to this message'):&mt('Please do not reply to this address.')."\n*** ".
271: &mt('A reply will not be received by the recipient!'))."\n\n".$body;
1.53 www 272: my $msg = new Mail::Send;
273: $msg->to($to);
274: $msg->subject('[LON-CAPA] '.$subject);
1.188 www 275: if ($senderaddress) { $msg->add('Reply-to',$senderaddress); $msg->add('From',$senderaddress); }
1.97 matthew 276: if (my $fh = $msg->open()) {
1.172 albertel 277: print $fh $body;
278: $fh->close;
1.68 www 279: }
1.53 www 280: }
281:
282: # ==================================================== Send notification emails
283:
284: sub sendnotification {
1.194 raeburn 285: my ($to,$touname,$toudom,$subj,$crit,$text,$msgid)=@_;
1.140 albertel 286: my $sender=$env{'environment.firstname'}.' '.$env{'environment.lastname'};
1.131 www 287: unless ($sender=~/\w/) {
1.172 albertel 288: $sender=$env{'user.name'}.'@'.$env{'user.domain'};
1.131 www 289: }
1.53 www 290: my $critical=($crit?' critical':'');
1.131 www 291: $text=~s/\<\;/\</gs;
292: $text=~s/\>\;/\>/gs;
293: $text=~s/\<\/*[^\>]+\>//gs;
1.53 www 294: my $url='http://'.
1.198 albertel 295: &Apache::lonnet::hostname(&Apache::lonnet::homeserver($touname,$toudom)).
1.54 www 296: '/adm/email?username='.$touname.'&domain='.$toudom;
1.194 raeburn 297: my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid,
298: $symb,$error) = &Apache::lonmsg::unpackmsgid($msgid);
299: my $coursetext;
300: if ($fromcid ne '') {
301: $coursetext = "\n".&mt('Course').': ';
302: if ($env{'course.'.$fromcid.'.description'} ne '') {
303: $coursetext .= $env{'course.'.$fromcid.'.description'};
304: } else {
305: my %coursehash = &Apache::lonnet::coursedescription($fromcid,);
306: if ($coursehash{'description'} ne '') {
307: $coursetext .= $coursehash{'description'};
308: }
309: }
310: $coursetext .= "\n\n";
311: }
312: my $body = $coursetext.
1.195 raeburn 313: &mt('You received a'.$critical.' message from [_1] in LON-CAPA.',$sender).' '.&mt('The subject is
1.53 www 314:
1.195 raeburn 315: [_1]
1.53 www 316:
1.195 raeburn 317: ',$subj)."\n".
1.194 raeburn 318: '=== '.&mt('Excerpt')." ============================================================
1.131 www 319: $text
320: ========================================================================
321:
1.195 raeburn 322: ".&mt('Use
1.53 www 323:
1.195 raeburn 324: [_1]
1.53 www 325:
1.195 raeburn 326: to access the full message.',$url);
1.53 www 327: &sendemail($to,'New'.$critical.' message from '.$sender,$body);
328: }
1.40 www 329: # ============================================================= Check for email
330:
331: sub newmail {
1.140 albertel 332: if ((time-$env{'user.mailcheck.time'})>300) {
1.40 www 333: my %what=&Apache::lonnet::get('email_status',['recnewemail']);
334: &Apache::lonnet::appenv('user.mailcheck.time'=>time);
335: if ($what{'recnewemail'}>0) { return 1; }
336: }
337: return 0;
338: }
339:
1.1 www 340: # =============================== Automated message to the author of a resource
341:
1.58 bowersj2 342: =pod
343:
344: =item * B<author_res_msg($filename, $message)>: Sends message $message to the owner
345: of the resource with the URI $filename.
346:
347: =cut
348:
1.1 www 349: sub author_res_msg {
350: my ($filename,$message)=@_;
1.2 www 351: unless ($message) { return 'empty'; }
1.1 www 352: $filename=&Apache::lonnet::declutter($filename);
1.72 www 353: my ($domain,$author,@dummy)=split(/\//,$filename);
1.1 www 354: my $homeserver=&Apache::lonnet::homeserver($author,$domain);
355: if ($homeserver ne 'no_host') {
356: my $id=unpack("%32C*",$message);
1.181 albertel 357: $message .= " <p>This error occurred on machine ".
358: $Apache::lonnet::perlvar{'lonHostID'}."</p>";
1.2 www 359: my $msgid;
1.72 www 360: ($msgid,$message)=&packagemsg($filename,$message);
1.3 www 361: return &Apache::lonnet::reply('put:'.$domain.':'.$author.
1.72 www 362: ':nohist_res_msgs:'.
1.184 www 363: &escape($filename.'_'.$id).'='.
364: &escape($message),$homeserver);
1.1 www 365: }
1.2 www 366: return 'no_host';
1.73 www 367: }
368:
369: # =========================================== Retrieve author resource messages
370:
371: sub retrieve_author_res_msg {
1.75 www 372: my $url=shift;
1.73 www 373: $url=&Apache::lonnet::declutter($url);
1.187 albertel 374: my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.76 www 375: my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$domain,$author);
1.73 www 376: my $msgs='';
377: foreach (keys %errormsgs) {
1.80 www 378: if ($_=~/^\Q$url\E\_\d+$/) {
1.73 www 379: my %content=&unpackagemsg($errormsgs{$_});
1.74 www 380: $msgs.='<p><img src="/adm/lonMisc/bomb.gif" /><b>'.
381: $content{'time'}.'</b>: '.$content{'message'}.
382: '<br /></p>';
1.73 www 383: }
384: }
385: return $msgs;
386: }
387:
388:
389: # =============================== Delete all author messages related to one URL
390:
391: sub del_url_author_res_msg {
1.75 www 392: my $url=shift;
1.73 www 393: $url=&Apache::lonnet::declutter($url);
1.187 albertel 394: my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.77 www 395: my @delmsgs=();
396: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
397: if ($_=~/^\Q$url\E\_\d+$/) {
398: push (@delmsgs,$_);
399: }
400: }
401: return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
1.73 www 402: }
1.152 www 403: # =================================== Clear out all author messages in URL path
1.73 www 404:
1.152 www 405: sub clear_author_res_msg {
406: my $url=shift;
407: $url=&Apache::lonnet::declutter($url);
1.187 albertel 408: my ($domain,$author)=($url=~/^($match_domain)\/($match_username)\//);
1.152 www 409: my @delmsgs=();
410: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
411: if ($_=~/^\Q$url\E/) {
412: push (@delmsgs,$_);
413: }
414: }
415: return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author);
416: }
1.73 www 417: # ================= Return hash with URLs for which there is a resource message
418:
419: sub all_url_author_res_msg {
420: my ($author,$domain)=@_;
1.75 www 421: my %returnhash=();
1.76 www 422: foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) {
1.75 www 423: $_=~/^(.+)\_\d+/;
424: $returnhash{$1}=1;
425: }
426: return %returnhash;
1.1 www 427: }
428:
1.185 albertel 429: # ====================================== Add a comment to the User Notes screen
430:
431: sub store_instructor_comment {
432: my ($msg,$uname,$udom) = @_;
433: my $cid = $env{'request.course.id'};
434: my $cnum = $env{'course.'.$cid.'.num'};
435: my $cdom = $env{'course.'.$cid.'.domain'};
436: my $subject= &mt('Record').' ['.$uname.':'.$udom.']';
437: my $result = &user_normal_msg_raw($cnum,$cdom,$subject,$msg);
1.201 ! raeburn 438: if ($result eq 'ok' || $result eq 'con_delayed') {
! 439:
! 440: }
1.185 albertel 441: return $result;
442: }
443:
1.1 www 444: # ================================================== Critical message to a user
445:
1.38 www 446: sub user_crit_msg_raw {
1.201 ! raeburn 447: my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage,
! 448: $nosentstore)=@_;
1.2 www 449: # Check if allowed missing
1.190 raeburn 450: my ($status,$packed_message);
1.2 www 451: my $msgid='undefined';
452: unless (($message)&&($user)&&($domain)) { $status='empty'; };
1.131 www 453: my $text=$message;
1.2 www 454: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
455: if ($homeserver ne 'no_host') {
1.190 raeburn 456: ($msgid,$packed_message)=&packagemsg($subject,$message);
457: if ($sendback) { $packed_message.='<sendback>true</sendback>'; }
1.4 www 458: $status=&Apache::lonnet::critical(
459: 'put:'.$domain.':'.$user.':critical:'.
1.184 www 460: &escape($msgid).'='.
1.190 raeburn 461: &escape($packed_message),$homeserver);
1.159 raeburn 462: if (defined($sentmessage)) {
1.190 raeburn 463: $$sentmessage = $packed_message;
1.159 raeburn 464: }
1.201 ! raeburn 465: if (!$nosentstore) {
1.193 raeburn 466: (undef,my $packed_message_no_citation) =
1.190 raeburn 467: &packagemsg($subject,$message,undef,undef,undef,$user,$domain,
468: $msgid);
1.193 raeburn 469: if ($status eq 'ok' || $status eq 'con_delayed') {
470: &store_sent_mail($msgid,$packed_message_no_citation);
471: }
472: }
1.2 www 473: } else {
474: $status='no_host';
475: }
1.190 raeburn 476:
1.53 www 477: # Notifications
1.186 www 478: my %userenv = &Apache::loncommon::getemails($user,$domain);
1.53 www 479: if ($userenv{'critnotification'}) {
1.131 www 480: &sendnotification($userenv{'critnotification'},$user,$domain,$subject,1,
1.194 raeburn 481: $text,$msgid);
1.53 www 482: }
1.132 www 483: if ($toperm && $userenv{'permanentemail'}) {
484: &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,1,
1.194 raeburn 485: $text,$msgid);
1.132 www 486: }
1.53 www 487: # Log this
1.2 www 488: &Apache::lonnet::logthis(
1.4 www 489: 'Sending critical email '.$msgid.
1.2 www 490: ', log status: '.
1.140 albertel 491: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
492: $env{'user.home'},
1.2 www 493: 'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
1.4 www 494: .$status));
1.2 www 495: return $status;
496: }
497:
1.38 www 498: # New routine that respects "forward" and calls old routine
499:
1.58 bowersj2 500: =pod
501:
1.201 ! raeburn 502: =item * B<user_crit_msg($user, $domain, $subject, $message, $sendback, $nosentstore)>:
! 503: Sends a critical message $message to the $user at $domain. If $sendback
! 504: is true, a receipt will be sent to the current user when $user receives
! 505: the message.
1.58 bowersj2 506:
1.183 albertel 507: Additionally it will check if the user has a Forwarding address
508: set, and send the message to that address instead
509:
510: returns
511: - in array context a list of results for each message that was sent
512: - in scalar context a space seperated list of results for each
513: message sent
514:
1.58 bowersj2 515: =cut
516:
1.38 www 517: sub user_crit_msg {
1.201 ! raeburn 518: my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage,
! 519: $nosentstore)=@_;
1.183 albertel 520: my @status;
1.38 www 521: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
522: $domain,$user);
523: my $msgforward=$userenv{'msgforward'};
524: if ($msgforward) {
1.183 albertel 525: foreach my $addr (split(/\,/,$msgforward)) {
526: my ($forwuser,$forwdomain)=split(/\:/,$addr);
527: push(@status,
528: &user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
1.201 ! raeburn 529: $sendback,$toperm,$sentmessage,$nosentstore));
1.38 www 530: }
531: } else {
1.183 albertel 532: push(@status,
533: &user_crit_msg_raw($user,$domain,$subject,$message,$sendback,
1.201 ! raeburn 534: $toperm,$sentmessage,$nosentstore));
1.38 www 535: }
1.183 albertel 536: if (wantarray) {
537: return @status;
538: }
539: return join(' ',@status);
1.38 www 540: }
541:
1.2 www 542: # =================================================== Critical message received
543:
544: sub user_crit_received {
1.12 www 545: my $msgid=shift;
546: my %message=&Apache::lonnet::get('critical',[$msgid]);
1.52 www 547: my %contents=&unpackagemsg($message{$msgid},1);
1.24 www 548: my $status='rec: '.($contents{'sendback'}?
1.5 www 549: &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
1.140 albertel 550: &mt('Receipt').': '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.', '.$contents{'subject'},
551: &mt('User').' '.$env{'user.name'}.' '.&mt('at').' '.$env{'user.domain'}.
1.42 www 552: ' acknowledged receipt of message'."\n".' "'.
1.67 www 553: $contents{'subject'}.'"'."\n".&mt('dated').' '.
1.42 www 554: $contents{'time'}.".\n"
555: ):'no msg req');
1.5 www 556: $status.=' trans: '.
1.12 www 557: &Apache::lonnet::put(
558: 'nohist_email',{$contents{'msgid'} => $message{$msgid}});
1.5 www 559: $status.=' del: '.
1.9 albertel 560: &Apache::lonnet::del('critical',[$contents{'msgid'}]);
1.140 albertel 561: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
562: $env{'user.home'},'Received critical message '.
1.5 www 563: $contents{'msgid'}.
564: ', '.$status);
1.12 www 565: return $status;
1.2 www 566: }
567:
568: # ======================================================== Normal communication
569:
1.38 www 570: sub user_normal_msg_raw {
1.132 www 571: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.191 raeburn 572: $toperm,$currid,$newid,$sentmessage,$crsmsgid,$symb,$restitle,
1.201 ! raeburn 573: $error,$nosentstore)=@_;
1.2 www 574: # Check if allowed missing
1.173 albertel 575: my ($status,$packed_message);
1.2 www 576: my $msgid='undefined';
1.131 www 577: my $text=$message;
1.2 www 578: unless (($message)&&($user)&&($domain)) { $status='empty'; };
579: my $homeserver=&Apache::lonnet::homeserver($user,$domain);
580: if ($homeserver ne 'no_host') {
1.173 albertel 581: ($msgid,$packed_message)=
582: &packagemsg($subject,$message,$citation,$baseurl,
1.174 raeburn 583: $attachmenturl,$user,$domain,$currid,
1.191 raeburn 584: undef,$crsmsgid,$symb,$error);
1.174 raeburn 585:
1.108 www 586: # Store in user folder
1.4 www 587: $status=&Apache::lonnet::critical(
588: 'put:'.$domain.':'.$user.':nohist_email:'.
1.184 www 589: &escape($msgid).'='.
590: &escape($packed_message),$homeserver);
1.108 www 591: # Save new message received time
1.40 www 592: &Apache::lonnet::put
593: ('email_status',{'recnewemail'=>time},$domain,$user);
1.201 ! raeburn 594: # Into sent-mail folder if sent mail storage required
! 595: if (!$nosentstore) {
1.190 raeburn 596: (undef,my $packed_message_no_citation) =
597: &packagemsg($subject,$message,undef,$baseurl,$attachmenturl,
1.191 raeburn 598: $user,$domain,$currid,undef,$crsmsgid,$symb,$error);
1.193 raeburn 599: if ($status eq 'ok' || $status eq 'con_delayed') {
600: &store_sent_mail($msgid,$packed_message_no_citation);
601: }
1.156 raeburn 602: }
1.201 ! raeburn 603: if (ref($newid) eq 'SCALAR') {
1.196 www 604: $$newid = $msgid;
605: }
1.201 ! raeburn 606: if (ref($sentmessage) eq 'SCALAR') {
1.196 www 607: $$sentmessage = $packed_message;
608: }
609: # Notifications
610: my %userenv = &Apache::lonnet::get('environment',['notification',
611: 'permanentemail'],
612: $domain,$user);
613: if ($userenv{'notification'}) {
614: &sendnotification($userenv{'notification'},$user,$domain,$subject,0,
615: $text,$msgid);
616: }
617: if ($toperm && $userenv{'permanentemail'}) {
618: &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,0,
619: $text,$msgid);
620: }
621: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
622: $env{'user.home'},
623: 'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
624: } else {
1.2 www 625: $status='no_host';
1.196 www 626: }
1.2 www 627: return $status;
628: }
1.38 www 629:
630: # New routine that respects "forward" and calls old routine
631:
1.58 bowersj2 632: =pod
633:
1.191 raeburn 634: =item * B<user_normal_msg($user, $domain, $subject, $message, $citation,
1.201 ! raeburn 635: $baseurl, $attachmenturl, $toperm, $sentmessage, $symb, $restitle,
! 636: $error,$nosentstore)>:
1.191 raeburn 637: Sends a message to the $user at $domain, with subject $subject and message $message.
1.58 bowersj2 638:
1.199 raeburn 639: Additionally it will check if the user has a Forwarding address
640: set, and send the message to that address instead
641:
642: returns
643: - in array context a list of results for each message that was sent
644: - in scalar context a space seperated list of results for each
645: message sent
646:
1.58 bowersj2 647: =cut
648:
1.38 www 649: sub user_normal_msg {
1.132 www 650: my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
1.201 ! raeburn 651: $toperm,$sentmessage,$symb,$restitle,$error,$nosentstore)=@_;
1.199 raeburn 652: my @status;
1.38 www 653: my %userenv = &Apache::lonnet::get('environment',['msgforward'],
654: $domain,$user);
655: my $msgforward=$userenv{'msgforward'};
656: if ($msgforward) {
1.171 banghart 657: foreach (split(/\,/,$msgforward)) {
1.172 albertel 658: my ($forwuser,$forwdomain)=split(/\:/,$_);
1.199 raeburn 659: push(@status,
1.171 banghart 660: &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
1.172 albertel 661: $citation,$baseurl,$attachmenturl,$toperm,
1.201 ! raeburn 662: undef,undef,$sentmessage,undef,$symb,
! 663: $restitle,$error,$nosentstore));
1.171 banghart 664: }
1.191 raeburn 665: } else {
1.199 raeburn 666: push(@status,&user_normal_msg_raw($user,$domain,$subject,$message,
1.172 albertel 667: $citation,$baseurl,$attachmenturl,$toperm,
1.201 ! raeburn 668: undef,undef,$sentmessage,undef,$symb,
! 669: $restitle,$error,$nosentstore));
1.199 raeburn 670: }
671: if (wantarray) {
672: return @status;
1.38 www 673: }
1.199 raeburn 674: return join(' ',@status);
1.38 www 675: }
676:
1.201 ! raeburn 677: sub process_sent_mail {
! 678: my ($msgsubj,$subj_prefix,$numsent,$stamp,$msgname,$msgdom,$msgcount,$context,$pid,$savemsg,$recusers,$recudoms,$baseurl,$attachmenturl,$symb,$error,$senderuname,$senderdom,$senderhome) = @_;
! 679: my $sentsubj;
! 680: if ($numsent > 1) {
! 681: $sentsubj = $subj_prefix.' ('.$numsent.' sent) '.$msgsubj;
! 682: }
! 683: $sentsubj = &HTML::Entities::encode($sentsubj,'<>&"');
! 684: my $sentmsgid =
! 685: &buildmsgid($stamp,$sentsubj,$msgname,$msgdom,$msgcount,$context,$pid);
! 686: (undef,my $sentmessage) =
! 687: &packagemsg($msgsubj,$savemsg,undef,$baseurl,$attachmenturl,$recusers,
! 688: $recudoms,$sentmsgid,undef,undef,$symb,$error);
! 689: my $status = &store_sent_mail($sentmsgid,$sentmessage,$senderuname,
! 690: $senderdom,$senderhome);
! 691: return $status;
! 692: }
! 693:
1.156 raeburn 694: sub store_sent_mail {
1.201 ! raeburn 695: my ($msgid,$message,$senderuname,$senderdom,$senderhome) = @_;
! 696: if ($senderuname eq '') {
! 697: $senderuname = $env{'user.name'};
! 698: }
! 699: if ($senderdom eq '') {
! 700: $senderdom = $env{'user.domain'};
! 701: }
! 702: if ($senderhome eq '') {
! 703: $senderhome = $env{'user.home'};
! 704: }
1.171 banghart 705: my $status =' '.&Apache::lonnet::critical(
1.201 ! raeburn 706: 'put:'.$senderdom.':'.$senderuname.':nohist_email_sent:'.
! 707: &escape($msgid).'='.&escape($message),$senderhome);
1.156 raeburn 708: return $status;
709: }
1.2 www 710:
1.106 www 711: # =============================================================== Folder suffix
712:
713: sub foldersuffix {
714: my $folder=shift;
715: unless ($folder) { return ''; }
1.189 raeburn 716: my $suffix;
717: my %folderhash = &get_user_folders($folder);
718: if (ref($folderhash{$folder}) eq 'HASH') {
719: $suffix = '_'.&escape($folderhash{$folder}{'id'});
720: } else {
721: $suffix = '_'.&escape($folder);
722: }
723: return $suffix;
724: }
725:
726: # ========================================================= User-defined folders
727:
728: sub get_user_folders {
729: my ($folder) = @_;
730: my %userfolders =
731: &Apache::lonnet::dump('email_folders',undef,undef,$folder);
732: my $lock = "\0".'lock_counter'; # locks db while counter incremented
733: my $counter = "\0".'idcount'; # used in suffix for email db files
734: if (defined($userfolders{$lock})) {
735: delete($userfolders{$lock});
736: }
737: if (defined($userfolders{$counter})) {
738: delete($userfolders{$counter});
739: }
740: return %userfolders;
1.106 www 741: }
742:
1.197 albertel 743: sub secapply {
744: my $rec=shift;
745: my $defaultflag=shift;
746: $rec=~s/\s+//g;
747: $rec=~s/\@/\:/g;
748: my ($adr,$sections_or_groups)=($rec=~/^([^\(]+)\(([^\)]+)\)/);
749: if ($sections_or_groups) {
750: foreach my $item (split(/\;/,$sections_or_groups)) {
751: if (($item eq $env{'request.course.sec'}) ||
752: ($defaultflag && ($item eq '*'))) {
753: return $adr;
754: } elsif ($env{'request.course.groups'}) {
755: my @usersgroups = split(/:/,$env{'request.course.groups'});
756: if (grep(/^\Q$item\E$/,@usersgroups)) {
757: return $adr;
758: }
759: }
760: }
761: } else {
762: return $rec;
763: }
764: return '';
765: }
766:
767: =pod
768:
1.199 raeburn 769: =item * B<decide_receiver($feedurl,$author,$question,$course,$policy,$defaultflag)>:
1.197 albertel 770:
771: Arguments
772: $feedurl - /res/ url of resource (only need if $author is true)
773: $author,$question,$course,$policy - all true/false parameters
774: if true will attempt to find the addresses of user that should receive
775: this type of feedback (author - feedback to author of resource $feedurl,
776: $question 'Resource Content Questions', $course 'Course Content Question',
777: $policy 'Course Policy')
778: (Additionally it also checks $env for whether the corresponding form.<name>
779: element exists, for ease of use in a html response context)
780:
781: $defaultflag - (internal should be left blank) if true gather addresses
782: that aren't for a section even if I have a section
783: (used for reccursion internally, first we look for
784: addresses for our specific section then we recurse
785: and look for non section addresses)
786:
787: Returns
788: $typestyle - string of html text, describing what addresses were found
789: %to - a hash, which keys are addresses of users to send messages to
790: the keys will look like name:domain
791:
792: =cut
793:
794: sub decide_receiver {
795: my ($feedurl,$author,$question,$course,$policy,$defaultflag) = @_;
796: &Apache::lonenc::check_decrypt(\$feedurl);
797: my $typestyle='';
798: my %to=();
799: if ($env{'form.discuss'} eq 'author' ||$author) {
800: $typestyle.='Submitting as Author Feedback<br />';
801: $feedurl=~ m{^/res/($LONCAPA::domain_re)/($LONCAPA::username_re)/};
802: $to{$2.':'.$1}=1;
803: }
804: my $cid = $env{'request.course.id'};
805: if ($env{'form.discuss'} eq 'question' ||$question) {
806: $typestyle.=&mt('Submitting as Question').'<br />';
807: foreach my $item (split(/\,/,$env{'course.'.$cid.'.question.email'})) {
808: my $rec=&secapply($item,$defaultflag);
809: if ($rec) { $to{$rec}=1; }
810: }
811: }
812: if ($env{'form.discuss'} eq 'course' ||$course) {
813: $typestyle.=&mt('Submitting as Comment').'<br />';
814: foreach my $item (split(/\,/,$env{'course.'.$cid.'.comment.email'})) {
815: my $rec=&secapply($item,$defaultflag);
816: if ($rec) { $to{$rec}=1; }
817: }
818: }
819: if ($env{'form.discuss'} eq 'policy' ||$policy) {
820: $typestyle.=&mt('Submitting as Policy Feedback').'<br />';
821: foreach my $item (split(/\,/,$env{'course.'.$cid.'.policy.email'})) {
822: my $rec=&secapply($item,$defaultflag);
823: if ($rec) { $to{$rec}=1; }
824: }
825: }
826: if ((scalar(%to) eq '0') && (!$defaultflag)) {
827: ($typestyle,%to)=
828: &decide_receiver($feedurl,$author,$question,$course,$policy,1);
829: }
830: return ($typestyle,%to);
831: }
832:
1.199 raeburn 833: =pod
834:
835: =back
836:
837: =cut
838:
1.180 albertel 839: 1;
1.1 www 840: __END__
841:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>