aboutsummaryrefslogtreecommitdiff
path: root/telegram/commands.go
blob: 59fe1c77158febd08bea01c32fc09fc584e0314c (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
package telegram

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

	"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"
const notOnline string = "Not online"

var permissionsAdmin = client.ChatMemberStatusAdministrator{
	CanBeEdited:        true,
	CanChangeInfo:      true,
	CanPostMessages:    true,
	CanEditMessages:    true,
	CanDeleteMessages:  true,
	CanInviteUsers:     true,
	CanRestrictMembers: true,
	CanPinMessages:     true,
	CanPromoteMembers:  false,
}
var permissionsMember = client.ChatPermissions{
	CanSendMessages:       true,
	CanSendMediaMessages:  true,
	CanSendPolls:          true,
	CanSendOtherMessages:  true,
	CanAddWebPagePreviews: true,
	CanChangeInfo:         true,
	CanInviteUsers:        true,
	CanPinMessages:        true,
}
var permissionsReadonly = client.ChatPermissions{}

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"},
	"report":      command{"[chat] [comment]", "report a chat by id or @username"},
	"add":         command{"@username", "add @username to your chat list"},
	"join":        command{"https://t.me/invite_link", "join to chat via invite link or @publicname"},
	"supergroup":  command{"title description", "create new supergroup «title» with «description»"},
	"channel":     command{"title description", "create new channel «title» with «description»"},
}

var chatCommands = map[string]command{
	"d":          command{"[n]", "delete your last message(s)"},
	"s":          command{"edited message", "edit your last message"},
	"silent":     command{"message", "send a message without sound"},
	"schedule":   command{"{online | 2006-01-02T15:04:05 | 15:04:05} message", "schedules a message either to timestamp or to whenever the user goes online"},
	"forward":    command{"message_id target_chat", "forwards a message"},
	"vcard":      command{"", "print vCard as text"},
	"add":        command{"@username", "add @username to your chat list"},
	"join":       command{"https://t.me/invite_link", "join to chat via invite link or @publicname"},
	"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"},
	"link":       command{"", "get invite link for current chat"},
	"kick":       command{"id or @username", "remove user to current chat"},
	"mute":       command{"id or @username [hours]", "mute user in current chat"},
	"unmute":     command{"id or @username", "unrestrict user from current chat"},
	"ban":        command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"},
	"unban":      command{"id or @username", "unbans @username in current chat (and devotes from admins)"},
	"promote":    command{"id or @username [title]", "promote user to admin in current chat"},
	"leave":      command{"", "leave current chat"},
	"leave!":     command{"", "leave current chat (for owners)"},
	"ttl":        command{"", "set secret chat messages TTL before self-destroying (in seconds)"},
	"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{"<timezone>", "adjust timezone for Telegram user statuses (example: +02:00)"},
	"keeponline":  configurationOption{"<bool>", "always keep telegram session online and rely on jabber offline messages (example: true)"},
	"rawmessages": configurationOption{"<bool>", "do not add additional info (message id, origin etc.) to incoming messages (example: true)"},
}

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")
		}
	}
	str.WriteString("\nYou may use ! instead of / if it conflicts with internal commands of a client")

	return str.String()
}

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

func rawCmdArguments(cmdline string, start uint8) string {
	var state uint
	//  /cmd   ababa galamaga
	// 01   2  3    45
	startState := uint(3 + 2*start)
	for i, r := range cmdline {
		isOdd := state%2 == 1
		isSpace := unicode.IsSpace(r)
		if (!isOdd && !isSpace) || (isOdd && isSpace) {
			state += 1
		}
		if state == startState {
			return cmdline[i:]
		}
	}
	return ""
}

func keyValueString(key, value string) string {
	return fmt.Sprintf("%s: %s", key, value)
}

func (c *Client) unsubscribe(chatID int64) error {
	return gateway.SendPresence(
		c.xmpp,
		c.jid,
		gateway.SPFrom(strconv.FormatInt(chatID, 10)),
		gateway.SPType("unsubscribed"),
	)
}

