Show command execution success status

This commit is contained in:
Bohdan Horbeshko 2024-02-18 04:36:23 -05:00
parent 5dd60450c2
commit 154b59de44
3 changed files with 147 additions and 135 deletions

View file

@ -308,27 +308,27 @@ func (c *Client) usernameOrIDToID(username string) (int64, error) {
} }
// ProcessTransportCommand executes a command sent directly to the component // ProcessTransportCommand executes a command sent directly to the component
// and returns a response // and returns a response and execution success result
func (c *Client) ProcessTransportCommand(cmdline string, resource string) string { func (c *Client) ProcessTransportCommand(cmdline string, resource string) (string, bool) {
cmd, args := parseCommand(cmdline) cmd, args := parseCommand(cmdline)
command, ok := transportCommands[cmd] command, ok := transportCommands[cmd]
if !ok { if !ok {
return unknownCommand return unknownCommand, false
} }
if len(args) < command.RequiredArgs { if len(args) < command.RequiredArgs {
return notEnoughArguments return notEnoughArguments, false
} }
switch cmd { switch cmd {
case "login", "code", "password": case "login", "code", "password":
if cmd == "login" && c.Session.Login != "" { 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" { if cmd == "login" {
err := c.TryLogin(resource, args[0]) err := c.TryLogin(resource, args[0])
if err != nil { if err != nil {
return err.Error() return err.Error(), false
} }
c.locks.authorizerWriteLock.Lock() c.locks.authorizerWriteLock.Lock()
@ -340,11 +340,11 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
defer c.locks.authorizerWriteLock.Unlock() defer c.locks.authorizerWriteLock.Unlock()
if c.authorizer == nil { if c.authorizer == nil {
return TelegramNotInitialized return TelegramNotInitialized, false
} }
if c.authorizer.isClosed { if c.authorizer.isClosed {
return TelegramAuthDone return TelegramAuthDone, false
} }
switch cmd { switch cmd {
@ -359,7 +359,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
// sign out // sign out
case "logout": case "logout":
if !c.Online() { if !c.Online() {
return notOnline return notOnline, false
} }
for _, id := range c.cache.ChatsKeys() { for _, id := range c.cache.ChatsKeys() {
@ -369,21 +369,21 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
_, err := c.client.LogOut() _, err := c.client.LogOut()
if err != nil { if err != nil {
c.forceClose() c.forceClose()
return errors.Wrap(err, "Logout error").Error() return errors.Wrap(err, "Logout error").Error(), false
} }
c.Session.Login = "" c.Session.Login = ""
// cancel auth // cancel auth
case "cancelauth": case "cancelauth":
if c.Online() { if c.Online() {
return "Not allowed when online, use /logout instead" return "Not allowed when online, use /logout instead", false
} }
c.cancelAuth() c.cancelAuth()
return "Cancelled" return "Cancelled", true
// set @username // set @username
case "setusername": case "setusername":
if !c.Online() { if !c.Online() {
return notOnline return notOnline, false
} }
var username string var username string
@ -395,7 +395,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
Username: username, Username: username,
}) })
if err != nil { 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 // set My Name
case "setname": case "setname":
@ -403,7 +403,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
var lastname string var lastname string
if firstname == "" { 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 { if len(args) > 1 {
lastname = rawCmdArguments(cmdline, 1) lastname = rawCmdArguments(cmdline, 1)
@ -417,7 +417,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
} else { } else {
c.locks.authorizerWriteLock.Unlock() c.locks.authorizerWriteLock.Unlock()
if !c.Online() { if !c.Online() {
return notOnline return notOnline, false
} }
_, err := c.client.SetName(&client.SetNameRequest{ _, err := c.client.SetName(&client.SetNameRequest{
@ -425,25 +425,25 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
LastName: lastname, LastName: lastname,
}) })
if err != nil { if err != nil {
return errors.Wrap(err, "Couldn't set name").Error() return errors.Wrap(err, "Couldn't set name").Error(), false
} }
} }
// set About // set About
case "setbio": case "setbio":
if !c.Online() { if !c.Online() {
return notOnline return notOnline, false
} }
_, err := c.client.SetBio(&client.SetBioRequest{ _, err := c.client.SetBio(&client.SetBioRequest{
Bio: rawCmdArguments(cmdline, 0), Bio: rawCmdArguments(cmdline, 0),
}) })
if err != nil { if err != nil {
return errors.Wrap(err, "Couldn't set bio").Error() return errors.Wrap(err, "Couldn't set bio").Error(), false
} }
// set password // set password
case "setpassword": case "setpassword":
if !c.Online() { if !c.Online() {
return notOnline return notOnline, false
} }
var oldPassword string var oldPassword string
@ -458,39 +458,39 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
NewPassword: newPassword, NewPassword: newPassword,
}) })
if err != nil { if err != nil {
return errors.Wrap(err, "Couldn't set password").Error() return errors.Wrap(err, "Couldn't set password").Error(), false
} }
case "config": case "config":
if len(args) > 1 { if len(args) > 1 {
var msg string var msg string
if gateway.MessageOutgoingPermissionVersion == 0 && args[0] == "carbons" && args[1] == "true" { 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" { 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" { if c.Session.NativeEdits && args[0] == "rawmessages" && args[1] == "false" {
_, err := c.Session.Set("nativeedits", "false") _, err := c.Session.Set("nativeedits", "false")
if err != nil { if err != nil {
return err.Error() return err.Error(), false
} }
msg = "Automatically disabling nativeedits too...\n" msg = "Automatically disabling nativeedits too...\n"
} }
value, err := c.Session.Set(args[0], args[1]) value, err := c.Session.Set(args[0], args[1])
if err != nil { if err != nil {
return err.Error() return err.Error(), false
} }
gateway.DirtySessions = true 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 { } else if len(args) > 0 {
value, err := c.Session.Get(args[0]) value, err := c.Session.Get(args[0])
if err != nil { 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 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)) 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": case "report":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0])
if err != nil { if err != nil {
return err.Error() return err.Error(), false
} }
if contact == nil { if contact == nil {
return "Contact not found" return "Contact not found", false
} }
text := rawCmdArguments(cmdline, 1) text := rawCmdArguments(cmdline, 1)
@ -515,9 +515,9 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
Text: text, Text: text,
}) })
if err != nil { if err != nil {
return err.Error() return err.Error(), false
} else { } else {
return "Reported" return "Reported", true
} }
case "add": case "add":
return c.cmdAdd(args) return c.cmdAdd(args)
@ -528,45 +528,45 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
case "channel": case "channel":
return c.cmdChannel(args, cmdline) return c.cmdChannel(args, cmdline)
case "help": 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 // ProcessChatCommand executes a command sent in a mapped chat
// and returns a response and the status of command support // and returns a response, the status of command support and the execution success result
func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) { func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool, bool) {
if !c.Online() { if !c.Online() {
return notOnline, true return notOnline, true, false
} }
cmd, args := parseCommand(cmdline) cmd, args := parseCommand(cmdline)
command, ok := chatCommands[cmd] command, ok := chatCommands[cmd]
if !ok { if !ok {
return unknownCommand, false return unknownCommand, false, false
} }
if len(args) < command.RequiredArgs { if len(args) < command.RequiredArgs {
return notEnoughArguments, true return notEnoughArguments, true, false
} }
chatType, chatTypeErr := c.GetChatType(chatID) chatType, chatTypeErr := c.GetChatType(chatID)
if chatTypeErr == nil && !IsCommandForChatType(command, chatType) { 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 { switch cmd {
// delete message // delete message
case "d": case "d":
if c.me == nil { if c.me == nil {
return "@me is not initialized", true return "@me is not initialized", true, false
} }
var limit int32 var limit int32
if len(args) > 0 { if len(args) > 0 {
limit64, err := strconv.ParseInt(args[0], 10, 32) limit64, err := strconv.ParseInt(args[0], 10, 32)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
limit = int32(limit64) limit = int32(limit64)
} else { } else {
@ -575,7 +575,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
messages, err := c.getLastMessages(chatID, "", c.me.Id, limit) messages, err := c.getLastMessages(chatID, "", c.me.Id, limit)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
log.Debugf("pre-deletion query: %#v %#v", messages, messages.Messages) 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, Revoke: true,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// edit message // edit message
case "s": case "s":
if c.me == nil { 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) messages, err := c.getLastMessages(chatID, "", c.me.Id, 1)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
if len(messages.Messages) == 0 { if len(messages.Messages) == 0 {
return "No last message", true return "No last message", true, false
} }
message := messages.Messages[0] message := messages.Messages[0]
if message == nil { if message == nil {
return "Last message is empty", true return "Last message is empty", true, false
} }
content := c.PrepareOutgoingMessageContent(rawCmdArguments(cmdline, 0)) content := c.PrepareOutgoingMessageContent(rawCmdArguments(cmdline, 0))
@ -622,10 +622,10 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
InputMessageContent: content, InputMessageContent: content,
}) })
if err != nil { if err != nil {
return "Message editing error", true return "Message editing error", true, false
} }
} else { } else {
return "Message processing error", true return "Message processing error", true, false
} }
// send without sound // send without sound
case "silent": case "silent":
@ -640,10 +640,10 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
}, },
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
} else { } else {
return "Message processing error", true return "Message processing error", true, false
} }
// schedule a message to timestamp or to going online // schedule a message to timestamp or to going online
case "schedule": case "schedule":
@ -700,7 +700,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
break 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 { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
return "Scheduled to " + result, true return "Scheduled to " + result, true, true
} else { } else {
return "Message processing error", true return "Message processing error", true, false
} }
// forward a message to chat // forward a message to chat
case "forward": case "forward":
messageId, err := strconv.ParseInt(args[0], 10, 64) messageId, err := strconv.ParseInt(args[0], 10, 64)
if err != nil { 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 targetChatParts := strings.Split(args[1], "@") // full JIDs are supported too
targetChatId, err := strconv.ParseInt(targetChatParts[0], 10, 64) targetChatId, err := strconv.ParseInt(targetChatParts[0], 10, 64)
if err != nil { 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{ messages, err := c.client.ForwardMessages(&client.ForwardMessagesRequest{
@ -740,7 +740,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
MessageIds: []int64{messageId}, MessageIds: []int64{messageId},
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
if messages != nil && messages.Messages != nil { if messages != nil && messages.Messages != nil {
for _, message := range messages.Messages { for _, message := range messages.Messages {
@ -751,7 +751,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
case "vcard": case "vcard":
info, err := c.GetVcardInfo(chatID) info, err := c.GetVcardInfo(chatID)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
_, link := c.PermastoreFile(info.Photo, true) _, link := c.PermastoreFile(info.Photo, true)
entries := []string{ entries := []string{
@ -761,26 +761,30 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
keyValueString("Full name", info.Given+" "+info.Family), keyValueString("Full name", info.Given+" "+info.Family),
keyValueString("Phone number", info.Tel), keyValueString("Phone number", info.Tel),
} }
return strings.Join(entries, "\n"), true return strings.Join(entries, "\n"), true, true
// add @contact // add @contact
case "add": case "add":
return c.cmdAdd(args), true response, success := c.cmdAdd(args)
return response, true, success
// join https://t.me/publichat or @publicchat // join https://t.me/publichat or @publicchat
case "join": case "join":
return c.cmdJoin(args), true response, success := c.cmdJoin(args)
return response, true, success
// create new supergroup // create new supergroup
case "supergroup": case "supergroup":
return c.cmdSupergroup(args, cmdline), true response, success := c.cmdSupergroup(args, cmdline)
return response, true, success
// create new channel // create new channel
case "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 // create new secret chat with current user
case "secret": case "secret":
_, err := c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{ _, err := c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{
UserId: chatID, UserId: chatID,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// create group chat with current user // create group chat with current user
case "group": case "group":
@ -789,7 +793,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
Title: args[0], Title: args[0],
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// blacklists current user // blacklists current user
case "block": case "block":
@ -798,7 +802,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
BlockList: &client.BlockListMain{}, BlockList: &client.BlockListMain{},
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// unblacklists current user // unblacklists current user
case "unblock": case "unblock":
@ -807,16 +811,16 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
BlockList: nil, BlockList: nil,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// invite @username to current groupchat // invite @username to current groupchat
case "invite": case "invite":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0])
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
if contact == nil { if contact == nil {
return "Contact not found", true return "Contact not found", true, false
} }
_, err = c.client.AddChatMember(&client.AddChatMemberRequest{ _, err = c.client.AddChatMember(&client.AddChatMemberRequest{
@ -825,7 +829,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
ForwardLimit: 100, ForwardLimit: 100,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// get link to current chat // get link to current chat
case "link": case "link":
@ -833,17 +837,17 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
ChatId: chatID, ChatId: chatID,
}) })
if err != nil { 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 // kick @username from current group chat
case "kick": case "kick":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0])
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
if contact == nil { if contact == nil {
return "Contact not found", true return "Contact not found", true, false
} }
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
@ -852,23 +856,23 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
Status: &client.ChatMemberStatusLeft{}, Status: &client.ChatMemberStatusLeft{},
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// mute @username [n hours] // mute @username [n hours]
case "mute": case "mute":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0])
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
if contact == nil { if contact == nil {
return "Contact not found", true return "Contact not found", true, false
} }
var hours int64 var hours int64
if len(args) > 1 { if len(args) > 1 {
hours, err = strconv.ParseInt(args[1], 10, 32) hours, err = strconv.ParseInt(args[1], 10, 32)
if err != nil { 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 { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// unmute @username // unmute @username
case "unmute": case "unmute":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0])
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
if contact == nil { if contact == nil {
return "Contact not found", true return "Contact not found", true, false
} }
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
@ -904,23 +908,23 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
}, },
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// ban @username from current chat [for N hours] // ban @username from current chat [for N hours]
case "ban": case "ban":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0])
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
if contact == nil { if contact == nil {
return "Contact not found", true return "Contact not found", true, false
} }
var hours int64 var hours int64
if len(args) > 1 { if len(args) > 1 {
hours, err = strconv.ParseInt(args[1], 10, 32) hours, err = strconv.ParseInt(args[1], 10, 32)
if err != nil { 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 { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// unban @username // unban @username
case "unban": case "unban":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0])
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
if contact == nil { if contact == nil {
return "Contact not found", true return "Contact not found", true, false
} }
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ _, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
@ -950,16 +954,16 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
Status: &client.ChatMemberStatusMember{}, Status: &client.ChatMemberStatusMember{},
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// promote @username to admin // promote @username to admin
case "promote": case "promote":
contact, _, err := c.GetContactByUsername(args[0]) contact, _, err := c.GetContactByUsername(args[0])
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
if contact == nil { if contact == nil {
return "Contact not found", true return "Contact not found", true, false
} }
// clone the permissions // clone the permissions
@ -978,7 +982,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
Status: &status, Status: &status,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// leave current chat // leave current chat
case "leave": case "leave":
@ -986,12 +990,12 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
ChatId: chatID, ChatId: chatID,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
err = c.unsubscribe(chatID) err = c.unsubscribe(chatID)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// leave current chat (for owners) // leave current chat (for owners)
case "leave!": case "leave!":
@ -999,12 +1003,12 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
ChatId: chatID, ChatId: chatID,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
err = c.unsubscribe(chatID) err = c.unsubscribe(chatID)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// set TTL // set TTL
case "ttl": case "ttl":
@ -1013,7 +1017,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
if len(args) > 0 { if len(args) > 0 {
ttl, err = strconv.ParseInt(args[0], 10, 32) ttl, err = strconv.ParseInt(args[0], 10, 32)
if err != nil { if err != nil {
return "Invalid TTL", true return "Invalid TTL", true, false
} }
} }
_, err = c.client.SetChatMessageAutoDeleteTime(&client.SetChatMessageAutoDeleteTimeRequest{ _, err = c.client.SetChatMessageAutoDeleteTime(&client.SetChatMessageAutoDeleteTimeRequest{
@ -1022,16 +1026,16 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// close secret chat // close secret chat
case "close": case "close":
chat, _, err := c.GetContactByID(chatID, nil) chat, _, err := c.GetContactByID(chatID, nil)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
if chat == nil { if chat == nil {
return "Chat not found", true return "Chat not found", true, false
} }
chatType := chat.Type.ChatTypeType() chatType := chat.Type.ChatTypeType()
@ -1041,12 +1045,12 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
SecretChatId: chatTypeSecret.SecretChatId, SecretChatId: chatTypeSecret.SecretChatId,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
err = c.unsubscribe(chatID) err = c.unsubscribe(chatID)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
} }
// delete current chat // delete current chat
@ -1057,12 +1061,12 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
Revoke: true, Revoke: true,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
err = c.unsubscribe(chatID) err = c.unsubscribe(chatID)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
// message search // message search
case "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) messages, err := c.getLastMessages(chatID, query, 0, limit)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
c.sendMessagesReverse(chatID, messages.Messages) c.sendMessagesReverse(chatID, messages.Messages)
@ -1110,7 +1114,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
Limit: limit, Limit: limit,
}) })
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
messages = append(messages, newMessages.Messages...) 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) members, err := c.GetChatMembers(chatID, false, query, MembersListMembers)
if err != nil { if err != nil {
return err.Error(), true return err.Error(), true, false
} }
var entries []string 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": case "help":
return c.helpString(CommandTypeChat, chatID), true return c.helpString(CommandTypeChat, chatID), true, true
default: 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{ chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{
Username: args[0], Username: args[0],
}) })
if err != nil { if err != nil {
return err.Error() return err.Error(), false
} }
if chat == nil { if chat == nil {
return "No error, but chat is nil" return "No error, but chat is nil", false
} }
c.subscribeToID(chat.Id, chat) 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], "@") { if strings.HasPrefix(args[0], "@") {
chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{ chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{
Username: args[0], Username: args[0],
}) })
if err != nil { if err != nil {
return err.Error() return err.Error(), false
} }
if chat == nil { if chat == nil {
return "No error, but chat is nil" return "No error, but chat is nil", false
} }
_, err = c.client.JoinChat(&client.JoinChatRequest{ _, err = c.client.JoinChat(&client.JoinChatRequest{
ChatId: chat.Id, ChatId: chat.Id,
}) })
if err != nil { if err != nil {
return err.Error() return err.Error(), false
} }
} else { } else {
_, err := c.client.JoinChatByInviteLink(&client.JoinChatByInviteLinkRequest{ _, err := c.client.JoinChatByInviteLink(&client.JoinChatByInviteLinkRequest{
InviteLink: args[0], InviteLink: args[0],
}) })
if err != nil { 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{ _, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
Title: args[0], Title: args[0],
Description: rawCmdArguments(cmdline, 1), Description: rawCmdArguments(cmdline, 1),
}) })
if err != nil { 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{ _, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
Title: args[0], Title: args[0],
Description: rawCmdArguments(cmdline, 1), Description: rawCmdArguments(cmdline, 1),
IsChannel: true, IsChannel: true,
}) })
if err != nil { if err != nil {
return err.Error() return err.Error(), false
} }
return "" return "", true
} }

