Permalink
Browse files

Good thing we're not in version 1.0.0 yet, or this update would be a …

…version bump
  • Loading branch information...
1 parent 0c7fd65 commit 1d774195c33825c80e8a495cbb83dedc55199cfd @sent1nel committed Jun 28, 2012
Showing with 153 additions and 141 deletions.
  1. +4 −4 README.md
  2. +130 −128 index.js
  3. +19 −9 tests/noob_client.js
View
@@ -20,9 +20,9 @@ Derp
### "PING"
-## Client Events
+## Client Commands
-The driver listens for the following events, which may be emitted by the client:
+The driver exposes the following commands, which may be called by the client:
### "PONG" (hostname)
@@ -33,7 +33,7 @@ The proper way to respond to a PING event from the server. Hostname is an option
Sets a user's mode in the channel
```js
- client.emit("mode", "#example", true, "v", null, "zipp", null);
+ client.mode("#example", true, "v", null, "zipp", null);
```
### "register" (nickname, hostname, servername)
@@ -57,7 +57,7 @@ Attempts to leave a channel; reason argument is optional.
Attempts to kick a user from a channel; reason argument is optional.
```js
- client.emit("kick", "#example", "zipp", "Stop derailing chat with links to pictures of cats");
+ client.kick("#example", "zipp", "Stop derailing chat with links to pictures of cats");
```
### "invite" (user, channel)
View
258 index.js
@@ -42,134 +42,6 @@ function zIRCClient(stream, options) {
this.send_command("PONG %s", [ hostname ]);
});
- this.on("mode", function (channel, set, modes, limit, user, mask) {
- var command = '';
- if (set) {
- command = "MODE " + channel + " +" + modes;
- }
- else {
- command = "MODE " + channel + " -" + modes;
- }
- if (typeof limit == 'number') {
- command = command + " " + limit;
- }
- else if (typeof user === 'string') {
- command = command + " " + user;
- }
- else if (typeof mask === 'string') {
- command = command + " " + mask;
- }
- self.send_command(command);
- });
-
- this.on("register", function (nickname, hostname, servername) {
- if (typeof self.options.password === 'string') {
- self.send_command("PASS %s", [ self.options.password ]);
- }
- self.emit("setNick", nickname);
- if (typeof self.options.username !== 'string') {
- self.options.username = nickname;
- }
- self.send_command("USER %s %s %s :%s", [ self.options.username, hostname, servername, self.options.realname ]);
- });
-
- this.on("setNick", function (nickname) {
- self._triedNickname = nickname;
- self.send_command("NICK %s", [ nickname ]);
- });
-
- this.on("join", function (channel, key) {
- if (CHANNEL_PREFIXES.indexOf(channel.charAt(0)) == -1) {
- channel = "#" + channel;
- }
- if (typeof key !== 'string') {
- self.send_command("JOIN %s", [ channel ]);
- }
- else {
- self.send_command("JOIN %s %s", [ channel, key ]);
- }
- });
-
- this.on("leave", function (channel, reason) {
- if (CHANNEL_PREFIXES.indexOf(channel.charAt(0)) == -1) {
- channel = "#" + channel;
- }
- if (typeof reason !== 'string') {
- self.send_command("PART %s", [ channel ]);
- }
- else {
- self.send_command("PART %s :%s", [ channel, reason ]);
- }
- });
-
- this.on("kick", function (channel, user, reason) {
- if (CHANNEL_PREFIXES.indexOf(channel.charAt(0)) == -1) {
- channel = "#" + channel;
- }
- if (typeof reason !== 'string') {
- self.send_command("KICK %s %s", [ channel, user ]);
- }
- else {
- self.send_command("KICK %s %s :%s", [ channel, user, reason ]);
- }
- });
-
- this.on("invite", function (user, channel) {
- if (CHANNEL_PREFIXES.indexOf(channel.charAt(0)) == -1) {
- channel = "#" + channel;
- }
- self.send_command("INVITE %s %s", [ user, channel ]);
- });
-
- this.on("topic", function (channel, topic) {
- if (CHANNEL_PREFIXES.indexOf(channel.charAt(0)) == -1) {
- channel = "#" + channel;
- }
- if (typeof topic !== 'string') {
- this.send_command("TOPIC %s", [ channel ]);
- }
- else {
- this.send_command("TOPIC %s :%s", [ channel, topic ]);
- }
- });
-
- this.on("msg", function (user, message) {
- this.send_command("PRIVMSG %s :%s", [ user, message ]);
- });
-
- this.on("notice", function (user, message) {
- this.send_command("NOTICE %s :%s", [ user, message ]);
- });
-
- this.on("away", function (message) {
- message = (typeof message === 'string') ? message : '';
- this.send_command("AWAY :%s", [ message ]);
- });
-
- this.on("back", function () {
- this.emit("away");
- });
-
- this.on("whois", function(nickname, server) {
- if (!server) {
- this.send_command("WHOIS %s", [ nickname ]);
- }
- else {
- this.send_command("WHOIS %s %s", [ nickname, server ]);
- }
- });
-
- this.on("say", function (channel, message) {
- if (CHANNEL_PREFIXES.indexOf(channel.charAt(0)) == -1) {
- channel = "#" + channel;
- }
- this.send_command("PRIVMSG %s :%s", [ channel, message ]);
- });
-
- this.on("quit", function (message) {
- this.send_command("QUIT :%s", [ message ]);
- });
-
/*
Connection events
*/
@@ -257,6 +129,136 @@ zIRCClient.prototype.handle_command = function (message) {
return true;
}
+zIRCClient.prototype.join = function (channel, key) {
+ if (CHANNEL_PREFIXES.indexOf(channel.charAt(0)) == -1) {
+ channel = "#" + channel;
+ }
+ if (typeof key !== 'string') {
+ this.send_command("JOIN %s", [ channel ]);
+ }
+ else {
+ this.send_command("JOIN %s %s", [ channel, key ]);
+ }
+};
+
+zIRCClient.prototype.leave = function (channel, reason) {
+ if (CHANNEL_PREFIXES.indexOf(channel.charAt(0)) == -1) {
+ channel = "#" + channel;
+ }
+ if (typeof reason !== 'string') {
+ this.send_command("PART %s", [ channel ]);
+ }
+ else {
+ this.send_command("PART %s :%s", [ channel, reason ]);
+ }
+};
+
+zIRCClient.prototype.kick = function (channel, user, reason) {
+ if (CHANNEL_PREFIXES.indexOf(channel.charAt(0)) == -1) {
+ channel = "#" + channel;
+ }
+ if (typeof reason !== 'string') {
+ this.send_command("KICK %s %s", [ channel, user ]);
+ }
+ else {
+ this.send_command("KICK %s %s :%s", [ channel, user, reason ]);
+ }
+};
+
+zIRCClient.prototype.invite = function (user, channel) {
+ if (CHANNEL_PREFIXES.indexOf(channel.charAt(0)) == -1) {
+ channel = "#" + channel;
+ }
+ this.send_command("INVITE %s %s", [ user, channel ]);
+};
+
+zIRCClient.prototype.topic = function (channel, topic) {
+ if (CHANNEL_PREFIXES.indexOf(channel.charAt(0)) == -1) {
+ channel = "#" + channel;
+ }
+ if (typeof topic !== 'string') {
+ this.send_command("TOPIC %s", [ channel ]);
+ }
+ else {
+ this.send_command("TOPIC %s :%s", [ channel, topic ]);
+ }
+};
+
+zIRCClient.prototype.msg = function (user, message) {
+ this.send_command("PRIVMSG %s :%s", [ user, message ]);
+};
+
+zIRCClient.prototype.notice = function (user, message) {
+ this.send_command("NOTICE %s :%s", [ user, message ]);
+};
+
+zIRCClient.prototype.say = function (channel, message) {
+ if (CHANNEL_PREFIXES.indexOf(channel.charAt(0)) == -1) {
+ channel = "#" + channel;
+ }
+ this.send_command("PRIVMSG %s :%s", [ channel, message ]);
+};
+
+zIRCClient.prototype.mode = function (channel, set, modes, limit, user, mask) {
+ var command = '';
+ if (set) {
+ command = "MODE " + channel + " +" + modes;
+ }
+ else {
+ command = "MODE " + channel + " -" + modes;
+ }
+ if (typeof limit == 'number') {
+ command = command + " " + limit;
+ }
+ else if (typeof user === 'string') {
+ command = command + " " + user;
+ }
+ else if (typeof mask === 'string') {
+ command = command + " " + mask;
+ }
+ this.send_command(command);
+};
+
+zIRCClient.prototype.register = function (nickname, hostname, servername) {
+ if (typeof this.options.password === 'string') {
+ this.send_command("PASS %s", [ this.options.password ]);
+ }
+ this.setNick(nickname);
+ if (typeof this.options.username !== 'string') {
+ this.options.username = nickname;
+ }
+ this.send_command("USER %s %s %s :%s", [ this.options.username, hostname, servername, this.options.realname ]);
+};
+
+zIRCClient.prototype.setNick = function (nickname) {
+ this._triedNickname = nickname;
+ if (typeof nickname === 'string') {
+ this.send_command("NICK %s", [ nickname ]);
+ }
+};
+
+zIRCClient.prototype.away = function (message) {
+ message = (typeof message === 'string') ? message : '';
+ this.send_command("AWAY :%s", [ message ]);
+};
+
+zIRCClient.prototype.back = function () {
+ this.away();
+};
+
+zIRCClient.prototype.whois = function (nickname, server) {
+ if (!server) {
+ this.send_command("WHOIS %s", [ nickname ]);
+ }
+ else {
+ this.send_command("WHOIS %s %s", [ nickname, server ]);
+ }
+};
+
+zIRCClient.prototype.quit = function (message) {
+ this.send_command("QUIT :%s", [ message ]);
+};
+
zIRCClient.prototype.parse_message = function (msg) {
var args = [];
if (!msg) {
View
@@ -46,48 +46,58 @@ zirc_client.on("ready", function() {
switch (command) {
case "quit":
var message = text.substring(text.indexOf(" ") + 1);
- zirc_client.emit("quit", message);
+ zirc_client.quit(message);
process.exit(1);
break;
case "whois":
var args = text.substring(text.indexOf(" ") + 1);
args = args.split(" ");
- zirc_client.emit("whois", args[0], args[1]);
+ zirc_client.whois(args[0], args[1]);
break;
case "away":
var message = text.substring(text.indexOf(" ") + 1);
- zirc_client.emit("away", message);
+ zirc_client.away(message);
break;
case "msg":
var args = text.substring(text.indexOf(" ") + 1);
args = args.split(" ");
- zirc_client.emit("msg", args.shift(), util.format(args.toString()));
+ zirc_client.msg(args.shift(), util.format(args.toString()));
break;
case "notice":
var args = text.substring(text.indexOf(" ") + 1);
args = args.split(" ");
- zirc_client.emit("notice", args.shift(), util.format(args.toString()));
+ zirc_client.notice(args.shift(), util.format(args.toString()));
break;
case "back":
- zirc_client.emit("back");
+ zirc_client.back();
break;
case "join":
var args = text.substring(text.indexOf(" ") + 1);
args = args.split(" ");
- zirc_client.emit("join", args[0]);
+ zirc_client.join(args[0]);
break;
+ case "setNick":
+ var args = text.substring(text.indexOf(" ") + 1);
+ args = args.split(" ");
+ zirc_client.setNick(args[0]);
+ break;
case "leave":
var args = text.substring(text.indexOf(" ") + 1);
args = args.split(" ");
- zirc_client.emit("leave", args.shift(), util.format(args.toString()));
+ zirc_client.leave(args.shift(), util.format(args.join(" ")));
+ break;
+ case "topic":
+ var args = text.substring(text.indexOf(" ") + 1);
+ args = args.split(" ");
+ zirc_client.topic(args.shift(), util.format(args.join(" ")));
break;
default:
console.log("That command doesn't exist!");
break;
}
return true;
}
- zirc_client.emit("say", "lolzipp", text);
+ zirc_client.say("#lolzipp", text);
});
});

0 comments on commit 1d77419

Please sign in to comment.