func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {
	for i := len(messages) - 1; i >= 0; i-- {
		message := messages[i]
		reply, _ := c.getMessageReply(message)

		gateway.SendMessage(
			c.jid,
			strconv.FormatInt(chatID, 10),
			c.formatMessage(0, 0, false, message),
			strconv.FormatInt(message.Id, 10),
			c.xmpp,
			reply,
			false,
		)
	}
}

func (c *Client) usernameOrIDToID(username string) (int64, error) {
	userID, err := strconv.ParseInt(username, 10, 64)
	// couldn't parse the id, try to lookup as a username
	if err != nil {
		chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{
			Username: username,
		})
		if err != nil {
			return 0, err
		}

		userID = chat.Id
		if userID <= 0 {
			return 0, errors.New("Not a user")
		}
	}

	return userID, nil
}

// ProcessTransportCommand executes a command sent directly to the component
// and returns a response
func (c *Client) ProcessTransportCommand(cmdline string, resource 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 cmd == "login" {
			wasSessionLoginEmpty := c.Session.Login == ""
			c.Session.Login = args[0]

			if wasSessionLoginEmpty && c.authorizer == nil {
				go func() {
					err := c.Connect(resource)
					if err != nil {
						log.Error(errors.Wrap(err, "TDlib connection failure"))
					}
				}()
				// a quirk for authorizer to become ready. If it's still not,
				// nothing bad: the command just needs to be resent again
				time.Sleep(1e5)
			}
		}

		if c.authorizer == nil {
			return telegramNotInitialized
		}

		switch cmd {
		// sign in
		case "login":
			c.authorizer.PhoneNumber <- 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":
		if !c.Online() {
			return notOnline
		}

		for _, id := range c.cache.ChatsKeys() {
			c.unsubscribe(id)
		}

		_, err := c.client.LogOut()
		if err != nil {
			c.forceClose()
			return errors.Wrap(err, "Logout error").Error()
		}

		c.Session.Login = ""
	// set @username
	case "setusername":
		if !c.Online() {
			return notOnline
		}

		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 firstname == "" {
			return "The name should contain at least one character"
		}
		if len(args) > 1 {
			lastname = rawCmdArguments(cmdline, 1)
		}

		if c.authorizer != nil && !c.authorizer.isClosed {
			c.authorizer.FirstName <- firstname
			c.authorizer.LastName <- lastname
		} else {
			if !c.Online() {
				return notOnline
			}

			_, 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":
		if !c.Online() {
			return notOnline
		}

		_, err := c.client.SetBio(&client.SetBioRequest{
			Bio: rawCmdArguments(cmdline, 0),
		})
		if err != nil {
			return errors.Wrap(err, "Couldn't set bio").Error()
		}
	// set password
	case "setpassword":
		if !c.Online() {
			return notOnline
		}

		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 {
			if !gateway.MessageOutgoingPermission && args[0] == "carbons" && args[1] == "true" {
				return "The server did not allow to enable carbons"
			}

			value, err := c.Session.Set(args[0], args[1])
			if err != nil {
				return err.Error()
			}
			gateway.DirtySessions = true

			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 "report":
		if len(args) < 2 {
			return "Not enough arguments"
		}

		contact, _, err := c.GetContactByUsername(args[0])
		if err != nil {
			return err.Error()
		}

		text := rawCmdArguments(cmdline, 1)
		_, err = c.client.ReportChat(&client.ReportChatRequest{
			ChatId: contact.Id,
			Reason: &client.ChatReportReasonCustom{},
			Text:   text,
		})
		if err != nil {
			return err.Error()
		} else {
			return "Reported"
		}
	case "add":
		return c.cmdAdd(args)
	case "join":
		return c.cmdJoin(args)
	case "supergroup":
		return c.cmdSupergroup(args, cmdline)
	case "channel":
		return c.cmdChannel(args, cmdline)
	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) {
	if !c.Online() {
		return notOnline, true
	}

	cmd, args := parseCommand(cmdline)
	switch cmd {
	// delete message
	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.getLastMessages(chatID, "", c.me.Id, limit)
		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 message
	case "s":
		if c.me == nil {
			return "@me is not initialized", true
		}
		if len(args) < 1 {
			return "Not enough arguments", true
		}

		messages, err := c.getLastMessages(chatID, "", c.me.Id, 1)
		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
		}

		content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 0), "", "", 0)

		if content != nil {
			c.client.EditMessageText(&client.EditMessageTextRequest{
				ChatId:              chatID,
				MessageId:           message.Id,
				InputMessageContent: content,
			})
		} else {
			return "Message processing error", true
		}
	// send without sound
	case "silent":
		if len(args) < 1 {
			return "Not enough arguments", true
		}

		content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 0), "", "", 0)

		if content != nil {
			_, err := c.client.SendMessage(&client.SendMessageRequest{
				ChatId:              chatID,
				InputMessageContent: content,
				Options: &client.MessageSendOptions{
					DisableNotification: true,
				},
			})
			if err != nil {
				return err.Error(), true
			}
		} else {
			return "Message processing error", true
		}
	// schedule a message to timestamp or to going online
	case "schedule":
		if len(args) < 2 {
			return "Not enough arguments", true
		}

		var state client.MessageSchedulingState
		var result string
		due := args[0]
		if due == "online" {
			state = &client.MessageSchedulingStateSendWhenOnline{}
			result = due
		} else {
			if c.Session.Timezone == "" {
				due += "Z"
			} else {
				due += c.Session.Timezone
			}

			switch 0 {
			default:
				// try bare time first
				timestamp, err := time.Parse("15:04:05Z07:00", due)
				if err == nil {
					now := time.Now().In(c.Session.TimezoneToLocation())
					// combine timestamp's time with today's date
					timestamp = time.Date(
						now.Year(),
						now.Month(),
						now.Day(),
						timestamp.Hour(),
						timestamp.Minute(),
						timestamp.Second(),
						0,
						timestamp.Location(),
					)
					diff := timestamp.Sub(now)
					if diff < 0 { // set to tomorrow
						timestamp = timestamp.AddDate(0, 0, 1)
					}
					state = &client.MessageSchedulingStateSendAtDate{
						SendDate: int32(timestamp.Unix()),
					}
					result = timestamp.Format(time.RFC3339)

					break
				}

				timestamp, err = time.Parse(time.RFC3339, due)
				if err == nil {
					// 2038 doomsday again
					state = &client.MessageSchedulingStateSendAtDate{
						SendDate: int32(timestamp.Unix()),
					}
					result = timestamp.Format(time.RFC3339)

					break
				}

				return "Invalid schedule time specifier", true
			}
		}

		content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 1), "", "", 0)

		if content != nil {
			_, err := c.client.SendMessage(&client.SendMessageRequest{
				ChatId:              chatID,
				InputMessageContent: content,
				Options: &client.MessageSendOptions{
					SchedulingState: state,
				},
			})
			if err != nil {
				return err.Error(), true
			}
			return "Scheduled to " + result, true
		} else {
			return "Message processing error", true
		}
	// forward a message to chat
	case "forward":
		if len(args) < 2 {
			return notEnoughArguments, true
		}

		messageId, err := strconv.ParseInt(args[0], 10, 64)
		if err != nil {
			return "Cannot parse message ID", true
		}

		targetChatParts := strings.Split(args[1], "@") // full JIDs are supported too
		targetChatId, err := strconv.ParseInt(targetChatParts[0], 10, 64)
		if err != nil {
			return "Cannot parse target chat ID", true
		}

		messages, err := c.client.ForwardMessages(&client.ForwardMessagesRequest{
			ChatId:     targetChatId,
			FromChatId: chatID,
			MessageIds: []int64{messageId},
		})
		if err != nil {
			return err.Error(), true
		}
		if messages != nil && messages.Messages != nil {
			for _, message := range messages.Messages {
				c.ProcessIncomingMessage(targetChatId, message)
			}
		}
	// print vCard
	case "vcard":
		info, err := c.GetVcardInfo(chatID)
		if err != nil {
			return err.Error(), true
		}
		_, link := c.PermastoreFile(info.Photo, true)
		entries := []string{
			keyValueString("Chat title", info.Fn),
			keyValueString("Photo", link),
			keyValueString("Username", info.Nickname),
			keyValueString("Full name", info.Given + " " + info.Family),
			keyValueString("Phone number", info.Tel),
		}
		return strings.Join(entries, "\n"), true
	// add @contact
	case "add":
		return c.cmdAdd(args), true
	// join https://t.me/publichat or @publicchat
	case "join":
		return c.cmdJoin(args), true
	// create new supergroup
	case "supergroup":
		return c.cmdSupergroup(args, cmdline), true
	// create new channel
	case "channel":
		return c.cmdChannel(args, cmdline), true
	// create new secret chat with current user
	case "secret":
		_, err := c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{
			UserId: chatID,
		})
		if err != nil {
			return err.Error(), true
		}
	// create group chat with current user
	case "group":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		_, err := c.client.CreateNewBasicGroupChat(&client.CreateNewBasicGroupChatRequest{
			UserIds: []int64{chatID},
			Title:   args[0],
		})
		if err != nil {
			return err.Error(), true
		}
	// blacklists current user
	case "block":
		_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
			SenderId:  &client.MessageSenderUser{UserId: chatID},
			IsBlocked: true,
		})
		if err != nil {
			return err.Error(), true
		}
	// unblacklists current user
	case "unblock":
		_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
			SenderId:  &client.MessageSenderUser{UserId: chatID},
			IsBlocked: false,
		})
		if err != nil {
			return err.Error(), true
		}
	// invite @username to current groupchat
	case "invite":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		contact, _, err := c.GetContactByUsername(args[0])
		if err != nil {
			return err.Error(), true
		}

		_, err = c.client.AddChatMember(&client.AddChatMemberRequest{
			ChatId:       chatID,
			UserId:       contact.Id,
			ForwardLimit: 100,
		})
		if err != nil {
			return err.Error(), true
		}
	// get link to current chat
	case "link":
		link, err := c.client.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{
			ChatId: chatID,
		})
		if err != nil {
			return err.Error(), true
		}
		return link.InviteLink, true
	// kick @username from current group chat
	case "kick":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		contact, _, err := c.GetContactByUsername(args[0])
		if err != nil {
			return err.Error(), true
		}

		_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
			ChatId:   chatID,
			MemberId: &client.MessageSenderUser{UserId: contact.Id},
			Status:   &client.ChatMemberStatusLeft{},
		})
		if err != nil {
			return err.Error(), true
		}
	// mute @username [n hours]
	case "mute":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		contact, _, err := c.GetContactByUsername(args[0])
		if err != nil {
			return err.Error(), true
		}

		var hours int64
		if len(args) > 1 {
			hours, err = strconv.ParseInt(args[1], 10, 32)
			if err != nil {
				return "Invalid number of hours", true
			}
		}

		_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
			ChatId:   chatID,
			MemberId: &client.MessageSenderUser{UserId: contact.Id},
			Status: &client.ChatMemberStatusRestricted{
				IsMember:            true,
				RestrictedUntilDate: c.formatBantime(hours),
				Permissions:         &permissionsReadonly,
			},
		})
		if err != nil {
			return err.Error(), true
		}
	// unmute @username
	case "unmute":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		contact, _, err := c.GetContactByUsername(args[0])
		if err != nil {
			return err.Error(), true
		}

		_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
			ChatId:   chatID,
			MemberId: &client.MessageSenderUser{UserId: contact.Id},
			Status: &client.ChatMemberStatusRestricted{
				IsMember:            true,
				RestrictedUntilDate: 0,
				Permissions:         &permissionsMember,
			},
		})
		if err != nil {
			return err.Error(), true
		}
	// ban @username from current chat [for N hours]
	case "ban":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		contact, _, err := c.GetContactByUsername(args[0])
		if err != nil {
			return err.Error(), true
		}

		var hours int64
		if len(args) > 1 {
			hours, err = strconv.ParseInt(args[1], 10, 32)
			if err != nil {
				return "Invalid number of hours", true
			}
		}

		_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
			ChatId:   chatID,
			MemberId: &client.MessageSenderUser{UserId: contact.Id},
			Status: &client.ChatMemberStatusBanned{
				BannedUntilDate: c.formatBantime(hours),
			},
		})
		if err != nil {
			return err.Error(), true
		}
	// unban @username
	case "unban":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		contact, _, err := c.GetContactByUsername(args[0])
		if err != nil {
			return err.Error(), true
		}

		_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
			ChatId:   chatID,
			MemberId: &client.MessageSenderUser{UserId: contact.Id},
			Status:   &client.ChatMemberStatusMember{},
		})
		if err != nil {
			return err.Error(), true
		}
	// promote @username to admin
	case "promote":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		contact, _, err := c.GetContactByUsername(args[0])
		if err != nil {
			return err.Error(), true
		}

		// clone the permissions
		status := permissionsAdmin

		if len(args) > 1 {
			status.CustomTitle = args[1]
		}

		_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
			ChatId:   chatID,
			MemberId: &client.MessageSenderUser{UserId: contact.Id},
			Status:   &status,
		})
		if err != nil {
			return err.Error(), true
		}
	// leave current chat
	case "leave":
		_, err := c.client.LeaveChat(&client.LeaveChatRequest{
			ChatId: chatID,
		})
		if err != nil {
			return err.Error(), true
		}

		err = c.unsubscribe(chatID)
		if err != nil {
			return err.Error(), true
		}
	// leave current chat (for owners)
	case "leave!":
		_, err := c.client.DeleteChat(&client.DeleteChatRequest{
			ChatId: chatID,
		})
		if err != nil {
			return err.Error(), true
		}

		err = c.unsubscribe(chatID)
		if err != nil {
			return err.Error(), true
		}
	// set TTL
	case "ttl":
		var ttl int64
		var err error
		if len(args) > 0 {
			ttl, err = strconv.ParseInt(args[0], 10, 32)
			if err != nil {
				return "Invalid TTL", true
			}
		}
		_, err = c.client.SetChatMessageTtl(&client.SetChatMessageTtlRequest{
			ChatId: chatID,
			Ttl:    int32(ttl),
		})

		if err != nil {
			return err.Error(), true
		}
	// close secret chat
	case "close":
		chat, _, err := c.GetContactByID(chatID, nil)
		if err != nil {
			return err.Error(), true
		}

		chatType := chat.Type.ChatTypeType()
		if chatType == client.TypeChatTypeSecret {
			chatTypeSecret, _ := chat.Type.(*client.ChatTypeSecret)
			_, err = c.client.CloseSecretChat(&client.CloseSecretChatRequest{
				SecretChatId: chatTypeSecret.SecretChatId,
			})
			if err != nil {
				return err.Error(), true
			}

			err = c.unsubscribe(chatID)
			if err != nil {
				return err.Error(), true
			}
		}
	// delete current chat
	case "delete":
		_, err := c.client.DeleteChatHistory(&client.DeleteChatHistoryRequest{
			ChatId:             chatID,
			RemoveFromChatList: true,
			Revoke:             true,
		})
		if err != nil {
			return err.Error(), true
		}

		err = c.unsubscribe(chatID)
		if err != nil {
			return err.Error(), true
		}
	// message search
	case "search":
		var limit int32 = 100
		if len(args) > 1 {
			newLimit, err := strconv.ParseInt(args[1], 10, 32)
			if err == nil {
				limit = int32(newLimit)
			}
		}

		var query string
		if len(args) > 0 {
			query = args[0]
		}

		messages, err := c.getLastMessages(chatID, query, 0, limit)
		if err != nil {
			return err.Error(), true
		}

		c.sendMessagesReverse(chatID, messages.Messages)
	// get latest entries from history
	case "history":
		var limit int32 = 10
		if len(args) > 0 {
			newLimit, err := strconv.ParseInt(args[0], 10, 32)
			if err == nil {
				limit = int32(newLimit)
			}
		}

		var newMessages *client.Messages
		var messages []*client.Message
		var err error
		var fromId int64
		for _ = range make([]struct{}, limit) { // safety limit
			if len(messages) > 0 {
				fromId = messages[len(messages)-1].Id
			}

			newMessages, err = c.client.GetChatHistory(&client.GetChatHistoryRequest{
				ChatId:        chatID,
				FromMessageId: fromId,
				Limit:         limit,
			})
			if err != nil {
				return err.Error(), true
			}

			messages = append(messages, newMessages.Messages...)

			if len(newMessages.Messages) == 0 || len(messages) >= int(limit) {
				break
			}
		}

		c.sendMessagesReverse(chatID, messages)
	// chat members
	case "members":
		var query string
		if len(args) > 0 {
			query = args[0]
		}

		members, err := c.client.SearchChatMembers(&client.SearchChatMembersRequest{
			ChatId: chatID,
			Limit:  9999,
			Query:  query,
			Filter: &client.ChatMembersFilterMembers{},
		})
		if err != nil {
			return err.Error(), true
		}

		var entries []string
		for _, member := range members.Members {
			var senderId int64
			switch member.MemberId.MessageSenderType() {
			case client.TypeMessageSenderUser:
				memberUser, _ := member.MemberId.(*client.MessageSenderUser)
				senderId = memberUser.UserId
			case client.TypeMessageSenderChat:
				memberChat, _ := member.MemberId.(*client.MessageSenderChat)
				senderId = memberChat.ChatId
			}
			entries = append(entries, fmt.Sprintf(
				"%v | role: %v",
				c.formatContact(senderId),
				member.Status.ChatMemberStatusType(),
			))
		}

		return strings.Join(entries, "\n"), true
	case "help":
		return helpString(helpTypeChat), true
	default:
		return "", false
	}

	return "", true
}

