2021-05-07 00:08:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
tg "github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
)
|
|
|
|
|
2023-06-21 16:59:58 +00:00
|
|
|
const (
|
|
|
|
VERIF_TOKEN_EXPIRY_SEC = 10 * 60
|
|
|
|
)
|
|
|
|
|
2021-05-17 22:42:33 +00:00
|
|
|
type TelegramVerifiedToken struct {
|
2023-06-21 17:26:08 +00:00
|
|
|
ChatID int64
|
|
|
|
Username string
|
|
|
|
JellyfinID string // optional, for ensuring a user-requested change is only accessed by them.
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifToken stores details about a pending user verification token.
|
|
|
|
type VerifToken struct {
|
|
|
|
Expiry time.Time
|
|
|
|
JellyfinID string // optional, for ensuring a user-requested change is only accessed by them.
|
2021-05-07 13:32:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
type TelegramDaemon struct {
|
|
|
|
Stopped bool
|
|
|
|
ShutdownChannel chan string
|
|
|
|
bot *tg.BotAPI
|
|
|
|
username string
|
2023-06-21 17:26:08 +00:00
|
|
|
tokens map[string]VerifToken // Map of pins to tokens.
|
|
|
|
verifiedTokens map[string]TelegramVerifiedToken // Map of token pins to the responsible ChatID+Username.
|
2023-06-21 16:59:58 +00:00
|
|
|
languages map[int64]string // Store of languages for chatIDs. Added to on first interaction, and loaded from app.storage.telegram on start.
|
2021-05-07 00:08:12 +00:00
|
|
|
link string
|
|
|
|
app *appContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTelegramDaemon(app *appContext) (*TelegramDaemon, error) {
|
|
|
|
token := app.config.Section("telegram").Key("token").String()
|
|
|
|
if token == "" {
|
|
|
|
return nil, fmt.Errorf("token was blank")
|
|
|
|
}
|
|
|
|
bot, err := tg.NewBotAPI(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-07 13:32:51 +00:00
|
|
|
td := &TelegramDaemon{
|
2021-05-07 00:08:12 +00:00
|
|
|
ShutdownChannel: make(chan string),
|
|
|
|
bot: bot,
|
|
|
|
username: bot.Self.UserName,
|
2023-06-21 17:26:08 +00:00
|
|
|
tokens: map[string]VerifToken{},
|
2023-06-21 16:59:58 +00:00
|
|
|
verifiedTokens: map[string]TelegramVerifiedToken{},
|
2021-05-07 13:32:51 +00:00
|
|
|
languages: map[int64]string{},
|
2021-05-07 00:08:12 +00:00
|
|
|
link: "https://t.me/" + bot.Self.UserName,
|
|
|
|
app: app,
|
2021-05-07 13:32:51 +00:00
|
|
|
}
|
2023-06-20 11:19:24 +00:00
|
|
|
for _, user := range app.storage.GetTelegram() {
|
2021-05-07 13:32:51 +00:00
|
|
|
if user.Lang != "" {
|
|
|
|
td.languages[user.ChatID] = user.Lang
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return td, nil
|
2021-05-07 00:08:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 22:42:33 +00:00
|
|
|
func genAuthToken() string {
|
2021-05-07 00:08:12 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
pin := make([]rune, 8)
|
|
|
|
for i := range pin {
|
2021-05-29 16:43:11 +00:00
|
|
|
if (i+1)%3 == 0 {
|
2021-05-07 00:08:12 +00:00
|
|
|
pin[i] = '-'
|
|
|
|
} else {
|
|
|
|
pin[i] = runes[rand.Intn(len(runes))]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(pin)
|
|
|
|
}
|
|
|
|
|
2021-05-17 22:42:33 +00:00
|
|
|
var runes = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
|
|
|
|
|
|
|
// NewAuthToken generates an 8-character pin in the form "A1-2B-CD".
|
|
|
|
func (t *TelegramDaemon) NewAuthToken() string {
|
|
|
|
pin := genAuthToken()
|
2023-06-21 17:26:08 +00:00
|
|
|
t.tokens[pin] = VerifToken{Expiry: time.Now().Add(VERIF_TOKEN_EXPIRY_SEC * time.Second), JellyfinID: ""}
|
|
|
|
return pin
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAssignedAuthToken generates an 8-character pin in the form "A1-2B-CD",
|
|
|
|
// and assigns it for access only with the given Jellyfin ID.
|
|
|
|
func (t *TelegramDaemon) NewAssignedAuthToken(id string) string {
|
|
|
|
pin := genAuthToken()
|
|
|
|
t.tokens[pin] = VerifToken{Expiry: time.Now().Add(VERIF_TOKEN_EXPIRY_SEC * time.Second), JellyfinID: id}
|
2021-05-17 22:42:33 +00:00
|
|
|
return pin
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (t *TelegramDaemon) run() {
|
|
|
|
t.app.info.Println("Starting Telegram bot daemon")
|
|
|
|
u := tg.NewUpdate(0)
|
|
|
|
u.Timeout = 60
|
|
|
|
updates, err := t.bot.GetUpdatesChan(u)
|
|
|
|
if err != nil {
|
|
|
|
t.app.err.Printf("Failed to start Telegram daemon: %v", err)
|
2021-05-23 13:48:36 +00:00
|
|
|
telegramEnabled = false
|
2021-05-07 00:08:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
var upd tg.Update
|
|
|
|
select {
|
|
|
|
case upd = <-updates:
|
|
|
|
if upd.Message == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sects := strings.Split(upd.Message.Text, " ")
|
|
|
|
if len(sects) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lang := t.app.storage.lang.chosenTelegramLang
|
2021-05-07 13:32:51 +00:00
|
|
|
storedLang, ok := t.languages[upd.Message.Chat.ID]
|
2021-05-07 00:08:12 +00:00
|
|
|
if !ok {
|
2021-05-07 13:32:51 +00:00
|
|
|
found := false
|
2021-05-07 00:08:12 +00:00
|
|
|
for code := range t.app.storage.lang.Telegram {
|
|
|
|
if code[:2] == upd.Message.From.LanguageCode {
|
|
|
|
lang = code
|
2021-05-07 13:32:51 +00:00
|
|
|
found = true
|
2021-05-07 00:08:12 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 13:32:51 +00:00
|
|
|
if found {
|
|
|
|
t.languages[upd.Message.Chat.ID] = lang
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lang = storedLang
|
2021-05-07 00:08:12 +00:00
|
|
|
}
|
|
|
|
switch msg := sects[0]; msg {
|
|
|
|
case "/start":
|
2021-05-16 14:00:13 +00:00
|
|
|
t.commandStart(&upd, sects, lang)
|
2021-05-07 00:08:12 +00:00
|
|
|
continue
|
|
|
|
case "/lang":
|
2021-05-16 14:00:13 +00:00
|
|
|
t.commandLang(&upd, sects, lang)
|
2021-05-07 00:08:12 +00:00
|
|
|
continue
|
|
|
|
default:
|
2021-05-16 14:00:13 +00:00
|
|
|
t.commandPIN(&upd, sects, lang)
|
2021-05-07 00:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case <-t.ShutdownChannel:
|
2021-05-07 17:29:56 +00:00
|
|
|
t.bot.StopReceivingUpdates()
|
2021-05-07 00:08:12 +00:00
|
|
|
t.ShutdownChannel <- "Down"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) Reply(upd *tg.Update, content string) error {
|
|
|
|
msg := tg.NewMessage((*upd).Message.Chat.ID, content)
|
|
|
|
_, err := t.bot.Send(msg)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) QuoteReply(upd *tg.Update, content string) error {
|
|
|
|
msg := tg.NewMessage((*upd).Message.Chat.ID, content)
|
|
|
|
msg.ReplyToMessageID = (*upd).Message.MessageID
|
|
|
|
_, err := t.bot.Send(msg)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:03:13 +00:00
|
|
|
var escapedChars = []string{"_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(", "\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>", "#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|", "\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!"}
|
|
|
|
var escaper = strings.NewReplacer(escapedChars...)
|
|
|
|
|
2021-05-07 15:33:44 +00:00
|
|
|
// Send will send a telegram message to a list of chat IDs. message.text is used if no markdown is given.
|
2021-05-07 15:06:47 +00:00
|
|
|
func (t *TelegramDaemon) Send(message *Message, ID ...int64) error {
|
|
|
|
for _, id := range ID {
|
2021-05-07 15:33:44 +00:00
|
|
|
var msg tg.MessageConfig
|
|
|
|
if message.Markdown == "" {
|
|
|
|
msg = tg.NewMessage(id, message.Text)
|
|
|
|
} else {
|
2021-05-25 22:03:13 +00:00
|
|
|
text := escaper.Replace(message.Markdown)
|
2021-05-22 22:32:30 +00:00
|
|
|
msg = tg.NewMessage(id, text)
|
2021-05-07 15:33:44 +00:00
|
|
|
msg.ParseMode = "MarkdownV2"
|
|
|
|
}
|
2021-05-07 15:06:47 +00:00
|
|
|
_, err := t.bot.Send(msg)
|
2021-05-07 00:08:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) Shutdown() {
|
|
|
|
t.Stopped = true
|
|
|
|
t.ShutdownChannel <- "Down"
|
|
|
|
<-t.ShutdownChannel
|
|
|
|
close(t.ShutdownChannel)
|
|
|
|
}
|
2021-05-16 14:00:13 +00:00
|
|
|
|
|
|
|
func (t *TelegramDaemon) commandStart(upd *tg.Update, sects []string, lang string) {
|
|
|
|
content := t.app.storage.lang.Telegram[lang].Strings.get("startMessage") + "\n"
|
2021-05-18 17:41:42 +00:00
|
|
|
content += t.app.storage.lang.Telegram[lang].Strings.template("languageMessage", tmpl{"command": "/lang"})
|
2021-05-16 14:00:13 +00:00
|
|
|
err := t.Reply(upd, content)
|
|
|
|
if err != nil {
|
|
|
|
t.app.err.Printf("Telegram: Failed to send message to \"%s\": %v", upd.Message.From.UserName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) commandLang(upd *tg.Update, sects []string, lang string) {
|
|
|
|
if len(sects) == 1 {
|
2021-05-18 17:41:42 +00:00
|
|
|
list := "/lang `<lang>`\n"
|
2021-05-16 14:00:13 +00:00
|
|
|
for code := range t.app.storage.lang.Telegram {
|
2021-05-18 17:41:42 +00:00
|
|
|
list += fmt.Sprintf("`%s`: %s\n", code, t.app.storage.lang.Telegram[code].Meta.Name)
|
2021-05-16 14:00:13 +00:00
|
|
|
}
|
|
|
|
err := t.Reply(upd, list)
|
|
|
|
if err != nil {
|
|
|
|
t.app.err.Printf("Telegram: Failed to send message to \"%s\": %v", upd.Message.From.UserName, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := t.app.storage.lang.Telegram[sects[1]]; ok {
|
|
|
|
t.languages[upd.Message.Chat.ID] = sects[1]
|
2023-06-20 11:19:24 +00:00
|
|
|
for jfID, user := range t.app.storage.GetTelegram() {
|
2021-05-16 14:00:13 +00:00
|
|
|
if user.ChatID == upd.Message.Chat.ID {
|
|
|
|
user.Lang = sects[1]
|
2023-06-20 11:19:24 +00:00
|
|
|
t.app.storage.SetTelegramKey(jfID, user)
|
2021-05-18 17:41:42 +00:00
|
|
|
if err := t.app.storage.storeTelegramUsers(); err != nil {
|
2021-05-16 14:00:13 +00:00
|
|
|
t.app.err.Printf("Failed to store Telegram users: %v", err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) commandPIN(upd *tg.Update, sects []string, lang string) {
|
2023-06-21 17:26:08 +00:00
|
|
|
token, ok := t.tokens[upd.Message.Text]
|
|
|
|
if !ok || time.Now().After(token.Expiry) {
|
2021-05-16 14:00:13 +00:00
|
|
|
err := t.QuoteReply(upd, t.app.storage.lang.Telegram[lang].Strings.get("invalidPIN"))
|
|
|
|
if err != nil {
|
|
|
|
t.app.err.Printf("Telegram: Failed to send message to \"%s\": %v", upd.Message.From.UserName, err)
|
|
|
|
}
|
2023-06-21 16:59:58 +00:00
|
|
|
delete(t.tokens, upd.Message.Text)
|
2021-05-16 14:00:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err := t.QuoteReply(upd, t.app.storage.lang.Telegram[lang].Strings.get("pinSuccess"))
|
|
|
|
if err != nil {
|
|
|
|
t.app.err.Printf("Telegram: Failed to send message to \"%s\": %v", upd.Message.From.UserName, err)
|
|
|
|
}
|
2023-06-21 16:59:58 +00:00
|
|
|
t.verifiedTokens[upd.Message.Text] = TelegramVerifiedToken{
|
2023-06-21 17:26:08 +00:00
|
|
|
ChatID: upd.Message.Chat.ID,
|
|
|
|
Username: upd.Message.Chat.UserName,
|
|
|
|
JellyfinID: token.JellyfinID,
|
2023-06-21 16:59:58 +00:00
|
|
|
}
|
|
|
|
delete(t.tokens, upd.Message.Text)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokenVerified returns whether or not a token with the given PIN has been verified, and the token itself.
|
|
|
|
func (t *TelegramDaemon) TokenVerified(pin string) (token TelegramVerifiedToken, ok bool) {
|
|
|
|
token, ok = t.verifiedTokens[pin]
|
|
|
|
// delete(t.verifiedTokens, pin)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-21 17:26:08 +00:00
|
|
|
// AssignedTokenVerified returns whether or not a token with the given PIN has been verified, and the token itself.
|
|
|
|
// Returns false if the given Jellyfin ID does not match the one in the token.
|
|
|
|
func (t *TelegramDaemon) AssignedTokenVerified(pin string, jfID string) (token TelegramVerifiedToken, ok bool) {
|
|
|
|
token, ok = t.verifiedTokens[pin]
|
|
|
|
if ok && token.JellyfinID != jfID {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
// delete(t.verifiedTokens, pin)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-21 16:59:58 +00:00
|
|
|
// UserExists returns whether or not a user with the given username exists.
|
|
|
|
func (t *TelegramDaemon) UserExists(username string) (ok bool) {
|
|
|
|
ok = false
|
|
|
|
for _, u := range t.app.storage.GetTelegram() {
|
|
|
|
if u.Username == username {
|
|
|
|
ok = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVerifiedToken removes the token with the given PIN.
|
|
|
|
func (t *TelegramDaemon) DeleteVerifiedToken(pin string) {
|
|
|
|
delete(t.verifiedTokens, pin)
|
2021-05-16 14:00:13 +00:00
|
|
|
}
|