forked from see-base/spaceapi-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
togglestate
executable file
·122 lines (116 loc) · 4.99 KB
/
togglestate
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
#!/bin/bash
saveIFS=$IFS
IFS='=&'
parm=($QUERY_STRING)
IFS=$saveIFS
for ((i=0; i<${#parm[@]}; i+=2))
do
declare var_${parm[i]}=${parm[i+1]}
done
echo
version="0.20"
# Now with GitHub :D
function getstate {
currentstate=$(grep open $1)
if [[ $currentstate == *"true"* ]]; then
echo -e "\t\"status\": \"open\","
elif [[ $currentstate == *"false"* ]]; then
echo -e "\t\"status\": \"closed\","
fi
}
function changestate {
if ! [ -z $var_text ]; then var_state=$var_text; fi
echo -e "{\n\t\"space\": \"$var_space\"," # Create JSON
if [[ $(getstate $3) = $var_state || $var_state = "show" ]] ; then
if [[ $(getstate $3) = $var_state ]]; then
echo -e "\t\"state\": \"no change\",\n"
fi
echo "$(getstate $3)"
else
if [ $var_token = $1 ]; then
if [ $var_state = "open" ]; then
sed -i s/'"open": false'/'"open": true'/g $3
sed -i "s/.*lastchange.*/ \"lastchange\": $(date +%s)/" $3
sed -i s/CLOSED/OPEN/g $4
echo -n "change" >/dev/udp/$5
echo -e "\t\"status\": \"open\","
if [ -n $6 ]; then
(exec "$6" "open")
fi
elif [ $var_state = "closed" ]; then
sed -i s/'"open": true'/'"open": false'/g $3
sed -i "s/.*lastchange.*/ \"lastchange\": $(date +%s)/" $3
sed -i s/OPEN/CLOSED/g $4
echo -n "change" >/dev/udp/$5
echo -e "\t\"status\": \"closed\","
if [ -n $6 ]; then
(exec "$6" "closed")
fi
elif [ $var_state = "help" ]; then
echo -e "\t\"available_commands\": {\n\t\t\"open\": \"Set status to 'open'\",\n\t\t\"closed\": \"Set status to closed\",\n\t\t\"show\": \"Show current space status\",\n\t\t\"help\": \"Show this help\"\n\t},"
else
echo -e "\t\"status\": \"unchanged because of wrong given state information\",\n\t\"hint\": \"try 'help' as parameter for available options\","
fi
echo -e "\t\"access\": \"authorized\","
else
echo -e "\t\"access\": \"unauthorized\","
echo -e "\t\"error\": {\n\t\t\"reason\": \"invalid token\",\n\t\t\"token_used\": \"$var_token\"\n\t},"
fi
fi
echo -e "\t\"version\": \"$version\"\n}\n"
}
# Die Konfiguration wird aus einer Seperaten nicht mit dem git gesyncten CSV Datei gelesen:
IFS='|'
declare -a config=( $( cat $(dirname $0)/spaceapi-server/config.csv ) )
IFS=$saveIFS
case $var_space in
${config[0]})
# Eintrag fuer den IRC Bot: !see-base ["", "open", "closed"]
token="${config[1]}"
jsonfilename="${config[2]}"
mapfilename="${config[3]}"
returnhost="${config[4]}"
externalscript="${config[5]}"
changestate $token $var_space $jsonfilename $mapfilename $returnhost $externalscript
;;
${config[6]})
# Eintrag fuer den Slack channel: '/see-base show' s/show/open|closed|help/g
token="${config[7]}"
jsonfilename="${config[8]}"
mapfilename="${config[9]}"
returnhost="${config[10]}"
externalscript="${config[11]}"
changestate $token $var_space $jsonfilename $mapfilename $returnhost $externalscript
;;
${config[12]})
# Eintrag für den IRC Bot: !toolbox ["", "open", "closed"]
token="${config[13]}"
jsonfilename="${config[14]}"
mapfilename="${config[15]}"
returnhost="${config[16]}"
externalscript="${config[17]}"
changestate $token $var_space $jsonfilename $mapfilename $returnhost $externalscript
;;
${config[18]})
# Eintrag für den Slack Channel: '/space show' s/show/open|closed|help/g
token="${config[19]}"
jsonfilename="${config[20]}"
mapfilename="${config[21]}"
returnhost="${config[22]}"
externalscript="${config[23]}"
changestate $token $var_space $jsonfilename $mapfilename $returnhost $externalscript
;;
${config[24]})
# Eintrag für den Slack Channel: '/space show' s/show/open|closed|help/g
token="${config[25]}"
jsonfilename="${config[26]}"
mapfilename="${config[27]}"
returnhost="${config[28]}"
externalscript="${config[29]}"
changestate $token $var_space $jsonfilename $mapfilename $returnhost $externalscript
;;
*)
# Falls ein ungueltiger Space verwendet wird...
echo -e "{\n\t\"space\": \"no such space\",\n\t\"error\"[\n\t\t\"reason\": \"Space does not exisit in this script\",\n\t\t\"asked_for\": \"$var_space\"\n\t],\n\t\"version\": \"$version\"\n}"
;;
esac