func (c *Client) cmdAdd(args []string) string {
	if len(args) < 1 {
		return notEnoughArguments
	}

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

	c.subscribeToID(chat.Id, chat)

	return ""
}

func (c *Client) cmdJoin(args []string) string {
	if len(args) < 1 {
		return notEnoughArguments
	}

	if strings.HasPrefix(args[0], "@") {
		chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{
			Username: args[0],
		})
		if err != nil {
			return err.Error()
		}
		if chat == nil {
			return "No error, but chat is nil"
		}
		_, err = c.client.JoinChat(&client.JoinChatRequest{
			ChatId: chat.Id,
		})
		if err != nil {
			return err.Error()
		}
	} else {
		_, err := c.client.JoinChatByInviteLink(&client.JoinChatByInviteLinkRequest{
			InviteLink: args[0],
		})
		if err != nil {
			return err.Error()
		}
	}

	return ""
}

func (c *Client) cmdSupergroup(args []string, cmdline string) string {
	if len(args) < 1 {
		return notEnoughArguments
	}

	_, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
		Title:       args[0],
		Description: rawCmdArguments(cmdline, 1),
	})
	if err != nil {
		return err.Error()
	}

	return ""
}

func (c *Client) cmdChannel(args []string, cmdline string) string {
	if len(args) < 1 {
		return notEnoughArguments
	}

	_, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
		Title:       args[0],
		Description: rawCmdArguments(cmdline, 1),
		IsChannel:   true,
	})
	if err != nil {
		return err.Error()
	}

	return ""
}