OmnomIRC.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!node
  2. var fs = require('fs'),
  3. url = require('url'),
  4. path = require('path'),
  5. vm = require('vm'),
  6. mimeTypes = {
  7. 'html': 'text/html',
  8. 'js': 'text/javascript',
  9. 'css': 'text/css',
  10. 'png': 'image/png',
  11. 'jpg': 'image/jpeg'
  12. },
  13. app = require('http').createServer(function(req,res){
  14. req.addListener('end',function(){
  15. logger.debug('served static content for '+req.url);
  16. var uri = url.parse(req.url).pathname,
  17. serveFile = function(filename,req,res){
  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. if(fs.existsSync(path.join(filename,'index.html'))){
  38. serveFile(path.join(filename,'index.html'),req,res);
  39. }else if(fs.existsSync(path.join(filename,'index.htm'))){
  40. serveFile(path.join(filename,'index.htm'),req,res);
  41. }else if(fs.existsSync(path.join(filename,'index.txt'))){
  42. serveFile(path.join(filename,'index.txt'),req,res);
  43. }else{
  44. res.writeHead(200,{
  45. 'Content-Type': 'text/plain'
  46. });
  47. res.write('Index of '+url+'\n');
  48. res.write('TODO, show index');
  49. res.end();
  50. }
  51. }else{
  52. res.writeHead(500,{
  53. 'Content-Type': 'text/plain'
  54. });
  55. res.write('500 Internal server error\n');
  56. res.end();
  57. }
  58. },
  59. filepath = unescape(uri);
  60. if(filepath.substr(0,5) == '/api/'){
  61. filepath = path.join('./api/',filepath.substr(5));
  62. logger.debug('Attempting to run api script '+filepath);
  63. if(fs.existsSync(filepath)){
  64. fs.readFile(filepath,function(e,data){
  65. if(e){
  66. logger.error(e);
  67. res.end('null;');
  68. }else{
  69. var output = '',
  70. sandbox = {
  71. log: function(text){
  72. output += text;
  73. },
  74. error: function(msg){
  75. logger.error(msg);
  76. },
  77. info: function(msg){
  78. logger.info(msg);
  79. },
  80. debug: function(msg){
  81. logger.debug(msg);
  82. },
  83. head: {
  84. 'Content-Type': 'text/javascript'
  85. },
  86. returnCode: 200,
  87. vm: vm,
  88. fs: fs
  89. };
  90. vm.runInNewContext(data,sandbox,filepath);
  91. res.writeHead(sandbox.returnCode,sandbox.head);
  92. res.end(output);
  93. }
  94. });
  95. }else{
  96. res.writeHead(404,{
  97. 'Content-Type': 'text/javascript'
  98. });
  99. res.end('null;');
  100. }
  101. }else{
  102. serveFile(path.join('./www/',filepath),req,res);
  103. }
  104. }).resume();
  105. }).listen(80),
  106. io = require('socket.io').listen(app)
  107. logger = io.log;
  108. io.set('log level',3);
  109. io.sockets.on('connection',function(socket){
  110. socket.on('join',function(data){
  111. socket.join(data.name);
  112. data.title = data.name;
  113. socket.emit('join',{
  114. name: data.name,
  115. title: data.title
  116. });
  117. sendUserList(data.name);
  118. socket.get('nick',function(e,nick){
  119. logger.debug(nick+' joined '+data.name);
  120. io.sockets.in(data.name).emit('message',{
  121. message: nick+' joined the channel',
  122. room: data.name,
  123. from: 0
  124. });
  125. });
  126. });
  127. socket.on('part',function(data){
  128. socket.leave(data.name);
  129. socket.get('nick',function(e,nick){
  130. logger.debug(nick+' left '+data.name);
  131. sendUserList(data.name);
  132. });
  133. });
  134. socket.on('disconnect',function(data){
  135. var rooms = io.sockets.manager.rooms,
  136. i,
  137. room;
  138. for(i in rooms){
  139. if(rooms[i] != '' && typeof rooms[i] == 'string'){
  140. try{
  141. room = rooms[i].substr(1);
  142. }catch(e){}
  143. sendUserList(names);
  144. }
  145. }
  146. });
  147. socket.on('message',function(data){
  148. logger.debug('message sent to '+data.room);
  149. io.sockets.in(data.room).emit('message',data);
  150. });
  151. socket.on('echo',function(data){
  152. logger.debug('echoing to '+data.room);
  153. socket.emit('message',data);
  154. });
  155. socket.on('names',function(data){
  156. var sockets = io.sockets.clients(data.name),
  157. i;
  158. socket.emit('message',{
  159. message: data.name+' users:',
  160. room: data.name,
  161. from: 0
  162. });
  163. for(i in sockets){
  164. sockets[i].get('nick',function(e,nick){
  165. socket.emit('message',{
  166. message: ' '+nick,
  167. room: data.name,
  168. from: 0
  169. });
  170. });
  171. }
  172. sendUserList(data.name);
  173. });
  174. socket.on('auth',function(data){
  175. logger.info(data.nick+' registered');
  176. // TODO - authorize
  177. socket.set('nick',data.nick.substr(0,12));
  178. socket.emit('authorized',{
  179. nick: data.nick.substr(0,12)
  180. });
  181. });
  182. var usersInRoom = function(room){
  183. var sockets = io.sockets.clients(room),
  184. i,
  185. ret = [];
  186. for(i in sockets){
  187. sockets[i].get('nick',function(e,nick){
  188. ret.push(nick);
  189. });
  190. }
  191. return ret;
  192. },
  193. sendUserList = function(room){
  194. if(typeof room != 'undefined'){
  195. io.sockets.in(room).emit('names',{
  196. room: room,
  197. names: usersInRoom(room)
  198. });
  199. }
  200. };
  201. });
  202. process.on('uncaughtException',function(e){
  203. logger.error(e);
  204. });