-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
233 lines (175 loc) · 4.49 KB
/
utils.go
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
package main
import (
"errors"
"fmt"
"log"
"math"
"regexp"
"strconv"
"strings"
"github.com/asdine/storm"
tb "gopkg.in/tucnak/telebot.v2"
)
func plural(count int64, singular string) (result string) {
if (count == 1) || (count == 0) {
result = fmt.Sprintf("%d %s", count, singular)
return
}
result = fmt.Sprintf("%d %ss", count, singular)
return
}
func secondsToHuman(input int64) (result string) {
years := math.Floor(float64(input) / 60 / 60 / 24 / 7 / 30 / 12)
seconds := input % (60 * 60 * 24 * 7 * 30 * 12)
months := math.Floor(float64(seconds) / 60 / 60 / 24 / 7 / 30)
seconds = input % (60 * 60 * 24 * 7 * 30)
weeks := math.Floor(float64(seconds) / 60 / 60 / 24 / 7)
seconds = input % (60 * 60 * 24 * 7)
days := math.Floor(float64(seconds) / 60 / 60 / 24)
seconds = input % (60 * 60 * 24)
hours := math.Floor(float64(seconds) / 60 / 60)
seconds = input % (60 * 60)
minutes := math.Floor(float64(seconds) / 60)
seconds = input % 60
if years > 0 {
result = fmt.Sprintf("%s, ", plural(int64(years), "year"))
}
if months > 0 {
result = fmt.Sprintf("%s%s, ", result, plural(int64(months), "month"))
}
if weeks > 0 {
result = fmt.Sprintf("%s%s, ", result, plural(int64(weeks), "week"))
}
if days > 0 {
result = fmt.Sprintf("%s%s, ", result, plural(int64(days), "day"))
}
if hours > 0 {
result = fmt.Sprintf("%s%s, ", result, plural(int64(hours), "hour"))
}
if minutes > 0 {
result = fmt.Sprintf("%s%s, ", result, plural(int64(minutes), "minute"))
}
if seconds > 0 {
result = fmt.Sprintf("%s%s", result, plural(int64(seconds), "second"))
}
return
}
func alreadyOnIntArray(arr []int64, value int64) bool {
for _, v := range arr {
if v == value {
return true
}
}
return false
}
func alreadyOnStringArray(arr []string, value string) bool {
for _, v := range arr {
if v == value {
return true
}
}
return false
}
func arrStringToString(arr []string) (message string) {
for _, v := range arr {
if len(message) == 0 {
message = fmt.Sprintf("'%s'", v)
} else {
message = fmt.Sprintf("%s, '%s'", message, v)
}
}
return
}
func removeFromIntArray(arr []int64, value int64) (newArr []int64) {
for _, v := range arr {
if v != value {
newArr = append(newArr, v)
}
}
return
}
func statusText(id int) string {
switch id {
case statusDown:
return "DOWN"
case statusUp:
return "UP"
case statusStarted:
return "STARTED"
case statusStopped:
return "STOPPED"
default:
return ""
}
}
func (u *urlTester) accessGranted(tbUser *tb.User) (authorized bool) {
_, authorized = u.userInfo(tbUser.ID)
return
}
func (u *urlTester) userInfo(id int64) (tgUser user, authorized bool) {
// on error user will be unauthorized (if the user didn't /start'ed)
if err := u.db.One("ID", id, &tgUser); err != nil {
u.bot.Send(telegramUser{id}, "Access not allowed. \nPlease use /start to ask for permissions.")
return
}
authorized = tgUser.Authorized
return
}
// isUserAdmin returns if the current user is allowed as administrator
func (u *urlTester) isUserAdmin(id int64) bool {
for _, adminID := range u.admins {
if adminID == id {
return true
}
}
return false
}
func (u *urlTester) sendMessageAndNotifyAdmins(userID int64, message string) {
log.Println(userID, ":", message)
u.bot.Send(telegramUser{userID}, message)
u.sendMessageToAdmins(message)
}
func (u *urlTester) sendMessageToAdmins(message string) {
for _, adminID := range u.admins {
u.bot.Send(telegramUser{adminID}, message)
}
}
func (u *urlTester) getScheduleByID(id int) (sched schedule, err error) {
err = u.db.One("ID", id, &sched)
if err != nil {
if err == storm.ErrNotFound {
err = errors.New("ID not found")
return
}
err = errors.New("unexpected error")
return
}
return
}
// evaluateTimeExp verifies that the expression to evaluate meets the requirements
// time expressions:
// (n)s = n seconds
// (n)m = n minutes
// (n)h = n hours
func evaluateTimeExp(exp string) (int, string, bool) {
var matcher = regexp.MustCompile(`^([0-9]+)([a-zA-Z])$`)
parts := matcher.FindAllStringSubmatch(exp, -1)
if len(parts) == 1 {
if len(parts[0]) == 3 {
i, _ := strconv.Atoi(parts[0][1])
switch parts[0][2] {
case "s", "S", "m", "M", "h", "H":
return i, strings.ToLower(parts[0][2]), true
default:
return 0, "", false
}
}
}
return 0, "", false
}
func headersToString(headers map[string]string) (headersString string) {
for k, v := range headers {
headersString = fmt.Sprintf("%s%s: %s\n", headersString, k, v)
}
return
}