Fixed variable name verbosity

This commit is contained in:
0xCA 2023-12-29 01:35:50 +05:00
parent 2126bace57
commit c05eaf1744
2 changed files with 25 additions and 25 deletions

View file

@ -140,9 +140,9 @@ func init() {
lvl, _ := util.ParseLogLevel(util.LookupEnvOrString(util.LogLevel, "INFO")) lvl, _ := util.ParseLogLevel(util.LookupEnvOrString(util.LogLevel, "INFO"))
telegram.TelegramToken = flagTelegramToken telegram.Token = flagTelegramToken
telegram.TelegramAllowConfRequest = flagTelegramAllowConfRequest telegram.AllowConfRequest = flagTelegramAllowConfRequest
telegram.TelegramFloodWait = flagTelegramFloodWait telegram.FloodWait = flagTelegramFloodWait
telegram.LogLevel = lvl telegram.LogLevel = lvl
// print only if log level is INFO or lower // print only if log level is INFO or lower

View file

@ -18,13 +18,13 @@ type TgBotInitDependencies struct {
} }
var ( var (
TelegramToken string Token string
TelegramAllowConfRequest bool AllowConfRequest bool
TelegramFloodWait int FloodWait int
LogLevel log.Lvl LogLevel log.Lvl
TgBot *echotron.API Bot *echotron.API
TgBotMutex sync.RWMutex BotMutex sync.RWMutex
floodWait = make(map[int64]int64, 0) floodWait = make(map[int64]int64, 0)
) )
@ -32,16 +32,16 @@ var (
func Start(initDeps TgBotInitDependencies) (err error) { func Start(initDeps TgBotInitDependencies) (err error) {
ticker := time.NewTicker(time.Minute) ticker := time.NewTicker(time.Minute)
defer func() { defer func() {
TgBotMutex.Lock() BotMutex.Lock()
TgBot = nil Bot = nil
TgBotMutex.Unlock() BotMutex.Unlock()
ticker.Stop() ticker.Stop()
if r := recover(); r != nil { if r := recover(); r != nil {
err = fmt.Errorf("[PANIC] recovered from panic: %v", r) err = fmt.Errorf("[PANIC] recovered from panic: %v", r)
} }
}() }()
token := TelegramToken token := Token
if token == "" || len(token) < 30 { if token == "" || len(token) < 30 {
return return
} }
@ -54,9 +54,9 @@ func Start(initDeps TgBotInitDependencies) (err error) {
return return
} }
TgBotMutex.Lock() BotMutex.Lock()
TgBot = &bot Bot = &bot
TgBotMutex.Unlock() BotMutex.Unlock()
if LogLevel <= log.INFO { if LogLevel <= log.INFO {
fmt.Printf("[Telegram] Authorized as %s\n", res.Result.Username) fmt.Printf("[Telegram] Authorized as %s\n", res.Result.Username)
@ -68,7 +68,7 @@ func Start(initDeps TgBotInitDependencies) (err error) {
} }
}() }()
if !TelegramAllowConfRequest { if !AllowConfRequest {
return return
} }
@ -78,7 +78,7 @@ func Start(initDeps TgBotInitDependencies) (err error) {
userid := update.Message.Chat.ID userid := update.Message.Chat.ID
if _, wait := floodWait[userid]; wait { if _, wait := floodWait[userid]; wait {
bot.SendMessage( bot.SendMessage(
fmt.Sprintf("You can only request your configs once per %d minutes", TelegramFloodWait), fmt.Sprintf("You can only request your configs once per %d minutes", FloodWait),
userid, userid,
&echotron.MessageOptions{ &echotron.MessageOptions{
ReplyToMessageID: update.Message.ID, ReplyToMessageID: update.Message.ID,
@ -106,15 +106,15 @@ func Start(initDeps TgBotInitDependencies) (err error) {
} }
func SendConfig(userid int64, clientName string, confData, qrData []byte, ignoreFloodWait bool) error { func SendConfig(userid int64, clientName string, confData, qrData []byte, ignoreFloodWait bool) error {
TgBotMutex.RLock() BotMutex.RLock()
defer TgBotMutex.RUnlock() defer BotMutex.RUnlock()
if TgBot == nil { if Bot == nil {
return fmt.Errorf("telegram bot is not configured or not available") return fmt.Errorf("telegram bot is not configured or not available")
} }
if _, wait := floodWait[userid]; wait && !ignoreFloodWait { if _, wait := floodWait[userid]; wait && !ignoreFloodWait {
return fmt.Errorf("this client already got their config less than %d minutes ago", TelegramFloodWait) return fmt.Errorf("this client already got their config less than %d minutes ago", FloodWait)
} }
if !ignoreFloodWait { if !ignoreFloodWait {
@ -122,14 +122,14 @@ func SendConfig(userid int64, clientName string, confData, qrData []byte, ignore
} }
qrAttachment := echotron.NewInputFileBytes("qr.png", qrData) qrAttachment := echotron.NewInputFileBytes("qr.png", qrData)
_, err := TgBot.SendPhoto(qrAttachment, userid, &echotron.PhotoOptions{Caption: clientName}) _, err := Bot.SendPhoto(qrAttachment, userid, &echotron.PhotoOptions{Caption: clientName})
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return fmt.Errorf("unable to send qr picture") return fmt.Errorf("unable to send qr picture")
} }
confAttachment := echotron.NewInputFileBytes(clientName+".conf", confData) confAttachment := echotron.NewInputFileBytes(clientName+".conf", confData)
_, err = TgBot.SendDocument(confAttachment, userid, nil) _, err = Bot.SendDocument(confAttachment, userid, nil)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return fmt.Errorf("unable to send conf file") return fmt.Errorf("unable to send conf file")
@ -138,7 +138,7 @@ func SendConfig(userid int64, clientName string, confData, qrData []byte, ignore
} }
func updateFloodWait() { func updateFloodWait() {
thresholdTS := time.Now().Unix() - 60*int64(TelegramFloodWait) thresholdTS := time.Now().Unix() - 60*int64(FloodWait)
for userid, ts := range floodWait { for userid, ts := range floodWait {
if ts < thresholdTS { if ts < thresholdTS {
delete(floodWait, userid) delete(floodWait, userid)