irc.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. var net = require('net');
  2. var IRCserver = require('./server');
  3. var IRCuser = require('./user');
  4. var IRCchannel = require('./channel');
  5. var uplink = null;
  6. var connection = null;
  7. var recvData = '';
  8. var users = [];
  9. var servers = [];
  10. var channels = [];
  11. var me = null;
  12. var events = {};
  13. function addInternalEvents(){
  14. events.netSynced.unshift(function(){
  15. for(var i=0; i<users.length; i++){
  16. var user = users[i];
  17. if(user.uplink == me){
  18. uplink.introduceUser(user);
  19. }
  20. }
  21. for(var i=0; i<channels.length; i++){
  22. uplink.syncChannel(channels[i]);
  23. }
  24. });
  25. }
  26. function findServer(s){
  27. for(var i=0; i<servers.length; i++){
  28. srv = servers[i];
  29. if(srv.sid == s || srv.name == s) return srv;
  30. }
  31. return null;
  32. }
  33. function findUser(u){
  34. for(var i=0; i<users.length; i++){
  35. usr = users[i];
  36. if(usr.uid == u || usr.name == u) return usr;
  37. }
  38. return null;
  39. }
  40. function findChannel(c){
  41. for(var i=0; i<channels.length; i++){
  42. usr = channels[i];
  43. if(channels[i].name == c) return channels[i];
  44. }
  45. return null;
  46. }
  47. function removeServer(server){
  48. var length = servers.length;
  49. var done = false;
  50. do {
  51. for(var i=0; i<length; i++){
  52. if(servers.length != length) break;
  53. if(servers[i].uplink == server){
  54. removeServer(servers[i]); // remove connected servers recursively
  55. }
  56. }
  57. if(i == length) done = true;
  58. } while(!done);
  59. for(var i=users.length-1; i>=0; i--){
  60. if(users[i].uplink == server){
  61. quitUser(users[i]); // remove users connected to this server
  62. }
  63. }
  64. for(var i=servers.length-1; i>=0; i--){ // finally, remove the given server
  65. if(servers[i] == server){
  66. console.log('Removing server '+servers[i].name);
  67. events.doEvent('serverDelete', server);
  68. servers.splice(i, 1);
  69. delete server;
  70. break;
  71. }
  72. }
  73. }
  74. function getChannel(name){
  75. var lowerName = name.toLowerCase();
  76. var channel = null;
  77. for(var i=0; i<channels.length; i++){
  78. if(channels[i].name == lowerName){
  79. channel = channels[i];
  80. break;
  81. }
  82. }
  83. if(!channel){
  84. console.log('Creating channel '+name);
  85. channel = new IRCchannel;
  86. channel.setEvents(events);
  87. channel.name = name;
  88. channels.push(channel);
  89. events.doEvent('channelCreate', channel);
  90. }
  91. return channel;
  92. }
  93. function killUser(user){
  94. console.log('KILL');
  95. console.log(user);
  96. // TODO
  97. }
  98. function newUser(nick, distance, TS, ident, host, uid, account, umodes, vhost, cloakedHost, ip, realname, uplink){ // nick, distance, TS, ident, host, uid, account, umodes, vhost, cloakedHost, ip, realname, uplink
  99. var user;
  100. if((user = findUser(uid)) || (user = findUser(nick))){
  101. console.log(user);
  102. throw 'User already exists';
  103. }
  104. console.log('Introducing user '+nick);
  105. user = new IRCuser;
  106. user.setEvents(events);
  107. user.introduce(nick, distance, TS, ident, host, uid, account, umodes, vhost, cloakedHost, ip, realname, uplink);
  108. users.push(user);
  109. events.doEvent('userCreate', user);
  110. return user;
  111. }
  112. function newServer(name, sid, desc, distance, uplink){
  113. if(findServer(sid) || findServer(name)){
  114. throw 'Server already exists';
  115. }
  116. console.log('Introducing server '+name+' (SID='+sid+', uplink="'+uplink.name+'")');
  117. server = new IRCserver;
  118. server.setEvents(events);
  119. server.introduce(name, sid, desc, distance, uplink);
  120. servers.push(server);
  121. events.doEvent('serverCreate', server);
  122. return server;
  123. }
  124. function ircSendData(tags, from, cmd, args){
  125. var data = makeTagsString(tags);
  126. if(from){
  127. data += ':' + from + ' ';
  128. }
  129. data += cmd;
  130. if(args) for(var i=0; i<args.length; i++){
  131. var arg = args[i];
  132. if(arg.indexOf(' ') >= 0){ // valid for last arg only
  133. arg = ':' + arg;
  134. }
  135. data += ' ' + arg;
  136. }
  137. console.log('<<< '+data);
  138. data += "\r\n";
  139. connection.write(data);
  140. }
  141. function ircDataReceived(data){
  142. data = data.toString('utf8');
  143. recvData += data;
  144. var buffer = '';
  145. for(var i=0; i<recvData.length; i++){
  146. var c = recvData.charAt(i);
  147. switch(c){
  148. default: buffer += c; break;
  149. case '\r': case '\n':
  150. if(buffer.length > 0)
  151. ircMessage(buffer);
  152. buffer = '';
  153. break;
  154. }
  155. }
  156. recvData = buffer;
  157. }
  158. function makeTagsString(tags){
  159. return '';
  160. //TODO
  161. }
  162. function ircMessage(data){
  163. var msg = irc.parseLine(data);
  164. uplink.processMessage(msg);
  165. }
  166. function ircConnectionClosed(e){
  167. events.doEvent('uplinkLost', me);
  168. console.log('Closed: '+e);
  169. process.exit(1);
  170. }
  171. function processReplyTags(inputTags, newTags){
  172. return newTags;
  173. // TODO
  174. }
  175. function quitUser(user){
  176. console.log('Removing user '+user.name);
  177. for(var i=0; i<channels.length; i++){
  178. channels[i].removeUser(user);
  179. }
  180. for(var i=0; i<users.length; i++){
  181. if(users[i] == user){
  182. users.splice(i, 1);
  183. events.doEvent('userDelete', user);
  184. delete user;
  185. break;
  186. }
  187. }
  188. }
  189. function removeEmptyChannels(){
  190. for(var i=channels.length - 1; i >= 0; i--){
  191. if(channels[i].users.length == 0){
  192. storeChannelData(channels[i]);
  193. console.log('Removing channel '+channels[i].name);
  194. var channel = channels[i];
  195. channels.splice(i, 1);
  196. delete channel;
  197. }
  198. }
  199. }
  200. function storeChannelData(channel){
  201. // TODO maybe move a level up
  202. }
  203. setInterval(removeEmptyChannels, 1000);
  204. var irc = {
  205. 'users': users,
  206. 'channels': channels,
  207. 'servers': servers,
  208. 'getChannel': getChannel,
  209. 'setConnection': function(host, port, protocol){
  210. uplink = require('./protocol/' + protocol);
  211. me = new IRCserver;
  212. me.setEvents(events);
  213. me.introduce('serwisy.pirc.pl', '11K', 'Serwisy', 0, null);
  214. servers.push(me);
  215. uplink.setHandlers({
  216. send: ircSendData,
  217. findServer: findServer,
  218. findUser: findUser,
  219. killUser: killUser,
  220. newServer: newServer,
  221. newUser: newUser,
  222. getChannel: getChannel,
  223. quitUser: quitUser,
  224. removeServer: removeServer,
  225. findChannel: findChannel
  226. });
  227. uplink.setSettings({
  228. ID: me.sid,
  229. password: 'myservicespassword',
  230. name: me.name,
  231. description: me.description,
  232. version: 'k4be-services',
  233. me: me,
  234. maxUsers: 0
  235. });
  236. addInternalEvents();
  237. uplink.setEvents(events);
  238. connection = new net.Socket();
  239. connection.connect(port, host, uplink.connected.bind(this));
  240. connection.on('data', ircDataReceived);
  241. connection.on('close', ircConnectionClosed);
  242. },
  243. 'setEvents': function(newEvents){
  244. events = newEvents;
  245. },
  246. 'makeUser': function(nick, ident, host, realname){
  247. var user = new IRCuser;
  248. user.setEvents(events);
  249. var uid = uplink.makeUid();
  250. user.introduce(nick, 0, Math.floor(new Date() / 1000).toString(10), ident, host, uid, null, '+SoB'/*TODO*/, null, null, null, realname, me);
  251. users.push(user);
  252. return user;
  253. },
  254. 'messagedata': function() {
  255. this.text = '';
  256. this.args = [];
  257. this.tags = [];
  258. this.command = '';
  259. this.sender = {
  260. 'nick': '',
  261. 'ident': '',
  262. 'host': '',
  263. 'server': false,
  264. 'user': false
  265. };
  266. this.time = new Date();
  267. this.reply = function(tags, cmd, args){
  268. var outTags = processReplyTags(this.tags, tags);
  269. ircSendData(outTags, this.sender.nick, cmd, args);
  270. }
  271. this.originalString = '';
  272. },
  273. 'parseTags': function(tagsLine){
  274. var tags = [];
  275. var tagState = 'keyName';
  276. var keyValue;
  277. var keyName = '';
  278. for(var i = 0; i < tagsLine.length; i++){
  279. var cchar = tagsLine.charAt(i);
  280. switch(tagState){
  281. case 'keyName':
  282. switch(cchar){
  283. case '=':
  284. tagState = 'keyValue';
  285. keyValue = '';
  286. break;
  287. case ';':
  288. tags[keyName] = '';
  289. keyName = ''; // staying in tagStateKeyName
  290. break;
  291. default: keyName += cchar; break;
  292. }
  293. break;
  294. case 'keyValue':
  295. switch(cchar){
  296. case '\\': tagState = 'keyValueEscape'; break;
  297. case ';':
  298. tags[keyName] = keyValue;
  299. keyName = '';
  300. tagState = 'keyName';
  301. break;
  302. default: keyValue += cchar; break;
  303. }
  304. break;
  305. case 'keyValueEscape':
  306. switch(cchar){
  307. case ':': keyValue += ';'; break;
  308. case 's': keyValue += ' '; break;
  309. case 'r': keyValue += '\r'; break;
  310. case 'n': keyValue += '\n'; break;
  311. default: keyValue += cchar; break;
  312. }
  313. tagState = 'keyValue';
  314. break;
  315. }
  316. }
  317. if(keyName.length > 0) tags[keyName] = keyValue; // flush last tag
  318. return tags;
  319. },
  320. 'parseLine': function(line){
  321. var ircmsg = new irc.messagedata();
  322. var line = line.trim();
  323. line.replace(/^\s+|\s+$/gm,'');
  324. if(line == ''){
  325. return;
  326. }
  327. ircmsg.originalString = line;
  328. var msglen = line.length;
  329. var pstate = 'start';
  330. var currArg = '';
  331. var tags = '';
  332. var haveText = false;
  333. console.log('>>> ' + line);
  334. for(var i = 0; i < msglen; i++){
  335. var cchar = line.charAt(i);
  336. switch(pstate){
  337. case 'start':
  338. switch(cchar){
  339. case '@': pstate = 'tags'; break;
  340. case ':': pstate = 'senderNick'; break;
  341. default:
  342. pstate = 'command';
  343. ircmsg.command += cchar;
  344. break;
  345. }
  346. break;
  347. case 'tags':
  348. switch(cchar){
  349. case ' ':
  350. pstate = 'start';
  351. ircmsg.tags = irc.parseTags(tags);
  352. break;
  353. default: tags += cchar; break;
  354. }
  355. break;
  356. case 'senderNick':
  357. switch(cchar){
  358. case '!': pstate = 'senderUser'; break;
  359. case '@': pstate = 'senderHost'; break;
  360. case ' ': pstate = 'command'; break;
  361. default: ircmsg.sender.nick += cchar; break;
  362. }
  363. break;
  364. case 'senderUser':
  365. switch(cchar){
  366. case '@': pstate = 'senderHost'; break;
  367. case ' ': pstate = 'command'; break;
  368. default: ircmsg.sender.ident += cchar; break;
  369. }
  370. break;
  371. case 'senderHost':
  372. switch(cchar){
  373. case ' ': pstate = 'command'; break;
  374. default: ircmsg.sender.host += cchar; break;
  375. }
  376. break;
  377. case 'command':
  378. switch(cchar){
  379. case ' ': pstate = 'args'; break;
  380. default: ircmsg.command += cchar; break;
  381. }
  382. break;
  383. case 'args':
  384. switch(cchar){
  385. case ' ':
  386. if(currArg != ''){
  387. ircmsg.args.push(currArg);
  388. }
  389. currArg = '';
  390. break;
  391. case ':':
  392. if(prevChar == ' '){
  393. pstate = 'message';
  394. haveText = true;
  395. } else {
  396. currArg += cchar;
  397. }
  398. break;
  399. default: currArg += cchar; break;
  400. }
  401. break;
  402. case 'message':
  403. ircmsg.text += cchar;
  404. break;
  405. }
  406. var prevChar = cchar;
  407. }
  408. if(pstate == 'args'){
  409. ircmsg.args.push(currArg);
  410. }
  411. ircmsg.sender = uplink.processSender(ircmsg.sender);
  412. if(!ircmsg.sender){
  413. return false;
  414. }
  415. if(!ircmsg.sender.server && !ircmsg.sender.user){
  416. if(ircmsg.sender.ident == '' && ircmsg.sender.host == '' && ircmsg.sender.nick.indexOf('.')!=-1){
  417. ircmsg.sender.server = true;
  418. } else {
  419. ircmsg.sender.user = true;
  420. }
  421. }
  422. if(!haveText){
  423. ircmsg.text = ircmsg.args[ircmsg.args.length-1]; // handling last argument as text if : is missing
  424. } else {
  425. ircmsg.args.push(ircmsg.text); // handling text as a last argument as required by the protocol
  426. }
  427. // add u@h
  428. /* if(ircmsg.sender.user){
  429. if(ircmsg.sender.ident) users.getUser(ircmsg.sender.nick).setIdent(ircmsg.sender.ident);
  430. if(ircmsg.sender.host) users.getUser(ircmsg.sender.nick).setHost(ircmsg.sender.host);
  431. }*/
  432. // process known tags
  433. /* if('time' in ircmsg.tags){
  434. ircmsg.time = parseISOString(ircmsg.tags['time']);
  435. }
  436. if('account' in ircmsg.tags){
  437. users.getUser(ircmsg.sender.nick).setAccount(ircmsg.tags['account']);
  438. }*/
  439. // console.log(ircmsg);
  440. return ircmsg;
  441. }
  442. };
  443. module.exports = irc;