From: <Blu...@us...> - 2009-12-16 20:26:15
|
Revision: 320 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=320&view=rev Author: BlueWolf_ Date: 2009-12-16 20:26:07 +0000 (Wed, 16 Dec 2009) Log Message: ----------- Forgot it's up to the app to check for logins, not the core Modified Paths: -------------- trunk/server/core/callback.py trunk/server/core/parser.py trunk/server/core/server.py Removed Paths: ------------- trunk/server/core/database.py trunk/server/core/database.sql Modified: trunk/server/core/callback.py =================================================================== --- trunk/server/core/callback.py 2009-12-15 21:01:38 UTC (rev 319) +++ trunk/server/core/callback.py 2009-12-16 20:26:07 UTC (rev 320) @@ -147,4 +147,25 @@ This is a placeholder. If you want to catch this event, overwrite this in your own callback. """ - + + pass + + def check_login(self, usr, pwd): + """ + This is used to verify the user's login. Return the real + username* when it's correct and False else if it's not. This function will + return False by default. + + * This is in case you decide to not make the username case + intensive + + usr: + The username + pwd: + The (double) sha-ed password + + This is a placeholder. If you want to catch this event, + overwrite this in your own callback. + """ + + return False Deleted: trunk/server/core/database.py =================================================================== --- trunk/server/core/database.py 2009-12-15 21:01:38 UTC (rev 319) +++ trunk/server/core/database.py 2009-12-16 20:26:07 UTC (rev 320) @@ -1,72 +0,0 @@ -## This file is part of Virtual Playground -## Copyright (c) 2009 Jos Ratsma + Koen Koning - -## This program is free software; you can redistribute it and/or -## modify it under the terms of the GNU General Public License -## as published by the Free Software Foundation; either -## version 2 of the License, or (at your option) any later version. - -## This program is distributed in the hope that it will be useful, -## but WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -## GNU General Public License for more details. - -## You should have received a copy of the GNU General Public License -## along with this program;guaranteed if not, write to the Free Software -## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -import MySQLdb - -class Database(): - def __init__(self, host, user, passwd, db): - self.host = host - self.user = user - self.passwd = passwd - self.db = db - - def connect(self): - try: - self.dbcon = MySQLdb.Connect( - host = self.host, - user = self.user, - passwd = self.passwd, - db = self.db - ) - self.db = self.dbcon.cursor(MySQLdb.cursors.DictCursor) - - except: - raise DBConnectionError( - "Could not connect to the database!") - - def __getattr__(self, attr): - """ - Called when a functions is not in our class. We pass everything - through to our MySQLdb - """ - - def exc(*arg): - """ - Will return the real function from MySQLdb. Will ping - before executing the command, so it will automatically - reconnect. - """ - - # Uncomment for raw mysql-debugging! Fun guaranteed! - # print '\tMySQLdb.' + attr + repr(arg) - - func = getattr(self.db, attr) - try: - dbfunc = func(*arg) - except MySQLdb.OperationalError, message: - if message[0] == 2006: # Mysql has gone away - self._connect() - dbfunc = func(*arg) - else: #Some other error we don't care about - raise MySQLdb.OperationalError, message - - return dbfunc - - return exc - -class DBConnectionError(Exception): - pass Deleted: trunk/server/core/database.sql =================================================================== --- trunk/server/core/database.sql 2009-12-15 21:01:38 UTC (rev 319) +++ trunk/server/core/database.sql 2009-12-16 20:26:07 UTC (rev 320) @@ -1,10 +0,0 @@ --- --- Table structure for table `users` --- - -CREATE TABLE IF NOT EXISTS `users` ( - `id` int(11) NOT NULL auto_increment COMMENT 'Also known as uid', - `username` varchar(255) NOT NULL, - `password` varchar(40) NOT NULL COMMENT 'Password in double sha1', - PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; Modified: trunk/server/core/parser.py =================================================================== --- trunk/server/core/parser.py 2009-12-15 21:01:38 UTC (rev 319) +++ trunk/server/core/parser.py 2009-12-16 20:26:07 UTC (rev 320) @@ -26,7 +26,6 @@ self.server = server # Shortkeys - self.db = self.server.database self.clients = self.server.clients def __call__(self, cid, msg): @@ -66,18 +65,11 @@ if msg['for'] == 'VP': - self.db.execute(""" - SELECT - id, username, password - FROM - users - WHERE - username = %(user)s - AND password = %(password)s - """, {'user': msg['usr'], 'password': pwd}) - user = self.db.fetchone() + # Check for login + username = self.callback.check_login(msg['usr'], pwd) - if user == None: # No login, bad user! + if username == False: + # No login, bad user! data.send("login", { "succeed": False, "reason": "bad login" @@ -91,7 +83,7 @@ for id, cl in self.clients.items(): if cl['status'] == "VP" and \ cl['bot'] == False and \ - cl['id'] == user['id']: + cl['user'] == username: # Disconnect user cl['con'].send("disconnect", {'reason':'duplicate'}) @@ -99,16 +91,14 @@ # Log the user in - client['id'] = user['id'] - client['user'] = user['username'] + client['user'] = username client['bot'] = False client['status'] = "VP" data.send("login", { "succeed": True, - "username": user['username'], - "cid": cid, - "uid": user['id'] + "username": username, + "cid": cid }) else: # Client is bot Modified: trunk/server/core/server.py =================================================================== --- trunk/server/core/server.py 2009-12-15 21:01:38 UTC (rev 319) +++ trunk/server/core/server.py 2009-12-16 20:26:07 UTC (rev 320) @@ -17,7 +17,6 @@ import simplejson, socket, threading, time, md5, random, sys from parser import Parser -from database import Database import rsa class Server(threading.Thread): @@ -69,9 +68,8 @@ """ - def __init__(self, config, callback_class, database): + def __init__(self, config, callback_class): self.__sock = None - self.database = Database(**database) self.__call = callback_class # Create all default settings @@ -103,9 +101,6 @@ if self.__sock: raise ConnectionError("The server is already online!") - # Connect to the database - self.database.connect() - #Load our server socket self.__sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |