From c4bfb48415701891ef01c104f8e0b36fb762dce2 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 2 Nov 2015 10:00:53 +0000 Subject: [PATCH] Command to add/edit sip user --- Makefile | 3 + src/freedombone-addsipuser | 125 +++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) diff --git a/Makefile b/Makefile index 939828b1..52073974 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,7 @@ install: install -m 755 src/${APP}-controlpanel ${DESTDIR}${PREFIX}/bin install -m 755 src/${APP}-controlpanel ${DESTDIR}${PREFIX}/bin/control install -m 755 src/${APP}-logging ${DESTDIR}${PREFIX}/bin + install -m 755 src/${APP}-addsipuser ${DESTDIR}${PREFIX}/bin mkdir -m 755 -p ${DESTDIR}${PREFIX}/share/man/man1 install -m 644 man/${APP}.1.gz ${DESTDIR}${PREFIX}/share/man/man1 install -m 644 man/${APP}-keydrive.1.gz ${DESTDIR}${PREFIX}/share/man/man1 @@ -95,6 +96,7 @@ uninstall: rm -f ${PREFIX}/share/man/man1/${APP}-mesh.1.gz rm -f ${PREFIX}/share/man/man1/${APP}-controlpanel.1.gz rm -f ${PREFIX}/share/man/man1/${APP}-logging.1.gz + rm -f ${PREFIX}/share/man/man1/${APP}-addsipuser.1.gz rm -rf ${PREFIX}/share/${APP} rm -f ${PREFIX}/bin/${APP} rm -f ${PREFIX}/bin/zeronetavahi @@ -125,6 +127,7 @@ uninstall: rm -f ${PREFIX}/bin/meshweb rm -f ${PREFIX}/bin/${APP}-controlpanel rm -f ${PREFIX}/bin/${APP}-logging + rm -f ${PREFIX}/bin/${APP}-addsipuser clean: rm -f \#* \.#* debian/*.substvars debian/*.log rm -fr deb.* debian/${APP} diff --git a/src/freedombone-addsipuser b/src/freedombone-addsipuser index 10d56549..e3c3665b 100755 --- a/src/freedombone-addsipuser +++ b/src/freedombone-addsipuser @@ -29,4 +29,129 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +MY_USERNAME= +EXTENSION= +PASSWORD= +CONFIG_FILE=/etc/sipwitch.conf +USER_EXISTS="no" + +function show_help { + echo '' + echo 'freedombone-addsipuser -u [username] -e [extension] -p [password]' + echo '' + exit 0 +} + +function sip_user_exists { + for line in $ (cat $CONFIG_FILE) + do + if [[ "$line" == "" ]]; then + USER_EXISTS="yes" + return + fi + done +} + +function update_sip_user { + USER_FOUND= + NEW_CONFIG_FILE="${CONFIG_FILE}.new" + if [ -f $NEW_CONFIG_FILE ]; then + rm -f $NEW_CONFIG_FILE + fi + touch $NEW_CONFIG_FILE + for line in $ (cat $CONFIG_FILE) + do + if [ ! $USER_FOUND ]; then + if [[ "$line" == "" ]]; then + USER_FOUND="yes" + fi + else + if [[ "$line" == ""* ]]; then + line="$EXTENSION" + fi + if [[ "$line" == ""* ]]; then + line="$PASSWORD" + fi + if [[ "$line" == ""* ]]; then + line="$MY_USERNAME $EXTENSION" + USER_FOUND= + fi + fi + echo "$line" >> $NEW_CONFIG_FILE + done + mv $NEW_CONFIG_FILE $CONFIG_FILE +} + +function add_sip_user { + NEW_CONFIG_FILE="${CONFIG_FILE}.new" + if [ -f $NEW_CONFIG_FILE ]; then + rm -f $NEW_CONFIG_FILE + fi + touch $NEW_CONFIG_FILE + for line in $ (cat $CONFIG_FILE) + do + if [[ "$line" == '' ]]; then + echo "" >> $NEW_CONFIG_FILE + echo "$EXTENSION" >> $NEW_CONFIG_FILE + echo "$PASSWORD" >> $NEW_CONFIG_FILE + echo "$MY_USERNAME $EXTENSION" >> $NEW_CONFIG_FILE + echo '' >> $NEW_CONFIG_FILE + fi + echo "$line" >> $NEW_CONFIG_FILE + done + mv $NEW_CONFIG_FILE $CONFIG_FILE +} + +while [[ $# > 1 ]] +do +key="$1" + +case $key in + -h|--help) + show_help + ;; + -u|--user) + shift + MY_USERNAME="$1" + ;; + -e|--extension) + shift + EXTENSION="$1" + ;; + -p|--password) + shift + PASSWORD="$1" + ;; + *) + # unknown option + ;; +esac +shift +done + +if ! [[ $MY_USERNAME && $EXTENSION && $PASSWORD ]]; then + show_help +fi + +if [ ! -f $CONFIG_FILE ]; then + echo "SIP configuration file not found" + exit 1 +fi + +# the user must already exist on the system +if [ ! -d /home/$MY_USERNAME ]; then + echo "User $MY_USERNAME not found" + exit 2 +fi + +sip_user_exists + +if [[ USER_EXISTS == "yes" ]]; then + update_sip_user +else + add_sip_user +fi + +service sipwitch restart + exit 0