From 37eb162b750f7bdd15bfa2b0bd2609f4b5e66844 Mon Sep 17 00:00:00 2001 From: delthas Date: Thu, 19 Mar 2020 00:57:14 +0100 Subject: [PATCH] Add create-network bouncer service command --- downstream.go | 5 ++++- service.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ user.go | 7 ++----- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/downstream.go b/downstream.go index 467d1c7..8fccfed 100644 --- a/downstream.go +++ b/downstream.go @@ -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 } diff --git a/service.go b/service.go index 35f0ae5..c37df40 100644 --- a/service.go +++ b/service.go @@ -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 [-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 +} diff --git a/user.go b/user.go index 980ddfc..763f4ff 100644 --- a/user.go +++ b/user.go @@ -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