[117] | 1 | package soju
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
[652] | 4 | "context"
|
---|
[307] | 5 | "crypto/sha1"
|
---|
| 6 | "crypto/sha256"
|
---|
[575] | 7 | "crypto/sha512"
|
---|
[307] | 8 | "encoding/hex"
|
---|
| 9 | "errors"
|
---|
[120] | 10 | "flag"
|
---|
[117] | 11 | "fmt"
|
---|
[120] | 12 | "io/ioutil"
|
---|
[339] | 13 | "sort"
|
---|
[542] | 14 | "strconv"
|
---|
[117] | 15 | "strings"
|
---|
[307] | 16 | "time"
|
---|
[566] | 17 | "unicode"
|
---|
[117] | 18 |
|
---|
[252] | 19 | "golang.org/x/crypto/bcrypt"
|
---|
[117] | 20 | "gopkg.in/irc.v3"
|
---|
| 21 | )
|
---|
| 22 |
|
---|
| 23 | const serviceNick = "BouncerServ"
|
---|
[478] | 24 | const serviceNickCM = "bouncerserv"
|
---|
[343] | 25 | const serviceRealname = "soju bouncer service"
|
---|
[117] | 26 |
|
---|
[606] | 27 | // maxRSABits is the maximum number of RSA key bits used when generating a new
|
---|
| 28 | // private key.
|
---|
| 29 | const maxRSABits = 8192
|
---|
| 30 |
|
---|
[220] | 31 | var servicePrefix = &irc.Prefix{
|
---|
| 32 | Name: serviceNick,
|
---|
| 33 | User: serviceNick,
|
---|
| 34 | Host: serviceNick,
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[150] | 37 | type serviceCommandSet map[string]*serviceCommand
|
---|
| 38 |
|
---|
[117] | 39 | type serviceCommand struct {
|
---|
[150] | 40 | usage string
|
---|
| 41 | desc string
|
---|
[677] | 42 | handle func(ctx context.Context, dc *downstreamConn, params []string) error
|
---|
[150] | 43 | children serviceCommandSet
|
---|
[328] | 44 | admin bool
|
---|
[117] | 45 | }
|
---|
| 46 |
|
---|
[218] | 47 | func sendServiceNOTICE(dc *downstreamConn, text string) {
|
---|
| 48 | dc.SendMessage(&irc.Message{
|
---|
[220] | 49 | Prefix: servicePrefix,
|
---|
[218] | 50 | Command: "NOTICE",
|
---|
| 51 | Params: []string{dc.nick, text},
|
---|
| 52 | })
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[117] | 55 | func sendServicePRIVMSG(dc *downstreamConn, text string) {
|
---|
| 56 | dc.SendMessage(&irc.Message{
|
---|
[220] | 57 | Prefix: servicePrefix,
|
---|
[117] | 58 | Command: "PRIVMSG",
|
---|
| 59 | Params: []string{dc.nick, text},
|
---|
| 60 | })
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[566] | 63 | func splitWords(s string) ([]string, error) {
|
---|
| 64 | var words []string
|
---|
| 65 | var lastWord strings.Builder
|
---|
| 66 | escape := false
|
---|
| 67 | prev := ' '
|
---|
| 68 | wordDelim := ' '
|
---|
| 69 |
|
---|
| 70 | for _, r := range s {
|
---|
| 71 | if escape {
|
---|
| 72 | // last char was a backslash, write the byte as-is.
|
---|
| 73 | lastWord.WriteRune(r)
|
---|
| 74 | escape = false
|
---|
| 75 | } else if r == '\\' {
|
---|
| 76 | escape = true
|
---|
| 77 | } else if wordDelim == ' ' && unicode.IsSpace(r) {
|
---|
| 78 | // end of last word
|
---|
| 79 | if !unicode.IsSpace(prev) {
|
---|
| 80 | words = append(words, lastWord.String())
|
---|
| 81 | lastWord.Reset()
|
---|
| 82 | }
|
---|
| 83 | } else if r == wordDelim {
|
---|
| 84 | // wordDelim is either " or ', switch back to
|
---|
| 85 | // space-delimited words.
|
---|
| 86 | wordDelim = ' '
|
---|
| 87 | } else if r == '"' || r == '\'' {
|
---|
| 88 | if wordDelim == ' ' {
|
---|
| 89 | // start of (double-)quoted word
|
---|
| 90 | wordDelim = r
|
---|
| 91 | } else {
|
---|
| 92 | // either wordDelim is " and r is ' or vice-versa
|
---|
| 93 | lastWord.WriteRune(r)
|
---|
| 94 | }
|
---|
| 95 | } else {
|
---|
| 96 | lastWord.WriteRune(r)
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | prev = r
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | if !unicode.IsSpace(prev) {
|
---|
| 103 | words = append(words, lastWord.String())
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | if wordDelim != ' ' {
|
---|
| 107 | return nil, fmt.Errorf("unterminated quoted string")
|
---|
| 108 | }
|
---|
| 109 | if escape {
|
---|
| 110 | return nil, fmt.Errorf("unterminated backslash sequence")
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | return words, nil
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[677] | 116 | func handleServicePRIVMSG(ctx context.Context, dc *downstreamConn, text string) {
|
---|
[566] | 117 | words, err := splitWords(text)
|
---|
[117] | 118 | if err != nil {
|
---|
[566] | 119 | sendServicePRIVMSG(dc, fmt.Sprintf(`error: failed to parse command: %v`, err))
|
---|
[117] | 120 | return
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[150] | 123 | cmd, params, err := serviceCommands.Get(words)
|
---|
| 124 | if err != nil {
|
---|
| 125 | sendServicePRIVMSG(dc, fmt.Sprintf(`error: %v (type "help" for a list of commands)`, err))
|
---|
[117] | 126 | return
|
---|
| 127 | }
|
---|
[328] | 128 | if cmd.admin && !dc.user.Admin {
|
---|
[625] | 129 | sendServicePRIVMSG(dc, "error: you must be an admin to use this command")
|
---|
[328] | 130 | return
|
---|
| 131 | }
|
---|
[117] | 132 |
|
---|
[334] | 133 | if cmd.handle == nil {
|
---|
| 134 | if len(cmd.children) > 0 {
|
---|
| 135 | var l []string
|
---|
[335] | 136 | appendServiceCommandSetHelp(cmd.children, words, dc.user.Admin, &l)
|
---|
[334] | 137 | sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", "))
|
---|
| 138 | } else {
|
---|
| 139 | // Pretend the command does not exist if it has neither children nor handler.
|
---|
| 140 | // This is obviously a bug but it is better to not die anyway.
|
---|
| 141 | dc.logger.Printf("command without handler and subcommands invoked:", words[0])
|
---|
| 142 | sendServicePRIVMSG(dc, fmt.Sprintf("command %q not found", words[0]))
|
---|
| 143 | }
|
---|
| 144 | return
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[677] | 147 | if err := cmd.handle(ctx, dc, params); err != nil {
|
---|
[117] | 148 | sendServicePRIVMSG(dc, fmt.Sprintf("error: %v", err))
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[150] | 152 | func (cmds serviceCommandSet) Get(params []string) (*serviceCommand, []string, error) {
|
---|
| 153 | if len(params) == 0 {
|
---|
| 154 | return nil, nil, fmt.Errorf("no command specified")
|
---|
| 155 | }
|
---|
[117] | 156 |
|
---|
[150] | 157 | name := params[0]
|
---|
| 158 | params = params[1:]
|
---|
| 159 |
|
---|
| 160 | cmd, ok := cmds[name]
|
---|
| 161 | if !ok {
|
---|
| 162 | for k := range cmds {
|
---|
| 163 | if !strings.HasPrefix(k, name) {
|
---|
| 164 | continue
|
---|
| 165 | }
|
---|
| 166 | if cmd != nil {
|
---|
| 167 | return nil, params, fmt.Errorf("command %q is ambiguous", name)
|
---|
| 168 | }
|
---|
| 169 | cmd = cmds[k]
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | if cmd == nil {
|
---|
| 173 | return nil, params, fmt.Errorf("command %q not found", name)
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | if len(params) == 0 || len(cmd.children) == 0 {
|
---|
| 177 | return cmd, params, nil
|
---|
| 178 | }
|
---|
| 179 | return cmd.children.Get(params)
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[339] | 182 | func (cmds serviceCommandSet) Names() []string {
|
---|
| 183 | l := make([]string, 0, len(cmds))
|
---|
| 184 | for name := range cmds {
|
---|
| 185 | l = append(l, name)
|
---|
| 186 | }
|
---|
| 187 | sort.Strings(l)
|
---|
| 188 | return l
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[150] | 191 | var serviceCommands serviceCommandSet
|
---|
| 192 |
|
---|
[117] | 193 | func init() {
|
---|
[150] | 194 | serviceCommands = serviceCommandSet{
|
---|
[117] | 195 | "help": {
|
---|
| 196 | usage: "[command]",
|
---|
| 197 | desc: "print help message",
|
---|
| 198 | handle: handleServiceHelp,
|
---|
| 199 | },
|
---|
[150] | 200 | "network": {
|
---|
| 201 | children: serviceCommandSet{
|
---|
| 202 | "create": {
|
---|
[542] | 203 | usage: "-addr <addr> [-name name] [-username username] [-pass pass] [-realname realname] [-nick nick] [-enabled enabled] [-connect-command command]...",
|
---|
[150] | 204 | desc: "add a new network",
|
---|
[325] | 205 | handle: handleServiceNetworkCreate,
|
---|
[150] | 206 | },
|
---|
[151] | 207 | "status": {
|
---|
| 208 | desc: "show a list of saved networks and their current status",
|
---|
| 209 | handle: handleServiceNetworkStatus,
|
---|
| 210 | },
|
---|
[313] | 211 | "update": {
|
---|
[542] | 212 | usage: "<name> [-addr addr] [-name name] [-username username] [-pass pass] [-realname realname] [-nick nick] [-enabled enabled] [-connect-command command]...",
|
---|
[315] | 213 | desc: "update a network",
|
---|
[313] | 214 | handle: handleServiceNetworkUpdate,
|
---|
| 215 | },
|
---|
[202] | 216 | "delete": {
|
---|
| 217 | usage: "<name>",
|
---|
| 218 | desc: "delete a network",
|
---|
| 219 | handle: handleServiceNetworkDelete,
|
---|
| 220 | },
|
---|
[577] | 221 | "quote": {
|
---|
| 222 | usage: "<name> <command>",
|
---|
| 223 | desc: "send a raw line to a network",
|
---|
| 224 | handle: handleServiceNetworkQuote,
|
---|
| 225 | },
|
---|
[150] | 226 | },
|
---|
[120] | 227 | },
|
---|
[307] | 228 | "certfp": {
|
---|
| 229 | children: serviceCommandSet{
|
---|
| 230 | "generate": {
|
---|
| 231 | usage: "[-key-type rsa|ecdsa|ed25519] [-bits N] <network name>",
|
---|
| 232 | desc: "generate a new self-signed certificate, defaults to using RSA-3072 key",
|
---|
[614] | 233 | handle: handleServiceCertFPGenerate,
|
---|
[307] | 234 | },
|
---|
| 235 | "fingerprint": {
|
---|
| 236 | usage: "<network name>",
|
---|
| 237 | desc: "show fingerprints of certificate associated with the network",
|
---|
[614] | 238 | handle: handleServiceCertFPFingerprints,
|
---|
[307] | 239 | },
|
---|
| 240 | },
|
---|
| 241 | },
|
---|
[363] | 242 | "sasl": {
|
---|
| 243 | children: serviceCommandSet{
|
---|
[730] | 244 | "status": {
|
---|
| 245 | usage: "<network name>",
|
---|
| 246 | desc: "show SASL status",
|
---|
| 247 | handle: handleServiceSaslStatus,
|
---|
| 248 | },
|
---|
[363] | 249 | "set-plain": {
|
---|
| 250 | usage: "<network name> <username> <password>",
|
---|
| 251 | desc: "set SASL PLAIN credentials",
|
---|
| 252 | handle: handleServiceSASLSetPlain,
|
---|
| 253 | },
|
---|
[364] | 254 | "reset": {
|
---|
| 255 | usage: "<network name>",
|
---|
| 256 | desc: "disable SASL authentication and remove stored credentials",
|
---|
| 257 | handle: handleServiceSASLReset,
|
---|
| 258 | },
|
---|
[363] | 259 | },
|
---|
| 260 | },
|
---|
[329] | 261 | "user": {
|
---|
| 262 | children: serviceCommandSet{
|
---|
| 263 | "create": {
|
---|
[568] | 264 | usage: "-username <username> -password <password> [-realname <realname>] [-admin]",
|
---|
[329] | 265 | desc: "create a new soju user",
|
---|
| 266 | handle: handleUserCreate,
|
---|
| 267 | admin: true,
|
---|
| 268 | },
|
---|
[568] | 269 | "update": {
|
---|
[570] | 270 | usage: "[-password <password>] [-realname <realname>]",
|
---|
[568] | 271 | desc: "update the current user",
|
---|
| 272 | handle: handleUserUpdate,
|
---|
| 273 | },
|
---|
[379] | 274 | "delete": {
|
---|
| 275 | usage: "<username>",
|
---|
| 276 | desc: "delete a user",
|
---|
| 277 | handle: handleUserDelete,
|
---|
| 278 | admin: true,
|
---|
| 279 | },
|
---|
[329] | 280 | },
|
---|
| 281 | },
|
---|
[436] | 282 | "channel": {
|
---|
| 283 | children: serviceCommandSet{
|
---|
[539] | 284 | "status": {
|
---|
| 285 | usage: "[-network name]",
|
---|
| 286 | desc: "show a list of saved channels and their current status",
|
---|
| 287 | handle: handleServiceChannelStatus,
|
---|
| 288 | },
|
---|
[436] | 289 | "update": {
|
---|
| 290 | usage: "<name> [-relay-detached <default|none|highlight|message>] [-reattach-on <default|none|highlight|message>] [-detach-after <duration>] [-detach-on <default|none|highlight|message>]",
|
---|
| 291 | desc: "update a channel",
|
---|
| 292 | handle: handleServiceChannelUpdate,
|
---|
| 293 | },
|
---|
| 294 | },
|
---|
| 295 | },
|
---|
[605] | 296 | "server": {
|
---|
| 297 | children: serviceCommandSet{
|
---|
| 298 | "status": {
|
---|
| 299 | desc: "show server statistics",
|
---|
| 300 | handle: handleServiceServerStatus,
|
---|
| 301 | admin: true,
|
---|
| 302 | },
|
---|
[615] | 303 | "notice": {
|
---|
| 304 | desc: "broadcast a notice to all connected bouncer users",
|
---|
| 305 | handle: handleServiceServerNotice,
|
---|
| 306 | admin: true,
|
---|
| 307 | },
|
---|
[605] | 308 | },
|
---|
| 309 | admin: true,
|
---|
| 310 | },
|
---|
[117] | 311 | }
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[328] | 314 | func appendServiceCommandSetHelp(cmds serviceCommandSet, prefix []string, admin bool, l *[]string) {
|
---|
[339] | 315 | for _, name := range cmds.Names() {
|
---|
| 316 | cmd := cmds[name]
|
---|
[328] | 317 | if cmd.admin && !admin {
|
---|
| 318 | continue
|
---|
| 319 | }
|
---|
[150] | 320 | words := append(prefix, name)
|
---|
| 321 | if len(cmd.children) == 0 {
|
---|
| 322 | s := strings.Join(words, " ")
|
---|
| 323 | *l = append(*l, s)
|
---|
| 324 | } else {
|
---|
[328] | 325 | appendServiceCommandSetHelp(cmd.children, words, admin, l)
|
---|
[150] | 326 | }
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[677] | 330 | func handleServiceHelp(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[117] | 331 | if len(params) > 0 {
|
---|
[150] | 332 | cmd, rest, err := serviceCommands.Get(params)
|
---|
| 333 | if err != nil {
|
---|
| 334 | return err
|
---|
[117] | 335 | }
|
---|
[150] | 336 | words := params[:len(params)-len(rest)]
|
---|
[117] | 337 |
|
---|
[150] | 338 | if len(cmd.children) > 0 {
|
---|
| 339 | var l []string
|
---|
[328] | 340 | appendServiceCommandSetHelp(cmd.children, words, dc.user.Admin, &l)
|
---|
[150] | 341 | sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", "))
|
---|
| 342 | } else {
|
---|
| 343 | text := strings.Join(words, " ")
|
---|
| 344 | if cmd.usage != "" {
|
---|
| 345 | text += " " + cmd.usage
|
---|
| 346 | }
|
---|
| 347 | text += ": " + cmd.desc
|
---|
| 348 |
|
---|
| 349 | sendServicePRIVMSG(dc, text)
|
---|
[117] | 350 | }
|
---|
| 351 | } else {
|
---|
| 352 | var l []string
|
---|
[328] | 353 | appendServiceCommandSetHelp(serviceCommands, nil, dc.user.Admin, &l)
|
---|
[117] | 354 | sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", "))
|
---|
| 355 | }
|
---|
| 356 | return nil
|
---|
| 357 | }
|
---|
[120] | 358 |
|
---|
[202] | 359 | func newFlagSet() *flag.FlagSet {
|
---|
[120] | 360 | fs := flag.NewFlagSet("", flag.ContinueOnError)
|
---|
| 361 | fs.SetOutput(ioutil.Discard)
|
---|
[202] | 362 | return fs
|
---|
| 363 | }
|
---|
| 364 |
|
---|
[313] | 365 | type stringSliceFlag []string
|
---|
[263] | 366 |
|
---|
[313] | 367 | func (v *stringSliceFlag) String() string {
|
---|
[263] | 368 | return fmt.Sprint([]string(*v))
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[313] | 371 | func (v *stringSliceFlag) Set(s string) error {
|
---|
[263] | 372 | *v = append(*v, s)
|
---|
| 373 | return nil
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[313] | 376 | // stringPtrFlag is a flag value populating a string pointer. This allows to
|
---|
| 377 | // disambiguate between a flag that hasn't been set and a flag that has been
|
---|
| 378 | // set to an empty string.
|
---|
| 379 | type stringPtrFlag struct {
|
---|
| 380 | ptr **string
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | func (f stringPtrFlag) String() string {
|
---|
[333] | 384 | if f.ptr == nil || *f.ptr == nil {
|
---|
[313] | 385 | return ""
|
---|
| 386 | }
|
---|
| 387 | return **f.ptr
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | func (f stringPtrFlag) Set(s string) error {
|
---|
| 391 | *f.ptr = &s
|
---|
| 392 | return nil
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[542] | 395 | type boolPtrFlag struct {
|
---|
| 396 | ptr **bool
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | func (f boolPtrFlag) String() string {
|
---|
| 400 | if f.ptr == nil || *f.ptr == nil {
|
---|
| 401 | return "<nil>"
|
---|
| 402 | }
|
---|
| 403 | return strconv.FormatBool(**f.ptr)
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | func (f boolPtrFlag) Set(s string) error {
|
---|
| 407 | v, err := strconv.ParseBool(s)
|
---|
| 408 | if err != nil {
|
---|
| 409 | return err
|
---|
| 410 | }
|
---|
| 411 | *f.ptr = &v
|
---|
| 412 | return nil
|
---|
| 413 | }
|
---|
| 414 |
|
---|
[313] | 415 | type networkFlagSet struct {
|
---|
| 416 | *flag.FlagSet
|
---|
| 417 | Addr, Name, Nick, Username, Pass, Realname *string
|
---|
[542] | 418 | Enabled *bool
|
---|
[315] | 419 | ConnectCommands []string
|
---|
[313] | 420 | }
|
---|
| 421 |
|
---|
| 422 | func newNetworkFlagSet() *networkFlagSet {
|
---|
| 423 | fs := &networkFlagSet{FlagSet: newFlagSet()}
|
---|
| 424 | fs.Var(stringPtrFlag{&fs.Addr}, "addr", "")
|
---|
| 425 | fs.Var(stringPtrFlag{&fs.Name}, "name", "")
|
---|
| 426 | fs.Var(stringPtrFlag{&fs.Nick}, "nick", "")
|
---|
| 427 | fs.Var(stringPtrFlag{&fs.Username}, "username", "")
|
---|
| 428 | fs.Var(stringPtrFlag{&fs.Pass}, "pass", "")
|
---|
| 429 | fs.Var(stringPtrFlag{&fs.Realname}, "realname", "")
|
---|
[542] | 430 | fs.Var(boolPtrFlag{&fs.Enabled}, "enabled", "")
|
---|
[313] | 431 | fs.Var((*stringSliceFlag)(&fs.ConnectCommands), "connect-command", "")
|
---|
| 432 | return fs
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | func (fs *networkFlagSet) update(network *Network) error {
|
---|
| 436 | if fs.Addr != nil {
|
---|
| 437 | if addrParts := strings.SplitN(*fs.Addr, "://", 2); len(addrParts) == 2 {
|
---|
| 438 | scheme := addrParts[0]
|
---|
| 439 | switch scheme {
|
---|
[358] | 440 | case "ircs", "irc+insecure", "unix":
|
---|
[313] | 441 | default:
|
---|
[358] | 442 | return fmt.Errorf("unknown scheme %q (supported schemes: ircs, irc+insecure, unix)", scheme)
|
---|
[313] | 443 | }
|
---|
| 444 | }
|
---|
| 445 | network.Addr = *fs.Addr
|
---|
| 446 | }
|
---|
| 447 | if fs.Name != nil {
|
---|
| 448 | network.Name = *fs.Name
|
---|
| 449 | }
|
---|
| 450 | if fs.Nick != nil {
|
---|
| 451 | network.Nick = *fs.Nick
|
---|
| 452 | }
|
---|
| 453 | if fs.Username != nil {
|
---|
| 454 | network.Username = *fs.Username
|
---|
| 455 | }
|
---|
| 456 | if fs.Pass != nil {
|
---|
| 457 | network.Pass = *fs.Pass
|
---|
| 458 | }
|
---|
| 459 | if fs.Realname != nil {
|
---|
| 460 | network.Realname = *fs.Realname
|
---|
| 461 | }
|
---|
[542] | 462 | if fs.Enabled != nil {
|
---|
| 463 | network.Enabled = *fs.Enabled
|
---|
| 464 | }
|
---|
[313] | 465 | if fs.ConnectCommands != nil {
|
---|
| 466 | if len(fs.ConnectCommands) == 1 && fs.ConnectCommands[0] == "" {
|
---|
| 467 | network.ConnectCommands = nil
|
---|
| 468 | } else {
|
---|
| 469 | for _, command := range fs.ConnectCommands {
|
---|
| 470 | _, err := irc.ParseMessage(command)
|
---|
| 471 | if err != nil {
|
---|
| 472 | return fmt.Errorf("flag -connect-command must be a valid raw irc command string: %q: %v", command, err)
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
| 475 | network.ConnectCommands = fs.ConnectCommands
|
---|
| 476 | }
|
---|
| 477 | }
|
---|
| 478 | return nil
|
---|
| 479 | }
|
---|
| 480 |
|
---|
[677] | 481 | func handleServiceNetworkCreate(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[313] | 482 | fs := newNetworkFlagSet()
|
---|
[120] | 483 | if err := fs.Parse(params); err != nil {
|
---|
| 484 | return err
|
---|
| 485 | }
|
---|
[313] | 486 | if fs.Addr == nil {
|
---|
[150] | 487 | return fmt.Errorf("flag -addr is required")
|
---|
[120] | 488 | }
|
---|
| 489 |
|
---|
[313] | 490 | record := &Network{
|
---|
[542] | 491 | Addr: *fs.Addr,
|
---|
| 492 | Enabled: true,
|
---|
[269] | 493 | }
|
---|
[313] | 494 | if err := fs.update(record); err != nil {
|
---|
| 495 | return err
|
---|
[263] | 496 | }
|
---|
| 497 |
|
---|
[677] | 498 | network, err := dc.user.createNetwork(ctx, record)
|
---|
[120] | 499 | if err != nil {
|
---|
| 500 | return fmt.Errorf("could not create network: %v", err)
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[202] | 503 | sendServicePRIVMSG(dc, fmt.Sprintf("created network %q", network.GetName()))
|
---|
[120] | 504 | return nil
|
---|
| 505 | }
|
---|
[151] | 506 |
|
---|
[677] | 507 | func handleServiceNetworkStatus(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[546] | 508 | n := 0
|
---|
[151] | 509 | dc.user.forEachNetwork(func(net *network) {
|
---|
| 510 | var statuses []string
|
---|
| 511 | var details string
|
---|
[279] | 512 | if uc := net.conn; uc != nil {
|
---|
[271] | 513 | if dc.nick != uc.nick {
|
---|
| 514 | statuses = append(statuses, "connected as "+uc.nick)
|
---|
| 515 | } else {
|
---|
| 516 | statuses = append(statuses, "connected")
|
---|
| 517 | }
|
---|
[478] | 518 | details = fmt.Sprintf("%v channels", uc.channels.Len())
|
---|
[542] | 519 | } else if !net.Enabled {
|
---|
| 520 | statuses = append(statuses, "disabled")
|
---|
[151] | 521 | } else {
|
---|
| 522 | statuses = append(statuses, "disconnected")
|
---|
[219] | 523 | if net.lastError != nil {
|
---|
| 524 | details = net.lastError.Error()
|
---|
| 525 | }
|
---|
[151] | 526 | }
|
---|
| 527 |
|
---|
| 528 | if net == dc.network {
|
---|
| 529 | statuses = append(statuses, "current")
|
---|
| 530 | }
|
---|
| 531 |
|
---|
[224] | 532 | name := net.GetName()
|
---|
| 533 | if name != net.Addr {
|
---|
| 534 | name = fmt.Sprintf("%v (%v)", name, net.Addr)
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | s := fmt.Sprintf("%v [%v]", name, strings.Join(statuses, ", "))
|
---|
[151] | 538 | if details != "" {
|
---|
| 539 | s += ": " + details
|
---|
| 540 | }
|
---|
| 541 | sendServicePRIVMSG(dc, s)
|
---|
[546] | 542 |
|
---|
| 543 | n++
|
---|
[151] | 544 | })
|
---|
[546] | 545 |
|
---|
| 546 | if n == 0 {
|
---|
| 547 | sendServicePRIVMSG(dc, `No network configured, add one with "network create".`)
|
---|
| 548 | }
|
---|
| 549 |
|
---|
[151] | 550 | return nil
|
---|
| 551 | }
|
---|
[202] | 552 |
|
---|
[677] | 553 | func handleServiceNetworkUpdate(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[313] | 554 | if len(params) < 1 {
|
---|
[436] | 555 | return fmt.Errorf("expected at least one argument")
|
---|
[313] | 556 | }
|
---|
| 557 |
|
---|
| 558 | fs := newNetworkFlagSet()
|
---|
| 559 | if err := fs.Parse(params[1:]); err != nil {
|
---|
| 560 | return err
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | net := dc.user.getNetwork(params[0])
|
---|
| 564 | if net == nil {
|
---|
| 565 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | record := net.Network // copy network record because we'll mutate it
|
---|
| 569 | if err := fs.update(&record); err != nil {
|
---|
| 570 | return err
|
---|
| 571 | }
|
---|
| 572 |
|
---|
[677] | 573 | network, err := dc.user.updateNetwork(ctx, &record)
|
---|
[313] | 574 | if err != nil {
|
---|
| 575 | return fmt.Errorf("could not update network: %v", err)
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | sendServicePRIVMSG(dc, fmt.Sprintf("updated network %q", network.GetName()))
|
---|
| 579 | return nil
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[677] | 582 | func handleServiceNetworkDelete(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[202] | 583 | if len(params) != 1 {
|
---|
| 584 | return fmt.Errorf("expected exactly one argument")
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | net := dc.user.getNetwork(params[0])
|
---|
| 588 | if net == nil {
|
---|
| 589 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 590 | }
|
---|
| 591 |
|
---|
[677] | 592 | if err := dc.user.deleteNetwork(ctx, net.ID); err != nil {
|
---|
[202] | 593 | return err
|
---|
| 594 | }
|
---|
| 595 |
|
---|
| 596 | sendServicePRIVMSG(dc, fmt.Sprintf("deleted network %q", net.GetName()))
|
---|
| 597 | return nil
|
---|
| 598 | }
|
---|
[252] | 599 |
|
---|
[677] | 600 | func handleServiceNetworkQuote(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[577] | 601 | if len(params) != 2 {
|
---|
| 602 | return fmt.Errorf("expected exactly two arguments")
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | net := dc.user.getNetwork(params[0])
|
---|
| 606 | if net == nil {
|
---|
| 607 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | uc := net.conn
|
---|
| 611 | if uc == nil {
|
---|
| 612 | return fmt.Errorf("network %q is not currently connected", params[0])
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 | m, err := irc.ParseMessage(params[1])
|
---|
| 616 | if err != nil {
|
---|
| 617 | return fmt.Errorf("failed to parse command %q: %v", params[1], err)
|
---|
| 618 | }
|
---|
[757] | 619 | uc.SendMessage(ctx, m)
|
---|
[577] | 620 |
|
---|
| 621 | sendServicePRIVMSG(dc, fmt.Sprintf("sent command to %q", net.GetName()))
|
---|
| 622 | return nil
|
---|
| 623 | }
|
---|
| 624 |
|
---|
[614] | 625 | func sendCertfpFingerprints(dc *downstreamConn, cert []byte) {
|
---|
| 626 | sha1Sum := sha1.Sum(cert)
|
---|
| 627 | sendServicePRIVMSG(dc, "SHA-1 fingerprint: "+hex.EncodeToString(sha1Sum[:]))
|
---|
| 628 | sha256Sum := sha256.Sum256(cert)
|
---|
| 629 | sendServicePRIVMSG(dc, "SHA-256 fingerprint: "+hex.EncodeToString(sha256Sum[:]))
|
---|
| 630 | sha512Sum := sha512.Sum512(cert)
|
---|
| 631 | sendServicePRIVMSG(dc, "SHA-512 fingerprint: "+hex.EncodeToString(sha512Sum[:]))
|
---|
| 632 | }
|
---|
| 633 |
|
---|
[677] | 634 | func handleServiceCertFPGenerate(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[325] | 635 | fs := newFlagSet()
|
---|
| 636 | keyType := fs.String("key-type", "rsa", "key type to generate (rsa, ecdsa, ed25519)")
|
---|
| 637 | bits := fs.Int("bits", 3072, "size of key to generate, meaningful only for RSA")
|
---|
| 638 |
|
---|
| 639 | if err := fs.Parse(params); err != nil {
|
---|
| 640 | return err
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | if len(fs.Args()) != 1 {
|
---|
| 644 | return errors.New("exactly one argument is required")
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | net := dc.user.getNetwork(fs.Arg(0))
|
---|
| 648 | if net == nil {
|
---|
| 649 | return fmt.Errorf("unknown network %q", fs.Arg(0))
|
---|
| 650 | }
|
---|
| 651 |
|
---|
[614] | 652 | if *bits <= 0 || *bits > maxRSABits {
|
---|
| 653 | return fmt.Errorf("invalid value for -bits")
|
---|
[325] | 654 | }
|
---|
| 655 |
|
---|
[614] | 656 | privKey, cert, err := generateCertFP(*keyType, *bits)
|
---|
[325] | 657 | if err != nil {
|
---|
| 658 | return err
|
---|
| 659 | }
|
---|
| 660 |
|
---|
[614] | 661 | net.SASL.External.CertBlob = cert
|
---|
| 662 | net.SASL.External.PrivKeyBlob = privKey
|
---|
[325] | 663 | net.SASL.Mechanism = "EXTERNAL"
|
---|
| 664 |
|
---|
[677] | 665 | if err := dc.srv.db.StoreNetwork(ctx, dc.user.ID, &net.Network); err != nil {
|
---|
[325] | 666 | return err
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | sendServicePRIVMSG(dc, "certificate generated")
|
---|
[614] | 670 | sendCertfpFingerprints(dc, cert)
|
---|
[325] | 671 | return nil
|
---|
| 672 | }
|
---|
| 673 |
|
---|
[677] | 674 | func handleServiceCertFPFingerprints(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[325] | 675 | if len(params) != 1 {
|
---|
| 676 | return fmt.Errorf("expected exactly one argument")
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | net := dc.user.getNetwork(params[0])
|
---|
| 680 | if net == nil {
|
---|
| 681 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 682 | }
|
---|
| 683 |
|
---|
[614] | 684 | if net.SASL.Mechanism != "EXTERNAL" {
|
---|
| 685 | return fmt.Errorf("CertFP not set up")
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 | sendCertfpFingerprints(dc, net.SASL.External.CertBlob)
|
---|
[325] | 689 | return nil
|
---|
| 690 | }
|
---|
| 691 |
|
---|
[730] | 692 | func handleServiceSaslStatus(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
| 693 | if len(params) != 1 {
|
---|
| 694 | return fmt.Errorf("expected exactly one argument")
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | net := dc.user.getNetwork(params[0])
|
---|
| 698 | if net == nil {
|
---|
| 699 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 700 | }
|
---|
| 701 |
|
---|
| 702 | switch net.SASL.Mechanism {
|
---|
| 703 | case "PLAIN":
|
---|
| 704 | sendServicePRIVMSG(dc, fmt.Sprintf("SASL PLAIN enabled with username %q", net.SASL.Plain.Username))
|
---|
| 705 | case "EXTERNAL":
|
---|
| 706 | sendServicePRIVMSG(dc, "SASL EXTERNAL (CertFP) enabled")
|
---|
| 707 | case "":
|
---|
| 708 | sendServicePRIVMSG(dc, "SASL is disabled")
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 | if uc := net.conn; uc != nil {
|
---|
| 712 | if uc.account != "" {
|
---|
| 713 | sendServicePRIVMSG(dc, fmt.Sprintf("Authenticated on upstream network with account %q", uc.account))
|
---|
| 714 | } else {
|
---|
| 715 | sendServicePRIVMSG(dc, "Unauthenticated on upstream network")
|
---|
| 716 | }
|
---|
| 717 | } else {
|
---|
| 718 | sendServicePRIVMSG(dc, "Disconnected from upstream network")
|
---|
| 719 | }
|
---|
| 720 |
|
---|
| 721 | return nil
|
---|
| 722 | }
|
---|
| 723 |
|
---|
[677] | 724 | func handleServiceSASLSetPlain(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[364] | 725 | if len(params) != 3 {
|
---|
| 726 | return fmt.Errorf("expected exactly 3 arguments")
|
---|
[325] | 727 | }
|
---|
| 728 |
|
---|
| 729 | net := dc.user.getNetwork(params[0])
|
---|
| 730 | if net == nil {
|
---|
| 731 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 732 | }
|
---|
| 733 |
|
---|
[364] | 734 | net.SASL.Plain.Username = params[1]
|
---|
| 735 | net.SASL.Plain.Password = params[2]
|
---|
| 736 | net.SASL.Mechanism = "PLAIN"
|
---|
[325] | 737 |
|
---|
[677] | 738 | if err := dc.srv.db.StoreNetwork(ctx, dc.user.ID, &net.Network); err != nil {
|
---|
[325] | 739 | return err
|
---|
| 740 | }
|
---|
| 741 |
|
---|
[364] | 742 | sendServicePRIVMSG(dc, "credentials saved")
|
---|
[325] | 743 | return nil
|
---|
| 744 | }
|
---|
| 745 |
|
---|
[677] | 746 | func handleServiceSASLReset(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[364] | 747 | if len(params) != 1 {
|
---|
| 748 | return fmt.Errorf("expected exactly one argument")
|
---|
[363] | 749 | }
|
---|
| 750 |
|
---|
| 751 | net := dc.user.getNetwork(params[0])
|
---|
| 752 | if net == nil {
|
---|
| 753 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 754 | }
|
---|
| 755 |
|
---|
[364] | 756 | net.SASL.Plain.Username = ""
|
---|
| 757 | net.SASL.Plain.Password = ""
|
---|
| 758 | net.SASL.External.CertBlob = nil
|
---|
| 759 | net.SASL.External.PrivKeyBlob = nil
|
---|
| 760 | net.SASL.Mechanism = ""
|
---|
[363] | 761 |
|
---|
[677] | 762 | if err := dc.srv.db.StoreNetwork(ctx, dc.user.ID, &net.Network); err != nil {
|
---|
[363] | 763 | return err
|
---|
| 764 | }
|
---|
| 765 |
|
---|
[364] | 766 | sendServicePRIVMSG(dc, "credentials reset")
|
---|
[363] | 767 | return nil
|
---|
| 768 | }
|
---|
| 769 |
|
---|
[677] | 770 | func handleUserCreate(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[329] | 771 | fs := newFlagSet()
|
---|
| 772 | username := fs.String("username", "", "")
|
---|
| 773 | password := fs.String("password", "", "")
|
---|
[568] | 774 | realname := fs.String("realname", "", "")
|
---|
[329] | 775 | admin := fs.Bool("admin", false, "")
|
---|
| 776 |
|
---|
| 777 | if err := fs.Parse(params); err != nil {
|
---|
| 778 | return err
|
---|
| 779 | }
|
---|
| 780 | if *username == "" {
|
---|
| 781 | return fmt.Errorf("flag -username is required")
|
---|
| 782 | }
|
---|
| 783 | if *password == "" {
|
---|
| 784 | return fmt.Errorf("flag -password is required")
|
---|
| 785 | }
|
---|
| 786 |
|
---|
| 787 | hashed, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)
|
---|
| 788 | if err != nil {
|
---|
| 789 | return fmt.Errorf("failed to hash password: %v", err)
|
---|
| 790 | }
|
---|
| 791 |
|
---|
| 792 | user := &User{
|
---|
| 793 | Username: *username,
|
---|
| 794 | Password: string(hashed),
|
---|
[568] | 795 | Realname: *realname,
|
---|
[329] | 796 | Admin: *admin,
|
---|
| 797 | }
|
---|
[680] | 798 | if _, err := dc.srv.createUser(ctx, user); err != nil {
|
---|
[329] | 799 | return fmt.Errorf("could not create user: %v", err)
|
---|
| 800 | }
|
---|
| 801 |
|
---|
| 802 | sendServicePRIVMSG(dc, fmt.Sprintf("created user %q", *username))
|
---|
| 803 | return nil
|
---|
| 804 | }
|
---|
[379] | 805 |
|
---|
[625] | 806 | func popArg(params []string) (string, []string) {
|
---|
| 807 | if len(params) > 0 && !strings.HasPrefix(params[0], "-") {
|
---|
| 808 | return params[0], params[1:]
|
---|
| 809 | }
|
---|
| 810 | return "", params
|
---|
| 811 | }
|
---|
| 812 |
|
---|
[677] | 813 | func handleUserUpdate(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[570] | 814 | var password, realname *string
|
---|
[625] | 815 | var admin *bool
|
---|
[568] | 816 | fs := newFlagSet()
|
---|
[570] | 817 | fs.Var(stringPtrFlag{&password}, "password", "")
|
---|
[569] | 818 | fs.Var(stringPtrFlag{&realname}, "realname", "")
|
---|
[625] | 819 | fs.Var(boolPtrFlag{&admin}, "admin", "")
|
---|
[568] | 820 |
|
---|
[625] | 821 | username, params := popArg(params)
|
---|
[568] | 822 | if err := fs.Parse(params); err != nil {
|
---|
| 823 | return err
|
---|
| 824 | }
|
---|
[625] | 825 | if len(fs.Args()) > 0 {
|
---|
| 826 | return fmt.Errorf("unexpected argument")
|
---|
| 827 | }
|
---|
[568] | 828 |
|
---|
[625] | 829 | var hashed *string
|
---|
[570] | 830 | if password != nil {
|
---|
[625] | 831 | hashedBytes, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)
|
---|
[570] | 832 | if err != nil {
|
---|
| 833 | return fmt.Errorf("failed to hash password: %v", err)
|
---|
| 834 | }
|
---|
[625] | 835 | hashedStr := string(hashedBytes)
|
---|
| 836 | hashed = &hashedStr
|
---|
[570] | 837 | }
|
---|
[568] | 838 |
|
---|
[625] | 839 | if username != "" && username != dc.user.Username {
|
---|
| 840 | if !dc.user.Admin {
|
---|
| 841 | return fmt.Errorf("you must be an admin to update other users")
|
---|
| 842 | }
|
---|
| 843 | if realname != nil {
|
---|
| 844 | return fmt.Errorf("cannot update -realname of other user")
|
---|
| 845 | }
|
---|
| 846 |
|
---|
| 847 | u := dc.srv.getUser(username)
|
---|
| 848 | if u == nil {
|
---|
| 849 | return fmt.Errorf("unknown username %q", username)
|
---|
| 850 | }
|
---|
| 851 |
|
---|
| 852 | done := make(chan error, 1)
|
---|
[679] | 853 | event := eventUserUpdate{
|
---|
[625] | 854 | password: hashed,
|
---|
| 855 | admin: admin,
|
---|
| 856 | done: done,
|
---|
| 857 | }
|
---|
[679] | 858 | select {
|
---|
| 859 | case <-ctx.Done():
|
---|
| 860 | return ctx.Err()
|
---|
| 861 | case u.events <- event:
|
---|
| 862 | }
|
---|
| 863 | // TODO: send context to the other side
|
---|
[625] | 864 | if err := <-done; err != nil {
|
---|
| 865 | return err
|
---|
| 866 | }
|
---|
| 867 |
|
---|
| 868 | sendServicePRIVMSG(dc, fmt.Sprintf("updated user %q", username))
|
---|
| 869 | } else {
|
---|
| 870 | // copy the user record because we'll mutate it
|
---|
| 871 | record := dc.user.User
|
---|
| 872 |
|
---|
| 873 | if hashed != nil {
|
---|
| 874 | record.Password = *hashed
|
---|
| 875 | }
|
---|
| 876 | if realname != nil {
|
---|
| 877 | record.Realname = *realname
|
---|
| 878 | }
|
---|
| 879 | if admin != nil {
|
---|
| 880 | return fmt.Errorf("cannot update -admin of own user")
|
---|
| 881 | }
|
---|
| 882 |
|
---|
[677] | 883 | if err := dc.user.updateUser(ctx, &record); err != nil {
|
---|
[625] | 884 | return err
|
---|
| 885 | }
|
---|
| 886 |
|
---|
| 887 | sendServicePRIVMSG(dc, fmt.Sprintf("updated user %q", dc.user.Username))
|
---|
[572] | 888 | }
|
---|
| 889 |
|
---|
[568] | 890 | return nil
|
---|
| 891 | }
|
---|
| 892 |
|
---|
[677] | 893 | func handleUserDelete(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[379] | 894 | if len(params) != 1 {
|
---|
| 895 | return fmt.Errorf("expected exactly one argument")
|
---|
| 896 | }
|
---|
| 897 | username := params[0]
|
---|
| 898 |
|
---|
| 899 | u := dc.srv.getUser(username)
|
---|
| 900 | if u == nil {
|
---|
| 901 | return fmt.Errorf("unknown username %q", username)
|
---|
| 902 | }
|
---|
| 903 |
|
---|
| 904 | u.stop()
|
---|
| 905 |
|
---|
[677] | 906 | if err := dc.srv.db.DeleteUser(ctx, u.ID); err != nil {
|
---|
[379] | 907 | return fmt.Errorf("failed to delete user: %v", err)
|
---|
| 908 | }
|
---|
| 909 |
|
---|
| 910 | sendServicePRIVMSG(dc, fmt.Sprintf("deleted user %q", username))
|
---|
| 911 | return nil
|
---|
| 912 | }
|
---|
[436] | 913 |
|
---|
[677] | 914 | func handleServiceChannelStatus(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[539] | 915 | var defaultNetworkName string
|
---|
| 916 | if dc.network != nil {
|
---|
| 917 | defaultNetworkName = dc.network.GetName()
|
---|
| 918 | }
|
---|
| 919 |
|
---|
| 920 | fs := newFlagSet()
|
---|
| 921 | networkName := fs.String("network", defaultNetworkName, "")
|
---|
| 922 |
|
---|
| 923 | if err := fs.Parse(params); err != nil {
|
---|
| 924 | return err
|
---|
| 925 | }
|
---|
| 926 |
|
---|
[546] | 927 | n := 0
|
---|
| 928 |
|
---|
[539] | 929 | sendNetwork := func(net *network) {
|
---|
[573] | 930 | var channels []*Channel
|
---|
[539] | 931 | for _, entry := range net.channels.innerMap {
|
---|
[573] | 932 | channels = append(channels, entry.value.(*Channel))
|
---|
| 933 | }
|
---|
[539] | 934 |
|
---|
[573] | 935 | sort.Slice(channels, func(i, j int) bool {
|
---|
| 936 | return strings.ReplaceAll(channels[i].Name, "#", "") <
|
---|
| 937 | strings.ReplaceAll(channels[j].Name, "#", "")
|
---|
| 938 | })
|
---|
| 939 |
|
---|
| 940 | for _, ch := range channels {
|
---|
[539] | 941 | var uch *upstreamChannel
|
---|
| 942 | if net.conn != nil {
|
---|
| 943 | uch = net.conn.channels.Value(ch.Name)
|
---|
| 944 | }
|
---|
| 945 |
|
---|
| 946 | name := ch.Name
|
---|
| 947 | if *networkName == "" {
|
---|
| 948 | name += "/" + net.GetName()
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | var status string
|
---|
| 952 | if uch != nil {
|
---|
| 953 | status = "joined"
|
---|
| 954 | } else if net.conn != nil {
|
---|
| 955 | status = "parted"
|
---|
| 956 | } else {
|
---|
| 957 | status = "disconnected"
|
---|
| 958 | }
|
---|
| 959 |
|
---|
| 960 | if ch.Detached {
|
---|
| 961 | status += ", detached"
|
---|
| 962 | }
|
---|
| 963 |
|
---|
| 964 | s := fmt.Sprintf("%v [%v]", name, status)
|
---|
| 965 | sendServicePRIVMSG(dc, s)
|
---|
[546] | 966 |
|
---|
| 967 | n++
|
---|
[539] | 968 | }
|
---|
| 969 | }
|
---|
| 970 |
|
---|
| 971 | if *networkName == "" {
|
---|
| 972 | dc.user.forEachNetwork(sendNetwork)
|
---|
| 973 | } else {
|
---|
| 974 | net := dc.user.getNetwork(*networkName)
|
---|
| 975 | if net == nil {
|
---|
| 976 | return fmt.Errorf("unknown network %q", *networkName)
|
---|
| 977 | }
|
---|
| 978 | sendNetwork(net)
|
---|
| 979 | }
|
---|
| 980 |
|
---|
[546] | 981 | if n == 0 {
|
---|
| 982 | sendServicePRIVMSG(dc, "No channel configured.")
|
---|
| 983 | }
|
---|
| 984 |
|
---|
[539] | 985 | return nil
|
---|
| 986 | }
|
---|
| 987 |
|
---|
[436] | 988 | type channelFlagSet struct {
|
---|
| 989 | *flag.FlagSet
|
---|
| 990 | RelayDetached, ReattachOn, DetachAfter, DetachOn *string
|
---|
| 991 | }
|
---|
| 992 |
|
---|
| 993 | func newChannelFlagSet() *channelFlagSet {
|
---|
| 994 | fs := &channelFlagSet{FlagSet: newFlagSet()}
|
---|
| 995 | fs.Var(stringPtrFlag{&fs.RelayDetached}, "relay-detached", "")
|
---|
| 996 | fs.Var(stringPtrFlag{&fs.ReattachOn}, "reattach-on", "")
|
---|
| 997 | fs.Var(stringPtrFlag{&fs.DetachAfter}, "detach-after", "")
|
---|
| 998 | fs.Var(stringPtrFlag{&fs.DetachOn}, "detach-on", "")
|
---|
| 999 | return fs
|
---|
| 1000 | }
|
---|
| 1001 |
|
---|
| 1002 | func (fs *channelFlagSet) update(channel *Channel) error {
|
---|
| 1003 | if fs.RelayDetached != nil {
|
---|
| 1004 | filter, err := parseFilter(*fs.RelayDetached)
|
---|
| 1005 | if err != nil {
|
---|
| 1006 | return err
|
---|
| 1007 | }
|
---|
| 1008 | channel.RelayDetached = filter
|
---|
| 1009 | }
|
---|
| 1010 | if fs.ReattachOn != nil {
|
---|
| 1011 | filter, err := parseFilter(*fs.ReattachOn)
|
---|
| 1012 | if err != nil {
|
---|
| 1013 | return err
|
---|
| 1014 | }
|
---|
| 1015 | channel.ReattachOn = filter
|
---|
| 1016 | }
|
---|
| 1017 | if fs.DetachAfter != nil {
|
---|
| 1018 | dur, err := time.ParseDuration(*fs.DetachAfter)
|
---|
| 1019 | if err != nil || dur < 0 {
|
---|
| 1020 | return fmt.Errorf("unknown duration for -detach-after %q (duration format: 0, 300s, 22h30m, ...)", *fs.DetachAfter)
|
---|
| 1021 | }
|
---|
| 1022 | channel.DetachAfter = dur
|
---|
| 1023 | }
|
---|
| 1024 | if fs.DetachOn != nil {
|
---|
| 1025 | filter, err := parseFilter(*fs.DetachOn)
|
---|
| 1026 | if err != nil {
|
---|
| 1027 | return err
|
---|
| 1028 | }
|
---|
| 1029 | channel.DetachOn = filter
|
---|
| 1030 | }
|
---|
| 1031 | return nil
|
---|
| 1032 | }
|
---|
| 1033 |
|
---|
[677] | 1034 | func handleServiceChannelUpdate(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[436] | 1035 | if len(params) < 1 {
|
---|
| 1036 | return fmt.Errorf("expected at least one argument")
|
---|
| 1037 | }
|
---|
| 1038 | name := params[0]
|
---|
| 1039 |
|
---|
| 1040 | fs := newChannelFlagSet()
|
---|
| 1041 | if err := fs.Parse(params[1:]); err != nil {
|
---|
| 1042 | return err
|
---|
| 1043 | }
|
---|
| 1044 |
|
---|
| 1045 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1046 | if err != nil {
|
---|
| 1047 | return fmt.Errorf("unknown channel %q", name)
|
---|
| 1048 | }
|
---|
| 1049 |
|
---|
[478] | 1050 | ch := uc.network.channels.Value(upstreamName)
|
---|
| 1051 | if ch == nil {
|
---|
[436] | 1052 | return fmt.Errorf("unknown channel %q", name)
|
---|
| 1053 | }
|
---|
| 1054 |
|
---|
| 1055 | if err := fs.update(ch); err != nil {
|
---|
| 1056 | return err
|
---|
| 1057 | }
|
---|
| 1058 |
|
---|
| 1059 | uc.updateChannelAutoDetach(upstreamName)
|
---|
| 1060 |
|
---|
[677] | 1061 | if err := dc.srv.db.StoreChannel(ctx, uc.network.ID, ch); err != nil {
|
---|
[436] | 1062 | return fmt.Errorf("failed to update channel: %v", err)
|
---|
| 1063 | }
|
---|
| 1064 |
|
---|
| 1065 | sendServicePRIVMSG(dc, fmt.Sprintf("updated channel %q", name))
|
---|
| 1066 | return nil
|
---|
| 1067 | }
|
---|
[605] | 1068 |
|
---|
[677] | 1069 | func handleServiceServerStatus(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
| 1070 | dbStats, err := dc.user.srv.db.Stats(ctx)
|
---|
[607] | 1071 | if err != nil {
|
---|
| 1072 | return err
|
---|
| 1073 | }
|
---|
| 1074 | serverStats := dc.user.srv.Stats()
|
---|
[710] | 1075 | sendServicePRIVMSG(dc, fmt.Sprintf("%v/%v users, %v downstreams, %v upstreams, %v networks, %v channels", serverStats.Users, dbStats.Users, serverStats.Downstreams, serverStats.Upstreams, dbStats.Networks, dbStats.Channels))
|
---|
[605] | 1076 | return nil
|
---|
| 1077 | }
|
---|
[615] | 1078 |
|
---|
[677] | 1079 | func handleServiceServerNotice(ctx context.Context, dc *downstreamConn, params []string) error {
|
---|
[615] | 1080 | if len(params) != 1 {
|
---|
| 1081 | return fmt.Errorf("expected exactly one argument")
|
---|
| 1082 | }
|
---|
| 1083 | text := params[0]
|
---|
| 1084 |
|
---|
| 1085 | dc.logger.Printf("broadcasting bouncer-wide NOTICE: %v", text)
|
---|
| 1086 |
|
---|
| 1087 | broadcastMsg := &irc.Message{
|
---|
| 1088 | Prefix: servicePrefix,
|
---|
| 1089 | Command: "NOTICE",
|
---|
[691] | 1090 | Params: []string{"$" + dc.srv.Config().Hostname, text},
|
---|
[615] | 1091 | }
|
---|
[678] | 1092 | var err error
|
---|
[755] | 1093 | sent := 0
|
---|
| 1094 | total := 0
|
---|
[615] | 1095 | dc.srv.forEachUser(func(u *user) {
|
---|
[755] | 1096 | total++
|
---|
[678] | 1097 | select {
|
---|
| 1098 | case <-ctx.Done():
|
---|
| 1099 | err = ctx.Err()
|
---|
| 1100 | case u.events <- eventBroadcast{broadcastMsg}:
|
---|
[755] | 1101 | sent++
|
---|
[678] | 1102 | }
|
---|
[615] | 1103 | })
|
---|
[755] | 1104 |
|
---|
| 1105 | dc.logger.Printf("broadcast bouncer-wide NOTICE to %v/%v downstreams", sent, total)
|
---|
| 1106 | sendServicePRIVMSG(dc, fmt.Sprintf("sent to %v/%v downstream connections", sent, total))
|
---|
| 1107 |
|
---|
[678] | 1108 | return err
|
---|
[615] | 1109 | }
|
---|