#!/usr/bin/perl -w

# CLI admin client for the FEX service
#
# Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
#
# Copyright: GNU General Public License

use 5.006;
use Getopt::Std;
use File::Basename;

# add fex lib
$FEXLIB =
  $0 =~ m:(/.+)/.+/: ? "$1/lib":
  $0 =~ m:(.*/):     ? "$1/../lib":
                       "../lib";

die "$0: no \$FEXLIB, run $0 with full path!\n" unless -d $FEXLIB;

$0 =~ s:.*/::;

# become effective user fex
unless ($>) {
  if (my @pw = getpwnam('fex')) {
    $) = $pw[3];
    $> = $pw[2];
    $ENV{HOME} = $pw[7];
  }
}

# import from fex.pp
our ($FEXHOME,$spooldir,$logdir,$docdir,$durl,$mdomain,$admin,$keep_default);

# load common code, local config : $HOME/lib/fex.ph
require "$FEXLIB/fex.pp" or die "$0: cannot load $FEXLIB/fex.pp - $!\n";

die "$0: \$admin not configured in $FEXLIB/fex.ph\n" if $admin =~ /example.org/;

$EDITOR = $ENV{EDITOR} || 'vi';

$opt_v = $opt_l = $opt_h = $opt_w = 0;
$opt_u = $opt_s = $opt_e = $opt_d = '';

getopts('hvlwu:s:e:d:') or usage(2);
usage(0) if $opt_h;

unless (-d $spooldir) {
  die "$0: no $spooldir - create it (mkdir)\n";
}

@stat = stat $spooldir or die "$0: cannot access $spooldir - $!\n";
warn "$0: $spooldir with owner=root !?\n" unless $stat[4];
$) = $stat[5];
$> = $stat[4];

$fup = $durl;
$fup =~ s:/[^/]+$:/fup:;

# list files and download URLs
if ($opt_w) {
  $log = "$logdir/fexsrv.log";
  warn "$0: polling $log\n\n";
  exec "$FEXHOME/bin/logwatch",$log;
  die "$0: logwatch not found\n";
}

# list files and download URLs
if ($opt_l) {
  my ($file,$dkey);
  chdir $spooldir or die "$0: $spooldir - $!\n";
  foreach $file (glob "*/*/*") {
    if (-s "$file/data" and 
        $dkey = readlink("$file/dkey") and 
        -l ".dkeys/$dkey") {
      ($to,$from,$file) = split "/",$file;
      print "$from --> $to : $durl/$dkey/$file\n";
    }
  }
  exit;
}

# show user config
if ($opt_s) {
  $idf = "$spooldir/$opt_s/@";
  if (open F,$idf) {
    $id = <F>;
    die "$0: no auth-ID in $idf" unless $id;
    chomp $id;
    close F;
    print "$fup?from=$opt_s&id=$id\n";
  } else {
    die "$0: no $idf"; 
  }
  exit;
}

# delete user 
if ($opt_d) {
  $idf = "$spooldir/$opt_d/\@";
  die "$0: no such user $opt_d\n" unless -f $idf;
  unlink $idf or die "$0: cannot remove $idf - $!\n";
  unlink "$spooldir/$opt_d/\@ALLOWED_RECIPIENTS";
  print "$opt_d deleted\n";
  exit;
}

# edit user restriction file
if ($opt_e) {
  die "$0: no user $opt_e" unless -d "$spooldir/$opt_e";
  $ar = "$spooldir/$opt_e/\@ALLOWED_RECIPIENTS";
  unless (-f $ar) {
    open F,">$ar" or die "$0: cannot open $ar - $!";
    print F<<EOD;
# Restrict allowed recipients. Only those listed here are allowed.
# Make this file COMPLETLY empty if you want to disable the restriction.
# An allowed recipient is an e-mail address, you can use * as wildcard.
# Example: *\@flupp.org
EOD
    close F;
  }
  system $EDITOR,$ar;
  unlink $ar unless -s $ar;
  exit;
}

# show config
if ($opt_v) {
  print "spooldir     = $spooldir\n";
  print "logdir       = $logdir\n";
  print "docdir       = $docdir\n";
  print "durl         = $durl\n";
  print "mdomain      = $mdomain\n";
  print "keep_default = $keep_default\n";
  print "admin        = $admin\n";
  print "\nto change these settings, edit $FEXLIB/fex.ph\n";
  exit;
}

# add user or show show upload URL
if ($opt_u) {
  $user = lc $opt_u;
  $user .= '@'.$mdomain if $user !~ /@/;
  $id = shift;
  $idf = "$spooldir/$user/@";
  $sid = '';
  if (open F,$idf) {
    $ido = <F>;
    if ($ido) {
      chomp $ido;
      undef $/;
      $sid = <F> || '';
      close F;
    }
  }
  unless ($id) {
    unless ($ido) {
      die "$0: user is not a regular FEX user\n";
    }
    print "$fup?from=$user&ID=$ido\n";
    exit;
  }
  unless ($user =~ /\w@[\w\.\-]+\.[a-z]+$/) {
    warn "$0: $user is not a valid e-mail-address!\n";
  }
  unless (-d "$spooldir/$user") {
    mkdir "$spooldir/$user",0755 
      or die "$0: cannot mkdir $spooldir/$user - $!\n";
  }
  open F,">$idf" or die "$0: cannot write $idf - $!\n";
  print F $id,"\n",$sid;
  close F or die "$0: cannot write $idf - $!\n";
  print "$fup?from=$user&ID=$id\n";
  exit;
}

usage(3);

sub usage {
  print <<EOD;
usage: $0 -u user [auth-ID]
   or: $0 -e user
   or: $0 -v
   or: $0 -l
   or: $0 -w
options: -u user		show upload URL for user
	 -u user auth-ID	create new user or set new auth-ID
	 -e user 		edit user recipients restriction file
	 -d user 		delete user
         -v			show config
         -l			list current files
         -w			watch fexsrv.log (continously)
hints: user must be a valid e-mail-address! (= sender address in fup CGI)
       auth-ID is an authentification ID, something like a low-secure password
EOD
  exit shift;
}
