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