diff options
Diffstat (limited to 'imapsync')
-rwxr-xr-x | imapsync | 34 |
1 files changed, 25 insertions, 9 deletions
@@ -47,10 +47,29 @@ usage(1) unless GetOptions(\%CONFIG, qw/debug help|h config=s quiet|q oneshot|1/ usage(0) if $CONFIG{help}; -my $CONFFILE = delete $CONFIG{config} // 'imapsync'; -my $CACHEDIR = './imapsync.cache'; # XXX use a config option -my $DBFILE = "$CACHEDIR/imap.guilhem.org.db"; -my $LOCKFILE = "$CACHEDIR/.imap.guilhem.org.lck"; +my $CONF = read_config( delete $CONFIG{config} // $NAME + , [qw/_ local remote/] + , database => qr/\A(\P{Control}+)\z/ ); +my ($DBFILE, $LOCKFILE); + +{ + $DBFILE = $CONF->{_}->{database} if defined $CONF->{_}; + $DBFILE //= $CONF->{remote}->{host}.'.db' if defined $CONF->{remote}; + $DBFILE //= $CONF->{local}->{host}. '.db' if defined $CONF->{local}; + die "Missing option database" unless defined $DBFILE; + + unless ($DBFILE =~ /\A\//) { + my $dir = ($ENV{XDG_DATA_HOME} // "$ENV{HOME}/.local/share") .'/'. $NAME; + $dir =~ /\A(\/\p{Print}+)\z/ or die "Insecure $dir"; + $dir = $1; + $DBFILE = $dir .'/'. $DBFILE; + unless (-d $dir) { + mkdir $dir, 0700 or die "Cannot mkdir $dir: $!\n"; + } + } + + $LOCKFILE = $DBFILE =~ s/([^\/]+)\z/.$1.lck/r; +} my ($DBH, $IMAP); @@ -67,10 +86,7 @@ $SIG{$_} = sub { clean(); die "$!\n"; } foreach qw/INT TERM/; ############################################################################# # Lock the database { - if (!-d $CACHEDIR) { - mkdir $CACHEDIR, 0700 or die "Cannot mkdir $CACHEDIR: $!\n"; - } - elsif (-f $LOCKFILE) { + if (-f $LOCKFILE) { open my $lock, '<', $LOCKFILE or die "Cannot open $LOCKFILE: $!\n"; my $pid = <$lock>; close $lock; @@ -164,7 +180,7 @@ sub msg($@) { # Connect to the local and remote IMAP servers foreach my $name (qw/local remote/) { - my %config = Net::IMAP::Sync::read_config($CONFFILE, $name); + my %config = %{$CONF->{$name}}; $config{$_} = $CONFIG{$_} foreach keys %CONFIG; $config{enable} = 'QRESYNC'; $config{name} = $name; |