# SME delimiter_extension catch all 
# this first detect existing username with a - in it
# if none existing, then consider part after - delimiter_extension as an extension
{
# Strip everything from the first dash to the @ sign
#/^([^-@]+)-[^@]*@(.*)$/    ${1}@${2}

    return "#/!\\=============> DelegateMailServer is set" if ($DelegateMailServer && ($DelegateMailServer !~ /^\s*$/));

    $OUT = "";
    $myhostname=$qpsmtpd{HeloHost} || "$SystemName.$DomainName";
    
    $mydomains= "localhost|alias-localdelivery-$SystemName|$myhostname|$DomainName|$SystemName";
    my $i = 0;

    use esmith::DomainsDB;
    my $ddb = esmith::DomainsDB->open_ro;
    my @domains = map { $_->key } $ddb->get_all_by_prop('type' => 'domain');

    foreach my $domain ( @domains )
    {
        my $d = $ddb->get($domain);
        next if (($d->prop('EmailTo')||"") !~ /^\s*$/);
        next unless (($d->prop('MailServer') || '') eq '');
        next if $domain eq $DomainName;
        next if $domain eq "$SystemName.$DomainName";
        next if ( $qpsmtpd{HeloHost} && ($domain eq $qpsmtpd{HeloHost}));
        $mydomains .= "|$domain";
    }
   
    return "#/!\\=============> no virtual domain" if $mydomains =~ /^\s*$/;

    use esmith::AccountsDB;

    my $adb = esmith::AccountsDB->open_ro or die "Couldn't open AccountsDB";
    my %dashed;
    my $remove;
    for my $user ($adb->users,$adb->pseudonyms,$adb->groups)
    {
      
      $dashed{$user->key}=$user->key if ($user->key =~ /-/);
      $remove .= "(?!".$user->key.")" if ($user->key =~ /-/);
    }
    # generik catch all, except usernames with - in it; we remove domain as we want to redirect
    # pseudonyms, groups etc. to another run in virtual table
    $OUT = '/^'.$remove.'([^-@]+)-[^@]*@('.$mydomains.')$/    ${1}'."\n";
    $OUT .= '# here are all the usernames/pseudonyms/groups with - in it'."\n";
    foreach my $key (keys %dashed )
    {
      my $to = ($adb->get($key)->prop("type")  eq "pseudonym")? $adb->get($key)->prop("Account") : $key;
      # groups are already defined further in the file
      $OUT .='/^'.$key.'@('.$mydomains.')$/    '.$to."\@alias-localdelivery-$SystemName\n" unless ($adb->get($key)->prop("type")  eq "group");
      $OUT .='/^'.$key.'-[^@]*@('.$mydomains.')$/    '.$to."\@alias-localdelivery-$SystemName\n";
    }
}
