aboutsummaryrefslogtreecommitdiff
path: root/telegram/commands.go
blob: 2602609306a8aa5274ec1b0e1f416219460d466d (plain) (blame)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package telegram

import (
	"fmt"
	"github.com/pkg/errors"
	"regexp"
	"strconv"
	"strings"

	"dev.narayana.im/narayana/telegabber/xmpp/gateway"

	log "github.com/sirupsen/logrus"
	"github.com/zelenin/go-tdlib/client"
)

const notEnoughArguments string = "Not enough arguments"
const telegramNotInitialized string = "Telegram connection is not initialized yet"

var transportCommands = map[string]command{
	"login":       command{"phone", "sign in"},
	"logout":      command{"", "sign out"},
	"code":        command{"", "check one-time code"},
	"password":    command{"", "check 2fa password"},
	"setusername": command{"", "update @username"},
	"setname":     command{"first last", "update name"},
	"setbio":      command{"", "update about"},
	"setpassword": command{"[old] [new]", "set or remove password"},
	"config":      command{"[param] [value]", "view or update configuration options"},
}

var chatCommands = map[string]command{
	"d": command{"[n]", "delete your last message(s)"},
	//"s":          command{"regex replace", "edit your last message"},
	//"add":        command{"@username", "add @username to your chat list"},
	//"join":       command{"https://t.me/invite_link", "join to chat via invite link"},
	//"group":      command{"title", "create groupchat «title» with current user"},
	//"supergroup": command{"title description", "create new supergroup «title» with «description»"},
	//"channel":    command{"title description", "create new channel «title» with «description»"},
	//"secret":     command{"", "create secretchat with current user"},
	//"search":     command{"string [limit]", "search <string> in current chat"},
	//"history":    command{"[limit]", "get last [limit] messages from current chat"},
	//"block":      command{"", "blacklist current user"},
	//"unblock":    command{"", "unblacklist current user"},
	//"invite":     command{"id or @username", "add user to current chat"},
	//"kick":       command{"id or @username", "remove user to current chat"},
	//"ban":        command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"},
	//"leave":      command{"", "leave current chat"},
	//"close":      command{"", "close current secret chat"},
	//"delete":     command{"", "delete current chat from chat list"},
	//"members":    command{"[query]", "search members [by optional query] in current chat (requires admin rights)"},
}

var transportConfigurationOptions = map[string]configurationOption{
	//"timezone": configurationOption{"00:00", "adjust timezone for Telegram user statuses"}
}

type command struct {
	arguments   string
	description string
}
type configurationOption command

type helpType int

const (
	helpTypeTransport helpType = iota
	helpTypeChat
)

func helpString(ht helpType) string {
	var str strings.Builder
	var commandMap map[string]command

	switch ht {
	case helpTypeTransport:
		commandMap = transportCommands
	case helpTypeChat:
		commandMap = chatCommands
	}

	str.WriteString("Available commands:\n")
	for name, command := range commandMap {
		str.WriteString("/")
		str.WriteString(name)
		if command.arguments != "" {
			str.WriteString(" ")
			str.WriteString(command.arguments)
		}
		str.WriteString(" — ")
		str.WriteString(command.description)
		str.WriteString("\n")
	}

	if ht == helpTypeTransport {
		str.WriteString("Configuration options\n")
		for name, option := range transportConfigurationOptions {
			str.WriteString(name)
			str.WriteString(" ")
			str.WriteString(option.arguments)
			str.WriteString(" — ")
			str.WriteString(option.description)
			str.WriteString("\n")
		}
	}

	return str.String()
}

func parseCommand(cmdline string) (string, []string) {
	bodyFields := strings.Fields(cmdline)
	return bodyFields[0][1:], bodyFields[1:]
}

