#! /bin/sh
# Script to add users to a Jabber server
#
# Author: John Heller 03-Apr-2003

# Location of Jabber server config file
config="/etc/jabber/jabber.xml"


prog=`basename $0`

if [ $# != 2 ]
then
    echo "Usage: $prog <username> <password>"
    exit 1
fi

user=$1
pass=$2

if [ ! -f $config ]
then
    echo "$config does not exist"
    exit 1
fi

# Source the Mandrake jabber.cfg if its present
[ -f /etc/jabber/jabber.cfg ] && . /etc/jabber/jabber.cfg

if [ -z "$JABBER_SPOOL" ]
then
    JABBER_SPOOL=`egrep -o "<spool>.+</spool>" $config | sed -e "s/<[^>]*>//g"`
fi

if [ -z "$JABBER_SPOOL" ]
then
    echo "Unable to determine the Jabber spool directory"
    exit 1
fi

if [ -z "$JABBER_HOSTNAME" ]
then
    JABBER_HOSTNAME=`egrep -o "<host>.+</host>" $config | sed -e "s/<[^>]*>//g"`
fi

if [ -z "$JABBER_HOSTNAME" ]
then
    echo "Unable to determine the Jabber server hostname"
    exit 1
fi


if [ -z "$user" -a -z "$pass" ]; then
    echo "Invalid username or password supplied"
    exit 1
fi

mkdir -p $JABBER_SPOOL/$JABBER_HOSTNAME

if [ -e "$JABBER_SPOOL/$JABBER_HOSTNAME/$user.xml" ]; then
    echo "User $user already exists, not creating."
    exit 1
fi

if cat > $JABBER_SPOOL/$JABBER_HOSTNAME/$user.xml << EOF
<xdb><password xmlns='jabber:iq:auth' xdbns='jabber:iq:auth'>$pass</password><qu
ery xmlns='jabber:iq:register' xdbns='jabber:iq:register'><username>$user</usern
ame><password xmlns='jabber:iq:auth'>$pass</password></query></xdb>
EOF
then
    echo "User \"$user@$JABBER_HOSTNAME\" created"
fi
