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