 		green: `'\033[0;32m'`
 		yellow: `'\033[0;33m'`
 		blue: `'\033[0;34m'`
-
 	constructor: (@name, @includeTimeStamp=yes) -> @levels = []
 	log: (message, prefix='', suffix='', type='log') ->
-		console.log "#{prefix}#{if @includeTimeStamp then ("[" + new Date().toTimeString().substr 0,8 + "]") else ''}#{@name} #{type} -> #{level+" -> " for level in @levels}#{message}#{suffix}"
+		console.log "#{prefix}#{if @includeTimeStamp then ("[" + new Date().toTimeString().substr(0,8) + "] ") else ''}#{@name} #{type} -> #{level+" -> " for level in @levels}#{message}#{suffix}"
 	error: (message) -> @log message, @Colors.red, @Colors.reset, 'ERROR'
 	warn: (message) -> @log message, @Colors.yellow, @Colors.reset, 'WARN'
 	info: (message) -> @log message, @Colors.blue, @Colors.reset, 'info'


User.fromJSON = (jsonExpression) -> new User(jsonExpression.id, jsonExpression.powerLevel, jsonExpression.auth)
Group.fromJSON = (jsonExpression) -> new Group(jsonExpression.id, getUserById userExp for userExp in jsonExpression.members, jsonExpression.passphrase)
Message.fromJSON = (je) -> new Message je.type, je.sourceId, je.destId, je.message, je.read

# Set up variables and functions that use the defined classes
userRegistry.push User.fromJSON json for json in getFileJSON 'users'
groupRegistry.push Group.fromJSON json for json in getFileJSON 'groups'
messageRegistry.push Message.fromJSON json for json in getFileJSON 'messages'

KcCoreLogger = new Logger "KentChat"
KcMessageLogger = new Logger "MessageLogger"

containsUser = (id) -> (getUserById id)?
containsGroup = (id) -> (getGroupById id)?
messagesFrom = (id) -> (msg for msg in messageRegistry when msg.sourceId is id)
messagesToUser = (id) -> (msg for msg in messageRegistry when msg.type is Message.DIRECT_MESSAGE and msg.destId is id).concat \
	(msg for msg in messageRegistry when msg.type is Message.GROUP_MESSAGE and id in (getGroupById msg.destId).members)
groupIDs = () -> (group.id for group in groupRegistry)
userIDs = () -> (user.id for group in groupRegistry)
connectionIsLoggedIn = (conn) -> if user.connID is conn.id for user in userRegistry then yes else no
getUserBySocketID = (socketID) ->
	return user for user in userRegistry when user.connID is socketID
User::unreadMessages = () -> (msg for msg in messagesToUser @id when not msg.read)
User::sentMessages = () -> messagesFrom @id
User::onRead = (message) -> (message.read = yes) if message.type is Message.DIRECT_MESSAGE and message.destId is @id and not message.read
User::sendMessage = (destId, message, type) ->
	msg = new Message type, @id, destId, message
	messageRegistry.push msg
	@send {type: "newMessage", id: msg.id}
Group::getMessages = () -> (msg for msg in messageRegistry when msg.type is Message.GROUP_MESSAGE and msg.destId is @id)

# Event handler common functions
fail = (reason) -> ['failure', reason]
succeed = (info) -> if info? then ['success', info] else ['success']
data = (raw) -> ['data', (if raw instanceof Array then raw.join ', ' else raw)]
# Event handler modules
eventHandlers = [
	{

	DIRECT_MESSAGE: 0
	GROUP_MESSAGE: 1

	constructor: (type, @sourceId, @destId, @message, @read = no, @id) ->
		if not (type in [@DIRECT_MESSAGE..@GROUP_MESSAGE])
			throw Error "Unknown message type: #{type}"
		@type = type
		@id ?= (String.fromCharCode(Math.floor(Math.random() * \
			(Options.phRand[1] - Options.phRand[0])) + Options.phRand[0]) for _ in [0..13]).join ''

	toJSON: () ->
