diff --git a/client/src/js/actions/channel.js b/client/src/js/actions/channel.js index e917910d..c33d098c 100644 --- a/client/src/js/actions/channel.js +++ b/client/src/js/actions/channel.js @@ -30,36 +30,4 @@ channelActions.part.preEmit = function(channels, server) { }); }; -socket.on('join', function(data) { - channelActions.addUser(data.user, data.server, data.channels[0]); -}); - -socket.on('part', function(data) { - channelActions.removeUser(data.user, data.server, data.channels[0]); -}); - -socket.on('quit', function(data) { - channelActions.removeUserAll(data.user, data.server); -}); - -socket.on('nick', function(data) { - channelActions.renameUser(data.old, data.new, data.server); -}); - -socket.on('users', function(data) { - channelActions.setUsers(data.users, data.server, data.channel); -}); - -socket.on('topic', function(data) { - channelActions.setTopic(data.topic, data.server, data.channel); -}); - -socket.on('mode', function(data) { - channelActions.setMode(data); -}); - -socket.on('channels', function(data) { - channelActions.load(data); -}); - module.exports = channelActions; \ No newline at end of file diff --git a/client/src/js/actions/message.js b/client/src/js/actions/message.js index 27293a02..8e12ef02 100644 --- a/client/src/js/actions/message.js +++ b/client/src/js/actions/message.js @@ -6,7 +6,8 @@ var socket = require('../socket'); var messageActions = Reflux.createActions([ 'send', 'add', - 'broadcast' + 'broadcast', + 'inform' ]); messageActions.send.preEmit = function(message, to, server) { @@ -17,48 +18,4 @@ messageActions.send.preEmit = function(message, to, server) { }); }; -socket.on('message', function(data) { - messageActions.add(data); -}); - -socket.on('pm', function(data) { - messageActions.add(data); -}); - -socket.on('join', function(data) { - messageActions.add({ - server: data.server, - to: data.channels[0], - message: data.user + ' joined the channel', - type: 'info' - }); -}); - -socket.on('part', function(data) { - messageActions.add({ - server: data.server, - to: data.channels[0], - message: data.user + ' left the channel', - type: 'info' - }); -}); - -socket.on('quit', function(data) { - messageActions.broadcast(data.user + ' quit', data.server); -}); - -socket.on('nick', function(data) { - messageActions.broadcast(data.old + ' changed nick to ' + data.new, data.server); -}); - -socket.on('motd', function(data) { - _.each(data.content.split('\n'), function(line) { - messageActions.add({ - server: data.server, - to: data.server, - message: line - }); - }); -}); - module.exports = messageActions; \ No newline at end of file diff --git a/client/src/js/actions/server.js b/client/src/js/actions/server.js index 1412724c..ab01c586 100644 --- a/client/src/js/actions/server.js +++ b/client/src/js/actions/server.js @@ -21,8 +21,4 @@ serverActions.disconnect.preEmit = function(server) { socket.send('quit', { server: server }); }; -socket.on('servers', function(data) { - serverActions.load(data); -}); - module.exports = serverActions; \ No newline at end of file diff --git a/client/src/js/app.js b/client/src/js/app.js index d7fe7fe4..6f72d899 100644 --- a/client/src/js/app.js +++ b/client/src/js/app.js @@ -1,5 +1,6 @@ var React = require('react'); +require('./irc'); var socket = require('./socket'); var util = require('./util'); var App = require('./components/App.jsx'); diff --git a/client/src/js/irc.js b/client/src/js/irc.js new file mode 100644 index 00000000..537509bd --- /dev/null +++ b/client/src/js/irc.js @@ -0,0 +1,68 @@ +var _ = require('lodash'); + +var socket = require('./socket'); +var channelActions = require('./actions/channel'); +var messageActions = require('./actions/message'); +var serverActions = require('./actions/server'); + +socket.on('join', function(data) { + channelActions.addUser(data.user, data.server, data.channels[0]); + messageActions.inform(data.user + ' joined the channel', data.server, data.channels[0]); +}); + +socket.on('part', function(data) { + channelActions.removeUser(data.user, data.server, data.channels[0]); + messageActions.inform(withReason(data.user + ' left the channel', data.reason), data.server, data.channels[0]); +}); + +socket.on('quit', function(data) { + messageActions.broadcast(withReason(data.user + ' quit', data.reason), data.server, data.user); + channelActions.removeUserAll(data.user, data.server); +}); + +socket.on('nick', function(data) { + messageActions.broadcast(data.old + ' changed nick to ' + data.new, data.server, data.old); + channelActions.renameUser(data.old, data.new, data.server); +}); + +socket.on('message', function(data) { + messageActions.add(data); +}); + +socket.on('pm', function(data) { + messageActions.add(data); +}); + +socket.on('motd', function(data) { + _.each(data.content.split('\n'), function(line) { + messageActions.add({ + server: data.server, + to: data.server, + message: line + }); + }); +}); + +socket.on('users', function(data) { + channelActions.setUsers(data.users, data.server, data.channel); +}); + +socket.on('topic', function(data) { + channelActions.setTopic(data.topic, data.server, data.channel); +}); + +socket.on('mode', function(data) { + channelActions.setMode(data); +}); + +socket.on('servers', function(data) { + serverActions.load(data); +}); + +socket.on('channels', function(data) { + channelActions.load(data); +}); + +function withReason(message, reason) { + return message + (reason ? ' (' + reason + ')' : ''); +} \ No newline at end of file diff --git a/client/src/js/stores/message.js b/client/src/js/stores/message.js index f0aa65f9..5297d9a5 100644 --- a/client/src/js/stores/message.js +++ b/client/src/js/stores/message.js @@ -8,6 +8,8 @@ var actions = require('../actions/message'); var messages = {}; function addMessage(message, dest) { + message.time = new Date(); + if (!(message.server in messages)) { messages[message.server] = {}; messages[message.server][dest] = [message]; @@ -28,8 +30,7 @@ var messageStore = Reflux.createStore({ server: server, from: serverStore.getNick(server), to: to, - message: message, - time: new Date() + message: message }, to); this.trigger(messages); @@ -41,25 +42,34 @@ var messageStore = Reflux.createStore({ dest = message.server; } - message.time = new Date(); - addMessage(message, dest); this.trigger(messages); }, - broadcast: function(message, server) { + broadcast: function(message, server, user) { _.each(channelStore.getChannels(server), function(channel, channelName) { - addMessage({ - server: server, - to: channelName, - message: message, - type: 'info', - time: new Date() - }, channelName); + if (!user || (user && _.find(channel.users, { nick: user }))) { + addMessage({ + server: server, + to: channelName, + message: message, + type: 'info' + }, channelName); + } }); this.trigger(messages); }, + inform: function(message, server, channel) { + addMessage({ + server: server, + to: channel, + message: message, + type: 'info' + }, channel); + this.trigger(messages); + }, + getState: function() { return messages; } diff --git a/json_types.go b/json_types.go index 3f30f8d1..ae281a7c 100644 --- a/json_types.go +++ b/json_types.go @@ -33,6 +33,11 @@ type Join struct { Channels []string `json:"channels"` } +type Part struct { + Join + Reason string `json:"reason,omitempty"` +} + type Mode struct { Server string `json:"server"` Channel string `json:"channel"` @@ -44,6 +49,7 @@ type Mode struct { type Quit struct { Server string `json:"server"` User string `json:"user"` + Reason string `json:"reason,omitempty"` } type Chat struct { diff --git a/message_handler.go b/message_handler.go index 607e81d2..14db63e8 100644 --- a/message_handler.go +++ b/message_handler.go @@ -16,8 +16,6 @@ func handleMessages(irc *IRC, session *Session) { for msg := range irc.Messages { switch msg.Command { case NICK: - printMessage(msg, irc) - session.sendJSON("nick", Nick{ Server: irc.Host, Old: msg.Prefix, @@ -47,10 +45,13 @@ func handleMessages(irc *IRC, session *Session) { case PART: user := msg.Prefix - session.sendJSON("part", Join{ - Server: irc.Host, - User: user, - Channels: msg.Params, + session.sendJSON("part", Part{ + Join: Join{ + Server: irc.Host, + User: user, + Channels: msg.Params, + }, + Reason: msg.Trailing, }) channelStore.RemoveUser(user, irc.Host, msg.Params[0]) @@ -94,6 +95,7 @@ func handleMessages(irc *IRC, session *Session) { session.sendJSON("quit", Quit{ Server: irc.Host, User: user, + Reason: msg.Trailing, }) channelStore.RemoveUserAll(user, irc.Host) diff --git a/websocket_handler.go b/websocket_handler.go index d1e10525..4d9ad537 100644 --- a/websocket_handler.go +++ b/websocket_handler.go @@ -104,7 +104,7 @@ func handleWS(ws *websocket.Conn) { } case "part": - var data Join + var data Part json.Unmarshal(req.Request, &data)