#!/bin/bash
### cryptoml-ed - Edit your cryptoml.

version="cryptoml-ed 0.1.0 (2005-10-07) [alpha] {for sendmail-cryptoml 0.5.0}"
location="https://www.inventati.org/inventa/doku.php?id=cryptoml-ed"
purpose="Edit your cryptoml."

copyright="NO Copyright (C)  2005  NonfirmareNiente.
Released in the Public Domain."

conditions="Copying and distribution of this file, with or without
modification, are permitted worldwide, without royalty, in any medium."

nowarranty="THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE."

help="$version
Location: <$location>
Purpose: $purpose

Usage: cryptoml-ed [OPTIONS]...
 Documentation options:
  -h         --help              Show help and exit.
  -V         --version           Show version and exit.
  -W         --no-warranty       \`cryptoml-ed' comes with NO WARRANTY.
  -C         --copying           Show copying conditions and exit.
 Configuration options:
  -L LIST    --thelist LIST      Mailing list on which to operate on.
  -U USER    --user              User on which to operate on.
  -A ADDRESS --address ADDRESS   email address for the remailer used by USER.
  -K KEY     --key KEY           PGP key id for the remailer used by USER.
  -R RPLBLK  --replyblock RPLBLK Reply block for USER (filename).
 Action options:
  -l         --list              List lists.
  -m         --list-users        List LIST users.
  -p         --print             Print LIST configuration.
  -n         --new               Make LIST.
  -d         --delete            Delete LIST.
  -s         --subscribe         Subscribe USER to LIST.
  -u         --unsubscribe       Unsubscribe USER from LIST."

## Set some default values.

# FIXME!: Hey, this needs to be syncronized with the other tools,
# it would be better to find a common file to include!
#
confdir=~/.cryptoml
mlconfdir=$confdir/ml

actions=""
thelist=""
user=""
address=""
key=""
replyblock=""
rm="rm -f"
rmdir=rmdir

if which wipe >& /dev/null; then
    rm="wipe -sf"
    rmdir="wipe -sfr"
elif which shred >& /dev/null; then
    rm="shred -f"
fi

## Utility functions.

# arg 1: var name; arg 2: var value.
function fail_if_set(){
    if [ "$2" != "" ]; then
	echo "cryptoml-ed: error: $1 was already set." > /dev/stderr
	exit 1
    fi
}

# arg 1: var name; arg 2: var value.
function fail_if_not_set(){
    if [ "$2" == "" ]; then
	echo "cryptoml-ed: error: $1 was not set." > /dev/stderr
	exit 1
    fi
}

# arg 1: list
function fail_if_list_exists(){
    if [ -d "$mlconfdir/$1" ]; then
	echo "cryptoml-ed: error: list does already exists." > /dev/stderr
	exit 1
    fi
}

# arg 1: list
function fail_if_list_does_not_exists(){
    if [ ! -d "$mlconfdir/$1" ]; then
	echo "cryptoml-ed: error: list does not exists." > /dev/stderr
	exit 1
    fi
}

# arg 1: list; arg 2: user.
function fail_if_user_subscribed(){
    if [ -f "$mlconfdir/$1/$2" ]; then
	echo "cryptoml-ed: error: user was already subscribed." > /dev/stderr
	exit 1
    fi
}

# arg 1: list; arg 2: user.
function fail_if_user_not_subscribed(){
    if [ ! -f "$mlconfdir/$1/$2" ]; then
	echo "cryptoml-ed: error: user was not subscribed." > /dev/stderr
	exit 1
    fi
}

## Main functions.

function list_lists(){
    ( cd $mlconfdir
	find . -type d -mindepth 1 -maxdepth 1 -printf "%f\n" )
}

# arg 1: list
function list_users(){
    fail_if_not_set list $1
    fail_if_list_does_not_exists $1
    ls "$mlconfdir/$1" | grep '^0x[0-9A-F]\{8\}$'
}

# arg 1: list
function print_list(){
    fail_if_not_set list $1
    fail_if_list_does_not_exists $1
    for filename in `ls $mlconfdir/$thelist`; do
	echo "-----BEGIN OF FILE <$filename>-----"
	cat "$mlconfdir/$thelist/$filename"
	echo "-----END OF FILE <$filename>-----"
    done
}

# arg 1: list
function new_list(){
    fail_if_not_set list $1
    fail_if_list_exists $1
    mkdir "$mlconfdir/$1"
}

# arg1: list
function delete_list(){
    fail_if_not_set list $1
    fail_if_list_does_not_exists $1
    for filename in `ls "$mlconfdir/$1"`; do
	$rm "$mlconfdir/$1/$filename"
    done
    $rmdir "$mlconfdir/$1"
}

# arg 1: list; arg 2: the user;
# arg 3: address; arg 4: key; arg 5: reply block
function subscribe(){
    fail_if_not_set list $1
    fail_if_not_set user $2
    fail_if_not_set address $3
    fail_if_not_set key $4
    fail_if_not_set replyblock $5
    fail_if_list_does_not_exists $1
    fail_if_user_subscribed $1 $2
    echo $3 > "$mlconfdir/$1/$2"
    echo $4 >> "$mlconfdir/$1/$2"
    cat $5 >> "$mlconfdir/$1/$2"
}

# arg 1: list; arg 2: user.
function unsubscribe(){
    fail_if_not_set list $1
    fail_if_not_set user $2
    fail_if_list_does_not_exists $1
    fail_if_user_not_subscribed $1 $2
    $rm "$mlconfdir/$1/$2"
}

## Parse the command line options.

cmdline=`getopt -o hVWCL:U:A:K:R:lmpndsu --long help,version,no-warranty,copying,thelist:,user:,address:,key:,replyblock:,list,list-users,print,new,delete,subscribe,unsubscribe -n cryptoml-ed -- ${1+"$@"}`
if [ $? != 0 ]; then exit 1; fi
eval set -- "$cmdline"
while true; do
    case "$1" in
	-h|--help) echo "$help" | more; exit 0;;
        -V|--version) echo "$version"; exit 0;;
	-W|--no-warranty) echo "$nowarranty" | more; exit 0;;
	-C|--copying) echo -e "$copyright\n\n$conditions\n\n$nowarranty" | more
	    exit 0;;
	-L|--thelist) fail_if_set list $thelist; thelist="$2"; shift 2;;
	-U|--user) fail_if_set user $user; user="$2"; shift 2;;
	-A|--address) fail_if_set address $address; address="$2"; shift 2;;
	-K|--key) fail_if_set key $key; key="$2"; shift 2;;
	-R|--replyblock) fail_if_set replyblock $replyblock;
	    replyblock="$2"; shift 2;;
	-l|--list) actions="$actions list"; shift;;
	-m|--list-users) actions="$actions listusers"; shift;;
	-p|--print) actions="$actions print"; shift;;
	-n|--new) actions="$actions new"; shift;;
	-d|--delete) actions="$actions delete"; shift;;
	-s|--subscribe) actions="$actions subscribe"; shift;;
	-u|--unsubscribe) actions="$actions unsubscribe"; shift;;
	--) shift; break;;
	*)  echo "cryptoml-ed: internal error." > /dev/stderr; exit 1;;
    esac
done

actions="$actions --"
eval set -- "$actions"
while true; do
    case "$1" in
	list) list_lists; shift;;
	listusers) list_users $thelist; shift;;
	print) print_list $thelist; shift;;
	new) new_list $thelist; shift;;
	delete) delete_list $thelist; shift;;
	subscribe) subscribe $thelist $user $address $key $replyblock; shift;;
	unsubscribe) unsubscribe $thelist $user; shift;;
	--) shift; break;;
	*) echo "cryptoml-ed: internal error." > /dev/stderr; exit 1;;
    esac
done

exit 0

#### cryptoml-ed ends here.
 
  cryptoml-ed.txt · Ultima modifica: 08.10.2005 00:30 by 127.0.0.1
 
RSS: Modifiche Recenti Contenuti: Licenza Creative Commons Valid XHTML 1.0 Valid CSS Sito fatto con: DokuWiki Sito ospitato da: Inventati.org