Преглед на файлове

Command to add/edit sip user

Bob Mottram преди 9 години
родител
ревизия
c4bfb48415
променени са 2 файла, в които са добавени 128 реда и са изтрити 0 реда
  1. 3
    0
      Makefile
  2. 125
    0
      src/freedombone-addsipuser

+ 3
- 0
Makefile Целия файл

@@ -41,6 +41,7 @@ install:
41 41
 	install -m 755 src/${APP}-controlpanel ${DESTDIR}${PREFIX}/bin
42 42
 	install -m 755 src/${APP}-controlpanel ${DESTDIR}${PREFIX}/bin/control
43 43
 	install -m 755 src/${APP}-logging ${DESTDIR}${PREFIX}/bin
44
+	install -m 755 src/${APP}-addsipuser ${DESTDIR}${PREFIX}/bin
44 45
 	mkdir -m 755 -p ${DESTDIR}${PREFIX}/share/man/man1
45 46
 	install -m 644 man/${APP}.1.gz ${DESTDIR}${PREFIX}/share/man/man1
46 47
 	install -m 644 man/${APP}-keydrive.1.gz ${DESTDIR}${PREFIX}/share/man/man1
@@ -95,6 +96,7 @@ uninstall:
95 96
 	rm -f ${PREFIX}/share/man/man1/${APP}-mesh.1.gz
96 97
 	rm -f ${PREFIX}/share/man/man1/${APP}-controlpanel.1.gz
97 98
 	rm -f ${PREFIX}/share/man/man1/${APP}-logging.1.gz
99
+	rm -f ${PREFIX}/share/man/man1/${APP}-addsipuser.1.gz
98 100
 	rm -rf ${PREFIX}/share/${APP}
99 101
 	rm -f ${PREFIX}/bin/${APP}
100 102
 	rm -f ${PREFIX}/bin/zeronetavahi
@@ -125,6 +127,7 @@ uninstall:
125 127
 	rm -f ${PREFIX}/bin/meshweb
126 128
 	rm -f ${PREFIX}/bin/${APP}-controlpanel
127 129
 	rm -f ${PREFIX}/bin/${APP}-logging
130
+	rm -f ${PREFIX}/bin/${APP}-addsipuser
128 131
 clean:
129 132
 	rm -f \#* \.#* debian/*.substvars debian/*.log
130 133
 	rm -fr deb.* debian/${APP}

+ 125
- 0
src/freedombone-addsipuser Целия файл

@@ -29,4 +29,129 @@
29 29
 # You should have received a copy of the GNU General Public License
30 30
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
31 31
 
32
+MY_USERNAME=
33
+EXTENSION=
34
+PASSWORD=
35
+CONFIG_FILE=/etc/sipwitch.conf
36
+USER_EXISTS="no"
37
+
38
+function show_help {
39
+    echo ''
40
+    echo 'freedombone-addsipuser -u [username] -e [extension] -p [password]'
41
+    echo ''
42
+    exit 0
43
+}
44
+
45
+function sip_user_exists {
46
+    for line in $ (cat $CONFIG_FILE)
47
+    do
48
+        if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then
49
+            USER_EXISTS="yes"
50
+            return
51
+        fi
52
+    done
53
+}
54
+
55
+function update_sip_user {
56
+    USER_FOUND=
57
+    NEW_CONFIG_FILE="${CONFIG_FILE}.new"
58
+    if [ -f $NEW_CONFIG_FILE ]; then
59
+        rm -f $NEW_CONFIG_FILE
60
+    fi
61
+    touch $NEW_CONFIG_FILE
62
+    for line in $ (cat $CONFIG_FILE)
63
+    do
64
+        if [ ! $USER_FOUND ]; then
65
+            if [[ "$line" == "<user id=\"$MY_USERNAME\">" ]]; then
66
+                USER_FOUND="yes"
67
+            fi
68
+        else
69
+            if [[ "$line" == "<extension>"* ]]; then
70
+                line="<extension>$EXTENSION</extension>"
71
+            fi
72
+            if [[ "$line" == "<secret>"* ]]; then
73
+                line="<secret>$PASSWORD</secret>"
74
+            fi              
75
+            if [[ "$line" == "<display>"* ]]; then
76
+                line="<display>$MY_USERNAME $EXTENSION</display>"
77
+                USER_FOUND=
78
+            fi              
79
+        fi
80
+        echo "$line" >> $NEW_CONFIG_FILE
81
+    done
82
+    mv $NEW_CONFIG_FILE $CONFIG_FILE
83
+}
84
+
85
+function add_sip_user {
86
+    NEW_CONFIG_FILE="${CONFIG_FILE}.new"
87
+    if [ -f $NEW_CONFIG_FILE ]; then
88
+        rm -f $NEW_CONFIG_FILE
89
+    fi
90
+    touch $NEW_CONFIG_FILE
91
+    for line in $ (cat $CONFIG_FILE)
92
+    do
93
+        if [[ "$line" == '</provision>' ]]; then
94
+            echo "<user id=\"$MY_USERNAME\">" >> $NEW_CONFIG_FILE
95
+            echo "<extension>$EXTENSION</extension>" >> $NEW_CONFIG_FILE
96
+            echo "<secret>$PASSWORD</secret>" >> $NEW_CONFIG_FILE
97
+            echo "<display>$MY_USERNAME $EXTENSION</display>" >> $NEW_CONFIG_FILE
98
+            echo '</user>' >> $NEW_CONFIG_FILE
99
+        fi
100
+        echo "$line" >> $NEW_CONFIG_FILE
101
+    done
102
+    mv $NEW_CONFIG_FILE $CONFIG_FILE
103
+}
104
+
105
+while [[ $# > 1 ]]
106
+do
107
+key="$1"
108
+
109
+case $key in
110
+    -h|--help)
111
+    show_help
112
+    ;;
113
+    -u|--user)
114
+    shift
115
+    MY_USERNAME="$1"
116
+    ;;
117
+    -e|--extension)
118
+    shift
119
+    EXTENSION="$1"
120
+    ;;
121
+    -p|--password)
122
+    shift
123
+    PASSWORD="$1"
124
+    ;;
125
+    *)
126
+    # unknown option
127
+    ;;
128
+esac
129
+shift
130
+done
131
+
132
+if ! [[ $MY_USERNAME && $EXTENSION && $PASSWORD ]]; then
133
+    show_help
134
+fi
135
+
136
+if [ ! -f $CONFIG_FILE ]; then
137
+    echo "SIP configuration file not found"
138
+    exit 1
139
+fi
140
+
141
+# the user must already exist on the system
142
+if [ ! -d /home/$MY_USERNAME ]; then
143
+    echo "User $MY_USERNAME not found"
144
+    exit 2
145
+fi
146
+
147
+sip_user_exists
148
+
149
+if [[ USER_EXISTS == "yes" ]]; then
150
+    update_sip_user
151
+else
152
+    add_sip_user
153
+fi
154
+
155
+service sipwitch restart
156
+
32 157
 exit 0