|
@@ -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
|