PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/socket.js

https://github.com/predominant/subway
JavaScript | 178 lines | 145 code | 24 blank | 9 comment | 24 complexity | b4316ef892912d658b3fe468439a397b MD5 | raw file
  1. var bcrypt = require('bcrypt'),
  2. mongoose = require('mongoose'),
  3. IRCLink = require('./irclink');
  4. // establish models
  5. var User = mongoose.model('User');
  6. var Connection = mongoose.model('Connection');
  7. var Message = mongoose.model('Message');
  8. module.exports = function(socket, connections) {
  9. var current_user;
  10. socket.on('getDatabaseState', function(){
  11. socket.emit('databaseState', {state: mongoose.connection.readyState});
  12. });
  13. socket.on('register', function(data) {
  14. bcrypt.genSalt(10, function(err, salt) {
  15. bcrypt.hash(data.password, salt, function(err, hash) {
  16. // Store hash in your password DB.
  17. var user = new User();
  18. user.username = data.username;
  19. user.password = hash;
  20. user.save();
  21. socket.emit('register_success', {username: user.username});
  22. current_user = user;
  23. });
  24. });
  25. });
  26. socket.on('login', function(data){
  27. User.findOne({username: data.username}, function(err, user) {
  28. if(user){
  29. bcrypt.compare(data.password, user.password, function(err, res) {
  30. if(res === true){
  31. var exists;
  32. current_user = user;
  33. if(connections[user.username] !== undefined){
  34. exists = true;
  35. } else {
  36. exists = false;
  37. }
  38. socket.emit('login_success', {username: user.username, exists: exists});
  39. } else {
  40. socket.emit('login_error', {message: 'Wrong password'});
  41. }
  42. });
  43. } else {
  44. }
  45. });
  46. });
  47. socket.on('connect', function(data) {
  48. var connection;
  49. if(current_user){
  50. connection = connections[current_user.username];
  51. }
  52. if(connection === undefined) {
  53. connection = new IRCLink(data.server, data.port, data.secure, data.selfSigned, data.nick, data.realName, data.password, data.rejoin, data.away);
  54. // save this connection
  55. if(current_user){
  56. // bind this socket to the proper IRC instance
  57. connection.associateUser(current_user.username);
  58. var conn = new Connection({ user: current_user.username,
  59. hostname: data.server,
  60. port: data.port || (data.secure ? 6697 : 6667),
  61. ssl: data.secure,
  62. rejoin: data.rejoin,
  63. away: data.away,
  64. realName: data.realName,
  65. selfSigned: data.selfSigned,
  66. channels: data.channels,
  67. nick: data.nick,
  68. password: data.password });
  69. conn.save();
  70. connections[current_user.username] = connection;
  71. }
  72. } else {
  73. socket.emit('restore_connection', {nick: connection.client.nick,
  74. server: connection.client.opt.server, channels: connection.client.chans});
  75. connection.clearUnreads();
  76. }
  77. // register this socket with our user's IRC connection
  78. connection.addSocket(socket);
  79. // Socket events sent FROM the front-end
  80. socket.on('join', function(name) {
  81. if (name[0] != '#')
  82. name = '#' + name;
  83. connection.client.join(name);
  84. });
  85. socket.on('part_pm', function(name){
  86. if(connection.clients.chans[name.toLowerCase()] !== undefined){
  87. delete connection.clients.chans[name.toLowerCase()];
  88. }
  89. });
  90. socket.on('part', function(name) {
  91. if (name[0] != '#')
  92. name = '#' + name;
  93. connection.client.part(name);
  94. if(current_user){
  95. // update the user's connection / channel list
  96. Connection.update({ user: current_user.username }, { $pull: { channels: name.toLowerCase() } }, function(err) {});
  97. }
  98. });
  99. socket.on('say', function(data) {
  100. connection.client.say(data.target, data.message);
  101. socket.emit('message', {to:data.target.toLowerCase(), from: connection.client.nick, text:data.message});
  102. if(current_user){
  103. connection.logMessage(data.target, connection.client.nick, data.message);
  104. }
  105. });
  106. socket.on('action', function(data) {
  107. connection.client.action(data.target, data.message);
  108. socket.emit('message', {
  109. to: data.target.toLowerCase(),
  110. from: connection.client.nick,
  111. text: '\u0001ACTION ' + data.message}
  112. );
  113. });
  114. socket.on('topic', function(data){
  115. connection.client.send('TOPIC ', data.name, data.topic);
  116. });
  117. socket.on('nick', function(data){
  118. connection.client.send('NICK', data.nick);
  119. connection.client.nick = data.nick;
  120. connection.client.opt.nick = client.nick;
  121. });
  122. socket.on('command', function(text) {
  123. connection.client.send(text);
  124. });
  125. socket.on('disconnect', function() {
  126. if(!current_user){
  127. // not logged in, drop this session
  128. connection.disconnect();
  129. } else {
  130. // keep the session alive, remove this socket, and clear unreads
  131. connection.removeSocket(socket);
  132. connection.clearUnreads();
  133. }
  134. });
  135. socket.on('getOldMessages', function(data){
  136. if (current_user) {
  137. var query = Message.find({channel: data.channelName.toLowerCase(), server: connection.server.toLowerCase(), linkedto: current_user.username});
  138. query.limit(data.amount);
  139. query.sort('date', -1);
  140. query.skip(data.skip);
  141. query.exec(function (err, results) {
  142. if(results){
  143. var returnData = {};
  144. if(results && results.length > 0){
  145. returnData['name'] = data.channelName.toLowerCase();
  146. returnData['messages'] = results;
  147. }
  148. socket.emit('oldMessages', returnData);
  149. }
  150. });
  151. }
  152. });
  153. });
  154. }