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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: #
                      3: # Routines for messaging
                      4: #
                      5: # (Routines to control the menu
                      6: #
                      7: # (TeX Conversion Module
                      8: #
                      9: # 05/29/00,05/30 Gerd Kortemeyer)
                     10: #
                     11: # 10/05 Gerd Kortemeyer)
                     12: #
1.6       www        13: # 10/19,10/20,10/30,
                     14: # 02/06/01 Gerd Kortemeyer
1.1       www        15: 
                     16: package Apache::lonmsg;
                     17: 
                     18: use strict;
                     19: use Apache::lonnet();
1.2       www        20: use vars qw($msgcount);
                     21: use HTML::TokeParser;
1.5       www        22: use Apache::Constants qw(:common);
1.1       www        23: 
                     24: # ===================================================================== Package
                     25: 
1.3       www        26: sub packagemsg {
1.7     ! www        27:     my ($subject,$message,$citation)=@_;
1.1       www        28:     $message=~s/\</\&lt\;/g;
                     29:     $message=~s/\>/\&gt\;/g;
1.7     ! www        30:     $citation=~s/\</\&lt\;/g;
        !            31:     $citation=~s/\>/\&gt\;/g;
1.1       www        32:     $subject=~s/\</\&lt\;/g;
                     33:     $subject=~s/\>/\&gt\;/g;
1.2       www        34:     my $now=time;
                     35:     $msgcount++;
1.6       www        36:     my $partsubj=$subject;
                     37:     $partsubj=&Apache::lonnet::escape($partsubj);
                     38:     $partsubj=substr($partsubj,0,50);
                     39:     my $msgid=&Apache::lonnet::escape(
                     40:            $now.':'.$partsubj.':'.$ENV{'user.name'}.':'.
                     41:            $ENV{'user.domain'}.':'.$msgcount.':'.$$);
1.2       www        42:     return $msgid,
                     43:            '<sendername>'.$ENV{'user.name'}.'</sendername>'.
1.1       www        44:            '<senderdomain>'.$ENV{'user.domain'}.'</senderdomain>'.
                     45:            '<subject>'.$subject.'</subject>'.
1.2       www        46: 	   '<time>'.localtime($now).'</time>'.
1.1       www        47: 	   '<servername>'.$ENV{'SERVER_NAME'}.'</servername>'.
                     48:            '<host>'.$ENV{'HTTP_HOST'}.'</host>'.
                     49: 	   '<client>'.$ENV{'REMOTE_ADDR'}.'</client>'.
                     50: 	   '<browsertype>'.$ENV{'browser.type'}.'</browsertype>'.
                     51: 	   '<browseros>'.$ENV{'browser.os'}.'</browseros>'.
                     52: 	   '<browserversion>'.$ENV{'browser.version'}.'</browserversion>'.
                     53:            '<browsermathml>'.$ENV{'browser.mathml'}.'</browsermathml>'.
                     54: 	   '<browserraw>'.$ENV{'HTTP_USER_AGENT'}.'</browserraw>'.
                     55: 	   '<courseid>'.$ENV{'request.course.id'}.'</courseid>'.
                     56: 	   '<role>'.$ENV{'request.role'}.'</role>'.
                     57: 	   '<resource>'.$ENV{'request.filename'}.'</resource>'.
1.2       www        58:            '<msgid>'.$msgid.'</msgid>'.
1.7     ! www        59: 	   '<message>'.$message.'</message>'.
        !            60: 	   '<citation>'.$citation.'</citation>';
1.1       www        61: }
                     62: 
1.2       www        63: # ================================================== Unpack message into a hash
                     64: 
1.3       www        65: sub unpackagemsg {
1.2       www        66:     my $message=shift;
                     67:     my %content=();
                     68:     my $parser=HTML::TokeParser->new(\$message);
                     69:     my $token;
                     70:     while ($token=$parser->get_token) {
                     71:        if ($token->[0] eq 'S') {
                     72: 	   my $entry=$token->[1];
                     73:            my $value=$parser->get_text('/'.$entry);
                     74:            $content{$entry}=$value;
                     75:        }
                     76:     }
                     77:     return %content;
                     78: }
                     79: 
