aboutsummaryrefslogblamecommitdiff
path: root/telegram/commands.go
blob: 8baf0688369cd8adb533330f077121f6f7075fc8 (plain) (tree)
1
2
3
4
5
6
7
8
9

                
        
             
                               
                 
                 
              
                 


                                                          
                                        
                                            

 

                                                                                  
                                     
 
                                           







                                                                        
                                                                                          


                                      







                                                                                                           
                                                                                   








                                                                                                                        
                                                                                                                       


                                                                   

                                                                                                                                               





















































                                                                         




                                                      

















                                                               








                                                              










                                                                                

                                                                   








                                                                                       

                                



                                                          
                          

 

                                                                            
                                                                                  
                                          

                                         

                                                            




                                                 






                                                                        
                                                                  









                                                                                                       




                                                     
                          

                                                           
                                  

                                                    
                                      


                                                        

                      



                                        
                                                        


                                         

                                           
                                      
                                                                       

                 
                                    

                           



                                        












                                                                                



                                        

















                                                                            



                                        
                                                                
                                                         





                                                                           



                                        













                                                                                





                                                                     
                                                    
















                                                                                             

                                                    



                 


                                                              
                                                                                  



                                      
                                          
                    
                         



                                                             
 










                                                                         
                                                                              






                                                                                      
                                           
                                                                           
                         


                                                                               

                                               




                                                
                       



                                                             
                                  

                                                           
 
                                                                          











                                                            
                                                                                       
 








                                                                                


















                                                                                       
                                                                       

                                                    











                                                                                            







                                                                                                  
                                                                 











                                                                                                  
                                                                 




                                                







                                                                                         
                                       











                                                                                                          
                                                         





                                                        


                                  


                                                                                                                    







                                                        


                                                                                                                    




                                                        






                                                       
                                                                  
                                       
                                                        


                                                                                     

                                               











                                                        
                                                                  




                                                                                                 

                                               
                                                                       




                                                        






                                                        
                                                                  












                                                                                                 

                                               







                                                                       









                                                                                                           
                                               




                                                        
                                             
                 










                                                                                         
                                                                          




                                                        
                                             
                 


                                                                                      
                                                   






                                                 
                         













                                                                          
                                                                           



                                                










                                                                          














                                                                                              
                 

                                                                







                                                                                            
                                       









                                                                   








                                                                                            

                                                              
                                                          









                                                                     
                    
                                                     

                                

         
                       
 
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/godcong/go-tdlib/client"
)

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

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{"<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)"},
}

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:]
}

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 (c *Client) unsubscribe(chatID int64) {
	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-- {
		gateway.SendMessage(
			c.jid,
			strconv.FormatInt(chatID, 10),
			c.formatMessage(0, 0, false, messages[i]),
			c.xmpp,
		)
	}
}

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

		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":
		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 {
			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 "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), "")

		if content != nil {
			c.client.EditMessageText(&client.EditMessageTextRequest{
				ChatID:              chatID,
				MessageID:           message.ID,
				InputMessageContent: content,
			})
		} else {
			return "Message processing error", true
		}
	// 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
		}
	// create new supergroup
	case "supergroup":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		_, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
			Title:       args[0],
			Description: rawCmdArguments(cmdline, 1),
		})
		if err != nil {
			return err.Error(), true
		}
	// create new channel
	case "channel":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		_, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
			Title:       args[0],
			Description: rawCmdArguments(cmdline, 1),
			IsChannel:   true,
		})
		if err != nil {
			return err.Error(), true
		}
	// create new secret chat with current user
	case "secret":
		_, user, err := c.GetContactByID(chatID, nil)
		if err != nil || user == nil {
			return "User not found", true
		}

		_, 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
		}

		if chatID > 0 {
			_, err := c.client.CreateNewBasicGroupChat(&client.CreateNewBasicGroupChatRequest{
				UserIDs: []int64{chatID},
				Title:   args[0],
			})
			if err != nil {
				return err.Error(), true
			}
		}
	// blacklists current user
	case "block":
		if chatID > 0 {
			_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
				Sender:    &client.MessageSenderUser{UserID: chatID},
				IsBlocked: true,
			})
			if err != nil {
				return err.Error(), true
			}
		}
	// unblacklists current user
	case "unblock":
		if chatID > 0 {
			_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
				Sender:    &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
		}

		if chatID < 0 {
			userID, err := c.usernameOrIDToID(args[0])
			if err != nil {
				return err.Error(), true
			}

			_, err = c.client.AddChatMember(&client.AddChatMemberRequest{
				ChatID: chatID,
				UserID: userID,
			})
			if err != nil {
				return err.Error(), true
			}
		}
	// kick @username from current group chat
	case "kick":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		if chatID < 0 {
			userID, err := c.usernameOrIDToID(args[0])
			if err != nil {
				return err.Error(), true
			}

			_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
				ChatID: chatID,
				UserID: userID,
				Status: &client.ChatMemberStatusLeft{},
			})
			if err != nil {
				return err.Error(), true
			}
		}
	// ban @username from current chat [for N hours]
	case "ban":
		if len(args) < 1 {
			return notEnoughArguments, true
		}

		if chatID < 0 {
			userID, err := c.usernameOrIDToID(args[0])
			if err != nil {
				return err.Error(), true
			}

			var until int32
			if len(args) > 1 {
				hours, err := strconv.ParseInt(args[1], 10, 32)
				if err != nil {
					until = int32(time.Now().Unix() + hours*3600)
				}
			}

			_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
				ChatID: chatID,
				UserID: userID,
				Status: &client.ChatMemberStatusBanned{
					BannedUntilDate: until,
				},
			})
			if err != nil {
				return err.Error(), true
			}
		}
	// leave current chat
	case "leave":
		chat, _, err := c.GetContactByID(chatID, nil)
		if err != nil {
			return err.Error(), true
		}

		chatType := chat.Type.ChatTypeType()
		if chatType == client.TypeChatTypeBasicGroup || chatType == client.TypeChatTypeSupergroup {
			_, err = c.client.LeaveChat(&client.LeaveChatRequest{
				ChatID: chatID,
			})
			if err != nil {
				return err.Error(), true
			}

			c.unsubscribe(chatID)
		}
	// 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
			}

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

		c.unsubscribe(chatID)
	// message search
	case "search":
		var limit int32 = 10
		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 messages *client.Messages
		var err error
		for _ = range make([]struct{}, 2) {
			messages, err = c.client.GetChatHistory(&client.GetChatHistoryRequest{
				ChatID: chatID,
				Limit:  limit,
			})
			if err != nil {
				return err.Error(), true
			}

			// TDlib yields only the latest message on the first request
			if !(len(messages.Messages) == 1 && limit > 1) {
				break
			}
		}

		c.sendMessagesReverse(chatID, messages.Messages)
	// members list (for admins)
	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(),
			))
		}

		gateway.SendMessage(
			c.jid,
			strconv.FormatInt(chatID, 10),
			strings.Join(entries, "\n"),
			c.xmpp,
		)
	case "help":
		return helpString(helpTypeChat), true
	default:
		return "", false
	}

	return "", true
}