diff --git a/telegram/commands.go b/telegram/commands.go index 596f4a9..0200e05 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -308,27 +308,27 @@ func (c *Client) usernameOrIDToID(username string) (int64, error) { } // ProcessTransportCommand executes a command sent directly to the component -// and returns a response -func (c *Client) ProcessTransportCommand(cmdline string, resource string) string { +// and returns a response and execution success result +func (c *Client) ProcessTransportCommand(cmdline string, resource string) (string, bool) { cmd, args := parseCommand(cmdline) command, ok := transportCommands[cmd] if !ok { - return unknownCommand + return unknownCommand, false } if len(args) < command.RequiredArgs { - return notEnoughArguments + return notEnoughArguments, false } switch cmd { case "login", "code", "password": if cmd == "login" && c.Session.Login != "" { - return "Phone number already provided, use /cancelauth to start over" + return "Phone number already provided, use /cancelauth to start over", false } if cmd == "login" { err := c.TryLogin(resource, args[0]) if err != nil { - return err.Error() + return err.Error(), false } c.locks.authorizerWriteLock.Lock() @@ -340,11 +340,11 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string defer c.locks.authorizerWriteLock.Unlock() if c.authorizer == nil { - return TelegramNotInitialized + return TelegramNotInitialized, false } if c.authorizer.isClosed { - return TelegramAuthDone + return TelegramAuthDone, false } switch cmd { @@ -359,7 +359,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string // sign out case "logout": if !c.Online() { - return notOnline + return notOnline, false } for _, id := range c.cache.ChatsKeys() { @@ -369,21 +369,21 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string _, err := c.client.LogOut() if err != nil { c.forceClose() - return errors.Wrap(err, "Logout error").Error() + return errors.Wrap(err, "Logout error").Error(), false } c.Session.Login = "" // cancel auth case "cancelauth": if c.Online() { - return "Not allowed when online, use /logout instead" + return "Not allowed when online, use /logout instead", false } c.cancelAuth() - return "Cancelled" + return "Cancelled", true // set @username case "setusername": if !c.Online() { - return notOnline + return notOnline, false } var username string @@ -395,7 +395,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string Username: username, }) if err != nil { - return errors.Wrap(err, "Couldn't set username").Error() + return errors.Wrap(err, "Couldn't set username").Error(), false } // set My Name case "setname": @@ -403,7 +403,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string var lastname string if firstname == "" { - return "The name should contain at least one character" + return "The name should contain at least one character", false } if len(args) > 1 { lastname = rawCmdArguments(cmdline, 1) @@ -417,7 +417,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string } else { c.locks.authorizerWriteLock.Unlock() if !c.Online() { - return notOnline + return notOnline, false } _, err := c.client.SetName(&client.SetNameRequest{ @@ -425,25 +425,25 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string LastName: lastname, }) if err != nil { - return errors.Wrap(err, "Couldn't set name").Error() + return errors.Wrap(err, "Couldn't set name").Error(), false } } // set About case "setbio": if !c.Online() { - return notOnline + return notOnline, false } _, err := c.client.SetBio(&client.SetBioRequest{ Bio: rawCmdArguments(cmdline, 0), }) if err != nil { - return errors.Wrap(err, "Couldn't set bio").Error() + return errors.Wrap(err, "Couldn't set bio").Error(), false } // set password case "setpassword": if !c.Online() { - return notOnline + return notOnline, false } var oldPassword string @@ -458,39 +458,39 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string NewPassword: newPassword, }) if err != nil { - return errors.Wrap(err, "Couldn't set password").Error() + return errors.Wrap(err, "Couldn't set password").Error(), false } case "config": if len(args) > 1 { var msg string if gateway.MessageOutgoingPermissionVersion == 0 && args[0] == "carbons" && args[1] == "true" { - return "The server did not allow to enable carbons" + return "The server did not allow to enable carbons", false } if !c.Session.RawMessages && args[0] == "nativeedits" && args[1] == "true" { - return "nativeedits only works with rawmessages as of yet, enable it first" + return "nativeedits only works with rawmessages as of yet, enable it first", false } if c.Session.NativeEdits && args[0] == "rawmessages" && args[1] == "false" { _, err := c.Session.Set("nativeedits", "false") if err != nil { - return err.Error() + return err.Error(), false } msg = "Automatically disabling nativeedits too...\n" } value, err := c.Session.Set(args[0], args[1]) if err != nil { - return err.Error() + return err.Error(), false } gateway.DirtySessions = true - return fmt.Sprintf("%s%s set to %s", msg, args[0], value) + return fmt.Sprintf("%s%s set to %s", msg, args[0], value), true } else if len(args) > 0 { value, err := c.Session.Get(args[0]) if err != nil { - return err.Error() + return err.Error(), false } - return fmt.Sprintf("%s is set to %s", args[0], value) + return fmt.Sprintf("%s is set to %s", args[0], value), true } var entries []string @@ -498,14 +498,14 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string entries = append(entries, fmt.Sprintf("%s is set to %s", key, value)) } - return strings.Join(entries, "\n") + return strings.Join(entries, "\n"), true case "report": contact, _, err := c.GetContactByUsername(args[0]) if err != nil { - return err.Error() + return err.Error(), false } if contact == nil { - return "Contact not found" + return "Contact not found", false } text := rawCmdArguments(cmdline, 1) @@ -515,9 +515,9 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string Text: text, }) if err != nil { - return err.Error() + return err.Error(), false } else { - return "Reported" + return "Reported", true } case "add": return c.cmdAdd(args) @@ -528,45 +528,45 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string case "channel": return c.cmdChannel(args, cmdline) case "help": - return c.helpString(CommandTypeTransport, 0) + return c.helpString(CommandTypeTransport, 0), true } - return "" + return "", true } // 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) { +// and returns a response, the status of command support and the execution success result +func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, bool) { if !c.Online() { - return notOnline, true + return notOnline, true, false } cmd, args := parseCommand(cmdline) command, ok := chatCommands[cmd] if !ok { - return unknownCommand, false + return unknownCommand, false, false } if len(args) < command.RequiredArgs { - return notEnoughArguments, true + return notEnoughArguments, true, false } chatType, chatTypeErr := c.GetChatType(chatID) if chatTypeErr == nil && !IsCommandForChatType(command, chatType) { - return "Not applicable for this chat type", true + return "Not applicable for this chat type", true, false } switch cmd { // delete message case "d": if c.me == nil { - return "@me is not initialized", true + return "@me is not initialized", true, false } var limit int32 if len(args) > 0 { limit64, err := strconv.ParseInt(args[0], 10, 32) if err != nil { - return err.Error(), true + return err.Error(), true, false } limit = int32(limit64) } else { @@ -575,7 +575,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) messages, err := c.getLastMessages(chatID, "", c.me.Id, limit) if err != nil { - return err.Error(), true + return err.Error(), true, false } log.Debugf("pre-deletion query: %#v %#v", messages, messages.Messages) @@ -592,25 +592,25 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) Revoke: true, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // edit message case "s": if c.me == nil { - return "@me is not initialized", true + return "@me is not initialized", true, false } messages, err := c.getLastMessages(chatID, "", c.me.Id, 1) if err != nil { - return err.Error(), true + return err.Error(), true, false } if len(messages.Messages) == 0 { - return "No last message", true + return "No last message", true, false } message := messages.Messages[0] if message == nil { - return "Last message is empty", true + return "Last message is empty", true, false } content := c.PrepareOutgoingMessageContent(rawCmdArguments(cmdline, 0)) @@ -622,10 +622,10 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) InputMessageContent: content, }) if err != nil { - return "Message editing error", true + return "Message editing error", true, false } } else { - return "Message processing error", true + return "Message processing error", true, false } // send without sound case "silent": @@ -640,10 +640,10 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) }, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } } else { - return "Message processing error", true + return "Message processing error", true, false } // schedule a message to timestamp or to going online case "schedule": @@ -700,7 +700,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) break } - return "Invalid schedule time specifier", true + return "Invalid schedule time specifier", true, false } } @@ -715,23 +715,23 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) }, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } - return "Scheduled to " + result, true + return "Scheduled to " + result, true, true } else { - return "Message processing error", true + return "Message processing error", true, false } // forward a message to chat case "forward": messageId, err := strconv.ParseInt(args[0], 10, 64) if err != nil { - return "Cannot parse message ID", true + return "Cannot parse message ID", true, false } 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 + return "Cannot parse target chat ID", true, false } messages, err := c.client.ForwardMessages(&client.ForwardMessagesRequest{ @@ -740,7 +740,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) MessageIds: []int64{messageId}, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } if messages != nil && messages.Messages != nil { for _, message := range messages.Messages { @@ -751,7 +751,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) case "vcard": info, err := c.GetVcardInfo(chatID) if err != nil { - return err.Error(), true + return err.Error(), true, false } _, link := c.PermastoreFile(info.Photo, true) entries := []string{ @@ -761,26 +761,30 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) keyValueString("Full name", info.Given+" "+info.Family), keyValueString("Phone number", info.Tel), } - return strings.Join(entries, "\n"), true + return strings.Join(entries, "\n"), true, true // add @contact case "add": - return c.cmdAdd(args), true + response, success := c.cmdAdd(args) + return response, true, success // join https://t.me/publichat or @publicchat case "join": - return c.cmdJoin(args), true + response, success := c.cmdJoin(args) + return response, true, success // create new supergroup case "supergroup": - return c.cmdSupergroup(args, cmdline), true + response, success := c.cmdSupergroup(args, cmdline) + return response, true, success // create new channel case "channel": - return c.cmdChannel(args, cmdline), true + response, success := c.cmdChannel(args, cmdline) + return response, true, success // create new secret chat with current user case "secret": _, err := c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{ UserId: chatID, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // create group chat with current user case "group": @@ -789,7 +793,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) Title: args[0], }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // blacklists current user case "block": @@ -798,7 +802,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) BlockList: &client.BlockListMain{}, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // unblacklists current user case "unblock": @@ -807,16 +811,16 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) BlockList: nil, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // invite @username to current groupchat case "invite": contact, _, err := c.GetContactByUsername(args[0]) if err != nil { - return err.Error(), true + return err.Error(), true, false } if contact == nil { - return "Contact not found", true + return "Contact not found", true, false } _, err = c.client.AddChatMember(&client.AddChatMemberRequest{ @@ -825,7 +829,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) ForwardLimit: 100, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // get link to current chat case "link": @@ -833,17 +837,17 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) ChatId: chatID, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } - return link.InviteLink, true + return link.InviteLink, true, true // kick @username from current group chat case "kick": contact, _, err := c.GetContactByUsername(args[0]) if err != nil { - return err.Error(), true + return err.Error(), true, false } if contact == nil { - return "Contact not found", true + return "Contact not found", true, false } _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ @@ -852,23 +856,23 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) Status: &client.ChatMemberStatusLeft{}, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // mute @username [n hours] case "mute": contact, _, err := c.GetContactByUsername(args[0]) if err != nil { - return err.Error(), true + return err.Error(), true, false } if contact == nil { - return "Contact not found", true + return "Contact not found", true, false } var hours int64 if len(args) > 1 { hours, err = strconv.ParseInt(args[1], 10, 32) if err != nil { - return "Invalid number of hours", true + return "Invalid number of hours", true, false } } @@ -882,16 +886,16 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) }, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // unmute @username case "unmute": contact, _, err := c.GetContactByUsername(args[0]) if err != nil { - return err.Error(), true + return err.Error(), true, false } if contact == nil { - return "Contact not found", true + return "Contact not found", true, false } _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ @@ -904,23 +908,23 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) }, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // ban @username from current chat [for N hours] case "ban": contact, _, err := c.GetContactByUsername(args[0]) if err != nil { - return err.Error(), true + return err.Error(), true, false } if contact == nil { - return "Contact not found", true + return "Contact not found", true, false } var hours int64 if len(args) > 1 { hours, err = strconv.ParseInt(args[1], 10, 32) if err != nil { - return "Invalid number of hours", true + return "Invalid number of hours", true, false } } @@ -932,16 +936,16 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) }, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // unban @username case "unban": contact, _, err := c.GetContactByUsername(args[0]) if err != nil { - return err.Error(), true + return err.Error(), true, false } if contact == nil { - return "Contact not found", true + return "Contact not found", true, false } _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ @@ -950,16 +954,16 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) Status: &client.ChatMemberStatusMember{}, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // promote @username to admin case "promote": contact, _, err := c.GetContactByUsername(args[0]) if err != nil { - return err.Error(), true + return err.Error(), true, false } if contact == nil { - return "Contact not found", true + return "Contact not found", true, false } // clone the permissions @@ -978,7 +982,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) Status: &status, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // leave current chat case "leave": @@ -986,12 +990,12 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) ChatId: chatID, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } err = c.unsubscribe(chatID) if err != nil { - return err.Error(), true + return err.Error(), true, false } // leave current chat (for owners) case "leave!": @@ -999,12 +1003,12 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) ChatId: chatID, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } err = c.unsubscribe(chatID) if err != nil { - return err.Error(), true + return err.Error(), true, false } // set TTL case "ttl": @@ -1013,7 +1017,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) if len(args) > 0 { ttl, err = strconv.ParseInt(args[0], 10, 32) if err != nil { - return "Invalid TTL", true + return "Invalid TTL", true, false } } _, err = c.client.SetChatMessageAutoDeleteTime(&client.SetChatMessageAutoDeleteTimeRequest{ @@ -1022,16 +1026,16 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) }) if err != nil { - return err.Error(), true + return err.Error(), true, false } // close secret chat case "close": chat, _, err := c.GetContactByID(chatID, nil) if err != nil { - return err.Error(), true + return err.Error(), true, false } if chat == nil { - return "Chat not found", true + return "Chat not found", true, false } chatType := chat.Type.ChatTypeType() @@ -1041,12 +1045,12 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) SecretChatId: chatTypeSecret.SecretChatId, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } err = c.unsubscribe(chatID) if err != nil { - return err.Error(), true + return err.Error(), true, false } } // delete current chat @@ -1057,12 +1061,12 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) Revoke: true, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } err = c.unsubscribe(chatID) if err != nil { - return err.Error(), true + return err.Error(), true, false } // message search case "search": @@ -1081,7 +1085,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) messages, err := c.getLastMessages(chatID, query, 0, limit) if err != nil { - return err.Error(), true + return err.Error(), true, false } c.sendMessagesReverse(chatID, messages.Messages) @@ -1110,7 +1114,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) Limit: limit, }) if err != nil { - return err.Error(), true + return err.Error(), true, false } messages = append(messages, newMessages.Messages...) @@ -1130,7 +1134,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) members, err := c.GetChatMembers(chatID, false, query, MembersListMembers) if err != nil { - return err.Error(), true + return err.Error(), true, false } var entries []string @@ -1143,82 +1147,82 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) )) } - return strings.Join(entries, "\n"), true + return strings.Join(entries, "\n"), true, true case "help": - return c.helpString(CommandTypeChat, chatID), true + return c.helpString(CommandTypeChat, chatID), true, true default: - return "", false + return "", false, false } - return "", true + return "", true, true } -func (c *Client) cmdAdd(args []string) string { +func (c *Client) cmdAdd(args []string) (string, bool) { chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{ Username: args[0], }) if err != nil { - return err.Error() + return err.Error(), false } if chat == nil { - return "No error, but chat is nil" + return "No error, but chat is nil", false } c.subscribeToID(chat.Id, chat) - return "" + return "", true } -func (c *Client) cmdJoin(args []string) string { +func (c *Client) cmdJoin(args []string) (string, bool) { if strings.HasPrefix(args[0], "@") { chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{ Username: args[0], }) if err != nil { - return err.Error() + return err.Error(), false } if chat == nil { - return "No error, but chat is nil" + return "No error, but chat is nil", false } _, err = c.client.JoinChat(&client.JoinChatRequest{ ChatId: chat.Id, }) if err != nil { - return err.Error() + return err.Error(), false } } else { _, err := c.client.JoinChatByInviteLink(&client.JoinChatByInviteLinkRequest{ InviteLink: args[0], }) if err != nil { - return err.Error() + return err.Error(), false } } - return "" + return "", true } -func (c *Client) cmdSupergroup(args []string, cmdline string) string { +func (c *Client) cmdSupergroup(args []string, cmdline string) (string, bool) { _, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ Title: args[0], Description: rawCmdArguments(cmdline, 1), }) if err != nil { - return err.Error() + return err.Error(), false } - return "" + return "", true } -func (c *Client) cmdChannel(args []string, cmdline string) string { +func (c *Client) cmdChannel(args []string, cmdline string) (string, bool) { _, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ Title: args[0], Description: rawCmdArguments(cmdline, 1), IsChannel: true, }) if err != nil { - return err.Error() + return err.Error(), false } - return "" + return "", true } diff --git a/telegram/utils.go b/telegram/utils.go index e7d16d6..5c26b8c 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -1169,7 +1169,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str if replaceId == 0 && (strings.HasPrefix(text, "/") || strings.HasPrefix(text, "!")) { // try to execute commands - response, isCommand := c.ProcessChatCommand(chatID, text) + response, isCommand, _ := c.ProcessChatCommand(chatID, text) if response != "" { c.returnMessage(returnJid, chatID, response) } diff --git a/xmpp/handlers.go b/xmpp/handlers.go index 945f119..be53189 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -230,7 +230,7 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) { } else { toJid, err := stanza.NewJid(msg.To) if err == nil && toJid.Bare() == gatewayJid && (strings.HasPrefix(msg.Body, "/") || strings.HasPrefix(msg.Body, "!")) { - response := session.ProcessTransportCommand(msg.Body, resource) + response, _ := session.ProcessTransportCommand(msg.Body, resource) if response != "" { gateway.SendServiceMessage(msg.From, response, component) } @@ -862,10 +862,18 @@ func handleSetQueryCommand(s xmpp.Sender, iq *stanza.IQ, command *stanza.Command } var response string + var success bool if toOk { - response, _ = session.ProcessChatCommand(toId, cmdString) + response, _, success = session.ProcessChatCommand(toId, cmdString) } else { - response = session.ProcessTransportCommand(cmdString, resource) + response, success = session.ProcessTransportCommand(cmdString, resource) + } + + var noteType string + if success { + noteType = stanza.CommandNoteTypeInfo + } else { + noteType = stanza.CommandNoteTypeErr } answer.Payload = &stanza.Command{ @@ -874,7 +882,7 @@ func handleSetQueryCommand(s xmpp.Sender, iq *stanza.IQ, command *stanza.Command Status: stanza.CommandStatusCompleted, CommandElement: &stanza.Note{ Text: response, - Type: stanza.CommandNoteTypeInfo, + Type: noteType, }, }