Improve nick highlights

This commit is contained in:
Pavel Djundik 2016-05-12 14:15:38 +03:00
parent bbf7b8086f
commit aa8c3f6419
5 changed files with 31 additions and 7 deletions

View file

@ -144,6 +144,7 @@ Client.prototype.connect = function(args) {
ip: args.ip,
hostname: args.hostname,
});
network.setNick(nick);
client.networks.push(network);
client.emit("network", {

View file

@ -33,13 +33,34 @@ function Network(attr) {
);
}
Network.prototype.setNick = function(nick) {
this.nick = nick;
this.highlightRegex = new RegExp(
// Do not match characters and numbers (unless IRC color)
"(?:^|[^a-z0-9]|\x03[0-9]{1,2})" +
// Escape nickname, as it may contain regex stuff
nick.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&") +
// Do not match characters and numbers
"(?:[^a-z0-9]|$)",
// Case insensitive search
"i"
);
};
Network.prototype.toJSON = function() {
var json = _.extend(this, {nick: (this.irc && this.irc.user.nick) || ""});
return _.omit(json, "irc", "password");
return _.omit(this, [
"irc",
"password",
"highlightRegex"
]);
};
Network.prototype.export = function() {
var network = _.pick(this, [
"nick",
"name",
"host",
"port",
@ -51,11 +72,12 @@ Network.prototype.export = function() {
"ip",
"hostname"
]);
network.nick = (this.irc && this.irc.user.nick) || "";
network.join = _.map(
_.filter(this.channels, {type: "channel"}),
"name"
).join(",");
return network;
};

View file

@ -64,9 +64,7 @@ module.exports = function(irc, network) {
// Self messages are never highlighted
// Non-self messages are highlighted as soon as the nick is detected
if (!highlight && !self) {
highlight = data.message.split(" ").some(function(w) {
return (w.replace(/^@/, "").toLowerCase().indexOf(irc.user.nick.toLowerCase()) === 0);
});
highlight = network.highlightRegex.test(data.message);
}
if (!self && chan.id !== client.activeChannel) {

View file

@ -6,6 +6,8 @@ module.exports = function(irc, network) {
irc.on("nick", function(data) {
var self = false;
if (data.nick === irc.user.nick) {
network.setNick(data.new_nick);
var lobby = network.channels[0];
var msg = new Msg({
text: "You're now known as " + data.new_nick,

View file

@ -3,7 +3,8 @@ var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("registered", function(data) {
network.nick = data.nick;
network.setNick(data.nick);
var lobby = network.channels[0];
var msg = new Msg({
text: "You're now known as " + data.nick