require [ "copy", "environment", "imap4flags", "imapsieve", "variables", "vnd.dovecot.pipe" ];

if environment :is "imap.changedflags" "Junk" {
  if environment :matches "imap.user" "*" { set "username" "${1}"; }
  if hasflag :is "Junk" {
    pipe :copy :try "learn-spam.sh" [ "${username}" ];
  } elsif not hasflag :is "Junk" {
    pipe :copy :try "learn-ham.sh" [ "${username}" ];
  }
}