--- loncom/interface/lonmsg.pm	2009/01/04 16:20:27	1.220
+++ loncom/interface/lonmsg.pm	2010/11/10 14:44:50	1.230
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Routines for messaging
 #
-# $Id: lonmsg.pm,v 1.220 2009/01/04 16:20:27 raeburn Exp $
+# $Id: lonmsg.pm,v 1.230 2010/11/10 14:44:50 bisitz Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -194,8 +194,6 @@ Returns
   %to - a hash, which keys are addresses of users to send messages to
         the keys will look like   name:domain
 
-=item user_lang()
-
 =back
 
 =cut
@@ -205,6 +203,8 @@ use Apache::lonnet;
 use HTML::TokeParser();
 use Apache::lonlocal;
 use Mail::Send;
+use HTML::Entities;
+use Encode;
 use LONCAPA qw(:DEFAULT :match);
 
 {
@@ -455,6 +455,7 @@ sub sendemail {
     "*** ".&mt_user($user_lh,'This is an automatic e-mail generated by the LON-CAPA system.')."\n".
     "*** ".($senderaddress?&mt_user($user_lh,'You can reply to this e-mail'):&mt_user($user_lh,'Please do not reply to this address.')."\n*** ".
     &mt_user($user_lh,'A reply will not be received by the recipient!'))."\n\n".$body;
+    
     my $msg = new Mail::Send;
     $msg->to($to);
     $msg->subject('[LON-CAPA] '.$subject);
@@ -464,6 +465,7 @@ sub sendemail {
     if ($senderaddress) {
         $msg->add('From',$senderaddress);
     }
+    $msg->add('Content-type','text/plain; charset=UTF-8');
     if (my $fh = $msg->open()) {
 	print $fh $body;
 	$fh->close;
@@ -486,11 +488,12 @@ sub sendnotification {
     my $protocol = $Apache::lonnet::protocol{$homeserver};
     $protocol = 'http' if ($protocol ne 'https');
     my $url = $protocol.'://'.&Apache::lonnet::hostname($homeserver).
-              '/adm/email?username='.$touname.'&domain='.$toudom;
+              '/adm/email?username='.$touname.'&domain='.$toudom.
+              '&display='.&escape($msgid);
     my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid,
         $symb,$error) = &Apache::lonmsg::unpackmsgid($msgid);
     my ($coursetext,$body,$bodybegin,$bodysubj,$bodyend);
-    my $user_lh = &user_lang($touname,$toudom,$fromcid);
+    my $user_lh = &Apache::loncommon::user_lang($touname,$toudom,$fromcid);
     if ($fromcid ne '') {
         $coursetext = "\n".&mt_user($user_lh,'Course').': ';
         if ($env{'course.'.$fromcid.'.description'} ne '') {
@@ -539,13 +542,16 @@ to access the full message.',$url);
     }
     if ($userenv{'notifywithhtml'} ne '') {
         my @htmlexcerpt = split(/,/,$userenv{'notifywithhtml'});
+        my $htmlfree = &make_htmlfree($text);
         foreach my $addr (@recipients) {
             if ($blocked) {
                 $body = $bodybegin."\n".$blocktext."\n".$bodyend;
             } else {
-                my $sendtext = $text;
+                my $sendtext;
                 if (!grep/^\Q$addr\E/,@htmlexcerpt) {
-                    $sendtext =~ s/\<\/*[^\>]+\>//gs;
+                    $sendtext = $htmlfree;
+                } else {
+                    $sendtext = $text;
                 }
                 $body = $bodybegin.$bodysubj.$sendtext.$bodyend;
             }
@@ -555,18 +561,32 @@ to access the full message.',$url);
         if ($blocked) {
             $body = $bodybegin."\n".$blocktext."\n".$bodyend;
         } else {
-            $text =~ s/\<\/*[^\>]+\>//gs;
-            $body = $bodybegin.$bodysubj.$text.$bodyend;
+            my $htmlfree = &make_htmlfree($text);
+            $body = $bodybegin.$bodysubj.$htmlfree.$bodyend;
         }
         &sendemail($to,$subject,$body,$touname,$toudom,$user_lh);
     }
 }
 
+sub make_htmlfree {
+    my ($text) = @_;
+    $text =~ s/\<\/*[^\>]+\>//gs;
+    $text = &HTML::Entities::decode($text);
+    $text = &Encode::encode('utf8',$text);
+    return $text;
+}
+
+sub mynewmail{
+	&newmail();
+	return $env{'user.mailcheck.lastnewmassagetime'} > $env{'user.mailcheck.lastvisit'};
+}
+
 
 sub newmail {
     if ((time-$env{'user.mailcheck.time'})>300) {
         my %what=&Apache::lonnet::get('email_status',['recnewemail']);
         &Apache::lonnet::appenv({'user.mailcheck.time'=>time});
+		&Apache::lonnet::appenv({'user.mailcheck.lastnewmassagetime'=> $what{'recnewemail'}});
         if ($what{'recnewemail'}>0) { return 1; }
     }
     return 0;
@@ -966,7 +986,9 @@ sub secapply {
     my $rec=shift;
     my $defaultflag=shift;
     $rec=~s/\s+//g;
-    $rec=~s/\@/\:/g;
+    unless ($rec =~ /\:/) {
+        $rec=~s/\@/\:/g;
+    }
     my ($adr,$sections_or_groups)=($rec=~/^([^\(]+)\(([^\)]+)\)/);
     if ($sections_or_groups) {
 	foreach my $item (split(/\;/,$sections_or_groups)) {
@@ -1025,28 +1047,6 @@ sub decide_receiver {
     return ($typestyle,%to);
 }
 
-sub user_lang {
-    my ($touname,$toudom,$fromcid) = @_;
-    my @userlangs;
-    if (($fromcid ne '') && ($env{'course.'.$fromcid.'.languages'} ne '')) {
-        @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
-                    $env{'course.'.$fromcid.'.languages'}));
-    } else {
-        my %langhash = &Apache::loncommon::getlangs($toudom,$touname);
-        if ($langhash{'languages'} ne '') {
-            @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});  
-        } else {
-            my %domdefs = &Apache::lonnet::get_domain_defaults($toudom);
-            if ($domdefs{'lang_def'} ne '') {
-                @userlangs = ($domdefs{'lang_def'});
-            }
-        }
-    }
-    my @languages=&Apache::lonlocal::get_genlanguages(@userlangs);
-    my $user_lh = Apache::localize->get_handle(@languages);
-    return $user_lh;
-}
-
 1;
 __END__