Skip to content

Commit

Permalink
Merge pull request #33 from voldyman/master
Browse files Browse the repository at this point in the history
Embedded lock in to server struct
  • Loading branch information
shazow committed Dec 14, 2014
2 parents a960e1a + 79eb6b4 commit e3e46f9
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions server.go
Expand Up @@ -28,7 +28,6 @@ type Server struct {
sshConfig *ssh.ServerConfig
done chan struct{}
clients Clients
lock sync.Mutex
count int
history *History
motd string
Expand All @@ -37,6 +36,7 @@ type Server struct {
bannedPk map[string]*time.Time // fingerprint lookup
bannedIp map[net.Addr]*time.Time
started time.Time
sync.Mutex
}

func NewServer(privateKey []byte) (*Server, error) {
Expand Down Expand Up @@ -124,9 +124,9 @@ func (s *Server) Privmsg(nick, message string, sender *Client) error {
}

func (s *Server) SetMotd(client *Client, motd string) {
s.lock.Lock()
s.Lock()
s.motd = motd
s.lock.Unlock()
s.Unlock()
}

func (s *Server) MotdUnicast(client *Client) {
Expand All @@ -147,7 +147,7 @@ func (s *Server) Add(client *Client) {
client.SysMsg("Welcome to ssh-chat. Enter /help for more.")
}()

s.lock.Lock()
s.Lock()
s.count++

newName, err := s.proposeName(client.Name)
Expand All @@ -158,15 +158,15 @@ func (s *Server) Add(client *Client) {
client.Rename(newName)
s.clients[client.Name] = client
num := len(s.clients)
s.lock.Unlock()
s.Unlock()

s.Broadcast(ContinuousFormat(SYSTEM_MESSAGE_FORMAT, fmt.Sprintf(" * %s joined. (Total connected: %d)", client.ColoredName(), num)), client)
}

func (s *Server) Remove(client *Client) {
s.lock.Lock()
s.Lock()
delete(s.clients, client.Name)
s.lock.Unlock()
s.Unlock()

s.SysMsg("%s left.", client.ColoredName())
}
Expand All @@ -192,12 +192,12 @@ func (s *Server) proposeName(name string) (string, error) {
}

func (s *Server) Rename(client *Client, newName string) {
s.lock.Lock()
s.Lock()

newName, err := s.proposeName(newName)
if err != nil {
client.SysMsg("%s", err)
s.lock.Unlock()
s.Unlock()
return
}

Expand All @@ -206,7 +206,7 @@ func (s *Server) Rename(client *Client, newName string) {
oldName := client.Name
client.Rename(newName)
s.clients[client.Name] = client
s.lock.Unlock()
s.Unlock()

s.SysMsg("%s is now known as %s.", ColorString(client.Color, oldName), ColorString(client.Color, newName))
}
Expand All @@ -230,9 +230,9 @@ func (s *Server) Who(name string) *Client {

func (s *Server) Op(fingerprint string) {
logger.Infof("Adding admin: %s", fingerprint)
s.lock.Lock()
s.Lock()
s.admins[fingerprint] = struct{}{}
s.lock.Unlock()
s.Unlock()
}

func (s *Server) Whitelist(fingerprint string) {
Expand Down Expand Up @@ -279,19 +279,19 @@ func (s *Server) IsBanned(fingerprint string) bool {

func (s *Server) Ban(fingerprint string, duration *time.Duration) {
var until *time.Time
s.lock.Lock()
s.Lock()
if duration != nil {
when := time.Now().Add(*duration)
until = &when
}
s.bannedPk[fingerprint] = until
s.lock.Unlock()
s.Unlock()
}

func (s *Server) Unban(fingerprint string) {
s.lock.Lock()
s.Lock()
delete(s.bannedPk, fingerprint)
s.lock.Unlock()
s.Unlock()
}

func (s *Server) Start(laddr string) error {
Expand Down

0 comments on commit e3e46f9

Please sign in to comment.