--- add-popa3d-user	Sat Oct  4 21:39:07 2003
+++ add-popa3d-user-with-postfix	Sun Dec  7 16:04:23 2003
@@ -57,6 +57,23 @@
 my $virtual_mail_owner = "vmail";
 my $mail_group = "mail";
 
+# MTA options
+# Point your MTA here. Possible options are "exim", "postfix" or 
+# "manual"
+
+my $mta_program = "postfix";
+
+# MTA config file to edit (file with virtual users)
+# In postfix configuration, this could be /etc/postfix/vmailbox
+# In exim ???
+
+my $mta_virtual_file = "/etc/postfix/vmailbox";
+
+# Enter the full path to postfix's postmap program
+# (If you are using postfix)
+
+my $postfix_postmap = "/usr/sbin/postmap";
+
 # End of options.  No need to change anything below this line.
 
 #######################################################################
@@ -162,14 +179,25 @@
 
 print "Creating mail file for $user...\n";
 
-sysopen(MAIL, "$virtual_domain_root_directory/mail/$user",
-              O_RDONLY | O_CREAT | O_EXCL, $mail_mask)
-        or &cleanup("Can't create ",
-                    "$virtual_domain_root_directory/mail/$user: $!\n");
 
-$undo_mail_file = "$virtual_domain_root_directory/mail/$user";
+# Probably is good idea to check if this file already exist.
+# For example, I've moved my mailbox to virtual root directory
+# before creating user with add-popa3d-user. So user adding failed.
 
-close(MAIL);
+unless ( -e "$virtual_domain_root_directory/mail/$user") 
+{
+	sysopen(MAIL, "$virtual_domain_root_directory/mail/$user",
+	              O_RDONLY | O_CREAT | O_EXCL, $mail_mask)
+	        or &cleanup("Can't create ",
+	                    "$virtual_domain_root_directory/mail/$user: $!\n");
+
+	$undo_mail_file = "$virtual_domain_root_directory/mail/$user";
+
+	close(MAIL);
+} else 
+{
+	print "\"$virtual_domain_root_directory/mail/$user\" mailbox file already exist\n";
+}
 
 # Set mail file ownership and permissions
 
@@ -193,6 +221,22 @@
         &cleanup("Can't change permissions for ",
             "$virtual_domain_root_directory/mail/$user: $!\n");
 
+# MTA configuration
+
+if ($mta_program eq "manual")
+{
+	# nothing to do here
+
+} elsif ($mta_program eq "postfix")
+{
+	&add_postfix_user ($user);
+
+} elsif ($mta_program eq "exim")
+{
+	# exim function here
+}
+
+
 sub cleanup
 {
         print STDERR "@{_}Cleaning up...\n";
@@ -219,5 +263,60 @@
 sub sighandler
 {
         &cleanup("Caught a SIG@_.\n");
+}
+
+
+# postifx configuration
+
+sub add_postfix_user
+{
+	print "\nBeginning postifx configuration...\n";
+	# first, check if this username already exist in $mta_virtual_file
+	open (PFILE, "< $mta_virtual_file") or 
+		(print "Error opening $mta_virtual_file for reading\n" and return);
+	my @content = <PFILE>;
+	close PFILE;
+
+	foreach (@content)
+	{
+		chomp;
+		next if (/^\s*#/);	# comment
+		next if (/^\s*$/);	# empty line
+		s/^\s+//;		# removing spaces at the beginnig
+
+		my $em = (split(/\s+/))[0];
+		if (/^(.+)\@/)
+		{
+			if ($1 eq $user)
+			{
+				print "This user already exists in $mta_virtual_file\n";
+				return;
+			}
+		}
+	}
+
+	# Now we can add the user
+	my $domain;
+	while (1)
+	{
+		print "Enter the domain name for user $user (e.g. example.org): ";
+		chomp ($domain = <STDIN>);
+		
+		# currently, no real checks available
+		last unless ($domain =~ /^\s*$/);
+	}
+
+	print "Adding $user to $mta_virtual_file ... ";
+	open (PFILE, ">> $mta_virtual_file") or 
+		(print "Error opening $mta_virtual_file for appending\n" and return);
+	print PFILE "$user\@$domain	$user\n";
+	close PFILE;
+	print "DONE\n";
+	
+	print "Running postmap for file $mta_virtual_file ... ";
+	system $postfix_postmap, $mta_virtual_file;
+	print "DONE\n";
+	
+	# all done
 }
 
