OmnomIRC.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!node
  2. var fs = require('fs'),
  3. url = require('url'),
  4. path = require('path'),
  5. mimeTypes = {
  6. 'html': 'text/html',
  7. 'js': 'text/javascript',
  8. 'css': 'text/css',
  9. 'png': 'image/png',
  10. 'jpg': 'image/jpeg'
  11. },
  12. app = require('http').createServer(function(req,res){
  13. req.addListener('end',function(){
  14. logger.debug('served static content for '+req.url);
  15. var uri = url.parse(req.url).pathname,
  16. filename = path.join('./www/',unescape(uri)),
  17. stats;
  18. try{
  19. stats = fs.lstatSync(filename);
  20. }catch(e){
  21. res.writeHead(404,{
  22. 'Content-type': 'text/plain'
  23. });
  24. res.write('404 Not Found\n');
  25. res.end();
  26. return;
  27. }
  28. if(stats.isFile()){
  29. var fileStream,
  30. mimetype = mimeTypes[path.extname(filename).split('.')[1]];
  31. res.writeHead(200,{
  32. 'Content-Type': mimetype
  33. });
  34. fileStream = fs.createReadStream(filename);
  35. fileStream.pipe(res);
  36. }else if(stats.isDirectory()){
  37. res.writeHead(200,{
  38. 'Content-Type': 'text/plain'
  39. });
  40. res.write('Index of '+url+'\n');
  41. res.write('TODO, show index');
  42. res.end();
  43. }else{
  44. res.writeHead(500,{
  45. 'Content-Type': 'text/plain'
  46. });
  47. res.write('500 Internal server error\n');
  48. res.end();
  49. }
  50. }).resume();
  51. }).listen(80),
  52. io = require('socket.io').listen(app)
  53. logger = io.log;
  54. io.set('log level',3);
  55. io.sockets.on('connection',function(socket){
  56. socket.on('join',function(data){
  57. socket.join(data.name);
  58. data.title = data.name;
  59. socket.emit('join',{
  60. name: data.name,
  61. title: data.title
  62. });
  63. sendUserList(data.name);
  64. socket.get('nick',function(e,nick){
  65. logger.debug(nick+' joined '+data.name);
  66. io.sockets.in(data.name).emit('message',{
  67. message: nick+' joined the channel',
  68. room: data.name,
  69. from: 0
  70. });
  71. });
  72. });
  73. socket.on('part',function(data){
  74. socket.leave(data.name);
  75. socket.get('nick',function(e,nick){
  76. logger.debug(nick+' left '+data.name);
  77. sendUserList(data.name);
  78. });
  79. });
  80. socket.on('disconnect',function(data){
  81. var rooms = io.sockets.manager.rooms,
  82. i,
  83. room;
  84. for(i in rooms){
  85. if(rooms[i] != '' && typeof rooms[i] == 'string'){
  86. room = rooms[i].substr(1);
  87. sendUserList(names);
  88. }
  89. }
  90. });
  91. socket.on('message',function(data){
  92. logger.debug('message sent to '+data.room);
  93. io.sockets.in(data.room).emit('message',data);
  94. });
  95. socket.on('names',function(data){
  96. var sockets = io.sockets.clients(data.name),
  97. i;
  98. socket.emit('message',{
  99. message: data.name+' users:',
  100. room: data.name,
  101. from: 0
  102. });
  103. for(i in sockets){
  104. sockets[i].get('nick',function(e,nick){
  105. socket.emit('message',{
  106. message: ' '+nick,
  107. room: data.name,
  108. from: 0
  109. });
  110. });
  111. }
  112. sendUserList(data.name);
  113. });
  114. socket.on('auth',function(data){
  115. logger.info(data.nick+' registered');
  116. // TODO - authorize
  117. socket.set('nick',data.nick.substr(0,12));
  118. socket.emit('authorized',{
  119. nick: data.nick.substr(0,12)
  120. });
  121. });
  122. var usersInRoom = function(room){
  123. var sockets = io.sockets.clients(room),
  124. i,
  125. ret = [];
  126. for(i in sockets){
  127. sockets[i].get('nick',function(e,nick){
  128. ret.push(nick);
  129. });
  130. }
  131. return ret;
  132. },
  133. sendUserList = function(room){
  134. if(typeof room != 'undefined'){
  135. io.sockets.in(room).emit('names',{
  136. room: room,
  137. names: usersInRoom(room)
  138. });
  139. }
  140. };
  141. });