#!/usr/bin/perl -w

#
#  This script changes the domain part of the "From:" addresses
#  of WebMail users in a given domain to another value.
#

use CLI;


 # you need to change this to the domain name you want
 # to be in users' "From:" addresses
my $destDomain = 'company.com';

my $count=0;

print "Domain: ";        
my $Domain = <STDIN>;     
chomp $Domain;           

  # You may need to redefine the following variable if you're connecting not to
  # the main domain. IP address is OK.
$CGServerAddress = $Domain;

print "Login (Enter for \"Postmaster\@$Domain\"): ";
my $Login = <STDIN>;
chomp $Login;
if ($Login eq '') { $Login = "Postmaster\@$Domain"; }

print "Password: ";
my $Password = <STDIN>;
chomp $Password;

my $cli = new CGP::CLI( { PeerAddr => $CGServerAddress,
                          PeerPort => 106,
                          login    => $Login,
                          password => $Password } )
   || die "Can't login to CGPro: ".$CGP::ERR_STRING."\n";



sub updateFromAddress {

  my $account = $_;

  my $settings = $cli->GetAccountSettings("$account\@$Domain")
    || die "Can't get settings for $account:" . $cli->getErrMessage;
  my $name = @$settings{RealName};
  $name=$account unless(defined $name && $name ne '');

  my $webSettings = $cli->GetWebUser("$account\@$Domain")
    || die "Can't get Web settings for $account:" . $cli->getErrMessage;

    
  @$webSettings{UserFrom} = "$name <$account\@$destDomain>";
  
  $cli->SetWebUser("$account\@$Domain", $webSettings )
    || die "Can't set Web settings for $account:" . $cli->getErrMessage;
  $count++;
}

my $accountsList = $cli->ListAccounts($Domain)
  || die "Can't list accounts:" . $cli->getErrMessage;

foreach ( sort keys %$accountsList ) {
  updateFromAddress;
}

print "$count accounts updated\n";

$cli->Logout;


