irc.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. module.exports.setConnection = setConnection;
  13. function findServer(s){
  14. for(var i=0; i<servers.length; i++){
  15. srv = servers[i];
  16. if(srv.sid == s || srv.name == s) return srv;
  17. }
  18. return null;
  19. }
  20. function findUser(u){
  21. for(var i=0; i<users.length; i++){
  22. usr = users[i];
  23. if(usr.uid == u || usr.name == u) return usr;
  24. }
  25. return null;
  26. }
  27. function getChannel(name){
  28. var lowerName = name.toLowerCase();
  29. var channel = null;
  30. for(var i=0; i<channels.length; i++){
  31. if(channels[i].name == lowerName){
  32. channel = channels[i];
  33. break;
  34. }
  35. }
  36. if(!channel){
  37. console.log('Creating channel '+name);
  38. channel = new IRCchannel;
  39. channel.name = name;
  40. channels.push(channel);
  41. }
  42. return channel;
  43. }
  44. function killUser(user){
  45. console.log('KILL');
  46. console.log(user);
  47. // TODO
  48. }
  49. 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
  50. var user;
  51. if((user = findUser(uid)) || (user = findUser(nick))){
  52. console.log(user);
  53. throw 'User already exists';
  54. }
  55. console.log('Introducing user '+nick);
  56. user = new IRCuser;
  57. user.introduce(nick, distance, TS, ident, host, uid, account, umodes, vhost, cloakedHost, ip, realname, uplink);
  58. users.push(user);
  59. }
  60. function newServer(name, sid, desc, distance, uplink){
  61. if(findServer(sid) || findServer(name)){
  62. throw 'Server already exists';
  63. }
  64. console.log('Introducing server '+name+' (SID='+sid+', desc="'+desc+'")');
  65. server = new IRCserver;
  66. server.introduce(name, sid, desc, distance, uplink);
  67. servers.push(server);
  68. }
  69. function parseUmodes(text){
  70. return null;
  71. // TODO
  72. }
  73. function setConnection(host, port, protocol){
  74. uplink = require('./protocol/' + protocol);
  75. me = new IRCserver;
  76. me.introduce('serwisy.pirc.pl', '11K', 'Serwisy', 0, null);
  77. servers.push(me);
  78. uplink.setHandlers({
  79. send: ircSendData,
  80. findServer: findServer,
  81. findUser: findUser,
  82. killUser: killUser,
  83. newServer: newServer,
  84. newUser: newUser,
  85. parseUmodes: parseUmodes,
  86. getChannel: getChannel
  87. });
  88. uplink.setSettings({
  89. ID: me.sid,
  90. password: 'myservicespassword',
  91. name: me.name,
  92. description: me.description,
  93. version: 'k4be-services',
  94. me: me
  95. });
  96. connection = new net.Socket();
  97. connection.connect(port, host, uplink.connected.bind(this));
  98. connection.on('data', ircDataReceived);
  99. connection.on('close', ircConnectionClosed);
  100. }
  101. function ircSendData(tags, from, cmd, args){
  102. var data = makeTagsString(tags);
  103. if(from){
  104. data += ':' + from + ' ';
  105. }
  106. data += cmd;
  107. if(args) for(var i=0; i<args.length; i++){
  108. var arg = args[i];
  109. if(arg.indexOf(' ') >= 0){ // valid for last arg only
  110. arg = ':' + arg;
  111. }
  112. data += ' ' + arg;
  113. }
  114. console.log('<<< '+data);
  115. data += "\r\n";
  116. connection.write(data);
  117. }
  118. function ircDataReceived(data){
  119. data = data.toString('utf8');
  120. recvData += data;
  121. var buffer = '';
  122. for(var i=0; i<recvData.length; i++){
  123. var c = recvData.charAt(i);
  124. switch(c){
  125. default: buffer += c; break;
  126. case '\r': case '\n':
  127. if(buffer.length > 0)
  128. ircMessage(buffer);
  129. buffer = '';
  130. break;
  131. }
  132. }
  133. recvData = buffer;
  134. }
  135. function makeTagsString(tags){
  136. return '';
  137. }
  138. function ircMessage(data){
  139. var msg = irc.parseLine(data);
  140. uplink.processMessage(msg);
  141. }
  142. function ircConnectionClosed(e){
  143. console.log('Closed: '+e);
  144. process.exit(1);
  145. }
  146. function processReplyTags(inputTags, newTags){
  147. return newTags;
  148. // TODO
  149. }
  150. var irc = {
  151. 'messagedata': function() {
  152. this.text = '';
  153. this.args = [];
  154. this.tags = [];
  155. this.command = '';
  156. this.sender = {
  157. 'nick': '',
  158. 'ident': '',
  159. 'host': '',
  160. 'server': false,
  161. 'user': false
  162. };
  163. this.time = new Date();
  164. this.reply = function(tags, cmd, args){
  165. var outTags = processReplyTags(this.tags, tags);
  166. ircSendData(outTags, this.sender.nick, cmd, args);
  167. }
  168. this.originalString = '';
  169. },
  170. 'parseTags': function(tagsLine){
  171. var tags = [];
  172. var tagState = 'keyName';
  173. var keyValue;
  174. var keyName = '';
  175. for(var i = 0; i < tagsLine.length; i++){
  176. var cchar = tagsLine.charAt(i);
  177. switch(tagState){
  178. case 'keyName':
  179. switch(cchar){
  180. case '=':
  181. tagState = 'keyValue';
  182. keyValue = '';
  183. break;
  184. case ';':
  185. tags[keyName] = '';
  186. keyName = ''; // staying in tagStateKeyName
  187. break;
  188. default: keyName += cchar; break;
  189. }
  190. break;
  191. case 'keyValue':
  192. switch(cchar){
  193. case '\\': tagState = 'keyValueEscape'; break;
  194. case ';':
  195. tags[keyName] = keyValue;
  196. keyName = '';
  197. tagState = 'keyName';
  198. break;
  199. default: keyValue += cchar; break;
  200. }
  201. break;
  202. case 'keyValueEscape':
  203. switch(cchar){
  204. case ':': keyValue += ';'; break;
  205. case 's': keyValue += ' '; break;
  206. case 'r': keyValue += '\r'; break;
  207. case 'n': keyValue += '\n'; break;
  208. default: keyValue += cchar; break;
  209. }
  210. tagState = 'keyValue';
  211. break;
  212. }
  213. }
  214. if(keyName.length > 0) tags[keyName] = keyValue; // flush last tag
  215. return tags;
  216. },
  217. 'parseLine': function(line){
  218. var ircmsg = new irc.messagedata();
  219. var line = line.trim();
  220. line.replace(/^\s+|\s+$/gm,'');
  221. if(line == ''){
  222. return;
  223. }
  224. ircmsg.originalString = line;
  225. var msglen = line.length;
  226. var pstate = 'start';
  227. var currArg = '';
  228. var tags = '';
  229. var haveText = false;
  230. console.log('>>> ' + line);
  231. for(var i = 0; i < msglen; i++){
  232. var cchar = line.charAt(i);
  233. switch(pstate){
  234. case 'start':
  235. switch(cchar){
  236. case '@': pstate = 'tags'; break;
  237. case ':': pstate = 'senderNick'; break;
  238. default:
  239. pstate = 'command';
  240. ircmsg.command += cchar;
  241. break;
  242. }
  243. break;
  244. case 'tags':
  245. switch(cchar){
  246. case ' ':
  247. pstate = 'start';
  248. ircmsg.tags = irc.parseTags(tags);
  249. break;
  250. default: tags += cchar; break;
  251. }
  252. break;
  253. case 'senderNick':
  254. switch(cchar){
  255. case '!': pstate = 'senderUser'; break;
  256. case '@': pstate = 'senderHost'; break;
  257. case ' ': pstate = 'command'; break;
  258. default: ircmsg.sender.nick += cchar; break;
  259. }
  260. break;
  261. case 'senderUser':
  262. switch(cchar){
  263. case '@': pstate = 'senderHost'; break;
  264. case ' ': pstate = 'command'; break;
  265. default: ircmsg.sender.ident += cchar; break;
  266. }
  267. break;
  268. case 'senderHost':
  269. switch(cchar){
  270. case ' ': pstate = 'command'; break;
  271. default: ircmsg.sender.host += cchar; break;
  272. }
  273. break;
  274. case 'command':
  275. switch(cchar){
  276. case ' ': pstate = 'args'; break;
  277. default: ircmsg.command += cchar; break;
  278. }
  279. break;
  280. case 'args':
  281. switch(cchar){
  282. case ' ':
  283. if(currArg != ''){
  284. ircmsg.args.push(currArg);
  285. }
  286. currArg = '';
  287. break;
  288. case ':':
  289. if(prevChar == ' '){
  290. pstate = 'message';
  291. haveText = true;
  292. } else {
  293. currArg += cchar;
  294. }
  295. break;
  296. default: currArg += cchar; break;
  297. }
  298. break;
  299. case 'message':
  300. ircmsg.text += cchar;
  301. break;
  302. }
  303. var prevChar = cchar;
  304. }
  305. if(pstate == 'args'){
  306. ircmsg.args.push(currArg);
  307. }
  308. ircmsg.sender = uplink.processSender(ircmsg.sender);
  309. if(!ircmsg.sender){
  310. return false;
  311. }
  312. if(!ircmsg.sender.server && !ircmsg.sender.user){
  313. if(ircmsg.sender.ident == '' && ircmsg.sender.host == '' && ircmsg.sender.nick.indexOf('.')!=-1){
  314. ircmsg.sender.server = true;
  315. } else {
  316. ircmsg.sender.user = true;
  317. }
  318. }
  319. if(!haveText){
  320. ircmsg.text = ircmsg.args[ircmsg.args.length-1]; // handling last argument as text if : is missing
  321. } else {
  322. ircmsg.args.push(ircmsg.text); // handling text as a last argument as required by the protocol
  323. }
  324. // add u@h
  325. /* if(ircmsg.sender.user){
  326. if(ircmsg.sender.ident) users.getUser(ircmsg.sender.nick).setIdent(ircmsg.sender.ident);
  327. if(ircmsg.sender.host) users.getUser(ircmsg.sender.nick).setHost(ircmsg.sender.host);
  328. }*/
  329. // process known tags
  330. /* if('time' in ircmsg.tags){
  331. ircmsg.time = parseISOString(ircmsg.tags['time']);
  332. }
  333. if('account' in ircmsg.tags){
  334. users.getUser(ircmsg.sender.nick).setAccount(ircmsg.tags['account']);
  335. }*/
  336. // console.log(ircmsg);
  337. return ircmsg;
  338. }
  339. };