Skip to content

Commit

Permalink
Extensive refactor and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jhalter committed Jul 10, 2024
1 parent 8fa1667 commit d937efb
Show file tree
Hide file tree
Showing 35 changed files with 2,752 additions and 1,858 deletions.
96 changes: 60 additions & 36 deletions cmd/mobius-hotline-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import (
"flag"
"fmt"
"github.com/jhalter/mobius/hotline"
"github.com/jhalter/mobius/internal/mobius"
"gopkg.in/natefinch/lumberjack.v2"
"io"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
"path"
"runtime"
"syscall"
)

//go:embed mobius/config
Expand All @@ -36,28 +39,16 @@ var (
)

func main() {
ctx, _ := context.WithCancel(context.Background())

// TODO: implement graceful shutdown by closing context
//c := make(chan os.Signal, 1)
//signal.Notify(c, os.Interrupt)
//defer func() {
// signal.Stop(c)
// cancel()
//}()
//go func() {
// select {
// case <-c:
// cancel()
// case <-ctx.Done():
// }
//}()
ctx, cancel := context.WithCancel(context.Background())

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT, os.Interrupt)

netInterface := flag.String("interface", "", "IP addr of interface to listen on. Defaults to all interfaces.")
basePort := flag.Int("bind", defaultPort, "Base Hotline server port. File transfer port is base port + 1.")
statsPort := flag.String("stats-port", "", "Enable stats HTTP endpoint on address and port")
configDir := flag.String("config", defaultConfigPath(), "Path to config root")
printVersion := flag.Bool("version", false, "print version and exit")
printVersion := flag.Bool("version", false, "Print version and exit")
logLevel := flag.String("log-level", "info", "Log level")
logFile := flag.String("log-file", "", "Path to log file")

Expand Down Expand Up @@ -105,12 +96,36 @@ func main() {
os.Exit(1)
}

srv, err := hotline.NewServer(*configDir, *netInterface, *basePort, slogger, &hotline.OSFileStore{})
config, err := mobius.LoadConfig(path.Join(*configDir, "config.yaml"))
if err != nil {
slogger.Error(fmt.Sprintf("Error loading config: %v", err))
os.Exit(1)
}

srv, err := hotline.NewServer(*config, *configDir, *netInterface, *basePort, slogger, &hotline.OSFileStore{})
if err != nil {
slogger.Error(fmt.Sprintf("Error starting server: %s", err))
os.Exit(1)
}

srv.MessageBoard, err = mobius.NewFlatNews(path.Join(*configDir, "MessageBoard.txt"))
if err != nil {
slogger.Error(fmt.Sprintf("Error loading message board: %v", err))
os.Exit(1)
}

srv.BanList, err = mobius.NewBanFile(path.Join(*configDir, "Banlist.yaml"))
if err != nil {
slogger.Error(fmt.Sprintf("Error loading ban list: %v", err))
os.Exit(1)
}

srv.ThreadedNewsMgr, err = mobius.NewThreadedNewsYAML(path.Join(*configDir, "ThreadedNews.yaml"))
if err != nil {
slogger.Error(fmt.Sprintf("Error loading news: %v", err))
os.Exit(1)
}

sh := statHandler{hlServer: srv}
if *statsPort != "" {
http.HandleFunc("/", sh.RenderStats)
Expand All @@ -124,6 +139,33 @@ func main() {
}(srv)
}

go func() {
for {
sig := <-sigChan
switch sig {
case syscall.SIGHUP:
slogger.Info("SIGHUP received. Reloading configuration.")

if err := srv.MessageBoard.(*mobius.FlatNews).Reload(); err != nil {
slogger.Error("Error reloading news", "err", err)
}

if err := srv.BanList.(*mobius.BanFile).Load(); err != nil {
slogger.Error("Error reloading ban list", "err", err)
}

if err := srv.ThreadedNewsMgr.(*mobius.ThreadedNewsYAML).Load(); err != nil {
slogger.Error("Error reloading threaded news list", "err", err)
}
default:
signal.Stop(sigChan)
cancel()
os.Exit(0)
}

}
}()

slogger.Info("Hotline server started",
"version", version,
"API port", fmt.Sprintf("%s:%v", *netInterface, *basePort),
Expand Down Expand Up @@ -168,24 +210,6 @@ func defaultConfigPath() string {
return cfgPath
}

// copyFile copies a file from src to dst. If dst does not exist, it is created.
func copyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()

destinationFile, err := os.Create(dst)
if err != nil {
return err
}
defer destinationFile.Close()

_, err = io.Copy(destinationFile, sourceFile)
return err
}

