From 4f6f3dcd4b8c71f5caa52864092dbde22665a645 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Mon, 30 Jan 2023 11:34:13 -0700 Subject: finger protocol --- finger/system.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 finger/system.go (limited to 'finger/system.go') 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) + } +} -- cgit v1.2.3