slate-irc
Version:
General purpose IRC client
3 lines (2 loc) • 12.2 kB
JavaScript
var t=require("events"),R=require("debug"),n=require("slate-irc-parser");function e(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var i=/*#__PURE__*/e(R),a=/*#__PURE__*/e(n),o={200:"RPL_TRACELINK",201:"RPL_TRACECONNECTING",202:"RPL_TRACEHANDSHAKE",203:"RPL_TRACEUNKNOWN",204:"RPL_TRACEOPERATOR",205:"RPL_TRACEUSER",206:"RPL_TRACESERVER",207:"RPL_TRACESERVICE",208:"RPL_TRACENEWTYPE",209:"RPL_TRACECLASS",211:"RPL_STATSLINKINFO",212:"RPL_STATSCOMMANDS",213:"RPL_STATSCLINE",214:"RPL_STATSNLINE",215:"RPL_STATSILINE",216:"RPL_STATSKLINE",217:"RPL_STATSQLINE",218:"RPL_STATSYLINE",219:"RPL_ENDOFSTATS",221:"RPL_UMODEIS",231:"RPL_SERVICEINFO",232:"RPL_ENDOFSERVICES",233:"RPL_SERVICE",234:"RPL_SERVLIST",235:"RPL_SERVLISTEND",239:"RPL_STATSIAUTH",240:"RPL_STATSVLINE",241:"RPL_STATSLLINE",242:"RPL_STATSUPTIME",243:"RPL_STATSOLINE",244:"RPL_STATSHLINE",245:"RPL_STATSSLINE",246:"RPL_STATSPING",247:"RPL_STATSBLINE",248:"RPL_STATSDEFINE",249:"RPL_STATSDEBUG",250:"RPL_STATSDLINE",251:"RPL_LUSERCLIENT",252:"RPL_LUSEROP",253:"RPL_LUSERUNKNOWN",254:"RPL_LUSERCHANNELS",255:"RPL_LUSERME",256:"RPL_ADMINME",257:"RPL_ADMINLOC1",258:"RPL_ADMINLOC2",259:"RPL_ADMINEMAIL",261:"RPL_TRACELOG",262:"RPL_TRACEEND",263:"RPL_TRYAGAIN",265:"RPL_LOCALUSERS",266:"RPL_GLOBALUSERS",300:"RPL_NONE",301:"RPL_AWAY",302:"RPL_USERHOST",303:"RPL_ISON",304:"RPL_TEXT",305:"RPL_UNAWAY",306:"RPL_NOWAWAY",311:"RPL_WHOISUSER",312:"RPL_WHOISSERVER",313:"RPL_WHOISOPERATOR",314:"RPL_WHOWASUSER",315:"RPL_ENDOFWHO",316:"RPL_WHOISCHANOP",317:"RPL_WHOISIDLE",318:"RPL_ENDOFWHOIS",319:"RPL_WHOISCHANNELS",321:"RPL_LISTSTART",322:"RPL_LIST",323:"RPL_LISTEND",324:"RPL_CHANNELMODEIS",325:"RPL_UNIQOPIS",331:"RPL_NOTOPIC",332:"RPL_TOPIC",333:"RPL_TOPIC_WHO_TIME",341:"RPL_INVITING",342:"RPL_SUMMONING",344:"RPL_REOPLIST",345:"RPL_ENDOFREOPLIST",346:"RPL_INVITELIST",347:"RPL_ENDOFINVITELIST",348:"RPL_EXCEPTLIST",349:"RPL_ENDOFEXCEPTLIST",351:"RPL_VERSION",352:"RPL_WHOREPLY",353:"RPL_NAMREPLY",361:"RPL_KILLDONE",362:"RPL_CLOSING",363:"RPL_CLOSEEND",364:"RPL_LINKS",365:"RPL_ENDOFLINKS",366:"RPL_ENDOFNAMES",367:"RPL_BANLIST",368:"RPL_ENDOFBANLIST",369:"RPL_ENDOFWHOWAS",371:"RPL_INFO",372:"RPL_MOTD",373:"RPL_INFOSTART",374:"RPL_ENDOFINFO",375:"RPL_MOTDSTART",376:"RPL_ENDOFMOTD",381:"RPL_YOUREOPER",382:"RPL_REHASHING",383:"RPL_YOURESERVICE",384:"RPL_MYPORTIS",385:"RPL_NOTOPERANYMORE",391:"RPL_TIME",392:"RPL_USERSSTART",393:"RPL_USERS",394:"RPL_ENDOFUSERS",395:"RPL_NOUSERS",401:"ERR_NOSUCHNICK",402:"ERR_NOSUCHSERVER",403:"ERR_NOSUCHCHANNEL",404:"ERR_CANNOTSENDTOCHAN",405:"ERR_TOOMANYCHANNELS",406:"ERR_WASNOSUCHNICK",407:"ERR_TOOMANYTARGETS",408:"ERR_NOSUCHSERVICE",409:"ERR_NOORIGIN",411:"ERR_NORECIPIENT",412:"ERR_NOTEXTTOSEND",413:"ERR_NOTOPLEVEL",414:"ERR_WILDTOPLEVEL",415:"ERR_BADMASK",416:"ERR_TOOMANYMATCHES",421:"ERR_UNKNOWNCOMMAND",422:"ERR_NOMOTD",423:"ERR_NOADMININFO",424:"ERR_FILEERROR",431:"ERR_NONICKNAMEGIVEN",432:"ERR_ERRONEOUSNICKNAME",433:"ERR_NICKNAMEINUSE",434:"ERR_SERVICENAMEINUSE",435:"ERR_SERVICECONFUSED",436:"ERR_NICKCOLLISION",437:"ERR_UNAVAILRESOURCE",441:"ERR_USERNOTINCHANNEL",442:"ERR_NOTONCHANNEL",443:"ERR_USERONCHANNEL",444:"ERR_NOLOGIN",445:"ERR_SUMMONDISABLED",446:"ERR_USERSDISABLED",451:"ERR_NOTREGISTERED",461:"ERR_NEEDMOREPARAMS",462:"ERR_ALREADYREGISTRED",463:"ERR_NOPERMFORHOST",464:"ERR_PASSWDMISMATCH",465:"ERR_YOUREBANNEDCREEP",466:"ERR_YOUWILLBEBANNED",467:"ERR_KEYSET",471:"ERR_CHANNELISFULL",472:"ERR_UNKNOWNMODE",473:"ERR_INVITEONLYCHAN",474:"ERR_BANNEDFROMCHAN",475:"ERR_BADCHANNELKEY",476:"ERR_BADCHANMASK",477:"ERR_NOCHANMODES",478:"ERR_BANLISTFULL",481:"ERR_NOPRIVILEGES",482:"ERR_CHANOPRIVSNEEDED",483:"ERR_CANTKILLSERVER",484:"ERR_RESTRICTED",485:"ERR_UNIQOPRIVSNEEDED",491:"ERR_NOOPERHOST",492:"ERR_NOSERVICEHOST",499:"ERR_STATSKLINE",501:"ERR_UMODEUNKNOWNFLAG",502:"ERR_USERSDONTMATCH",708:"RPL_ETRACEFULL",759:"RPL_ETRACEEND","001":"RPL_WELCOME","002":"RPL_YOURHOST","003":"RPL_CREATED","004":"RPL_MYINFO","005":"RPL_ISUPPORT","010":"RPL_BOUNCE","015":"RPL_MAP","017":"RPL_MAPEND","018":"RPL_MAPSTART","020":"RPL_HELLO","042":"RPL_YOURID","043":"RPL_SAVENICK"};function E(t){return t.prefix.split("!")[0]}function s(t){var R={},n=t.prefix.split("!");R.nick=n[0];try{n=n[1].split("@"),R.username=n[0],R.hostname=n[1]}catch(t){}return R.string=t.prefix,R}const L=i.default("slate-irc:names");function r(t,R){var n=this;t=t.toLowerCase(),R&&(this.nameCallbacks[t]=function(e){delete n.nameCallbacks[t],R(null,e.names)}),this.write("NAMES "+t)}function N(t,R,n){"function"==typeof R&&(n=R,R="");var e=this;t=t.toLowerCase(),n&&(this.whoisCallbacks[t]=function(R,i){delete e.whoisCallbacks[t],n(R,i)}),this.write(["WHOIS",t,R].filter(Boolean).join(" "))}i.default("slate-irc:whois");const _=i.default("slate-irc");function c(t,R,n){if(!(this instanceof c))return new c(t,R,n);t.setEncoding(n||"utf8"),this.stream=t,this.parser=R||new a.default,this.parser.on("message",this.onmessage.bind(this)),t.pipe(this.parser),this.setMaxListeners(100),this.use(function(t){t.on("data",function(R){if("RPL_AWAY"==R.command||"RPL_NOWAWAY"==R.command||"RPL_UNAWAY"==R.command){var n=R.params.split(" "),e={};e.nick="RPL_NOWAWAY"==R.command||"RPL_UNAWAY"==R.command?n[0]:n[1],e.message=R.trailing,t.emit("away",e)}})}),this.use(function(t){var R=t.stream;R.on("close",function(){t.emit("disconnect")}),null!=R.setTimeout&&(R.setTimeout(6e4),R.on("timeout",function(){var n=!0,e=(new Date).getTime();t.write("PING :"+e),t.once("pong",function(t){t==e&&(n=!1)}),setTimeout(function(){n&&R.destroy()},5e3)}))}),this.use(function(t){t.on("data",function(R){if(0===R.command.indexOf("ERR_")){var n={};n.cmd=R.command,n.message=R.trailing,t.emit("errors",n)}})}),this.use(function(t){t.on("data",function(R){if("INVITE"==R.command){var n={};n.from=E(R),n.hostmask=s(R),n.to=R.params.toLowerCase(),n.channel=R.trailing,t.emit("invite",n)}})}),this.use(function(t){t.on("data",function(R){if("JOIN"==R.command){var n={};n.nick=E(R),n.hostmask=s(R),n.channel=(R.params||R.trailing).toLowerCase(),t.emit("join",n)}})}),this.use(function(t){t.on("data",function(R){if("KICK"==R.command){var n={},e=R.params.split(" ");n.nick=E(R),n.hostmask=s(R),n.channel=e[0].toLowerCase(),n.client=e[1],n.message=R.trailing,t.emit("kick",n)}})}),this.use(function(t){t.on("data",function(R){if("MODE"==R.command){var n=R.params.split(" "),e={};e.nick=E(R),e.target=n[0],e.mode=n[1]||R.trailing,e.client=n[2],t.emit("mode",e)}})}),this.use(function(t){var R={motd:[]};t.on("data",function(n){switch(n.command){case"RPL_MOTDSTART":R.motd.length=0;case"RPL_ENDOFMOTD":case"RPL_MOTD":R.motd.push(n.trailing)}"RPL_ENDOFMOTD"==n.command&&t.emit("motd",R)})}),this.use(function(t){var R={};t.names=r,t.nameCallbacks={},t.on("data",function(n){switch(n.command){case"RPL_NAMREPLY":var e=n.params.split(/ [=@*] /)[1].toLowerCase(),i=n.trailing.split(" "),a=[];i.forEach(function(t){var R=t.split(/([~&@%+])/),n=R.pop(),e=R.pop();a.push({name:n,mode:e||""})}),R[e]=(R[e]||[]).concat(a),L("add %s %j",e,a);break;case"RPL_ENDOFNAMES":e=n.params.split(" ")[1].toLowerCase(),L('emit "names" for %s',e);var o={channel:e,names:R[e]||[]},E=t.nameCallbacks[e];E?E(o):t.emit("names",o),delete R[e]}})}),this.use(function(t){t.on("data",function(R){if("NICK"==R.command){var n={};n.nick=E(R),n.hostmask=s(R),n.new=R.trailing,n.new||(n.new=R.params),n.nick==t.me&&(t.me=n.new),t.emit("nick",n)}})}),this.use(function(t){t.on("data",function(R){if("NOTICE"==R.command){var n={};n.from=E(R),n.hostmask=s(R),n.to=R.params.toLowerCase(),n.message=R.trailing,t.emit("notice",n)}})}),this.use(function(t){t.on("data",function(R){if("PART"==R.command){var n={};n.nick=E(R),n.hostmask=s(R),n.channels=R.params.split(",").map(function(t){return t.toLowerCase()}),n.message=R.trailing,t.emit("part",n)}})}),this.use(function(t){t.on("data",function(R){"PONG"==R.command&&t.emit("pong",R.trailing),"PING"==R.command&&t.write("PONG :"+R.trailing)})}),this.use(function(t){t.on("data",function(R){if("PRIVMSG"==R.command){var n={};n.from=E(R),n.hostmask=s(R),n.to=R.params.toLowerCase(),n.message=R.trailing,t.emit("message",n)}})}),this.use(function(t){t.on("data",function(R){if("QUIT"==R.command){var n={};n.nick=E(R),n.hostmask=s(R),n.message=R.trailing,t.emit("quit",n)}})}),this.use(function(t){var R;t.on("data",function(n){switch(n.command){case"RPL_NOTOPIC":case"RPL_TOPIC":R=n.params.split(" ")[1];break;case"TOPIC":R=n.params;break;default:return}var e={};"TOPIC"==n.command&&(e.nick=E(n)),e.hostmask=s(n),e.channel=R.toLowerCase(),e.topic=n.trailing,t.emit("topic",e)})}),this.use(function(t){t.on("data",function(R){"RPL_WELCOME"==R.command&&(t.me=R.params,t.emit("welcome",t.me))})}),this.use(function(t){var R={};t.whois=N,t.whoisCallbacks={},t.on("data",function(n){switch(n.command){case"RPL_WHOISUSER":var e=(o=n.params.split(" "))[1].toLowerCase();R[e]=R[e]||{},R[e].nickname=o[1],R[e].username=o[2],R[e].hostname=o[3],R[e].realname=n.trailing,R[e].channels=[],R[e].oper=!1;break;case"RPL_WHOISCHANNELS":e=(o=n.params.split(" "))[1].toLowerCase();var i=n.trailing.split(" ");R[e].channels=R[e].channels.concat(i);break;case"RPL_WHOISSERVER":e=(o=n.params.split(" "))[1].toLowerCase(),R[e].server=o[2];break;case"RPL_AWAY":if(e=(o=n.params.split(" "))[1].toLowerCase(),!R[e])return;R[e].away=n.trailing;break;case"RPL_WHOISOPERATOR":e=(o=n.params.split(" "))[1].toLowerCase(),R[e].oper=!0;break;case"RPL_WHOISIDLE":e=(o=n.params.split(" "))[1].toLowerCase(),R[e].idle=o[2],R[e].sign=o[3];break;case"RPL_ENDOFWHOIS":if(e=(o=n.params.split(" "))[1].toLowerCase(),!R[e])return;(E=t.whoisCallbacks[e])?E(a,R[e]):t.emit("whois",null,R[e]),R={};break;case"ERR_NEEDMOREPARAMS":var a="Not enough parameters";if("whois"!==(e=(o=n.params.split(" "))[1].toLowerCase()))return;(E=t.whoisCallbacks[e])?E(a,null):t.emit("whois",a,null);break;case"ERR_NOSUCHSERVER":a="No such server",e=(o=n.params.split(" "))[1].toLowerCase(),(E=t.whoisCallbacks[e])?E(a,null):t.emit("whois",a,null);break;case"ERR_NOSUCHNICK":var o,E;a="No such nick/channel",e=(o=n.params.split(" "))[1].toLowerCase(),(E=t.whoisCallbacks[e])?E(a,null):t.emit("whois",a,null)}})})}function P(t){return Array.isArray(t)?t:[t]}c.prototype.__proto__=t.EventEmitter.prototype,c.prototype.writeUnsafe=function(t,R){this.stream.write(t+"\r\n",R)},c.prototype.write=function(t,R){-1==t.indexOf("\n")&&-1==t.indexOf("\r")?this.writeUnsafe(t,R):R&&R(new Error("The parameter to write() must not contain any '\\n' or '\\r'."))},c.prototype.pass=function(t,R){this.write("PASS "+t,R)},c.prototype.webirc=function(t,R,n,e,i){var a=[t,R,n,e].join(" ");this.write("WEBIRC "+a,i)},c.prototype.nick=function(t,R){this.write("NICK "+t,R)},c.prototype.user=function(t,R,n){this.write("USER "+t+" 0 * :"+R,n)},c.prototype.invite=function(t,R,n){this.write("INVITE "+t+" "+R,n)},c.prototype.send=function(t,R,n){this.write("PRIVMSG "+P(t).join(",")+" :"+R,n)},c.prototype.action=function(t,R,n){this.send(t,"ACTION "+R+"",n)},c.prototype.notice=function(t,R,n){this.write("NOTICE "+t+" :"+R,n)},c.prototype.ctcp=function(t,R,n){this.notice(t,""+R+"",n)},c.prototype.join=function(t,R,n){"function"==typeof R&&(n=R,R=""),this.write("JOIN "+P(t).join(",")+" "+P(R).join(","),n)},c.prototype.part=function(t,R,n){"function"==typeof R&&(n=R,R="");var e="PART "+P(t).join(",");R&&(e+=" :"+R),this.write(e,n)},c.prototype.away=function(t,R){this.write("AWAY :"+(t=t||"Talk to you later!"),R)},c.prototype.back=function(t){this.write("AWAY",t)},c.prototype.topic=function(t,R,n){"function"==typeof R&&(n=R,R=""),R&&(R=" :"+R),this.write("TOPIC "+t+R,n)},c.prototype.kick=function(t,R,n,e){"function"==typeof n&&(e=n,n="");var i="KICK "+P(t).join(",")+" "+P(R).join(",");n&&(i+=" :"+n),this.write(i,e)},c.prototype.quit=function(t,R){this.write("QUIT :"+(t=t||"Bye!"),R)},c.prototype.oper=function(t,R,n){this.write("OPER "+t+" "+R,n)},c.prototype.mode=function(t,R,n,e){"function"==typeof n&&(e=n,n=""),this.write(n?"MODE "+t+" "+R+" "+n:"MODE "+t+" "+R,e)},c.prototype.use=function(t){return t(this),this},c.prototype.onmessage=function(t){t.command=o[t.command]||t.command,_("message %s %s",t.command,t.string),this.emit("data",t)},module.exports=c;
//# sourceMappingURL=slate-irc.js.map