// ProcessTransportCommand executes a command sent directly to the component
// and returns a response
func (c *Client) ProcessTransportCommand(cmdline string) string {
	cmd, args := parseCommand(cmdline)
	switch cmd {
	case "login", "code", "password":
		if cmd == "login" && c.Session.Login != "" {
			return ""
		}

		if len(args) < 1 {
			return notEnoughArguments
		}
		if c.authorizer == nil {
			return telegramNotInitialized
		}

		switch cmd {
		// sign in
		case "login":
			c.authorizer.PhoneNumber <- args[0]
			c.Session.Login = args[0]
		// check auth code
		case "code":
			c.authorizer.Code <- args[0]
		// check auth password
		case "password":
			c.authorizer.Password <- args[0]
		}
	// sign out
	case "logout":
		_, err := c.client.LogOut()
		if err != nil {
			return errors.Wrap(err, "Logout error").Error()
		}

		for id := range c.cache.chats {
			gateway.SendPresence(
				c.xmpp,
				c.jid,
				gateway.SPFrom(strconv.FormatInt(id, 10)),
				gateway.SPType("unsubscribed"),
			)
		}

		c.Session.Login = ""
	// set @username
	case "setusername":
		var username string
		if len(args) > 0 {
			username = args[0]
		}

		_, err := c.client.SetUsername(&client.SetUsernameRequest{
			Username: username,
		})
		if err != nil {
			return errors.Wrap(err, "Couldn't set username").Error()
		}
	// set My Name
	case "setname":
		var firstname string
		var lastname string
		if len(args) > 0 {
			firstname = args[0]
		}
		if len(args) > 1 {
			lastname = args[1]
		}

		_, err := c.client.SetName(&client.SetNameRequest{
			FirstName: firstname,
			LastName:  lastname,
		})
		if err != nil {
			return errors.Wrap(err, "Couldn't set name").Error()
		}
	// set About
	case "setbio":
		_, err := c.client.SetBio(&client.SetBioRequest{
			Bio: strings.Join(args, " "),
		})
		if err != nil {
			return errors.Wrap(err, "Couldn't set bio").Error()
		}
	// set password
	case "setpassword":
		var oldPassword string
		var newPassword string
		// 0 or 1 argument is ignored and the password is reset
		if len(args) > 1 {
			oldPassword = args[0]
			newPassword = args[1]
		}
		_, err := c.client.SetPassword(&client.SetPasswordRequest{
			OldPassword: oldPassword,
			NewPassword: newPassword,
		})
		if err != nil {
			return errors.Wrap(err, "Couldn't set password").Error()
		}
	case "config":
		if len(args) > 1 {
			value, err := c.Session.Set(args[0], args[1])
			if err != nil {
				return err.Error()
			}

			return fmt.Sprintf("%s set to %s", args[0], value)
		} else if len(args) > 0 {
			value, err := c.Session.Get(args[0])
			if err != nil {
				return err.Error()
			}

			return fmt.Sprintf("%s is set to %s", args[0], value)
		}

		var entries []string
		for key, value := range c.Session.ToMap() {
			entries = append(entries, fmt.Sprintf("%s is set to %s", key, value))
		}

		return strings.Join(entries, "\n")
	case "help":
		return helpString(helpTypeTransport)
	}

	return ""
}

// ProcessChatCommand executes a command sent in a mapped chat
// and returns a response and the status of command support
func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) {
	cmd, args := parseCommand(cmdline)
	switch cmd {
	// delete last message(s)
	case "d":
		if c.me == nil {
			return "@me is not initialized", true
		}

		var limit int32
		if len(args) > 0 {
			limit64, err := strconv.ParseInt(args[0], 10, 32)
			if err != nil {
				return err.Error(), true
			}
			limit = int32(limit64)
		} else {
			limit = 1
		}

		messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
			ChatId:       chatID,
			Limit:        limit,
			SenderUserId: c.me.Id,
			Filter:       &client.SearchMessagesFilterEmpty{},
		})
		if err != nil {
			return err.Error(), true
		}
		log.Debugf("pre-deletion query: %#v %#v", messages, messages.Messages)

		var messageIds []int64
		for _, message := range messages.Messages {
			if message != nil {
				messageIds = append(messageIds, message.Id)
			}
		}

		_, err = c.client.DeleteMessages(&client.DeleteMessagesRequest{
			ChatId:     chatID,
			MessageIds: messageIds,
			Revoke:     true,
		})
		if err != nil {
			return err.Error(), true
		}
	// edit last message
	case "s":
		if c.me == nil {
			return "@me is not initialized", true
		}
		if len(args) < 2 {
			return "Not enough arguments", true
		}
		regex, err := regexp.Compile(args[0])
		if err != nil {
			return err.Error(), true
		}

		messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
			ChatId:       chatID,
			Limit:        1,
			SenderUserId: c.me.Id,
			Filter:       &client.SearchMessagesFilterEmpty{},
		})
		log.Debugf("%#v", client.SearchChatMessagesRequest{
			ChatId:       chatID,
			Limit:        1,
			SenderUserId: c.me.Id,
			Filter:       &client.SearchMessagesFilterEmpty{},
		})
		if err != nil {
			return err.Error(), true
		}
		if len(messages.Messages) == 0 {
			return "No last message", true
		}

		message := messages.Messages[0]
		if message == nil {
			return "Last message is empty", true
		}

		messageText, ok := message.Content.(*client.MessageText)
		if !ok {
			return "Last message is not a text!", true
		}

		text := regex.ReplaceAllString(messageText.Text.Text, strings.Join(args[1:], " "))
		c.ProcessOutgoingMessage(chatID, text, message.Id, "")
	// add @contact
	case "add":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{
			Username: args[0],
		})
		if err != nil {
			return err.Error(), true
		}
		if chat == nil {
			return "No error, but chat is nil", true
		}

		gateway.SendPresence(
			c.xmpp,
			c.jid,
			gateway.SPFrom(strconv.FormatInt(chat.Id, 10)),
			gateway.SPType("subscribe"),
		)
	// join https://t.me/publichat
	case "join":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		_, err := c.client.JoinChatByInviteLink(&client.JoinChatByInviteLinkRequest{
			InviteLink: args[0],
		})
		if err != nil {
			return err.Error(), true
		}
	case "help":
		return helpString(helpTypeChat), true
	default:
		return "", false
	}

	return "", true
}