// copyDir recursively copies a directory tree, attempting to preserve permissions.
func copyDir(src, dst string) error {
entries, err := cfgTemplate.ReadDir(src)
Expand Down
76 changes: 38 additions & 38 deletions hotline/access.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
package hotline

const (
accessDeleteFile = 0 // File System Maintenance: Can Delete Files
accessUploadFile = 1 // File System Maintenance: Can Upload Files
accessDownloadFile = 2 // File System Maintenance: Can Download Files
accessRenameFile = 3 // File System Maintenance: Can Rename Files
accessMoveFile = 4 // File System Maintenance: Can Move Files
accessCreateFolder = 5 // File System Maintenance: Can Create Folders
accessDeleteFolder = 6 // File System Maintenance: Can Delete Folders
accessRenameFolder = 7 // File System Maintenance: Can Rename Folders
accessMoveFolder = 8 // File System Maintenance: Can Move Folders
accessReadChat = 9 // Chat: Can Read Chat
accessSendChat = 10 // Chat: Can Send Chat
accessOpenChat = 11 // Chat: Can Initial Private Chat
// accessCloseChat = 12 // Present in the Hotline 1.9 protocol documentation, but seemingly unused
// accessShowInList = 13 // Present in the Hotline 1.9 protocol documentation, but seemingly unused
accessCreateUser = 14 // User Maintenance: Can Create Accounts
accessDeleteUser = 15 // User Maintenance: Can Delete Accounts
accessOpenUser = 16 // User Maintenance: Can Read Accounts
accessModifyUser = 17 // User Maintenance: Can Modify Accounts
// accessChangeOwnPass = 18 // Present in the Hotline 1.9 protocol documentation, but seemingly unused
accessNewsReadArt = 20 // News: Can Read Articles
accessNewsPostArt = 21 // News: Can Post Articles
accessDisconUser = 22 // User Maintenance: Can Disconnect Users (Note: Turns username red in user list)
accessCannotBeDiscon = 23 // User Maintenance: Cannot be Disconnected
accessGetClientInfo = 24 // User Maintenance: Can Get User Info
accessUploadAnywhere = 25 // File System Maintenance: Can Upload Anywhere
accessAnyName = 26 // Miscellaneous: Can User Any Name
accessNoAgreement = 27 // Miscellaneous: Don't Show Agreement
accessSetFileComment = 28 // File System Maintenance: Can Comment Files
accessSetFolderComment = 29 // File System Maintenance: Can Comment Folders
accessViewDropBoxes = 30 // File System Maintenance: Can View Drop Boxes
accessMakeAlias = 31 // File System Maintenance: Can Make Aliases
accessBroadcast = 32 // Messaging: Can Broadcast
accessNewsDeleteArt = 33 // News: Can Delete Articles
accessNewsCreateCat = 34 // News: Can Create Categories
accessNewsDeleteCat = 35 // News: Can Delete Categories
accessNewsCreateFldr = 36 // News: Can Create News Bundles
accessNewsDeleteFldr = 37 // News: Can Delete News Bundles
accessSendPrivMsg = 40 // Messaging: Can Send Messages (Note: 1.9 protocol doc incorrectly says this is bit 19)
AccessDeleteFile = 0 // File System Maintenance: Can Delete Files
AccessUploadFile = 1 // File System Maintenance: Can Upload Files
AccessDownloadFile = 2 // File System Maintenance: Can Download Files
AccessRenameFile = 3 // File System Maintenance: Can Rename Files
AccessMoveFile = 4 // File System Maintenance: Can Move Files
AccessCreateFolder = 5 // File System Maintenance: Can Create Folders
AccessDeleteFolder = 6 // File System Maintenance: Can Delete Folders
AccessRenameFolder = 7 // File System Maintenance: Can Rename Folders
AccessMoveFolder = 8 // File System Maintenance: Can Move Folders
AccessReadChat = 9 // Chat: Can Read Chat
AccessSendChat = 10 // Chat: Can Send Chat
AccessOpenChat = 11 // Chat: Can Initial Private Chat
AccessCloseChat = 12 // Present in the Hotline 1.9 protocol documentation, but seemingly unused
AccessShowInList = 13 // Present in the Hotline 1.9 protocol documentation, but seemingly unused
AccessCreateUser = 14 // User Maintenance: Can Create Accounts
AccessDeleteUser = 15 // User Maintenance: Can Delete Accounts
AccessOpenUser = 16 // User Maintenance: Can Read Accounts
AccessModifyUser = 17 // User Maintenance: Can Modify Accounts
AccessChangeOwnPass = 18 // Present in the Hotline 1.9 protocol documentation, but seemingly unused
AccessNewsReadArt = 20 // News: Can Read Articles
AccessNewsPostArt = 21 // News: Can Post Articles
AccessDisconUser = 22 // User Maintenance: Can Disconnect Users (Note: Turns username red in user list)
AccessCannotBeDiscon = 23 // User Maintenance: Cannot be Disconnected
AccessGetClientInfo = 24 // User Maintenance: Can Get User Info
AccessUploadAnywhere = 25 // File System Maintenance: Can Upload Anywhere
AccessAnyName = 26 // Miscellaneous: Can User Any Name
AccessNoAgreement = 27 // Miscellaneous: Don't Show Agreement
AccessSetFileComment = 28 // File System Maintenance: Can Comment Files
AccessSetFolderComment = 29 // File System Maintenance: Can Comment Folders
AccessViewDropBoxes = 30 // File System Maintenance: Can View Drop Boxes
AccessMakeAlias = 31 // File System Maintenance: Can Make Aliases
AccessBroadcast = 32 // Messaging: Can Broadcast
AccessNewsDeleteArt = 33 // News: Can Delete Articles
AccessNewsCreateCat = 34 // News: Can Create Categories
AccessNewsDeleteCat = 35 // News: Can Delete Categories
AccessNewsCreateFldr = 36 // News: Can Create News Bundles
AccessNewsDeleteFldr = 37 // News: Can Delete News Bundles
AccessSendPrivMsg = 40 // Messaging: Can Send Messages (Note: 1.9 protocol doc incorrectly says this is bit 19)
)

type accessBitmap [8]byte
Expand Down
Loading

0 comments on commit d937efb

Please sign in to comment.