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