1.6       www        80: # ======================================================= Get info out of msgid
                     81: 
                     82: sub unpackmsgid {
1.7     ! www        83:     my $msgid=&Apache::lonnet::unescape(shift);
1.6       www        84:     my ($sendtime,$shortsubj,$fromname,$fromdomain)=split(/\:/,
1.7     ! www        85:                           &Apache::lonnet::unescape($msgid));
1.6       www        86:     my %status=&Apache::lonnet::get('email_status',$msgid);
                     87:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
                     88:     unless ($status{$msgid}) { $status{$msgid}='new'; }
                     89:     return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid});
                     90: } 
                     91: 
1.1       www        92: # =============================== Automated message to the author of a resource
                     93: 
                     94: sub author_res_msg {
                     95:     my ($filename,$message)=@_;
1.2       www        96:     unless ($message) { return 'empty'; }
1.1       www        97:     $filename=&Apache::lonnet::declutter($filename);
                     98:     my ($domain,$author,@dummy)=split(/\//,$filename);
                     99:     my $homeserver=&Apache::lonnet::homeserver($author,$domain);
                    100:     if ($homeserver ne 'no_host') {
                    101:        my $id=unpack("%32C*",$message);
1.2       www       102:        my $msgid;
1.3       www       103:        ($msgid,$message)=&packagemsg($filename,$message);
                    104:        return &Apache::lonnet::reply('put:'.$domain.':'.$author.
                    105:          ':nohist_res_msgs:'.
                    106:           &Apache::lonnet::escape($filename.'_'.$id).'='.
                    107:           &Apache::lonnet::escape($message),$homeserver);
1.1       www       108:     }
1.2       www       109:     return 'no_host';
1.1       www       110: }
                    111: 
                    112: # ================================================== Critical message to a user
                    113: 
                    114: sub user_crit_msg {
                    115:     my ($user,$domain,$subject,$message)=@_;
1.2       www       116: # Check if allowed missing
                    117:     my $status='';
                    118:     my $msgid='undefined';
                    119:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
                    120:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    121:     if ($homeserver ne 'no_host') {
                    122:        my $msgid;
1.3       www       123:        ($msgid,$message)=&packagemsg($subject,$message);
1.4       www       124:        $status=&Apache::lonnet::critical(
                    125:            'put:'.$domain.':'.$user.':critical:'.
                    126:            &Apache::lonnet::escape($msgid).'='.
                    127:            &Apache::lonnet::escape($message),$homeserver);
1.2       www       128:     } else {
                    129:        $status='no_host';
                    130:     }
                    131:     &Apache::lonnet::logthis(
1.4       www       132:       'Sending critical email '.$msgid.
1.2       www       133:       ', log status: '.
                    134:       &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    135:                          $ENV{'user.home'},
                    136:       'Sending critical '.$msgid.' to '.$user.' at '.$domain.' with status: '
1.4       www       137:       .$status));
1.2       www       138:     return $status;
                    139: }
                    140: 
                    141: # =================================================== Critical message received
                    142: 
                    143: sub user_crit_received {
                    144:     my $message=shift;
1.4       www       145:     my %contents=&unpackagemsg($message);
1.5       www       146:     my $status='rec: '.
                    147:      &user_normal_msg($contents{'sendername'},$contents{'senderdomain'},
1.4       www       148:                      'Receipt: '.$ENV{'user.name'}.' at '.$ENV{'user.domain'},
                    149:                      'User '.$ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    150:                      ' acknowledged receipt of message "'.
                    151:                      $contents{'subject'}.'" dated '.$contents{'time'}.".\n\n"
                    152:                      .'Message ID: '.$contents{'msgid'});
1.5       www       153:     $status.=' trans: '.
                    154:      &Apache::lonnet::put('nohist_email',$contents{'msgid'} => $message);
                    155:     $status.=' del: '.
                    156:      &Apache::lonnet::del('critical',$contents{'msgid'});
                    157:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    158:                          $ENV{'user.home'},'Received critical message '.
                    159:                          $contents{'msgid'}.
                    160:                          ', '.$status);
