Sending queries and messages to channels now works

This commit is contained in:
Mattias Erming 2014-03-12 20:45:41 +01:00
parent a4a3499a54
commit 57bd684c74
2 changed files with 69 additions and 15 deletions

View File

@ -11,7 +11,7 @@ var id = 1;
models.User = Backbone.Model.extend({ models.User = Backbone.Model.extend({
defaults: { defaults: {
mode: "", mode: "",
name: "" name: "user"
} }
}); });

View File

@ -20,10 +20,7 @@ Server.prototype.listen = function(port) {
this.networks.on( this.networks.on(
"all", "all",
function(type, data) { function(type, data) {
if ([ if (type == "users" || type == "messages") {
"users",
"messages"
].indexOf(type) != -1) {
self.sockets.emit(type, data); self.sockets.emit(type, data);
} else { } else {
self.sockets.emit("networks", self.networks); self.sockets.emit("networks", self.networks);
@ -31,11 +28,8 @@ Server.prototype.listen = function(port) {
} }
); );
var options = {
log: false
};
this.sockets = io this.sockets = io
.listen(http, options) .listen(http, {log: false})
.sockets; .sockets;
this.sockets.on("connection", function(socket) { this.sockets.on("connection", function(socket) {
socket.emit( socket.emit(
@ -65,11 +59,32 @@ function handleInput(input) {
switch (cmd) { switch (cmd) {
case "": case "":
var irc = target.network.irc;
if (typeof irc !== "undefined") {
irc.say(target.channel.get("name"), input.text);
}
target.channel.get("messages").add( target.channel.get("messages").add(
new models.Message({user: "user", text: input.text}) new models.Message({
user: target.network.get("nick"),
text: input.text
})
); );
break; break;
case "QUERY":
var irc = target.network.irc;
if (argv[1] && typeof irc !== "undefined") {
var channels = target.network.get("channels");
if (argv[1].charAt(0) != "#" && !channels.findWhere({name: argv[1]})) {
channels.add(
new models.Channel({
name: argv[1]
})
);
}
}
break;
case "JOIN": case "JOIN":
var irc = target.network.irc; var irc = target.network.irc;
if (argv[1] && typeof irc !== "undefined") { if (argv[1] && typeof irc !== "undefined") {
@ -79,15 +94,15 @@ function handleInput(input) {
case "PART": case "PART":
var irc = target.network.irc; var irc = target.network.irc;
if (argv[1] && typeof irc !== "undefined") { if (typeof irc !== "undefined") {
irc.part(argv[1]); irc.part(argv[1] ? argv[1] : target.channel.get("name"));
} }
break; break;
case "NAMES": case "NAMES":
var irc = target.network.irc; var irc = target.network.irc;
if (argv[1] && typeof irc !== "undefined") { if (typeof irc !== "undefined") {
irc.send("NAMES", argv[1]); irc.send("NAMES", argv[1] ? argv[1] : target.channel.get("name"));
} }
break; break;
@ -97,6 +112,9 @@ function handleInput(input) {
var network = this.networks.add( var network = this.networks.add(
new models.Network({host: argv[1]}) new models.Network({host: argv[1]})
); );
network.irc.addListener(
"error", function() { /* .. */ }
);
network.irc.addListener( network.irc.addListener(
"raw", "raw",
function() { function() {
@ -135,6 +153,31 @@ function handleEvent(argv) {
var event = argv.command; var event = argv.command;
switch (event) { switch (event) {
case "PRIVMSG":
var target = argv.args[0];
if (target.charAt(0) != "#") {
target = argv.nick;
}
var channel = channels.findWhere({name: target});
var message = argv.args[1];
if (typeof channel == "undefined") {
channel = channels.add(
new models.Channel({
name: target
})
);
}
channel.get("messages").add(
new models.Message({
user: argv.nick,
text: message
})
);
break;
case "JOIN": case "JOIN":
if (argv.nick == network.get("nick")) { if (argv.nick == network.get("nick")) {
channels.add( channels.add(
@ -205,6 +248,17 @@ function handleEvent(argv) {
"add", {}, users "add", {}, users
); );
break; break;
case "err_cannotsendtochan":
case "err_nosuchnick":
var channel = network.get("channels").findWhere({name: argv.args[1]});
var messages = channel.get("messages");
messages.add(
new models.Message({
text: argv.args[2]
})
);
break;
} }
// Debug // Debug