Add create-network bouncer service command

This commit is contained in:
delthas 2020-03-19 00:57:14 +01:00 committed by Simon Ser
parent 37e56a01b2
commit 37eb162b75
3 changed files with 51 additions and 6 deletions

View File

@ -638,7 +638,10 @@ func (dc *downstreamConn) setNetwork(networkName string) error {
dc.logger.Printf("auto-saving network %q", networkName)
var err error
network, err = dc.user.createNetwork(networkName, dc.nick)
network, err = dc.user.createNetwork(&Network{
Addr: networkName,
Nick: dc.nick,
})
if err != nil {
return err
}

View File

@ -1,7 +1,9 @@
package soju
import (
"flag"
"fmt"
"io/ioutil"
"strings"
"github.com/google/shlex"
@ -58,6 +60,11 @@ func init() {
desc: "print help message",
handle: handleServiceHelp,
},
"create-network": {
usage: "-addr <addr> [-name name] [-username username] [-pass pass] [-realname realname] [-nick nick]",
desc: "add a new network",
handle: handleServiceCreateNetwork,
},
}
}
@ -85,3 +92,41 @@ func handleServiceHelp(dc *downstreamConn, params []string) error {
}
return nil
}
func handleServiceCreateNetwork(dc *downstreamConn, params []string) error {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.SetOutput(ioutil.Discard)
addr := fs.String("addr", "", "")
name := fs.String("name", "", "")
username := fs.String("username", "", "")
pass := fs.String("pass", "", "")
realname := fs.String("realname", "", "")
nick := fs.String("nick", "", "")
if err := fs.Parse(params); err != nil {
return err
}
if *addr == "" {
return fmt.Errorf("flag addr is required")
}
if *nick == "" {
*nick = dc.nick
}
var err error
network, err := dc.user.createNetwork(&Network{
Addr: *addr,
Name: *name,
Username: *username,
Pass: *pass,
Realname: *realname,
Nick: *nick,
})
if err != nil {
return fmt.Errorf("could not create network: %v", err)
}
sendServicePRIVMSG(dc, fmt.Sprintf("created network %s successfully", network.GetName()))
return nil
}

View File

@ -158,11 +158,8 @@ func (u *user) run() {
}
}
func (u *user) createNetwork(addr, nick string) (*network, error) {
network := newNetwork(u, &Network{
Addr: addr,
Nick: nick,
})
func (u *user) createNetwork(net *Network) (*network, error) {
network := newNetwork(u, net)
err := u.srv.db.StoreNetwork(u.Username, &network.Network)
if err != nil {
return nil, err