Skip to content

Instantly share code, notes, and snippets.

@czbaker
Created January 16, 2015 18:48
Show Gist options
  • Save czbaker/3507b5f7dc3647a80e73 to your computer and use it in GitHub Desktop.
Save czbaker/3507b5f7dc3647a80e73 to your computer and use it in GitHub Desktop.
1c1
< var net = Npm.require('net');
---
> net = Npm.require("net");
4,7c4,7
< * IRC Constructor
< *
< * Creates the IRC instance
< * @param params optional preferences for the connection
---
> IRC Constructor
>
> Creates the IRC instance
> @param params optional preferences for the connection
9c9,10
< IRC = function IRC(params) {
---
>
> IRC = IRC = function(params) {
11c12
< this.buffer = '';
---
> this.buffer = "";
13c14
< server: (params && params.server) || 'irc.freenode.net',
---
> server: (params && params.server) || "irc.freenode.net",
17,20c18,21
< nick: (params && params.nick) || 'meteorirc',
< password: (params && params.password) || '',
< realname: (params && params.realname) || 'Meteor IRC',
< username: (params && params.username) || 'Meteor-IRC',
---
> nick: (params && params.nick) || "meteorirc",
> password: (params && params.password) || "",
> realname: (params && params.realname) || "Meteor IRC",
> username: (params && params.username) || "Meteor-IRC",
23c24,25
< stripColors: (params && params.stripColors) || true
---
> stripColors: (params && params.stripColors) || true,
> servpass: (params && params.servpass) || null
26a29
>
28,30c31,33
< * connect
< *
< * Connects to the IRC server, sets up listeners for certain events
---
> connect
>
> Connects to the IRC server, sets up listeners for certain events
31a35
>
33c37,38
< var self = this;
---
> var self;
> self = this;
35c40,42
< if(self.config.debug) console.log('connecting...');
---
> if (self.config.debug) {
> console.log("connecting...");
> }
37c44,50
< this.connection.setEncoding('utf8');
---
> this.connection.setEncoding("utf8");
> this.connection.addListener("connect", function() {
> if (self.config.servpass) {
> self.send("PASS", self.config.servpass);
> }
> self.send("NICK", self.config.nick);
> self.send("USER", self.config.username, 8, "*", self.config.realname);
39,41d51
< this.connection.addListener('connect', function () {
< self.send('NICK', self.config.nick);
< self.send('USER', self.config.username, 8, "*", self.config.realname);
43c53
< self.send('JOIN', channel);
---
> self.send("JOIN", channel);
46,50c56,60
<
< //wrap in a meteor environment in order to use x.insert()
< //fixme: on motd, identify with nickserv
< this.connection.addListener('data', Meteor.bindEnvironment(function(chunk) {
< if(self.config.debug) console.log(chunk);
---
> this.connection.addListener("data", Meteor.bindEnvironment(function(chunk) {
> var lines;
> if (self.config.debug) {
> console.log(chunk);
> }
52c62
< var lines = self.buffer.split("\r\n");
---
> lines = self.buffer.split("\r\n");
54,56c64,67
< lines.forEach(function (dirtyLine) {
< var line = self.parseLine(dirtyLine);
< switch ( line.command ) {
---
> lines.forEach(function(dirtyLine) {
> var action, channel, handle, isLink, line, nick, text;
> line = self.parseLine(dirtyLine);
> switch (line.command) {
58,59c69
< self.send("PONG", line.args[0]);
< break;
---
> return self.send("PONG", line.args[0]);
61,62c71
< self.send("METEOR-IRC 1.0", line.args[0]);
< break;
---
> return self.send("METEOR-IRC 1.0", line.args[0]);
64,70c73,81
< var nick = line.nick ? line.nick.toLowerCase() : '';
< var text = line.args[1] ? line.args[1].toLowerCase() : '';
< if(nick === 'nickserv') {
< if(text.indexOf('registered') != -1) {
< self.say('nickserv', 'IDENTIFY ' + self.config.password);
< } else if(text.indexOf('invalid') != -1) {
< self.nick(self.config.nick + Math.floor(Math.random() * 10));
---
> nick = (line.nick ? line.nick.toLowerCase() : "");
> text = (line.args[1] ? line.args[1].toLowerCase() : "");
> if (nick === "nickserv") {
> if (text.indexOf("registered") !== -1) {
> return self.say("nickserv", "IDENTIFY " + self.config.password);
> } else {
> if (text.indexOf("invalid") !== -1) {
> return self.nick(self.config.nick + Math.floor(Math.random() * 10));
> }
75,80c86,91
< var handle = line.nick;
< var channel = line.args[0];
< var text = line.args[1];
< var action = false;
< if(text.substring(1, 7) === 'ACTION') {
< var text = text.substring(8);
---
> handle = line.nick;
> channel = line.args[0];
> text = line.args[1];
> action = false;
> if (text.substring(1, 7) === "ACTION") {
> text = text.substring(8);
83d93
< //insert irc message into db
93,95c103,109
< var isLink = text.match(/(https?|ftp):\/\/(([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+(:([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+)?@)?((([a-z0-9]\.|[a-z0-9][a-z0-9-]*[a-z0-9]\.)*[a-z][a-z0-9-]*[a-z0-9]|((\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])\.){3}(\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5]))(:\d+)?)(((\/+([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)*(\?([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?)?)?(#([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?/i);
< if(isLink != null) {
< IRCLinks.insert({url: isLink[0], channel: channel, ts: +new Date});
---
> isLink = text.match(/(https?|ftp):\/\/(([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+(:([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+)?@)?((([a-z0-9]\.|[a-z0-9][a-z0-9-]*[a-z0-9]\.)*[a-z][a-z0-9-]*[a-z0-9]|((\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])\.){3}(\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5]))(:\d+)?)(((\/+([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)*(\?([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?)?)?(#([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?/i);
> if (isLink != null) {
> return IRCLinks.insert({
> url: isLink[0],
> channel: channel,
> ts: +(new Date)
> });
99,100c113,115
< if(self.config.debug) console.log("QUIT: " + line.prefix + " " + line.args.join(" "));
< break;
---
> if (self.config.debug) {
> return console.log("QUIT: " + line.prefix + " " + line.args.join(" "));
> }
106,108c121,127
<
< this.connection.addListener('drain', function() {
< self.buffer = '';
---
> this.connection.addListener("drain", function() {
> self.buffer = "";
> });
> this.connection.addListener("close", function() {
> if (self.config.debug) {
> console.log("disconnected");
> }
110,113d128
<
< this.connection.addListener('close', function() {
< if(self.config.debug) console.log('disconnected');
< })
115a131
>
117,120c133,136
< *join
< *
< * sends a join command to the irc server
< * @param channel to join
---
> join
>
> sends a join command to the irc server
> @param channel to join
121a138
>
123,124c140,144
< if(this.connection) {
< this.send.apply(this, ['JOIN'].concat(channel));
---
> if (this.connection) {
> this.send.apply(this, ["JOIN"].concat(channel));
> IRCChannels.insert({
> channel: channel
> });
127a148
>
129,132c150,153
< *part
< *
< * sends a part command to the irc server
< * @param channel to part
---
> part
>
> sends a part command to the irc server
> @param channel to part
133a155,156
>
>
135,136c158,162
< if(this.connection) {
< this.send.apply(this, ['PART'].concat(channel));
---
> if (this.connection) {
> this.send.apply(this, ["PART"].concat(channel));
> IRCChannels.remove({
> channel: channel
> });
139a166
>
141,144c168,171
< *nick
< *
< * sends a nick command to the irc server
< * @param nickname
---
> nick
>
> sends a nick command to the irc server
> @param nickname
145a173
>
147,148c175,176
< if(this.connection) {
< this.send.apply(this, ['NICK'].concat(nickname));
---
> if (this.connection) {
> this.send.apply(this, ["NICK"].concat(nickname));
151a180
>
153,156c182,185
< * send
< *
< * sends a command to the irc server
< * @param command the command to send to the irc server
---
> send
>
> sends a command to the irc server
> @param command the command to send to the irc server
158,159d186
< IRC.prototype.send = function(command) {
< var args = Array.prototype.slice.call(arguments);
161,162c188,192
< if(args[args.length-1].match(/\s/) || args[args.length-1].match(/^:/) || args[args.length-1] === "") {
< args[args.length-1] = ":" + args[args.length-1];
---
> IRC.prototype.send = function(command) {
> var args;
> args = Array.prototype.slice.call(arguments);
> if (args[args.length - 1].match(/\s/) || args[args.length - 1].match(/^:/) || args[args.length - 1] === "") {
> args[args.length - 1] = ":" + args[args.length - 1];
164,165c194
<
< if(this.connection) {
---
> if (this.connection) {
167c196,198
< if(this.config.debug) console.log(args);
---
> if (this.config.debug) {
> console.log(args);
> }
170a202
>
172,176c204,208
< * say
< *
< * a convenience method that sends a message to a channel or user
< * @param channel the channel to send to
< * @param message the message
---
> say
>
> a convenience method that sends a message to a channel or user
> @param channel the channel to send to
> @param message the message
177a210
>
179c212
< this.send('PRIVMSG', channel, message);
---
> this.send("PRIVMSG", channel, message);
181a215
>
183,186c217,220
< * disconnect
< *
< * disconnects from a server
< * @param msg the quit message
---
> disconnect
>
> disconnects from a server
> @param msg the quit message
187a222
>
189c224,225
< var message = msg || 'Powered by Meteor-IRC http://github.com/Pent/meteor-irc';
---
> var message;
> message = msg || "Powered by Meteor-IRC http://github.com/Pent/meteor-irc";
193a230
>
195,198c232,235
< * parseLine
< *
< * transforms response from irc server into something usable
< * @param line the raw object received from the irc server
---
> parseLine
>
> transforms response from irc server into something usable
> @param line the raw object received from the irc server
200,203d236
< IRC.prototype.parseLine = function(line) {
< var message = {};
< var match;
< var stripColors = this.config.stripColors || true;
204a238,242
> IRC.prototype.parseLine = function(line) {
> var match, message, middle, stripColors, trailing;
> message = {};
> match = void 0;
> stripColors = this.config.stripColors || true;
208,210c246
<
< // Parse prefix
< if ( match = line.match(/^:([^ ]+) +/) ) {
---
> if (match = line.match(/^:([^ ]+) +/)) {
212,213c248,249
< line = line.replace(/^:[^ ]+ +/, '');
< if ( match = message.prefix.match(/^([_a-zA-Z0-9\[\]\\`^{}|-]*)(!([^@]+)@(.*))?$/) ) {
---
> line = line.replace(/^:[^ ]+ +/, "");
> if (match = message.prefix.match(/^([_a-zA-Z0-9\[\]\\`^{}|-]*)(!([^@]+)@(.*))?$/)) {
217,218c253
< }
< else {
---
> } else {
222,223d256
<
< // Parse command
227,229c260,261
< message.commandType = 'normal';
< line = line.replace(/^[^ ]+ +/, '');
<
---
> message.commandType = "normal";
> line = line.replace(/^[^ ]+ +/, "");
231,234c263,265
< var middle, trailing;
<
< // Parse parameters
< if ( line.search(/^:|\s+:/) != -1 ) {
---
> middle = void 0;
> trailing = void 0;
> if (line.search(/^:|\s+:/) !== -1) {
238,239c269
< }
< else {
---
> } else {
242,246c272,275
<
< if ( middle.length )
< message.args = middle.split(/ +/);
<
< if ( typeof(trailing) != 'undefined' && trailing.length )
---
> if (middle.length) {
> message.args = middle.split(RegExp(" +"));
> }
> if (typeof trailing !== "undefined" && trailing.length) {
248c277
<
---
> }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment