summaryrefslogtreecommitdiff
path: root/finger/system.go
diff options
context:
space:
mode:
Diffstat (limited to 'finger/system.go')
-rw-r--r--finger/system.go48
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)
+ }
+}