Auto-save IRC networks

This commit is contained in:
Simon Ser 2020-03-12 21:28:09 +01:00
parent 0ef08dfbb5
commit 85f28daf2d
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 48 additions and 7 deletions

View File

@ -14,6 +14,8 @@ A user-friendly IRC bouncer.
go run ./cmd/jouncectl create-user jounce
go run ./cmd/jounce
Then connect with username `jounce@chat.freenode.net` and join `#jounce`.
## Contributing
Send patches on the [mailing list], report bugs on the [issue tracker].

View File

@ -1,10 +1,12 @@
package jounce
import (
"crypto/tls"
"fmt"
"io"
"net"
"strings"
"time"
"golang.org/x/crypto/bcrypt"
"gopkg.in/irc.v3"
@ -336,6 +338,15 @@ func (dc *downstreamConn) handleMessageUnregistered(msg *irc.Message) error {
return nil
}
func sanityCheckServer(addr string) error {
dialer := net.Dialer{Timeout: 30 * time.Second}
conn, err := tls.DialWithDialer(&dialer, "tcp", addr, nil)
if err != nil {
return err
}
return conn.Close()
}
func (dc *downstreamConn) register() error {
username := strings.TrimPrefix(dc.username, "~")
var networkName string
@ -365,13 +376,25 @@ func (dc *downstreamConn) register() error {
if networkName != "" {
network = u.getNetwork(networkName)
if network == nil {
dc.logger.Printf("failed registration: unknown network %q", networkName)
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: irc.ERR_PASSWDMISMATCH,
Params: []string{"*", fmt.Sprintf("Unknown network %q", networkName)},
})
return nil
addr := networkName
if !strings.ContainsRune(addr, ':') {
addr = addr + ":6697"
}
dc.logger.Printf("trying to connect to new upstream server %q", addr)
if err := sanityCheckServer(addr); err != nil {
dc.logger.Printf("failed to connect to %q: %v", addr, err)
return ircError{&irc.Message{
Command: irc.ERR_PASSWDMISMATCH,
Params: []string{"*", fmt.Sprintf("Failed to connect to %q", networkName)},
}}
}
dc.logger.Printf("auto-adding network %q", networkName)
network, err = u.createNetwork(networkName, dc.nick)
if err != nil {
return err
}
}
}

View File

@ -163,6 +163,22 @@ func (u *user) run() {
u.lock.Unlock()
}
func (u *user) createNetwork(addr, nick string) (*network, error) {
network := newNetwork(u, &Network{
Addr: addr,
Nick: nick,
})
err := u.srv.db.StoreNetwork(u.Username, &network.Network)
if err != nil {
return nil, err
}
u.lock.Lock()
u.networks = append(u.networks, network)
u.lock.Unlock()
go network.run()
return network, nil
}
type Server struct {
Hostname string
Logger Logger