Permalink
Browse files

implemented lunch and lunches command

  • Loading branch information...
1 parent 85aa437 commit f73b387e120ad4e91b33d9397610e6188a012741 @nikhilm committed Jul 4, 2011
Showing with 67 additions and 5 deletions.
  1. +3 −3 README.md
  2. +64 −2 lunchbot.js
View
@@ -30,9 +30,9 @@ or exclusion.
eg. lunch at 12:30 at Le Boulanger
Options:
-
-* +invite <names> - automatically ping all <names> (space separated nicks) at that time
-* +exclusive - no one else can sign up for this lunch and it won't show up when anyone not in +invite runs command (5)
+[NOT implemented, needs some thinking]
+* --invite=<names> - automatically ping all <names> (space separated nicks) at that time
+* --exclusive - no one else can sign up for this lunch and it won't show up when anyone not in --invite runs command (5)
5) view lunch times
- lunches
View
@@ -13,10 +13,38 @@ var CHAN = '#lunchbot';
var conn = new irc({ 'server': SERV, 'nick': NICK });
+var LunchTime = function(time) {
+ this._time = time;
+ this._list = [];
+}
+
+LunchTime.prototype.add = function(by, message) {
+ this._list.push({by: by, message: message});
+}
+
+LunchTime.prototype.signup = function(with_) {
+}
+
+LunchTime.prototype.notify = function() {
+}
+
+LunchTime.prototype.toString = function() {
+ var str = '';
+ for (var i = 0; i < this._list.length; i++)
+ str += this._time + ' ' + this._list[i].by + ': ' + this._list[i].message + '\n';
+ return str;
+}
+
+var lunches = {};
+
+var sanitizeNick = function(nick) {
+ return nick.replace(/^[%@]/, '');
+}
+
var sanitizeNickList = function(nicks) {
var clean = [];
for (var i = 0; i < nicks.length; i++)
- clean.push(nicks[i].replace(/^[%@]/, ''));
+ clean.push(sanitizeNick(nicks[i]));
return clean;
}
@@ -26,10 +54,14 @@ ask: cmd.use({
conn.names(CHAN, function(channel, nicks) {
var ops = [];
for (var i = 0; i < nicks.length; i++) {
+ console.log(nicks[i]);
if (nicks[i].indexOf('@') == 0)
ops.push(nicks[i]);
}
+ if (ops.length == 0)
+ return;
+
conn.privmsg(CHAN, sanitizeNickList(ops).join(', ') + ': lunch?');
});
},
@@ -38,11 +70,38 @@ ask: cmd.use({
conn.privmsg(CHAN, sanitizeNickList(nicks).join(', ') + ': lunch?');
});
}
-})
+}),
+
+lunch: cmd.use({
+ 'at': function(command) {
+ var args = command.unshifted();
+ var time = args[0];
+ var match = time.match(/^(0?[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9])$/);
+ if (!match)
+ conn.privmsg(command.options.channel, sanitizeNick(command.options.nick) + ': invalid time');
+
+ if (!lunches[time])
+ lunches[time] = new LunchTime(time);
+ lunches[time].add(command.options.nick, args.slice(1).join(' '));
+ }
+}),
+
+lunches: function(command) {
+ var lunchesStr = '';
+ for (var time in lunches) {
+ if (!lunches.hasOwnProperty(time))
+ continue;
+ var lunchtime = lunches[time];
+ lunchesStr += lunchtime;
+ }
+ conn.privmsg(command.options.channel, lunchesStr);
+}
};
var dispatcher = cmd.use({
'ask': commands.ask,
+ 'lunch': commands.lunch,
+ 'lunches': commands.lunches,
_unhandled: function(cmd) {
console.log("don't understand", cmd);
}
@@ -51,10 +110,13 @@ var dispatcher = cmd.use({
var onConnect = function() {
conn.join(CHAN);
conn.on('privmsg', function(args) {
+ console.dir(args);
var message = args['params'][1];
var regex = new RegExp(NICK + "[:]?\\s*");
if (message.match(regex)) {
var arr = message.replace(regex, '').split(' ');
+ arr.push('--nick=' + args.person.nick);
+ arr.push('--channel=' + args.params[0]);
dispatcher.apply(dispatcher, arr);
}
});

0 comments on commit f73b387

Please sign in to comment.