-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts3_messenger.sh
More file actions
executable file
·100 lines (93 loc) · 3.08 KB
/
ts3_messenger.sh
File metadata and controls
executable file
·100 lines (93 loc) · 3.08 KB
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
#!/bin/bash
#
### TeamSpeak 3 server messenger bash connector
#
# Requires: php, ts3 php scripts
. /opt/scripts/shlog.sh
. /opt/ts3scripts/.secrets
serverMessage="/opt/ts3scripts/server_message.php"
clientsOnline="/opt/ts3scripts/clients_online.php"
clientsList="/opt/ts3scripts/clients_list.php"
server="$1"
instance="$2"
statusFile="/tmp/ts3_messenger_status-$server-$instance.txt"
newFile="/tmp/ts3_messenger_new-$server-$instance.txt"
tmpFile="/tmp/ts3_messenger_tmp-$server-$instance.txt"
send_message (){
message="$1"
log="$2"
# Avoid anti-flood protection
sleep 5
# Send the message
result=$(php "$serverMessage" "$server" "$instance" "$message")
shlog -s datestamp "$log"
}
case "$3" in
'--only-new')
# Shift $4 to $3
shift
message="$3"
# Check if status file is empty
statusCount=$(cat $statusFile 2>/dev/null | wc -l)
if [[ $statusCount -eq 0 ]]; then
# Output current clients list to status file
php "$clientsList" "$server" "$instance" > $statusFile
# Check count after update
statusCount=$(cat $statusFile 2>/dev/null | wc -l)
if [[ $statusCount -ne 0 ]]; then
statusList=$(for line in `cat $statusFile`; do echo -n "$line "; done)
# Send the message
send_message "$message" "Sent to $statusCount client(s) ( $statusList) on $server server ($instance):\n $message"
else
shlog "No clients connected" -p nolog
exit 2
fi
else
# Output current clients list to tmp file
php "$clientsList" "$server" "$instance" > $newFile
# Check if current client list is empty
newCount=$(cat $newFile 2>/dev/null | wc -l)
if [[ $newCount -eq 0 ]]; then
shlog -s datestamp "Clients are no longer connected to the server"
cp -f $newFile $statusFile
else
# Check if files differ
diff -q $statusFile $newFile
if [[ $? -eq 1 ]]; then
# Save current clients in tmp file for later update of status file
cp -f $newFile $tmpFile
# Remove previous clients from current, <> used for exact matching
for line in $(cat $statusFile); do
sed -i "/\<$line\>/d" $newFile
done
# Update status file wirh current
mv -f $tmpFile $statusFile
# Check count after sed
newCount=$(cat $newFile 2>/dev/null | wc -l)
if [[ $newCount -ne 0 ]]; then
newList=$(for line in `cat $newFile`; do echo -n "$line "; done)
# Send the message
send_message "$message" "Sent to $newCount new client(s) ( $newList) on $server server ($instance):\n $message"
else
statusList=$(for line in `cat $statusFile`; do echo -n "$line "; done)
shlog -s datestamp "Connected clients ( $statusList) have already been notified"
fi
else
shlog -s datestamp "Previous and current clients are the same, not sending message"
fi
fi
fi
;;
*)
message="$3"
# Check if there are clients online
online_clients=$(php "$clientsOnline" "$server" "$instance")
if [[ $online_clients -ne 0 ]]; then
send_message "$message" "Sent to $online_clients client(s) on $server server ($instance):\n $message"
else
shlog "No clients connected" -p nolog
exit 2
fi
;;
esac
exit 0