# message-id-or-email.pl
# Copyright (c) 1999 Florian Weimer <fw@deneb.enyo.de>.
#
# See http://piology.org/perl/id-or-mail.pl.html for another solution.

use strict;

sub rate_it () {
  # returns M for (relatively) certain Message-ID, m for Message-ID,
  # ? for unknown, X for invalid, A for certain address, a for address.

  return 'X' if (/\@.*\@/ || / /);

  my $score = 0.0;

  /^(.*)\@/;
  my $local_part = $1;
  my $local_part_len = length $local_part;

  # Certain special cases.
  return 'A' if /^0[0-9]+-[0-9]{4,4}\@t-online\.de$/;
  return 'A' if /^[0-9]+\.[0-9]+\@compuserve/;

  $score -= 10.0 if /.+\$.+\@/;
  $score -= 10.0 if /\#/;
  $score -= 10.0 if /\*/;
  $score -=  5.0 if /\+[^+]*\+.*\@/; # two plus signs
  $score -=  5.0 if /\@news/i;
  $score -=  5.0 if /\@.*dialup/i;
  $score -=  1.0 if /^[^a-z]+\@/;

  $score -=  5.0 if /\.[0-9]{2,}.*\@/;
  $score -=  5.0 if /([a-z].*[A-Z].*){2,}\@/;
  $score -=  3.0 if /[A-Z][A-Z][a-z][a-z].*\@/;
  $score -=  5.0 if /\..{1,3}\@/;

  $score -=  2.0 if /^[0-9]/;
  $score -=  1.0 if /^[0-9][0-9]/;
  $score -=  3.0 if /^[0-9][0-9a-fA-F]{2,2}/;
  $score -=  5.0 if /^[0-9][0-9a-fA-F]{3,3}/;
  $score -=  3.0 if /[0-9]{5,}.*\@/;
  $score -=  3.0 if /[0-9]{8,}.*\@/;
  $score -=  3.0 if /[0-9]{12,}.*\@/;

  $score -= 20.0 if /\.fsf\@/;  # Gnus
  $score -= 20.0 if /^slrn/;
  $score -= 20.0 if /^Pine/;
  $score -= 20.0 if /_-_/;      # Subject change in thread

  $score += 5.0 if $local_part_len <= 7;
  $score += 10.0 if /^[^0-9]+\@/;
  $score +=  3.0 if /^[^0-9]+[0-9]{1,3}\@/; # digits only at end of local part
  $score +=  3.0 if /\@stud/;
  $score += 25.0 if /-dated-[0-9]{8,}.*\@/; # TDMA

  $score +=  2.0 if /[a-z][a-z][._-][A-Z][a-z].*\@/;

  $score +=  0.5 if /^[A-Z][a-z]/;
  $score +=  0.5 if /^[A-Z][a-z][a-z]/;
  $score +=  1.5 if /^[A-Z][a-z]{3,3}/;
  $score +=  2.0 if /^[A-Z][a-z]{4,4}/;

  if ($local_part_len >= 12) {
    if (/[0-9][^0-9]+[0-9].*\@/) {
      # Long local part should contain realname if e-mail address,
      # too many digits: message-id.
      $score -= 5.0 + 0.1 * $local_part_len;
    } elsif (/[^aeiouy]{4,}.*\@/) {
      # Too few vowels
      $score -= 5.0;
    } else {
      $score += 5.0;
    }
  }


  return 'M' if $score < -10.0;
  return 'm' if $score <  -2.0;
  return 'A' if $score >  10.0;
  return 'a' if $score >   1.0;
  
  return '?';
}

my $res;
while (<>) {
  chomp;
  $res = rate_it;
  print "$res $_\n";
}
