mirror of
				https://github.com/KevinMidboe/zoff.git
				synced 2025-10-29 18:00:23 +00:00 
			
		
		
		
	More persistent chat-names, and server handling of chat-names instead of client sending empty namechange request on join
This commit is contained in:
		| @@ -87,7 +87,8 @@ function chat(msg, guid, offline, socket) { | ||||
|         db.collection(coll + "_settings").find(function(err, docs){ | ||||
|             if(docs.length > 0 && (docs[0].userpass == undefined || docs[0].userpass == "" || (msg.hasOwnProperty('pass') && docs[0].userpass == crypto.createHash('sha256').update(Functions.decrypt_string(msg.pass)).digest("base64")))) { | ||||
|                 var data = msg.data; | ||||
|                 Functions.check_inlist(coll, guid, socket, offline); | ||||
|  | ||||
|                 Functions.check_inlist(coll, guid, socket, offline, function() { | ||||
|                     if(data !== "" && data !== undefined && data !== null && | ||||
|                     data.length < 151 && data.replace(/\s/g, '').length){ | ||||
|                         db.collection("user_names").find({"guid": guid}, function(err, docs) { | ||||
| @@ -105,6 +106,7 @@ function chat(msg, guid, offline, socket) { | ||||
|                             } | ||||
|                         }); | ||||
|                     } | ||||
|                 }); | ||||
|             } else { | ||||
|                 socket.emit('auth_required'); | ||||
|             } | ||||
| @@ -133,7 +135,7 @@ function all_chat(msg, guid, offline, socket) { | ||||
|     var data = msg.data; | ||||
|     coll = Functions.removeEmojis(coll).toLowerCase(); | ||||
|     //coll = filter.clean(coll); | ||||
|     Functions.check_inlist(coll, guid, socket, offline); | ||||
|     Functions.check_inlist(coll, guid, socket, offline, function() { | ||||
|         if(data !== "" && data !== undefined && data !== null && | ||||
|         data.length < 151 && data.replace(/\s/g, '').length){ | ||||
|             db.collection("user_names").find({"guid": guid}, function(err, docs) { | ||||
| @@ -151,9 +153,10 @@ function all_chat(msg, guid, offline, socket) { | ||||
|                 } | ||||
|             }); | ||||
|         } | ||||
|     }); | ||||
| } | ||||
|  | ||||
| function namechange(data, guid, socket, tried) { | ||||
| function namechange(data, guid, socket, tried, callback) { | ||||
|     /*if(!data.hasOwnProperty("channel") || | ||||
|      typeof(data.channel) != "string") return;*/ | ||||
|     var pw = ""; | ||||
| @@ -195,7 +198,8 @@ function namechange(data, guid, socket, tried) { | ||||
|                 } | ||||
|                 accepted_password = true; | ||||
|                 Functions.setSessionChatPass(Functions.getSession(socket), name.toLowerCase(), data.password, function() { | ||||
|                     db.collection("registered_users").update({"_id": name.toLowerCase()}, {$set: {password: Functions.hash_pass(password)}}, {upsert: true}, function() {}); | ||||
|                     db.collection("registered_users").update({"_id": name.toLowerCase()}, {$set: {password: Functions.hash_pass(password)}}, {upsert: true}, function() { | ||||
|                     }); | ||||
|                 }); | ||||
|             } else if(docs[0].password == Functions.hash_pass(password)) { | ||||
|                 if(docs[0].icon) { | ||||
| @@ -204,7 +208,9 @@ function namechange(data, guid, socket, tried) { | ||||
|                 accepted_password = true; | ||||
|                 if(new_password) { | ||||
|                     Functions.setSessionChatPass(Functions.getSession(socket), name.toLowerCase(), data.new_password, function() { | ||||
|                         db.collection("registered_users").update({"_id": name.toLowerCase(), password: Functions.hash_pass(password)}, {$set: {password: Functions.hash_pass(new_password)}}, function() {}); | ||||
|                         db.collection("registered_users").update({"_id": name.toLowerCase(), password: Functions.hash_pass(password)}, {$set: {password: Functions.hash_pass(new_password)}}, function() { | ||||
|  | ||||
|                         }); | ||||
|                     }); | ||||
|                 } else { | ||||
|                     Functions.setSessionChatPass(Functions.getSession(socket), name.toLowerCase(), data.password, function() { | ||||
| @@ -213,18 +219,23 @@ function namechange(data, guid, socket, tried) { | ||||
|             } | ||||
|             if(accepted_password) { | ||||
|                 db.collection("user_names").find({"guid": guid}, function(err, names) { | ||||
|                     if(names.length > 0) { | ||||
|                     if(names.length > 0 || (docs.length != 0 && docs[0].password == Functions.hash_pass(password))) { | ||||
|                         var no_name = false; | ||||
|                         if(names.length == 0) no_name = true; | ||||
|                         if(!no_name) { | ||||
|                             var old_name = names[0].name; | ||||
|                             db.collection("user_names").update({"_id": "all_names"}, {$pull: {names: old_name}}, function() {}); | ||||
|                         db.collection("user_names").update({"guid": guid}, {$set: {name: name, icon: icon}}, function(err, docs) { | ||||
|                         } | ||||
|                         db.collection("user_names").update({"guid": guid}, {$set: {name: name, icon: icon}}, {upsert: true}, function(err, docs) { | ||||
|                             db.collection("user_names").update({"_id": "all_names"}, {$addToSet: {names: name}}, function(err, docs) { | ||||
|                                 //socket.emit('name', {type: "name", accepted: true}); | ||||
|                                 if(old_name != name && !first) { | ||||
|                                 if(old_name != name && !first && !no_name) { | ||||
|                                     if(data.hasOwnProperty("channel") && typeof(data.channel) == "string") { | ||||
|                                         io.to(data.channel).emit('chat', {from: old_name, msg: " changed name to " + name}); | ||||
|                                         io.sockets.emit('chat.all', {from: old_name , msg: " changed name to " + name, channel: data.channel}); | ||||
|                                     } | ||||
|                                 } | ||||
|                                 if(callback != undefined && typeof(callback) == "function") callback(); | ||||
|                             }); | ||||
|                         }); | ||||
|                     } else { | ||||
|   | ||||
| @@ -14,6 +14,8 @@ var uniqid = require('uniqid'); | ||||
| var Filter = require('bad-words'); | ||||
| var filter = new Filter({ placeHolder: 'x'}); | ||||
|  | ||||
| var Chat = require(pathThumbnails + '/handlers/chat.js'); | ||||
|  | ||||
| function encodeChannelName(str) { | ||||
|     var _fn = encodeURIComponent; | ||||
|     str = filter.clean(str); | ||||
| @@ -107,7 +109,7 @@ function get_short_id(socket) { | ||||
|     socket.emit("id", new_short_id); | ||||
| } | ||||
|  | ||||
| function check_inlist(coll, guid, socket, offline) | ||||
| function check_inlist(coll, guid, socket, offline, callback) | ||||
| { | ||||
|  | ||||
|     if(coll == undefined) return; | ||||
| @@ -122,18 +124,23 @@ function check_inlist(coll, guid, socket, offline) | ||||
|                         } else { | ||||
|                             io.to(coll).emit("viewers", new_doc[0].users.length); | ||||
|                         } | ||||
|                         Chat.namechange({initial: true, first:true, channel: coll}, guid, socket, false, function() { | ||||
|                             db.collection("user_names").find({"guid": guid}, function(err, docs) { | ||||
|                                 if(docs.length == 1) { | ||||
|                                     socket.broadcast.to(coll).emit('chat', {from: docs[0].name, msg: " joined"}); | ||||
|                                 } | ||||
|                             }); | ||||
|                         db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs){}); | ||||
|                             db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs){ | ||||
|                                 if(callback != undefined && typeof(callback) == "function") callback(); | ||||
|                             }); | ||||
|                         }); | ||||
|                     }); | ||||
|                 }); | ||||
|             } else { | ||||
|                 db.collection("connected_users").find({"_id": coll}, function(err, new_doc) { | ||||
|                     io.to(coll).emit("viewers", new_doc[0].users.length); | ||||
|                 }); | ||||
|                 if(callback != undefined && typeof(callback) == "function") callback(); | ||||
|             } | ||||
|         }); | ||||
|  | ||||
| @@ -147,6 +154,7 @@ function check_inlist(coll, guid, socket, offline) | ||||
|         if(coll != undefined && coll != "") { | ||||
|             db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs) {}); | ||||
|         } | ||||
|         if(callback != undefined && typeof(callback) == "function") callback(); | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user