2020-03-18 11:23:08 +00:00
|
|
|
package soju
|
|
|
|
|
|
|
|
import (
|
2020-03-18 23:57:14 +00:00
|
|
|
"flag"
|
2020-03-18 11:23:08 +00:00
|
|
|
"fmt"
|
2020-03-18 23:57:14 +00:00
|
|
|
"io/ioutil"
|
2020-03-18 11:23:08 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/shlex"
|
|
|
|
"gopkg.in/irc.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
const serviceNick = "BouncerServ"
|
|
|
|
|
2020-04-04 02:57:20 +00:00
|
|
|
var servicePrefix = &irc.Prefix{
|
|
|
|
Name: serviceNick,
|
|
|
|
User: serviceNick,
|
|
|
|
Host: serviceNick,
|
|
|
|
}
|
|
|
|
|
2020-03-25 19:58:07 +00:00
|
|
|
type serviceCommandSet map[string]*serviceCommand
|
|
|
|
|
2020-03-18 11:23:08 +00:00
|
|
|
type serviceCommand struct {
|
2020-03-25 19:58:07 +00:00
|
|
|
usage string
|
|
|
|
desc string
|
|
|
|
handle func(dc *downstreamConn, params []string) error
|
|
|
|
children serviceCommandSet
|
2020-03-18 11:23:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 02:48:25 +00:00
|
|
|
func sendServiceNOTICE(dc *downstreamConn, text string) {
|
|
|
|
dc.SendMessage(&irc.Message{
|
2020-04-04 02:57:20 +00:00
|
|
|
Prefix: servicePrefix,
|
2020-04-04 02:48:25 +00:00
|
|
|
Command: "NOTICE",
|
|
|
|
Params: []string{dc.nick, text},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-18 11:23:08 +00:00
|
|
|
func sendServicePRIVMSG(dc *downstreamConn, text string) {
|
|
|
|
dc.SendMessage(&irc.Message{
|
2020-04-04 02:57:20 +00:00
|
|
|
Prefix: servicePrefix,
|
2020-03-18 11:23:08 +00:00
|
|
|
Command: "PRIVMSG",
|
|
|
|
Params: []string{dc.nick, text},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleServicePRIVMSG(dc *downstreamConn, text string) {
|
|
|
|
words, err := shlex.Split(text)
|
|
|
|
if err != nil {
|
|
|
|
sendServicePRIVMSG(dc, fmt.Sprintf("error: failed to parse command: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-25 19:58:07 +00:00
|
|
|
cmd, params, err := serviceCommands.Get(words)
|
|
|
|
if err != nil {
|
|
|
|
sendServicePRIVMSG(dc, fmt.Sprintf(`error: %v (type "help" for a list of commands)`, err))
|
2020-03-18 11:23:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.handle(dc, params); err != nil {
|
|
|
|
sendServicePRIVMSG(dc, fmt.Sprintf("error: %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 19:58:07 +00:00
|
|
|
func (cmds serviceCommandSet) Get(params []string) (*serviceCommand, []string, error) {
|
|
|
|
if len(params) == 0 {
|
|
|
|
return nil, nil, fmt.Errorf("no command specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
name := params[0]
|
|
|
|
params = params[1:]
|
|
|
|
|
|
|
|
cmd, ok := cmds[name]
|
|
|
|
if !ok {
|
|
|
|
for k := range cmds {
|
|
|
|
if !strings.HasPrefix(k, name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cmd != nil {
|
|
|
|
return nil, params, fmt.Errorf("command %q is ambiguous", name)
|
|
|
|
}
|
|
|
|
cmd = cmds[k]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cmd == nil {
|
|
|
|
return nil, params, fmt.Errorf("command %q not found", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(params) == 0 || len(cmd.children) == 0 {
|
|
|
|
return cmd, params, nil
|
|
|
|
}
|
|
|
|
return cmd.children.Get(params)
|
|
|
|
}
|
|
|
|
|
|
|
|
var serviceCommands serviceCommandSet
|
2020-03-18 11:23:08 +00:00
|
|
|
|
|
|
|
func init() {
|
2020-03-25 19:58:07 +00:00
|
|
|
serviceCommands = serviceCommandSet{
|
2020-03-18 11:23:08 +00:00
|
|
|
"help": {
|
|
|
|
usage: "[command]",
|
|
|
|
desc: "print help message",
|
|
|
|
handle: handleServiceHelp,
|
|
|
|
},
|
2020-03-25 19:58:07 +00:00
|
|
|
"network": {
|
|
|
|
children: serviceCommandSet{
|
|
|
|
"create": {
|
|
|
|
usage: "-addr <addr> [-name name] [-username username] [-pass pass] [-realname realname] [-nick nick]",
|
|
|
|
desc: "add a new network",
|
|
|
|
handle: handleServiceCreateNetwork,
|
|
|
|
},
|
2020-03-25 21:57:48 +00:00
|
|
|
"status": {
|
|
|
|
desc: "show a list of saved networks and their current status",
|
|
|
|
handle: handleServiceNetworkStatus,
|
|
|
|
},
|
2020-04-01 13:40:20 +00:00
|
|
|
"delete": {
|
|
|
|
usage: "<name>",
|
|
|
|
desc: "delete a network",
|
|
|
|
handle: handleServiceNetworkDelete,
|
|
|
|
},
|
2020-03-25 19:58:07 +00:00
|
|
|
},
|
2020-03-18 23:57:14 +00:00
|
|
|
},
|
2020-03-18 11:23:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 19:58:07 +00:00
|
|
|
func appendServiceCommandSetHelp(cmds serviceCommandSet, prefix []string, l *[]string) {
|
|
|
|
for name, cmd := range cmds {
|
|
|
|
words := append(prefix, name)
|
|
|
|
if len(cmd.children) == 0 {
|
|
|
|
s := strings.Join(words, " ")
|
|
|
|
*l = append(*l, s)
|
|
|
|
} else {
|
|
|
|
appendServiceCommandSetHelp(cmd.children, words, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 11:23:08 +00:00
|
|
|
func handleServiceHelp(dc *downstreamConn, params []string) error {
|
|
|
|
if len(params) > 0 {
|
2020-03-25 19:58:07 +00:00
|
|
|
cmd, rest, err := serviceCommands.Get(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-03-18 11:23:08 +00:00
|
|
|
}
|
2020-03-25 19:58:07 +00:00
|
|
|
words := params[:len(params)-len(rest)]
|
|
|
|
|
|
|
|
if len(cmd.children) > 0 {
|
|
|
|
var l []string
|
|
|
|
appendServiceCommandSetHelp(cmd.children, words, &l)
|
|
|
|
sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", "))
|
|
|
|
} else {
|
|
|
|
text := strings.Join(words, " ")
|
|
|
|
if cmd.usage != "" {
|
|
|
|
text += " " + cmd.usage
|
|
|
|
}
|
|
|
|
text += ": " + cmd.desc
|
|
|
|
|
|
|
|
sendServicePRIVMSG(dc, text)
|
2020-03-18 11:23:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var l []string
|
2020-03-25 19:58:07 +00:00
|
|
|
appendServiceCommandSetHelp(serviceCommands, nil, &l)
|
2020-03-18 11:23:08 +00:00
|
|
|
sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", "))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-18 23:57:14 +00:00
|
|
|
|
2020-04-01 13:40:20 +00:00
|
|
|
func newFlagSet() *flag.FlagSet {
|
2020-03-18 23:57:14 +00:00
|
|
|
fs := flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
fs.SetOutput(ioutil.Discard)
|
2020-04-01 13:40:20 +00:00
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleServiceCreateNetwork(dc *downstreamConn, params []string) error {
|
|
|
|
fs := newFlagSet()
|
2020-03-18 23:57:14 +00:00
|
|
|
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 == "" {
|
2020-03-25 19:58:07 +00:00
|
|
|
return fmt.Errorf("flag -addr is required")
|
2020-03-18 23:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-04-01 13:40:20 +00:00
|
|
|
sendServicePRIVMSG(dc, fmt.Sprintf("created network %q", network.GetName()))
|
2020-03-18 23:57:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-03-25 21:57:48 +00:00
|
|
|
|
|
|
|
func handleServiceNetworkStatus(dc *downstreamConn, params []string) error {
|
|
|
|
dc.user.forEachNetwork(func(net *network) {
|
|
|
|
var statuses []string
|
|
|
|
var details string
|
|
|
|
if uc := net.upstream(); uc != nil {
|
|
|
|
statuses = append(statuses, "connected as "+uc.nick)
|
|
|
|
details = fmt.Sprintf("%v channels", len(uc.channels))
|
|
|
|
} else {
|
|
|
|
statuses = append(statuses, "disconnected")
|
2020-04-04 02:51:48 +00:00
|
|
|
if net.lastError != nil {
|
|
|
|
details = net.lastError.Error()
|
|
|
|
}
|
2020-03-25 21:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if net == dc.network {
|
|
|
|
statuses = append(statuses, "current")
|
|
|
|
}
|
|
|
|
|
|
|
|
s := fmt.Sprintf("%v (%v) [%v]", net.GetName(), net.Addr, strings.Join(statuses, ", "))
|
|
|
|
if details != "" {
|
|
|
|
s += ": " + details
|
|
|
|
}
|
|
|
|
sendServicePRIVMSG(dc, s)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-01 13:40:20 +00:00
|
|
|
|
|
|
|
func handleServiceNetworkDelete(dc *downstreamConn, params []string) error {
|
|
|
|
if len(params) != 1 {
|
|
|
|
return fmt.Errorf("expected exactly one argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
net := dc.user.getNetwork(params[0])
|
|
|
|
if net == nil {
|
|
|
|
return fmt.Errorf("unknown network %q", params[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dc.user.deleteNetwork(net.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sendServicePRIVMSG(dc, fmt.Sprintf("deleted network %q", net.GetName()))
|
|
|
|
return nil
|
|
|
|
}
|