Permalink
Browse files

Add channel settings and talkback command

  • Loading branch information...
1 parent e42c5bd commit 167385a2fa730c94fd5417736288e563c081487d @rehandalal rehandalal committed Sep 26, 2012
Showing with 105 additions and 17 deletions.
  1. +19 −0 migrations/3.js
  2. +60 −16 standup-irc.js
  3. +26 −1 utils.js
View
@@ -0,0 +1,19 @@
+exports.upgrade = function(client) {
+ client.query("CREATE TABLE channel_settings (id bigint NOT NULL, " +
+ "channel character varying(255), " +
+ "name character varying(255), " +
+ "value character varying(255));");
+ client.query("CREATE SEQUENCE channel_settings_id_seq " +
+ "START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;");
+ client.query("ALTER SEQUENCE channel_settings_id_seq " +
+ "OWNED BY channel_settings.id;");
+ client.query("ALTER TABLE ONLY channel_settings ALTER COLUMN id " +
+ "SET DEFAULT nextval('channel_settings_id_seq'::regclass);");
+ client.query("ALTER TABLE ONLY channel_settings ADD CONSTRAINT " +
+ "channel_settings_pkey PRIMARY KEY (id);");
+}
+
+exports.downgrade = function(client) {
+ client.query("DROP channel_settings");
+ client.query("DROP SEQUENCE channel_settings_id_seq");
+}
View
@@ -27,6 +27,8 @@ var defaults = {
blacklist: []
};
+
+
var existsSync = fs.existsSync || path.existsSync;
// Global config.
@@ -101,6 +103,12 @@ irc_client.on('registered', function(message) {
query.on('row', function(row) {
irc_client.join(row.id);
});
+
+ var query = pg_client.query("SELECT channel, name, value FROM channel_settings");
+
+ query.on('row', function(row) {
+ utils.channelSetting(row.channel, row.name, row.value);
+ });
}
});
@@ -227,10 +235,10 @@ var commands = {
}
} else {
if (config.blacklist.length > 0) {
- irc_client.say(channel, 'Blacklisted users:');
- irc_client.say(channel, config.blacklist.join(', '));
+ utils.talkback(channel, user, 'Blacklisted users:');
+ utils.talkback(channel, user, config.blacklist.join(', '));
} else {
- irc_client.say(channel, 'No blacklisted users.');
+ utils.talkback(channel, user, 'No blacklisted users.');
}
}
});
@@ -269,8 +277,8 @@ var commands = {
'chanlist': {
help: "Get a list of channels that I am in.",
func: function(user, channel) {
- irc_client.say(channel, "I'm currently in:");
- irc_client.say(channel, _.keys(irc_client.chans).sort().join(', '));
+ utils.talkback(channel, user, "I'm currently in:");
+ utils.talkback(channel, user, _.keys(irc_client.chans).sort().join(', '));
}
},
@@ -286,28 +294,28 @@ var commands = {
}
id = parseInt(id, 10);
if (isNaN(id)) {
- irc_client.say(channel, '"' + args[0] + '" ' +
+ utils.talkback(channel, user, '"' + args[0] + '" ' +
'is not a valid status ID.');
return;
}
var response = api.status.delete(id, user);
response.once('ok', function(data) {
- irc_client.say(channel, 'Ok, status #' + id + ' is no more!');
+ utils.talkback(channel, user, 'Ok, status #' + id + ' is no more!');
});
response.once('error', function(code, data) {
data = JSON.parse(data);
if (code === 403) {
- irc_client.say(channel, "You don't have permission " +
+ utils.talkback(channel, user, "You don't have permission " +
" to do that. Did you post that status?");
} else {
var error = "I'm a failure, I couldn't do it.";
if (data.error) {
error += ' The server said: "' + data.error + '"';
}
- irc_client.say(channel, error);
+ utils.talkback(channel, user, error);
}
});
});
@@ -351,7 +359,7 @@ var commands = {
func: function(user, channel) {
var command, help, usage;
- irc_client.say(channel, 'Available commands:');
+ irc_client.say(user, 'Available commands:');
_.each(_.keys(commands).sort(), function(command) {
help = commands[command].help;
@@ -366,7 +374,7 @@ var commands = {
message.push('- ' + help);
- irc_client.say(channel, message.join(' '));
+ irc_client.say(user, message.join(' '));
}
});
}
@@ -395,16 +403,52 @@ var commands = {
var response = api.status.create(user, project, status);
response.once('ok', function(data) {
- irc_client.say(channel, 'Ok, submitted status #' + data.id);
+ utils.talkback(channel, user, 'Ok, submitted status #' + data.id);
});
response.once('error', function(err, data) {
- irc_client.say(channel, 'Uh oh, something went wrong.');
+ utils.talkback(channel, user, 'Uh oh, something went wrong.');
});
});
}
},
+ /* Get/set the channel talkback setting */
+ 'talkback': {
+ help: "Per channel setting to determine if the bot should PM responses.",
+ usage: "[<loud|quiet>]",
+ func: function(user, channel, message, args) {
+ if (!args[0]) {
+ var msg = 'Talkback is currently: ' +
+ irc_client.say(channel, utils.channelSetting(channel, 'talkback'));
+ } else if ((args[0] === 'loud') || (args[0] === 'quiet')) {
+ if (args[0] === 'loud') {
+ irc_client.say(channel, 'I will respond in the channel from now on.');
+ } else {
+ irc_client.say(channel, 'I will only PM users now.');
+ }
+
+ utils.channelSetting(channel, 'talkback', args[0]);
+
+ var query = pg_client.query({
+ text: "DELETE FROM channel_settings WHERE " +
+ "channel=$1 AND name='talkback'",
+ values: [channel]
+ });
+
+ query.on('end', function() {
+ pg_client.query({
+ text: "INSERT INTO channel_settings(channel, " +
+ "name, value) values($1, 'talkback', $2)",
+ values: [channel, args[0]]
+ });
+ });
+ } else {
+ irc_client.say(channel, 'Does not compute!');
+ }
+ }
+ },
+
/* Check a user's authorization status. */
'trust': {
help: "Check a user's authorization status.",
@@ -413,9 +457,9 @@ var commands = {
var a = authman.checkUser(args);
a.once('authorization', function(trust) {
if (trust) {
- irc_client.say(channel, 'I trust ' + args);
+ utils.talkback(channel, user, 'I trust ' + args);
} else {
- irc_client.say(channel, "I don't trust " + args);
+ utils.talkback(channel, user, "I don't trust " + args);
}
});
}
@@ -470,7 +514,7 @@ var commands = {
if (nick) {
config.blacklist = _.without(config.blacklist, nick);
pg_client.query("DELETE FROM blacklist WHERE id='" + nick + "'");
- irc_client.say('Will do!');
+ utils.talkback(channel, user, 'Will do!');
}
});
}
View
@@ -2,6 +2,9 @@ var _ = require('underscore');
var http = require('http');
var events = require('events');
+// Channel settings
+var channel_settings = {};
+
var request = function(path, method, data, emitter, unicode) {
if (data === undefined) {
data = {};
@@ -52,14 +55,28 @@ var request = function(path, method, data, emitter, unicode) {
exports.request = request;
+var channelSetting = function(channel, name, value) {
+ if (channel_settings[channel] === undefined) {
+ channel_settings[channel] = {};
+ }
+
+ if (value === undefined) {
+ return channel_settings[channel][name];
+ } else {
+ channel_settings[channel][name] = value;
+ }
+}
+
+exports.channelSetting = channelSetting;
+
exports.ifAuthorized = function(user, channel, callback) {
var a = authman.checkUser(user);
a.once('authorization', function(trust) {
if (trust) {
callback();
} else {
- client.say(channel, "I don't trust you, " + user + ", " +
+ irc_client.say(channel, "I don't trust you, " + user + ", " +
"are you identified with nickserv?");
}
});
@@ -106,6 +123,14 @@ exports.parseArgs = function(argList) {
return args;
};
+exports.talkback = function(channel, user, message) {
+ if (channelSetting(channel, 'talkback') === 'quiet') {
+ channel = user;
+ }
+
+ irc_client.say(channel, message);
+}
+
exports.jsonStringifyUnicode = function(str, emitUnicode) {
var json = JSON.stringify(str);
if (!emitUnicode) {

0 comments on commit 167385a

Please sign in to comment.