-sub send_data(#){
- my $client = shift;
- my $hash = ${$conf{'client'}}{$client};
- if (${$hash}{'mbox'} =~ /\/$/){ #ok, maildir format
- my $path = "${$hash}{'mbox'}";
- $path =~ s/\/$//igo;
- &debug_msg("Sending datas for ${$hash}{'mbox'}\n");
- my @list=`ls -1 $path/new | head -n ${$hash}{'nb_mails'}`;
- if ($#list >= 0){
- #Send files, one by one
- foreach my $fich (@list){
- chomp($fich);
- &debug_msg("File : $fich\n");
- if (my $pid = open(CHILD, "|-")) {
- CHILD->autoflush(1);
- if (open(DATA,"<$path/new/$fich")){
- while (my $line=<DATA>){
- print CHILD "$line";
- }
- close(DATA);
- &debug_msg("$path/new/$fich -> $path/cur/$fich ... ");
- if (rename("$path/new/$fich","$path/cur/$fich")){
- &debug_msg("ok\n");
- }else{
- print STDERR "Error moving $path/new/$fich, keeped : $!\n";
- }
- }else{
- print STDERR "Error opening $path/new/$fich : $!\n";
- exit(1);
- }
- close(CHILD);
- } else {
- die "cannot fork: $!" unless defined $pid;
- &change_uid_gid("$client");
- &debug_msg("Executing ${$hash}{'command'} .... \n");
- exec("${$hash}{'command'}");
- }
- }
- }else{
- &debug_msg("No mail available\n");
- }
- }else{
- &debug_msg("Mbox format not yet supported.\n");
- exit(1);
- }
+sub send_data(#) {
+ # TODO check that the child has properly finished before moving
+ # the file tu cur/
+ my $client = shift;
+ my $fich = shift;
+ &debug_msg("run $conf{'client'}{$client}{'command'} with file $fich for $client\n");
+ my $pid = fork;
+ if (!defined $pid) {
+ die "cannot fork $!";
+ } elsif ($pid == 0) {
+ # fils
+ if (open(STDIN, '<', $fich)) {
+ &change_uid_gid($client);
+ &debug_msg("sending output to $conf{'client'}{$client}{'command'} with file $fich for $client $fich openned\n");
+ exec $conf{'client'}{$client}{'command'} or die "exec error $!";
+ } else {
+ die "Error opening $fich : $!\n";
+ }
+ } else {
+ # père
+ wait;
+ &debug_msg("sending output to $conf{'client'}{$client}{'command'} with file $fich for $client done\n");
+ # return true if the program exited normaly
+ return ($? >> 8) == 0;
+ }
+}
+
+sub list_files(#) {
+ my $client = shift;
+ my %hash = %{$conf{'client'}{$client}};
+ if ($hash{'mbox'} =~ /\/$/) { #ok, maildir format
+ my @tomove;
+ my @args;
+ my $path = $hash{'mbox'};
+ $path =~ s/\/$//igo; # suppress the final /
+ &debug_msg("Sending datas for $hash{'mbox'}\n");
+ # read the dir
+ if (opendir MAILDIR, "$path/new") {
+ my $nb_left = $hash{'nb_mails'};
+ while (my $fich = readdir MAILDIR) {
+ # skip . and ..
+ next if ($fich eq "." or $fich eq "..");
+ # read the dir until the max number of mail is done
+ &debug_msg("nb_left : $nb_left\n");
+ last if ($nb_left-- == 0);
+ &debug_msg("File : $fich\n");
+ # if add_to_cmdline is true, add to the list
+ if ($hash{'add_to_cmdline'}) {
+ push @args, $fich;
+ } else {
+ # else run the command for each file
+ if (&send_data($client, "$path/new/$fich")) {
+ push @tomove, $fich;
+ }
+ }
+ &debug_msg("Done file : $fich, $?\n");
+ }
+ closedir MAILDIR;
+ } else {
+ print STDERR "Error opening $path/new : $!\n";
+ }
+ # if add_to_cmdline is true and there are files to treat,
+ # run the command
+ if ($hash{'add_to_cmdline'} and scalar @args > 0) {
+ chdir "$path/new" or die "Cannot chdir to $path/new : $!";
+ &debug_msg("run $hash{'command'} with files @args for $client\n");
+ my @cmd = split(/ /, $hash{'command'});
+ push @cmd, @args;
+ &debug_msg("@cmd\n");
+ my $pid = fork;
+ if (!defined $pid) {
+ die "cannot fork $!";
+ } elsif ($pid == 0) {
+ # fils
+ &change_uid_gid($client);
+ exec @cmd or die "exec error $!";
+ } else {
+ # père
+ wait;
+ my $status = $? >> 8;
+ if ($status == 0) {
+ # if the command didn't fail, mark the files to be moved
+ @tomove = @args;
+ }
+ }
+ }
+ # if there is files to move, moves them
+ foreach my $fich (@tomove) {
+ &debug_msg("$path/new/$fich -> $path/cur/$fich ... ");
+ if (rename("$path/new/$fich", "$path/cur/$fich")){
+ &debug_msg("ok\n");
+ } else {
+ print STDERR "Error moving $path/new/$fich, keeped : $!\n";
+ }
+ }
+ } else {
+ &debug_msg("Mbox format not yet supported.\n");
+ exit(1);
+ }