[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 | },
|
---|
| 297 | },
|
---|
| 298 | admin: true,
|
---|
| 299 | },
|
---|
[117] | 300 | }
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[328] | 303 | func appendServiceCommandSetHelp(cmds serviceCommandSet, prefix []string, admin bool, l *[]string) {
|
---|
[339] | 304 | for _, name := range cmds.Names() {
|
---|
| 305 | cmd := cmds[name]
|
---|
[328] | 306 | if cmd.admin && !admin {
|
---|
| 307 | continue
|
---|
| 308 | }
|
---|
[150] | 309 | words := append(prefix, name)
|
---|
| 310 | if len(cmd.children) == 0 {
|
---|
| 311 | s := strings.Join(words, " ")
|
---|
| 312 | *l = append(*l, s)
|
---|
| 313 | } else {
|
---|
[328] | 314 | appendServiceCommandSetHelp(cmd.children, words, admin, l)
|
---|
[150] | 315 | }
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[117] | 319 | func handleServiceHelp(dc *downstreamConn, params []string) error {
|
---|
| 320 | if len(params) > 0 {
|
---|
[150] | 321 | cmd, rest, err := serviceCommands.Get(params)
|
---|
| 322 | if err != nil {
|
---|
| 323 | return err
|
---|
[117] | 324 | }
|
---|
[150] | 325 | words := params[:len(params)-len(rest)]
|
---|
[117] | 326 |
|
---|
[150] | 327 | if len(cmd.children) > 0 {
|
---|
| 328 | var l []string
|
---|
[328] | 329 | appendServiceCommandSetHelp(cmd.children, words, dc.user.Admin, &l)
|
---|
[150] | 330 | sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", "))
|
---|
| 331 | } else {
|
---|
| 332 | text := strings.Join(words, " ")
|
---|
| 333 | if cmd.usage != "" {
|
---|
| 334 | text += " " + cmd.usage
|
---|
| 335 | }
|
---|
| 336 | text += ": " + cmd.desc
|
---|
| 337 |
|
---|
| 338 | sendServicePRIVMSG(dc, text)
|
---|
[117] | 339 | }
|
---|
| 340 | } else {
|
---|
| 341 | var l []string
|
---|
[328] | 342 | appendServiceCommandSetHelp(serviceCommands, nil, dc.user.Admin, &l)
|
---|
[117] | 343 | sendServicePRIVMSG(dc, "available commands: "+strings.Join(l, ", "))
|
---|
| 344 | }
|
---|
| 345 | return nil
|
---|
| 346 | }
|
---|
[120] | 347 |
|
---|
[202] | 348 | func newFlagSet() *flag.FlagSet {
|
---|
[120] | 349 | fs := flag.NewFlagSet("", flag.ContinueOnError)
|
---|
| 350 | fs.SetOutput(ioutil.Discard)
|
---|
[202] | 351 | return fs
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[313] | 354 | type stringSliceFlag []string
|
---|
[263] | 355 |
|
---|
[313] | 356 | func (v *stringSliceFlag) String() string {
|
---|
[263] | 357 | return fmt.Sprint([]string(*v))
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[313] | 360 | func (v *stringSliceFlag) Set(s string) error {
|
---|
[263] | 361 | *v = append(*v, s)
|
---|
| 362 | return nil
|
---|
| 363 | }
|
---|
| 364 |
|
---|
[313] | 365 | // stringPtrFlag is a flag value populating a string pointer. This allows to
|
---|
| 366 | // disambiguate between a flag that hasn't been set and a flag that has been
|
---|
| 367 | // set to an empty string.
|
---|
| 368 | type stringPtrFlag struct {
|
---|
| 369 | ptr **string
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | func (f stringPtrFlag) String() string {
|
---|
[333] | 373 | if f.ptr == nil || *f.ptr == nil {
|
---|
[313] | 374 | return ""
|
---|
| 375 | }
|
---|
| 376 | return **f.ptr
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | func (f stringPtrFlag) Set(s string) error {
|
---|
| 380 | *f.ptr = &s
|
---|
| 381 | return nil
|
---|
| 382 | }
|
---|
| 383 |
|
---|
[542] | 384 | type boolPtrFlag struct {
|
---|
| 385 | ptr **bool
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | func (f boolPtrFlag) String() string {
|
---|
| 389 | if f.ptr == nil || *f.ptr == nil {
|
---|
| 390 | return "<nil>"
|
---|
| 391 | }
|
---|
| 392 | return strconv.FormatBool(**f.ptr)
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | func (f boolPtrFlag) Set(s string) error {
|
---|
| 396 | v, err := strconv.ParseBool(s)
|
---|
| 397 | if err != nil {
|
---|
| 398 | return err
|
---|
| 399 | }
|
---|
| 400 | *f.ptr = &v
|
---|
| 401 | return nil
|
---|
| 402 | }
|
---|
| 403 |
|
---|
[313] | 404 | type networkFlagSet struct {
|
---|
| 405 | *flag.FlagSet
|
---|
| 406 | Addr, Name, Nick, Username, Pass, Realname *string
|
---|
[542] | 407 | Enabled *bool
|
---|
[315] | 408 | ConnectCommands []string
|
---|
[313] | 409 | }
|
---|
| 410 |
|
---|
| 411 | func newNetworkFlagSet() *networkFlagSet {
|
---|
| 412 | fs := &networkFlagSet{FlagSet: newFlagSet()}
|
---|
| 413 | fs.Var(stringPtrFlag{&fs.Addr}, "addr", "")
|
---|
| 414 | fs.Var(stringPtrFlag{&fs.Name}, "name", "")
|
---|
| 415 | fs.Var(stringPtrFlag{&fs.Nick}, "nick", "")
|
---|
| 416 | fs.Var(stringPtrFlag{&fs.Username}, "username", "")
|
---|
| 417 | fs.Var(stringPtrFlag{&fs.Pass}, "pass", "")
|
---|
| 418 | fs.Var(stringPtrFlag{&fs.Realname}, "realname", "")
|
---|
[542] | 419 | fs.Var(boolPtrFlag{&fs.Enabled}, "enabled", "")
|
---|
[313] | 420 | fs.Var((*stringSliceFlag)(&fs.ConnectCommands), "connect-command", "")
|
---|
| 421 | return fs
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | func (fs *networkFlagSet) update(network *Network) error {
|
---|
| 425 | if fs.Addr != nil {
|
---|
| 426 | if addrParts := strings.SplitN(*fs.Addr, "://", 2); len(addrParts) == 2 {
|
---|
| 427 | scheme := addrParts[0]
|
---|
| 428 | switch scheme {
|
---|
[358] | 429 | case "ircs", "irc+insecure", "unix":
|
---|
[313] | 430 | default:
|
---|
[358] | 431 | return fmt.Errorf("unknown scheme %q (supported schemes: ircs, irc+insecure, unix)", scheme)
|
---|
[313] | 432 | }
|
---|
| 433 | }
|
---|
| 434 | network.Addr = *fs.Addr
|
---|
| 435 | }
|
---|
| 436 | if fs.Name != nil {
|
---|
| 437 | network.Name = *fs.Name
|
---|
| 438 | }
|
---|
| 439 | if fs.Nick != nil {
|
---|
| 440 | network.Nick = *fs.Nick
|
---|
| 441 | }
|
---|
| 442 | if fs.Username != nil {
|
---|
| 443 | network.Username = *fs.Username
|
---|
| 444 | }
|
---|
| 445 | if fs.Pass != nil {
|
---|
| 446 | network.Pass = *fs.Pass
|
---|
| 447 | }
|
---|
| 448 | if fs.Realname != nil {
|
---|
| 449 | network.Realname = *fs.Realname
|
---|
| 450 | }
|
---|
[542] | 451 | if fs.Enabled != nil {
|
---|
| 452 | network.Enabled = *fs.Enabled
|
---|
| 453 | }
|
---|
[313] | 454 | if fs.ConnectCommands != nil {
|
---|
| 455 | if len(fs.ConnectCommands) == 1 && fs.ConnectCommands[0] == "" {
|
---|
| 456 | network.ConnectCommands = nil
|
---|
| 457 | } else {
|
---|
| 458 | for _, command := range fs.ConnectCommands {
|
---|
| 459 | _, err := irc.ParseMessage(command)
|
---|
| 460 | if err != nil {
|
---|
| 461 | return fmt.Errorf("flag -connect-command must be a valid raw irc command string: %q: %v", command, err)
|
---|
| 462 | }
|
---|
| 463 | }
|
---|
| 464 | network.ConnectCommands = fs.ConnectCommands
|
---|
| 465 | }
|
---|
| 466 | }
|
---|
| 467 | return nil
|
---|
| 468 | }
|
---|
| 469 |
|
---|
[325] | 470 | func handleServiceNetworkCreate(dc *downstreamConn, params []string) error {
|
---|
[313] | 471 | fs := newNetworkFlagSet()
|
---|
[120] | 472 | if err := fs.Parse(params); err != nil {
|
---|
| 473 | return err
|
---|
| 474 | }
|
---|
[313] | 475 | if fs.Addr == nil {
|
---|
[150] | 476 | return fmt.Errorf("flag -addr is required")
|
---|
[120] | 477 | }
|
---|
| 478 |
|
---|
[313] | 479 | record := &Network{
|
---|
[542] | 480 | Addr: *fs.Addr,
|
---|
| 481 | Nick: dc.nick,
|
---|
| 482 | Enabled: true,
|
---|
[269] | 483 | }
|
---|
[313] | 484 | if err := fs.update(record); err != nil {
|
---|
| 485 | return err
|
---|
[263] | 486 | }
|
---|
| 487 |
|
---|
[313] | 488 | network, err := dc.user.createNetwork(record)
|
---|
[120] | 489 | if err != nil {
|
---|
| 490 | return fmt.Errorf("could not create network: %v", err)
|
---|
| 491 | }
|
---|
| 492 |
|
---|
[202] | 493 | sendServicePRIVMSG(dc, fmt.Sprintf("created network %q", network.GetName()))
|
---|
[120] | 494 | return nil
|
---|
| 495 | }
|
---|
[151] | 496 |
|
---|
| 497 | func handleServiceNetworkStatus(dc *downstreamConn, params []string) error {
|
---|
[546] | 498 | n := 0
|
---|
[151] | 499 | dc.user.forEachNetwork(func(net *network) {
|
---|
| 500 | var statuses []string
|
---|
| 501 | var details string
|
---|
[279] | 502 | if uc := net.conn; uc != nil {
|
---|
[271] | 503 | if dc.nick != uc.nick {
|
---|
| 504 | statuses = append(statuses, "connected as "+uc.nick)
|
---|
| 505 | } else {
|
---|
| 506 | statuses = append(statuses, "connected")
|
---|
| 507 | }
|
---|
[478] | 508 | details = fmt.Sprintf("%v channels", uc.channels.Len())
|
---|
[542] | 509 | } else if !net.Enabled {
|
---|
| 510 | statuses = append(statuses, "disabled")
|
---|
[151] | 511 | } else {
|
---|
| 512 | statuses = append(statuses, "disconnected")
|
---|
[219] | 513 | if net.lastError != nil {
|
---|
| 514 | details = net.lastError.Error()
|
---|
| 515 | }
|
---|
[151] | 516 | }
|
---|
| 517 |
|
---|
| 518 | if net == dc.network {
|
---|
| 519 | statuses = append(statuses, "current")
|
---|
| 520 | }
|
---|
| 521 |
|
---|
[224] | 522 | name := net.GetName()
|
---|
| 523 | if name != net.Addr {
|
---|
| 524 | name = fmt.Sprintf("%v (%v)", name, net.Addr)
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | s := fmt.Sprintf("%v [%v]", name, strings.Join(statuses, ", "))
|
---|
[151] | 528 | if details != "" {
|
---|
| 529 | s += ": " + details
|
---|
| 530 | }
|
---|
| 531 | sendServicePRIVMSG(dc, s)
|
---|
[546] | 532 |
|
---|
| 533 | n++
|
---|
[151] | 534 | })
|
---|
[546] | 535 |
|
---|
| 536 | if n == 0 {
|
---|
| 537 | sendServicePRIVMSG(dc, `No network configured, add one with "network create".`)
|
---|
| 538 | }
|
---|
| 539 |
|
---|
[151] | 540 | return nil
|
---|
| 541 | }
|
---|
[202] | 542 |
|
---|
[313] | 543 | func handleServiceNetworkUpdate(dc *downstreamConn, params []string) error {
|
---|
| 544 | if len(params) < 1 {
|
---|
[436] | 545 | return fmt.Errorf("expected at least one argument")
|
---|
[313] | 546 | }
|
---|
| 547 |
|
---|
| 548 | fs := newNetworkFlagSet()
|
---|
| 549 | if err := fs.Parse(params[1:]); err != nil {
|
---|
| 550 | return err
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 | net := dc.user.getNetwork(params[0])
|
---|
| 554 | if net == nil {
|
---|
| 555 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | record := net.Network // copy network record because we'll mutate it
|
---|
| 559 | if err := fs.update(&record); err != nil {
|
---|
| 560 | return err
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | network, err := dc.user.updateNetwork(&record)
|
---|
| 564 | if err != nil {
|
---|
| 565 | return fmt.Errorf("could not update network: %v", err)
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | sendServicePRIVMSG(dc, fmt.Sprintf("updated network %q", network.GetName()))
|
---|
| 569 | return nil
|
---|
| 570 | }
|
---|
| 571 |
|
---|
[202] | 572 | func handleServiceNetworkDelete(dc *downstreamConn, params []string) error {
|
---|
| 573 | if len(params) != 1 {
|
---|
| 574 | return fmt.Errorf("expected exactly one argument")
|
---|
| 575 | }
|
---|
| 576 |
|
---|
| 577 | net := dc.user.getNetwork(params[0])
|
---|
| 578 | if net == nil {
|
---|
| 579 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | if err := dc.user.deleteNetwork(net.ID); err != nil {
|
---|
| 583 | return err
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | sendServicePRIVMSG(dc, fmt.Sprintf("deleted network %q", net.GetName()))
|
---|
| 587 | return nil
|
---|
| 588 | }
|
---|
[252] | 589 |
|
---|
[577] | 590 | func handleServiceNetworkQuote(dc *downstreamConn, params []string) error {
|
---|
| 591 | if len(params) != 2 {
|
---|
| 592 | return fmt.Errorf("expected exactly two arguments")
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | net := dc.user.getNetwork(params[0])
|
---|
| 596 | if net == nil {
|
---|
| 597 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | uc := net.conn
|
---|
| 601 | if uc == nil {
|
---|
| 602 | return fmt.Errorf("network %q is not currently connected", params[0])
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | m, err := irc.ParseMessage(params[1])
|
---|
| 606 | if err != nil {
|
---|
| 607 | return fmt.Errorf("failed to parse command %q: %v", params[1], err)
|
---|
| 608 | }
|
---|
| 609 | uc.SendMessage(m)
|
---|
| 610 |
|
---|
| 611 | sendServicePRIVMSG(dc, fmt.Sprintf("sent command to %q", net.GetName()))
|
---|
| 612 | return nil
|
---|
| 613 | }
|
---|
| 614 |
|
---|
[614] | 615 | func sendCertfpFingerprints(dc *downstreamConn, cert []byte) {
|
---|
| 616 | sha1Sum := sha1.Sum(cert)
|
---|
| 617 | sendServicePRIVMSG(dc, "SHA-1 fingerprint: "+hex.EncodeToString(sha1Sum[:]))
|
---|
| 618 | sha256Sum := sha256.Sum256(cert)
|
---|
| 619 | sendServicePRIVMSG(dc, "SHA-256 fingerprint: "+hex.EncodeToString(sha256Sum[:]))
|
---|
| 620 | sha512Sum := sha512.Sum512(cert)
|
---|
| 621 | sendServicePRIVMSG(dc, "SHA-512 fingerprint: "+hex.EncodeToString(sha512Sum[:]))
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | func handleServiceCertFPGenerate(dc *downstreamConn, params []string) error {
|
---|
[325] | 625 | fs := newFlagSet()
|
---|
| 626 | keyType := fs.String("key-type", "rsa", "key type to generate (rsa, ecdsa, ed25519)")
|
---|
| 627 | bits := fs.Int("bits", 3072, "size of key to generate, meaningful only for RSA")
|
---|
| 628 |
|
---|
| 629 | if err := fs.Parse(params); err != nil {
|
---|
| 630 | return err
|
---|
| 631 | }
|
---|
| 632 |
|
---|
| 633 | if len(fs.Args()) != 1 {
|
---|
| 634 | return errors.New("exactly one argument is required")
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | net := dc.user.getNetwork(fs.Arg(0))
|
---|
| 638 | if net == nil {
|
---|
| 639 | return fmt.Errorf("unknown network %q", fs.Arg(0))
|
---|
| 640 | }
|
---|
| 641 |
|
---|
[614] | 642 | if *bits <= 0 || *bits > maxRSABits {
|
---|
| 643 | return fmt.Errorf("invalid value for -bits")
|
---|
[325] | 644 | }
|
---|
| 645 |
|
---|
[614] | 646 | privKey, cert, err := generateCertFP(*keyType, *bits)
|
---|
[325] | 647 | if err != nil {
|
---|
| 648 | return err
|
---|
| 649 | }
|
---|
| 650 |
|
---|
[614] | 651 | net.SASL.External.CertBlob = cert
|
---|
| 652 | net.SASL.External.PrivKeyBlob = privKey
|
---|
[325] | 653 | net.SASL.Mechanism = "EXTERNAL"
|
---|
| 654 |
|
---|
[421] | 655 | if err := dc.srv.db.StoreNetwork(dc.user.ID, &net.Network); err != nil {
|
---|
[325] | 656 | return err
|
---|
| 657 | }
|
---|
| 658 |
|
---|
| 659 | sendServicePRIVMSG(dc, "certificate generated")
|
---|
[614] | 660 | sendCertfpFingerprints(dc, cert)
|
---|
[325] | 661 | return nil
|
---|
| 662 | }
|
---|
| 663 |
|
---|
[614] | 664 | func handleServiceCertFPFingerprints(dc *downstreamConn, params []string) error {
|
---|
[325] | 665 | if len(params) != 1 {
|
---|
| 666 | return fmt.Errorf("expected exactly one argument")
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | net := dc.user.getNetwork(params[0])
|
---|
| 670 | if net == nil {
|
---|
| 671 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 672 | }
|
---|
| 673 |
|
---|
[614] | 674 | if net.SASL.Mechanism != "EXTERNAL" {
|
---|
| 675 | return fmt.Errorf("CertFP not set up")
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | sendCertfpFingerprints(dc, net.SASL.External.CertBlob)
|
---|
[325] | 679 | return nil
|
---|
| 680 | }
|
---|
| 681 |
|
---|
[364] | 682 | func handleServiceSASLSetPlain(dc *downstreamConn, params []string) error {
|
---|
| 683 | if len(params) != 3 {
|
---|
| 684 | return fmt.Errorf("expected exactly 3 arguments")
|
---|
[325] | 685 | }
|
---|
| 686 |
|
---|
| 687 | net := dc.user.getNetwork(params[0])
|
---|
| 688 | if net == nil {
|
---|
| 689 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 690 | }
|
---|
| 691 |
|
---|
[364] | 692 | net.SASL.Plain.Username = params[1]
|
---|
| 693 | net.SASL.Plain.Password = params[2]
|
---|
| 694 | net.SASL.Mechanism = "PLAIN"
|
---|
[325] | 695 |
|
---|
[421] | 696 | if err := dc.srv.db.StoreNetwork(dc.user.ID, &net.Network); err != nil {
|
---|
[325] | 697 | return err
|
---|
| 698 | }
|
---|
| 699 |
|
---|
[364] | 700 | sendServicePRIVMSG(dc, "credentials saved")
|
---|
[325] | 701 | return nil
|
---|
| 702 | }
|
---|
| 703 |
|
---|
[364] | 704 | func handleServiceSASLReset(dc *downstreamConn, params []string) error {
|
---|
| 705 | if len(params) != 1 {
|
---|
| 706 | return fmt.Errorf("expected exactly one argument")
|
---|
[363] | 707 | }
|
---|
| 708 |
|
---|
| 709 | net := dc.user.getNetwork(params[0])
|
---|
| 710 | if net == nil {
|
---|
| 711 | return fmt.Errorf("unknown network %q", params[0])
|
---|
| 712 | }
|
---|
| 713 |
|
---|
[364] | 714 | net.SASL.Plain.Username = ""
|
---|
| 715 | net.SASL.Plain.Password = ""
|
---|
| 716 | net.SASL.External.CertBlob = nil
|
---|
| 717 | net.SASL.External.PrivKeyBlob = nil
|
---|
| 718 | net.SASL.Mechanism = ""
|
---|
[363] | 719 |
|
---|
[421] | 720 | if err := dc.srv.db.StoreNetwork(dc.user.ID, &net.Network); err != nil {
|
---|
[363] | 721 | return err
|
---|
| 722 | }
|
---|
| 723 |
|
---|
[364] | 724 | sendServicePRIVMSG(dc, "credentials reset")
|
---|
[363] | 725 | return nil
|
---|
| 726 | }
|
---|
| 727 |
|
---|
[329] | 728 | func handleUserCreate(dc *downstreamConn, params []string) error {
|
---|
| 729 | fs := newFlagSet()
|
---|
| 730 | username := fs.String("username", "", "")
|
---|
| 731 | password := fs.String("password", "", "")
|
---|
[568] | 732 | realname := fs.String("realname", "", "")
|
---|
[329] | 733 | admin := fs.Bool("admin", false, "")
|
---|
| 734 |
|
---|
| 735 | if err := fs.Parse(params); err != nil {
|
---|
| 736 | return err
|
---|
| 737 | }
|
---|
| 738 | if *username == "" {
|
---|
| 739 | return fmt.Errorf("flag -username is required")
|
---|
| 740 | }
|
---|
| 741 | if *password == "" {
|
---|
| 742 | return fmt.Errorf("flag -password is required")
|
---|
| 743 | }
|
---|
| 744 |
|
---|
| 745 | hashed, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)
|
---|
| 746 | if err != nil {
|
---|
| 747 | return fmt.Errorf("failed to hash password: %v", err)
|
---|
| 748 | }
|
---|
| 749 |
|
---|
| 750 | user := &User{
|
---|
| 751 | Username: *username,
|
---|
| 752 | Password: string(hashed),
|
---|
[568] | 753 | Realname: *realname,
|
---|
[329] | 754 | Admin: *admin,
|
---|
| 755 | }
|
---|
| 756 | if _, err := dc.srv.createUser(user); err != nil {
|
---|
| 757 | return fmt.Errorf("could not create user: %v", err)
|
---|
| 758 | }
|
---|
| 759 |
|
---|
| 760 | sendServicePRIVMSG(dc, fmt.Sprintf("created user %q", *username))
|
---|
| 761 | return nil
|
---|
| 762 | }
|
---|
[379] | 763 |
|
---|
[568] | 764 | func handleUserUpdate(dc *downstreamConn, params []string) error {
|
---|
[570] | 765 | var password, realname *string
|
---|
[568] | 766 | fs := newFlagSet()
|
---|
[570] | 767 | fs.Var(stringPtrFlag{&password}, "password", "")
|
---|
[569] | 768 | fs.Var(stringPtrFlag{&realname}, "realname", "")
|
---|
[568] | 769 |
|
---|
| 770 | if err := fs.Parse(params); err != nil {
|
---|
| 771 | return err
|
---|
| 772 | }
|
---|
| 773 |
|
---|
[572] | 774 | // copy the user record because we'll mutate it
|
---|
| 775 | record := dc.user.User
|
---|
| 776 |
|
---|
[570] | 777 | if password != nil {
|
---|
| 778 | hashed, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)
|
---|
| 779 | if err != nil {
|
---|
| 780 | return fmt.Errorf("failed to hash password: %v", err)
|
---|
| 781 | }
|
---|
[572] | 782 | record.Password = string(hashed)
|
---|
[570] | 783 | }
|
---|
[569] | 784 | if realname != nil {
|
---|
[572] | 785 | record.Realname = *realname
|
---|
[568] | 786 | }
|
---|
| 787 |
|
---|
[572] | 788 | if err := dc.user.updateUser(&record); err != nil {
|
---|
| 789 | return err
|
---|
| 790 | }
|
---|
| 791 |
|
---|
[568] | 792 | sendServicePRIVMSG(dc, fmt.Sprintf("updated user %q", dc.user.Username))
|
---|
| 793 | return nil
|
---|
| 794 | }
|
---|
| 795 |
|
---|
[379] | 796 | func handleUserDelete(dc *downstreamConn, params []string) error {
|
---|
| 797 | if len(params) != 1 {
|
---|
| 798 | return fmt.Errorf("expected exactly one argument")
|
---|
| 799 | }
|
---|
| 800 | username := params[0]
|
---|
| 801 |
|
---|
| 802 | u := dc.srv.getUser(username)
|
---|
| 803 | if u == nil {
|
---|
| 804 | return fmt.Errorf("unknown username %q", username)
|
---|
| 805 | }
|
---|
| 806 |
|
---|
| 807 | u.stop()
|
---|
| 808 |
|
---|
[508] | 809 | if err := dc.srv.db.DeleteUser(u.ID); err != nil {
|
---|
[379] | 810 | return fmt.Errorf("failed to delete user: %v", err)
|
---|
| 811 | }
|
---|
| 812 |
|
---|
| 813 | sendServicePRIVMSG(dc, fmt.Sprintf("deleted user %q", username))
|
---|
| 814 | return nil
|
---|
| 815 | }
|
---|
[436] | 816 |
|
---|
[539] | 817 | func handleServiceChannelStatus(dc *downstreamConn, params []string) error {
|
---|
| 818 | var defaultNetworkName string
|
---|
| 819 | if dc.network != nil {
|
---|
| 820 | defaultNetworkName = dc.network.GetName()
|
---|
| 821 | }
|
---|
| 822 |
|
---|
| 823 | fs := newFlagSet()
|
---|
| 824 | networkName := fs.String("network", defaultNetworkName, "")
|
---|
| 825 |
|
---|
| 826 | if err := fs.Parse(params); err != nil {
|
---|
| 827 | return err
|
---|
| 828 | }
|
---|
| 829 |
|
---|
[546] | 830 | n := 0
|
---|
| 831 |
|
---|
[539] | 832 | sendNetwork := func(net *network) {
|
---|
[573] | 833 | var channels []*Channel
|
---|
[539] | 834 | for _, entry := range net.channels.innerMap {
|
---|
[573] | 835 | channels = append(channels, entry.value.(*Channel))
|
---|
| 836 | }
|
---|
[539] | 837 |
|
---|
[573] | 838 | sort.Slice(channels, func(i, j int) bool {
|
---|
| 839 | return strings.ReplaceAll(channels[i].Name, "#", "") <
|
---|
| 840 | strings.ReplaceAll(channels[j].Name, "#", "")
|
---|
| 841 | })
|
---|
| 842 |
|
---|
| 843 | for _, ch := range channels {
|
---|
[539] | 844 | var uch *upstreamChannel
|
---|
| 845 | if net.conn != nil {
|
---|
| 846 | uch = net.conn.channels.Value(ch.Name)
|
---|
| 847 | }
|
---|
| 848 |
|
---|
| 849 | name := ch.Name
|
---|
| 850 | if *networkName == "" {
|
---|
| 851 | name += "/" + net.GetName()
|
---|
| 852 | }
|
---|
| 853 |
|
---|
| 854 | var status string
|
---|
| 855 | if uch != nil {
|
---|
| 856 | status = "joined"
|
---|
| 857 | } else if net.conn != nil {
|
---|
| 858 | status = "parted"
|
---|
| 859 | } else {
|
---|
| 860 | status = "disconnected"
|
---|
| 861 | }
|
---|
| 862 |
|
---|
| 863 | if ch.Detached {
|
---|
| 864 | status += ", detached"
|
---|
| 865 | }
|
---|
| 866 |
|
---|
| 867 | s := fmt.Sprintf("%v [%v]", name, status)
|
---|
| 868 | sendServicePRIVMSG(dc, s)
|
---|
[546] | 869 |
|
---|
| 870 | n++
|
---|
[539] | 871 | }
|
---|
| 872 | }
|
---|
| 873 |
|
---|
| 874 | if *networkName == "" {
|
---|
| 875 | dc.user.forEachNetwork(sendNetwork)
|
---|
| 876 | } else {
|
---|
| 877 | net := dc.user.getNetwork(*networkName)
|
---|
| 878 | if net == nil {
|
---|
| 879 | return fmt.Errorf("unknown network %q", *networkName)
|
---|
| 880 | }
|
---|
| 881 | sendNetwork(net)
|
---|
| 882 | }
|
---|
| 883 |
|
---|
[546] | 884 | if n == 0 {
|
---|
| 885 | sendServicePRIVMSG(dc, "No channel configured.")
|
---|
| 886 | }
|
---|
| 887 |
|
---|
[539] | 888 | return nil
|
---|
| 889 | }
|
---|
| 890 |
|
---|
[436] | 891 | type channelFlagSet struct {
|
---|
| 892 | *flag.FlagSet
|
---|
| 893 | RelayDetached, ReattachOn, DetachAfter, DetachOn *string
|
---|
| 894 | }
|
---|
| 895 |
|
---|
| 896 | func newChannelFlagSet() *channelFlagSet {
|
---|
| 897 | fs := &channelFlagSet{FlagSet: newFlagSet()}
|
---|
| 898 | fs.Var(stringPtrFlag{&fs.RelayDetached}, "relay-detached", "")
|
---|
| 899 | fs.Var(stringPtrFlag{&fs.ReattachOn}, "reattach-on", "")
|
---|
| 900 | fs.Var(stringPtrFlag{&fs.DetachAfter}, "detach-after", "")
|
---|
| 901 | fs.Var(stringPtrFlag{&fs.DetachOn}, "detach-on", "")
|
---|
| 902 | return fs
|
---|
| 903 | }
|
---|
| 904 |
|
---|
| 905 | func (fs *channelFlagSet) update(channel *Channel) error {
|
---|
| 906 | if fs.RelayDetached != nil {
|
---|
| 907 | filter, err := parseFilter(*fs.RelayDetached)
|
---|
| 908 | if err != nil {
|
---|
| 909 | return err
|
---|
| 910 | }
|
---|
| 911 | channel.RelayDetached = filter
|
---|
| 912 | }
|
---|
| 913 | if fs.ReattachOn != nil {
|
---|
| 914 | filter, err := parseFilter(*fs.ReattachOn)
|
---|
| 915 | if err != nil {
|
---|
| 916 | return err
|
---|
| 917 | }
|
---|
| 918 | channel.ReattachOn = filter
|
---|
| 919 | }
|
---|
| 920 | if fs.DetachAfter != nil {
|
---|
| 921 | dur, err := time.ParseDuration(*fs.DetachAfter)
|
---|
| 922 | if err != nil || dur < 0 {
|
---|
| 923 | return fmt.Errorf("unknown duration for -detach-after %q (duration format: 0, 300s, 22h30m, ...)", *fs.DetachAfter)
|
---|
| 924 | }
|
---|
| 925 | channel.DetachAfter = dur
|
---|
| 926 | }
|
---|
| 927 | if fs.DetachOn != nil {
|
---|
| 928 | filter, err := parseFilter(*fs.DetachOn)
|
---|
| 929 | if err != nil {
|
---|
| 930 | return err
|
---|
| 931 | }
|
---|
| 932 | channel.DetachOn = filter
|
---|
| 933 | }
|
---|
| 934 | return nil
|
---|
| 935 | }
|
---|
| 936 |
|
---|
| 937 | func handleServiceChannelUpdate(dc *downstreamConn, params []string) error {
|
---|
| 938 | if len(params) < 1 {
|
---|
| 939 | return fmt.Errorf("expected at least one argument")
|
---|
| 940 | }
|
---|
| 941 | name := params[0]
|
---|
| 942 |
|
---|
| 943 | fs := newChannelFlagSet()
|
---|
| 944 | if err := fs.Parse(params[1:]); err != nil {
|
---|
| 945 | return err
|
---|
| 946 | }
|
---|
| 947 |
|
---|
| 948 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 949 | if err != nil {
|
---|
| 950 | return fmt.Errorf("unknown channel %q", name)
|
---|
| 951 | }
|
---|
| 952 |
|
---|
[478] | 953 | ch := uc.network.channels.Value(upstreamName)
|
---|
| 954 | if ch == nil {
|
---|
[436] | 955 | return fmt.Errorf("unknown channel %q", name)
|
---|
| 956 | }
|
---|
| 957 |
|
---|
| 958 | if err := fs.update(ch); err != nil {
|
---|
| 959 | return err
|
---|
| 960 | }
|
---|
| 961 |
|
---|
| 962 | uc.updateChannelAutoDetach(upstreamName)
|
---|
| 963 |
|
---|
| 964 | if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
|
---|
| 965 | return fmt.Errorf("failed to update channel: %v", err)
|
---|
| 966 | }
|
---|
| 967 |
|
---|
| 968 | sendServicePRIVMSG(dc, fmt.Sprintf("updated channel %q", name))
|
---|
| 969 | return nil
|
---|
| 970 | }
|
---|
[605] | 971 |
|
---|
| 972 | func handleServiceServerStatus(dc *downstreamConn, params []string) error {
|
---|
[607] | 973 | dbStats, err := dc.user.srv.db.Stats()
|
---|
| 974 | if err != nil {
|
---|
| 975 | return err
|
---|
| 976 | }
|
---|
| 977 | serverStats := dc.user.srv.Stats()
|
---|
| 978 | sendServicePRIVMSG(dc, fmt.Sprintf("%v/%v users, %v downstreams, %v networks, %v channels", serverStats.Users, dbStats.Users, serverStats.Downstreams, dbStats.Networks, dbStats.Channels))
|
---|
[605] | 979 | return nil
|
---|
| 980 | }
|
---|