def run(sc, parameters): parameters[0] = parameters[0].strip().lower() c = database.get_cursor() r = c.execute('SELECT * from users where username=?', [parameters[0]]) rows = r.fetchall() if len(rows) > 0: sc.send(MessageType.username_taken) return #用户ip获取,命名证书,获取信息更新证书,服务端证书也会用自己的个人信息更新 ip = str(parameters[3]) certname = ip + "_cert.pem" with open(certname, 'rb') as f: context = f.read() sp = context.split() f.close() with open(certname, 'wb') as f: f.write((str(parameters[0]) + ' ' + str(parameters[2]) + " " + str(sp[2])).encode()) f.close() c = database.get_cursor() c.execute( 'INSERT into users (username,password,email,ip,port,sex,age) values (?,?,?,?,?,?,?)', [ parameters[0], md5(parameters[1]), parameters[2], parameters[3], parameters[4], parameters[5], parameters[6] ]) sc.send(MessageType.register_successful, c.lastrowid)
def run(sc, parameters): user_id = sc_to_user_id[sc] uid = parameters[0] accepted = parameters[1] c = database.get_cursor() sql="SELECT 1 from friends where from_user_id=%d and to_user_id=%d and accepted=0"%(uid, user_id) c.execute(sql) rows = c.fetchall() if len(rows) == 0: return if not accepted: c = database.get_cursor() spl="delete from friends where from_user_id=%d and to_user_id=%d and accepted=0"%(uid,user_id) c.execute(sql) return if accepted: c = database.get_cursor() sql="update friends set accepted=1 where from_user_id=%d and to_user_id=%d and accepted=0"%(uid, user_id) c.execute(sql) c = database.get_cursor() sql="insert into friends (from_user_id,to_user_id,accepted) values (%d,%d,1)"%(user_id, uid) c.execute(sql) sc.send(MessageType.contact_info, add_target_type(database.get_user(uid), 0)) if uid in user_id_to_sc: user_id_to_sc[uid].send(MessageType.contact_info, add_target_type(database.get_user(user_id), 0))
def run(sc, parameters): parameters[0] = parameters[0].strip().lower() c = database.get_cursor() r = c.execute('SELECT * from users where username=?', [parameters[0]]) rows = r.fetchall() if len(rows) > 0: sc.send(MessageType.username_taken) return c = database.get_cursor() c.execute('INSERT into users (username,password,nickname) values (?,?,?)', [parameters[0], md5(parameters[1]), parameters[2]]) sc.send(MessageType.register_successful, c.lastrowid)
def run(sc, parameters): user_id = sc_to_user_id[sc] # parameters = username c = database.get_cursor() username = parameters.strip().lower() # print(user_id) r = c.execute('SELECT id from users where username=?', [username]).fetchall() #print(r[0][0]) #异常条件判断 if len(r) == 0: sc.send(MessageType.del_friend_result, [False, '用户名不存在']) return uid = r[0][0] if uid == user_id: sc.send(MessageType.del_friend_result, [False, '不能删除自己']) return c = database.get_cursor() r = c.execute( 'SELECT 1 from friends where from_user_id=? and to_user_id=? and accepted=1', [user_id, uid]).fetchall() #判断对方是否已经是自己的好友了 if len(r) == 0: sc.send(MessageType.del_friend_result, [False, '该用户还不是您的好友']) return #对方是你的好友 if len(r) != 0: #删除操作 c = database.get_cursor() c.execute( 'delete from friends where from_user_id=? and to_user_id=? and accepted=1', [uid, user_id]).fetchall() c.execute( 'delete from friends where from_user_id=? and to_user_id=? and accepted=1', [user_id, uid]).fetchall() sc.send(MessageType.del_friend_result, [True, '']) sc.send(MessageType.del_info, database.get_user(uid)) #如果要删除的好友在线,将他的好友列表中将我删除的操作码发送给服务器 if uid in user_id_to_sc: user_id_to_sc[uid].send(MessageType.del_info, database.get_user(user_id)) return
def run(sc, parameters): user_id = server.memory.sc_to_user_id[sc] c = database.get_cursor() c.execute("insert into rooms (room_name) values (?)", [parameters]) sc.send(MessageType.contact_info, add_target_type(database.get_room(c.lastrowid), 1)) database.add_to_room(user_id, c.lastrowid) sc.send(MessageType.general_msg, '创建成功,群号为:' + str(c.lastrowid))
def run(sc, parameters): user_id = sc_to_user_id[sc] # parameters = username c = database.get_cursor() username = parameters.strip().lower() sql = "SELECT id from users where username='******'" % username #r = c.execute('SELECT id from users where username=?', [username]).fetchall() c.execute(sql) r = c.fetchall() if len(r) == 0: sc.send(MessageType.add_friend_result, [False, '用户名不存在']) return uid = r[0][0] if uid == user_id: sc.send(MessageType.add_friend_rescult, [False, '不能加自己为好友']) return c = database.get_cursor() sql = "SELECT 1 from friends where from_user_id=%d and to_user_id=%d" % ( user_id, uid) c.execute(sql) r = c.fetchall() # r = c.execute('SELECT 1 from friends where from_user_id=? and to_user_id=?', [user_id, uid]).fetchall() if len(r) != 0: sc.send(MessageType.add_friend_result, [False, '已经是好友/已经发送过好友请求']) return c = database.get_cursor() sql = "insert into friends (from_user_id,to_user_id,accepted) values (%d,%d,0)" % ( user_id, uid) c.execute(sql) r = c.fetchall() #c.execute('insert into friends (from_user_id,to_user_id,accepted) values (?,?,0)', [user_id, uid]).fetchall() sc.send(MessageType.add_friend_result, [True, '']) if uid in user_id_to_sc: user_id_to_sc[uid].send(MessageType.incoming_friend_request, database.get_user(user_id))
def run(sc, parameters): parameters[0] = parameters[0].strip().lower() c = database.get_cursor() r = c.execute('SELECT * from users where username=%s', [parameters[0]]) rows = c.fetchall() if len(rows) > 0: sc.send(MessageType.username_taken) return c = database.get_cursor() sql = "INSERT into users (username,password,nickname) values ('%s','%s','%s')" % ( parameters[0], md5(parameters[1]), parameters[2]) ret = c.execute(sql) #database.conn.commit() #ret = c.execute('INSERT into users (username,password,nickname) values (%s,%s,%s)', # (parameters[0], md5(parameters[1]), parameters[2])) print('sql = ' + sql) print(ret) sc.send(MessageType.register_successful, c.lastrowid)
def run(sc, parameters): user_id = sc_to_user_id[sc] uid = parameters[0] accepted = parameters[1] c = database.get_cursor() r = c.execute( 'SELECT 1 from friends where from_user_id=? and to_user_id=? and accepted=0', [uid, user_id]) rows = r.fetchall() if len(rows) == 0: return if not accepted: c = database.get_cursor() c.execute( 'delete from friends where from_user_id=? and to_user_id=? and accepted=0', [uid, user_id]) return if accepted: c = database.get_cursor() c.execute( 'update friends set accepted=1 where from_user_id=? and to_user_id=? and accepted=0', [uid, user_id]) c = database.get_cursor() c.execute( 'insert into friends (from_user_id,to_user_id,accepted) values (?,?,1)', [user_id, uid]) sc.send(MessageType.contact_info, add_target_type(database.get_user(uid), 0)) if uid in user_id_to_sc: user_id_to_sc[uid].send( MessageType.contact_info, add_target_type(database.get_user(user_id), 0))
def run(sc, parameters): parameters[0] = parameters[0].strip().lower() c = database.get_cursor() sql = "SELECT id,username from users where username='******' and password='******'" % ( parameters[0], md5(parameters[1])) print(sql) #r = c.execute("SELECT id,username from users where username='******' and password='******'", (parameters[0], md5(parameters[1]))) c.execute(sql) rows = c.fetchall() if len(rows) == 0: sc.send(MessageType.login_failed) return user_id = rows[0][0] # 已经登入,踢下线 if user_id in user_id_to_sc: sc_old = user_id_to_sc[user_id] sc_old.send(MessageType.server_kick) sc_old.close() remove_sc_from_socket_mapping(sc_old) sc_to_user_id[sc] = user_id user_id_to_sc[user_id] = sc user = database.get_user(user_id) sc.send(MessageType.login_successful, user) login_bundle = {} # 发送群列表 rms = database.get_user_rooms(user_id) login_bundle['rooms'] = list(map(lambda x: add_target_type(x, 1), rms)) # for rm in rms: # sc.send(MessageType.contact_info, add_target_type(rm, 1)) # 发送好友请求 frs = database.get_pending_friend_request(user_id) for fr in frs: sc.send(MessageType.incoming_friend_request, fr) # 发送好友列表 frs = database.get_friends(user_id) login_bundle['friends'] = list(map(lambda x: add_target_type(x, 0), frs)) for fr in frs: # sc.send(MessageType.contact_info, add_target_type(fr, 0)) # 通知他的好友他上线了 if fr['id'] in user_id_to_sc: user_id_to_sc[fr['id']].send(MessageType.friend_on_off_line, [True, user_id]) # 通知群聊里的人他上线了 # [room_id, user_id, online] rooms_id = database.get_user_rooms_id(user_id) for room_id in rooms_id: users_id = database.get_room_members_id(room_id) for _user_id in users_id: if _user_id in user_id_to_sc and user_id != _user_id: user_id_to_sc[_user_id].send(MessageType.room_user_on_off_line, [room_id, user_id, True]) login_bundle['messages'] = database.get_chat_history(user_id) sc.send(MessageType.login_bundle, login_bundle)