package main import ( "html/template" "log" "net" "os" "path/filepath" "sync" "sync/atomic" "time" "git.sr.ht/~michalr/go-satel" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) const ( PersistenceFilename = "hs_wro_last_seen.bin" retryDelaySec = 25 ) type RealSleeper struct { duration time.Duration } func (self RealSleeper) Sleep(ch chan<- interface{}) { go func() { time.Sleep(self.duration) ch <- nil }() } func makeSatel(satelAddr string, poolInterval time.Duration) *satel.Satel { satelConn, err := net.Dial("tcp", satelAddr) if err != nil { panic(err) } return satel.NewConfig(satelConn, satel.Config{EventsQueueSize: 10, PoolingInterval: poolInterval}) } func getPersistenceFilePath() string { var stateDir = os.Getenv("STATE_DIRECTORY") if len(stateDir) != 0 { return filepath.Join(stateDir, PersistenceFilename) } return PersistenceFilename } func main() { var ( wg sync.WaitGroup tgEvents = make(chan GenericMessage, 5) logger = log.New(os.Stderr, "Main", log.Lmicroseconds) sleeper = RealSleeper{time.Second * 60} cleanShutdown = atomic.Bool{} ) cleanShutdown.Store(false) config := MakeConfig(logger) s := makeSatel(config.SatelAddr, config.PoolInterval.GetDuration()) logger.Printf("Connected to Satel: %s", config.SatelAddr) var bot TelegramBot = nil if len(config.TelegramApiKey) != 0 { b, err := tgbotapi.NewBotAPI(config.TelegramApiKey) if err != nil { panic(err) } logger.Print("Created Telegram Bot API client") bot = b } else { bot = EmptyBot{} } tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), config.ChatIds} tgTpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate)) ircTpl := template.Must(template.New("IRCMessage").Parse(IRCMessageTemplate)) dataStore := MakeDataStore(log.New(os.Stderr, "DataStore", log.Lmicroseconds), getPersistenceFilePath()) collect := CollectFromChannel[satel.Event]{} filterByLastSeen := MakeFilterByLastSeen(&dataStore) filterByTypeOrIndex := MakeFilterByTypeOrIndex(config.AllowedTypes, config.AllowedIndexes) convert := MakeConvert( func(ev satel.Event) GenericMessage { return GenericMessage{ev.BasicEvents} }, ) notifyViaHttp := MakeNofityViaHTTPSync(config, log.New(os.Stderr, "HTTPNotify", log.Lmicroseconds)) throttle := MakeThrottleSync(sleeper, log.New(os.Stderr, "MessageThrottle", log.Lmicroseconds), &wg) sendToTg := MakeSendToTelegramSync(tgSender, log.New(os.Stderr, "SendToTg", log.Lmicroseconds), tgTpl) sendToMatterbridge := MakeSendToMatterbridgeSync(s, config, log.New(os.Stderr, "SendToMatterbridge", log.Lmicroseconds), ircTpl) collect.Then(filterByLastSeen). Then(filterByTypeOrIndex). Then(convert) convert.ConvertTo(notifyViaHttp). Then(throttle). Then(sendToTg). Then(sendToMatterbridge) collect.Collect(s.Events, &wg, func() {}) wg.Add(1) go func() { HandleTelegramCommands(bot, log.New(os.Stderr, "CommandHandler", log.Lmicroseconds), &dataStore, config, tgTpl, s) wg.Done() }() go CloseSatelOnCtrlC(s, &cleanShutdown) closeDebugTools := make(chan interface{}) if config.WriteMemoryProfile { WriteMemoryProfilePeriodically(&wg, log.New(os.Stderr, "DebugTools", log.Lmicroseconds), closeDebugTools) } logger.Print("Closing...") bot.StopReceivingUpdates() close(closeDebugTools) close(tgEvents) wg.Wait() if cleanShutdown.Load() { os.Exit(0) } os.Exit(1) }