OmnomIRC.js 3.9 KB

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