diff --git a/client/src/js/actions/message.js b/client/src/js/actions/message.js index 8e12ef02..70c7488a 100644 --- a/client/src/js/actions/message.js +++ b/client/src/js/actions/message.js @@ -7,7 +7,8 @@ var messageActions = Reflux.createActions([ 'send', 'add', 'broadcast', - 'inform' + 'inform', + 'command' ]); messageActions.send.preEmit = function(message, to, server) { diff --git a/client/src/js/app.js b/client/src/js/app.js index aba50c7a..16344f84 100644 --- a/client/src/js/app.js +++ b/client/src/js/app.js @@ -4,6 +4,7 @@ var Route = Router.Route; var DefaultRoute = Router.DefaultRoute; require('./irc'); +require('./command'); var socket = require('./socket'); var util = require('./util'); var App = require('./components/App.jsx'); diff --git a/client/src/js/command.js b/client/src/js/command.js new file mode 100644 index 00000000..349a2a63 --- /dev/null +++ b/client/src/js/command.js @@ -0,0 +1,49 @@ +var channelStore = require('./stores/channel'); +var channelActions = require('./actions/channel'); +var messageActions = require('./actions/message'); +var serverActions = require('./actions/server'); +var tabActions = require('./actions/tab'); + +messageActions.command.listen(function(cmd, channel, server) { + var params = cmd.slice(1).split(' '); + + switch (params[0].toLowerCase()) { + case 'nick': + if (params[1]) { + serverActions.setNick(params[1], server); + } + break; + + case 'quit': + serverActions.disconnect(server); + break; + + case 'join': + if (params[1]) { + channelActions.join([params[1]], server); + tabActions.select(server, params[1]); + } + break; + + case 'part': + if (channel) { + channelActions.part([channel], server); + } + break; + + case 'me': + if (params.length > 1) { + messageActions.send('\x01ACTION ' + params.slice(1).join(' ') + '\x01', channel, server); + } + break; + + case 'topic': + var topic = channelStore.getTopic(server, channel); + if (topic) { + messageActions.inform(topic, server, channel); + } else { + messageActions.inform('No topic set', server, channel); + } + break; + } +}); \ No newline at end of file diff --git a/client/src/js/components/MessageInput.jsx b/client/src/js/components/MessageInput.jsx index 7c84db94..737df37d 100644 --- a/client/src/js/components/MessageInput.jsx +++ b/client/src/js/components/MessageInput.jsx @@ -1,53 +1,10 @@ var React = require('react'); var Reflux = require('reflux'); -var channelStore = require('../stores/channel'); var selectedTabStore = require('../stores/selectedTab'); var messageActions = require('../actions/message'); -var channelActions = require('../actions/channel'); -var serverActions = require('../actions/server'); var tabActions = require('../actions/tab'); -function dispatchCommand(cmd, channel, server) { - var params = cmd.slice(1).split(' '); - - switch (params[0].toLowerCase()) { - case 'join': - if (params[1]) { - channelActions.join([params[1]], server); - tabActions.select(server, params[1]); - } - break; - - case 'part': - if (channel) { - channelActions.part([channel], server); - } - break; - - case 'me': - if (params.length > 1) { - messageActions.send('\x01ACTION ' + params.slice(1).join(' ') + '\x01', channel, server); - } - break; - - case 'topic': - var topic = channelStore.getTopic(server, channel); - if (topic) { - messageActions.inform(topic, server, channel); - } else { - messageActions.inform('No topic set', server, channel); - } - break; - - case 'nick': - if (params[1]) { - serverActions.setNick(params[1], server); - } - break; - } -} - var MessageInput = React.createClass({ mixins: [ Reflux.connect(selectedTabStore, 'selectedTab') @@ -64,7 +21,7 @@ var MessageInput = React.createClass({ var tab = this.state.selectedTab; if (e.target.value[0] === '/') { - dispatchCommand(e.target.value, tab.channel, tab.server); + messageActions.command(e.target.value, tab.channel, tab.server); } else { messageActions.send(e.target.value, tab.channel, tab.server); } diff --git a/client/src/js/stores/channel.js b/client/src/js/stores/channel.js index 614fbe45..7ad7f3c0 100644 --- a/client/src/js/stores/channel.js +++ b/client/src/js/stores/channel.js @@ -82,6 +82,7 @@ var channelStore = Reflux.createStore({ init: function() { this.listenToMany(actions); this.listenTo(serverActions.connect, 'addServer'); + this.listenTo(serverActions.disconnect, 'removeServer'); this.listenTo(serverActions.load, 'loadServers'); }, @@ -185,6 +186,11 @@ var channelStore = Reflux.createStore({ } }, + removeServer: function(server) { + delete channels[server]; + this.trigger(channels); + }, + loadServers: function(storedServers) { _.each(storedServers, function(server) { if (!(server.address in channels)) { diff --git a/client/src/js/stores/server.js b/client/src/js/stores/server.js index 53bbac9c..97b1f6d1 100644 --- a/client/src/js/stores/server.js +++ b/client/src/js/stores/server.js @@ -28,6 +28,11 @@ var serverStore = Reflux.createStore({ tabActions.select(server); }, + disconnect: function(server) { + delete servers[server]; + this.trigger(servers); + }, + setNick: function(nick, server) { servers[server].nick = nick; this.trigger(servers); diff --git a/session.go b/session.go index 236ba162..4749a135 100644 --- a/session.go +++ b/session.go @@ -30,9 +30,9 @@ func NewSession() *Session { func (s *Session) getIRC(server string) (*IRC, bool) { s.ircLock.Lock() - defer s.ircLock.Unlock() - irc, ok := s.irc[server] + s.ircLock.Unlock() + return irc, ok } @@ -42,6 +42,20 @@ func (s *Session) setIRC(server string, irc *IRC) { s.ircLock.Unlock() } +func (s *Session) deleteIRC(server string) { + s.ircLock.Lock() + delete(s.irc, server) + s.ircLock.Unlock() +} + +func (s *Session) numIRC() int { + s.ircLock.Lock() + n := len(s.irc) + s.ircLock.Unlock() + + return n +} + func (s *Session) setWS(addr string, ws *websocket.Conn) { socket := NewWebSocket(ws) go socket.write() diff --git a/websocket_handler.go b/websocket_handler.go index 1dc18751..428c02d9 100644 --- a/websocket_handler.go +++ b/websocket_handler.go @@ -43,7 +43,7 @@ func handleWS(ws *websocket.Conn) { sessionLock.Unlock() session = storedSession - log.Println(addr, "attached to existing IRC connections") + log.Println(addr, "attached to", session.numIRC(), "existing IRC connections") channels := session.user.GetChannels() for i, channel := range channels { @@ -124,9 +124,10 @@ func handleWS(ws *websocket.Conn) { json.Unmarshal(req.Request, &data) if irc, ok := session.getIRC(data.Server); ok { + irc.Quit() + session.deleteIRC(data.Server) channelStore.RemoveUserAll(irc.GetNick(), data.Server) session.user.RemoveServer(data.Server) - irc.Quit() } case "chat":