-
Notifications
You must be signed in to change notification settings - Fork 1
/
iroffer.init
executable file
·315 lines (297 loc) · 7.45 KB
/
iroffer.init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/bash
### BEGIN INIT INFO
# Provides: iroffer
# Required-Start: $remote_fs $network $syslog $named
# Required-Stop: $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: iroffer xdcc bot
### END INIT INFO
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License version 2 for
# more details.
#
# You should have received a copy of the GNU General Public License version 2
# along with this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
DAEMON="/usr/local/bin/iroffer"
NAME="$( basename "${BASH_SOURCE[0]}" )"
USER=iroffer
DESC="xdcc bot"
ARGS="-b"
CONFDIR="/etc/$NAME/config"
TELNET_PORT="/etc/$NAME/telnet"
METACONFDIR="/etc/$NAME/"
PIDDIR="/etc/$NAME/pid"
[ -f "$CONFDIR/$NAME.default" ] && . $CONFDIR/$NAME.default
if [[ $CHROOT = "true" ]]; then
ARGS="$ARGS -t $CHROOT_DIR -u $CHROOT_USER $CHROOT_CONFDIR"
USER=root
CONFDIR="${CHROOT_DIR}${CHROOT_CONFDIR}"
else
ARGS="$ARGS $CONFDIR"
fi
. /lib/lsb/init-functions
if [ ! -e "$PIDDIR" ];then
mkdir -p "$PIDDIR"
if [[ $CHROOT = "true" ]]; then
chown $CHROOT_USER "$PIDDIR"
else
chown $USER "$PIDDIR"
fi
fi
if [ ! -e "$CONFDIR" ];then
mkdir -p "$CONFDIR"
fi
if [ ! -e "$TELNET_PORT" ];then
mkdir -p "$TELNET_PORT"
fi
need_root(){
if [ $UID -ne 0 ]; then
log_failure_msg "Operation need root rights"
exit 1
fi
}
telnet_iroffer(){
BOTNAME=$1
if [ -f "$TELNET_PORT/$BOTNAME.port" ]; then
log_daemon_msg "Starting telnet console for $BOTNAME"
CMD="nc 127.0.0.1 `cat \"$TELNET_PORT/$BOTNAME.port\"`"
if which rlwrap >/dev/null; then
rlwrap --command-name telnet_$BOTNAME $CMD
else
$CMD
fi
else
log_failure_msg "File $TELNET_PORT/$BOTNAME.port missing"
fi
}
genconf_iroffer(){
BOTNAME=$1
if [ -f $METACONFDIR/$BOTNAME.py ]; then
log_daemon_msg "Generating config for $BOTNAME"
$METACONFDIR/$BOTNAME.py -t "$TELNET_PORT/$BOTNAME.port" > $CONFDIR/$BOTNAME.config
log_end_msg $?
else
log_failure_msg " File $METACONFDIR/$BOTNAME.py missing"
fi
}
start_iroffer(){
BOTNAME=$1
need_root
# see if stale pid file
if [ -f $PIDDIR/${BOTNAME}.pid ]; then
pid=`cat $PIDDIR/${BOTNAME}.pid`
if [ `ps -p $pid | wc -l` -eq 2 ]; then
log_failure_msg "$BOTNAME already running"
return
else
log_warning_msg "Stale PID File for $BOTNAME"
fi
fi
if [ "$CHROOT" = "true" ] && [ -n "$CHROOT_EXEC" ]; then
eval "$CHROOT_EXEC"
fi
log_daemon_msg "Starting iroffer $BOTNAME..."
/bin/su -l $USER -c "$DAEMON $ARGS/$BOTNAME.config"
}
stop_iroffer(){
BOTNAME=$1
need_root
if [ -f $PIDDIR/${BOTNAME}.pid ]; then
pid=`cat $PIDDIR/${BOTNAME}.pid`
echo -n " * Stopping $NAME $BOTNAME"
for i in `seq 30`; do
echo -n "."
if [ `ps -p $pid | wc -l` -eq 2 ]; then
kill $pid
else
log_end_msg 0
return
fi
sleep 1
done
log_end_msg 1
else
log_warning_msg "$PIDDIR/${BOTNAME}.pid no found"
fi
}
reload_iroffer(){
BOTNAME=$1
need_root
if [ -f $PIDDIR/${BOTNAME}.pid ]; then
pid=`cat $PIDDIR/${BOTNAME}.pid`
log_daemon_msg "Reloading config of $BOTNAME"
kill -n 12 $pid
else
log_failure_msg "$PIDDIR/${BOTNAME}.pid no found"
fi
}
reconnect_iroffer(){
BOTNAME=$1
need_root
if [ -f $PIDDIR/${BOTNAME}.pid ]; then
pid=`cat $PIDDIR/${BOTNAME}.pid`
log_daemon_msg "Reconnecting $BOTNAME"
kill -n 10 $pid
else
log_failure_msg "$PIDDIR/${BOTNAME}.pid no found"
fi
}
start(){
# make sure filesystem isn't full
freespace=`df -k . | tail -1 | awk {'print $4'}`
if [ $freespace -lt 10 ]; then
log_failure_msg "Filesystem Full!"
exit
fi
if test -z "$2" ; then
for BOT in `cd $CONFDIR; ls *.config 2> /dev/null`; do
BOTNAME=${BOT%%.config}
start_iroffer $BOTNAME
done
else
while shift ; do
[ -z "$1" ] && break
BOTNAME=$1
if test -e $CONFDIR/$BOTNAME.config ; then
start_iroffer $BOTNAME
else
log_failure_msg "$BOTNAME, missing $CONFDIR/$BOTNAME.config"
fi
done
fi
}
stop(){
if test -z "$2" ; then
for BOT in `cd $CONFDIR; ls *.config 2> /dev/null`; do
BOTNAME=${BOT%%.config}
stop_iroffer $BOTNAME
done
else
while shift ; do
[ -z "$1" ] && break
BOTNAME=$1
if test -e $CONFDIR/$BOTNAME.config ; then
stop_iroffer $BOTNAME
else
log_failure_msg "$BOTNAME, missing $CONFDIR/$BOTNAME.config"
fi
done
fi
}
restart(){
# make sure filesystem isn't full
freespace=`df -k . | tail -1 | awk {'print $4'}`
if [ $freespace -lt 10 ]; then
log_failure_msg "Filesystem Full!"
exit
fi
if test -z "$2" ; then
for BOT in `cd $CONFDIR; ls *.config 2> /dev/null`; do
BOTNAME=${BOT%%.config}
stop_iroffer $BOTNAME
start_iroffer $BOTNAME
done
else
while shift ; do
[ -z "$1" ] && break
BOTNAME=$1
if test -e $CONFDIR/$BOTNAME.config ; then
stop_iroffer $BOTNAME
start_iroffer $BOTNAME
else
log_failure_msg "$BOTNAME, missing $CONFDIR/$BOTNAME.config"
fi
done
fi
}
status(){
if test -z "$2" ; then
for BOT in `cd $CONFDIR; ls *.config 2> /dev/null`; do
BOTNAME=${BOT%%.config}
status_of_proc -p "$PIDDIR/$BOTNAME.pid" "$DAEMON" "$DESC $BOTNAME"
done
else
while shift ; do
[ -z "$1" ] && break
BOTNAME=$1
status_of_proc -p "$PIDDIR/$BOTNAME.pid" "$DAEMON" "$DESC $BOTNAME"
done
fi
}
genconf(){
if test -z "$2" ; then
for BOT in `cd $CONFDIR; ls *.config 2> /dev/null`; do
BOTNAME=${BOT%%.config}
genconf_iroffer $BOTNAME
done
else
while shift ; do
[ -z "$1" ] && break
BOTNAME=$1
genconf_iroffer $BOTNAME
done
fi
}
case $1 in
start)
start $@;
;;
stop)
stop $@
;;
restart)
restart $@
;;
reload)
if test -z "$2" ; then
for BOT in `cd $CONFDIR; ls *.config 2> /dev/null`; do
BOTNAME=${BOT%%.config}
reload_iroffer $BOTNAME
done
else
while shift ; do
[ -z "$1" ] && break
BOTNAME=$1
if test -e $CONFDIR/$BOTNAME.config ; then
reload_iroffer $BOTNAME
else
log_failure_msg "$BOTNAME, missing $CONFDIR/$BOTNAME.config"
fi
done
fi
;;
reconnect)
if test -z "$2" ; then
for BOT in `cd $CONFDIR; ls *.config 2> /dev/null`; do
BOTNAME=${BOT%%.config}
reconnect_iroffer $BOTNAME
done
else
while shift ; do
[ -z "$1" ] && break
BOTNAME=$1
if test -e $CONFDIR/$BOTNAME.config ; then
reconnect_iroffer $BOTNAME
else
log_failure_msg "$BOTNAME, missing $CONFDIR/$BOTNAME.config"
fi
done
fi
;;
status)
status $@
;;
genconf)
genconf $@;
;;
telnet)
telnet_iroffer $2;
;;
*)
log_failure_msg "Usage: $0 {start|stop|restart|reload|status|reconnect|genconf|telnet} [`cd $CONFDIR; ls -m *.config 2> /dev/null | sed 's/\.config//g;s/, /|/g'`]"
exit 1
;;
esac