8 "github.com/jhalter/mobius/hotline"
9 "github.com/jhalter/mobius/internal/mobius"
10 "github.com/oleksandr/bonjour"
20 //go:embed mobius/config
21 var cfgTemplate embed.FS
23 // Values swapped in by go-releaser at build time
30 ctx, cancel := context.WithCancel(context.Background())
32 sigChan := make(chan os.Signal, 1)
33 signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT, os.Interrupt)
35 netInterface := flag.String("interface", "", "IP addr of interface to listen on. Defaults to all interfaces.")
36 basePort := flag.Int("bind", 5500, "Base Hotline server port. File transfer port is base port + 1.")
37 apiAddr := flag.String("api-addr", "", "Enable HTTP API endpoint on address and port")
38 configDir := flag.String("config", configSearchPaths(), "Path to config root")
39 printVersion := flag.Bool("version", false, "Print version and exit")
40 logLevel := flag.String("log-level", "info", "Log level")
41 logFile := flag.String("log-file", "", "Path to log file")
42 init := flag.Bool("init", false, "Populate the config dir with default configuration")
47 fmt.Printf("mobius-hotline-server version %s, commit %s\n", version, commit)
51 slogger := mobius.NewLogger(logLevel, logFile)
53 // It's important for Windows compatibility to use path.Join and not filepath.Join for the config dir initialization.
54 // https://github.com/golang/go/issues/44305
56 if _, err := os.Stat(path.Join(*configDir, "/config.yaml")); os.IsNotExist(err) {
57 if err := os.MkdirAll(*configDir, 0750); err != nil {
58 slogger.Error(fmt.Sprintf("error creating config dir: %s", err))
61 if err := copyDir(path.Join("mobius", "config"), *configDir); err != nil {
62 slogger.Error(fmt.Sprintf("error copying config dir: %s", err))
65 slogger.Info("Config dir initialized at " + *configDir)
67 slogger.Info("Existing config dir found. Skipping initialization.")
71 config, err := mobius.LoadConfig(path.Join(*configDir, "config.yaml"))
73 slogger.Error(fmt.Sprintf("Error loading config: %v", err))
77 srv, err := hotline.NewServer(
78 hotline.WithInterface(*netInterface),
79 hotline.WithLogger(slogger),
80 hotline.WithPort(*basePort),
81 hotline.WithConfig(*config),
84 slogger.Error(fmt.Sprintf("Error starting server: %s", err))
88 srv.MessageBoard, err = mobius.NewFlatNews(path.Join(*configDir, "MessageBoard.txt"))
90 slogger.Error(fmt.Sprintf("Error loading message board: %v", err))
94 srv.BanList, err = mobius.NewBanFile(path.Join(*configDir, "Banlist.yaml"))
96 slogger.Error(fmt.Sprintf("Error loading ban list: %v", err))
100 srv.ThreadedNewsMgr, err = mobius.NewThreadedNewsYAML(path.Join(*configDir, "ThreadedNews.yaml"))
102 slogger.Error(fmt.Sprintf("Error loading news: %v", err))
106 srv.AccountManager, err = mobius.NewYAMLAccountManager(filepath.Join(*configDir, "Users/"))
108 slogger.Error(fmt.Sprintf("Error loading accounts: %v", err))
112 srv.Agreement, err = mobius.NewAgreement(*configDir, "\r")
114 slogger.Error(fmt.Sprintf("Error loading agreement: %v", err))
118 bannerPath := filepath.Join(*configDir, config.BannerFile)
119 srv.Banner, err = os.ReadFile(bannerPath)
121 slogger.Error(fmt.Sprintf("Error loading accounts: %v", err))
125 reloadFunc := func() {
126 if err := srv.MessageBoard.(*mobius.FlatNews).Reload(); err != nil {
127 slogger.Error("Error reloading news", "err", err)
130 if err := srv.BanList.(*mobius.BanFile).Load(); err != nil {
131 slogger.Error("Error reloading ban list", "err", err)
134 if err := srv.ThreadedNewsMgr.(*mobius.ThreadedNewsYAML).Load(); err != nil {
135 slogger.Error("Error reloading threaded news list", "err", err)
138 if err := srv.Agreement.(*mobius.Agreement).Reload(); err != nil {
139 slogger.Error(fmt.Sprintf("Error reloading agreement: %v", err))
145 sh := mobius.NewAPIServer(srv, reloadFunc, slogger)
146 go sh.Serve(*apiAddr)
154 slogger.Info("SIGHUP received. Reloading configuration.")
166 slogger.Info("Hotline server started", "version", version, "config", *configDir)
168 // Assign functions to handle specific Hotline transaction types
169 mobius.RegisterHandlers(srv)
171 if srv.Config.EnableBonjour {
172 s, err := bonjour.Register(srv.Config.Name, "_hotline._tcp", "", *basePort, []string{"txtv=1", "app=hotline"}, nil)
174 slogger.Error("Error registering Hotline server with Bonjour", "err", err)
179 // Serve Hotline requests until program exit
180 log.Fatal(srv.ListenAndServe(ctx))
183 func configSearchPaths() string {
184 for _, cfgPath := range mobius.ConfigSearchOrder {
185 if _, err := os.Stat(cfgPath); err == nil {
193 // copyDir recursively copies a directory tree, attempting to preserve permissions.
194 func copyDir(src, dst string) error {
195 entries, err := cfgTemplate.ReadDir(src)
199 for _, dirEntry := range entries {
200 if dirEntry.IsDir() {
201 if err := os.MkdirAll(path.Join(dst, dirEntry.Name()), 0777); err != nil {
204 subdirEntries, _ := cfgTemplate.ReadDir(path.Join(src, dirEntry.Name()))
205 for _, subDirEntry := range subdirEntries {
206 f, err := os.Create(path.Join(dst, dirEntry.Name(), subDirEntry.Name()))
211 srcFile, err := cfgTemplate.Open(path.Join(src, dirEntry.Name(), subDirEntry.Name()))
213 return fmt.Errorf("error copying srcFile: %w", err)
215 _, err = io.Copy(f, srcFile)
222 f, err := os.Create(path.Join(dst, dirEntry.Name()))
227 srcFile, err := cfgTemplate.Open(path.Join(src, dirEntry.Name()))
231 _, err = io.Copy(f, srcFile)