Re-implemented all irc-events

This commit is contained in:
Mattias Erming 2014-06-29 21:41:02 +02:00
parent abd9099018
commit 4ef13d6a18
20 changed files with 459 additions and 23 deletions

View File

@ -74,7 +74,7 @@ button {
padding: 6px 10px 8px;
position: relative;
text-align: left;
transition: background .1s, color 5s;
transition: all .2s;
width: 160px;
}
#channels .chan:first-child {

View File

@ -19,3 +19,7 @@ function Chan(attr) {
users: []
}));
}
Chan.prototype.sortUsers = function() {
// ..
};

View File

@ -1,4 +1,5 @@
var _ = require("lodash");
var Network = require("./network");
module.exports = Client;
@ -8,12 +9,14 @@ function Client(attr) {
_.merge(this, _.extend({
id: id++,
networks: [],
nick: "",
keepAlive: false,
sockets: null
}, attr));
}
Client.prototype.emit = function(event, data) {
if (this.sockets != null) {
if (this.sockets !== null) {
this.sockets.in(this.id).emit(event, data);
}
};

View File

@ -2,6 +2,7 @@ var _ = require("lodash");
var moment = require("moment");
Msg.Type = {
ACTION: "action",
ERROR: "error",
JOIN: "join",
KICK: "kick",

View File

@ -1,4 +1,5 @@
var _ = require("lodash");
var Chan = require("./chan");
module.exports = Network;
@ -8,13 +9,20 @@ function Network(attr) {
_.merge(this, _.extend({
id: id++,
connected: false,
slate: null,
host: "",
name: capitalize(this.host.split(".")[1]) || this.host,
irc: null,
name: capitalize(attr.host.split(".")[1]) || attr.host,
channels: []
}, attr));
this.channels.unshift(
new Chan({name: this.name, type: Chan.Type.LOBBY})
);
}
Network.prototype.toJSON = function() {
return _.omit(this, "irc");
};
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}

View File

@ -1,2 +1,21 @@
module.exports = function() {
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("errors", function(data) {
var msg = new Msg({
type: Msg.Type.ERROR,
from: "*",
text: data.message,
});
client.emit("msg", {
msg: msg
});
if (!network.connected) {
if (data.cmd == "ERR_NICKNAMEINUSE") {
var random = client.nick + Math.floor(10 + (Math.random() * 89));
irc.nick(random);
}
}
});
};

View File

@ -1,2 +1,36 @@
module.exports = function() {
var _ = require("lodash");
var Msg = require("../../models/msg");
var User = require("../../models/user");
module.exports = function(irc, network) {
var client = this;
irc.on("join", function(data) {
var chan = _.find(network.channels, {name: data.channel});
if (typeof chan === "undefined") {
chan = new Chan({
name: data.channel
});
network.channels.push(chan);
client.emit("join", {
network: network.id,
chan: chan
});
}
var users = chan.users;
users.push(new User({name: data.nick}));
chan.sortUsers();
client.emit("users", {
chan: chan.id,
users: users
});
var msg = new Msg({
from: data.nick,
type: Msg.Type.JOIN
});
chan.messages.push(msg);
client.emit("msg", {
id: chan.id,
msg: msg
});
});
};

View File

@ -1,2 +1,31 @@
module.exports = function() {
var _ = require("lodash");
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("kick", function(data) {
var chan = _.findWhere(network.channels, {name: data.channel});
if (typeof chan === "undefined") {
return;
}
if (data.client == irc.me) {
chan.users = [];
} else {
chan.users = _.without(chan.users, _.findWhere(chan.users, {name: data.client}));
}
client.emit("users", {
chan: chan.id,
users: chan.users
});
var msg = new Msg({
type: Msg.Type.KICK,
from: data.nick,
text: data.client
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
});
};

View File

@ -1,2 +1,41 @@
module.exports = function() {
var _ = require("lodash");
var Chan = require("../../models/chan");
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("message", function(data) {
var target = data.to;
var chan = _.findWhere(network.channels, {name: target.charAt(0) == "#" ? target : data.from});
if (typeof chan === "undefined") {
chan = new Chan({
type: Chan.Type.QUERY,
name: data.from
});
network.channels.push(chan);
client.emit("join", {
network: network.id,
chan: chan
});
}
var type = "";
var text = data.message;
if (text.split(" ")[0] === "\u0001ACTION") {
type = Msg.Type.ACTION;
text = text.replace(/\u0001|ACTION/g, "");
}
text.split(" ").forEach(function(w) {
if (w.indexOf(irc.me) === 0) type += " highlight";
});
var msg = new Msg({
type: type || Msg.Type.MESSAGE,
from: data.from,
text: text
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
});
};

View File

@ -1,2 +1,27 @@
module.exports = function() {
var _ = require("lodash");
module.exports = function(irc, network) {
var client = this;
irc.on("mode", function(data) {
var chan = _.findWhere(network.channels, {name: data.target});
if (typeof chan !== "undefined") {
setTimeout(function() {
irc.write("NAMES " + data.target);
}, 200);
var nick = data.nick;
if (nick.indexOf(".") !== -1) {
nick = data.target;
}
var msg = new Msg({
type: Msg.Type.MODE,
from: nick,
text: data.mode + " " + data.client,
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg,
});
}
});
};

View File

@ -1,2 +1,20 @@
module.exports = function() {
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("motd", function(data) {
var lobby = network.channels[0];
data.motd.forEach(function(text) {
var msg = new Msg({
type: Msg.Type.MOTD,
from: "*",
text: text
});
lobby.messages.push(msg);
client.emit("msg", {
chan: lobby.id,
msg: msg
});
});
});
};

View File

@ -1,2 +1,21 @@
module.exports = function() {
var _ = require("lodash");
var User = require("../../models/user");
module.exports = function(irc, network) {
var client = this;
irc.on("names", function(data) {
var chan = _.findWhere(network.channels, {name: data.channel});
if (typeof chan === "undefined") {
return;
}
chan.users = [];
_.each(data.names, function(u) {
chan.users.push(new User(u));
});
chan.sortUsers();
client.emit("users", {
chan: chan.id,
users: chan.users
});
});
};

View File

@ -1,2 +1,42 @@
module.exports = function() {
var _ = require("lodash");
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("nick", function(data) {
if (data["new"] == irc.me) {
var lobby = network.channels[0];
var msg = new Msg({
from: "*",
text: "You're now known as " + data["new"],
});
chan.messages.push(msg);
client.emit("msg", {
chan: lobby.id,
msg: msg
});
}
network.channels.forEach(function(chan) {
var user = _.findWhere(chan.users, {name: data.nick});
if (typeof user === "undefined") {
return;
}
user.name = data["new"];
chan.sortUsers();
client.emit("users", {
chan: chan.id,
users: chan.users
});
var msg = new Msg({
type: Msg.Type.NICK,
from: data.nick,
text: data["new"]
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
});
});
};

View File

@ -1,2 +1,22 @@
module.exports = function() {
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("notice", function(data) {
var lobby = network.channels[0];
var from = data.from || "*";
if (data.to == "*" || data.from.indexOf(".") !== -1) {
from = "*";
}
var msg = new Msg({
type: Msg.Type.NOTICE,
from: from,
text: data.message
});
lobby.messages.push(msg);
client.emit("msg", {
chan: lobby.id,
msg: msg
});
});
};

View File

@ -1,2 +1,34 @@
module.exports = function() {
var _ = require("lodash");
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("part", function(data) {
var chan = _.findWhere(network.channels, {name: data.channels[0]});
if (typeof chan === "undefined") {
return;
}
if (data.nick == irc.me) {
network.channels = _.without(network.channels, chan);
client.emit("part", {
chan: chan.id
});
} else {
var user = _.findWhere(chan.users, {name: data.nick});
chan.users = _.without(chan.users, user);
client.emit("users", {
chan: chan.id,
users: chan.users
});
var msg = new Msg({
type: Msg.Type.PART,
from: data.nick
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
}
});
};

View File

@ -1,2 +1,28 @@
module.exports = function() {
var _ = require("lodash");
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("quit", function(data) {
network.channels.forEach(function(chan) {
var user = _.findWhere(chan.users, {name: data.nick});
if (typeof user === "undefined") {
return;
}
chan.users = _.without(chan.users, user);
client.emit("users", {
chan: chan.id,
users: chan.users
});
var msg = new Msg({
type: Msg.Type.QUIT,
from: data.nick
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
});
});
};

View File

@ -1,2 +1,23 @@
module.exports = function() {
var _ = require("lodash");
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("topic", function(data) {
var chan = _.findWhere(network.channels, {name: data.channel});
if (typeof chan === "undefined") {
return;
}
var from = data.nick || chan.name;
var msg = new Msg({
type: Msg.Type.TOPIC,
from: from,
text: data.topic,
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
});
};

View File

@ -1,2 +1,19 @@
module.exports = function() {
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("welcome", function(data) {
network.connected = true;
irc.write("PING " + network.host);
var lobby = network.channels[0];
var msg = new Msg({
from: "*",
text: "You're now known as " + data
});
lobby.messages.push(msg);
client.emit("msg", {
chan: lobby.id,
msg: msg
});
});
};

View File

@ -1,2 +1,47 @@
module.exports = function() {
var _ = require("lodash");
var Chan = require("../../models/chan");
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("whois", function(data) {
if (!data) {
return;
}
var chan = _.findWhere(network.channels, {name: data.nickname});
if (typeof chan === "undefined") {
chan = new Chan({
type: Chan.Type.QUERY,
name: data.nickname
});
network.channels.push(chan);
client.emit("join", {
network: network.id,
chan: chan
});
}
var prefix = {
hostname: "from",
realname: "is",
channels: "on",
server: "using"
};
var i = 0;
for (var k in data) {
var key = prefix[k];
if (!key || data[k].toString() == "") {
continue;
}
var msg = new Msg({
type: Msg.Type.WHOIS,
from: data.nickname,
text: key + " " + data[k]
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
}
});
};

View File

@ -2,7 +2,11 @@ var _ = require("lodash");
var Client = require("./models/client");
var config = require("../config") || {};
var http = require("connect");
var net = require("net");
var Network = require("./models/network");
var io = require("socket.io");
var slate = require("slate-irc");
var tls = require("tls");
var sockets = null;
var clients = [];
@ -45,7 +49,9 @@ module.exports = function() {
sockets = io(http().use(http.static("client")).listen(config.port || 9000));
sockets.on("connection", function(socket) {
if (config.public) {
init(socket, new Client());
var client = new Client({sockets: sockets});
init(socket, client);
connect(client, {host: "irc.rizon.net"});
} else {
init(socket);
}
@ -60,11 +66,41 @@ function init(socket, client) {
socket.on("input", function(data) { input(client, data); });
socket.join(client.id);
socket.emit("init", {
networks: client.networks
init: client.networks
});
}
};
}
function auth() {
var socket = this;
};
}
function connect(client, args) {
var options = {
host: args.host,
port: args.port || 6667
};
var stream = args.tls ? tls.connect(options) : net.connect(options);
stream.on("error", function(e) {
console.log(e);
});
var irc = slate(stream);
irc.nick("shout");
irc.user("shout", "Shout User");
client.nick = "shout";
var network = new Network({
host: options.host,
irc: irc
});
client.networks.push(network);
client.emit("network", {
network: network
});
events.forEach(function(plugin) {
require("./plugins/irc-events/" + plugin).apply(client, [irc, network]);
});
}
function input(client, data) {
console.log(data);
}