diff --git a/mc_man.go b/mc_man.go index 7f47f03..5eb890f 100644 --- a/mc_man.go +++ b/mc_man.go @@ -73,15 +73,29 @@ func main() { mm := util.NewManager(stdin) util.LoadConfig(&mm) - // The forever loop to monitor the channel -loop: - for { - s, ok := <-ch - if !ok { - break loop + go func() { + for { + s, ok := <-ch + if !ok { + break + } + m := util.NewMessage(s) + fmt.Printf("\x1b[34;1m%s\x1b[0m", m.Output()) + mm.ProcessMessage(s) } - m := util.NewMessage(s) - fmt.Printf("\x1b[34;1m%s\x1b[0m", m.Output()) - mm.ProcessMessage(s) + }() + + // The forever loop to monitor everything + for { + time.Sleep(time.Second) + if util.StopServer { + break + } + // fmt.Printf("Monitoring... (%d users)\n", len(util.GetConfig().LoggedInUsers)) + // for i, u := range util.GetConfig().LoggedInUsers { + // if !u.HasQuota() { + // fmt.Printf(">> User %s is out of quota\n", u.Name) + // } + // } } } diff --git a/util/config.go b/util/config.go index 64ff241..bddc7e2 100644 --- a/util/config.go +++ b/util/config.go @@ -4,9 +4,6 @@ import ( "fmt" "github.com/antonholmquist/jason" "io/ioutil" - // "log" - // "os" - // "bytes" "strings" ) @@ -22,6 +19,7 @@ type Config struct { } var c *Config +var StopServer = false func LoadConfig(mm *MessageManager) { c = new(Config) @@ -35,6 +33,7 @@ func LoadConfig(mm *MessageManager) { AddListener(func(i *Message) bool { if i.User.IsOp && i.Text == "!stop\n" { mm.Output("stop") + StopServer = true return true } return false @@ -169,11 +168,32 @@ func LoadConfig(mm *MessageManager) { find = r[0] // find should be the user name now LoginUser(*FindUser(find, true)) + return true } } } return false }) + // Add logout listener + AddListener(func(i *Message) bool { + if i.User.Name == "" && strings.Contains(i.Text, " lost connection: ") { + // Find the user that just logged out + r := strings.Split(i.Text, "]: ") + find := "" + if len(r) > 0 { + find = r[1] + r := strings.Split(find, " lost connection: ") + if len(r) > 0 { + find = r[0] + // find should be the user name now + LogoutUser(*FindUser(find, false)) + return true + } + } + } + return false + }) + // Add !help listener AddListener(func(i *Message) bool { if i.User.Name != "" && i.Text == "!help\n" { diff --git a/util/user.go b/util/user.go index a0f1927..5544587 100644 --- a/util/user.go +++ b/util/user.go @@ -1,5 +1,9 @@ package util +import ( + "time" +) + type User struct { Name string Index int @@ -7,6 +11,9 @@ type User struct { Home string Porch string ToJSONString func() string + Quota time.Duration + quotaUsed time.Duration + loginTime time.Time } func NewUser(nm string) *User { @@ -19,7 +26,23 @@ func NewUser(nm string) *User { m.Home = "" m.Porch = "" m.ToJSONString = func() string { - return "{\"name\":\"" + m.Name + "\",\"home\":\"" + m.Home + "\",\"porch\":\"" + m.Porch + "\"}" + return "{\"name\":\"" + m.Name + "\",\"home\":\"" + m.Home + "\",\"porch\":\"" + m.Porch + "\",\"quota\":\"" + string(m.Quota) + "\",\"quota_used\":\"" + string(m.quotaUsed) + "\"}" } return m } + +func (u *User) HasQuota() bool { + if u.Quota > 0 { + return u.quotaUsed < u.Quota + } else { + return true + } +} + +func (u *User) RemainingQuota() time.Duration { + if u.Quota > 0 { + return u.Quota - u.quotaUsed + } else { + return 0 + } +}