1.2       www       161: }
                    162: 
                    163: # ======================================================== Normal communication
                    164: 
                    165: sub user_normal_msg {
1.7     ! www       166:     my ($user,$domain,$subject,$message,$citation)=@_;
1.2       www       167: # Check if allowed missing
                    168:     my $status='';
                    169:     my $msgid='undefined';
                    170:     unless (($message)&&($user)&&($domain)) { $status='empty'; };
                    171:     my $homeserver=&Apache::lonnet::homeserver($user,$domain);
                    172:     if ($homeserver ne 'no_host') {
                    173:        my $msgid;
1.7     ! www       174:        ($msgid,$message)=&packagemsg($subject,$message,$citation);
1.4       www       175:        $status=&Apache::lonnet::critical(
                    176:            'put:'.$domain.':'.$user.':nohist_email:'.
                    177:            &Apache::lonnet::escape($msgid).'='.
                    178:            &Apache::lonnet::escape($message),$homeserver);
1.2       www       179:     } else {
                    180:        $status='no_host';
                    181:     }
                    182:     &Apache::lonnet::log($ENV{'user.domain'},$ENV{'user.name'},
                    183:                          $ENV{'user.home'},
                    184:       'Sending '.$msgid.' to '.$user.' at '.$domain.' with status: '.$status);
                    185:     return $status;
                    186: }
                    187: 
1.7     ! www       188: # =============================================================== Status Change
        !           189: 
        !           190: sub statuschange {
        !           191:     my ($msgid,$newstatus)=@_;
        !           192:     my %status=&Apache::lonnet::get('email_status',$msgid);
        !           193:     if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
        !           194:     unless ($status{$msgid}) { $status{$msgid}='new'; }
        !           195:     unless (($status{$msgid} eq 'replied') || 
        !           196:             ($status{$msgid} eq 'forwarded')) {
        !           197: 	&Apache::lonnet::put('email_status',($msgid => $newstatus));
        !           198:     }
        !           199: }
