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