Added user login

This commit is contained in:
Mattias Erming 2014-07-18 18:31:00 -07:00
parent 5c10af457f
commit 73b995931b
8 changed files with 126 additions and 72 deletions

View File

@ -144,8 +144,10 @@ button {
color: #7c838d; color: #7c838d;
line-height: 1.6; line-height: 1.6;
font-size: 12px; font-size: 12px;
padding: 25px 40px 80px;
text-align: center; text-align: center;
width: 140px; position: absolute;
top: 0;
} }
#sidebar .empty:before { #sidebar .empty:before {
content: "\f085"; content: "\f085";

View File

@ -21,11 +21,10 @@
<div id="wrap"> <div id="wrap">
<div id="viewport"> <div id="viewport">
<aside id="sidebar"> <aside id="sidebar">
<div class="networks"> <div class="networks"></div>
<div class="empty"> <div class="empty">
You're not connected to any networks yet. You're not connected to any networks yet.
</div> </div>
</div>
<footer id="footer"> <footer id="footer">
<button class="sign-in" data-target="#sign-in" data-title="Sign in" data-placement="top" title="Sign in to Shout"></button> <button class="sign-in" data-target="#sign-in" data-title="Sign in" data-placement="top" title="Sign in to Shout"></button>
<button class="connect" data-target="#connect" data-title="Connect" data-placement="top" title="Connect to network"></button> <button class="connect" data-target="#connect" data-title="Connect" data-placement="top" title="Connect to network"></button>
@ -76,31 +75,31 @@
<div class="col-xs-8"> <div class="col-xs-8">
<label> <label>
Server Server
<input class="input" name="host" placeholder="<%=defaults.host%>"> <input class="input" name="host" placeholder="irc.freenode.org">
</label> </label>
</div> </div>
<div class="col-xs-4"> <div class="col-xs-4">
<label class="port"> <label class="port">
Port Port
<input class="input" name="port" placeholder="<%=defaults.port%>"> <input class="input" name="port" placeholder="6667">
</label> </label>
</div> </div>
<div class="col-xs-5"> <div class="col-xs-5">
<label> <label>
Nick Nick
<input class="input" name="nick" placeholder="<%=defaults.nick%>"> <input class="input" name="nick" placeholder="shout-user">
</label> </label>
</div> </div>
<div class="col-xs-7"> <div class="col-xs-7">
<label> <label>
Real Name Real Name
<input class="input" name="realname" class="input" placeholder="<%=defaults.realname%>"> <input class="input" name="realname" class="input" placeholder="Shout User">
</label> </label>
</div> </div>
<div class="col-xs-12"> <div class="col-xs-12">
<label> <label>
Channels Channels
<input class="input" name="join" class="input" placeholder="<%=defaults.join%>"> <input class="input" name="join" class="input" placeholder="#shout-irc">
</label> </label>
</div> </div>
<div class="col-xs-12"> <div class="col-xs-12">

View File

