trunk v0.0.0
vilmibm 2021-03-21 03:01:12 +00:00
parent c4b901e805
commit 253825768b
2 changed files with 43 additions and 0 deletions

2
go.mod
View File

@ -1 +1,3 @@
module git.tilde.town/tildetown/town
go 1.14 go 1.14

41
town.go 100644
View File

@ -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
}