--- storeinfo.c.old	2004-04-03 14:05:08.000000000 +0200
+++ storeinfo.c	2004-04-03 14:05:34.000000000 +0200
@@ -335,6 +335,15 @@
 char	buf[BUFSIZ];
 struct uidplus_info *new_uidplus_info;
 
+//DAVE: just a few variable declarations
+char	*extprogram;
+char	*envvar;
+char	*syscall;
+char	*mailboxclean;
+int	ret;
+int	idx;
+//EVAD
+
 	if (fstat(fd, &stat_buf) < 0
 	    || (new_uidplus_info=(struct uidplus_info *)
 		malloc(sizeof(struct uidplus_info))) == NULL)
@@ -419,6 +428,71 @@
 	new_uidplus_info->old_uid=old_uid;
 	*cpy_info->uidplus_tail=new_uidplus_info;
 	cpy_info->uidplus_tail=&new_uidplus_info->next;
+
+//DAVE: call an external program depending on the target mailbox
+
+        	envvar=(char *)malloc(1024);
+        	syscall=(char *)malloc(1024);
+        	mailboxclean=(char *)malloc(1024);
+        	
+		fprintf(stderr, "DEBUG: full mailbox: '%s'\n", cpy_info->mailbox);
+		
+		idx = 0;
+		while( cpy_info->mailbox[idx] == '.' || cpy_info->mailbox[idx] == '/') idx++ ; 
+		
+       		strncpy(mailboxclean, (char *)(cpy_info->mailbox+idx), 1000);
+       		
+		fprintf(stderr, "DEBUG: half clean mailbox: '%s'\n", mailboxclean);
+				
+        	// if length of the mailbox string is 0, it is INBOX
+        	if( strlen(mailboxclean) == 0 ) {
+        		extprogram=getenv("ON_COPY_TO_INBOX");
+        		strcpy(envvar, "ON_COPY_TO_INBOX");
+        	} else {
+        		//we want to get the environment variable
+        		//matching the target mailbox.
+        		strcpy(envvar, "ON_COPY_TO_BOX_");
+        		
+        		//we need to convert all special chars to underscores,
+        		//since an environment variable can only handle these
+        		for( idx = 0; (mailboxclean[idx] != 0) && (idx<1000); idx++ ) {
+        			if( ((mailboxclean[idx] < 'A') ||
+        			     (mailboxclean[idx] > 'Z')) &&
+        			    ((mailboxclean[idx] < 'a') ||
+        			     (mailboxclean[idx] > 'z'))      ) {
+        				mailboxclean[idx] = '_';
+        			}
+        		}
+        		
+        	}
+       		//now get the env. var.
+       		strncat(envvar, mailboxclean, 1000);
+       		fprintf(stderr, "DEBUG: Trying to get envvar '%s'\n", envvar);
+       		extprogram=getenv(envvar);
+        	
+        	//if env. var. not specified, get the fallback var.
+        	if( extprogram == NULL ) {
+        		extprogram=getenv("ON_COPY_TO_NOT_LISTED");
+        		strcpy(envvar, "ON_COPY_TO_NOT_LISTED");
+        	}
+        	
+        	//run the program in the env. var., but replace the %s
+        	//with the filename first.
+        	if( (extprogram != NULL) && (strlen(extprogram) > 0) ) {
+        		snprintf(syscall, 1000, extprogram, tmpname);
+        		fprintf(stderr, "INFO: option: %s, calling %s\n", envvar, syscall);
+        		ret=system(syscall);
+        		//we want a syslog if there were errors.
+        		if( ret != 0 ) {
+        			fprintf(stderr, "INFO: calling %s returned error code %d\n", syscall, ret); 
+        		}
+        	}
+        	
+        	free(syscall);
+        	free(mailboxclean);
+        	free(envvar);
+//EVAD	
+
 	return (0);
 }
 

