OmnomIRC.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #!node
  2. process.chdir(__dirname);
  3. var fs = require('fs'),
  4. url = require('url'),
  5. path = require('path'),
  6. vm = require('vm'),
  7. toobusy = function(){return false;},//require('toobusy'),
  8. noop = function(){},
  9. cluster = require('cluster'),
  10. ircClient = require('node-irc'),
  11. logger = {
  12. log: function(msg){
  13. if(options.loglevel > 2){
  14. console.log(msg);
  15. }
  16. },
  17. debug: function(msg){
  18. if(options.loglevel > 2){
  19. console.log('DEBUG - '+msg);
  20. }
  21. },
  22. warn: function(msg){
  23. if(options.loglevel > 1){
  24. console.log('WARN - '+msg);
  25. }
  26. },
  27. info: function(msg){
  28. if(options.loglevel > 1){
  29. console.log('INFO - '+msg);
  30. }
  31. },
  32. error: function(msg){
  33. if(options.loglevel > 0){
  34. console.error(msg);
  35. }
  36. }
  37. },
  38. options = global.options = (function(){
  39. var defaults = {
  40. port: 80,
  41. loglevel: 3,
  42. threads: require('os').cpus().length,
  43. redis: {
  44. port: 6379,
  45. host: 'localhost'
  46. },
  47. debug: false,
  48. paths: {
  49. www: './www/',
  50. api: './api/',
  51. plugins: './plugins/'
  52. },
  53. irc: {
  54. host: 'irp.irc.omnimaga.org',
  55. port: 6667,
  56. nick: 'oirc3',
  57. name: 'OmnomIRC3',
  58. channels: [
  59. '#omnimaga'
  60. ],
  61. messages: {
  62. quit: 'Server closed'
  63. }
  64. },
  65. origins: [
  66. ['O','OmnomIRC'],
  67. ['#','IRC']
  68. ]
  69. },
  70. i,
  71. options;
  72. try{
  73. options = JSON.parse(fs.readFileSync('./options.json'));
  74. defaults = (function merge(options,defaults){
  75. for(var i in options){
  76. if(typeof defaults[i] != 'object' || defaults[i] instanceof Array){
  77. defaults[i] = options[i];
  78. }else{
  79. defaults[i] = merge(options[i],defaults[i]);
  80. }
  81. }
  82. return defaults
  83. })(options,defaults);
  84. }catch(e){
  85. console.warn('Using default settings. Please create options.json');
  86. console.error(e);
  87. }
  88. defaults.origins.unshift(['S','Server'],['?','Unknown']);
  89. options = {};
  90. for(i in defaults){
  91. Object.defineProperty(options,i,{
  92. value: defaults[i],
  93. enumerable: true,
  94. writable: false
  95. });
  96. }
  97. return options;
  98. })(),
  99. origin = function(name){
  100. for(var i in options.origins){
  101. if(options.origins[i][1] == name){
  102. return i;
  103. }
  104. }
  105. return 1;
  106. };
  107. if(typeof fs.existsSync == 'undefined') fs.existsSync = path.existsSync; // legacy support
  108. if(cluster.isMaster){
  109. var iWorker;
  110. cluster.on('exit', function(worker, code, signal) {
  111. console.log('worker ' + worker.process.pid + ' died');
  112. });
  113. iWorker = global.iw = cluster.fork();
  114. iWorker.on('online',function(){
  115. logger.info('First worker online');
  116. iWorker.send('S');
  117. });
  118. for(var i=1;i<options.threads;i++){
  119. cluster.fork().on('online',function(){
  120. logger.info('Child socket worker online');
  121. });
  122. }
  123. for(i in cluster.workers){
  124. var worker = cluster.workers[i];
  125. worker.on('message',function(msg){
  126. var c = msg[0];
  127. msg = msg.substr(1);
  128. logger.debug('Parent recieved command '+c+' with message '+msg);
  129. switch(c){
  130. case 'M':
  131. iWorker.send('M'+msg);
  132. break;
  133. }
  134. });
  135. }
  136. if(options.debug){
  137. require('repl').start({
  138. prompt: '> ',
  139. useGlobal: true
  140. }).on('exit',function(){
  141. for(var i in cluster.workers){
  142. cluster.workers[i].send('Q');
  143. }
  144. process.exit();
  145. });
  146. }
  147. }else{
  148. process.on('message',function(msg){
  149. var c = msg[0];
  150. msg = msg.substr(1);
  151. switch(c){
  152. case 'Q':
  153. if(typeof app != 'undefined' && typeof irc == 'undefined'){
  154. app.close();
  155. }else if(typeof irc != 'undefined'){
  156. irc.quit(options.irc.messages.quit);
  157. }
  158. break;
  159. case 'M':
  160. if(typeof irc != 'undefined'){
  161. msg = JSON.parse(msg);
  162. if(msg.message){
  163. irc.say(msg.room,'('+options.origins[msg.origin][0]+')'+'<'+msg.from+'> '+msg.message);
  164. }
  165. }
  166. break;
  167. case 'S':
  168. logger.info('Child starting irc');
  169. irc = new ircClient(options.irc.host,options.irc.port,options.irc.nick,options.irc.name);
  170. irc.on('ready',function(){
  171. logger.info('Connected to IRC');
  172. for(var i in options.irc.channels){
  173. irc.join(options.irc.channels[i]);
  174. //irc.client.send('WHO %s\n',options.irc.channels[i]);
  175. }
  176. });
  177. irc.on('CHANMSG',function(d){
  178. console.log(d);
  179. message(d.reciever,d.sender,d.message,origin('IRC'));
  180. });
  181. // Beginnings of names handler
  182. /*irc.on('names',function(chan,nicks){
  183. for(var i in nicks){
  184. logger.debug('[NICKS] Channel '+chan+' '+nicks[i]);
  185. }
  186. });*/
  187. irc.connect();
  188. logger.debug('Connecting to IRC');
  189. break;
  190. }
  191. });
  192. logger.info('Child starting socket.io');
  193. var RedisStore = require('socket.io/lib/stores/redis'),
  194. redis = require('socket.io/node_modules/redis'),
  195. pub = redis.createClient(options.redis.port,options.redis.host),
  196. sub = redis.createClient(options.redis.port,options.redis.host),
  197. client = redis.createClient(options.redis.port,options.redis.host),
  198. mimeTypes = {
  199. 'html': 'text/html',
  200. 'js': 'text/javascript',
  201. 'css': 'text/css',
  202. 'png': 'image/png',
  203. 'jpg': 'image/jpeg'
  204. },
  205. app = require('http').createServer(function(req,res){
  206. if(toobusy()){
  207. res.writeHead(503,{
  208. 'Content-type': 'text/plain'
  209. });
  210. res.write('503 Server busy.\n');
  211. res.end();
  212. return;
  213. }
  214. req.addListener('end',function(){
  215. logger.debug('served static content for '+req.url);
  216. var uri = url.parse(req.url).pathname,
  217. serveFile = function(filename,req,res){
  218. try{
  219. stats = fs.lstatSync(filename);
  220. }catch(e){
  221. res.writeHead(404,{
  222. 'Content-type': 'text/plain'
  223. });
  224. res.write('404 Not Found.\n');
  225. res.end();
  226. return;
  227. }
  228. if(stats.isFile()){
  229. var fileStream,
  230. mimetype = mimeTypes[path.extname(filename).split('.')[1]];
  231. res.writeHead(200,{
  232. 'Content-Type': mimetype
  233. });
  234. fileStream = fs.createReadStream(filename);
  235. fileStream.pipe(res);
  236. }else if(stats.isDirectory()){
  237. if(fs.existsSync(path.join(filename,'index.html'))){
  238. serveFile(path.join(filename,'index.html'),req,res);
  239. }else if(fs.existsSync(path.join(filename,'index.htm'))){
  240. serveFile(path.join(filename,'index.htm'),req,res);
  241. }else if(fs.existsSync(path.join(filename,'index.txt'))){
  242. serveFile(path.join(filename,'index.txt'),req,res);
  243. }else{
  244. res.writeHead(200,{
  245. 'Content-Type': 'text/plain'
  246. });
  247. res.write('Index of '+url+'\n');
  248. res.write('TODO, show index');
  249. res.end();
  250. }
  251. }else{
  252. res.writeHead(500,{
  253. 'Content-Type': 'text/plain'
  254. });
  255. res.write('500 Internal server error\n');
  256. res.end();
  257. }
  258. },
  259. filepath = unescape(uri);
  260. if(filepath.substr(0,5) == '/api/'){
  261. filepath = path.join(options.paths.api,filepath.substr(5));
  262. logger.debug('Attempting to run api script '+filepath);
  263. if(fs.existsSync(filepath)){
  264. fs.readFile(filepath,function(e,data){
  265. if(e){
  266. logger.error(e);
  267. res.end('null;');
  268. }else{
  269. var output = '',
  270. sandbox = {
  271. log: function(text){
  272. output += text;
  273. },
  274. error: function(msg){
  275. logger.error(msg);
  276. },
  277. info: function(msg){
  278. logger.info(msg);
  279. },
  280. debug: function(msg){
  281. logger.debug(msg);
  282. },
  283. head: {
  284. 'Content-Type': 'text/javascript'
  285. },
  286. returnCode: 200,
  287. vm: vm,
  288. fs: fs,
  289. options: options
  290. };
  291. vm.runInNewContext(data,sandbox,filepath);
  292. res.writeHead(sandbox.returnCode,sandbox.head);
  293. res.end(output);
  294. }
  295. });
  296. }else{
  297. res.writeHead(404,{
  298. 'Content-Type': 'text/javascript'
  299. });
  300. res.end('null;');
  301. }
  302. }else{
  303. serveFile(path.join(options.paths.www,filepath),req,res);
  304. }
  305. }).resume();
  306. }).listen(options.port),
  307. io = require('socket.io').listen(app);
  308. io.set('log level',options.loglevel);
  309. io.log = logger;
  310. if(typeof options.redis.password != 'undefined'){
  311. var eh = function(e){
  312. throw e;
  313. };
  314. pub.auth(options.redis.ppassword,eh);
  315. sub.auth(options.redis.ppassword,eh);
  316. client.auth(options.redis.ppassword,eh);
  317. }
  318. io.set('store', new RedisStore({
  319. redisPub : pub,
  320. redisSub : sub,
  321. redisClient : client
  322. }));
  323. io.sockets.on('connection',function(socket){
  324. socket.on('join',function(data){
  325. socket.join(data.name);
  326. data.title = data.name;
  327. socket.emit('join',{
  328. name: data.name
  329. });
  330. sendUserList(data.name);
  331. socket.get('nick',function(e,nick){
  332. logger.debug(nick+' joined '+data.name);
  333. fromServer(data.name,nick+' joined the channel');
  334. });
  335. });
  336. socket.on('part',function(data){
  337. socket.leave(data.name);
  338. socket.get('nick',function(e,nick){
  339. logger.debug(nick+' left '+data.name);
  340. sendUserList(data.name);
  341. });
  342. });
  343. socket.on('disconnect',function(data){
  344. var rooms = io.sockets.manager.rooms,
  345. i,
  346. room;
  347. for(i in rooms){
  348. if(rooms[i] != '' && typeof rooms[i] == 'string'){
  349. try{
  350. room = rooms[i].substr(1);
  351. }catch(e){}
  352. sendUserList(room);
  353. }
  354. }
  355. });
  356. socket.on('message',function(data){
  357. logger.debug('message sent to '+data.room);
  358. io.sockets.in(data.room).emit('message',data);
  359. process.send('M'+JSON.stringify(data));
  360. });
  361. socket.on('echo',function(data){
  362. logger.debug('echoing to '+data.room);
  363. socket.emit('message',data);
  364. });
  365. socket.on('names',function(data){
  366. var sockets = io.sockets.clients(data.name),
  367. i;
  368. runWithUserList(data.name,function(users){
  369. var temp = [],i;
  370. for(i in users) i && i != null && temp.push(users[i]);
  371. users = temp;
  372. fromServer(data.name,data.name+" users:\n\t\t"+users.join("\n\t\t"),socket);
  373. sendUserList(data.name);
  374. });
  375. });
  376. socket.on('auth',function(data){
  377. logger.info(data.nick+' registered');
  378. // TODO - authorize
  379. socket.set('nick',data.nick.substr(0,12));
  380. socket.emit('authorized',{
  381. nick: data.nick.substr(0,12)
  382. });
  383. });
  384. var runWithUserList = function(room,callback){
  385. var sockets = io.sockets.clients(room),
  386. i = 0,
  387. ret = [],
  388. getNext = function(){
  389. if(i < sockets.length){
  390. sockets[i].get('nick',function(e,nick){
  391. if(e){
  392. logger.error(e);
  393. }else if(!inArray(ret,nick)){
  394. logger.debug(room+' '+nick);
  395. ret.push(nick);
  396. }
  397. i++;
  398. getNext();
  399. });
  400. }else{
  401. callback(ret);
  402. }
  403. };
  404. getNext();
  405. },
  406. inArray = function(arr,val){
  407. for(var i in arr){
  408. if(arr[i] == val){
  409. return true;
  410. }
  411. }
  412. return false;
  413. },
  414. sendUserList = function(room){
  415. if(typeof room != 'undefined'){
  416. runWithUserList(room,function(users){
  417. io.sockets.in(room).emit('names',{
  418. room: room,
  419. names: users
  420. });
  421. });
  422. }
  423. },
  424. message = function(room,from,message,origin,socket){
  425. if(typeof socket == 'undefined'){
  426. socket = io.sockets.in(room);
  427. }
  428. socket.emit('message',{
  429. message: message,
  430. room: room,
  431. from: from,
  432. origin: origin
  433. })
  434. },
  435. fromServer = function(room,message,socket){
  436. if(typeof socket == 'undefined'){
  437. socket = io.sockets.in(room);
  438. }
  439. socket.emit('message',{
  440. message: message,
  441. room: room,
  442. from: 0,
  443. origin: 2
  444. });
  445. };
  446. });
  447. }
  448. process.on('uncaughtException',function(e){
  449. if(typeof logger != 'undefined'){
  450. logger.error(e);
  451. }else{
  452. console.error(e);
  453. }
  454. });