Refactor chat commands [untested]

This commit is contained in:
calzoneman 2013-03-05 12:51:58 -06:00
parent ab6fc96517
commit ab48c6aa50
2 changed files with 23 additions and 16 deletions

View File

@ -12,6 +12,7 @@ var Rank = require('./rank.js');
// I should use the <noun><verb>er naming scheme more often // I should use the <noun><verb>er naming scheme more often
var InfoGetter = require('./get-info.js'); var InfoGetter = require('./get-info.js');
var Media = require('./media.js').Media; var Media = require('./media.js').Media;
var ChatCommand = require('./chatcommand.js');
var Channel = function(name) { var Channel = function(name) {
console.log("Opening channel " + name); console.log("Opening channel " + name);
@ -387,37 +388,34 @@ Channel.prototype.moveMedia = function(data) {
// Chat message from a user // Chat message from a user
Channel.prototype.chatMessage = function(user, msg) { Channel.prototype.chatMessage = function(user, msg) {
// Temporary code if(msg.indexOf("/") == 0)
// When I add more modifiers I should store them in a table ChatCommand.handle(msg);
// of some kind
var msgclass = "";
if(msg.indexOf("/me ") == 0) {
msgclass = "action";
msg = msg.substring(3);
}
else if(msg.indexOf("/sp ") == 0) {
msgclass = "spoiler";
msg = msg.substring(3);
}
else if(msg.indexOf(">") == 0) else if(msg.indexOf(">") == 0)
msgclass = "greentext"; this.sendMessage(user.name, msg, "greentext");
else
this.sendMessage(user.name, msg, msgclass);
}
Channel.prototype.sendMessage = function(username, msg, msgclass) {
// I don't want HTML from strangers // I don't want HTML from strangers
msg = msg.replace(/</g, "&lt;").replace(/>/g, "&gt;"); msg = msg.replace(/</g, "&lt;").replace(/>/g, "&gt;");
// Match URLs // Match URLs
msg = msg.replace(/(((https?)|(ftp))(:\/\/[0-9a-zA-Z\.]+(:[0-9]+)?[^\s$]+))/, "<a href=\"$1\">$1</a>"); msg = msg.replace(/(((https?)|(ftp))(:\/\/[0-9a-zA-Z\.]+(:[0-9]+)?[^\s$]+))/, "<a href=\"$1\">$1</a>");
this.sendAll('chatMsg', { this.sendAll('chatMsg', {
username: user.name, username: username,
msg: msg, msg: msg,
msgclass: msgclass msgclass: msgclass
}); });
this.recentChat.push({ this.recentChat.push({
username: user.name, username: username,
msg: msg, msg: msg,
msgclass: msgclass msgclass: msgclass
}); });
if(this.recentChat.length > 15) if(this.recentChat.length > 15)
this.recentChat.shift(); this.recentChat.shift();
} };
// Promotion! Actor is the client who initiated the promotion, name is the // Promotion! Actor is the client who initiated the promotion, name is the
// name of the person being promoted // name of the person being promoted

9
chatcommand.js Normal file
View File

@ -0,0 +1,9 @@
function handle(chan, user, msg) {
if(msg.indexOf("/me ") == 0)
chan.sendMessage(user.name, msg.substring(4), "action");
else if(msg.indexOf("/sp ") == 0)
chan.sendMessage(user.name, msg.substring(4), "spoiler");
}
exports.handle = handle;