--- Message.pm.orig	Thu Oct  2 14:08:19 2003
+++ Message.pm	Thu Oct  2 16:46:46 2003
@@ -42,6 +42,7 @@
 use MIME::WordDecoder;
 use POSIX qw(setsid);
 use HTML::TokeParser;
+use Digest::MD5;
 
 # Install an extra MIME decoder for badly-header uue messages.
 install MIME::Decoder::UU 'uuencode';
@@ -145,6 +146,17 @@
   $this->{id} = $id;
   @{$this->{archiveplaces}} = (); # Hope this syntax is right!
 
+  #BBY: put this at the very beginning so we can kinda measure
+  #the time it takes from message object start of creation
+  my($date, $tag);
+  # This is the yyyymmddhhmmss timestamp part
+  my($sec,$min,$hour,$day,$month,$year);
+  ($sec,$min,$hour,$day,$month,$year) = (localtime)[0,1,2,3,4,5];
+  $month++;
+  $year += 1900;
+  $date = sprintf("%04d%02d%02d%02d%02d%02d",
+                  $year, $month, $day, $hour, $min, $sec);
+
   # Create somewhere to store the message
   $this->{store} = new MailScanner::SMDiskStore($id, $queuedirname);
 
@@ -161,11 +173,21 @@
   ($user, $domain) = address2userdomain($this->{from});
   $this->{fromuser} = $user;
   $this->{fromdomain} = $domain;
+  my $allto ;
   foreach $addr (@{$this->{to}}) {
     ($user, $domain) = address2userdomain($addr);
     push @{$this->{touser}}, $user;
     push @{$this->{todomain}}, $domain;
+    $allto .= ':' . $addr;
   }
+  $allto =~ s/^:/=/;
+
+
+  # BBY: Would this be sufficiently uniq id?
+  $tag = $global::MS->{proctag} . $date . '=' . $this->{clientip} . '=' .
+         $id . '=' . int(rand(1_000_000)) . '=' . $this->{from} .  $allto;
+  $this->{msid} = $tag;
+  $this->{md5msid} = Digest::MD5::md5_hex($tag);
 
   # Reset the infection counters to 0
   $this->{virusinfected} = 0;
@@ -658,6 +680,15 @@
     $global::MS->{mta}->ReplaceHeader($this, $infoheader, $infovalue);
   }
 
+  # Add the Id X- header
+  my $idheader = MailScanner::Config::Value('idheader', $this);
+  if ($idheader && MailScanner::Config::Value('includeidheader', $this)) {
+    my $idvalue = $this->{md5msid};
+    $idvalue .= "\n\t" . $this->{msid} 
+      if MailScanner::Config::Value('fullidheader', $this);
+    $global::MS->{mta}->ReplaceHeader($this, $idheader, $idvalue);
+  }
+
   # Add the Unscanned X- header
   if (MailScanner::Config::Value('signunscannedmessages', $this)) {
     $global::MS->{mta}->AddMultipleHeader($this, 'mailheader',
@@ -1749,6 +1780,15 @@
     $global::MS->{mta}->ReplaceHeader($this, $infoheader, $infovalue);
   }
 
+  # Add the Id X- header
+  my $idheader = MailScanner::Config::Value('idheader', $this);
+  if ($idheader && MailScanner::Config::Value('includeidheader', $this)) {
+    my $idvalue = $this->{md5msid};
+    $idvalue .= "\n\t" . $this->{msid}
+      if MailScanner::Config::Value('fullidheader', $this);
+    $global::MS->{mta}->ReplaceHeader($this, $idheader, $idvalue);
+  }
+
   $global::MS->{mta}->AddMultipleHeader($this, 'mailheader',
                MailScanner::Config::Value($headervalue, $this), ', ');
   # Delete all content length headers anyway. They are unsafe.
@@ -1874,6 +1914,15 @@
     $global::MS->{mta}->ReplaceHeader($this, $infoheader, $infovalue);
   }
 
+  # Add the Id X- header
+  my $idheader = MailScanner::Config::Value('idheader', $this);
+  if ($idheader && MailScanner::Config::Value('includeidheader', $this)) {
+    my $idvalue = $this->{md5msid};
+    $idvalue .= "\n\t" . $this->{msid}
+      if MailScanner::Config::Value('fullidheader', $this);
+    $global::MS->{mta}->ReplaceHeader($this, $idheader, $idvalue);
+  }
+
   # Add the clean/dirty header
   #print STDERR "Adding clean/dirty header $headervalue\n";
   $global::MS->{mta}->AddMultipleHeader($this, 'mailheader',
