Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More cleanup and functionality

Cleaned up the code some more and added in 'chanlist' and 'goto'
commands.
  • Loading branch information...
commit c71fecbc4cadc80379aae2ade28733062f6cd02b 1 parent f31baa1
@rehandalal rehandalal authored
View
2  auth.js
@@ -54,4 +54,4 @@ AuthManager.prototype._askNickserv = function(nick) {
client.say('nickserv', msg);
};
-exports.AuthManager = AuthManager;
+exports.AuthManager = AuthManager;
View
2  config-sample.json
@@ -13,4 +13,4 @@
"console": true,
"file": null
}
-}
+}
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "standup-irc",
"description": "IRC bot for recording status.",
- "version": "0.0.1",
+ "version": "0.1.0",
"author": {
"name": "Mike Cooper",
"email": "mythmon@mozilla.com"
View
246 standup-irc.js
@@ -82,8 +82,7 @@ client.on('error', function(error) {
*/
client.on('invite', function(channel, from) {
logger.info('Invited to ' + channel + ' by ' + from + '.');
- client.join(channel);
- config.irc.channels.push(channel);
+ commands.goto.func(from, channel, '', [channel]);
});
/* The bot gets kicked out of a channel
@@ -149,37 +148,48 @@ client.on('notice', function(from, to, text) {
});
var commands = {
- /* Simple presence check. */
- ping: {
- help: "A simple presence check.",
- usage: undefined,
+ /* Post a message in all channels */
+ 'announce': {
+ help: "Broadcast a message in all other channels.",
+ usage: "<message>",
func: function(user, channel, message, args) {
- client.say(channel, "Pong!");
+ _.each(client.chans, function(data, chan) {
+ if (chan !== channel) {
+ client.say(chan, args.join(' '));
+ }
+ });
}
},
- /* Create a status. */
- status: {
- help: undefined, // Don't tell the user about this.
- usage: "<project> status message",
+ /* Every bot loves botsnacks. */
+ 'botsnack': {
func: function(user, channel, message, args) {
- utils.ifAuthorized(user, channel, function() {
- var project = args[0];
- if (project[0] === '#') {
- project = project.slice(1);
- }
-
- var status = args.slice(1).join(' ');
- var response = api.status.create(user, project, status);
+ var replies = [
+ 'Yummy!',
+ 'Thanks, ' + user + '!',
+ 'My favorite!',
+ 'Can I have another?',
+ 'Tasty!'
+ ];
+ client.say(channel, _.shuffle(replies)[0]);
+ }
+ },
- response.once('ok', function(data) {
- client.say(channel, 'Ok, submitted status #' + data.id);
- });
+ /* Leave the channel */
+ 'bye': {
+ help: "Ask the bot to leave the channel.",
+ func: function(user, channel) {
+ client.say(channel, 'Bye!');
+ client.part(channel);
+ }
+ },
- response.once('error', function(err, data) {
- client.say(channel, 'Uh oh, something went wrong.');
- });
- });
+ /* List all channels */
+ 'chanlist': {
+ help: "Get a list of channels that I am in.",
+ func: function(user, channel) {
+ client.say(channel, "I'm currently in:");
+ client.say(channel, _.keys(client.chans).sort().join(', '));
}
},
@@ -196,7 +206,7 @@ var commands = {
id = parseInt(id, 10);
if (isNaN(id)) {
client.say(channel, '"' + args[0] + '" ' +
- 'is not a valid status ID.');
+ 'is not a valid status ID.');
return;
}
@@ -210,7 +220,7 @@ var commands = {
data = JSON.parse(data);
if (code === 403) {
client.say(channel, "You don't have permission to do " +
- "that. Did you post that status?");
+ "that. Did you post that status?");
} else {
var error = "I'm a failure, I couldn't do it.";
if (data.error) {
@@ -223,42 +233,20 @@ var commands = {
}
},
- /* Every bot loves botsnacks. */
- 'botsnack': {
+ /* Tell the bot to join a channel */
+ 'goto': {
+ help: 'Tell the bot to join a channel.reg ',
+ usage: '<channel>',
func: function(user, channel, message, args) {
- var replies = [
- 'Yummy!',
- 'Thanks, ' + user + '!',
- 'My favorite!',
- 'Can I have another?',
- 'Tasty!'
- ];
- client.say(channel, _.shuffle(replies)[0]);
- }
- },
+ var join = args[0];
- /* Check a user's authorization status. */
- 'trust': {
- help: "Check a user's authorization status.",
- usage: "<user>",
- func: function(user, channel, message, args) {
- var a = authman.checkUser(args);
- a.once('authorization', function(trust) {
- if (trust) {
- client.say(channel, 'I trust ' + args);
- } else {
- client.say(channel, "I don't trust " + args);
- }
- });
- }
- },
+ if (join[0] !== '#') {
+ join = '#' + join;
+ }
- /* Leave the channel */
- 'bye': {
- help: "Ask the bot to leave the channel.",
- func: function(user, channel) {
- client.say(channel, 'Bye!');
- client.part(channel);
+ if (join) {
+ client.join(join);
+ }
}
},
@@ -272,77 +260,121 @@ var commands = {
'help': {
help: "This help message.",
func: function(user, channel) {
- var command;
- var help;
- var usage;
+ var command, help, usage;
- say('Available commands:');
+ client.say(channel, 'Available commands:');
- for (command in commands) {
+ _.each(_.keys(commands).sort(), function(command) {
help = commands[command].help;
usage = commands[command].usage;
if (help !== undefined) {
- if (usage === undefined) {
- client.say(channel, '!' + command + ' - ' + help);
- } else {
- client.say(channel, '!' + command + ' ' + usage + ' - ' + help);
+ var message = ['!' + command];
+
+ if (usage !== undefined) {
+ message.push(usage)
}
+
+ message.push('- ' + help);
+
+ client.say(channel, message.join(' '));
}
- }
+ });
}
},
- /* Update a user's settings */
- 'update': function(user, channel, message, args) {
- utils.ifAuthorized(user, channel, function() {
- var what = args[0];
- var value = args[1];
- var who = args[2];
-
+ /* Simple presence check. */
+ 'ping': {
+ help: "A simple presence check.",
+ usage: undefined,
+ func: function(user, channel, message, args) {
+ client.say(channel, "Pong!");
+ }
+ },
- if (who === undefined) {
- who = user;
- }
+ /* Create a status. */
+ 'status': {
+ usage: "<project> status message",
+ func: function(user, channel, message, args) {
+ utils.ifAuthorized(user, channel, function() {
+ var project = args[0];
+ if (project[0] === '#') {
+ project = project.slice(1);
+ }
- if (what && value) {
- var response = api.user.update(user, what, value, who);
+ var status = args.slice(1).join(' ');
+ var response = api.status.create(user, project, status);
response.once('ok', function(data) {
- client.action(channel, "updates some stuff!");
+ client.say(channel, 'Ok, submitted status #' + data.id);
});
- response.once('error', function(code, data) {
- if (code === 403) {
- client.say(channel, "You don't have permission to do " +
- "that.");
- } else {
- var error = "I'm a failure, I couldn't do it.";
- if (data.error) {
- error += ' The server said: "' + data.error + '"';
- }
- client.say(channel, error);
- }
+ response.once('error', function(err, data) {
+ client.say(channel, 'Uh oh, something went wrong.');
});
- }
- });
+ });
+ }
},
- /* Post a message in all channels */
- 'announce': function(user, channel, message, args) {
- _.each(config.irc.channels, function(chan) {
- if (chan !== channel) {
- client.say(chan, args.join(' '));
- }
- });
+ /* Check a user's authorization status. */
+ 'trust': {
+ help: "Check a user's authorization status.",
+ usage: "<user>",
+ func: function(user, channel, message, args) {
+ var a = authman.checkUser(args);
+ a.once('authorization', function(trust) {
+ if (trust) {
+ client.say(channel, 'I trust ' + args);
+ } else {
+ client.say(channel, "I don't trust " + args);
+ }
+ });
+ }
+ },
+
+ /* Update a user's settings */
+ 'update': {
+ help: "Update the user's settings.",
+ usage: "<name|email|github_handle> <value> [<user>]",
+ func: function(user, channel, message, args) {
+ utils.ifAuthorized(user, channel, function() {
+ var what = args[0];
+ var value = args[1];
+ var who = args[2];
+
+
+ if (!who) {
+ who = user;
+ }
+
+ if (what && value) {
+ var response = api.user.update(user, what, value, who);
+
+ response.once('ok', function(data) {
+ client.action(channel, "updates some stuff!");
+ });
+
+ response.once('error', function(code, data) {
+ if (code === 403) {
+ client.say(channel, "You don't have permission to do " +
+ "that.");
+ } else {
+ var error = "I'm a failure, I couldn't do it.";
+ if (data.error) {
+ error += ' The server said: "' + data.error + '"';
+ }
+ client.say(channel, error);
+ }
+ });
+ }
+ });
+ }
},
/* The default action. Return an error. */
'default': {
- help: undefined,
- args: undefined,
func: function(user, channel, message) {
- client.say(channel, user + ": Huh? Try !help.");
+ client.say(channel, user + ': Huh? Try !help.');
}
}
};
View
2  utils.js
@@ -69,4 +69,4 @@ exports.ifAuthorized = function(user, channel, callback) {
exports.escapeRegExp = function(str) {
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.