2021-05-07 00:08:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2024-08-09 20:40:03 +00:00
|
|
|
"net/http"
|
2021-05-07 00:08:12 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
tg "github.com/go-telegram-bot-api/telegram-bot-api"
|
2024-08-01 19:17:05 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2023-06-24 16:01:52 +00:00
|
|
|
"github.com/timshannon/badgerhold/v4"
|
2021-05-07 00:08:12 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
2024-08-03 20:23:59 +00:00
|
|
|
JellyfinID string `badgerhold:"key"` // optional, for ensuring a user-requested change is only accessed by them.
|
|
|
|
ChatID int64 `badgerhold:"index"`
|
|
|
|
Username string `badgerhold:"index"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tv TelegramVerifiedToken) ToUser() *TelegramUser {
|
|
|
|
return &TelegramUser{
|
|
|
|
TelegramVerifiedToken: tv,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramVerifiedToken) Name() string { return t.Username }
|
|
|
|
func (t *TelegramVerifiedToken) SetMethodID(id any) { t.ChatID = id.(int64) }
|
|
|
|
func (t *TelegramVerifiedToken) MethodID() any { return t.ChatID }
|
|
|
|
func (t *TelegramVerifiedToken) SetJellyfin(id string) { t.JellyfinID = id }
|
|
|
|
func (t *TelegramVerifiedToken) Jellyfin() string { return t.JellyfinID }
|
|
|
|
func (t *TelegramUser) SetAllowContactFromDTO(req newUserDTO) { t.Contact = req.TelegramContact }
|
|
|
|
func (t *TelegramUser) SetAllowContact(contact bool) { t.Contact = contact }
|
|
|
|
func (t *TelegramUser) AllowContact() bool { return t.Contact }
|
|
|
|
func (t *TelegramUser) Store(st *Storage) {
|
|
|
|
st.SetTelegramKey(t.Jellyfin(), *t)
|
2023-06-21 17:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2024-08-09 20:40:03 +00:00
|
|
|
// SetTransport sets the http.Transport to use for requests. Can be used to set a proxy.
|
|
|
|
func (d *TelegramDaemon) SetTransport(t *http.Transport) {
|
|
|
|
d.bot.Client.Transport = t
|
|
|
|
}
|
|
|
|
|
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() {
|
2024-08-01 19:17:05 +00:00
|
|
|
t.app.info.Println(lm.StartDaemon, lm.Telegram)
|
2021-05-07 00:08:12 +00:00
|
|
|
u := tg.NewUpdate(0)
|
|
|
|
u.Timeout = 60
|
|
|
|
updates, err := t.bot.GetUpdatesChan(u)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
t.app.err.Printf(lm.FailedStartDaemon, lm.Telegram, 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 {
|
2024-08-01 19:17:05 +00:00
|
|
|
t.app.err.Printf(lm.FailedReply, lm.Telegram, upd.Message.From.UserName, err)
|
2021-05-16 14:00:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-08-01 19:17:05 +00:00
|
|
|
t.app.err.Printf(lm.FailedReply, lm.Telegram, upd.Message.From.UserName, err)
|
2021-05-16 14:00:13 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := t.app.storage.lang.Telegram[sects[1]]; ok {
|
|
|
|
t.languages[upd.Message.Chat.ID] = sects[1]
|
2023-06-24 16:01:52 +00:00
|
|
|
for _, 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-24 16:01:52 +00:00
|
|
|
t.app.storage.SetTelegramKey(user.JellyfinID, user)
|
2021-05-16 14:00:13 +00:00
|
|
|
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 {
|
2024-08-01 19:17:05 +00:00
|
|
|
t.app.err.Printf(lm.FailedReply, lm.Telegram, upd.Message.From.UserName, err)
|
2021-05-16 14:00:13 +00:00
|
|
|
}
|
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 {
|
2024-08-01 19:17:05 +00:00
|
|
|
t.app.err.Printf(lm.FailedReply, lm.Telegram, upd.Message.From.UserName, err)
|
2021-05-16 14:00:13 +00:00
|
|
|
}
|
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.
|
2023-06-24 16:01:52 +00:00
|
|
|
func (t *TelegramDaemon) UserExists(username string) bool {
|
|
|
|
c, err := t.app.storage.db.Count(&TelegramUser{}, badgerhold.Where("Username").Eq(username))
|
|
|
|
return err != nil || c > 0
|
2023-06-21 16:59:58 +00:00
|
|
|
}
|
|
|
|
|
2024-08-03 20:23:59 +00:00
|
|
|
// Exists returns whether or not the given user exists.
|
|
|
|
func (t *TelegramDaemon) Exists(user ContactMethodUser) bool {
|
|
|
|
return t.UserExists(user.Name())
|
|
|
|
}
|
|
|
|
|
2023-06-21 16:59:58 +00:00
|
|
|
// DeleteVerifiedToken removes the token with the given PIN.
|
2024-08-03 20:23:59 +00:00
|
|
|
func (t *TelegramDaemon) DeleteVerifiedToken(PIN string) {
|
|
|
|
delete(t.verifiedTokens, PIN)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) PIN(req newUserDTO) string { return req.TelegramPIN }
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) Name() string { return lm.Telegram }
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) Required() bool {
|
|
|
|
return t.app.config.Section("telegram").Key("required").MustBool(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) UniqueRequired() bool {
|
|
|
|
return t.app.config.Section("telegram").Key("require_unique").MustBool(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) UserVerified(PIN string) (ContactMethodUser, bool) {
|
|
|
|
token, ok := t.TokenVerified(PIN)
|
|
|
|
if !ok {
|
|
|
|
return &TelegramUser{}, false
|
|
|
|
}
|
|
|
|
tu := token.ToUser()
|
|
|
|
if lang, ok := t.languages[tu.ChatID]; ok {
|
|
|
|
tu.Lang = lang
|
|
|
|
}
|
|
|
|
|
|
|
|
return tu, ok
|
2021-05-16 14:00:13 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
|
|
|
|
func (t *TelegramDaemon) PostVerificationTasks(string, ContactMethodUser) error { return nil }
|