OmnomIRC.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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',2);
  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. socket.get('nick',function(e,nick){
  64. logger.debug(nick+' joined '+data.name);
  65. io.sockets.in(data.name).emit('message',{
  66. message: nick+' joined the channel',
  67. room: data.name,
  68. from: 0
  69. });
  70. });
  71. });
  72. socket.on('part',function(data){
  73. socket.leave(data.name);
  74. socket.get('nick',function(e,nick){
  75. logger.debug(nick+' left '+data.name);
  76. io.sockets.in(data.name).emit('message',{
  77. message: nick+' parted the channel',
  78. room: data.name,
  79. from: 0
  80. });
  81. });
  82. });
  83. socket.on('message',function(data){
  84. logger.debug('message sent to '+data.room);
  85. io.sockets.in(data.room).emit('message',data);
  86. });
  87. socket.on('names',function(data){
  88. var sockets = io.sockets.clients('room'),
  89. i;
  90. socket.emit('message',{
  91. message: data.name+' users:',
  92. room: data.name,
  93. from: 0
  94. });
  95. for(i in sockets){
  96. socket.get('nick',function(e,nick){
  97. socket.emit('message',{
  98. message: ' '+nick,
  99. room: data.name,
  100. from: 0
  101. });
  102. });
  103. }
  104. });
  105. socket.on('auth',function(data){
  106. logger.info(data.nick+' registered');
  107. // TODO - authorize
  108. socket.set('nick',data.nick);
  109. socket.emit('authorized');
  110. });
  111. });