require [ "date", "envelope", "fileinto", "mailbox", "subaddress", "variables" ];

#if currentdate :matches "year"  "*" { set "year"  "${1}"; }
#if currentdate :matches "month" "*" { set "month" "${1}"; }

if exists "X-MC-User" {
  if header :matches "From" "*<*@*.asso.fr>*" {
    fileinto :create "Listes+${3}";
    stop;
  }
  elsif header :matches "From" "*<*@*.*>*" {
    fileinto :create "Listes+${3}+${2}";
    stop;
  }
}
elsif exists "List-Id" {
  if header :matches "List-Id" "*<*.*.*.*>*" {
    if allof (not string :is "${2}" "", not string :is "${3}" "", not string :is "${4}" "")  {
      fileinto :create "Listes+${4}+${3}+${2}";
      stop;
    }
  }
  elsif header :matches "List-Id" "*<*.*.*>*" {
    if allof (not string :is "${2}" "", not string :is "${3}" "")  {
      fileinto :create "Listes+${3}+${2}";
      stop;
    }
  }
}

if envelope :matches :detail "TO" "*" { set "extension" "${1}"; }
if not string :is "${extension}" "" {
  fileinto :create "Listes+${extension}";
  stop;
}