@ -53,19 +53,28 @@ $(function() {
} }
); );
socket.on("error", function(e) {
console.log(e);
});
socket.on("auth", function(data) { socket.on("auth", function(data) {
$("#footer").find(".sign-in").trigger("click"); $("body").addClass("signed-out");
$("#sign-in input:first").focus(); sidebar.find(".sign-in")
.click()
.end()
.find(".networks")
.html("")
.next()
.show();
}); });
socket.on("init", function(data) { socket.on("init", function(data) {
if (data.networks.length === 0) { if (data.networks.length === 0) {
$("#footer").find(".connect").trigger("click"); $("#footer").find(".connect").trigger("click");
$("#connect input:first").focus();
return; return;
} }
sidebar.find(".networks").append( sidebar.find(".networks").html(
render("networks", { render("networks", {
networks: data.networks networks: data.networks
}) })
@ -79,6 +88,9 @@ $(function() {
}) })
); );
sidebar.find(".chan").eq(0).trigger("click"); sidebar.find(".empty").hide();
$("body").removeClass("signed-out");
var id = $.cookie("target"); var id = $.cookie("target");
var target = sidebar.find("[data-target=" + id + "]").trigger("click"); var target = sidebar.find("[data-target=" + id + "]").trigger("click");
if (target.length === 0) { if (target.length === 0) {
@ -139,7 +151,8 @@ $(function() {
sidebar.find(".chan") sidebar.find(".chan")
.last() .last()
.trigger("click"); .trigger("click");
connect.find(".btn") $("#connect")
.find(".btn")
.prop("disabled", false) .prop("disabled", false)
.end() .end()
.find("input") .find("input")
@ -164,10 +177,13 @@ $(function() {
var id = data.network; var id = data.network;
sidebar.find("#network-" + id) sidebar.find("#network-" + id)
.remove() .remove()
.end() .end();
.find(".chan") var chan = sidebar.find(".chan")
.eq(0) .eq(0)
.trigger("click"); .trigger("click");
if (chan.length === 0) {
sidebar.find(".empty").show();
}
}); });
socket.on("users", function(data) { socket.on("users", function(data) {
@ -359,25 +375,25 @@ $(function() {
} }
}); });
var connect = $("#connect"); $("#sign-in, #connect").on("submit", "form", function(e) {
connect.on("submit", "form", function(e) { e.preventDefault()
e.preventDefault(); var event = "auth";
var form = $(this) var form = $(this);
.find(".btn") if (form.closest(".window").attr("id") == "connect") {
event = "conn";
form.find(".btn")
.attr("disabled", true) .attr("disabled", true)
.end(); .end();
}
var post = {}; var values = {};
var values = form.serializeArray(); $.each(form.serializeArray(), function(i, obj) {
$.each(values, function(i, obj) {
if (obj.value !== "") { if (obj.value !== "") {
post[obj.name] = obj.value; values[obj.name] = obj.value;
} }
}); });
socket.emit(
console.log(post); event, values
socket.emit("conn", post); );
}); });
function complete(word) { function complete(word) {

View File

@ -1,12 +0,0 @@
module.exports = {
port: 9000,
theme: "themes/example.css",
public: true,
defaults: {
host: "irc.freenode.net",
port: 6667,
nick: "shout-user",
realname: "Shout User",
join: "#shout-irc"
}
};

5
config.json Normal file
View File

@ -0,0 +1,5 @@
{
"port": 9000,
"theme": "themes/example.css",
"public": false
}

View File

@ -1,5 +1,5 @@
var _ = require("lodash"); var _ = require("lodash");
var config = require("../config"); var config = require("../config.json");
var net = require("net"); var net = require("net");
var Network = require("./models/network"); var Network = require("./models/network");
var slate = require("slate-irc"); var slate = require("slate-irc");
@ -25,12 +25,20 @@ var events = [
"whois" "whois"
]; ];
function Client(sockets) { function Client(sockets, config) {
_.merge(this, { _.merge(this, {
networks: [], config: config,
id: id++, id: id++,
name: "",
networks: [],
sockets: sockets sockets: sockets
}); });
if (config) {
var client = this;
_.each(config.networks || [], function(n) {
client.connect(n);
});
}
} }
Client.prototype.emit = function(event, data) { Client.prototype.emit = function(event, data) {
@ -62,18 +70,18 @@ Client.prototype.find = function(id) {
Client.prototype.connect = function(args) { Client.prototype.connect = function(args) {
var client = this; var client = this;
var options = { var server = {
host: args.host || config.defaults.host, host: args.host || "irc.freenode.org",
port: args.port || config.defaults.port port: args.port || 6667
}; };
var stream = args.tls ? tls.connect(options) : net.connect(options); var stream = args.tls ? tls.connect(server) : net.connect(server);
stream.on("error", function(e) { stream.on("error", function(e) {
console.log(e); console.log(e);
}); });
var nick = args.nick || config.defaults.nick; var nick = args.nick || "shout-user";
var realname = args.realname || config.defaults.realname; var realname = args.realname || "Shout User";
var irc = slate(stream); var irc = slate(stream);
irc.me = nick; irc.me = nick;
@ -81,7 +89,7 @@ Client.prototype.connect = function(args) {
irc.user(nick, realname); irc.user(nick, realname);
var network = new Network({ var network = new Network({
host: options.host, host: server.host,
irc: irc irc: irc
}); });
@ -91,13 +99,14 @@ Client.prototype.connect = function(args) {
}); });
events.forEach(function(plugin) { events.forEach(function(plugin) {
require("./plugins/irc-events/" + plugin).apply(client, [ var path = "./plugins/irc-events/" + plugin;
require(path).apply(client, [
irc, irc,
network network
]); ]);
}); });
var join = (args.join || config.defaults.join).replace(/\,/g, " ").split(/\s+/g); var join = (args.join || "#shout-irc").replace(/\,/g, " ").split(/\s+/g);
irc.on("welcome", function() { irc.on("welcome", function() {
irc.join(join); irc.join(join);
}); });
@ -107,7 +116,7 @@ Client.prototype.quit = function() {
this.networks.forEach(function(network) { this.networks.forEach(function(network) {
var irc = network.irc; var irc = network.irc;
if (network.connected) { if (network.connected) {
irc.quit(""); irc.quit();
} else { } else {
irc.stream.end(); irc.stream.end();
} }

View File

@ -1,6 +1,6 @@
var _ = require("lodash"); var _ = require("lodash");
var Client = require("./client"); var Client = require("./client");
var config = require("../config"); var config = require("../config.json");
var fs = require("fs"); var fs = require("fs");
var http = require("connect"); var http = require("connect");
var io = require("socket.io"); var io = require("socket.io");
@ -41,8 +41,37 @@ module.exports = function() {
} }
}); });
console.log("Shout started."); console.log("Server started");
console.log("Running on port" + port); console.log("Shout is now running on port " + port);
if (config.public) {
return;
}
fs.readdir("users/", function(err, files) {
if (err) {
console.log(err);
return;
}
_.each(files, function(file) {
fs.readFile("users/" + file + "/user.json", "utf-8", function(err, json) {
if (err) {
console.log(err);
return;
}
try {
json = JSON.parse(json);
} catch(e) {
console.log(e);
return;
}
clients.push(new Client(
sockets,
json
));
});
});
});
}; };
function index(req, res, next) { function index(req, res, next) {
@ -73,6 +102,7 @@ function init(socket, client) {
socket.on( socket.on(
"conn", "conn",
function(data) { function(data) {
console.log(data);
client.connect(data); client.connect(data);
} }
); );
@ -94,15 +124,15 @@ function auth(data) {
}); });
init(socket, client); init(socket, client);
} else { } else {
if (false) { _.each(clients, function(client) {
// .. if (client.config.name == data.name && client.config.password == data.password) {
init(socket, client);
} }
});
} }
} }
function input(client, data) { function input(client, data) {
var target = client.find(data.target);
var text = data.text; var text = data.text;
if (text.charAt(0) !== "/") { if (text.charAt(0) !== "/") {
text = "/say " + text; text = "/say " + text;
@ -111,17 +141,19 @@ function input(client, data) {
var args = text.split(" "); var args = text.split(" ");
var cmd = args.shift().replace("/", "").toLowerCase(); var cmd = args.shift().replace("/", "").toLowerCase();
inputs.forEach(function(plugin) { var target = client.find(data.target);
_.each(inputs, function(plugin) {
try { try {
var fn = require("./plugins/inputs/" + plugin); var path = "./plugins/inputs/" + plugin;
var fn = require(path);
fn.apply(client, [ fn.apply(client, [
target.network, target.network,
target.chan, target.chan,
cmd, cmd,
args args
]); ]);
} catch (err) { } catch (e) {
// .. console.log(path + ": " + e);
} }
}); });
} }

View File

@ -1,7 +1,10 @@
{ {
"user": "example", "user": "example",
"password": "password", "password": "password",
"servers": [ "networks": [
{} {
"host": "irc.rizon.net",
"nick": "example"
}
] ]
} }