Split to Threads Complete
This commit is contained in:
		@@ -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" {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										25
									
								
								util/user.go
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								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
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user