-
Notifications
You must be signed in to change notification settings - Fork 22
/
commands.go
272 lines (227 loc) · 7.45 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
package main
import (
"fmt"
"log"
"sort"
"strings"
"sync"
"github.com/msoap/raphanus"
)
// Ctx - context for bot command function (users, command, args, ...)
type Ctx struct {
appConfig *Config // configuration
users *Users // all users
commands Commands // all chat commands
userID int // current user
allowExec bool // is user authorized
messageCmd string // command name
messageArgs string // command arguments
messageSignal chan<- BotMessage // for send telegram messages
chatID int // chat for send replay
exitSignal chan<- struct{} // for signal for terminate bot
cache *raphanus.DB // cache for commands output
cacheTTL int // cache timeout
oneThreadMutex *sync.Mutex // mutex for run shell commands in one thread
}
// /auth and /authroot - authorize users
func cmdAuth(ctx Ctx) (replayMsg string) {
forRoot := ctx.messageCmd == "/authroot"
if ctx.messageArgs == "" {
replayMsg = "See code in terminal with shell2telegram or ask code from root user and type:\n" + ctx.messageCmd + " code"
authCode := ctx.users.DoLogin(ctx.userID, forRoot)
rootRoleStr := ""
if forRoot {
rootRoleStr = "root "
}
secretCodeMsg := fmt.Sprintf("Request %saccess for %s. Code: %s\n", rootRoleStr, ctx.users.String(ctx.userID), authCode)
fmt.Print(secretCodeMsg)
ctx.users.BroadcastForRoots(ctx.messageSignal, secretCodeMsg, 0)
} else {
if ctx.users.IsValidCode(ctx.userID, ctx.messageArgs, forRoot) {
ctx.users.SetAuthorized(ctx.userID, forRoot)
if forRoot {
replayMsg = fmt.Sprintf("You (%s) authorized as root.", ctx.users.String(ctx.userID))
log.Print("root authorized: ", ctx.users.String(ctx.userID))
} else {
replayMsg = fmt.Sprintf("You (%s) authorized.", ctx.users.String(ctx.userID))
log.Print("authorized: ", ctx.users.String(ctx.userID))
}
} else {
replayMsg = "Code is not valid."
}
}
return replayMsg
}
// /help
func cmdHelp(ctx Ctx) (replayMsg string) {
helpMsg := []string{}
if ctx.allowExec {
for cmd, shellCmdRow := range ctx.commands {
description := shellCmdRow.description
if description == "" {
description = shellCmdRow.shellCmd
}
helpMsg = append(helpMsg, cmd+" → "+description)
}
}
sort.Strings(helpMsg)
if !ctx.appConfig.isPublicBot {
helpMsg = append(helpMsg,
"/auth [code] → authorize user",
"/authroot [code] → authorize user as root",
)
}
if ctx.users.IsRoot(ctx.userID) {
helpMsgForRoot := []string{
"/shell2telegram ban <user_id|username> → ban user",
"/shell2telegram broadcast_to_root <message> → send message to all root users in private chat",
"/shell2telegram desc <bot description> → set bot description",
"/shell2telegram message_to_user <user_id|username> <message> → send message to user in private chat",
"/shell2telegram rm </command> → delete command",
"/shell2telegram search <query> → search users by name/id",
"/shell2telegram stat → get stat about users",
"/shell2telegram version → show version",
}
if ctx.appConfig.addExit {
helpMsgForRoot = append(helpMsgForRoot, "/shell2telegram exit → terminate bot")
}
sort.Strings(helpMsgForRoot)
helpMsg = append(helpMsg, helpMsgForRoot...)
}
if ctx.appConfig.description != "" {
replayMsg = ctx.appConfig.description
} else {
replayMsg = "This bot created with shell2telegram"
}
replayMsg += "\n\n" +
"available commands:\n" +
strings.Join(helpMsg, "\n")
return replayMsg
}
// all commands from command-line
func cmdUser(ctx Ctx) {
if cmd, found := ctx.commands[ctx.messageCmd]; found {
go func() {
if ctx.appConfig.oneThread {
ctx.oneThreadMutex.Lock()
}
replayMsgRaw := execShell(
cmd.shellCmd,
ctx.messageArgs,
ctx.commands[ctx.messageCmd].vars,
ctx.userID,
ctx.chatID,
ctx.users.list[ctx.userID].UserName,
ctx.users.list[ctx.userID].FirstName+" "+ctx.users.list[ctx.userID].LastName,
ctx.cache,
ctx.cacheTTL,
ctx.appConfig,
)
if ctx.appConfig.oneThread {
ctx.oneThreadMutex.Unlock()
}
sendMessage(ctx.messageSignal, ctx.chatID, replayMsgRaw, cmd.isMarkdown)
}()
}
}
// /shell2telegram stat
func cmdShell2telegramStat(ctx Ctx) (replayMsg string) {
for userID := range ctx.users.list {
replayMsg += ctx.users.StringVerbose(userID) + "\n"
}
return replayMsg
}
// /shell2telegram search
func cmdShell2telegramSearch(ctx Ctx) (replayMsg string) {
query := ctx.messageArgs
if query == "" {
return "Please set query: /shell2telegram search <query>"
}
for _, userID := range ctx.users.Search(query) {
replayMsg += ctx.users.StringVerbose(userID) + "\n"
}
return replayMsg
}
// /shell2telegram ban
func cmdShell2telegramBan(ctx Ctx) (replayMsg string) {
userName := ctx.messageArgs
if userName == "" {
return "Please set user_id or login: /shell2telegram ban <user_id|username>"
}
userID := ctx.users.FindByIDOrUserName(userName)
if userID > 0 && ctx.users.BanUser(userID) {
replayMsg = fmt.Sprintf("User %s banned", ctx.users.String(userID))
} else {
replayMsg = "User not found"
}
return replayMsg
}
// set bot description
func cmdShell2telegramDesc(ctx Ctx) (replayMsg string) {
description := ctx.messageArgs
if description == "" {
return "Please set description: /shell2telegram desc <bot description>"
}
ctx.appConfig.description = description
replayMsg = "Bot description set to: " + description
return replayMsg
}
// /shell2telegram rm "/command" - delete command
func cmdShell2telegramRm(ctx Ctx) (replayMsg string) {
commandName := ctx.messageArgs
if commandName == "" {
return "Please set command for delete: /shell2telegram rm </command>"
}
if _, ok := ctx.commands[commandName]; ok {
delete(ctx.commands, commandName)
replayMsg = "Deleted command: " + commandName
} else {
replayMsg = fmt.Sprintf("Command %s not found", commandName)
}
return replayMsg
}
// /shell2telegram version - get version
func cmdShell2telegramVersion(_ Ctx) (replayMsg string) {
replayMsg = fmt.Sprintf("shell2telegram %s", version)
return replayMsg
}
// /shell2telegram exit - terminate bot
func cmdShell2telegramExit(ctx Ctx) (replayMsg string) {
if ctx.appConfig.addExit {
replayMsg = "bye..."
go func() {
ctx.exitSignal <- struct{}{}
}()
}
return replayMsg
}
// /shell2telegram broadcast_to_root - broadcast message to root users in private chat
func cmdShell2telegramBroadcastToRoot(ctx Ctx) (replayMsg string) {
message := ctx.messageArgs
if message == "" {
replayMsg = "Please set message: /shell2telegram broadcast_to_root <message>"
} else {
ctx.users.BroadcastForRoots(ctx.messageSignal,
fmt.Sprintf("Message from %s:\n%s", ctx.users.String(ctx.userID), message),
ctx.userID, // don't send self
)
replayMsg = "Message sent"
}
return replayMsg
}
// /shell2telegram message_to_user user_id|username "message" - send message to user in private chat
func cmdShell2telegramMessageToUser(ctx Ctx) (replayMsg string) {
userName, message := splitStringHalfBySpace(ctx.messageArgs)
if userName == "" || message == "" {
replayMsg = "Please set user_name and message: /shell2telegram message_to_user <user_id|username> <message>"
} else {
userID := ctx.users.FindByIDOrUserName(userName)
if userID > 0 {
ctx.users.SendMessageToPrivate(ctx.messageSignal, userID, message)
replayMsg = "Message sent"
} else {
replayMsg = "User not found"
}
}
return replayMsg
}