<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//
package main

import (
	"errors"
	"log"
)

var servers serverList

func init() {
	servers = make(serverList)
}

type serverList map[string]*server

func (sl *serverList) exists(name string) (b bool) {
	_, b = (*sl)[name]
	return
}

func (c *client) connect(name string) {
	if servers.exists(name) == false {
		servers[name] = newServer(name)
		go servers[name].hub()
	}
	c.server = name
	servers[name].connect <- c
	servers[name].broadcast <- c.user.Name + " has connected."
	c.command = &chatCommands
}

func (c *client) disconnect() error {
	name := c.server
	if servers.exists(name) {
		(*servers[name]).broadcast <- c.user.Name + " has disconnected."
		(*servers[name]).disconnect <- c
		c.server = ""
		c.command = &sysCommands
		return nil
	}
	return errors.New("Not connected to a server.")
}

type server struct {
	connections map[string]*client
	connect     chan *client
	disconnect  chan *client
	broadcast   chan string
	name        string
}

func (s *server) empty() bool {
	if len(s.connections) == 0 {
		return true
	}
	return false
}

func (s *server) hub() {
	defer log.Println("Server closed")
	for {
		select {
		case c := <-s.connect:
			s.connections[c.user.Name] = c
		case c := <-s.disconnect:
			delete(s.connections, c.user.Name)
			if s.empty() {
				delete(servers, s.name)
				return
			}<commit_msg>Added command prefix updating and user connected checking.<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//
package main

import (
	"errors"
	"log"
	//"strings"
)

var servers serverList

func init() {
	servers = make(serverList)
}

type serverList map[string]*server

func (sl *serverList) exists(name string) (b bool) {
	_, b = (*sl)[name]
	return
}

func (c *client) connect(name string) {
	if servers.exists(name) == false {
		servers[name] = newServer(name)
		go servers[name].hub()
	}
	c.server = name
	servers[name].connect <- c
	servers[name].broadcast <- c.user.Name + " has connected."
	c.command = &chatCommands
	c.cmdPrefix = "/"
}

func (c *client) disconnect() error {
	name := c.server
	if servers.exists(name) && servers[name].isConnected(c.user) {
		(*servers[name]).broadcast <- c.user.Name + " has disconnected."
		(*servers[name]).disconnect <- c
		c.server = ""
		c.command = &sysCommands
		c.cmdPrefix = ""
		return nil
	}
	return errors.New("Not connected to a server.")
}

type server struct {
	connections map[string]*client
	connect     chan *client
	disconnect  chan *client
	broadcast   chan string
	name        string
}

func (s *server) empty() bool {
	if len(s.connections) == 0 {
		return true
	}
	return false
}

func (s *server) isConnected(u user) bool {
	if _, ok := s.connections[u.Name]; ok {
		return true
	}
	return false
}

func (s *server) hub() {
	defer log.Println("Server closed")
	for {
		select {
		case c := <-s.connect:
			s.connections[c.user.Name] = c
		case c := <-s.disconnect:
			delete(s.connections, c.user.Name)
			if s.empty() {
				delete(servers, s.name)
				return
			}
{'fraction_non_alphanumeric': 0.14221824686940965, 'fraction_numerical': 0.0029815146094215863, 'mean_word_length': 3.3402328589909445, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2162109', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1170, 'n_words': 345}