use regex for online-check

This commit is contained in:
Arya Kiran 2023-01-18 23:28:56 +05:30
parent 7a3ed3d955
commit 367c91d843
Signed by untrusted user who does not match committer: arya
GPG Key ID: 842D12BDA50DF120

12
main.go
View File

@ -9,6 +9,7 @@ import (
"os/exec" "os/exec"
"runtime" "runtime"
"strings" "strings"
"regexp"
) )
type Userstruct struct { type Userstruct struct {
@ -56,12 +57,19 @@ func contains(strs []string, str string) bool {
func userdata(username, usersonline string) Userinfo { func userdata(username, usersonline string) Userinfo {
filename := "/home/" + username + "/meta-info.env" filename := "/home/" + username + "/meta-info.env"
_, error := os.Stat(filename) _, error := os.Stat(filename)
regex := "(^| )" + username + "($| )"
log.Info(usersonline)
isonline, err := regexp.MatchString(string(regex), string(usersonline))
log.Info(isonline)
if err != nil {
log.Error(err)
}
if error != nil { if error != nil {
if os.IsNotExist(error) { if os.IsNotExist(error) {
log.Error(username + " does not have a meta-info.env") log.Error(username + " does not have a meta-info.env")
var user Userinfo var user Userinfo
user.Name = username user.Name = username
if strings.Contains(usersonline, " "+username) == true { if isonline == true {
user.Online = true user.Online = true
} else { } else {
user.Online = false user.Online = false
@ -79,7 +87,7 @@ func userdata(username, usersonline string) Userinfo {
user.Desc = viper.GetString("DESCRIPTION") user.Desc = viper.GetString("DESCRIPTION")
user.Email = viper.GetString("EMAIL") user.Email = viper.GetString("EMAIL")
user.Loc = viper.GetString("LOCATION") user.Loc = viper.GetString("LOCATION")
if strings.Contains(usersonline, " "+username) == true { if isonline == true {
user.Online = true user.Online = true
} else { } else {
user.Online = false user.Online = false