Example #1
0
def start(update, context):
    """Welcome greating and proposing to choose the language"""
    lang = language(update)
    DB.add_user(update.effective_chat.id)
    if update.effective_chat.id in UM.currentUsers:
        del UM.currentUsers[update.effective_chat.id]
    if lang == 1 or lang == 0:
        markup = ReplyKeyboardMarkup([[c.text['to_main_menu'][lang]]],
                                     resize_keyboard=True,
                                     one_time_keyboard=True)
        update.message.reply_text(text=c.text['welcome_back'][lang],
                                  reply_markup=markup)
        return MAIN_MENU
    else:
        return LANG
Example #2
0
 def hash_pwd(self, username, pwd):
     hashed_pwd = hashpw(pwd, gensalt())
     pg = DB()
     pg.connect()
     pg.add_user(username, hashed_pwd)
     pg.close()
Example #3
0
class SocketHandler (WebSocketHandler):
	""" The WebSocket protocol is still in development. This module currently implements
	the hixie-76 and hybi-10 versions of the protocol. See this browser	compatibility
	table on Wikipedia: http://en.wikipedia.org/wiki/WebSockets#Browser_support """


	def open(self):
		print 'websocket opened!'
		# Create an instance of the recipe database to handle all requests
		self.db = DB()

	def on_message(self, message):
		# Convert things to be more friendly.  Check for good input.
		assert type(message) in [str, unicode]
		print 'got message: {0}'.format(message)
		message_dict = json.loads(message)
		assert type(message_dict) == dict
		# Check to ensure a command is received
		if "command" not in message_dict:
			raise KeyError("Key 'command' is missing. Way to suck...")
		command	= message_dict["command"]
		# Grab data and token if they are passed
		if "data" in message_dict:
			data = message_dict["data"]
		else:
			data = "" # Avoid an error later on if data is expected
		if "token" in message_dict:
			token = message_dict["token"]
			respond = True
		else:
			respond = False
		response = {} # Initialize as dictionary

		### Check the command received and proceed accordingly
		# user commands
		if command == "login":
			# Query the database for this user. If successful, assign username and return user info
			success = self.db.user_login(data)
			if success:
				print 'Successful login: '******'Loaded all data to user'
			else:
				print 'Unsuccessful login attempt from: ' + data['username']
				response["status"] = False

		elif command == "logout":
			# If logged in, log out
			if self.db.is_logged_in():
				self.db.logout()
				response["status"] = True
			else:
				response["status"] = False

		elif command == "user-query":
			# Check if username already exists
			exist = self.db.user_query(data)
			response["status"] = exist

		elif command == "add-user":
			# Add a new user
			success = self.db.add_user(data)
			if success:
				print 'Added user: '******'username']
			else:
				print 'Failed to add user: '******'username']
			# Send response to user query
			response["status"] = success
		elif command == "delete-user":
			#Delete a user's account
			success = self.db.delete_user(data)
			if success:
				print 'Deleted user: '******'username']
			else:
				print 'Failed to delete user: '******'username']
			# Send response to user query
			response["status"] = success

		# Node operations
		elif command == "add-node":
			success = self.db.add_node(data)
			if success:
				print 'added node'
			else:
				print 'failed to add node'

		elif command == "remove-node":
			success = self.db.remove_node(data)
			if success:
				print 'removed node'
			else:
				print 'failed to remove node'

		elif command == "add-edge":
			success = self.db.add_edge(data)
			if success:
				print 'added edge'
			else:
				print 'failed to add edge'

		elif command == "remove-edge":
			success = self.db.remove_edge(data)
			if success:
				print 'removed edge'
			else:
				print 'failed to remove edge'


		elif command == "update-node-info":
			success = self.db.update_node_info(data)
			if success:
				print 'updated node info'
			else:
				print 'failed to update node info'

		elif command == "update-data":
			# Request data is updated
			response["status"] = True
			response["data"] = self.db.load()
			print 'Sent full data back to user'

		# If a response is expected, send onem even if it's empty
		if respond:
			response["token"] = token
			self.send_message('response', response)

	def send_message(self, command, data):
		self.write_message({
			'command': command,
			'data': data,
		})

	def on_close(self):
		print 'websocket closed'