#!/usr/bin/env python3 
import cherrypy
import json

import commands

class zerver():
	@cherrypy.expose(['memebers.js'])
	@cherrypy.tools.json_out()
	def memebers(self, username, password, command, data):
		cherrypy.response.headers['Content-Type']= 'application/json'
		content = {
			'username':username,
			'password':password,
			'command':command,
			'data':json.loads(data)}
		if (commands.checkCreds(username,password)):
			try:
				if command == 'login':
					output = commands.login(content)
				if command == 'home':
					output = commands.home(content)
				if command == 'posts':
					output = commands.posts(content)
				if command == 'post':
					output = commands.post(content)
				if command == 'comments':
					output = commands.comments(content)
				if command == 'comment':
					output = commands.comment(content)
				if command == 'files':
					output = commands.files(content)
				if command == 'addFile':
					output = commands.addFile(content)
				if command == 'removeFile':
					output = commands.removeFile(content)
				if command == 'renameFile':
					output = commands.renameFile(content)
				if command == 'editFile':
					output = commands.editFile(content)
				if command == 'messagers':
					output = commands.messagers(content)
				if command == 'messager':
					output = commands.messager(content)
				if command == 'messages':
					output = commands.messages(content)
				if command == 'updateMessages':
					output = commands.updateMessages(content)
				if command == 'message':
					output = commands.message(content)
				if command == 'preferences':
					output = commands.preferences(content)
				if command == 'preference':
					output = commands.preference(content)
				if command == 'cat':
					output = commands.cat(content)

				cherrypy.response.headers["Access-Control-Allow-Origin"] = '*'
				return output
			except KeyError: pass
		raise cherrypy.HTTPError(403)

cherrypy.config.update({
	'server.socket_host': '0.0.0.0',
	'tools.encode.on': True,
	'tools.encode.encoding': 'utf-8'
})

if __name__ == '__main__':
	cherrypy.config.update({'environment' : 'production'})
	cherrypy.quickstart(zerver(),'/')

