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