From 253825768bf7f4eb406eb825723285ee8ba89e30 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Sun, 21 Mar 2021 03:01:12 +0000 Subject: [PATCH] stuff --- go.mod | 2 ++ town.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 town.go diff --git a/go.mod b/go.mod index 8ab49e9..79a419a 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ +module git.tilde.town/tildetown/town + go 1.14 diff --git a/town.go b/town.go new file mode 100644 index 0000000..cf8f9ba --- /dev/null +++ b/town.go @@ -0,0 +1,41 @@ +package town + +import ( + "bytes" + "fmt" + "os/exec" + "os/user" +) + +const adminGroup = "admin" + +func IsAdmin(u os.User) (bool, error) { + adminGroup, err := user.LookupGroup(adminGroup) + if err != nil { + return false, fmt.Errorf("failed to get admin group info: %w", err) + } + + groupIds, err := u.GroupIds() + if err != nil { + return false, fmt.Errorf("failed to get groups info: %w", err) + } + + for _, groupId := range groupIds { + if groupId == adminGroup.Gid { + return true, nil + } + } + + return false, nil +} + +func SendLocalEmail(username, subject, body string) error { + cmd := exec.Command("/usr/sbin/sendmail", "-s", subject, username) + cmd.Stdin = bytes.NewBufferString(body) + + if err := cmd.Run(); err != nil { + return fmt.Errof("failed to send local email: %w", err) + } + + return nil +}