Added /quit, the server IRC client gets cleaned up properly when quitting, pulled command handling out into a separate file

This commit is contained in:
khlieng 2015-02-09 01:00:30 +01:00
parent d11aa3ff4e
commit 99ef788906
8 changed files with 83 additions and 49 deletions

View File

@ -7,7 +7,8 @@ var messageActions = Reflux.createActions([
'send',
'add',
'broadcast',
'inform'
'inform',
'command'
]);
messageActions.send.preEmit = function(message, to, server) {

View File

@ -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');

49
client/src/js/command.js Normal file
View File

@ -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;
}
});

View File

@ -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);
}

View File

@ -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)) {

View File

@ -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);

View File

@ -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()

View File

@ -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":