*** vaddaliasdomain.c.orig	Fri Aug 31 14:13:27 2001
--- vaddaliasdomain.c	Fri Aug 31 15:33:12 2001
***************
*** 42,47 ****
--- 42,48 ----
  
  void usage();
  void get_options(int argc,char **argv);
+ int add_aliasdomain_virtualdomains( char *domain_new, char *domain_old );
  
  int main(argc,argv)
   int argc;
***************
*** 63,83 ****
  		usage();
  		vexit(0);
  	}
- 	strncpy(TmpBuf3, Dir, MAX_BUFF);
  
! 	tmpstr = strstr(Dir, "/domains");
! 	*tmpstr = 0;
! 
! 	snprintf(TmpBuf1, MAX_BUFF, "%s/domains/%s", Dir, Domain_new);
! 	snprintf(TmpBuf2, MAX_BUFF, "%s/domains/%s", Dir, Domain_old);
! 	if ( symlink(TmpBuf2, TmpBuf1) != 0 ) {
! 		fprintf(stderr, "Could not make link\n");
! 		perror("making link");
! 	}
! 	add_domain_assign( Domain_new, TmpBuf3, uid, gid );
  	signal_process("qmail-send", SIGHUP);
  
  	return(vexit(0));
  }
  
  void usage()
--- 64,119 ----
  		usage();
  		vexit(0);
  	}
  
! 	add_aliasdomain_virtualdomains( Domain_new, Domain_old );
  	signal_process("qmail-send", SIGHUP);
  
  	return(vexit(0));
+ }
+ 
+ /* 
+  * Add the aliasdomain to the required control files rcpthosts and virtualdomains 
+  * And signal qmail
+  * domain_new is the aliasdomain name
+  * domain_old is the original
+  */
+ int add_aliasdomain_virtualdomains( char *domain_new, char *domain_old )
+ {
+  static char tmpstr1[MAX_BUFF];
+  static char tmpstr2[MAX_BUFF];
+ 
+     /* If we have more than 50 domains in rcpthosts
+      * make a morercpthosts and compile it
+      */
+     if ( count_rcpthosts() >= 50 ) {
+         snprintf(tmpstr1, MAX_BUFF, "%s/control/morercpthosts", QMAILDIR);
+         update_file(tmpstr1, domain_new);
+         snprintf(tmpstr1, MAX_BUFF, "%s/control/morercpthosts", QMAILDIR);
+         chmod(tmpstr1, VPOPMAIL_QMAIL_MODE ); 
+         if ( OptimizeAddDomain == 0 ) {
+             compile_morercpthosts();
+         }
+ 
+     /* or just add to rcpthosts */
+     } else {
+         snprintf(tmpstr1, MAX_BUFF, "%s/control/rcpthosts", QMAILDIR);
+         update_file(tmpstr1, domain_new );
+         snprintf(tmpstr1, MAX_BUFF, "%s/control/rcpthosts", QMAILDIR);
+         chmod(tmpstr1, VPOPMAIL_QMAIL_MODE ); 
+     }
+     
+     /* Add to virtualdomains file and remove duplicates  and set mode */
+     snprintf(tmpstr1, MAX_BUFF, "%s/control/virtualdomains", QMAILDIR );
+     snprintf(tmpstr2, MAX_BUFF, "%s:%s", domain_new, domain_old );
+     update_file(tmpstr1, tmpstr2);
+     chmod(tmpstr1, VPOPMAIL_QMAIL_MODE ); 
+ 
+     /* make sure it's not in locals and set mode */
+     snprintf(tmpstr1, MAX_BUFF, "%s/control/locals", QMAILDIR);
+     remove_line( domain_new, tmpstr1); 
+     chmod(tmpstr1, VPOPMAIL_QMAIL_MODE ); 
+ 
+     return(0);
  }
  
  void usage()
