Improve port handling

This commit is contained in:
Ken-Håvard Lieng 2016-01-14 05:56:53 +01:00
parent 2bffef825d
commit 13a3d96b44
7 changed files with 48 additions and 43 deletions

File diff suppressed because one or more lines are too long

View File

@ -2,9 +2,15 @@ import * as actions from '../actions';
import { updateSelection } from './tab';
export function connect(server, nick, options) {
let host = server;
const i = server.indexOf(':');
if (i > 0) {
host = server.slice(0, i);
}
return {
type: actions.CONNECT,
server,
host,
nick,
options,
socket: {

View File

@ -168,22 +168,20 @@ export default createReducer(Map(), {
return state.withMutations(s => {
action.data.forEach(server => {
if (!state.has(server.address)) {
s.set(server.address, Map());
if (!state.has(server.host)) {
s.set(server.host, Map());
}
});
});
},
[actions.CONNECT](state, action) {
let { server } = action;
const i = server.indexOf(':');
if (i > 0) {
server = server.slice(0, i);
}
if (!state.has(server)) {
return state.set(server, Map());
const { host } = action;
if (!state.has(host)) {
return state.set(host, Map());
}
return state;
},

View File

@ -11,18 +11,16 @@ const Server = Record({
export default createReducer(Map(), {
[actions.CONNECT](state, action) {
let { server } = action;
const { nick, options } = action;
const { host, nick, options } = action;
const i = server.indexOf(':');
if (i > 0) {
server = server.slice(0, i);
if (!state.has(host)) {
return state.set(host, new Server({
nick,
name: options.name || host
}));
}
return state.set(server, new Server({
nick,
name: options.name || server
}));
return state;
},
[actions.DISCONNECT](state, action) {
@ -41,7 +39,7 @@ export default createReducer(Map(), {
return state.withMutations(s => {
action.data.forEach(server => {
s.set(server.address, new Server(server));
s.set(server.host, new Server(server));
});
});
},

View File

@ -2,6 +2,7 @@ package server
import (
"crypto/tls"
"net"
"github.com/khlieng/dispatch/irc"
"github.com/khlieng/dispatch/storage"
@ -28,13 +29,13 @@ func reconnectIRC() {
}
}
i.Connect(server.Address)
session.setIRC(i.Host, i)
session.setIRC(server.Host, i)
i.Connect(net.JoinHostPort(server.Host, server.Port))
go newIRCHandler(i, session).run()
var joining []string
for _, channel := range channels {
if channel.Server == server.Address {
if channel.Server == server.Host {
joining = append(joining, channel.Name)
}
}

View File

@ -4,7 +4,7 @@ import (
"crypto/tls"
"encoding/json"
"log"
"strings"
"net"
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/gorilla/websocket"
@ -98,7 +98,12 @@ func (h *wsHandler) connect(b []byte) {
var data Connect
json.Unmarshal(b, &data)
if _, ok := h.session.getIRC(data.Server); !ok {
host, port, err := net.SplitHostPort(data.Server)
if err != nil {
host = data.Server
}
if _, ok := h.session.getIRC(host); !ok {
log.Println(h.addr, "connecting to", data.Server)
i := irc.NewClient(data.Nick, data.Username)
@ -112,18 +117,14 @@ func (h *wsHandler) connect(b []byte) {
}
}
if idx := strings.Index(data.Server, ":"); idx < 0 {
h.session.setIRC(data.Server, i)
} else {
h.session.setIRC(data.Server[:idx], i)
}
h.session.setIRC(host, i)
i.Connect(data.Server)
go newIRCHandler(i, h.session).run()
h.session.user.AddServer(storage.Server{
Name: data.Name,
Address: i.Host,
Host: host,
Port: port,
TLS: data.TLS,
Password: data.Password,
Nick: data.Nick,

View File

@ -16,7 +16,8 @@ import (
type Server struct {
Name string `json:"name"`
Address string `json:"address"`
Host string `json:"host"`
Port string `json:"port,omitempty"`
TLS bool `json:"tls"`
Password string `json:"password,omitempty"`
Nick string `json:"nick"`
@ -134,7 +135,7 @@ func (u *User) AddServer(server Server) {
b := tx.Bucket([]byte("Servers"))
data, _ := json.Marshal(server)
b.Put([]byte(u.UUID+":"+server.Address), data)
b.Put([]byte(u.UUID+":"+server.Host), data)
return nil
})