#!/usr/bin/perl
#
# mf2murxrc:
# Convert mailfilterrc to murxrc
#
# Usage:
# $ chmod u+x mf2murxrc
# $ cat ~/.mailfilterrc | mf2murxrc > ~/.murxrc
#
# This is a perl-skript. Depending on your system you may have
# to adjust the first line.
#
# ---------------------
# Licensed under GPL v2
# ---------------------
#
# 10.03.2005
# Copyright Til Schubbe <t.schubbe@gmx.de>
#

# Hey guys'n gals, it's just a quick hack...

sub print_later {
  my ($val) = @_;
  $outstring .= $val;
}

$deprecated_keywords = ' REG_NEWLINE REG_TYPE MAXSIZE_SCORE SHOW_HEADERS ';
$account_no = 1;
$outstring = '';
$account_defs = '';

while (<>) {
  $l++;
  chomp;
  if (/^(#|\s*$)/) {		# skip comment or empty line
    print_later "$_\n";
  } elsif (/^\s/) {
    die "Illegal line (starts with whitespace) $l: '$_'\n";
  } elsif (my ($prelude, $keyword, $score, $equal, $regex) =
        $_ =~ m/^(([A-Z_]+)(\s+[+-]?[0-9]+)?\s*(=|<>)\s*)(.*)/
    ) {
    # quote the regex if mailfilterrc v0.5/0.6-files
    if ($regex =~ /^[^0-9]/ or $keyword eq 'USER') {
      $quoted_regex = $regex;
      # quote unquoted regex
      if ($quoted_regex !~ /^\"/) {$quoted_regex = '"' . $quoted_regex . '"';}
      # escape " inside the regex if not escaped already
      $quoted_regex =~ s/(?<=[^\\])"(?=.)/\\"/g;
      $_ =~ s/$regex$/$quoted_regex/;
      $regex = $quoted_regex;
    }
    if ($deprecated_keywords =~ / $keyword /) {
      print_later "#--DEPRECATED--# $_";
    } elsif ($keyword eq 'SERVER') {
      $account_defs .= "ACCOUNT \"account$account_no\"\n{\n";
      $account_defs .= "$_\n";
      $account_no++;
      next;
    } elsif ($keyword eq 'PORT') {
      $account_defs .= "$_\n}\n\n";
      next;
    } elsif ($keyword eq 'PASS') {
      $account_defs .= "PASSWORD$equal$regex\n";
      next;
    } elsif ($keyword eq 'DEL_DUPLICATES') {
      $account_defs .= "DELETE_DUPLICATES$equal$regex\n";
      next;
    } elsif ($keyword eq 'USER' or $keyword eq 'PROTOCOL') {
      $account_defs .= "$_\n";
      next;
    } elsif ($keyword eq 'NORMAL') {
      print_later "NORMALIZE_SUBJECT $equal $regex";
    } elsif ($keyword =~ /^(ALLOW|DENY)(?:_((?:NO)?CASE))?/) {
      if (defined $2) {$evtl_blank = "$2 "} else {$evtl_blank = ''}
      print_later "$1 $evtl_blank$equal $regex";
    } elsif ($keyword eq 'REG_CASE') {
      if ($regex eq '"yes"') {
        $inverted_regex = '"no"';
      } else {
        $inverted_regex = '"yes"';
      }
      print_later "IGNORE_CASE $equal $inverted_regex";
    } elsif ($keyword eq 'VERBOSE') {
      if ($regex > 5) {
        $level = 5;
      } else {
        $level = $regex;
      }
      print_later "LOGLEVEL $equal $level";
    } elsif ($keyword =~ /^SCORE(?:_(NO)?(CASE))?/) {
      $casecontrol = '';
      $casecontrol .= $1 if defined $1;
      $casecontrol .= $2 if defined $2;
      $casecontrol =~ s/(.)/ $1/;	# insert blank if defined
      print_later "SCORE$score$casecontrol $equal $regex";
    } else {
      print_later "$_";	# leave line untouched
    }
    print_later "\n";
  } else {
    die "Error in line $l: '$_'\n";
  }
}
print "$account_defs\n";
print $outstring;