View file

@ -1169,7 +1169,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
if replaceId == 0 && (strings.HasPrefix(text, "/") || strings.HasPrefix(text, "!")) { if replaceId == 0 && (strings.HasPrefix(text, "/") || strings.HasPrefix(text, "!")) {
// try to execute commands // try to execute commands
response, isCommand := c.ProcessChatCommand(chatID, text) response, isCommand, _ := c.ProcessChatCommand(chatID, text)
if response != "" { if response != "" {
c.returnMessage(returnJid, chatID, response) c.returnMessage(returnJid, chatID, response)
} }

View file

@ -230,7 +230,7 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
} else { } else {
toJid, err := stanza.NewJid(msg.To) toJid, err := stanza.NewJid(msg.To)
if err == nil && toJid.Bare() == gatewayJid && (strings.HasPrefix(msg.Body, "/") || strings.HasPrefix(msg.Body, "!")) { 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 != "" { if response != "" {
gateway.SendServiceMessage(msg.From, response, component) 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 response string
var success bool
if toOk { if toOk {
response, _ = session.ProcessChatCommand(toId, cmdString) response, _, success = session.ProcessChatCommand(toId, cmdString)
} else { } 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{ answer.Payload = &stanza.Command{
@ -874,7 +882,7 @@ func handleSetQueryCommand(s xmpp.Sender, iq *stanza.IQ, command *stanza.Command
Status: stanza.CommandStatusCompleted, Status: stanza.CommandStatusCompleted,
CommandElement: &stanza.Note{ CommandElement: &stanza.Note{
Text: response, Text: response,
Type: stanza.CommandNoteTypeInfo, Type: noteType,
}, },
} }