[98] | 1 | package soju
|
---|
[13] | 2 |
|
---|
| 3 | import (
|
---|
[652] | 4 | "context"
|
---|
[91] | 5 | "crypto/tls"
|
---|
[112] | 6 | "encoding/base64"
|
---|
[655] | 7 | "errors"
|
---|
[13] | 8 | "fmt"
|
---|
| 9 | "io"
|
---|
| 10 | "net"
|
---|
[108] | 11 | "strconv"
|
---|
[39] | 12 | "strings"
|
---|
[91] | 13 | "time"
|
---|
[13] | 14 |
|
---|
[112] | 15 | "github.com/emersion/go-sasl"
|
---|
[85] | 16 | "golang.org/x/crypto/bcrypt"
|
---|
[13] | 17 | "gopkg.in/irc.v3"
|
---|
| 18 | )
|
---|
| 19 |
|
---|
| 20 | type ircError struct {
|
---|
| 21 | Message *irc.Message
|
---|
| 22 | }
|
---|
| 23 |
|
---|
[85] | 24 | func (err ircError) Error() string {
|
---|
| 25 | return err.Message.String()
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[13] | 28 | func newUnknownCommandError(cmd string) ircError {
|
---|
| 29 | return ircError{&irc.Message{
|
---|
| 30 | Command: irc.ERR_UNKNOWNCOMMAND,
|
---|
| 31 | Params: []string{
|
---|
| 32 | "*",
|
---|
| 33 | cmd,
|
---|
| 34 | "Unknown command",
|
---|
| 35 | },
|
---|
| 36 | }}
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | func newNeedMoreParamsError(cmd string) ircError {
|
---|
| 40 | return ircError{&irc.Message{
|
---|
| 41 | Command: irc.ERR_NEEDMOREPARAMS,
|
---|
| 42 | Params: []string{
|
---|
| 43 | "*",
|
---|
| 44 | cmd,
|
---|
| 45 | "Not enough parameters",
|
---|
| 46 | },
|
---|
| 47 | }}
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[319] | 50 | func newChatHistoryError(subcommand string, target string) ircError {
|
---|
| 51 | return ircError{&irc.Message{
|
---|
| 52 | Command: "FAIL",
|
---|
| 53 | Params: []string{"CHATHISTORY", "MESSAGE_ERROR", subcommand, target, "Messages could not be retrieved"},
|
---|
| 54 | }}
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[85] | 57 | var errAuthFailed = ircError{&irc.Message{
|
---|
| 58 | Command: irc.ERR_PASSWDMISMATCH,
|
---|
| 59 | Params: []string{"*", "Invalid username or password"},
|
---|
| 60 | }}
|
---|
[13] | 61 |
|
---|
[535] | 62 | func parseBouncerNetID(subcommand, s string) (int64, error) {
|
---|
[532] | 63 | id, err := strconv.ParseInt(s, 10, 64)
|
---|
| 64 | if err != nil {
|
---|
| 65 | return 0, ircError{&irc.Message{
|
---|
| 66 | Command: "FAIL",
|
---|
[535] | 67 | Params: []string{"BOUNCER", "INVALID_NETID", subcommand, s, "Invalid network ID"},
|
---|
[532] | 68 | }}
|
---|
| 69 | }
|
---|
| 70 | return id, nil
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[654] | 73 | func fillNetworkAddrAttrs(attrs irc.Tags, network *Network) {
|
---|
| 74 | u, err := network.URL()
|
---|
| 75 | if err != nil {
|
---|
| 76 | return
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | hasHostPort := true
|
---|
| 80 | switch u.Scheme {
|
---|
| 81 | case "ircs":
|
---|
| 82 | attrs["tls"] = irc.TagValue("1")
|
---|
| 83 | case "irc+insecure":
|
---|
| 84 | attrs["tls"] = irc.TagValue("0")
|
---|
| 85 | default: // e.g. unix://
|
---|
| 86 | hasHostPort = false
|
---|
| 87 | }
|
---|
| 88 | if host, port, err := net.SplitHostPort(u.Host); err == nil && hasHostPort {
|
---|
| 89 | attrs["host"] = irc.TagValue(host)
|
---|
| 90 | attrs["port"] = irc.TagValue(port)
|
---|
| 91 | } else if hasHostPort {
|
---|
| 92 | attrs["host"] = irc.TagValue(u.Host)
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[535] | 96 | func getNetworkAttrs(network *network) irc.Tags {
|
---|
| 97 | state := "disconnected"
|
---|
| 98 | if uc := network.conn; uc != nil {
|
---|
| 99 | state = "connected"
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | attrs := irc.Tags{
|
---|
| 103 | "name": irc.TagValue(network.GetName()),
|
---|
| 104 | "state": irc.TagValue(state),
|
---|
[664] | 105 | "nickname": irc.TagValue(GetNick(&network.user.User, &network.Network)),
|
---|
[535] | 106 | }
|
---|
| 107 |
|
---|
| 108 | if network.Username != "" {
|
---|
| 109 | attrs["username"] = irc.TagValue(network.Username)
|
---|
| 110 | }
|
---|
[568] | 111 | if realname := GetRealname(&network.user.User, &network.Network); realname != "" {
|
---|
| 112 | attrs["realname"] = irc.TagValue(realname)
|
---|
[535] | 113 | }
|
---|
| 114 |
|
---|
[654] | 115 | fillNetworkAddrAttrs(attrs, &network.Network)
|
---|
| 116 |
|
---|
| 117 | return attrs
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | func networkAddrFromAttrs(attrs irc.Tags) string {
|
---|
| 121 | host, ok := attrs.GetTag("host")
|
---|
| 122 | if !ok {
|
---|
| 123 | return ""
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | addr := host
|
---|
| 127 | if port, ok := attrs.GetTag("port"); ok {
|
---|
| 128 | addr += ":" + port
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | if tlsStr, ok := attrs.GetTag("tls"); ok && tlsStr == "0" {
|
---|
| 132 | addr = "irc+insecure://" + tlsStr
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | return addr
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | func updateNetworkAttrs(record *Network, attrs irc.Tags, subcommand string) error {
|
---|
| 139 | addrAttrs := irc.Tags{}
|
---|
| 140 | fillNetworkAddrAttrs(addrAttrs, record)
|
---|
| 141 |
|
---|
| 142 | updateAddr := false
|
---|
| 143 | for k, v := range attrs {
|
---|
| 144 | s := string(v)
|
---|
| 145 | switch k {
|
---|
| 146 | case "host", "port", "tls":
|
---|
| 147 | updateAddr = true
|
---|
| 148 | addrAttrs[k] = v
|
---|
| 149 | case "name":
|
---|
| 150 | record.Name = s
|
---|
| 151 | case "nickname":
|
---|
| 152 | record.Nick = s
|
---|
| 153 | case "username":
|
---|
| 154 | record.Username = s
|
---|
| 155 | case "realname":
|
---|
| 156 | record.Realname = s
|
---|
| 157 | case "pass":
|
---|
| 158 | record.Pass = s
|
---|
[535] | 159 | default:
|
---|
[654] | 160 | return ircError{&irc.Message{
|
---|
| 161 | Command: "FAIL",
|
---|
| 162 | Params: []string{"BOUNCER", "UNKNOWN_ATTRIBUTE", subcommand, k, "Unknown attribute"},
|
---|
| 163 | }}
|
---|
[535] | 164 | }
|
---|
[654] | 165 | }
|
---|
| 166 |
|
---|
| 167 | if updateAddr {
|
---|
| 168 | record.Addr = networkAddrFromAttrs(addrAttrs)
|
---|
| 169 | if record.Addr == "" {
|
---|
| 170 | return ircError{&irc.Message{
|
---|
| 171 | Command: "FAIL",
|
---|
| 172 | Params: []string{"BOUNCER", "NEED_ATTRIBUTE", subcommand, "host", "Missing required host attribute"},
|
---|
| 173 | }}
|
---|
[535] | 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[654] | 177 | return nil
|
---|
[535] | 178 | }
|
---|
| 179 |
|
---|
[411] | 180 | // ' ' and ':' break the IRC message wire format, '@' and '!' break prefixes,
|
---|
[716] | 181 | // '*' and '?' break masks, '$' breaks server masks in PRIVMSG/NOTICE,
|
---|
| 182 | // "*" is the reserved nickname for registration
|
---|
[565] | 183 | const illegalNickChars = " :@!*?$"
|
---|
[404] | 184 |
|
---|
[275] | 185 | // permanentDownstreamCaps is the list of always-supported downstream
|
---|
| 186 | // capabilities.
|
---|
| 187 | var permanentDownstreamCaps = map[string]string{
|
---|
[535] | 188 | "batch": "",
|
---|
| 189 | "cap-notify": "",
|
---|
| 190 | "echo-message": "",
|
---|
| 191 | "invite-notify": "",
|
---|
| 192 | "message-tags": "",
|
---|
| 193 | "sasl": "PLAIN",
|
---|
| 194 | "server-time": "",
|
---|
[540] | 195 | "setname": "",
|
---|
[535] | 196 |
|
---|
| 197 | "soju.im/bouncer-networks": "",
|
---|
| 198 | "soju.im/bouncer-networks-notify": "",
|
---|
[275] | 199 | }
|
---|
| 200 |
|
---|
[292] | 201 | // needAllDownstreamCaps is the list of downstream capabilities that
|
---|
| 202 | // require support from all upstreams to be enabled
|
---|
| 203 | var needAllDownstreamCaps = map[string]string{
|
---|
[648] | 204 | "account-notify": "",
|
---|
| 205 | "account-tag": "",
|
---|
| 206 | "away-notify": "",
|
---|
| 207 | "extended-join": "",
|
---|
| 208 | "multi-prefix": "",
|
---|
[685] | 209 |
|
---|
| 210 | "draft/extended-monitor": "",
|
---|
[292] | 211 | }
|
---|
| 212 |
|
---|
[463] | 213 | // passthroughIsupport is the set of ISUPPORT tokens that are directly passed
|
---|
| 214 | // through from the upstream server to downstream clients.
|
---|
| 215 | //
|
---|
| 216 | // This is only effective in single-upstream mode.
|
---|
| 217 | var passthroughIsupport = map[string]bool{
|
---|
[580] | 218 | "AWAYLEN": true,
|
---|
| 219 | "BOT": true,
|
---|
| 220 | "CHANLIMIT": true,
|
---|
| 221 | "CHANMODES": true,
|
---|
| 222 | "CHANNELLEN": true,
|
---|
| 223 | "CHANTYPES": true,
|
---|
| 224 | "CLIENTTAGDENY": true,
|
---|
[683] | 225 | "ELIST": true,
|
---|
[580] | 226 | "EXCEPTS": true,
|
---|
| 227 | "EXTBAN": true,
|
---|
| 228 | "HOSTLEN": true,
|
---|
| 229 | "INVEX": true,
|
---|
| 230 | "KICKLEN": true,
|
---|
| 231 | "MAXLIST": true,
|
---|
| 232 | "MAXTARGETS": true,
|
---|
| 233 | "MODES": true,
|
---|
[684] | 234 | "MONITOR": true,
|
---|
[580] | 235 | "NAMELEN": true,
|
---|
| 236 | "NETWORK": true,
|
---|
| 237 | "NICKLEN": true,
|
---|
| 238 | "PREFIX": true,
|
---|
| 239 | "SAFELIST": true,
|
---|
| 240 | "TARGMAX": true,
|
---|
| 241 | "TOPICLEN": true,
|
---|
| 242 | "USERLEN": true,
|
---|
| 243 | "UTF8ONLY": true,
|
---|
[660] | 244 | "WHOX": true,
|
---|
[463] | 245 | }
|
---|
| 246 |
|
---|
[13] | 247 | type downstreamConn struct {
|
---|
[210] | 248 | conn
|
---|
[22] | 249 |
|
---|
[210] | 250 | id uint64
|
---|
| 251 |
|
---|
[693] | 252 | registered bool
|
---|
| 253 | user *user
|
---|
| 254 | nick string
|
---|
| 255 | nickCM string
|
---|
| 256 | rawUsername string
|
---|
| 257 | networkName string
|
---|
| 258 | clientName string
|
---|
| 259 | realname string
|
---|
| 260 | hostname string
|
---|
| 261 | password string // empty after authentication
|
---|
| 262 | network *network // can be nil
|
---|
| 263 | isMultiUpstream bool
|
---|
[105] | 264 |
|
---|
[590] | 265 | negotiatingCaps bool
|
---|
[108] | 266 | capVersion int
|
---|
[275] | 267 | supportedCaps map[string]string
|
---|
[236] | 268 | caps map[string]bool
|
---|
[108] | 269 |
|
---|
[551] | 270 | lastBatchRef uint64
|
---|
| 271 |
|
---|
[684] | 272 | monitored casemapMap
|
---|
| 273 |
|
---|
[112] | 274 | saslServer sasl.Server
|
---|
[13] | 275 | }
|
---|
| 276 |
|
---|
[347] | 277 | func newDownstreamConn(srv *Server, ic ircConn, id uint64) *downstreamConn {
|
---|
| 278 | remoteAddr := ic.RemoteAddr().String()
|
---|
[323] | 279 | logger := &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", remoteAddr)}
|
---|
[398] | 280 | options := connOptions{Logger: logger}
|
---|
[55] | 281 | dc := &downstreamConn{
|
---|
[398] | 282 | conn: *newConn(srv, ic, &options),
|
---|
[276] | 283 | id: id,
|
---|
[716] | 284 | nick: "*",
|
---|
| 285 | nickCM: "*",
|
---|
[275] | 286 | supportedCaps: make(map[string]string),
|
---|
[276] | 287 | caps: make(map[string]bool),
|
---|
[684] | 288 | monitored: newCasemapMap(0),
|
---|
[22] | 289 | }
|
---|
[323] | 290 | dc.hostname = remoteAddr
|
---|
[141] | 291 | if host, _, err := net.SplitHostPort(dc.hostname); err == nil {
|
---|
| 292 | dc.hostname = host
|
---|
| 293 | }
|
---|
[275] | 294 | for k, v := range permanentDownstreamCaps {
|
---|
| 295 | dc.supportedCaps[k] = v
|
---|
| 296 | }
|
---|
[691] | 297 | // TODO: this is racy, we should only enable chathistory after
|
---|
| 298 | // authentication and then check that user.msgStore implements
|
---|
| 299 | // chatHistoryMessageStore
|
---|
| 300 | if srv.Config().LogPath != "" {
|
---|
[319] | 301 | dc.supportedCaps["draft/chathistory"] = ""
|
---|
| 302 | }
|
---|
[55] | 303 | return dc
|
---|
[22] | 304 | }
|
---|
| 305 |
|
---|
[55] | 306 | func (dc *downstreamConn) prefix() *irc.Prefix {
|
---|
[27] | 307 | return &irc.Prefix{
|
---|
[55] | 308 | Name: dc.nick,
|
---|
[184] | 309 | User: dc.user.Username,
|
---|
[141] | 310 | Host: dc.hostname,
|
---|
[27] | 311 | }
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[90] | 314 | func (dc *downstreamConn) forEachNetwork(f func(*network)) {
|
---|
| 315 | if dc.network != nil {
|
---|
| 316 | f(dc.network)
|
---|
[693] | 317 | } else if dc.isMultiUpstream {
|
---|
[90] | 318 | dc.user.forEachNetwork(f)
|
---|
| 319 | }
|
---|
| 320 | }
|
---|
| 321 |
|
---|
[73] | 322 | func (dc *downstreamConn) forEachUpstream(f func(*upstreamConn)) {
|
---|
[693] | 323 | if dc.network == nil && !dc.isMultiUpstream {
|
---|
[532] | 324 | return
|
---|
| 325 | }
|
---|
[73] | 326 | dc.user.forEachUpstream(func(uc *upstreamConn) {
|
---|
[77] | 327 | if dc.network != nil && uc.network != dc.network {
|
---|
[73] | 328 | return
|
---|
| 329 | }
|
---|
| 330 | f(uc)
|
---|
| 331 | })
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[89] | 334 | // upstream returns the upstream connection, if any. If there are zero or if
|
---|
| 335 | // there are multiple upstream connections, it returns nil.
|
---|
| 336 | func (dc *downstreamConn) upstream() *upstreamConn {
|
---|
| 337 | if dc.network == nil {
|
---|
| 338 | return nil
|
---|
| 339 | }
|
---|
[279] | 340 | return dc.network.conn
|
---|
[89] | 341 | }
|
---|
| 342 |
|
---|
[260] | 343 | func isOurNick(net *network, nick string) bool {
|
---|
| 344 | // TODO: this doesn't account for nick changes
|
---|
| 345 | if net.conn != nil {
|
---|
[478] | 346 | return net.casemap(nick) == net.conn.nickCM
|
---|
[260] | 347 | }
|
---|
| 348 | // We're not currently connected to the upstream connection, so we don't
|
---|
| 349 | // know whether this name is our nickname. Best-effort: use the network's
|
---|
| 350 | // configured nickname and hope it was the one being used when we were
|
---|
| 351 | // connected.
|
---|
[664] | 352 | return net.casemap(nick) == net.casemap(GetNick(&net.user.User, &net.Network))
|
---|
[260] | 353 | }
|
---|
| 354 |
|
---|
[249] | 355 | // marshalEntity converts an upstream entity name (ie. channel or nick) into a
|
---|
| 356 | // downstream entity name.
|
---|
| 357 | //
|
---|
| 358 | // This involves adding a "/<network>" suffix if the entity isn't the current
|
---|
| 359 | // user.
|
---|
[260] | 360 | func (dc *downstreamConn) marshalEntity(net *network, name string) string {
|
---|
[289] | 361 | if isOurNick(net, name) {
|
---|
| 362 | return dc.nick
|
---|
| 363 | }
|
---|
[478] | 364 | name = partialCasemap(net.casemap, name)
|
---|
[257] | 365 | if dc.network != nil {
|
---|
[260] | 366 | if dc.network != net {
|
---|
[258] | 367 | panic("soju: tried to marshal an entity for another network")
|
---|
| 368 | }
|
---|
[257] | 369 | return name
|
---|
[119] | 370 | }
|
---|
[260] | 371 | return name + "/" + net.GetName()
|
---|
[119] | 372 | }
|
---|
| 373 |
|
---|
[260] | 374 | func (dc *downstreamConn) marshalUserPrefix(net *network, prefix *irc.Prefix) *irc.Prefix {
|
---|
| 375 | if isOurNick(net, prefix.Name) {
|
---|
[257] | 376 | return dc.prefix()
|
---|
| 377 | }
|
---|
[478] | 378 | prefix.Name = partialCasemap(net.casemap, prefix.Name)
|
---|
[130] | 379 | if dc.network != nil {
|
---|
[260] | 380 | if dc.network != net {
|
---|
[258] | 381 | panic("soju: tried to marshal a user prefix for another network")
|
---|
| 382 | }
|
---|
[257] | 383 | return prefix
|
---|
[119] | 384 | }
|
---|
[257] | 385 | return &irc.Prefix{
|
---|
[260] | 386 | Name: prefix.Name + "/" + net.GetName(),
|
---|
[257] | 387 | User: prefix.User,
|
---|
| 388 | Host: prefix.Host,
|
---|
| 389 | }
|
---|
[119] | 390 | }
|
---|
| 391 |
|
---|
[584] | 392 | // unmarshalEntityNetwork converts a downstream entity name (ie. channel or
|
---|
| 393 | // nick) into an upstream entity name.
|
---|
[249] | 394 | //
|
---|
| 395 | // This involves removing the "/<network>" suffix.
|
---|
[584] | 396 | func (dc *downstreamConn) unmarshalEntityNetwork(name string) (*network, string, error) {
|
---|
[464] | 397 | if dc.network != nil {
|
---|
[584] | 398 | return dc.network, name, nil
|
---|
[464] | 399 | }
|
---|
[89] | 400 |
|
---|
[584] | 401 | var net *network
|
---|
[119] | 402 | if i := strings.LastIndexByte(name, '/'); i >= 0 {
|
---|
[127] | 403 | network := name[i+1:]
|
---|
[119] | 404 | name = name[:i]
|
---|
| 405 |
|
---|
[584] | 406 | for _, n := range dc.user.networks {
|
---|
| 407 | if network == n.GetName() {
|
---|
| 408 | net = n
|
---|
| 409 | break
|
---|
[119] | 410 | }
|
---|
[584] | 411 | }
|
---|
[119] | 412 | }
|
---|
| 413 |
|
---|
[584] | 414 | if net == nil {
|
---|
[73] | 415 | return nil, "", ircError{&irc.Message{
|
---|
| 416 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
[584] | 417 | Params: []string{name, "Missing network suffix in name"},
|
---|
[73] | 418 | }}
|
---|
[69] | 419 | }
|
---|
[584] | 420 |
|
---|
| 421 | return net, name, nil
|
---|
[69] | 422 | }
|
---|
| 423 |
|
---|
[584] | 424 | // unmarshalEntity is the same as unmarshalEntityNetwork, but returns the
|
---|
| 425 | // upstream connection and fails if the upstream is disconnected.
|
---|
| 426 | func (dc *downstreamConn) unmarshalEntity(name string) (*upstreamConn, string, error) {
|
---|
| 427 | net, name, err := dc.unmarshalEntityNetwork(name)
|
---|
| 428 | if err != nil {
|
---|
| 429 | return nil, "", err
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | if net.conn == nil {
|
---|
| 433 | return nil, "", ircError{&irc.Message{
|
---|
| 434 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
| 435 | Params: []string{name, "Disconnected from upstream network"},
|
---|
| 436 | }}
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | return net.conn, name, nil
|
---|
| 440 | }
|
---|
| 441 |
|
---|
[268] | 442 | func (dc *downstreamConn) unmarshalText(uc *upstreamConn, text string) string {
|
---|
| 443 | if dc.upstream() != nil {
|
---|
| 444 | return text
|
---|
| 445 | }
|
---|
| 446 | // TODO: smarter parsing that ignores URLs
|
---|
| 447 | return strings.ReplaceAll(text, "/"+uc.network.GetName(), "")
|
---|
| 448 | }
|
---|
| 449 |
|
---|
[711] | 450 | func (dc *downstreamConn) ReadMessage() (*irc.Message, error) {
|
---|
| 451 | msg, err := dc.conn.ReadMessage()
|
---|
| 452 | if err != nil {
|
---|
| 453 | return nil, err
|
---|
| 454 | }
|
---|
| 455 | dc.srv.metrics.downstreamInMessagesTotal.Inc()
|
---|
| 456 | return msg, nil
|
---|
| 457 | }
|
---|
| 458 |
|
---|
[165] | 459 | func (dc *downstreamConn) readMessages(ch chan<- event) error {
|
---|
[22] | 460 | for {
|
---|
[210] | 461 | msg, err := dc.ReadMessage()
|
---|
[655] | 462 | if errors.Is(err, io.EOF) {
|
---|
[22] | 463 | break
|
---|
| 464 | } else if err != nil {
|
---|
| 465 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
| 466 | }
|
---|
| 467 |
|
---|
[165] | 468 | ch <- eventDownstreamMessage{msg, dc}
|
---|
[22] | 469 | }
|
---|
| 470 |
|
---|
[45] | 471 | return nil
|
---|
[22] | 472 | }
|
---|
| 473 |
|
---|
[230] | 474 | // SendMessage sends an outgoing message.
|
---|
| 475 | //
|
---|
| 476 | // This can only called from the user goroutine.
|
---|
[55] | 477 | func (dc *downstreamConn) SendMessage(msg *irc.Message) {
|
---|
[230] | 478 | if !dc.caps["message-tags"] {
|
---|
[303] | 479 | if msg.Command == "TAGMSG" {
|
---|
| 480 | return
|
---|
| 481 | }
|
---|
[216] | 482 | msg = msg.Copy()
|
---|
| 483 | for name := range msg.Tags {
|
---|
| 484 | supported := false
|
---|
| 485 | switch name {
|
---|
| 486 | case "time":
|
---|
[230] | 487 | supported = dc.caps["server-time"]
|
---|
[559] | 488 | case "account":
|
---|
| 489 | supported = dc.caps["account"]
|
---|
[216] | 490 | }
|
---|
| 491 | if !supported {
|
---|
| 492 | delete(msg.Tags, name)
|
---|
| 493 | }
|
---|
| 494 | }
|
---|
| 495 | }
|
---|
[551] | 496 | if !dc.caps["batch"] && msg.Tags["batch"] != "" {
|
---|
| 497 | msg = msg.Copy()
|
---|
| 498 | delete(msg.Tags, "batch")
|
---|
| 499 | }
|
---|
[419] | 500 | if msg.Command == "JOIN" && !dc.caps["extended-join"] {
|
---|
| 501 | msg.Params = msg.Params[:1]
|
---|
| 502 | }
|
---|
[540] | 503 | if msg.Command == "SETNAME" && !dc.caps["setname"] {
|
---|
| 504 | return
|
---|
| 505 | }
|
---|
[649] | 506 | if msg.Command == "AWAY" && !dc.caps["away-notify"] {
|
---|
| 507 | return
|
---|
| 508 | }
|
---|
[648] | 509 | if msg.Command == "ACCOUNT" && !dc.caps["account-notify"] {
|
---|
| 510 | return
|
---|
| 511 | }
|
---|
[216] | 512 |
|
---|
[711] | 513 | dc.srv.metrics.downstreamOutMessagesTotal.Inc()
|
---|
[210] | 514 | dc.conn.SendMessage(msg)
|
---|
[54] | 515 | }
|
---|
| 516 |
|
---|
[551] | 517 | func (dc *downstreamConn) SendBatch(typ string, params []string, tags irc.Tags, f func(batchRef irc.TagValue)) {
|
---|
| 518 | dc.lastBatchRef++
|
---|
| 519 | ref := fmt.Sprintf("%v", dc.lastBatchRef)
|
---|
| 520 |
|
---|
| 521 | if dc.caps["batch"] {
|
---|
| 522 | dc.SendMessage(&irc.Message{
|
---|
| 523 | Tags: tags,
|
---|
| 524 | Prefix: dc.srv.prefix(),
|
---|
| 525 | Command: "BATCH",
|
---|
| 526 | Params: append([]string{"+" + ref, typ}, params...),
|
---|
| 527 | })
|
---|
| 528 | }
|
---|
| 529 |
|
---|
| 530 | f(irc.TagValue(ref))
|
---|
| 531 |
|
---|
| 532 | if dc.caps["batch"] {
|
---|
| 533 | dc.SendMessage(&irc.Message{
|
---|
| 534 | Prefix: dc.srv.prefix(),
|
---|
| 535 | Command: "BATCH",
|
---|
| 536 | Params: []string{"-" + ref},
|
---|
| 537 | })
|
---|
| 538 | }
|
---|
| 539 | }
|
---|
| 540 |
|
---|
[428] | 541 | // sendMessageWithID sends an outgoing message with the specified internal ID.
|
---|
| 542 | func (dc *downstreamConn) sendMessageWithID(msg *irc.Message, id string) {
|
---|
| 543 | dc.SendMessage(msg)
|
---|
| 544 |
|
---|
[665] | 545 | if id == "" || !dc.messageSupportsBacklog(msg) {
|
---|
[428] | 546 | return
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | dc.sendPing(id)
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | // advanceMessageWithID advances history to the specified message ID without
|
---|
| 553 | // sending a message. This is useful e.g. for self-messages when echo-message
|
---|
| 554 | // isn't enabled.
|
---|
| 555 | func (dc *downstreamConn) advanceMessageWithID(msg *irc.Message, id string) {
|
---|
[665] | 556 | if id == "" || !dc.messageSupportsBacklog(msg) {
|
---|
[428] | 557 | return
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | dc.sendPing(id)
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | // ackMsgID acknowledges that a message has been received.
|
---|
| 564 | func (dc *downstreamConn) ackMsgID(id string) {
|
---|
[488] | 565 | netID, entity, err := parseMsgID(id, nil)
|
---|
[428] | 566 | if err != nil {
|
---|
| 567 | dc.logger.Printf("failed to ACK message ID %q: %v", id, err)
|
---|
| 568 | return
|
---|
| 569 | }
|
---|
| 570 |
|
---|
[440] | 571 | network := dc.user.getNetworkByID(netID)
|
---|
[428] | 572 | if network == nil {
|
---|
| 573 | return
|
---|
| 574 | }
|
---|
| 575 |
|
---|
[485] | 576 | network.delivered.StoreID(entity, dc.clientName, id)
|
---|
[428] | 577 | }
|
---|
| 578 |
|
---|
| 579 | func (dc *downstreamConn) sendPing(msgID string) {
|
---|
[488] | 580 | token := "soju-msgid-" + msgID
|
---|
[428] | 581 | dc.SendMessage(&irc.Message{
|
---|
| 582 | Command: "PING",
|
---|
| 583 | Params: []string{token},
|
---|
| 584 | })
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | func (dc *downstreamConn) handlePong(token string) {
|
---|
| 588 | if !strings.HasPrefix(token, "soju-msgid-") {
|
---|
| 589 | dc.logger.Printf("received unrecognized PONG token %q", token)
|
---|
| 590 | return
|
---|
| 591 | }
|
---|
[488] | 592 | msgID := strings.TrimPrefix(token, "soju-msgid-")
|
---|
[428] | 593 | dc.ackMsgID(msgID)
|
---|
| 594 | }
|
---|
| 595 |
|
---|
[245] | 596 | // marshalMessage re-formats a message coming from an upstream connection so
|
---|
| 597 | // that it's suitable for being sent on this downstream connection. Only
|
---|
[665] | 598 | // messages that may appear in logs are supported, except MODE messages which
|
---|
| 599 | // may only appear in single-upstream mode.
|
---|
[261] | 600 | func (dc *downstreamConn) marshalMessage(msg *irc.Message, net *network) *irc.Message {
|
---|
[686] | 601 | msg = msg.Copy()
|
---|
| 602 | msg.Prefix = dc.marshalUserPrefix(net, msg.Prefix)
|
---|
| 603 |
|
---|
[665] | 604 | if dc.network != nil {
|
---|
| 605 | return msg
|
---|
| 606 | }
|
---|
| 607 |
|
---|
[227] | 608 | switch msg.Command {
|
---|
[303] | 609 | case "PRIVMSG", "NOTICE", "TAGMSG":
|
---|
[261] | 610 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
[245] | 611 | case "NICK":
|
---|
| 612 | // Nick change for another user
|
---|
[261] | 613 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
[245] | 614 | case "JOIN", "PART":
|
---|
[261] | 615 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
[245] | 616 | case "KICK":
|
---|
[261] | 617 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
| 618 | msg.Params[1] = dc.marshalEntity(net, msg.Params[1])
|
---|
[245] | 619 | case "TOPIC":
|
---|
[261] | 620 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
[540] | 621 | case "QUIT", "SETNAME":
|
---|
[262] | 622 | // This space is intentionally left blank
|
---|
[227] | 623 | default:
|
---|
| 624 | panic(fmt.Sprintf("unexpected %q message", msg.Command))
|
---|
| 625 | }
|
---|
| 626 |
|
---|
[245] | 627 | return msg
|
---|
[227] | 628 | }
|
---|
| 629 |
|
---|
[704] | 630 | func (dc *downstreamConn) handleMessage(ctx context.Context, msg *irc.Message) error {
|
---|
| 631 | ctx, cancel := dc.conn.NewContext(ctx)
|
---|
[702] | 632 | defer cancel()
|
---|
| 633 |
|
---|
[703] | 634 | ctx, cancel = context.WithTimeout(ctx, handleDownstreamMessageTimeout)
|
---|
| 635 | defer cancel()
|
---|
| 636 |
|
---|
[13] | 637 | switch msg.Command {
|
---|
[28] | 638 | case "QUIT":
|
---|
[55] | 639 | return dc.Close()
|
---|
[13] | 640 | default:
|
---|
[55] | 641 | if dc.registered {
|
---|
[702] | 642 | return dc.handleMessageRegistered(ctx, msg)
|
---|
[13] | 643 | } else {
|
---|
[702] | 644 | return dc.handleMessageUnregistered(ctx, msg)
|
---|
[13] | 645 | }
|
---|
| 646 | }
|
---|
| 647 | }
|
---|
| 648 |
|
---|
[702] | 649 | func (dc *downstreamConn) handleMessageUnregistered(ctx context.Context, msg *irc.Message) error {
|
---|
[13] | 650 | switch msg.Command {
|
---|
| 651 | case "NICK":
|
---|
[117] | 652 | var nick string
|
---|
| 653 | if err := parseMessageParams(msg, &nick); err != nil {
|
---|
[43] | 654 | return err
|
---|
[13] | 655 | }
|
---|
[717] | 656 | if nick == "" || strings.ContainsAny(nick, illegalNickChars) {
|
---|
[404] | 657 | return ircError{&irc.Message{
|
---|
| 658 | Command: irc.ERR_ERRONEUSNICKNAME,
|
---|
| 659 | Params: []string{dc.nick, nick, "contains illegal characters"},
|
---|
| 660 | }}
|
---|
| 661 | }
|
---|
[478] | 662 | nickCM := casemapASCII(nick)
|
---|
| 663 | if nickCM == serviceNickCM {
|
---|
[117] | 664 | return ircError{&irc.Message{
|
---|
| 665 | Command: irc.ERR_NICKNAMEINUSE,
|
---|
| 666 | Params: []string{dc.nick, nick, "Nickname reserved for bouncer service"},
|
---|
| 667 | }}
|
---|
| 668 | }
|
---|
| 669 | dc.nick = nick
|
---|
[478] | 670 | dc.nickCM = nickCM
|
---|
[13] | 671 | case "USER":
|
---|
[117] | 672 | if err := parseMessageParams(msg, &dc.rawUsername, nil, nil, &dc.realname); err != nil {
|
---|
[43] | 673 | return err
|
---|
[13] | 674 | }
|
---|
[85] | 675 | case "PASS":
|
---|
| 676 | if err := parseMessageParams(msg, &dc.password); err != nil {
|
---|
| 677 | return err
|
---|
| 678 | }
|
---|
[108] | 679 | case "CAP":
|
---|
| 680 | var subCmd string
|
---|
| 681 | if err := parseMessageParams(msg, &subCmd); err != nil {
|
---|
| 682 | return err
|
---|
| 683 | }
|
---|
| 684 | if err := dc.handleCapCommand(subCmd, msg.Params[1:]); err != nil {
|
---|
| 685 | return err
|
---|
| 686 | }
|
---|
[112] | 687 | case "AUTHENTICATE":
|
---|
[230] | 688 | if !dc.caps["sasl"] {
|
---|
[112] | 689 | return ircError{&irc.Message{
|
---|
[716] | 690 | Prefix: dc.srv.prefix(),
|
---|
[125] | 691 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 692 | Params: []string{"*", "AUTHENTICATE requires the \"sasl\" capability to be enabled"},
|
---|
| 693 | }}
|
---|
| 694 | }
|
---|
| 695 | if len(msg.Params) == 0 {
|
---|
| 696 | return ircError{&irc.Message{
|
---|
[716] | 697 | Prefix: dc.srv.prefix(),
|
---|
[125] | 698 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 699 | Params: []string{"*", "Missing AUTHENTICATE argument"},
|
---|
| 700 | }}
|
---|
| 701 | }
|
---|
| 702 |
|
---|
| 703 | var resp []byte
|
---|
[653] | 704 | if msg.Params[0] == "*" {
|
---|
| 705 | dc.saslServer = nil
|
---|
| 706 | return ircError{&irc.Message{
|
---|
[716] | 707 | Prefix: dc.srv.prefix(),
|
---|
[653] | 708 | Command: irc.ERR_SASLABORTED,
|
---|
| 709 | Params: []string{"*", "SASL authentication aborted"},
|
---|
| 710 | }}
|
---|
| 711 | } else if dc.saslServer == nil {
|
---|
[112] | 712 | mech := strings.ToUpper(msg.Params[0])
|
---|
| 713 | switch mech {
|
---|
| 714 | case "PLAIN":
|
---|
| 715 | dc.saslServer = sasl.NewPlainServer(sasl.PlainAuthenticator(func(identity, username, password string) error {
|
---|
[700] | 716 | // TODO: we can't use the command context here, because it
|
---|
| 717 | // gets cancelled once the command handler returns. SASL
|
---|
| 718 | // might take multiple AUTHENTICATE commands to complete.
|
---|
| 719 | return dc.authenticate(context.TODO(), username, password)
|
---|
[112] | 720 | }))
|
---|
| 721 | default:
|
---|
| 722 | return ircError{&irc.Message{
|
---|
[716] | 723 | Prefix: dc.srv.prefix(),
|
---|
[125] | 724 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 725 | Params: []string{"*", fmt.Sprintf("Unsupported SASL mechanism %q", mech)},
|
---|
| 726 | }}
|
---|
| 727 | }
|
---|
| 728 | } else if msg.Params[0] == "+" {
|
---|
| 729 | resp = nil
|
---|
| 730 | } else {
|
---|
| 731 | // TODO: multi-line messages
|
---|
| 732 | var err error
|
---|
| 733 | resp, err = base64.StdEncoding.DecodeString(msg.Params[0])
|
---|
| 734 | if err != nil {
|
---|
| 735 | dc.saslServer = nil
|
---|
| 736 | return ircError{&irc.Message{
|
---|
[716] | 737 | Prefix: dc.srv.prefix(),
|
---|
[125] | 738 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 739 | Params: []string{"*", "Invalid base64-encoded response"},
|
---|
| 740 | }}
|
---|
| 741 | }
|
---|
| 742 | }
|
---|
| 743 |
|
---|
| 744 | challenge, done, err := dc.saslServer.Next(resp)
|
---|
| 745 | if err != nil {
|
---|
| 746 | dc.saslServer = nil
|
---|
| 747 | if ircErr, ok := err.(ircError); ok && ircErr.Message.Command == irc.ERR_PASSWDMISMATCH {
|
---|
| 748 | return ircError{&irc.Message{
|
---|
[716] | 749 | Prefix: dc.srv.prefix(),
|
---|
[125] | 750 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 751 | Params: []string{"*", ircErr.Message.Params[1]},
|
---|
| 752 | }}
|
---|
| 753 | }
|
---|
| 754 | dc.SendMessage(&irc.Message{
|
---|
| 755 | Prefix: dc.srv.prefix(),
|
---|
[125] | 756 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 757 | Params: []string{"*", "SASL error"},
|
---|
| 758 | })
|
---|
| 759 | return fmt.Errorf("SASL authentication failed: %v", err)
|
---|
| 760 | } else if done {
|
---|
| 761 | dc.saslServer = nil
|
---|
| 762 | dc.SendMessage(&irc.Message{
|
---|
| 763 | Prefix: dc.srv.prefix(),
|
---|
[125] | 764 | Command: irc.RPL_LOGGEDIN,
|
---|
[306] | 765 | Params: []string{dc.nick, dc.prefix().String(), dc.user.Username, "You are now logged in"},
|
---|
[112] | 766 | })
|
---|
| 767 | dc.SendMessage(&irc.Message{
|
---|
| 768 | Prefix: dc.srv.prefix(),
|
---|
[125] | 769 | Command: irc.RPL_SASLSUCCESS,
|
---|
[112] | 770 | Params: []string{dc.nick, "SASL authentication successful"},
|
---|
| 771 | })
|
---|
| 772 | } else {
|
---|
| 773 | challengeStr := "+"
|
---|
[135] | 774 | if len(challenge) > 0 {
|
---|
[112] | 775 | challengeStr = base64.StdEncoding.EncodeToString(challenge)
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 | // TODO: multi-line messages
|
---|
| 779 | dc.SendMessage(&irc.Message{
|
---|
| 780 | Prefix: dc.srv.prefix(),
|
---|
| 781 | Command: "AUTHENTICATE",
|
---|
| 782 | Params: []string{challengeStr},
|
---|
| 783 | })
|
---|
| 784 | }
|
---|
[532] | 785 | case "BOUNCER":
|
---|
| 786 | var subcommand string
|
---|
| 787 | if err := parseMessageParams(msg, &subcommand); err != nil {
|
---|
| 788 | return err
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | switch strings.ToUpper(subcommand) {
|
---|
| 792 | case "BIND":
|
---|
| 793 | var idStr string
|
---|
| 794 | if err := parseMessageParams(msg, nil, &idStr); err != nil {
|
---|
| 795 | return err
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 | if dc.user == nil {
|
---|
| 799 | return ircError{&irc.Message{
|
---|
| 800 | Command: "FAIL",
|
---|
| 801 | Params: []string{"BOUNCER", "ACCOUNT_REQUIRED", "BIND", "Authentication needed to bind to bouncer network"},
|
---|
| 802 | }}
|
---|
| 803 | }
|
---|
| 804 |
|
---|
[535] | 805 | id, err := parseBouncerNetID(subcommand, idStr)
|
---|
[532] | 806 | if err != nil {
|
---|
| 807 | return err
|
---|
| 808 | }
|
---|
| 809 |
|
---|
| 810 | var match *network
|
---|
| 811 | dc.user.forEachNetwork(func(net *network) {
|
---|
| 812 | if net.ID == id {
|
---|
| 813 | match = net
|
---|
| 814 | }
|
---|
| 815 | })
|
---|
| 816 | if match == nil {
|
---|
| 817 | return ircError{&irc.Message{
|
---|
| 818 | Command: "FAIL",
|
---|
| 819 | Params: []string{"BOUNCER", "INVALID_NETID", idStr, "Unknown network ID"},
|
---|
| 820 | }}
|
---|
| 821 | }
|
---|
| 822 |
|
---|
| 823 | dc.networkName = match.GetName()
|
---|
| 824 | }
|
---|
[13] | 825 | default:
|
---|
[55] | 826 | dc.logger.Printf("unhandled message: %v", msg)
|
---|
[13] | 827 | return newUnknownCommandError(msg.Command)
|
---|
| 828 | }
|
---|
[716] | 829 | if dc.rawUsername != "" && dc.nick != "*" && !dc.negotiatingCaps {
|
---|
[700] | 830 | return dc.register(ctx)
|
---|
[13] | 831 | }
|
---|
| 832 | return nil
|
---|
| 833 | }
|
---|
| 834 |
|
---|
[108] | 835 | func (dc *downstreamConn) handleCapCommand(cmd string, args []string) error {
|
---|
[111] | 836 | cmd = strings.ToUpper(cmd)
|
---|
| 837 |
|
---|
[108] | 838 | switch cmd {
|
---|
| 839 | case "LS":
|
---|
| 840 | if len(args) > 0 {
|
---|
| 841 | var err error
|
---|
| 842 | if dc.capVersion, err = strconv.Atoi(args[0]); err != nil {
|
---|
| 843 | return err
|
---|
| 844 | }
|
---|
| 845 | }
|
---|
[437] | 846 | if !dc.registered && dc.capVersion >= 302 {
|
---|
| 847 | // Let downstream show everything it supports, and trim
|
---|
| 848 | // down the available capabilities when upstreams are
|
---|
| 849 | // known.
|
---|
| 850 | for k, v := range needAllDownstreamCaps {
|
---|
| 851 | dc.supportedCaps[k] = v
|
---|
| 852 | }
|
---|
| 853 | }
|
---|
[108] | 854 |
|
---|
[275] | 855 | caps := make([]string, 0, len(dc.supportedCaps))
|
---|
| 856 | for k, v := range dc.supportedCaps {
|
---|
| 857 | if dc.capVersion >= 302 && v != "" {
|
---|
[276] | 858 | caps = append(caps, k+"="+v)
|
---|
[275] | 859 | } else {
|
---|
| 860 | caps = append(caps, k)
|
---|
| 861 | }
|
---|
[112] | 862 | }
|
---|
[108] | 863 |
|
---|
| 864 | // TODO: multi-line replies
|
---|
| 865 | dc.SendMessage(&irc.Message{
|
---|
| 866 | Prefix: dc.srv.prefix(),
|
---|
| 867 | Command: "CAP",
|
---|
[716] | 868 | Params: []string{dc.nick, "LS", strings.Join(caps, " ")},
|
---|
[108] | 869 | })
|
---|
| 870 |
|
---|
[275] | 871 | if dc.capVersion >= 302 {
|
---|
| 872 | // CAP version 302 implicitly enables cap-notify
|
---|
| 873 | dc.caps["cap-notify"] = true
|
---|
| 874 | }
|
---|
| 875 |
|
---|
[108] | 876 | if !dc.registered {
|
---|
[590] | 877 | dc.negotiatingCaps = true
|
---|
[108] | 878 | }
|
---|
| 879 | case "LIST":
|
---|
| 880 | var caps []string
|
---|
[521] | 881 | for name, enabled := range dc.caps {
|
---|
| 882 | if enabled {
|
---|
| 883 | caps = append(caps, name)
|
---|
| 884 | }
|
---|
[108] | 885 | }
|
---|
| 886 |
|
---|
| 887 | // TODO: multi-line replies
|
---|
| 888 | dc.SendMessage(&irc.Message{
|
---|
| 889 | Prefix: dc.srv.prefix(),
|
---|
| 890 | Command: "CAP",
|
---|
[716] | 891 | Params: []string{dc.nick, "LIST", strings.Join(caps, " ")},
|
---|
[108] | 892 | })
|
---|
| 893 | case "REQ":
|
---|
| 894 | if len(args) == 0 {
|
---|
| 895 | return ircError{&irc.Message{
|
---|
| 896 | Command: err_invalidcapcmd,
|
---|
[716] | 897 | Params: []string{dc.nick, cmd, "Missing argument in CAP REQ command"},
|
---|
[108] | 898 | }}
|
---|
| 899 | }
|
---|
| 900 |
|
---|
[275] | 901 | // TODO: atomically ack/nak the whole capability set
|
---|
[108] | 902 | caps := strings.Fields(args[0])
|
---|
| 903 | ack := true
|
---|
| 904 | for _, name := range caps {
|
---|
| 905 | name = strings.ToLower(name)
|
---|
| 906 | enable := !strings.HasPrefix(name, "-")
|
---|
| 907 | if !enable {
|
---|
| 908 | name = strings.TrimPrefix(name, "-")
|
---|
| 909 | }
|
---|
| 910 |
|
---|
[275] | 911 | if enable == dc.caps[name] {
|
---|
[108] | 912 | continue
|
---|
| 913 | }
|
---|
| 914 |
|
---|
[275] | 915 | _, ok := dc.supportedCaps[name]
|
---|
| 916 | if !ok {
|
---|
[108] | 917 | ack = false
|
---|
[275] | 918 | break
|
---|
[108] | 919 | }
|
---|
[275] | 920 |
|
---|
| 921 | if name == "cap-notify" && dc.capVersion >= 302 && !enable {
|
---|
| 922 | // cap-notify cannot be disabled with CAP version 302
|
---|
| 923 | ack = false
|
---|
| 924 | break
|
---|
| 925 | }
|
---|
| 926 |
|
---|
| 927 | dc.caps[name] = enable
|
---|
[108] | 928 | }
|
---|
| 929 |
|
---|
| 930 | reply := "NAK"
|
---|
| 931 | if ack {
|
---|
| 932 | reply = "ACK"
|
---|
| 933 | }
|
---|
| 934 | dc.SendMessage(&irc.Message{
|
---|
| 935 | Prefix: dc.srv.prefix(),
|
---|
| 936 | Command: "CAP",
|
---|
[716] | 937 | Params: []string{dc.nick, reply, args[0]},
|
---|
[108] | 938 | })
|
---|
[590] | 939 |
|
---|
| 940 | if !dc.registered {
|
---|
| 941 | dc.negotiatingCaps = true
|
---|
| 942 | }
|
---|
[108] | 943 | case "END":
|
---|
[590] | 944 | dc.negotiatingCaps = false
|
---|
[108] | 945 | default:
|
---|
| 946 | return ircError{&irc.Message{
|
---|
| 947 | Command: err_invalidcapcmd,
|
---|
[716] | 948 | Params: []string{dc.nick, cmd, "Unknown CAP command"},
|
---|
[108] | 949 | }}
|
---|
| 950 | }
|
---|
| 951 | return nil
|
---|
| 952 | }
|
---|
| 953 |
|
---|
[275] | 954 | func (dc *downstreamConn) setSupportedCap(name, value string) {
|
---|
| 955 | prevValue, hasPrev := dc.supportedCaps[name]
|
---|
| 956 | changed := !hasPrev || prevValue != value
|
---|
| 957 | dc.supportedCaps[name] = value
|
---|
| 958 |
|
---|
| 959 | if !dc.caps["cap-notify"] || !changed {
|
---|
| 960 | return
|
---|
| 961 | }
|
---|
| 962 |
|
---|
| 963 | cap := name
|
---|
| 964 | if value != "" && dc.capVersion >= 302 {
|
---|
| 965 | cap = name + "=" + value
|
---|
| 966 | }
|
---|
| 967 |
|
---|
| 968 | dc.SendMessage(&irc.Message{
|
---|
| 969 | Prefix: dc.srv.prefix(),
|
---|
| 970 | Command: "CAP",
|
---|
[716] | 971 | Params: []string{dc.nick, "NEW", cap},
|
---|
[275] | 972 | })
|
---|
| 973 | }
|
---|
| 974 |
|
---|
| 975 | func (dc *downstreamConn) unsetSupportedCap(name string) {
|
---|
| 976 | _, hasPrev := dc.supportedCaps[name]
|
---|
| 977 | delete(dc.supportedCaps, name)
|
---|
| 978 | delete(dc.caps, name)
|
---|
| 979 |
|
---|
| 980 | if !dc.caps["cap-notify"] || !hasPrev {
|
---|
| 981 | return
|
---|
| 982 | }
|
---|
| 983 |
|
---|
| 984 | dc.SendMessage(&irc.Message{
|
---|
| 985 | Prefix: dc.srv.prefix(),
|
---|
| 986 | Command: "CAP",
|
---|
[716] | 987 | Params: []string{dc.nick, "DEL", name},
|
---|
[275] | 988 | })
|
---|
| 989 | }
|
---|
| 990 |
|
---|
[276] | 991 | func (dc *downstreamConn) updateSupportedCaps() {
|
---|
[292] | 992 | supportedCaps := make(map[string]bool)
|
---|
| 993 | for cap := range needAllDownstreamCaps {
|
---|
| 994 | supportedCaps[cap] = true
|
---|
| 995 | }
|
---|
[276] | 996 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[292] | 997 | for cap, supported := range supportedCaps {
|
---|
| 998 | supportedCaps[cap] = supported && uc.caps[cap]
|
---|
| 999 | }
|
---|
[276] | 1000 | })
|
---|
| 1001 |
|
---|
[292] | 1002 | for cap, supported := range supportedCaps {
|
---|
| 1003 | if supported {
|
---|
| 1004 | dc.setSupportedCap(cap, needAllDownstreamCaps[cap])
|
---|
| 1005 | } else {
|
---|
| 1006 | dc.unsetSupportedCap(cap)
|
---|
| 1007 | }
|
---|
[276] | 1008 | }
|
---|
[665] | 1009 |
|
---|
[691] | 1010 | if _, ok := dc.user.msgStore.(chatHistoryMessageStore); ok && dc.network != nil {
|
---|
[665] | 1011 | dc.setSupportedCap("draft/event-playback", "")
|
---|
| 1012 | } else {
|
---|
| 1013 | dc.unsetSupportedCap("draft/event-playback")
|
---|
| 1014 | }
|
---|
[276] | 1015 | }
|
---|
| 1016 |
|
---|
[296] | 1017 | func (dc *downstreamConn) updateNick() {
|
---|
| 1018 | if uc := dc.upstream(); uc != nil && uc.nick != dc.nick {
|
---|
| 1019 | dc.SendMessage(&irc.Message{
|
---|
| 1020 | Prefix: dc.prefix(),
|
---|
| 1021 | Command: "NICK",
|
---|
| 1022 | Params: []string{uc.nick},
|
---|
| 1023 | })
|
---|
| 1024 | dc.nick = uc.nick
|
---|
[478] | 1025 | dc.nickCM = casemapASCII(dc.nick)
|
---|
[296] | 1026 | }
|
---|
| 1027 | }
|
---|
| 1028 |
|
---|
[540] | 1029 | func (dc *downstreamConn) updateRealname() {
|
---|
| 1030 | if uc := dc.upstream(); uc != nil && uc.realname != dc.realname && dc.caps["setname"] {
|
---|
| 1031 | dc.SendMessage(&irc.Message{
|
---|
| 1032 | Prefix: dc.prefix(),
|
---|
| 1033 | Command: "SETNAME",
|
---|
| 1034 | Params: []string{uc.realname},
|
---|
| 1035 | })
|
---|
| 1036 | dc.realname = uc.realname
|
---|
| 1037 | }
|
---|
| 1038 | }
|
---|
| 1039 |
|
---|
[698] | 1040 | func sanityCheckServer(ctx context.Context, addr string) error {
|
---|
[699] | 1041 | ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
---|
[698] | 1042 | defer cancel()
|
---|
| 1043 |
|
---|
| 1044 | conn, err := new(tls.Dialer).DialContext(ctx, "tcp", addr)
|
---|
[91] | 1045 | if err != nil {
|
---|
| 1046 | return err
|
---|
| 1047 | }
|
---|
[698] | 1048 |
|
---|
[91] | 1049 | return conn.Close()
|
---|
| 1050 | }
|
---|
| 1051 |
|
---|
[183] | 1052 | func unmarshalUsername(rawUsername string) (username, client, network string) {
|
---|
[112] | 1053 | username = rawUsername
|
---|
[183] | 1054 |
|
---|
| 1055 | i := strings.IndexAny(username, "/@")
|
---|
| 1056 | j := strings.LastIndexAny(username, "/@")
|
---|
| 1057 | if i >= 0 {
|
---|
| 1058 | username = rawUsername[:i]
|
---|
[73] | 1059 | }
|
---|
[183] | 1060 | if j >= 0 {
|
---|
[190] | 1061 | if rawUsername[j] == '@' {
|
---|
| 1062 | client = rawUsername[j+1:]
|
---|
| 1063 | } else {
|
---|
| 1064 | network = rawUsername[j+1:]
|
---|
| 1065 | }
|
---|
[73] | 1066 | }
|
---|
[183] | 1067 | if i >= 0 && j >= 0 && i < j {
|
---|
[190] | 1068 | if rawUsername[i] == '@' {
|
---|
| 1069 | client = rawUsername[i+1 : j]
|
---|
| 1070 | } else {
|
---|
| 1071 | network = rawUsername[i+1 : j]
|
---|
| 1072 | }
|
---|
[183] | 1073 | }
|
---|
| 1074 |
|
---|
| 1075 | return username, client, network
|
---|
[112] | 1076 | }
|
---|
[73] | 1077 |
|
---|
[700] | 1078 | func (dc *downstreamConn) authenticate(ctx context.Context, username, password string) error {
|
---|
[183] | 1079 | username, clientName, networkName := unmarshalUsername(username)
|
---|
[168] | 1080 |
|
---|
[700] | 1081 | u, err := dc.srv.db.GetUser(ctx, username)
|
---|
[173] | 1082 | if err != nil {
|
---|
[438] | 1083 | dc.logger.Printf("failed authentication for %q: user not found: %v", username, err)
|
---|
[168] | 1084 | return errAuthFailed
|
---|
| 1085 | }
|
---|
| 1086 |
|
---|
[322] | 1087 | // Password auth disabled
|
---|
| 1088 | if u.Password == "" {
|
---|
| 1089 | return errAuthFailed
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
[173] | 1092 | err = bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
---|
[168] | 1093 | if err != nil {
|
---|
[438] | 1094 | dc.logger.Printf("failed authentication for %q: wrong password: %v", username, err)
|
---|
[168] | 1095 | return errAuthFailed
|
---|
| 1096 | }
|
---|
| 1097 |
|
---|
[173] | 1098 | dc.user = dc.srv.getUser(username)
|
---|
| 1099 | if dc.user == nil {
|
---|
| 1100 | dc.logger.Printf("failed authentication for %q: user not active", username)
|
---|
| 1101 | return errAuthFailed
|
---|
| 1102 | }
|
---|
[183] | 1103 | dc.clientName = clientName
|
---|
[168] | 1104 | dc.networkName = networkName
|
---|
| 1105 | return nil
|
---|
| 1106 | }
|
---|
| 1107 |
|
---|
[700] | 1108 | func (dc *downstreamConn) register(ctx context.Context) error {
|
---|
[168] | 1109 | if dc.registered {
|
---|
| 1110 | return fmt.Errorf("tried to register twice")
|
---|
| 1111 | }
|
---|
| 1112 |
|
---|
| 1113 | password := dc.password
|
---|
| 1114 | dc.password = ""
|
---|
| 1115 | if dc.user == nil {
|
---|
[700] | 1116 | if err := dc.authenticate(ctx, dc.rawUsername, password); err != nil {
|
---|
[168] | 1117 | return err
|
---|
| 1118 | }
|
---|
| 1119 | }
|
---|
| 1120 |
|
---|
[183] | 1121 | if dc.clientName == "" && dc.networkName == "" {
|
---|
| 1122 | _, dc.clientName, dc.networkName = unmarshalUsername(dc.rawUsername)
|
---|
[168] | 1123 | }
|
---|
| 1124 |
|
---|
| 1125 | dc.registered = true
|
---|
[184] | 1126 | dc.logger.Printf("registration complete for user %q", dc.user.Username)
|
---|
[168] | 1127 | return nil
|
---|
| 1128 | }
|
---|
| 1129 |
|
---|
[701] | 1130 | func (dc *downstreamConn) loadNetwork(ctx context.Context) error {
|
---|
[168] | 1131 | if dc.networkName == "" {
|
---|
[112] | 1132 | return nil
|
---|
| 1133 | }
|
---|
[85] | 1134 |
|
---|
[168] | 1135 | network := dc.user.getNetwork(dc.networkName)
|
---|
[112] | 1136 | if network == nil {
|
---|
[168] | 1137 | addr := dc.networkName
|
---|
[112] | 1138 | if !strings.ContainsRune(addr, ':') {
|
---|
| 1139 | addr = addr + ":6697"
|
---|
| 1140 | }
|
---|
| 1141 |
|
---|
| 1142 | dc.logger.Printf("trying to connect to new network %q", addr)
|
---|
[701] | 1143 | if err := sanityCheckServer(ctx, addr); err != nil {
|
---|
[112] | 1144 | dc.logger.Printf("failed to connect to %q: %v", addr, err)
|
---|
| 1145 | return ircError{&irc.Message{
|
---|
| 1146 | Command: irc.ERR_PASSWDMISMATCH,
|
---|
[168] | 1147 | Params: []string{"*", fmt.Sprintf("Failed to connect to %q", dc.networkName)},
|
---|
[112] | 1148 | }}
|
---|
| 1149 | }
|
---|
| 1150 |
|
---|
[354] | 1151 | // Some clients only allow specifying the nickname (and use the
|
---|
| 1152 | // nickname as a username too). Strip the network name from the
|
---|
| 1153 | // nickname when auto-saving networks.
|
---|
| 1154 | nick, _, _ := unmarshalUsername(dc.nick)
|
---|
| 1155 |
|
---|
[168] | 1156 | dc.logger.Printf("auto-saving network %q", dc.networkName)
|
---|
[112] | 1157 | var err error
|
---|
[701] | 1158 | network, err = dc.user.createNetwork(ctx, &Network{
|
---|
[542] | 1159 | Addr: dc.networkName,
|
---|
| 1160 | Nick: nick,
|
---|
| 1161 | Enabled: true,
|
---|
[120] | 1162 | })
|
---|
[112] | 1163 | if err != nil {
|
---|
| 1164 | return err
|
---|
| 1165 | }
|
---|
| 1166 | }
|
---|
| 1167 |
|
---|
| 1168 | dc.network = network
|
---|
| 1169 | return nil
|
---|
| 1170 | }
|
---|
| 1171 |
|
---|
[701] | 1172 | func (dc *downstreamConn) welcome(ctx context.Context) error {
|
---|
[168] | 1173 | if dc.user == nil || !dc.registered {
|
---|
| 1174 | panic("tried to welcome an unregistered connection")
|
---|
[37] | 1175 | }
|
---|
| 1176 |
|
---|
[168] | 1177 | // TODO: doing this might take some time. We should do it in dc.register
|
---|
| 1178 | // instead, but we'll potentially be adding a new network and this must be
|
---|
| 1179 | // done in the user goroutine.
|
---|
[701] | 1180 | if err := dc.loadNetwork(ctx); err != nil {
|
---|
[168] | 1181 | return err
|
---|
[85] | 1182 | }
|
---|
| 1183 |
|
---|
[694] | 1184 | if dc.network == nil && !dc.caps["soju.im/bouncer-networks"] && dc.srv.Config().MultiUpstream {
|
---|
[693] | 1185 | dc.isMultiUpstream = true
|
---|
| 1186 | }
|
---|
| 1187 |
|
---|
[706] | 1188 | dc.updateSupportedCaps()
|
---|
| 1189 |
|
---|
[446] | 1190 | isupport := []string{
|
---|
[670] | 1191 | fmt.Sprintf("CHATHISTORY=%v", chatHistoryLimit),
|
---|
[478] | 1192 | "CASEMAPPING=ascii",
|
---|
[446] | 1193 | }
|
---|
| 1194 |
|
---|
[532] | 1195 | if dc.network != nil {
|
---|
| 1196 | isupport = append(isupport, fmt.Sprintf("BOUNCER_NETID=%v", dc.network.ID))
|
---|
| 1197 | }
|
---|
[691] | 1198 | if title := dc.srv.Config().Title; dc.network == nil && title != "" {
|
---|
| 1199 | isupport = append(isupport, "NETWORK="+encodeISUPPORT(title))
|
---|
[662] | 1200 | }
|
---|
[693] | 1201 | if dc.network == nil && !dc.isMultiUpstream {
|
---|
[660] | 1202 | isupport = append(isupport, "WHOX")
|
---|
| 1203 | }
|
---|
| 1204 |
|
---|
[463] | 1205 | if uc := dc.upstream(); uc != nil {
|
---|
| 1206 | for k := range passthroughIsupport {
|
---|
| 1207 | v, ok := uc.isupport[k]
|
---|
| 1208 | if !ok {
|
---|
| 1209 | continue
|
---|
| 1210 | }
|
---|
| 1211 | if v != nil {
|
---|
| 1212 | isupport = append(isupport, fmt.Sprintf("%v=%v", k, *v))
|
---|
| 1213 | } else {
|
---|
| 1214 | isupport = append(isupport, k)
|
---|
| 1215 | }
|
---|
| 1216 | }
|
---|
[447] | 1217 | }
|
---|
| 1218 |
|
---|
[55] | 1219 | dc.SendMessage(&irc.Message{
|
---|
| 1220 | Prefix: dc.srv.prefix(),
|
---|
[13] | 1221 | Command: irc.RPL_WELCOME,
|
---|
[98] | 1222 | Params: []string{dc.nick, "Welcome to soju, " + dc.nick},
|
---|
[54] | 1223 | })
|
---|
[55] | 1224 | dc.SendMessage(&irc.Message{
|
---|
| 1225 | Prefix: dc.srv.prefix(),
|
---|
[13] | 1226 | Command: irc.RPL_YOURHOST,
|
---|
[691] | 1227 | Params: []string{dc.nick, "Your host is " + dc.srv.Config().Hostname},
|
---|
[54] | 1228 | })
|
---|
[55] | 1229 | dc.SendMessage(&irc.Message{
|
---|
| 1230 | Prefix: dc.srv.prefix(),
|
---|
[13] | 1231 | Command: irc.RPL_MYINFO,
|
---|
[691] | 1232 | Params: []string{dc.nick, dc.srv.Config().Hostname, "soju", "aiwroO", "OovaimnqpsrtklbeI"},
|
---|
[54] | 1233 | })
|
---|
[463] | 1234 | for _, msg := range generateIsupport(dc.srv.prefix(), dc.nick, isupport) {
|
---|
| 1235 | dc.SendMessage(msg)
|
---|
| 1236 | }
|
---|
[553] | 1237 | if uc := dc.upstream(); uc != nil {
|
---|
| 1238 | dc.SendMessage(&irc.Message{
|
---|
| 1239 | Prefix: dc.srv.prefix(),
|
---|
| 1240 | Command: irc.RPL_UMODEIS,
|
---|
[672] | 1241 | Params: []string{dc.nick, "+" + string(uc.modes)},
|
---|
[553] | 1242 | })
|
---|
| 1243 | }
|
---|
[693] | 1244 | if dc.network == nil && !dc.isMultiUpstream && dc.user.Admin {
|
---|
[671] | 1245 | dc.SendMessage(&irc.Message{
|
---|
| 1246 | Prefix: dc.srv.prefix(),
|
---|
| 1247 | Command: irc.RPL_UMODEIS,
|
---|
| 1248 | Params: []string{dc.nick, "+o"},
|
---|
| 1249 | })
|
---|
| 1250 | }
|
---|
[13] | 1251 |
|
---|
[706] | 1252 | dc.updateNick()
|
---|
| 1253 | dc.updateRealname()
|
---|
| 1254 |
|
---|
[691] | 1255 | if motd := dc.user.srv.Config().MOTD; motd != "" && dc.network == nil {
|
---|
[636] | 1256 | for _, msg := range generateMOTD(dc.srv.prefix(), dc.nick, motd) {
|
---|
| 1257 | dc.SendMessage(msg)
|
---|
| 1258 | }
|
---|
| 1259 | } else {
|
---|
| 1260 | motdHint := "No MOTD"
|
---|
| 1261 | if dc.network != nil {
|
---|
| 1262 | motdHint = "Use /motd to read the message of the day"
|
---|
| 1263 | }
|
---|
| 1264 | dc.SendMessage(&irc.Message{
|
---|
| 1265 | Prefix: dc.srv.prefix(),
|
---|
| 1266 | Command: irc.ERR_NOMOTD,
|
---|
| 1267 | Params: []string{dc.nick, motdHint},
|
---|
| 1268 | })
|
---|
| 1269 | }
|
---|
| 1270 |
|
---|
[535] | 1271 | if dc.caps["soju.im/bouncer-networks-notify"] {
|
---|
[551] | 1272 | dc.SendBatch("soju.im/bouncer-networks", nil, nil, func(batchRef irc.TagValue) {
|
---|
| 1273 | dc.user.forEachNetwork(func(network *network) {
|
---|
| 1274 | idStr := fmt.Sprintf("%v", network.ID)
|
---|
| 1275 | attrs := getNetworkAttrs(network)
|
---|
| 1276 | dc.SendMessage(&irc.Message{
|
---|
| 1277 | Tags: irc.Tags{"batch": batchRef},
|
---|
| 1278 | Prefix: dc.srv.prefix(),
|
---|
| 1279 | Command: "BOUNCER",
|
---|
| 1280 | Params: []string{"NETWORK", idStr, attrs.String()},
|
---|
| 1281 | })
|
---|
[535] | 1282 | })
|
---|
| 1283 | })
|
---|
| 1284 | }
|
---|
| 1285 |
|
---|
[73] | 1286 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[478] | 1287 | for _, entry := range uc.channels.innerMap {
|
---|
| 1288 | ch := entry.value.(*upstreamChannel)
|
---|
[284] | 1289 | if !ch.complete {
|
---|
| 1290 | continue
|
---|
| 1291 | }
|
---|
[478] | 1292 | record := uc.network.channels.Value(ch.Name)
|
---|
| 1293 | if record != nil && record.Detached {
|
---|
[284] | 1294 | continue
|
---|
| 1295 | }
|
---|
[132] | 1296 |
|
---|
[284] | 1297 | dc.SendMessage(&irc.Message{
|
---|
| 1298 | Prefix: dc.prefix(),
|
---|
| 1299 | Command: "JOIN",
|
---|
| 1300 | Params: []string{dc.marshalEntity(ch.conn.network, ch.Name)},
|
---|
| 1301 | })
|
---|
| 1302 |
|
---|
| 1303 | forwardChannel(dc, ch)
|
---|
[30] | 1304 | }
|
---|
[143] | 1305 | })
|
---|
[50] | 1306 |
|
---|
[143] | 1307 | dc.forEachNetwork(func(net *network) {
|
---|
[496] | 1308 | if dc.caps["draft/chathistory"] || dc.user.msgStore == nil {
|
---|
| 1309 | return
|
---|
| 1310 | }
|
---|
| 1311 |
|
---|
[253] | 1312 | // Only send history if we're the first connected client with that name
|
---|
| 1313 | // for the network
|
---|
[482] | 1314 | firstClient := true
|
---|
| 1315 | dc.user.forEachDownstream(func(c *downstreamConn) {
|
---|
| 1316 | if c != dc && c.clientName == dc.clientName && c.network == dc.network {
|
---|
| 1317 | firstClient = false
|
---|
| 1318 | }
|
---|
| 1319 | })
|
---|
| 1320 | if firstClient {
|
---|
[485] | 1321 | net.delivered.ForEachTarget(func(target string) {
|
---|
[495] | 1322 | lastDelivered := net.delivered.LoadID(target, dc.clientName)
|
---|
| 1323 | if lastDelivered == "" {
|
---|
| 1324 | return
|
---|
| 1325 | }
|
---|
| 1326 |
|
---|
[701] | 1327 | dc.sendTargetBacklog(ctx, net, target, lastDelivered)
|
---|
[495] | 1328 |
|
---|
| 1329 | // Fast-forward history to last message
|
---|
| 1330 | targetCM := net.casemap(target)
|
---|
[666] | 1331 | lastID, err := dc.user.msgStore.LastMsgID(&net.Network, targetCM, time.Now())
|
---|
[495] | 1332 | if err != nil {
|
---|
| 1333 | dc.logger.Printf("failed to get last message ID: %v", err)
|
---|
| 1334 | return
|
---|
| 1335 | }
|
---|
| 1336 | net.delivered.StoreID(target, dc.clientName, lastID)
|
---|
[485] | 1337 | })
|
---|
[227] | 1338 | }
|
---|
[253] | 1339 | })
|
---|
[57] | 1340 |
|
---|
[253] | 1341 | return nil
|
---|
| 1342 | }
|
---|
[144] | 1343 |
|
---|
[665] | 1344 | // messageSupportsBacklog checks whether the provided message can be sent as
|
---|
[428] | 1345 | // part of an history batch.
|
---|
[665] | 1346 | func (dc *downstreamConn) messageSupportsBacklog(msg *irc.Message) bool {
|
---|
[428] | 1347 | // Don't replay all messages, because that would mess up client
|
---|
| 1348 | // state. For instance we just sent the list of users, sending
|
---|
| 1349 | // PART messages for one of these users would be incorrect.
|
---|
| 1350 | switch msg.Command {
|
---|
| 1351 | case "PRIVMSG", "NOTICE":
|
---|
| 1352 | return true
|
---|
| 1353 | }
|
---|
| 1354 | return false
|
---|
| 1355 | }
|
---|
| 1356 |
|
---|
[701] | 1357 | func (dc *downstreamConn) sendTargetBacklog(ctx context.Context, net *network, target, msgID string) {
|
---|
[423] | 1358 | if dc.caps["draft/chathistory"] || dc.user.msgStore == nil {
|
---|
[319] | 1359 | return
|
---|
| 1360 | }
|
---|
[485] | 1361 |
|
---|
[499] | 1362 | ch := net.channels.Value(target)
|
---|
| 1363 |
|
---|
[701] | 1364 | ctx, cancel := context.WithTimeout(ctx, backlogTimeout)
|
---|
[667] | 1365 | defer cancel()
|
---|
| 1366 |
|
---|
[484] | 1367 | targetCM := net.casemap(target)
|
---|
[670] | 1368 | history, err := dc.user.msgStore.LoadLatestID(ctx, &net.Network, targetCM, msgID, backlogLimit)
|
---|
[452] | 1369 | if err != nil {
|
---|
[495] | 1370 | dc.logger.Printf("failed to send backlog for %q: %v", target, err)
|
---|
[452] | 1371 | return
|
---|
| 1372 | }
|
---|
[253] | 1373 |
|
---|
[551] | 1374 | dc.SendBatch("chathistory", []string{dc.marshalEntity(net, target)}, nil, func(batchRef irc.TagValue) {
|
---|
| 1375 | for _, msg := range history {
|
---|
| 1376 | if ch != nil && ch.Detached {
|
---|
| 1377 | if net.detachedMessageNeedsRelay(ch, msg) {
|
---|
| 1378 | dc.relayDetachedMessage(net, msg)
|
---|
| 1379 | }
|
---|
| 1380 | } else {
|
---|
[651] | 1381 | msg.Tags["batch"] = batchRef
|
---|
[551] | 1382 | dc.SendMessage(dc.marshalMessage(msg, net))
|
---|
[499] | 1383 | }
|
---|
[256] | 1384 | }
|
---|
[551] | 1385 | })
|
---|
[13] | 1386 | }
|
---|
| 1387 |
|
---|
[499] | 1388 | func (dc *downstreamConn) relayDetachedMessage(net *network, msg *irc.Message) {
|
---|
| 1389 | if msg.Command != "PRIVMSG" && msg.Command != "NOTICE" {
|
---|
| 1390 | return
|
---|
| 1391 | }
|
---|
| 1392 |
|
---|
| 1393 | sender := msg.Prefix.Name
|
---|
| 1394 | target, text := msg.Params[0], msg.Params[1]
|
---|
| 1395 | if net.isHighlight(msg) {
|
---|
| 1396 | sendServiceNOTICE(dc, fmt.Sprintf("highlight in %v: <%v> %v", dc.marshalEntity(net, target), sender, text))
|
---|
| 1397 | } else {
|
---|
| 1398 | sendServiceNOTICE(dc, fmt.Sprintf("message in %v: <%v> %v", dc.marshalEntity(net, target), sender, text))
|
---|
| 1399 | }
|
---|
| 1400 | }
|
---|
| 1401 |
|
---|
[103] | 1402 | func (dc *downstreamConn) runUntilRegistered() error {
|
---|
[704] | 1403 | ctx, cancel := context.WithTimeout(context.TODO(), downstreamRegisterTimeout)
|
---|
| 1404 | defer cancel()
|
---|
| 1405 |
|
---|
| 1406 | // Close the connection with an error if the deadline is exceeded
|
---|
| 1407 | go func() {
|
---|
| 1408 | <-ctx.Done()
|
---|
| 1409 | if err := ctx.Err(); err == context.DeadlineExceeded {
|
---|
| 1410 | dc.SendMessage(&irc.Message{
|
---|
| 1411 | Prefix: dc.srv.prefix(),
|
---|
| 1412 | Command: "ERROR",
|
---|
| 1413 | Params: []string{"Connection registration timed out"},
|
---|
| 1414 | })
|
---|
| 1415 | dc.Close()
|
---|
| 1416 | }
|
---|
| 1417 | }()
|
---|
| 1418 |
|
---|
[103] | 1419 | for !dc.registered {
|
---|
[212] | 1420 | msg, err := dc.ReadMessage()
|
---|
[106] | 1421 | if err != nil {
|
---|
[655] | 1422 | return fmt.Errorf("failed to read IRC command: %w", err)
|
---|
[103] | 1423 | }
|
---|
| 1424 |
|
---|
[704] | 1425 | err = dc.handleMessage(ctx, msg)
|
---|
[103] | 1426 | if ircErr, ok := err.(ircError); ok {
|
---|
| 1427 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
| 1428 | dc.SendMessage(ircErr.Message)
|
---|
| 1429 | } else if err != nil {
|
---|
| 1430 | return fmt.Errorf("failed to handle IRC command %q: %v", msg, err)
|
---|
| 1431 | }
|
---|
| 1432 | }
|
---|
| 1433 |
|
---|
| 1434 | return nil
|
---|
| 1435 | }
|
---|
| 1436 |
|
---|
[702] | 1437 | func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.Message) error {
|
---|
[13] | 1438 | switch msg.Command {
|
---|
[111] | 1439 | case "CAP":
|
---|
| 1440 | var subCmd string
|
---|
| 1441 | if err := parseMessageParams(msg, &subCmd); err != nil {
|
---|
| 1442 | return err
|
---|
| 1443 | }
|
---|
| 1444 | if err := dc.handleCapCommand(subCmd, msg.Params[1:]); err != nil {
|
---|
| 1445 | return err
|
---|
| 1446 | }
|
---|
[107] | 1447 | case "PING":
|
---|
[412] | 1448 | var source, destination string
|
---|
| 1449 | if err := parseMessageParams(msg, &source); err != nil {
|
---|
| 1450 | return err
|
---|
| 1451 | }
|
---|
| 1452 | if len(msg.Params) > 1 {
|
---|
| 1453 | destination = msg.Params[1]
|
---|
| 1454 | }
|
---|
[691] | 1455 | hostname := dc.srv.Config().Hostname
|
---|
| 1456 | if destination != "" && destination != hostname {
|
---|
[412] | 1457 | return ircError{&irc.Message{
|
---|
| 1458 | Command: irc.ERR_NOSUCHSERVER,
|
---|
[413] | 1459 | Params: []string{dc.nick, destination, "No such server"},
|
---|
[412] | 1460 | }}
|
---|
| 1461 | }
|
---|
[107] | 1462 | dc.SendMessage(&irc.Message{
|
---|
| 1463 | Prefix: dc.srv.prefix(),
|
---|
| 1464 | Command: "PONG",
|
---|
[691] | 1465 | Params: []string{hostname, source},
|
---|
[107] | 1466 | })
|
---|
| 1467 | return nil
|
---|
[428] | 1468 | case "PONG":
|
---|
| 1469 | if len(msg.Params) == 0 {
|
---|
| 1470 | return newNeedMoreParamsError(msg.Command)
|
---|
| 1471 | }
|
---|
| 1472 | token := msg.Params[len(msg.Params)-1]
|
---|
| 1473 | dc.handlePong(token)
|
---|
[42] | 1474 | case "USER":
|
---|
[13] | 1475 | return ircError{&irc.Message{
|
---|
| 1476 | Command: irc.ERR_ALREADYREGISTERED,
|
---|
[55] | 1477 | Params: []string{dc.nick, "You may not reregister"},
|
---|
[13] | 1478 | }}
|
---|
[42] | 1479 | case "NICK":
|
---|
[429] | 1480 | var rawNick string
|
---|
| 1481 | if err := parseMessageParams(msg, &rawNick); err != nil {
|
---|
[90] | 1482 | return err
|
---|
| 1483 | }
|
---|
| 1484 |
|
---|
[429] | 1485 | nick := rawNick
|
---|
[297] | 1486 | var upstream *upstreamConn
|
---|
| 1487 | if dc.upstream() == nil {
|
---|
| 1488 | uc, unmarshaledNick, err := dc.unmarshalEntity(nick)
|
---|
| 1489 | if err == nil { // NICK nick/network: NICK only on a specific upstream
|
---|
| 1490 | upstream = uc
|
---|
| 1491 | nick = unmarshaledNick
|
---|
| 1492 | }
|
---|
| 1493 | }
|
---|
| 1494 |
|
---|
[717] | 1495 | if nick == "" || strings.ContainsAny(nick, illegalNickChars) {
|
---|
[404] | 1496 | return ircError{&irc.Message{
|
---|
| 1497 | Command: irc.ERR_ERRONEUSNICKNAME,
|
---|
[430] | 1498 | Params: []string{dc.nick, rawNick, "contains illegal characters"},
|
---|
[404] | 1499 | }}
|
---|
| 1500 | }
|
---|
[478] | 1501 | if casemapASCII(nick) == serviceNickCM {
|
---|
[429] | 1502 | return ircError{&irc.Message{
|
---|
| 1503 | Command: irc.ERR_NICKNAMEINUSE,
|
---|
| 1504 | Params: []string{dc.nick, rawNick, "Nickname reserved for bouncer service"},
|
---|
| 1505 | }}
|
---|
| 1506 | }
|
---|
[404] | 1507 |
|
---|
[90] | 1508 | var err error
|
---|
| 1509 | dc.forEachNetwork(func(n *network) {
|
---|
[297] | 1510 | if err != nil || (upstream != nil && upstream.network != n) {
|
---|
[90] | 1511 | return
|
---|
| 1512 | }
|
---|
| 1513 | n.Nick = nick
|
---|
[675] | 1514 | err = dc.srv.db.StoreNetwork(ctx, dc.user.ID, &n.Network)
|
---|
[90] | 1515 | })
|
---|
| 1516 | if err != nil {
|
---|
| 1517 | return err
|
---|
| 1518 | }
|
---|
| 1519 |
|
---|
[73] | 1520 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[297] | 1521 | if upstream != nil && upstream != uc {
|
---|
| 1522 | return
|
---|
| 1523 | }
|
---|
[301] | 1524 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[297] | 1525 | Command: "NICK",
|
---|
| 1526 | Params: []string{nick},
|
---|
| 1527 | })
|
---|
[42] | 1528 | })
|
---|
[296] | 1529 |
|
---|
[512] | 1530 | if dc.upstream() == nil && upstream == nil && dc.nick != nick {
|
---|
[296] | 1531 | dc.SendMessage(&irc.Message{
|
---|
| 1532 | Prefix: dc.prefix(),
|
---|
| 1533 | Command: "NICK",
|
---|
| 1534 | Params: []string{nick},
|
---|
| 1535 | })
|
---|
| 1536 | dc.nick = nick
|
---|
[478] | 1537 | dc.nickCM = casemapASCII(dc.nick)
|
---|
[296] | 1538 | }
|
---|
[540] | 1539 | case "SETNAME":
|
---|
| 1540 | var realname string
|
---|
| 1541 | if err := parseMessageParams(msg, &realname); err != nil {
|
---|
| 1542 | return err
|
---|
| 1543 | }
|
---|
| 1544 |
|
---|
[568] | 1545 | // If the client just resets to the default, just wipe the per-network
|
---|
| 1546 | // preference
|
---|
| 1547 | storeRealname := realname
|
---|
| 1548 | if realname == dc.user.Realname {
|
---|
| 1549 | storeRealname = ""
|
---|
| 1550 | }
|
---|
| 1551 |
|
---|
[540] | 1552 | var storeErr error
|
---|
| 1553 | var needUpdate []Network
|
---|
| 1554 | dc.forEachNetwork(func(n *network) {
|
---|
| 1555 | // We only need to call updateNetwork for upstreams that don't
|
---|
| 1556 | // support setname
|
---|
| 1557 | if uc := n.conn; uc != nil && uc.caps["setname"] {
|
---|
| 1558 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
| 1559 | Command: "SETNAME",
|
---|
| 1560 | Params: []string{realname},
|
---|
| 1561 | })
|
---|
| 1562 |
|
---|
[568] | 1563 | n.Realname = storeRealname
|
---|
[675] | 1564 | if err := dc.srv.db.StoreNetwork(ctx, dc.user.ID, &n.Network); err != nil {
|
---|
[540] | 1565 | dc.logger.Printf("failed to store network realname: %v", err)
|
---|
| 1566 | storeErr = err
|
---|
| 1567 | }
|
---|
| 1568 | return
|
---|
| 1569 | }
|
---|
| 1570 |
|
---|
| 1571 | record := n.Network // copy network record because we'll mutate it
|
---|
[568] | 1572 | record.Realname = storeRealname
|
---|
[540] | 1573 | needUpdate = append(needUpdate, record)
|
---|
| 1574 | })
|
---|
| 1575 |
|
---|
| 1576 | // Walk the network list as a second step, because updateNetwork
|
---|
| 1577 | // mutates the original list
|
---|
| 1578 | for _, record := range needUpdate {
|
---|
[676] | 1579 | if _, err := dc.user.updateNetwork(ctx, &record); err != nil {
|
---|
[540] | 1580 | dc.logger.Printf("failed to update network realname: %v", err)
|
---|
| 1581 | storeErr = err
|
---|
| 1582 | }
|
---|
| 1583 | }
|
---|
| 1584 | if storeErr != nil {
|
---|
| 1585 | return ircError{&irc.Message{
|
---|
| 1586 | Command: "FAIL",
|
---|
| 1587 | Params: []string{"SETNAME", "CANNOT_CHANGE_REALNAME", "Failed to update realname"},
|
---|
| 1588 | }}
|
---|
| 1589 | }
|
---|
| 1590 |
|
---|
[651] | 1591 | if dc.upstream() == nil {
|
---|
[540] | 1592 | dc.SendMessage(&irc.Message{
|
---|
| 1593 | Prefix: dc.prefix(),
|
---|
| 1594 | Command: "SETNAME",
|
---|
| 1595 | Params: []string{realname},
|
---|
| 1596 | })
|
---|
| 1597 | }
|
---|
[146] | 1598 | case "JOIN":
|
---|
| 1599 | var namesStr string
|
---|
| 1600 | if err := parseMessageParams(msg, &namesStr); err != nil {
|
---|
[48] | 1601 | return err
|
---|
| 1602 | }
|
---|
| 1603 |
|
---|
[146] | 1604 | var keys []string
|
---|
| 1605 | if len(msg.Params) > 1 {
|
---|
| 1606 | keys = strings.Split(msg.Params[1], ",")
|
---|
| 1607 | }
|
---|
| 1608 |
|
---|
| 1609 | for i, name := range strings.Split(namesStr, ",") {
|
---|
[145] | 1610 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1611 | if err != nil {
|
---|
[158] | 1612 | return err
|
---|
[145] | 1613 | }
|
---|
[48] | 1614 |
|
---|
[146] | 1615 | var key string
|
---|
| 1616 | if len(keys) > i {
|
---|
| 1617 | key = keys[i]
|
---|
| 1618 | }
|
---|
| 1619 |
|
---|
[545] | 1620 | if !uc.isChannel(upstreamName) {
|
---|
| 1621 | dc.SendMessage(&irc.Message{
|
---|
| 1622 | Prefix: dc.srv.prefix(),
|
---|
| 1623 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
| 1624 | Params: []string{name, "Not a channel name"},
|
---|
| 1625 | })
|
---|
| 1626 | continue
|
---|
| 1627 | }
|
---|
| 1628 |
|
---|
[146] | 1629 | params := []string{upstreamName}
|
---|
| 1630 | if key != "" {
|
---|
| 1631 | params = append(params, key)
|
---|
| 1632 | }
|
---|
[301] | 1633 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[146] | 1634 | Command: "JOIN",
|
---|
| 1635 | Params: params,
|
---|
[145] | 1636 | })
|
---|
[89] | 1637 |
|
---|
[478] | 1638 | ch := uc.network.channels.Value(upstreamName)
|
---|
| 1639 | if ch != nil {
|
---|
[285] | 1640 | // Don't clear the channel key if there's one set
|
---|
| 1641 | // TODO: add a way to unset the channel key
|
---|
[435] | 1642 | if key != "" {
|
---|
| 1643 | ch.Key = key
|
---|
| 1644 | }
|
---|
| 1645 | uc.network.attach(ch)
|
---|
| 1646 | } else {
|
---|
| 1647 | ch = &Channel{
|
---|
| 1648 | Name: upstreamName,
|
---|
| 1649 | Key: key,
|
---|
| 1650 | }
|
---|
[478] | 1651 | uc.network.channels.SetValue(upstreamName, ch)
|
---|
[285] | 1652 | }
|
---|
[675] | 1653 | if err := dc.srv.db.StoreChannel(ctx, uc.network.ID, ch); err != nil {
|
---|
[222] | 1654 | dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
|
---|
[89] | 1655 | }
|
---|
| 1656 | }
|
---|
[146] | 1657 | case "PART":
|
---|
| 1658 | var namesStr string
|
---|
| 1659 | if err := parseMessageParams(msg, &namesStr); err != nil {
|
---|
| 1660 | return err
|
---|
| 1661 | }
|
---|
| 1662 |
|
---|
| 1663 | var reason string
|
---|
| 1664 | if len(msg.Params) > 1 {
|
---|
| 1665 | reason = msg.Params[1]
|
---|
| 1666 | }
|
---|
| 1667 |
|
---|
| 1668 | for _, name := range strings.Split(namesStr, ",") {
|
---|
| 1669 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1670 | if err != nil {
|
---|
[158] | 1671 | return err
|
---|
[146] | 1672 | }
|
---|
| 1673 |
|
---|
[284] | 1674 | if strings.EqualFold(reason, "detach") {
|
---|
[478] | 1675 | ch := uc.network.channels.Value(upstreamName)
|
---|
| 1676 | if ch != nil {
|
---|
[435] | 1677 | uc.network.detach(ch)
|
---|
| 1678 | } else {
|
---|
| 1679 | ch = &Channel{
|
---|
| 1680 | Name: name,
|
---|
| 1681 | Detached: true,
|
---|
| 1682 | }
|
---|
[478] | 1683 | uc.network.channels.SetValue(upstreamName, ch)
|
---|
[284] | 1684 | }
|
---|
[675] | 1685 | if err := dc.srv.db.StoreChannel(ctx, uc.network.ID, ch); err != nil {
|
---|
[435] | 1686 | dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
|
---|
| 1687 | }
|
---|
[284] | 1688 | } else {
|
---|
| 1689 | params := []string{upstreamName}
|
---|
| 1690 | if reason != "" {
|
---|
| 1691 | params = append(params, reason)
|
---|
| 1692 | }
|
---|
[301] | 1693 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[284] | 1694 | Command: "PART",
|
---|
| 1695 | Params: params,
|
---|
| 1696 | })
|
---|
[146] | 1697 |
|
---|
[676] | 1698 | if err := uc.network.deleteChannel(ctx, upstreamName); err != nil {
|
---|
[284] | 1699 | dc.logger.Printf("failed to delete channel %q: %v", upstreamName, err)
|
---|
| 1700 | }
|
---|
[146] | 1701 | }
|
---|
| 1702 | }
|
---|
[159] | 1703 | case "KICK":
|
---|
| 1704 | var channelStr, userStr string
|
---|
| 1705 | if err := parseMessageParams(msg, &channelStr, &userStr); err != nil {
|
---|
| 1706 | return err
|
---|
| 1707 | }
|
---|
| 1708 |
|
---|
| 1709 | channels := strings.Split(channelStr, ",")
|
---|
| 1710 | users := strings.Split(userStr, ",")
|
---|
| 1711 |
|
---|
| 1712 | var reason string
|
---|
| 1713 | if len(msg.Params) > 2 {
|
---|
| 1714 | reason = msg.Params[2]
|
---|
| 1715 | }
|
---|
| 1716 |
|
---|
| 1717 | if len(channels) != 1 && len(channels) != len(users) {
|
---|
| 1718 | return ircError{&irc.Message{
|
---|
| 1719 | Command: irc.ERR_BADCHANMASK,
|
---|
| 1720 | Params: []string{dc.nick, channelStr, "Bad channel mask"},
|
---|
| 1721 | }}
|
---|
| 1722 | }
|
---|
| 1723 |
|
---|
| 1724 | for i, user := range users {
|
---|
| 1725 | var channel string
|
---|
| 1726 | if len(channels) == 1 {
|
---|
| 1727 | channel = channels[0]
|
---|
| 1728 | } else {
|
---|
| 1729 | channel = channels[i]
|
---|
| 1730 | }
|
---|
| 1731 |
|
---|
| 1732 | ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1733 | if err != nil {
|
---|
| 1734 | return err
|
---|
| 1735 | }
|
---|
| 1736 |
|
---|
| 1737 | ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
---|
| 1738 | if err != nil {
|
---|
| 1739 | return err
|
---|
| 1740 | }
|
---|
| 1741 |
|
---|
| 1742 | if ucChannel != ucUser {
|
---|
| 1743 | return ircError{&irc.Message{
|
---|
| 1744 | Command: irc.ERR_USERNOTINCHANNEL,
|
---|
[400] | 1745 | Params: []string{dc.nick, user, channel, "They are on another network"},
|
---|
[159] | 1746 | }}
|
---|
| 1747 | }
|
---|
| 1748 | uc := ucChannel
|
---|
| 1749 |
|
---|
| 1750 | params := []string{upstreamChannel, upstreamUser}
|
---|
| 1751 | if reason != "" {
|
---|
| 1752 | params = append(params, reason)
|
---|
| 1753 | }
|
---|
[301] | 1754 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[159] | 1755 | Command: "KICK",
|
---|
| 1756 | Params: params,
|
---|
| 1757 | })
|
---|
| 1758 | }
|
---|
[69] | 1759 | case "MODE":
|
---|
[46] | 1760 | var name string
|
---|
| 1761 | if err := parseMessageParams(msg, &name); err != nil {
|
---|
| 1762 | return err
|
---|
| 1763 | }
|
---|
| 1764 |
|
---|
| 1765 | var modeStr string
|
---|
| 1766 | if len(msg.Params) > 1 {
|
---|
| 1767 | modeStr = msg.Params[1]
|
---|
| 1768 | }
|
---|
| 1769 |
|
---|
[478] | 1770 | if casemapASCII(name) == dc.nickCM {
|
---|
[46] | 1771 | if modeStr != "" {
|
---|
[554] | 1772 | if uc := dc.upstream(); uc != nil {
|
---|
[301] | 1773 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[69] | 1774 | Command: "MODE",
|
---|
| 1775 | Params: []string{uc.nick, modeStr},
|
---|
| 1776 | })
|
---|
[554] | 1777 | } else {
|
---|
| 1778 | dc.SendMessage(&irc.Message{
|
---|
| 1779 | Prefix: dc.srv.prefix(),
|
---|
| 1780 | Command: irc.ERR_UMODEUNKNOWNFLAG,
|
---|
| 1781 | Params: []string{dc.nick, "Cannot change user mode in multi-upstream mode"},
|
---|
| 1782 | })
|
---|
| 1783 | }
|
---|
[46] | 1784 | } else {
|
---|
[553] | 1785 | var userMode string
|
---|
| 1786 | if uc := dc.upstream(); uc != nil {
|
---|
| 1787 | userMode = string(uc.modes)
|
---|
| 1788 | }
|
---|
| 1789 |
|
---|
[55] | 1790 | dc.SendMessage(&irc.Message{
|
---|
| 1791 | Prefix: dc.srv.prefix(),
|
---|
[46] | 1792 | Command: irc.RPL_UMODEIS,
|
---|
[672] | 1793 | Params: []string{dc.nick, "+" + userMode},
|
---|
[54] | 1794 | })
|
---|
[46] | 1795 | }
|
---|
[139] | 1796 | return nil
|
---|
[46] | 1797 | }
|
---|
[139] | 1798 |
|
---|
| 1799 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1800 | if err != nil {
|
---|
| 1801 | return err
|
---|
| 1802 | }
|
---|
| 1803 |
|
---|
| 1804 | if !uc.isChannel(upstreamName) {
|
---|
| 1805 | return ircError{&irc.Message{
|
---|
| 1806 | Command: irc.ERR_USERSDONTMATCH,
|
---|
| 1807 | Params: []string{dc.nick, "Cannot change mode for other users"},
|
---|
| 1808 | }}
|
---|
| 1809 | }
|
---|
| 1810 |
|
---|
| 1811 | if modeStr != "" {
|
---|
| 1812 | params := []string{upstreamName, modeStr}
|
---|
| 1813 | params = append(params, msg.Params[2:]...)
|
---|
[301] | 1814 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[139] | 1815 | Command: "MODE",
|
---|
| 1816 | Params: params,
|
---|
| 1817 | })
|
---|
| 1818 | } else {
|
---|
[478] | 1819 | ch := uc.channels.Value(upstreamName)
|
---|
| 1820 | if ch == nil {
|
---|
[139] | 1821 | return ircError{&irc.Message{
|
---|
| 1822 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
| 1823 | Params: []string{dc.nick, name, "No such channel"},
|
---|
| 1824 | }}
|
---|
| 1825 | }
|
---|
| 1826 |
|
---|
| 1827 | if ch.modes == nil {
|
---|
| 1828 | // we haven't received the initial RPL_CHANNELMODEIS yet
|
---|
| 1829 | // ignore the request, we will broadcast the modes later when we receive RPL_CHANNELMODEIS
|
---|
| 1830 | return nil
|
---|
| 1831 | }
|
---|
| 1832 |
|
---|
| 1833 | modeStr, modeParams := ch.modes.Format()
|
---|
| 1834 | params := []string{dc.nick, name, modeStr}
|
---|
| 1835 | params = append(params, modeParams...)
|
---|
| 1836 |
|
---|
| 1837 | dc.SendMessage(&irc.Message{
|
---|
| 1838 | Prefix: dc.srv.prefix(),
|
---|
| 1839 | Command: irc.RPL_CHANNELMODEIS,
|
---|
| 1840 | Params: params,
|
---|
| 1841 | })
|
---|
[162] | 1842 | if ch.creationTime != "" {
|
---|
| 1843 | dc.SendMessage(&irc.Message{
|
---|
| 1844 | Prefix: dc.srv.prefix(),
|
---|
| 1845 | Command: rpl_creationtime,
|
---|
| 1846 | Params: []string{dc.nick, name, ch.creationTime},
|
---|
| 1847 | })
|
---|
| 1848 | }
|
---|
[139] | 1849 | }
|
---|
[160] | 1850 | case "TOPIC":
|
---|
| 1851 | var channel string
|
---|
| 1852 | if err := parseMessageParams(msg, &channel); err != nil {
|
---|
| 1853 | return err
|
---|
| 1854 | }
|
---|
| 1855 |
|
---|
[478] | 1856 | uc, upstreamName, err := dc.unmarshalEntity(channel)
|
---|
[160] | 1857 | if err != nil {
|
---|
| 1858 | return err
|
---|
| 1859 | }
|
---|
| 1860 |
|
---|
| 1861 | if len(msg.Params) > 1 { // setting topic
|
---|
| 1862 | topic := msg.Params[1]
|
---|
[301] | 1863 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[160] | 1864 | Command: "TOPIC",
|
---|
[478] | 1865 | Params: []string{upstreamName, topic},
|
---|
[160] | 1866 | })
|
---|
| 1867 | } else { // getting topic
|
---|
[478] | 1868 | ch := uc.channels.Value(upstreamName)
|
---|
| 1869 | if ch == nil {
|
---|
[160] | 1870 | return ircError{&irc.Message{
|
---|
| 1871 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
[478] | 1872 | Params: []string{dc.nick, upstreamName, "No such channel"},
|
---|
[160] | 1873 | }}
|
---|
| 1874 | }
|
---|
| 1875 | sendTopic(dc, ch)
|
---|
| 1876 | }
|
---|
[177] | 1877 | case "LIST":
|
---|
[681] | 1878 | network := dc.network
|
---|
| 1879 | if network == nil && len(msg.Params) > 0 {
|
---|
| 1880 | var err error
|
---|
| 1881 | network, msg.Params[0], err = dc.unmarshalEntityNetwork(msg.Params[0])
|
---|
| 1882 | if err != nil {
|
---|
| 1883 | return err
|
---|
[177] | 1884 | }
|
---|
| 1885 | }
|
---|
[681] | 1886 | if network == nil {
|
---|
| 1887 | dc.SendMessage(&irc.Message{
|
---|
| 1888 | Prefix: dc.srv.prefix(),
|
---|
| 1889 | Command: irc.RPL_LISTEND,
|
---|
| 1890 | Params: []string{dc.nick, "LIST without a network suffix is not supported in multi-upstream mode"},
|
---|
| 1891 | })
|
---|
| 1892 | return nil
|
---|
| 1893 | }
|
---|
[177] | 1894 |
|
---|
[681] | 1895 | uc := network.conn
|
---|
| 1896 | if uc == nil {
|
---|
| 1897 | dc.SendMessage(&irc.Message{
|
---|
| 1898 | Prefix: dc.srv.prefix(),
|
---|
| 1899 | Command: irc.RPL_LISTEND,
|
---|
| 1900 | Params: []string{dc.nick, "Disconnected from upstream server"},
|
---|
| 1901 | })
|
---|
| 1902 | return nil
|
---|
| 1903 | }
|
---|
| 1904 |
|
---|
[682] | 1905 | uc.enqueueCommand(dc, msg)
|
---|
[140] | 1906 | case "NAMES":
|
---|
| 1907 | if len(msg.Params) == 0 {
|
---|
| 1908 | dc.SendMessage(&irc.Message{
|
---|
| 1909 | Prefix: dc.srv.prefix(),
|
---|
| 1910 | Command: irc.RPL_ENDOFNAMES,
|
---|
| 1911 | Params: []string{dc.nick, "*", "End of /NAMES list"},
|
---|
| 1912 | })
|
---|
| 1913 | return nil
|
---|
| 1914 | }
|
---|
| 1915 |
|
---|
| 1916 | channels := strings.Split(msg.Params[0], ",")
|
---|
| 1917 | for _, channel := range channels {
|
---|
[478] | 1918 | uc, upstreamName, err := dc.unmarshalEntity(channel)
|
---|
[140] | 1919 | if err != nil {
|
---|
| 1920 | return err
|
---|
| 1921 | }
|
---|
| 1922 |
|
---|
[478] | 1923 | ch := uc.channels.Value(upstreamName)
|
---|
| 1924 | if ch != nil {
|
---|
[140] | 1925 | sendNames(dc, ch)
|
---|
| 1926 | } else {
|
---|
| 1927 | // NAMES on a channel we have not joined, ask upstream
|
---|
[176] | 1928 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[140] | 1929 | Command: "NAMES",
|
---|
[478] | 1930 | Params: []string{upstreamName},
|
---|
[140] | 1931 | })
|
---|
| 1932 | }
|
---|
| 1933 | }
|
---|
[660] | 1934 | // For WHOX docs, see:
|
---|
| 1935 | // - http://faerion.sourceforge.net/doc/irc/whox.var
|
---|
| 1936 | // - https://github.com/quakenet/snircd/blob/master/doc/readme.who
|
---|
| 1937 | // Note, many features aren't widely implemented, such as flags and mask2
|
---|
[127] | 1938 | case "WHO":
|
---|
| 1939 | if len(msg.Params) == 0 {
|
---|
| 1940 | // TODO: support WHO without parameters
|
---|
| 1941 | dc.SendMessage(&irc.Message{
|
---|
| 1942 | Prefix: dc.srv.prefix(),
|
---|
| 1943 | Command: irc.RPL_ENDOFWHO,
|
---|
[140] | 1944 | Params: []string{dc.nick, "*", "End of /WHO list"},
|
---|
[127] | 1945 | })
|
---|
| 1946 | return nil
|
---|
| 1947 | }
|
---|
| 1948 |
|
---|
[660] | 1949 | // Clients will use the first mask to match RPL_ENDOFWHO
|
---|
| 1950 | endOfWhoToken := msg.Params[0]
|
---|
[127] | 1951 |
|
---|
[660] | 1952 | // TODO: add support for WHOX mask2
|
---|
| 1953 | mask := msg.Params[0]
|
---|
| 1954 | var options string
|
---|
| 1955 | if len(msg.Params) > 1 {
|
---|
| 1956 | options = msg.Params[1]
|
---|
| 1957 | }
|
---|
| 1958 |
|
---|
| 1959 | optionsParts := strings.SplitN(options, "%", 2)
|
---|
| 1960 | // TODO: add support for WHOX flags in optionsParts[0]
|
---|
| 1961 | var fields, whoxToken string
|
---|
| 1962 | if len(optionsParts) == 2 {
|
---|
| 1963 | optionsParts := strings.SplitN(optionsParts[1], ",", 2)
|
---|
| 1964 | fields = strings.ToLower(optionsParts[0])
|
---|
| 1965 | if len(optionsParts) == 2 && strings.Contains(fields, "t") {
|
---|
| 1966 | whoxToken = optionsParts[1]
|
---|
| 1967 | }
|
---|
| 1968 | }
|
---|
| 1969 |
|
---|
| 1970 | // TODO: support mixed bouncer/upstream WHO queries
|
---|
| 1971 | maskCM := casemapASCII(mask)
|
---|
| 1972 | if dc.network == nil && maskCM == dc.nickCM {
|
---|
[142] | 1973 | // TODO: support AWAY (H/G) in self WHO reply
|
---|
[658] | 1974 | flags := "H"
|
---|
| 1975 | if dc.user.Admin {
|
---|
[659] | 1976 | flags += "*"
|
---|
[658] | 1977 | }
|
---|
[660] | 1978 | info := whoxInfo{
|
---|
| 1979 | Token: whoxToken,
|
---|
| 1980 | Username: dc.user.Username,
|
---|
| 1981 | Hostname: dc.hostname,
|
---|
[691] | 1982 | Server: dc.srv.Config().Hostname,
|
---|
[660] | 1983 | Nickname: dc.nick,
|
---|
| 1984 | Flags: flags,
|
---|
[661] | 1985 | Account: dc.user.Username,
|
---|
[660] | 1986 | Realname: dc.realname,
|
---|
| 1987 | }
|
---|
| 1988 | dc.SendMessage(generateWHOXReply(dc.srv.prefix(), dc.nick, fields, &info))
|
---|
[142] | 1989 | dc.SendMessage(&irc.Message{
|
---|
| 1990 | Prefix: dc.srv.prefix(),
|
---|
| 1991 | Command: irc.RPL_ENDOFWHO,
|
---|
[660] | 1992 | Params: []string{dc.nick, endOfWhoToken, "End of /WHO list"},
|
---|
[142] | 1993 | })
|
---|
| 1994 | return nil
|
---|
| 1995 | }
|
---|
[660] | 1996 | if maskCM == serviceNickCM {
|
---|
| 1997 | info := whoxInfo{
|
---|
| 1998 | Token: whoxToken,
|
---|
| 1999 | Username: servicePrefix.User,
|
---|
| 2000 | Hostname: servicePrefix.Host,
|
---|
[691] | 2001 | Server: dc.srv.Config().Hostname,
|
---|
[660] | 2002 | Nickname: serviceNick,
|
---|
| 2003 | Flags: "H*",
|
---|
[661] | 2004 | Account: serviceNick,
|
---|
[660] | 2005 | Realname: serviceRealname,
|
---|
| 2006 | }
|
---|
| 2007 | dc.SendMessage(generateWHOXReply(dc.srv.prefix(), dc.nick, fields, &info))
|
---|
[343] | 2008 | dc.SendMessage(&irc.Message{
|
---|
| 2009 | Prefix: dc.srv.prefix(),
|
---|
| 2010 | Command: irc.RPL_ENDOFWHO,
|
---|
[660] | 2011 | Params: []string{dc.nick, endOfWhoToken, "End of /WHO list"},
|
---|
[343] | 2012 | })
|
---|
| 2013 | return nil
|
---|
| 2014 | }
|
---|
[142] | 2015 |
|
---|
[660] | 2016 | // TODO: properly support WHO masks
|
---|
| 2017 | uc, upstreamMask, err := dc.unmarshalEntity(mask)
|
---|
[127] | 2018 | if err != nil {
|
---|
| 2019 | return err
|
---|
| 2020 | }
|
---|
| 2021 |
|
---|
[660] | 2022 | params := []string{upstreamMask}
|
---|
| 2023 | if options != "" {
|
---|
| 2024 | params = append(params, options)
|
---|
[127] | 2025 | }
|
---|
| 2026 |
|
---|
[682] | 2027 | uc.enqueueCommand(dc, &irc.Message{
|
---|
[127] | 2028 | Command: "WHO",
|
---|
| 2029 | Params: params,
|
---|
| 2030 | })
|
---|
[128] | 2031 | case "WHOIS":
|
---|
| 2032 | if len(msg.Params) == 0 {
|
---|
| 2033 | return ircError{&irc.Message{
|
---|
| 2034 | Command: irc.ERR_NONICKNAMEGIVEN,
|
---|
| 2035 | Params: []string{dc.nick, "No nickname given"},
|
---|
| 2036 | }}
|
---|
| 2037 | }
|
---|
| 2038 |
|
---|
| 2039 | var target, mask string
|
---|
| 2040 | if len(msg.Params) == 1 {
|
---|
| 2041 | target = ""
|
---|
| 2042 | mask = msg.Params[0]
|
---|
| 2043 | } else {
|
---|
| 2044 | target = msg.Params[0]
|
---|
| 2045 | mask = msg.Params[1]
|
---|
| 2046 | }
|
---|
| 2047 | // TODO: support multiple WHOIS users
|
---|
| 2048 | if i := strings.IndexByte(mask, ','); i >= 0 {
|
---|
| 2049 | mask = mask[:i]
|
---|
| 2050 | }
|
---|
| 2051 |
|
---|
[520] | 2052 | if dc.network == nil && casemapASCII(mask) == dc.nickCM {
|
---|
[142] | 2053 | dc.SendMessage(&irc.Message{
|
---|
| 2054 | Prefix: dc.srv.prefix(),
|
---|
| 2055 | Command: irc.RPL_WHOISUSER,
|
---|
[184] | 2056 | Params: []string{dc.nick, dc.nick, dc.user.Username, dc.hostname, "*", dc.realname},
|
---|
[142] | 2057 | })
|
---|
| 2058 | dc.SendMessage(&irc.Message{
|
---|
| 2059 | Prefix: dc.srv.prefix(),
|
---|
| 2060 | Command: irc.RPL_WHOISSERVER,
|
---|
[691] | 2061 | Params: []string{dc.nick, dc.nick, dc.srv.Config().Hostname, "soju"},
|
---|
[142] | 2062 | })
|
---|
[658] | 2063 | if dc.user.Admin {
|
---|
| 2064 | dc.SendMessage(&irc.Message{
|
---|
| 2065 | Prefix: dc.srv.prefix(),
|
---|
| 2066 | Command: irc.RPL_WHOISOPERATOR,
|
---|
| 2067 | Params: []string{dc.nick, dc.nick, "is a bouncer administrator"},
|
---|
| 2068 | })
|
---|
| 2069 | }
|
---|
[142] | 2070 | dc.SendMessage(&irc.Message{
|
---|
| 2071 | Prefix: dc.srv.prefix(),
|
---|
[661] | 2072 | Command: rpl_whoisaccount,
|
---|
| 2073 | Params: []string{dc.nick, dc.nick, dc.user.Username, "is logged in as"},
|
---|
| 2074 | })
|
---|
| 2075 | dc.SendMessage(&irc.Message{
|
---|
| 2076 | Prefix: dc.srv.prefix(),
|
---|
[142] | 2077 | Command: irc.RPL_ENDOFWHOIS,
|
---|
| 2078 | Params: []string{dc.nick, dc.nick, "End of /WHOIS list"},
|
---|
| 2079 | })
|
---|
| 2080 | return nil
|
---|
| 2081 | }
|
---|
[609] | 2082 | if casemapASCII(mask) == serviceNickCM {
|
---|
| 2083 | dc.SendMessage(&irc.Message{
|
---|
| 2084 | Prefix: dc.srv.prefix(),
|
---|
| 2085 | Command: irc.RPL_WHOISUSER,
|
---|
| 2086 | Params: []string{dc.nick, serviceNick, servicePrefix.User, servicePrefix.Host, "*", serviceRealname},
|
---|
| 2087 | })
|
---|
| 2088 | dc.SendMessage(&irc.Message{
|
---|
| 2089 | Prefix: dc.srv.prefix(),
|
---|
| 2090 | Command: irc.RPL_WHOISSERVER,
|
---|
[691] | 2091 | Params: []string{dc.nick, serviceNick, dc.srv.Config().Hostname, "soju"},
|
---|
[609] | 2092 | })
|
---|
| 2093 | dc.SendMessage(&irc.Message{
|
---|
| 2094 | Prefix: dc.srv.prefix(),
|
---|
[657] | 2095 | Command: irc.RPL_WHOISOPERATOR,
|
---|
| 2096 | Params: []string{dc.nick, serviceNick, "is the bouncer service"},
|
---|
| 2097 | })
|
---|
| 2098 | dc.SendMessage(&irc.Message{
|
---|
| 2099 | Prefix: dc.srv.prefix(),
|
---|
[661] | 2100 | Command: rpl_whoisaccount,
|
---|
| 2101 | Params: []string{dc.nick, serviceNick, serviceNick, "is logged in as"},
|
---|
| 2102 | })
|
---|
| 2103 | dc.SendMessage(&irc.Message{
|
---|
| 2104 | Prefix: dc.srv.prefix(),
|
---|
[609] | 2105 | Command: irc.RPL_ENDOFWHOIS,
|
---|
| 2106 | Params: []string{dc.nick, serviceNick, "End of /WHOIS list"},
|
---|
| 2107 | })
|
---|
| 2108 | return nil
|
---|
| 2109 | }
|
---|
[142] | 2110 |
|
---|
[128] | 2111 | // TODO: support WHOIS masks
|
---|
| 2112 | uc, upstreamNick, err := dc.unmarshalEntity(mask)
|
---|
| 2113 | if err != nil {
|
---|
| 2114 | return err
|
---|
| 2115 | }
|
---|
| 2116 |
|
---|
| 2117 | var params []string
|
---|
| 2118 | if target != "" {
|
---|
[299] | 2119 | if target == mask { // WHOIS nick nick
|
---|
| 2120 | params = []string{upstreamNick, upstreamNick}
|
---|
| 2121 | } else {
|
---|
| 2122 | params = []string{target, upstreamNick}
|
---|
| 2123 | }
|
---|
[128] | 2124 | } else {
|
---|
| 2125 | params = []string{upstreamNick}
|
---|
| 2126 | }
|
---|
| 2127 |
|
---|
[176] | 2128 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[128] | 2129 | Command: "WHOIS",
|
---|
| 2130 | Params: params,
|
---|
| 2131 | })
|
---|
[562] | 2132 | case "PRIVMSG", "NOTICE":
|
---|
[58] | 2133 | var targetsStr, text string
|
---|
| 2134 | if err := parseMessageParams(msg, &targetsStr, &text); err != nil {
|
---|
| 2135 | return err
|
---|
| 2136 | }
|
---|
[303] | 2137 | tags := copyClientTags(msg.Tags)
|
---|
[58] | 2138 |
|
---|
| 2139 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
[691] | 2140 | if name == "$"+dc.srv.Config().Hostname || (name == "$*" && dc.network == nil) {
|
---|
[563] | 2141 | // "$" means a server mask follows. If it's the bouncer's
|
---|
| 2142 | // hostname, broadcast the message to all bouncer users.
|
---|
| 2143 | if !dc.user.Admin {
|
---|
| 2144 | return ircError{&irc.Message{
|
---|
| 2145 | Prefix: dc.srv.prefix(),
|
---|
| 2146 | Command: irc.ERR_BADMASK,
|
---|
| 2147 | Params: []string{dc.nick, name, "Permission denied to broadcast message to all bouncer users"},
|
---|
| 2148 | }}
|
---|
| 2149 | }
|
---|
| 2150 |
|
---|
| 2151 | dc.logger.Printf("broadcasting bouncer-wide %v: %v", msg.Command, text)
|
---|
| 2152 |
|
---|
| 2153 | broadcastTags := tags.Copy()
|
---|
| 2154 | broadcastTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
| 2155 | broadcastMsg := &irc.Message{
|
---|
| 2156 | Tags: broadcastTags,
|
---|
| 2157 | Prefix: servicePrefix,
|
---|
| 2158 | Command: msg.Command,
|
---|
| 2159 | Params: []string{name, text},
|
---|
| 2160 | }
|
---|
| 2161 | dc.srv.forEachUser(func(u *user) {
|
---|
| 2162 | u.events <- eventBroadcast{broadcastMsg}
|
---|
| 2163 | })
|
---|
| 2164 | continue
|
---|
| 2165 | }
|
---|
| 2166 |
|
---|
[529] | 2167 | if dc.network == nil && casemapASCII(name) == dc.nickCM {
|
---|
[618] | 2168 | dc.SendMessage(&irc.Message{
|
---|
| 2169 | Tags: msg.Tags.Copy(),
|
---|
| 2170 | Prefix: dc.prefix(),
|
---|
| 2171 | Command: msg.Command,
|
---|
| 2172 | Params: []string{name, text},
|
---|
| 2173 | })
|
---|
[529] | 2174 | continue
|
---|
| 2175 | }
|
---|
| 2176 |
|
---|
[562] | 2177 | if msg.Command == "PRIVMSG" && casemapASCII(name) == serviceNickCM {
|
---|
[431] | 2178 | if dc.caps["echo-message"] {
|
---|
| 2179 | echoTags := tags.Copy()
|
---|
| 2180 | echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
| 2181 | dc.SendMessage(&irc.Message{
|
---|
| 2182 | Tags: echoTags,
|
---|
| 2183 | Prefix: dc.prefix(),
|
---|
[562] | 2184 | Command: msg.Command,
|
---|
[431] | 2185 | Params: []string{name, text},
|
---|
| 2186 | })
|
---|
| 2187 | }
|
---|
[677] | 2188 | handleServicePRIVMSG(ctx, dc, text)
|
---|
[117] | 2189 | continue
|
---|
| 2190 | }
|
---|
| 2191 |
|
---|
[127] | 2192 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
[58] | 2193 | if err != nil {
|
---|
| 2194 | return err
|
---|
| 2195 | }
|
---|
| 2196 |
|
---|
[562] | 2197 | if msg.Command == "PRIVMSG" && uc.network.casemap(upstreamName) == "nickserv" {
|
---|
[675] | 2198 | dc.handleNickServPRIVMSG(ctx, uc, text)
|
---|
[95] | 2199 | }
|
---|
| 2200 |
|
---|
[268] | 2201 | unmarshaledText := text
|
---|
| 2202 | if uc.isChannel(upstreamName) {
|
---|
| 2203 | unmarshaledText = dc.unmarshalText(uc, text)
|
---|
| 2204 | }
|
---|
[301] | 2205 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[303] | 2206 | Tags: tags,
|
---|
[562] | 2207 | Command: msg.Command,
|
---|
[268] | 2208 | Params: []string{upstreamName, unmarshaledText},
|
---|
[60] | 2209 | })
|
---|
[105] | 2210 |
|
---|
[303] | 2211 | echoTags := tags.Copy()
|
---|
| 2212 | echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
[559] | 2213 | if uc.account != "" {
|
---|
| 2214 | echoTags["account"] = irc.TagValue(uc.account)
|
---|
| 2215 | }
|
---|
[113] | 2216 | echoMsg := &irc.Message{
|
---|
[690] | 2217 | Tags: echoTags,
|
---|
| 2218 | Prefix: &irc.Prefix{Name: uc.nick},
|
---|
[562] | 2219 | Command: msg.Command,
|
---|
[113] | 2220 | Params: []string{upstreamName, text},
|
---|
| 2221 | }
|
---|
[239] | 2222 | uc.produce(upstreamName, echoMsg, dc)
|
---|
[435] | 2223 |
|
---|
| 2224 | uc.updateChannelAutoDetach(upstreamName)
|
---|
[58] | 2225 | }
|
---|
[303] | 2226 | case "TAGMSG":
|
---|
| 2227 | var targetsStr string
|
---|
| 2228 | if err := parseMessageParams(msg, &targetsStr); err != nil {
|
---|
| 2229 | return err
|
---|
| 2230 | }
|
---|
| 2231 | tags := copyClientTags(msg.Tags)
|
---|
| 2232 |
|
---|
| 2233 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
[617] | 2234 | if dc.network == nil && casemapASCII(name) == dc.nickCM {
|
---|
| 2235 | dc.SendMessage(&irc.Message{
|
---|
| 2236 | Tags: msg.Tags.Copy(),
|
---|
| 2237 | Prefix: dc.prefix(),
|
---|
| 2238 | Command: "TAGMSG",
|
---|
| 2239 | Params: []string{name},
|
---|
| 2240 | })
|
---|
| 2241 | continue
|
---|
| 2242 | }
|
---|
| 2243 |
|
---|
[616] | 2244 | if casemapASCII(name) == serviceNickCM {
|
---|
| 2245 | continue
|
---|
| 2246 | }
|
---|
| 2247 |
|
---|
[303] | 2248 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 2249 | if err != nil {
|
---|
| 2250 | return err
|
---|
| 2251 | }
|
---|
[427] | 2252 | if _, ok := uc.caps["message-tags"]; !ok {
|
---|
| 2253 | continue
|
---|
| 2254 | }
|
---|
[303] | 2255 |
|
---|
| 2256 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
| 2257 | Tags: tags,
|
---|
| 2258 | Command: "TAGMSG",
|
---|
| 2259 | Params: []string{upstreamName},
|
---|
| 2260 | })
|
---|
[435] | 2261 |
|
---|
| 2262 | uc.updateChannelAutoDetach(upstreamName)
|
---|
[303] | 2263 | }
|
---|
[163] | 2264 | case "INVITE":
|
---|
| 2265 | var user, channel string
|
---|
| 2266 | if err := parseMessageParams(msg, &user, &channel); err != nil {
|
---|
| 2267 | return err
|
---|
| 2268 | }
|
---|
| 2269 |
|
---|
| 2270 | ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 2271 | if err != nil {
|
---|
| 2272 | return err
|
---|
| 2273 | }
|
---|
| 2274 |
|
---|
| 2275 | ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
---|
| 2276 | if err != nil {
|
---|
| 2277 | return err
|
---|
| 2278 | }
|
---|
| 2279 |
|
---|
| 2280 | if ucChannel != ucUser {
|
---|
| 2281 | return ircError{&irc.Message{
|
---|
| 2282 | Command: irc.ERR_USERNOTINCHANNEL,
|
---|
[401] | 2283 | Params: []string{dc.nick, user, channel, "They are on another network"},
|
---|
[163] | 2284 | }}
|
---|
| 2285 | }
|
---|
| 2286 | uc := ucChannel
|
---|
| 2287 |
|
---|
[176] | 2288 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[163] | 2289 | Command: "INVITE",
|
---|
| 2290 | Params: []string{upstreamUser, upstreamChannel},
|
---|
| 2291 | })
|
---|
[684] | 2292 | case "MONITOR":
|
---|
| 2293 | // MONITOR is unsupported in multi-upstream mode
|
---|
| 2294 | uc := dc.upstream()
|
---|
| 2295 | if uc == nil {
|
---|
| 2296 | return newUnknownCommandError(msg.Command)
|
---|
| 2297 | }
|
---|
| 2298 |
|
---|
| 2299 | var subcommand string
|
---|
| 2300 | if err := parseMessageParams(msg, &subcommand); err != nil {
|
---|
| 2301 | return err
|
---|
| 2302 | }
|
---|
| 2303 |
|
---|
| 2304 | switch strings.ToUpper(subcommand) {
|
---|
| 2305 | case "+", "-":
|
---|
| 2306 | var targets string
|
---|
| 2307 | if err := parseMessageParams(msg, nil, &targets); err != nil {
|
---|
| 2308 | return err
|
---|
| 2309 | }
|
---|
| 2310 | for _, target := range strings.Split(targets, ",") {
|
---|
| 2311 | if subcommand == "+" {
|
---|
| 2312 | // Hard limit, just to avoid having downstreams fill our map
|
---|
| 2313 | if len(dc.monitored.innerMap) >= 1000 {
|
---|
| 2314 | dc.SendMessage(&irc.Message{
|
---|
| 2315 | Prefix: dc.srv.prefix(),
|
---|
| 2316 | Command: irc.ERR_MONLISTFULL,
|
---|
| 2317 | Params: []string{dc.nick, "1000", target, "Bouncer monitor list is full"},
|
---|
| 2318 | })
|
---|
| 2319 | continue
|
---|
| 2320 | }
|
---|
| 2321 |
|
---|
| 2322 | dc.monitored.SetValue(target, nil)
|
---|
| 2323 |
|
---|
| 2324 | if uc.monitored.Has(target) {
|
---|
| 2325 | cmd := irc.RPL_MONOFFLINE
|
---|
| 2326 | if online := uc.monitored.Value(target); online {
|
---|
| 2327 | cmd = irc.RPL_MONONLINE
|
---|
| 2328 | }
|
---|
| 2329 |
|
---|
| 2330 | dc.SendMessage(&irc.Message{
|
---|
| 2331 | Prefix: dc.srv.prefix(),
|
---|
| 2332 | Command: cmd,
|
---|
| 2333 | Params: []string{dc.nick, target},
|
---|
| 2334 | })
|
---|
| 2335 | }
|
---|
| 2336 | } else {
|
---|
| 2337 | dc.monitored.Delete(target)
|
---|
| 2338 | }
|
---|
| 2339 | }
|
---|
| 2340 | uc.updateMonitor()
|
---|
| 2341 | case "C": // clear
|
---|
| 2342 | dc.monitored = newCasemapMap(0)
|
---|
| 2343 | uc.updateMonitor()
|
---|
| 2344 | case "L": // list
|
---|
| 2345 | // TODO: be less lazy and pack the list
|
---|
| 2346 | for _, entry := range dc.monitored.innerMap {
|
---|
| 2347 | dc.SendMessage(&irc.Message{
|
---|
| 2348 | Prefix: dc.srv.prefix(),
|
---|
| 2349 | Command: irc.RPL_MONLIST,
|
---|
| 2350 | Params: []string{dc.nick, entry.originalKey},
|
---|
| 2351 | })
|
---|
| 2352 | }
|
---|
| 2353 | dc.SendMessage(&irc.Message{
|
---|
| 2354 | Prefix: dc.srv.prefix(),
|
---|
| 2355 | Command: irc.RPL_ENDOFMONLIST,
|
---|
| 2356 | Params: []string{dc.nick, "End of MONITOR list"},
|
---|
| 2357 | })
|
---|
| 2358 | case "S": // status
|
---|
| 2359 | // TODO: be less lazy and pack the lists
|
---|
| 2360 | for _, entry := range dc.monitored.innerMap {
|
---|
| 2361 | target := entry.originalKey
|
---|
| 2362 |
|
---|
| 2363 | cmd := irc.RPL_MONOFFLINE
|
---|
| 2364 | if online := uc.monitored.Value(target); online {
|
---|
| 2365 | cmd = irc.RPL_MONONLINE
|
---|
| 2366 | }
|
---|
| 2367 |
|
---|
| 2368 | dc.SendMessage(&irc.Message{
|
---|
| 2369 | Prefix: dc.srv.prefix(),
|
---|
| 2370 | Command: cmd,
|
---|
| 2371 | Params: []string{dc.nick, target},
|
---|
| 2372 | })
|
---|
| 2373 | }
|
---|
| 2374 | }
|
---|
[319] | 2375 | case "CHATHISTORY":
|
---|
| 2376 | var subcommand string
|
---|
| 2377 | if err := parseMessageParams(msg, &subcommand); err != nil {
|
---|
| 2378 | return err
|
---|
| 2379 | }
|
---|
[516] | 2380 | var target, limitStr string
|
---|
| 2381 | var boundsStr [2]string
|
---|
| 2382 | switch subcommand {
|
---|
[719] | 2383 | case "AFTER", "BEFORE", "LATEST":
|
---|
[516] | 2384 | if err := parseMessageParams(msg, nil, &target, &boundsStr[0], &limitStr); err != nil {
|
---|
| 2385 | return err
|
---|
| 2386 | }
|
---|
| 2387 | case "BETWEEN":
|
---|
| 2388 | if err := parseMessageParams(msg, nil, &target, &boundsStr[0], &boundsStr[1], &limitStr); err != nil {
|
---|
| 2389 | return err
|
---|
| 2390 | }
|
---|
[549] | 2391 | case "TARGETS":
|
---|
[688] | 2392 | if dc.network == nil {
|
---|
| 2393 | // Either an unbound bouncer network, in which case we should return no targets,
|
---|
| 2394 | // or a multi-upstream downstream, but we don't support CHATHISTORY TARGETS for those yet.
|
---|
| 2395 | dc.SendBatch("draft/chathistory-targets", nil, nil, func(batchRef irc.TagValue) {})
|
---|
| 2396 | return nil
|
---|
| 2397 | }
|
---|
[549] | 2398 | if err := parseMessageParams(msg, nil, &boundsStr[0], &boundsStr[1], &limitStr); err != nil {
|
---|
| 2399 | return err
|
---|
| 2400 | }
|
---|
[516] | 2401 | default:
|
---|
[719] | 2402 | // TODO: support AROUND
|
---|
[319] | 2403 | return ircError{&irc.Message{
|
---|
| 2404 | Command: "FAIL",
|
---|
[516] | 2405 | Params: []string{"CHATHISTORY", "INVALID_PARAMS", subcommand, "Unknown command"},
|
---|
[319] | 2406 | }}
|
---|
| 2407 | }
|
---|
| 2408 |
|
---|
[586] | 2409 | // We don't save history for our service
|
---|
| 2410 | if casemapASCII(target) == serviceNickCM {
|
---|
| 2411 | dc.SendBatch("chathistory", []string{target}, nil, func(batchRef irc.TagValue) {})
|
---|
| 2412 | return nil
|
---|
| 2413 | }
|
---|
| 2414 |
|
---|
[441] | 2415 | store, ok := dc.user.msgStore.(chatHistoryMessageStore)
|
---|
| 2416 | if !ok {
|
---|
[319] | 2417 | return ircError{&irc.Message{
|
---|
| 2418 | Command: irc.ERR_UNKNOWNCOMMAND,
|
---|
[456] | 2419 | Params: []string{dc.nick, "CHATHISTORY", "Unknown command"},
|
---|
[319] | 2420 | }}
|
---|
| 2421 | }
|
---|
| 2422 |
|
---|
[585] | 2423 | network, entity, err := dc.unmarshalEntityNetwork(target)
|
---|
[319] | 2424 | if err != nil {
|
---|
| 2425 | return err
|
---|
| 2426 | }
|
---|
[585] | 2427 | entity = network.casemap(entity)
|
---|
[319] | 2428 |
|
---|
| 2429 | // TODO: support msgid criteria
|
---|
[516] | 2430 | var bounds [2]time.Time
|
---|
| 2431 | bounds[0] = parseChatHistoryBound(boundsStr[0])
|
---|
[719] | 2432 | if subcommand == "LATEST" && boundsStr[0] == "*" {
|
---|
[720] | 2433 | bounds[0] = time.Now()
|
---|
[719] | 2434 | } else if bounds[0].IsZero() {
|
---|
[319] | 2435 | return ircError{&irc.Message{
|
---|
| 2436 | Command: "FAIL",
|
---|
[516] | 2437 | Params: []string{"CHATHISTORY", "INVALID_PARAMS", subcommand, boundsStr[0], "Invalid first bound"},
|
---|
[319] | 2438 | }}
|
---|
| 2439 | }
|
---|
| 2440 |
|
---|
[516] | 2441 | if boundsStr[1] != "" {
|
---|
| 2442 | bounds[1] = parseChatHistoryBound(boundsStr[1])
|
---|
| 2443 | if bounds[1].IsZero() {
|
---|
| 2444 | return ircError{&irc.Message{
|
---|
| 2445 | Command: "FAIL",
|
---|
| 2446 | Params: []string{"CHATHISTORY", "INVALID_PARAMS", subcommand, boundsStr[1], "Invalid second bound"},
|
---|
| 2447 | }}
|
---|
| 2448 | }
|
---|
[319] | 2449 | }
|
---|
| 2450 |
|
---|
| 2451 | limit, err := strconv.Atoi(limitStr)
|
---|
[670] | 2452 | if err != nil || limit < 0 || limit > chatHistoryLimit {
|
---|
[319] | 2453 | return ircError{&irc.Message{
|
---|
| 2454 | Command: "FAIL",
|
---|
[456] | 2455 | Params: []string{"CHATHISTORY", "INVALID_PARAMS", subcommand, limitStr, "Invalid limit"},
|
---|
[319] | 2456 | }}
|
---|
| 2457 | }
|
---|
| 2458 |
|
---|
[665] | 2459 | eventPlayback := dc.caps["draft/event-playback"]
|
---|
| 2460 |
|
---|
[387] | 2461 | var history []*irc.Message
|
---|
[319] | 2462 | switch subcommand {
|
---|
[719] | 2463 | case "BEFORE", "LATEST":
|
---|
[667] | 2464 | history, err = store.LoadBeforeTime(ctx, &network.Network, entity, bounds[0], time.Time{}, limit, eventPlayback)
|
---|
[360] | 2465 | case "AFTER":
|
---|
[667] | 2466 | history, err = store.LoadAfterTime(ctx, &network.Network, entity, bounds[0], time.Now(), limit, eventPlayback)
|
---|
[516] | 2467 | case "BETWEEN":
|
---|
| 2468 | if bounds[0].Before(bounds[1]) {
|
---|
[667] | 2469 | history, err = store.LoadAfterTime(ctx, &network.Network, entity, bounds[0], bounds[1], limit, eventPlayback)
|
---|
[516] | 2470 | } else {
|
---|
[667] | 2471 | history, err = store.LoadBeforeTime(ctx, &network.Network, entity, bounds[0], bounds[1], limit, eventPlayback)
|
---|
[516] | 2472 | }
|
---|
[549] | 2473 | case "TARGETS":
|
---|
| 2474 | // TODO: support TARGETS in multi-upstream mode
|
---|
[667] | 2475 | targets, err := store.ListTargets(ctx, &network.Network, bounds[0], bounds[1], limit, eventPlayback)
|
---|
[549] | 2476 | if err != nil {
|
---|
[627] | 2477 | dc.logger.Printf("failed fetching targets for chathistory: %v", err)
|
---|
[549] | 2478 | return ircError{&irc.Message{
|
---|
| 2479 | Command: "FAIL",
|
---|
| 2480 | Params: []string{"CHATHISTORY", "MESSAGE_ERROR", subcommand, "Failed to retrieve targets"},
|
---|
| 2481 | }}
|
---|
| 2482 | }
|
---|
| 2483 |
|
---|
[551] | 2484 | dc.SendBatch("draft/chathistory-targets", nil, nil, func(batchRef irc.TagValue) {
|
---|
| 2485 | for _, target := range targets {
|
---|
[585] | 2486 | if ch := network.channels.Value(target.Name); ch != nil && ch.Detached {
|
---|
[551] | 2487 | continue
|
---|
| 2488 | }
|
---|
[549] | 2489 |
|
---|
[551] | 2490 | dc.SendMessage(&irc.Message{
|
---|
| 2491 | Tags: irc.Tags{"batch": batchRef},
|
---|
| 2492 | Prefix: dc.srv.prefix(),
|
---|
| 2493 | Command: "CHATHISTORY",
|
---|
| 2494 | Params: []string{"TARGETS", target.Name, target.LatestMessage.UTC().Format(serverTimeLayout)},
|
---|
| 2495 | })
|
---|
[550] | 2496 | }
|
---|
[549] | 2497 | })
|
---|
| 2498 |
|
---|
| 2499 | return nil
|
---|
[319] | 2500 | }
|
---|
[387] | 2501 | if err != nil {
|
---|
[515] | 2502 | dc.logger.Printf("failed fetching %q messages for chathistory: %v", target, err)
|
---|
[387] | 2503 | return newChatHistoryError(subcommand, target)
|
---|
| 2504 | }
|
---|
| 2505 |
|
---|
[551] | 2506 | dc.SendBatch("chathistory", []string{target}, nil, func(batchRef irc.TagValue) {
|
---|
| 2507 | for _, msg := range history {
|
---|
| 2508 | msg.Tags["batch"] = batchRef
|
---|
[585] | 2509 | dc.SendMessage(dc.marshalMessage(msg, network))
|
---|
[551] | 2510 | }
|
---|
[387] | 2511 | })
|
---|
[532] | 2512 | case "BOUNCER":
|
---|
| 2513 | var subcommand string
|
---|
| 2514 | if err := parseMessageParams(msg, &subcommand); err != nil {
|
---|
| 2515 | return err
|
---|
| 2516 | }
|
---|
| 2517 |
|
---|
| 2518 | switch strings.ToUpper(subcommand) {
|
---|
[646] | 2519 | case "BIND":
|
---|
| 2520 | return ircError{&irc.Message{
|
---|
| 2521 | Command: "FAIL",
|
---|
| 2522 | Params: []string{"BOUNCER", "REGISTRATION_IS_COMPLETED", "BIND", "Cannot bind to a network after registration"},
|
---|
| 2523 | }}
|
---|
[532] | 2524 | case "LISTNETWORKS":
|
---|
[551] | 2525 | dc.SendBatch("soju.im/bouncer-networks", nil, nil, func(batchRef irc.TagValue) {
|
---|
| 2526 | dc.user.forEachNetwork(func(network *network) {
|
---|
| 2527 | idStr := fmt.Sprintf("%v", network.ID)
|
---|
| 2528 | attrs := getNetworkAttrs(network)
|
---|
| 2529 | dc.SendMessage(&irc.Message{
|
---|
| 2530 | Tags: irc.Tags{"batch": batchRef},
|
---|
| 2531 | Prefix: dc.srv.prefix(),
|
---|
| 2532 | Command: "BOUNCER",
|
---|
| 2533 | Params: []string{"NETWORK", idStr, attrs.String()},
|
---|
| 2534 | })
|
---|
[532] | 2535 | })
|
---|
| 2536 | })
|
---|
| 2537 | case "ADDNETWORK":
|
---|
| 2538 | var attrsStr string
|
---|
| 2539 | if err := parseMessageParams(msg, nil, &attrsStr); err != nil {
|
---|
| 2540 | return err
|
---|
| 2541 | }
|
---|
| 2542 | attrs := irc.ParseTags(attrsStr)
|
---|
| 2543 |
|
---|
[654] | 2544 | record := &Network{Nick: dc.nick, Enabled: true}
|
---|
| 2545 | if err := updateNetworkAttrs(record, attrs, subcommand); err != nil {
|
---|
| 2546 | return err
|
---|
[532] | 2547 | }
|
---|
| 2548 |
|
---|
[664] | 2549 | if record.Nick == dc.user.Username {
|
---|
| 2550 | record.Nick = ""
|
---|
| 2551 | }
|
---|
[654] | 2552 | if record.Realname == dc.user.Realname {
|
---|
| 2553 | record.Realname = ""
|
---|
[532] | 2554 | }
|
---|
| 2555 |
|
---|
[676] | 2556 | network, err := dc.user.createNetwork(ctx, record)
|
---|
[532] | 2557 | if err != nil {
|
---|
| 2558 | return ircError{&irc.Message{
|
---|
| 2559 | Command: "FAIL",
|
---|
| 2560 | Params: []string{"BOUNCER", "UNKNOWN_ERROR", subcommand, fmt.Sprintf("Failed to create network: %v", err)},
|
---|
| 2561 | }}
|
---|
| 2562 | }
|
---|
| 2563 |
|
---|
| 2564 | dc.SendMessage(&irc.Message{
|
---|
| 2565 | Prefix: dc.srv.prefix(),
|
---|
| 2566 | Command: "BOUNCER",
|
---|
| 2567 | Params: []string{"ADDNETWORK", fmt.Sprintf("%v", network.ID)},
|
---|
| 2568 | })
|
---|
| 2569 | case "CHANGENETWORK":
|
---|
| 2570 | var idStr, attrsStr string
|
---|
| 2571 | if err := parseMessageParams(msg, nil, &idStr, &attrsStr); err != nil {
|
---|
| 2572 | return err
|
---|
| 2573 | }
|
---|
[535] | 2574 | id, err := parseBouncerNetID(subcommand, idStr)
|
---|
[532] | 2575 | if err != nil {
|
---|
| 2576 | return err
|
---|
| 2577 | }
|
---|
| 2578 | attrs := irc.ParseTags(attrsStr)
|
---|
| 2579 |
|
---|
| 2580 | net := dc.user.getNetworkByID(id)
|
---|
| 2581 | if net == nil {
|
---|
| 2582 | return ircError{&irc.Message{
|
---|
| 2583 | Command: "FAIL",
|
---|
[535] | 2584 | Params: []string{"BOUNCER", "INVALID_NETID", subcommand, idStr, "Invalid network ID"},
|
---|
[532] | 2585 | }}
|
---|
| 2586 | }
|
---|
| 2587 |
|
---|
| 2588 | record := net.Network // copy network record because we'll mutate it
|
---|
[654] | 2589 | if err := updateNetworkAttrs(&record, attrs, subcommand); err != nil {
|
---|
| 2590 | return err
|
---|
[532] | 2591 | }
|
---|
| 2592 |
|
---|
[664] | 2593 | if record.Nick == dc.user.Username {
|
---|
| 2594 | record.Nick = ""
|
---|
| 2595 | }
|
---|
[654] | 2596 | if record.Realname == dc.user.Realname {
|
---|
| 2597 | record.Realname = ""
|
---|
| 2598 | }
|
---|
| 2599 |
|
---|
[676] | 2600 | _, err = dc.user.updateNetwork(ctx, &record)
|
---|
[532] | 2601 | if err != nil {
|
---|
| 2602 | return ircError{&irc.Message{
|
---|
| 2603 | Command: "FAIL",
|
---|
| 2604 | Params: []string{"BOUNCER", "UNKNOWN_ERROR", subcommand, fmt.Sprintf("Failed to update network: %v", err)},
|
---|
| 2605 | }}
|
---|
| 2606 | }
|
---|
| 2607 |
|
---|
| 2608 | dc.SendMessage(&irc.Message{
|
---|
| 2609 | Prefix: dc.srv.prefix(),
|
---|
| 2610 | Command: "BOUNCER",
|
---|
| 2611 | Params: []string{"CHANGENETWORK", idStr},
|
---|
| 2612 | })
|
---|
| 2613 | case "DELNETWORK":
|
---|
| 2614 | var idStr string
|
---|
| 2615 | if err := parseMessageParams(msg, nil, &idStr); err != nil {
|
---|
| 2616 | return err
|
---|
| 2617 | }
|
---|
[535] | 2618 | id, err := parseBouncerNetID(subcommand, idStr)
|
---|
[532] | 2619 | if err != nil {
|
---|
| 2620 | return err
|
---|
| 2621 | }
|
---|
| 2622 |
|
---|
| 2623 | net := dc.user.getNetworkByID(id)
|
---|
| 2624 | if net == nil {
|
---|
| 2625 | return ircError{&irc.Message{
|
---|
| 2626 | Command: "FAIL",
|
---|
[535] | 2627 | Params: []string{"BOUNCER", "INVALID_NETID", subcommand, idStr, "Invalid network ID"},
|
---|
[532] | 2628 | }}
|
---|
| 2629 | }
|
---|
| 2630 |
|
---|
[676] | 2631 | if err := dc.user.deleteNetwork(ctx, net.ID); err != nil {
|
---|
[532] | 2632 | return err
|
---|
| 2633 | }
|
---|
| 2634 |
|
---|
| 2635 | dc.SendMessage(&irc.Message{
|
---|
| 2636 | Prefix: dc.srv.prefix(),
|
---|
| 2637 | Command: "BOUNCER",
|
---|
| 2638 | Params: []string{"DELNETWORK", idStr},
|
---|
| 2639 | })
|
---|
| 2640 | default:
|
---|
| 2641 | return ircError{&irc.Message{
|
---|
| 2642 | Command: "FAIL",
|
---|
| 2643 | Params: []string{"BOUNCER", "UNKNOWN_COMMAND", subcommand, "Unknown subcommand"},
|
---|
| 2644 | }}
|
---|
| 2645 | }
|
---|
[13] | 2646 | default:
|
---|
[55] | 2647 | dc.logger.Printf("unhandled message: %v", msg)
|
---|
[547] | 2648 |
|
---|
| 2649 | // Only forward unknown commands in single-upstream mode
|
---|
| 2650 | uc := dc.upstream()
|
---|
| 2651 | if uc == nil {
|
---|
| 2652 | return newUnknownCommandError(msg.Command)
|
---|
| 2653 | }
|
---|
| 2654 |
|
---|
| 2655 | uc.SendMessageLabeled(dc.id, msg)
|
---|
[13] | 2656 | }
|
---|
[42] | 2657 | return nil
|
---|
[13] | 2658 | }
|
---|
[95] | 2659 |
|
---|
[675] | 2660 | func (dc *downstreamConn) handleNickServPRIVMSG(ctx context.Context, uc *upstreamConn, text string) {
|
---|
[95] | 2661 | username, password, ok := parseNickServCredentials(text, uc.nick)
|
---|
| 2662 | if !ok {
|
---|
| 2663 | return
|
---|
| 2664 | }
|
---|
| 2665 |
|
---|
[307] | 2666 | // User may have e.g. EXTERNAL mechanism configured. We do not want to
|
---|
| 2667 | // automatically erase the key pair or any other credentials.
|
---|
| 2668 | if uc.network.SASL.Mechanism != "" && uc.network.SASL.Mechanism != "PLAIN" {
|
---|
| 2669 | return
|
---|
| 2670 | }
|
---|
| 2671 |
|
---|
[95] | 2672 | dc.logger.Printf("auto-saving NickServ credentials with username %q", username)
|
---|
| 2673 | n := uc.network
|
---|
| 2674 | n.SASL.Mechanism = "PLAIN"
|
---|
| 2675 | n.SASL.Plain.Username = username
|
---|
| 2676 | n.SASL.Plain.Password = password
|
---|
[675] | 2677 | if err := dc.srv.db.StoreNetwork(ctx, dc.user.ID, &n.Network); err != nil {
|
---|
[95] | 2678 | dc.logger.Printf("failed to save NickServ credentials: %v", err)
|
---|
| 2679 | }
|
---|
| 2680 | }
|
---|
| 2681 |
|
---|
| 2682 | func parseNickServCredentials(text, nick string) (username, password string, ok bool) {
|
---|
| 2683 | fields := strings.Fields(text)
|
---|
| 2684 | if len(fields) < 2 {
|
---|
| 2685 | return "", "", false
|
---|
| 2686 | }
|
---|
| 2687 | cmd := strings.ToUpper(fields[0])
|
---|
| 2688 | params := fields[1:]
|
---|
| 2689 | switch cmd {
|
---|
| 2690 | case "REGISTER":
|
---|
| 2691 | username = nick
|
---|
| 2692 | password = params[0]
|
---|
| 2693 | case "IDENTIFY":
|
---|
| 2694 | if len(params) == 1 {
|
---|
| 2695 | username = nick
|
---|
[182] | 2696 | password = params[0]
|
---|
[95] | 2697 | } else {
|
---|
| 2698 | username = params[0]
|
---|
[182] | 2699 | password = params[1]
|
---|
[95] | 2700 | }
|
---|
[182] | 2701 | case "SET":
|
---|
| 2702 | if len(params) == 2 && strings.EqualFold(params[0], "PASSWORD") {
|
---|
| 2703 | username = nick
|
---|
| 2704 | password = params[1]
|
---|
| 2705 | }
|
---|
[340] | 2706 | default:
|
---|
| 2707 | return "", "", false
|
---|
[95] | 2708 | }
|
---|
| 2709 | return username, password, true
|
---|
| 2710 | }
|
---|