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