|
|
|
@ -6,12 +6,13 @@ module.exports = function(sql) { |
|
|
|
|
|
|
|
|
|
console.log("new client"); |
|
|
|
|
|
|
|
|
|
function emit(signal, data) { |
|
|
|
|
function emit(signal, data, info) { |
|
|
|
|
if (typeof data == 'string') { |
|
|
|
|
console.log("<- signal: "+signal+"("+data+")"); |
|
|
|
|
} else { |
|
|
|
|
console.log("<- signal: "+signal); |
|
|
|
|
} |
|
|
|
|
if (info) console.log(info); |
|
|
|
|
socket.emit(signal, data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -28,7 +29,12 @@ module.exports = function(sql) { |
|
|
|
|
if (err || !res || !res.length) return emit("fail", "unknown sender"); |
|
|
|
|
sql.query("insert into message set ?", {user: msg.user, msg: msg.content}, |
|
|
|
|
function(err, result) { |
|
|
|
|
if (err) return emit("fail", "cannot store message"); |
|
|
|
|
if (err) { |
|
|
|
|
if (err.code=='ER_NET_PACKET_TOO_LARGE') |
|
|
|
|
return emit('fail', "message too large", err); |
|
|
|
|
else |
|
|
|
|
return emit("fail", "cannot store message", err); |
|
|
|
|
} |
|
|
|
|
sql.query("select * from message, user"+ |
|
|
|
|
" where message.id = ? and"+ |
|
|
|
|
" message.user = user.name", |
|
|
|
@ -55,11 +61,11 @@ module.exports = function(sql) { |
|
|
|
|
if (user.name=="safechat") return emit("fail", "user name safechat is reserved"); |
|
|
|
|
sql.query("select name, pubkey from user where name = ?", [user.name], |
|
|
|
|
function(err, res, flds) { |
|
|
|
|
if (err) return emit('fail', "login failed (db access) - "+err); |
|
|
|
|
if (err) return emit('fail', "login failed (db access)", err); |
|
|
|
|
if (!res || res.length==0) { |
|
|
|
|
sql.query("insert into user (name, pubkey) values (?,?)", |
|
|
|
|
[user.name, user.pubkey], function(err, res, flds) { |
|
|
|
|
if (err) return emit('fail', "create user failed - "+err); |
|
|
|
|
if (err) return emit('fail', "create user failed", err); |
|
|
|
|
broadcast("user", { |
|
|
|
|
name: user.name, exists: false, pubkey: user.pubkey |
|
|
|
|
}); |
|
|
|
|