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