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