forked from GAS85/nextcloud_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnextcloud-bot-time.sh
More file actions
185 lines (120 loc) · 3.49 KB
/
Copy pathnextcloud-bot-time.sh
File metadata and controls
185 lines (120 loc) · 3.49 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
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
#!/bin/bash
# By Georgiy Sitnikov.
#
# AS-IS without any warranty
#
# To added to Nextcloud please execute:
# sudo -u www-data php /var/www/nextcloud/occ talk:command:add time time "/usr/local/bin/nextcloud-bot-time.sh {ARGUMENTS}" 1 3
# More info under https://nextcloud-talk.readthedocs.io/en/latest/commands/
# Maximum Delay for API calls
maxDelay=5
# Temporary file
temp="/tmp/nextcloud-bot-time-$(date +"%m-%d-%N")"
###
while test $# -gt 0; do
case "$1" in
--help)
echo "/time - A Nextcloud Talk chat Time zone wrapper for a https://worldtimeapi.org/timezones API"
echo " "
echo "Simple execution: /time"
echo "will give you current time on the server"
echo " "
echo "Complex execution: /time TomeZone"
echo "E.g: /time Europe/Berlin"
echo " "
echo "You can get a full list of time zones via command:"
echo "/time --list"
echo " "
echo "Or added exact Location to search:"
echo "/time --list Europe"
exit 0
;;
*)
break
;;
esac
done
apiCall () {
if [ -z "$call" ]; then
curl -s -m $maxDelay "http://worldtimeapi.org/api/timezone.txt" > $temp
else
curl -s -m $maxDelay "http://worldtimeapi.org/api/timezone/"$call.txt > $temp
fi
}
APInewTry () {
newTry=$call
call=""
apiCall
call=$(grep "$newTry" $temp | head -n 1)
if [ ! -z "$call" ]; then
echo "Did you mean $call?"
apiCall
echo "Current local time in $call: "$(grep datetime $temp | tail -c33 | head -c10), $(grep datetime $temp | tail -c+21 | head -c8) $(grep abbreviation $temp | tail -c+14)
fi
}
checkForErrors () {
if [ "$(cat $temp| head -c 5)" = "Error" ]; then
awk '{ $1 = "Uuups, some error is here:"; print $0 }' $temp
APInewTry
echo " "
echo "For help, please, type /time --help"
rm $temp
exit 0
fi
# Added check if List returned
if [ "$(grep datetime $temp | tail -c33 | head -c10)" = "" ] && [ "$first" != "--list" ]; then
APInewTry
rm $temp
exit 0
fi
}
# Get first 6 Symbols of variable
first="$(echo $1 | head -c 6)"
rest="$(echo $1 | tail -c +8)"
#Added input Validator
#https://stackoverflow.com/questions/36926999/removing-all-special-characters-from-a-string-in-bash
validInput="$(echo "$rest" | sed 's/[^a-z A-Z 0-9]//g')"
if [ "$validInput" = "" ] && [ ! -z "$rest" ]; then
echo "Seems you use only Special Characters, currently only a-z, A-Z and digits are supported"
exit 0
fi
if [ "$first" = "--list" ]; then
apiCall
checkForErrors
if [ -z "$validInput" ]; then
echo "Here you can find whole list of Timezones:"
cat $temp
else
echo "Searching for your input in Time zones:"
#Check if multiple words are separated by spaces
case "$validInput" in
*\ * )
multipleInput=$(echo "$validInput" | tr ' ' '|')
#will display exact multiple match, or an error message
if ! grep -E "$multipleInput" "$temp"; then
echo "Hmmm, nothing was found"
echo "Try /time --list to see all Locations, or /time --help for help"
fi
;;
*)
#will display exact match, or an error message
if ! grep "$validInput" "$temp"; then
echo "Hmmm, nothing was found"
echo "Try /time --list to see all Locations, or /time --help for help"
fi
;;
esac
fi
rm $temp
exit 0
fi
if [ -z "$1" ]; then
echo "Current local time: $(date +"%Y-%m-%d"), $(date +"%T") $(date +"%Z")"
exit 0
fi
call=$1
apiCall
checkForErrors
echo "Current local time in $1: "$(grep datetime $temp | tail -c33 | head -c10), $(grep datetime $temp | tail -c+21 | head -c8) $(grep abbreviation $temp | tail -c+14)
rm $temp
exit 0