fixup
[freeside.git] / bin / create-fetchmailrc
1 #!/usr/bin/perl -w
2 # this quick hack helps you generate/maintain .fetchmailrc files from
3 # FS::acct_snarf data.  it is run from a shellcommands export as:
4 # create-fetchmailrc $username $dir $snarf_machine1 $snarf_username1 $snarf__password1 $snarf_machine2 $snarf_username2 $snarf__password2 ...
5
6 use strict;
7 use POSIX qw( setuid setgid );
8
9 my $header = <<END;
10 # Configuration created by create-fetchmailrc
11 set postmaster "postmaster"
12 set bouncemail
13 set no spambounce
14 set properties ""
15 set daemon 240
16 END
17
18 my $username = shift @ARGV or die "no username specified\n";
19 my $homedir  = shift @ARGV or die "no homedir specified\n";
20 my $filename = "$homedir/.fetchmailrc";
21
22 my $gid = scalar(getgrnam($username)) or die "can't find $username's gid\n";
23 my $uid = scalar(getpwnam($username)) or die "can't find $username's uid\n";
24
25 open(FETCHMAILRC, ">$filename") or die "can't open $filename: $!\n";
26 chown $uid, $gid, $filename     or die "can't chown $uid.$gid $filename: $!\n";
27 chmod 0600, $filename           or die "can't chmod 600 $filename: $!\n";
28 print FETCHMAILRC $header;
29
30 while ($ARGV[0]) {
31   my( $s_machine, $s_username, $s_password ) = splice( @ARGV, 0, 3 );
32   print FETCHMAILRC <<END;
33 poll $s_machine
34         user '$s_username' there with password '$s_password' is '$username' here
35 END
36 }
37
38 close FETCHMAILRC;
39
40 setgid($gid) or die "can't setgid $gid\n";
41 setuid($uid) or die "can't setuid $uid\n";
42 $ENV{HOME} = $homedir;
43
44 system(qq(fetchmail -a -K --antispam "550,451" -d 180 -f $filename));
45