1.5       www       200: # ===================================================================== Handler
                    201: 
                    202: sub handler {
                    203:     my $r=shift;
                    204: 
                    205: # ----------------------------------------------------------- Set document type
                    206: 
                    207:   $r->content_type('text/html');
                    208:   $r->send_http_header;
                    209: 
                    210:   return OK if $r->header_only;
                    211: 
1.6       www       212: # --------------------------- Get query string for limited number of parameters
                    213: 
                    214:     map {
                    215:        my ($name, $value) = split(/=/,$_);
                    216:        $value =~ tr/+/ /;
                    217:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                    218:        if (($name eq 'display') || ($name eq 'replyto') || 
1.7     ! www       219:            ($name eq 'forward') || ($name eq 'mark') ||
        !           220:            ($name eq 'sendreply')) {
1.6       www       221:            unless ($ENV{'form.'.$name}) {
                    222:               $ENV{'form.'.$name}=$value;
                    223: 	   }
                    224:        }
                    225:     } (split(/&/,$ENV{'QUERY_STRING'}));
                    226: 
1.5       www       227: # --------------------------------------------------------------- Render Output
                    228:   
                    229:   $r->print('<html><head><title>EMail and Messaging</title></head>');
1.7     ! www       230:   $r->print(
        !           231:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1.5       www       232:   $r->print('<h1>EMail</h1>');
1.6       www       233:   if ($ENV{'form.display'}) {
1.7     ! www       234:       my $msgid=$ENV{'form.display'};
        !           235:       &statuschange($msgid,'read');
        !           236:       my %message=&Apache::lonnet::get('nohist_email',$msgid);
        !           237:       my %content=&unpackagemsg($message{$msgid});
        !           238:       $r->print('<b>Subject:</b> '.$content{'subject'}.
        !           239:              '<br><b>From:</b> '.$content{'sendername'}.' at '.
        !           240:                                  $content{'senderdomain'}.
        !           241:              '<br><b>Time:</b> '.$content{'time'}.'<hr>Functions: '.
        !           242:              '<a href="/adm/email?replyto='.&Apache::lonnet::escape($msgid).
        !           243:              '"><b>Reply</b></a><hr><pre>'.
        !           244:              $content{'message'}.'</pre><hr>'.$content{'citation'});
1.6       www       245:   } elsif ($ENV{'form.replyto'}) {
1.7     ! www       246:       my $msgid=$ENV{'form.replyto'};
        !           247:       my %message=&Apache::lonnet::get('nohist_email',$msgid);
        !           248:       my %content=&unpackagemsg($message{$msgid});
        !           249:       my $quotemsg='> '.$content{'message'};
        !           250:       $quotemsg=~s/\r/\n/g;
        !           251:       $quotemsg=~s/\f/\n/g;
        !           252:       $quotemsg=~s/\n+/\n\> /g;
        !           253:       my $subject='Re: '.$content{'subject'};
        !           254:       $r->print(<<"ENDREPLY");
        !           255: <form action="/adm/email" method=post>
        !           256: <input type=hidden name=sendreply value="$msgid">
        !           257: Subject: <input type=text size=50 name=subject value="$subject"><p>
        !           258: <textarea name=message cols=60 rows=10>
        !           259: $quotemsg
        !           260: </textarea><p>
        !           261: <input type=submit value="Send Reply">
        !           262: </form>
        !           263: ENDREPLY
        !           264:   } elsif ($ENV{'form.sendreply'}) {
        !           265:       my $msgid=$ENV{'form.sendreply'};
        !           266:       my %message=&Apache::lonnet::get('nohist_email',$msgid);
        !           267:       my %content=&unpackagemsg($message{$msgid});
        !           268:       &statuschange($msgid,'replied');
        !           269:       $r->print('Sending: '.&user_normal_msg($content{'sendername'},
        !           270:                                  $content{'senderdomain'},
        !           271:                                  $ENV{'form.subject'},
        !           272:                                  $ENV{'form.message'}));
1.6       www       273:   } elsif ($ENV{'form.forward'}) {
                    274:   } elsif ($ENV{'form.mark'}) {
                    275:   } else {
                    276:     $r->print('<table border=2><tr><th>&nbsp</th><th>Date</th>'.
                    277:      '<th>Username</th><th>Domain</th><th>Subject</th><th>Status</th></tr>');
                    278:     map {
                    279:         my ($sendtime,$shortsubj,$fromname,$fromdomain,$status)=
                    280: 	    &Apache::lonmsg::unpackmsgid($_);
                    281:         if ($status eq 'new') {
                    282: 	    $r->print('<tr bgcolor="#FFBB77">');
                    283:         } elsif ($status eq 'read') {
                    284: 	    $r->print('<tr bgcolor="#BBBB77">');
                    285:         } elsif ($status eq 'replied') {
1.7     ! www       286: 	    $r->print('<tr bgcolor="#AAAA88">');
1.6       www       287: 	} else {
                    288: 	    $r->print('<tr bgcolor="#99BBBB">');
                    289:         }
                    290:         $r->print('<td><a href="/adm/email?display='.$_.
                    291:                   '">Open</a></td><td>'.localtime($sendtime).'</td><td>'.
                    292:                   $fromname.'</td><td>'.$fromdomain.'</td><td>'.
                    293: 		      &Apache::lonnet::unescape($shortsubj).'</td><td>'.
                    294:                       $status.'</td></tr>');
                    295:     } sort split(/\&/,&Apache::lonnet::reply('keys:'.
                    296: 					$ENV{'user.domain'}.':'.
                    297:                                         $ENV{'user.name'}.':nohist_email',
                    298:                                         $ENV{'user.home'}));
                    299:     $r->print('</table></body></html>');
                    300: 
                    301:   }
1.5       www       302:   $r->print('</body></html>');
                    303:   return OK;
                    304: 
                    305: }
1.2       www       306: # ================================================= Main program, reset counter
                    307: 
                    308: sub BEGIN {
                    309:     $msgcount=0;
1.1       www       310: }
                    311: 
                    312: 1;
                    313: __END__
                    314: 
                    315: 
                    316: 
                    317: 
                    318: 
                    319: 
                    320: 

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