diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-01-30 11:34:13 -0700 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-01-30 11:36:48 -0700 |
commit | 4f6f3dcd4b8c71f5caa52864092dbde22665a645 (patch) | |
tree | 19ae4d9203774173b83c25a91d7787d88bd3aa6a /finger/system.go | |
parent | 9cbc5cdd467ccd8e68f0f5d1d971ab76c2946624 (diff) |
finger protocol
Diffstat (limited to 'finger/system.go')
-rw-r--r-- | finger/system.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/finger/system.go b/finger/system.go new file mode 100644 index 0000000..7112967 --- /dev/null +++ b/finger/system.go @@ -0,0 +1,48 @@ +package finger + +import ( + "bytes" + "context" + "errors" + "os/exec" + + "tildegit.org/tjp/gus" +) + +// ListingDenied is returned to reject online user listing requests. +var ListingDenied = errors.New("Finger online user list denied.") + +// SystemFinger handles finger requests by invoking the finger(1) command-line utility. +func SystemFinger(allowListings bool) gus.Handler { + return func(ctx context.Context, request *gus.Request) *gus.Response { + fingerPath, err := exec.LookPath("finger") + if err != nil { + _ = request.Server.LogError( + "msg", "handler failure", + "ctx", "exec.LookPath(\"finger\")", + "err", err, + ) + return Error("Could not resolve request.") + } + + path := request.Path[1:] + + if len(path) == 0 && !allowListings { + return Error(ListingDenied.Error()) + } + + args := make([]string, 0, 1) + if len(path) > 0 { + args = append(args, path) + } + + cmd := exec.CommandContext(ctx, fingerPath, args...) + outbuf := &bytes.Buffer{} + cmd.Stdout = outbuf + + if err := cmd.Run(); err != nil { + return Error(err.Error()) + } + return Success(outbuf) + } +} |