-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
283 lines (274 loc) · 7.99 KB
/
commands.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
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
package main
import (
"fmt"
//"regexp"
"sort"
"strconv"
"strings"
tb "github.com/go-telegram-bot-api/telegram-bot-api"
pb "github.com/usher2/u2byckdump/msg"
)
func botUpdates(c pb.CheckClient, bot *tb.BotAPI, updatesChan tb.UpdatesChannel) {
for {
select {
case update := <-updatesChan:
switch {
case update.Message != nil: // ignore any non-Message Updates
if update.Message.Text != "" {
if update.Message.Chat.Type == "private" ||
(update.Message.ReplyToMessage == nil &&
update.Message.ForwardFromMessageID == 0) {
var uname string
// who writing
if update.Message.From != nil {
uname = update.Message.From.UserName
}
// chat/dialog
chat := update.Message.Chat
go Talks(c, bot, uname, chat, "", 0, "", update.Message.Text)
}
}
case update.CallbackQuery != nil:
var (
uname string
req string
)
// who writing
if update.CallbackQuery.From != nil {
uname = update.CallbackQuery.From.UserName
}
// chat/dialog
var chat *tb.Chat
if update.CallbackQuery.Message != nil {
chat = update.CallbackQuery.Message.Chat
i := strings.IndexByte(update.CallbackQuery.Message.Text, '\n')
if i > 0 {
switch {
case strings.HasPrefix(update.CallbackQuery.Message.Text[:i], "\U0001f525 ") &&
strings.HasSuffix(update.CallbackQuery.Message.Text[:i], " заблокирован"):
req = strings.TrimSuffix(strings.TrimPrefix(update.CallbackQuery.Message.Text[:i], "\U0001f525 "), " заблокирован")
case strings.Contains(update.CallbackQuery.Message.Text[:i], "/n_"):
j1 := strings.Index(update.CallbackQuery.Message.Text[:i], "/n_")
j2 := strings.IndexByte(update.CallbackQuery.Message.Text[j1:i], ' ')
if j2 != -1 {
req = update.CallbackQuery.Message.Text[j1 : j1+j2]
}
}
}
}
go bot.AnswerCallbackQuery(tb.NewCallback(update.CallbackQuery.ID, "")) // for some reason
go Talks(c, bot, uname, chat, "", update.CallbackQuery.Message.MessageID, update.CallbackQuery.Data, req)
case update.InlineQuery != nil:
if update.InlineQuery.Query != "" {
var uname string
// who writing
if update.InlineQuery.From != nil {
uname = update.InlineQuery.From.UserName
}
go Talks(c, bot, uname, nil, update.InlineQuery.ID, 0, "", update.InlineQuery.Query)
}
}
}
}
}
var noAdCount int = 0
const NO_AD_NUMBER = 20
func makePagination(offset TPagination, pages []TPagination) tb.InlineKeyboardMarkup {
var (
keyboard [][]tb.InlineKeyboardButton
o int
pict string
)
sort.Slice(pages, func(i, j int) bool {
return pages[i].Tag < pages[j].Tag
})
for i := range pages {
curTag := pages[i].Tag
if pages[i].Count > PRINT_LIMIT {
row := tb.NewInlineKeyboardRow()
if offset.Tag != curTag {
o = 0
} else {
o = offset.Count
}
slug := strconv.Itoa(o/PRINT_LIMIT + 1)
switch curTag {
case OFFSET_DOMAIN:
slug = "домен"
case OFFSET_URL:
slug = "URL"
case OFFSET_IP4:
slug = "IPv4"
}
if pages[i].Count > 2*PRINT_LIMIT {
pict = "\u23ea"
if o == 0 {
pict = "\U000023f9"
}
row = append(row,
tb.NewInlineKeyboardButtonData(fmt.Sprintf("%d %s", 1, pict),
fmt.Sprintf("%d:%d", curTag, 0)),
)
}
_o := o - PRINT_LIMIT
if _o < 0 {
_o = 0
}
pict = "\u23ee"
if o == 0 {
pict = "\U000023f9"
}
row = append(row,
tb.NewInlineKeyboardButtonData(fmt.Sprintf("%d %s", _o/PRINT_LIMIT+1, pict),
fmt.Sprintf("%d:%d", curTag, _o)),
)
row = append(row,
tb.NewInlineKeyboardButtonData(fmt.Sprintf("\u2022 %s \u2022", slug),
fmt.Sprintf("%d:%d", curTag, o)),
)
_o = o + PRINT_LIMIT
if _o > pages[i].Count-(pages[i].Count%PRINT_LIMIT) {
_o = pages[i].Count - (pages[i].Count % PRINT_LIMIT)
}
if _o == pages[i].Count {
_o -= PRINT_LIMIT
}
pict = "\u23ed"
if o >= _o {
pict = "\U000023f9"
}
_p := _o/PRINT_LIMIT + 1
row = append(row,
tb.NewInlineKeyboardButtonData(fmt.Sprintf("%s %d", pict, _p),
fmt.Sprintf("%d:%d", curTag, _o)),
)
if pages[i].Count > 2*PRINT_LIMIT {
_o = pages[i].Count - (pages[i].Count % PRINT_LIMIT)
if _o == pages[i].Count {
_o -= PRINT_LIMIT
}
_p = _o/PRINT_LIMIT + 1
pict = "\u23e9"
if o >= _o {
pict = "\U000023f9"
}
row = append(row,
tb.NewInlineKeyboardButtonData(fmt.Sprintf("%s %d", pict, _p),
fmt.Sprintf("%d:%d", curTag, _o)),
)
}
keyboard = append(keyboard, row)
}
}
return tb.InlineKeyboardMarkup{
InlineKeyboard: keyboard,
}
}
func sendMessage(bot *tb.BotAPI, chat *tb.Chat, inlineId string, messageId int, text string, offset TPagination, pages []TPagination) {
if chat != nil {
if noAdCount >= NO_AD_NUMBER {
text += "--- \n" + DonateFooter
noAdCount = 0
} else {
//text += Footer
noAdCount++
}
if messageId > 0 {
msg := tb.NewEditMessageText(chat.ID, messageId, text)
msg.ParseMode = tb.ModeMarkdown
msg.DisableWebPagePreview = true
inlineKeyboard := makePagination(offset, pages)
if len(inlineKeyboard.InlineKeyboard) > 0 {
msg.ReplyMarkup = &inlineKeyboard
}
_, err := bot.Send(msg)
if err != nil {
Warning.Printf("Error sending message: %s\n", err.Error())
}
} else {
msg := tb.NewMessage(chat.ID, text)
msg.ParseMode = tb.ModeMarkdown
msg.DisableWebPagePreview = true
inlineKeyboard := makePagination(offset, pages)
if len(inlineKeyboard.InlineKeyboard) > 0 {
msg.ReplyMarkup = inlineKeyboard
}
_, err := bot.Send(msg)
if err != nil {
Warning.Printf("Error sending message: %s\n", err.Error())
}
}
} else if inlineId != "" {
article := tb.InlineQueryResultArticle{
ID: inlineId,
Title: "Search result",
Type: "article",
InputMessageContent: tb.InputTextMessageContent{
Text: text + Footer,
ParseMode: tb.ModeMarkdown,
DisableWebPagePreview: true,
},
}
inlineConf := tb.InlineConfig{
InlineQueryID: inlineId,
Results: []interface{}{article},
}
if _, err := bot.AnswerInlineQuery(inlineConf); err != nil {
Warning.Printf("Error sending answer: %s\n", err.Error())
}
}
}
// Handle commands
func Talks(c pb.CheckClient, bot *tb.BotAPI, uname string, chat *tb.Chat, inlineId string, messageId int, callbackData, text string) {
var (
reply string
pages []TPagination
offset TPagination
)
if callbackData != "" && strings.IndexByte(callbackData, ':') != -1 {
i := strings.IndexByte(callbackData, ':')
if i != len(callbackData)-1 {
offset.Tag, _ = strconv.Atoi(callbackData[:i])
offset.Count, _ = strconv.Atoi(callbackData[i+1:])
}
}
//log.Printf("[%s] %d %s", UserName, ChatID, Text)
if i := strings.IndexByte(text, '\n'); i != -1 {
text = strings.Trim(text[:i], " ")
}
switch {
case text == "":
reply = "\U0001f440 Нечего искать\n"
case text == "/help":
reply = HelpMessage
case text == "/helpen":
reply = HelpMessageEn
case text == "/donate":
reply = DonateMessage
case text == "/start":
reply = "Вітаю цябе, " + Sanitize(uname) + "!\n"
case text == "/ping":
reply = Ping(c)
case text == "/ck" || text == "/check":
reply = HelpMessage
case strings.HasPrefix(text, "/ck ") || strings.HasPrefix(text, "/check "):
reply, pages = mainSearch(c, strings.TrimPrefix(strings.TrimPrefix(text, "/ck "), "/check "), offset)
case strings.HasPrefix(text, "/n_") || strings.HasPrefix(text, "#"):
args := ""
if strings.HasPrefix(text, "/n_") {
args = strings.TrimPrefix(text, "/n_")
} else if strings.HasPrefix(text, "#") {
args = strings.TrimPrefix(text, "#")
}
reply, pages = numberSearch(c, args, offset)
case strings.HasPrefix(text, "/"):
reply = "\U0001f523 iNJALID DEJICE\n"
default:
reply, pages = mainSearch(c, text, offset)
}
if reply == "" {
reply = "\U0001f463 Ничего не нашлось\n"
}
//Debug.Printf("************ %s", reply)
sendMessage(bot, chat, inlineId, messageId, reply, offset, pages)
}