116 lines
3.1 KiB
Go
116 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/pyed/transmission"
|
|
tgbotapi "gopkg.in/telegram-bot-api.v4"
|
|
"transmission-telegram/internal/bot"
|
|
"transmission-telegram/internal/config"
|
|
"transmission-telegram/internal/logger"
|
|
"transmission-telegram/internal/monitor"
|
|
transmissionClient "transmission-telegram/internal/transmission"
|
|
)
|
|
|
|
func main() {
|
|
// Load configuration
|
|
cfg, err := config.LoadConfig()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Configuration: %s\n\n", err)
|
|
config.PrintUsage()
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Initialize logger
|
|
var log *logger.Logger
|
|
var logf *os.File
|
|
if cfg.LogFile != "" {
|
|
var err error
|
|
logf, err = os.OpenFile(cfg.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Failed to open log file: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer logf.Close()
|
|
log = logger.New(logf)
|
|
} else {
|
|
log = logger.NewStdout()
|
|
}
|
|
|
|
// Log configuration (with masked secrets)
|
|
log.SafePrintf("[INFO] Token=%s\n\t\tMasters=%v\n\t\tURL=%s\n\t\tUSER=%s\n\t\tPASS=%s",
|
|
logger.MaskSecret(cfg.BotToken), cfg.Masters, cfg.RPCURL,
|
|
logger.MaskSecret(cfg.Username),
|
|
logger.MaskSecret(cfg.Password))
|
|
|
|
// Initialize Transmission client
|
|
transClient, err := transmission.New(cfg.RPCURL, cfg.Username, cfg.Password)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Transmission: Make sure you have the right URL, Username and Password\n")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Wrap with caching
|
|
cachedClient := transmissionClient.NewCachedClient(transClient, 2*time.Second)
|
|
|
|
// Initialize Telegram bot
|
|
telegramBotAPI, err := tgbotapi.NewBotAPI(cfg.BotToken)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Telegram: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
log.Printf("[INFO] Authorized: %s", telegramBotAPI.Self.UserName)
|
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
u.Timeout = config.TelegramUpdateTimeout
|
|
|
|
updates, err := telegramBotAPI.GetUpdatesChan(u)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Telegram: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Initialize monitor
|
|
mon := monitor.NewMonitor(cachedClient, log, config.DefaultPollInterval)
|
|
|
|
// Create bot instance first
|
|
telegramBot := bot.NewBot(telegramBotAPI, cachedClient, cfg, log, updates, mon)
|
|
|
|
// Set up completion callback - chatID will be set by bot when user sends first message
|
|
mon.SetOnComplete(func(torrent *transmission.Torrent) {
|
|
msg := fmt.Sprintf("✅ Completed: %s", torrent.Name)
|
|
// Send via bot helper - bot will handle chatID internally
|
|
telegramBot.SendCompletionNotification(msg)
|
|
})
|
|
|
|
// Start monitoring
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
go mon.Start(ctx)
|
|
|
|
// Handle log file monitoring if configured (will be implemented in monitor package)
|
|
if cfg.TransLogFile != "" {
|
|
log.Printf("[INFO] Log file monitoring configured but not yet fully implemented: %s", cfg.TransLogFile)
|
|
}
|
|
|
|
// Handle graceful shutdown
|
|
sigChan := make(chan os.Signal, 1)
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
// Start bot in goroutine
|
|
go telegramBot.Run(ctx)
|
|
|
|
// Wait for shutdown signal
|
|
<-sigChan
|
|
log.Printf("[INFO] Shutting down...")
|
|
cancel()
|
|
}
|
|
|
|
|