freedombone-rmsipuser 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Removes a SIP phone user from the system
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. export TEXTDOMAIN=${PROJECT_NAME}-rmsipuser
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. MY_USERNAME=$1
  33. CONFIG_FILE=/etc/sipwitch.conf
  34. USER_EXISTS="no"
  35. function show_help {
  36. echo ''
  37. echo $"${PROJECT_NAME}-rmsipuser [username]"
  38. echo ''
  39. exit 0
  40. }
  41. function sip_user_exists {
  42. IFS=''
  43. while read line; do
  44. if [[ "$line" == *"<user id=\"$MY_USERNAME\">" ]]; then
  45. USER_EXISTS="yes"
  46. return
  47. fi
  48. done < $CONFIG_FILE
  49. }
  50. function remove_sip_user {
  51. USER_FOUND=
  52. NEW_CONFIG_FILE="${CONFIG_FILE}.new"
  53. if [ -f $NEW_CONFIG_FILE ]; then
  54. rm -f $NEW_CONFIG_FILE
  55. fi
  56. touch $NEW_CONFIG_FILE
  57. IFS=''
  58. while read line; do
  59. if [ ! $USER_FOUND ]; then
  60. if [[ "$line" == *"<user id=\"$MY_USERNAME\">" ]]; then
  61. USER_FOUND="yes"
  62. fi
  63. fi
  64. if [ ! $USER_FOUND ]; then
  65. echo "$line" >> $NEW_CONFIG_FILE
  66. else
  67. if [[ "$line" == *'</user>' ]]; then
  68. USER_FOUND=
  69. fi
  70. fi
  71. done < $CONFIG_FILE
  72. mv $NEW_CONFIG_FILE $CONFIG_FILE
  73. }
  74. if [ ! $MY_USERNAME ]; then
  75. show_help
  76. fi
  77. if [ ! -f $CONFIG_FILE ]; then
  78. echo $"SIP configuration file not found"
  79. exit 1
  80. fi
  81. # the user must already exist on the system
  82. if [ ! -d /home/$MY_USERNAME ]; then
  83. echo $"User $MY_USERNAME not found"
  84. exit 2
  85. fi
  86. sip_user_exists
  87. if [[ $USER_EXISTS != "yes" ]]; then
  88. echo $'User not found within SIP configuration file'
  89. exit 3
  90. fi
  91. systemctl stop sipwitch
  92. remove_sip_user
  93. systemctl start sipwitch
  94. echo $"SIP user $MY_USERNAME removed"
  95. exit 0