telegabber/telegram/commands.go

254 lines
6.8 KiB
Go
Raw Normal View History

2019-11-24 17:10:29 +00:00
package telegram
2019-11-26 00:04:11 +00:00
import (
2019-12-05 19:56:12 +00:00
"fmt"
2019-12-05 18:13:17 +00:00
"github.com/pkg/errors"
2019-12-04 23:10:08 +00:00
"strconv"
2019-11-26 00:04:11 +00:00
"strings"
2019-12-04 23:10:08 +00:00
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
2019-12-05 18:13:17 +00:00
"github.com/zelenin/go-tdlib/client"
2019-11-26 00:04:11 +00:00
)
2019-11-24 17:10:29 +00:00
const notEnoughArguments string = "Not enough arguments"
const telegramNotInitialized string = "Telegram connection is not initialized yet"
2019-11-26 00:04:11 +00:00
var transportCommands = map[string]command{
2019-12-05 18:13:17 +00:00
"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"},
2019-12-05 19:56:12 +00:00
"config": command{"[param] [value]", "view or update configuration options"},
2019-11-26 00:04:11 +00:00
}
var chatCommands = map[string]command{
//"d": command{"[n]", "delete your last message(s)"},
//"s": command{"regex replace", "edit your last message"},
//"add": command{"@username", "add @username to your chat list"},
//"join": command{"https://t.me/invite_link", "join to chat via invite link"},
//"group": command{"title", "create groupchat «title» with current user"},
//"supergroup": command{"title description", "create new supergroup «title» with «description»"},
//"channel": command{"title description", "create new channel «title» with «description»"},
//"secret": command{"", "create secretchat with current user"},
//"search": command{"string [limit]", "search <string> in current chat"},
//"history": command{"[limit]", "get last [limit] messages from current chat"},
//"block": command{"", "blacklist current user"},
//"unblock": command{"", "unblacklist current user"},
//"invite": command{"id or @username", "add user to current chat"},
//"kick": command{"id or @username", "remove user to current chat"},
//"ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"},
//"leave": command{"", "leave current chat"},
//"close": command{"", "close current secret chat"},
//"delete": command{"", "delete current chat from chat list"},
//"members": command{"[query]", "search members [by optional query] in current chat (requires admin rights)"},
}
var transportConfigurationOptions = map[string]configurationOption{
//"timezone": configurationOption{"00:00", "adjust timezone for Telegram user statuses"}
}
type command struct {
arguments string
description string
}
type configurationOption command
type helpType int
const (
helpTypeTransport helpType = iota
helpTypeChat
)
func helpString(ht helpType) string {
var str strings.Builder
var commandMap map[string]command
switch ht {
case helpTypeTransport:
commandMap = transportCommands
case helpTypeChat:
commandMap = chatCommands
}
str.WriteString("Available commands:\n")
for name, command := range commandMap {
str.WriteString("/")
str.WriteString(name)
if command.arguments != "" {
str.WriteString(" ")
str.WriteString(command.arguments)
}
str.WriteString(" — ")
str.WriteString(command.description)
str.WriteString("\n")
}
if ht == helpTypeTransport {
str.WriteString("Configuration options\n")
for name, option := range transportConfigurationOptions {
str.WriteString(name)
str.WriteString(" ")
str.WriteString(option.arguments)
str.WriteString(" — ")
str.WriteString(option.description)
str.WriteString("\n")
}
}
return str.String()
}
2019-12-03 00:32:53 +00:00
func parseCommand(cmdline string) (string, []string) {
bodyFields := strings.Fields(cmdline)
return bodyFields[0][1:], bodyFields[1:]
}
// ProcessTransportCommand executes a command sent directly to the component
// and returns a response
func (c *Client) ProcessTransportCommand(cmdline string) string {
cmd, args := parseCommand(cmdline)
2019-11-24 17:10:29 +00:00
switch cmd {
case "login", "code", "password":
if cmd == "login" && c.Session.Login != "" {
return ""
}
if len(args) < 1 {
return notEnoughArguments
}
if c.authorizer == nil {
return telegramNotInitialized
}
switch cmd {
2019-12-04 23:10:08 +00:00
// sign in
2019-11-24 17:10:29 +00:00
case "login":
c.authorizer.PhoneNumber <- args[0]
c.Session.Login = args[0]
2019-12-04 23:10:08 +00:00
// check auth code
2019-11-24 17:10:29 +00:00
case "code":
c.authorizer.Code <- args[0]
2019-12-04 23:10:08 +00:00
// check auth password
2019-11-24 17:10:29 +00:00
case "password":
c.authorizer.Password <- args[0]
}
2019-12-04 23:10:08 +00:00
// sign out
case "logout":
_, err := c.client.LogOut()
if err != nil {
2019-12-05 18:13:17 +00:00
return errors.Wrap(err, "Logout error").Error()
2019-12-04 23:10:08 +00:00
}
for id := range c.cache.chats {
gateway.SendPresence(
c.xmpp,
c.jid,
gateway.SPFrom(strconv.FormatInt(id, 10)),
gateway.SPType("unsubscribed"),
)
}
c.Session.Login = ""
2019-12-05 18:13:17 +00:00
// set @username
case "setusername":
var username string
if len(args) > 0 {
username = args[0]
}
_, err := c.client.SetUsername(&client.SetUsernameRequest{
Username: username,
})
if err != nil {
return errors.Wrap(err, "Couldn't set username").Error()
}
// set My Name
case "setname":
var firstname string
var lastname string
if len(args) > 0 {
firstname = args[0]
}
if len(args) > 1 {
lastname = args[1]
}
_, err := c.client.SetName(&client.SetNameRequest{
FirstName: firstname,
LastName: lastname,
})
if err != nil {
return errors.Wrap(err, "Couldn't set name").Error()
}
// set About
case "setbio":
_, err := c.client.SetBio(&client.SetBioRequest{
Bio: strings.Join(args, " "),
})
if err != nil {
return errors.Wrap(err, "Couldn't set bio").Error()
}
// set password
case "setpassword":
var oldPassword string
var newPassword string
// 0 or 1 argument is ignored and the password is reset
if len(args) > 1 {
oldPassword = args[0]
newPassword = args[1]
}
_, err := c.client.SetPassword(&client.SetPasswordRequest{
OldPassword: oldPassword,
NewPassword: newPassword,
})
if err != nil {
return errors.Wrap(err, "Couldn't set password").Error()
}
2019-12-05 19:56:12 +00:00
case "config":
if len(args) > 1 {
value, err := c.Session.Set(args[0], args[1])
if err != nil {
return err.Error()
}
return fmt.Sprintf("%s set to %s", args[0], value)
} else if len(args) > 0 {
value, err := c.Session.Get(args[0])
if err != nil {
return err.Error()
}
return fmt.Sprintf("%s is set to %s", args[0], value)
}
var entries []string
for key, value := range c.Session.ToMap() {
entries = append(entries, fmt.Sprintf("%s is set to %s", key, value))
}
return strings.Join(entries, "\n")
2019-11-26 00:04:11 +00:00
case "help":
return helpString(helpTypeTransport)
2019-11-24 17:10:29 +00:00
}
return ""
}
2019-12-03 00:32:53 +00:00
// ProcessChatCommand executes a command sent in a mapped chat
// and returns a response and the status of command support
func (c *Client) ProcessChatCommand(cmdline string) (string, bool) {
cmd, _ := parseCommand(cmdline)
switch cmd {
case "help":
2019-12-03 16:48:41 +00:00
return helpString(helpTypeChat), true
2019-12-03 00:32:53 +00:00
}
2019-12-03 16:48:41 +00:00
return "", false
2019-12-03 00:32:53 +00:00
}