[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 |
|
---|
[108] | 188 | negociatingCaps bool
|
---|
| 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.registered {
|
---|
| 684 | return ircError{&irc.Message{
|
---|
| 685 | Command: "FAIL",
|
---|
| 686 | Params: []string{"BOUNCER", "REGISTRATION_IS_COMPLETED", "BIND", "Cannot bind bouncer network after registration"},
|
---|
| 687 | }}
|
---|
| 688 | }
|
---|
| 689 | if dc.user == nil {
|
---|
| 690 | return ircError{&irc.Message{
|
---|
| 691 | Command: "FAIL",
|
---|
| 692 | Params: []string{"BOUNCER", "ACCOUNT_REQUIRED", "BIND", "Authentication needed to bind to bouncer network"},
|
---|
| 693 | }}
|
---|
| 694 | }
|
---|
| 695 |
|
---|
[535] | 696 | id, err := parseBouncerNetID(subcommand, idStr)
|
---|
[532] | 697 | if err != nil {
|
---|
| 698 | return err
|
---|
| 699 | }
|
---|
| 700 |
|
---|
| 701 | var match *network
|
---|
| 702 | dc.user.forEachNetwork(func(net *network) {
|
---|
| 703 | if net.ID == id {
|
---|
| 704 | match = net
|
---|
| 705 | }
|
---|
| 706 | })
|
---|
| 707 | if match == nil {
|
---|
| 708 | return ircError{&irc.Message{
|
---|
| 709 | Command: "FAIL",
|
---|
| 710 | Params: []string{"BOUNCER", "INVALID_NETID", idStr, "Unknown network ID"},
|
---|
| 711 | }}
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | dc.networkName = match.GetName()
|
---|
| 715 | }
|
---|
[13] | 716 | default:
|
---|
[55] | 717 | dc.logger.Printf("unhandled message: %v", msg)
|
---|
[13] | 718 | return newUnknownCommandError(msg.Command)
|
---|
| 719 | }
|
---|
[108] | 720 | if dc.rawUsername != "" && dc.nick != "" && !dc.negociatingCaps {
|
---|
[55] | 721 | return dc.register()
|
---|
[13] | 722 | }
|
---|
| 723 | return nil
|
---|
| 724 | }
|
---|
| 725 |
|
---|
[108] | 726 | func (dc *downstreamConn) handleCapCommand(cmd string, args []string) error {
|
---|
[111] | 727 | cmd = strings.ToUpper(cmd)
|
---|
| 728 |
|
---|
[108] | 729 | replyTo := dc.nick
|
---|
| 730 | if !dc.registered {
|
---|
| 731 | replyTo = "*"
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 | switch cmd {
|
---|
| 735 | case "LS":
|
---|
| 736 | if len(args) > 0 {
|
---|
| 737 | var err error
|
---|
| 738 | if dc.capVersion, err = strconv.Atoi(args[0]); err != nil {
|
---|
| 739 | return err
|
---|
| 740 | }
|
---|
| 741 | }
|
---|
[437] | 742 | if !dc.registered && dc.capVersion >= 302 {
|
---|
| 743 | // Let downstream show everything it supports, and trim
|
---|
| 744 | // down the available capabilities when upstreams are
|
---|
| 745 | // known.
|
---|
| 746 | for k, v := range needAllDownstreamCaps {
|
---|
| 747 | dc.supportedCaps[k] = v
|
---|
| 748 | }
|
---|
| 749 | }
|
---|
[108] | 750 |
|
---|
[275] | 751 | caps := make([]string, 0, len(dc.supportedCaps))
|
---|
| 752 | for k, v := range dc.supportedCaps {
|
---|
| 753 | if dc.capVersion >= 302 && v != "" {
|
---|
[276] | 754 | caps = append(caps, k+"="+v)
|
---|
[275] | 755 | } else {
|
---|
| 756 | caps = append(caps, k)
|
---|
| 757 | }
|
---|
[112] | 758 | }
|
---|
[108] | 759 |
|
---|
| 760 | // TODO: multi-line replies
|
---|
| 761 | dc.SendMessage(&irc.Message{
|
---|
| 762 | Prefix: dc.srv.prefix(),
|
---|
| 763 | Command: "CAP",
|
---|
| 764 | Params: []string{replyTo, "LS", strings.Join(caps, " ")},
|
---|
| 765 | })
|
---|
| 766 |
|
---|
[275] | 767 | if dc.capVersion >= 302 {
|
---|
| 768 | // CAP version 302 implicitly enables cap-notify
|
---|
| 769 | dc.caps["cap-notify"] = true
|
---|
| 770 | }
|
---|
| 771 |
|
---|
[108] | 772 | if !dc.registered {
|
---|
| 773 | dc.negociatingCaps = true
|
---|
| 774 | }
|
---|
| 775 | case "LIST":
|
---|
| 776 | var caps []string
|
---|
[521] | 777 | for name, enabled := range dc.caps {
|
---|
| 778 | if enabled {
|
---|
| 779 | caps = append(caps, name)
|
---|
| 780 | }
|
---|
[108] | 781 | }
|
---|
| 782 |
|
---|
| 783 | // TODO: multi-line replies
|
---|
| 784 | dc.SendMessage(&irc.Message{
|
---|
| 785 | Prefix: dc.srv.prefix(),
|
---|
| 786 | Command: "CAP",
|
---|
| 787 | Params: []string{replyTo, "LIST", strings.Join(caps, " ")},
|
---|
| 788 | })
|
---|
| 789 | case "REQ":
|
---|
| 790 | if len(args) == 0 {
|
---|
| 791 | return ircError{&irc.Message{
|
---|
| 792 | Command: err_invalidcapcmd,
|
---|
| 793 | Params: []string{replyTo, cmd, "Missing argument in CAP REQ command"},
|
---|
| 794 | }}
|
---|
| 795 | }
|
---|
| 796 |
|
---|
[275] | 797 | // TODO: atomically ack/nak the whole capability set
|
---|
[108] | 798 | caps := strings.Fields(args[0])
|
---|
| 799 | ack := true
|
---|
| 800 | for _, name := range caps {
|
---|
| 801 | name = strings.ToLower(name)
|
---|
| 802 | enable := !strings.HasPrefix(name, "-")
|
---|
| 803 | if !enable {
|
---|
| 804 | name = strings.TrimPrefix(name, "-")
|
---|
| 805 | }
|
---|
| 806 |
|
---|
[275] | 807 | if enable == dc.caps[name] {
|
---|
[108] | 808 | continue
|
---|
| 809 | }
|
---|
| 810 |
|
---|
[275] | 811 | _, ok := dc.supportedCaps[name]
|
---|
| 812 | if !ok {
|
---|
[108] | 813 | ack = false
|
---|
[275] | 814 | break
|
---|
[108] | 815 | }
|
---|
[275] | 816 |
|
---|
| 817 | if name == "cap-notify" && dc.capVersion >= 302 && !enable {
|
---|
| 818 | // cap-notify cannot be disabled with CAP version 302
|
---|
| 819 | ack = false
|
---|
| 820 | break
|
---|
| 821 | }
|
---|
| 822 |
|
---|
| 823 | dc.caps[name] = enable
|
---|
[108] | 824 | }
|
---|
| 825 |
|
---|
| 826 | reply := "NAK"
|
---|
| 827 | if ack {
|
---|
| 828 | reply = "ACK"
|
---|
| 829 | }
|
---|
| 830 | dc.SendMessage(&irc.Message{
|
---|
| 831 | Prefix: dc.srv.prefix(),
|
---|
| 832 | Command: "CAP",
|
---|
| 833 | Params: []string{replyTo, reply, args[0]},
|
---|
| 834 | })
|
---|
| 835 | case "END":
|
---|
| 836 | dc.negociatingCaps = false
|
---|
| 837 | default:
|
---|
| 838 | return ircError{&irc.Message{
|
---|
| 839 | Command: err_invalidcapcmd,
|
---|
| 840 | Params: []string{replyTo, cmd, "Unknown CAP command"},
|
---|
| 841 | }}
|
---|
| 842 | }
|
---|
| 843 | return nil
|
---|
| 844 | }
|
---|
| 845 |
|
---|
[275] | 846 | func (dc *downstreamConn) setSupportedCap(name, value string) {
|
---|
| 847 | prevValue, hasPrev := dc.supportedCaps[name]
|
---|
| 848 | changed := !hasPrev || prevValue != value
|
---|
| 849 | dc.supportedCaps[name] = value
|
---|
| 850 |
|
---|
| 851 | if !dc.caps["cap-notify"] || !changed {
|
---|
| 852 | return
|
---|
| 853 | }
|
---|
| 854 |
|
---|
| 855 | replyTo := dc.nick
|
---|
| 856 | if !dc.registered {
|
---|
| 857 | replyTo = "*"
|
---|
| 858 | }
|
---|
| 859 |
|
---|
| 860 | cap := name
|
---|
| 861 | if value != "" && dc.capVersion >= 302 {
|
---|
| 862 | cap = name + "=" + value
|
---|
| 863 | }
|
---|
| 864 |
|
---|
| 865 | dc.SendMessage(&irc.Message{
|
---|
| 866 | Prefix: dc.srv.prefix(),
|
---|
| 867 | Command: "CAP",
|
---|
| 868 | Params: []string{replyTo, "NEW", cap},
|
---|
| 869 | })
|
---|
| 870 | }
|
---|
| 871 |
|
---|
| 872 | func (dc *downstreamConn) unsetSupportedCap(name string) {
|
---|
| 873 | _, hasPrev := dc.supportedCaps[name]
|
---|
| 874 | delete(dc.supportedCaps, name)
|
---|
| 875 | delete(dc.caps, name)
|
---|
| 876 |
|
---|
| 877 | if !dc.caps["cap-notify"] || !hasPrev {
|
---|
| 878 | return
|
---|
| 879 | }
|
---|
| 880 |
|
---|
| 881 | replyTo := dc.nick
|
---|
| 882 | if !dc.registered {
|
---|
| 883 | replyTo = "*"
|
---|
| 884 | }
|
---|
| 885 |
|
---|
| 886 | dc.SendMessage(&irc.Message{
|
---|
| 887 | Prefix: dc.srv.prefix(),
|
---|
| 888 | Command: "CAP",
|
---|
| 889 | Params: []string{replyTo, "DEL", name},
|
---|
| 890 | })
|
---|
| 891 | }
|
---|
| 892 |
|
---|
[276] | 893 | func (dc *downstreamConn) updateSupportedCaps() {
|
---|
[292] | 894 | supportedCaps := make(map[string]bool)
|
---|
| 895 | for cap := range needAllDownstreamCaps {
|
---|
| 896 | supportedCaps[cap] = true
|
---|
| 897 | }
|
---|
[276] | 898 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[292] | 899 | for cap, supported := range supportedCaps {
|
---|
| 900 | supportedCaps[cap] = supported && uc.caps[cap]
|
---|
| 901 | }
|
---|
[276] | 902 | })
|
---|
| 903 |
|
---|
[292] | 904 | for cap, supported := range supportedCaps {
|
---|
| 905 | if supported {
|
---|
| 906 | dc.setSupportedCap(cap, needAllDownstreamCaps[cap])
|
---|
| 907 | } else {
|
---|
| 908 | dc.unsetSupportedCap(cap)
|
---|
| 909 | }
|
---|
[276] | 910 | }
|
---|
| 911 | }
|
---|
| 912 |
|
---|
[296] | 913 | func (dc *downstreamConn) updateNick() {
|
---|
| 914 | if uc := dc.upstream(); uc != nil && uc.nick != dc.nick {
|
---|
| 915 | dc.SendMessage(&irc.Message{
|
---|
| 916 | Prefix: dc.prefix(),
|
---|
| 917 | Command: "NICK",
|
---|
| 918 | Params: []string{uc.nick},
|
---|
| 919 | })
|
---|
| 920 | dc.nick = uc.nick
|
---|
[478] | 921 | dc.nickCM = casemapASCII(dc.nick)
|
---|
[296] | 922 | }
|
---|
| 923 | }
|
---|
| 924 |
|
---|
[540] | 925 | func (dc *downstreamConn) updateRealname() {
|
---|
| 926 | if uc := dc.upstream(); uc != nil && uc.realname != dc.realname && dc.caps["setname"] {
|
---|
| 927 | dc.SendMessage(&irc.Message{
|
---|
| 928 | Prefix: dc.prefix(),
|
---|
| 929 | Command: "SETNAME",
|
---|
| 930 | Params: []string{uc.realname},
|
---|
| 931 | })
|
---|
| 932 | dc.realname = uc.realname
|
---|
| 933 | }
|
---|
| 934 | }
|
---|
| 935 |
|
---|
[91] | 936 | func sanityCheckServer(addr string) error {
|
---|
| 937 | dialer := net.Dialer{Timeout: 30 * time.Second}
|
---|
| 938 | conn, err := tls.DialWithDialer(&dialer, "tcp", addr, nil)
|
---|
| 939 | if err != nil {
|
---|
| 940 | return err
|
---|
| 941 | }
|
---|
| 942 | return conn.Close()
|
---|
| 943 | }
|
---|
| 944 |
|
---|
[183] | 945 | func unmarshalUsername(rawUsername string) (username, client, network string) {
|
---|
[112] | 946 | username = rawUsername
|
---|
[183] | 947 |
|
---|
| 948 | i := strings.IndexAny(username, "/@")
|
---|
| 949 | j := strings.LastIndexAny(username, "/@")
|
---|
| 950 | if i >= 0 {
|
---|
| 951 | username = rawUsername[:i]
|
---|
[73] | 952 | }
|
---|
[183] | 953 | if j >= 0 {
|
---|
[190] | 954 | if rawUsername[j] == '@' {
|
---|
| 955 | client = rawUsername[j+1:]
|
---|
| 956 | } else {
|
---|
| 957 | network = rawUsername[j+1:]
|
---|
| 958 | }
|
---|
[73] | 959 | }
|
---|
[183] | 960 | if i >= 0 && j >= 0 && i < j {
|
---|
[190] | 961 | if rawUsername[i] == '@' {
|
---|
| 962 | client = rawUsername[i+1 : j]
|
---|
| 963 | } else {
|
---|
| 964 | network = rawUsername[i+1 : j]
|
---|
| 965 | }
|
---|
[183] | 966 | }
|
---|
| 967 |
|
---|
| 968 | return username, client, network
|
---|
[112] | 969 | }
|
---|
[73] | 970 |
|
---|
[168] | 971 | func (dc *downstreamConn) authenticate(username, password string) error {
|
---|
[183] | 972 | username, clientName, networkName := unmarshalUsername(username)
|
---|
[168] | 973 |
|
---|
[173] | 974 | u, err := dc.srv.db.GetUser(username)
|
---|
| 975 | if err != nil {
|
---|
[438] | 976 | dc.logger.Printf("failed authentication for %q: user not found: %v", username, err)
|
---|
[168] | 977 | return errAuthFailed
|
---|
| 978 | }
|
---|
| 979 |
|
---|
[322] | 980 | // Password auth disabled
|
---|
| 981 | if u.Password == "" {
|
---|
| 982 | return errAuthFailed
|
---|
| 983 | }
|
---|
| 984 |
|
---|
[173] | 985 | err = bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
---|
[168] | 986 | if err != nil {
|
---|
[438] | 987 | dc.logger.Printf("failed authentication for %q: wrong password: %v", username, err)
|
---|
[168] | 988 | return errAuthFailed
|
---|
| 989 | }
|
---|
| 990 |
|
---|
[173] | 991 | dc.user = dc.srv.getUser(username)
|
---|
| 992 | if dc.user == nil {
|
---|
| 993 | dc.logger.Printf("failed authentication for %q: user not active", username)
|
---|
| 994 | return errAuthFailed
|
---|
| 995 | }
|
---|
[183] | 996 | dc.clientName = clientName
|
---|
[168] | 997 | dc.networkName = networkName
|
---|
| 998 | return nil
|
---|
| 999 | }
|
---|
| 1000 |
|
---|
| 1001 | func (dc *downstreamConn) register() error {
|
---|
| 1002 | if dc.registered {
|
---|
| 1003 | return fmt.Errorf("tried to register twice")
|
---|
| 1004 | }
|
---|
| 1005 |
|
---|
| 1006 | password := dc.password
|
---|
| 1007 | dc.password = ""
|
---|
| 1008 | if dc.user == nil {
|
---|
| 1009 | if err := dc.authenticate(dc.rawUsername, password); err != nil {
|
---|
| 1010 | return err
|
---|
| 1011 | }
|
---|
| 1012 | }
|
---|
| 1013 |
|
---|
[183] | 1014 | if dc.clientName == "" && dc.networkName == "" {
|
---|
| 1015 | _, dc.clientName, dc.networkName = unmarshalUsername(dc.rawUsername)
|
---|
[168] | 1016 | }
|
---|
| 1017 |
|
---|
| 1018 | dc.registered = true
|
---|
[184] | 1019 | dc.logger.Printf("registration complete for user %q", dc.user.Username)
|
---|
[168] | 1020 | return nil
|
---|
| 1021 | }
|
---|
| 1022 |
|
---|
| 1023 | func (dc *downstreamConn) loadNetwork() error {
|
---|
| 1024 | if dc.networkName == "" {
|
---|
[112] | 1025 | return nil
|
---|
| 1026 | }
|
---|
[85] | 1027 |
|
---|
[168] | 1028 | network := dc.user.getNetwork(dc.networkName)
|
---|
[112] | 1029 | if network == nil {
|
---|
[168] | 1030 | addr := dc.networkName
|
---|
[112] | 1031 | if !strings.ContainsRune(addr, ':') {
|
---|
| 1032 | addr = addr + ":6697"
|
---|
| 1033 | }
|
---|
| 1034 |
|
---|
| 1035 | dc.logger.Printf("trying to connect to new network %q", addr)
|
---|
| 1036 | if err := sanityCheckServer(addr); err != nil {
|
---|
| 1037 | dc.logger.Printf("failed to connect to %q: %v", addr, err)
|
---|
| 1038 | return ircError{&irc.Message{
|
---|
| 1039 | Command: irc.ERR_PASSWDMISMATCH,
|
---|
[168] | 1040 | Params: []string{"*", fmt.Sprintf("Failed to connect to %q", dc.networkName)},
|
---|
[112] | 1041 | }}
|
---|
| 1042 | }
|
---|
| 1043 |
|
---|
[354] | 1044 | // Some clients only allow specifying the nickname (and use the
|
---|
| 1045 | // nickname as a username too). Strip the network name from the
|
---|
| 1046 | // nickname when auto-saving networks.
|
---|
| 1047 | nick, _, _ := unmarshalUsername(dc.nick)
|
---|
| 1048 |
|
---|
[168] | 1049 | dc.logger.Printf("auto-saving network %q", dc.networkName)
|
---|
[112] | 1050 | var err error
|
---|
[120] | 1051 | network, err = dc.user.createNetwork(&Network{
|
---|
[542] | 1052 | Addr: dc.networkName,
|
---|
| 1053 | Nick: nick,
|
---|
| 1054 | Enabled: true,
|
---|
[120] | 1055 | })
|
---|
[112] | 1056 | if err != nil {
|
---|
| 1057 | return err
|
---|
| 1058 | }
|
---|
| 1059 | }
|
---|
| 1060 |
|
---|
| 1061 | dc.network = network
|
---|
| 1062 | return nil
|
---|
| 1063 | }
|
---|
| 1064 |
|
---|
[168] | 1065 | func (dc *downstreamConn) welcome() error {
|
---|
| 1066 | if dc.user == nil || !dc.registered {
|
---|
| 1067 | panic("tried to welcome an unregistered connection")
|
---|
[37] | 1068 | }
|
---|
| 1069 |
|
---|
[168] | 1070 | // TODO: doing this might take some time. We should do it in dc.register
|
---|
| 1071 | // instead, but we'll potentially be adding a new network and this must be
|
---|
| 1072 | // done in the user goroutine.
|
---|
| 1073 | if err := dc.loadNetwork(); err != nil {
|
---|
| 1074 | return err
|
---|
[85] | 1075 | }
|
---|
| 1076 |
|
---|
[446] | 1077 | isupport := []string{
|
---|
| 1078 | fmt.Sprintf("CHATHISTORY=%v", dc.srv.HistoryLimit),
|
---|
[478] | 1079 | "CASEMAPPING=ascii",
|
---|
[446] | 1080 | }
|
---|
| 1081 |
|
---|
[532] | 1082 | if dc.network != nil {
|
---|
| 1083 | isupport = append(isupport, fmt.Sprintf("BOUNCER_NETID=%v", dc.network.ID))
|
---|
| 1084 | }
|
---|
| 1085 |
|
---|
[463] | 1086 | if uc := dc.upstream(); uc != nil {
|
---|
| 1087 | for k := range passthroughIsupport {
|
---|
| 1088 | v, ok := uc.isupport[k]
|
---|
| 1089 | if !ok {
|
---|
| 1090 | continue
|
---|
| 1091 | }
|
---|
| 1092 | if v != nil {
|
---|
| 1093 | isupport = append(isupport, fmt.Sprintf("%v=%v", k, *v))
|
---|
| 1094 | } else {
|
---|
| 1095 | isupport = append(isupport, k)
|
---|
| 1096 | }
|
---|
| 1097 | }
|
---|
[447] | 1098 | }
|
---|
| 1099 |
|
---|
[55] | 1100 | dc.SendMessage(&irc.Message{
|
---|
| 1101 | Prefix: dc.srv.prefix(),
|
---|
[13] | 1102 | Command: irc.RPL_WELCOME,
|
---|
[98] | 1103 | Params: []string{dc.nick, "Welcome to soju, " + dc.nick},
|
---|
[54] | 1104 | })
|
---|
[55] | 1105 | dc.SendMessage(&irc.Message{
|
---|
| 1106 | Prefix: dc.srv.prefix(),
|
---|
[13] | 1107 | Command: irc.RPL_YOURHOST,
|
---|
[55] | 1108 | Params: []string{dc.nick, "Your host is " + dc.srv.Hostname},
|
---|
[54] | 1109 | })
|
---|
[55] | 1110 | dc.SendMessage(&irc.Message{
|
---|
| 1111 | Prefix: dc.srv.prefix(),
|
---|
[13] | 1112 | Command: irc.RPL_CREATED,
|
---|
[55] | 1113 | Params: []string{dc.nick, "Who cares when the server was created?"},
|
---|
[54] | 1114 | })
|
---|
[55] | 1115 | dc.SendMessage(&irc.Message{
|
---|
| 1116 | Prefix: dc.srv.prefix(),
|
---|
[13] | 1117 | Command: irc.RPL_MYINFO,
|
---|
[98] | 1118 | Params: []string{dc.nick, dc.srv.Hostname, "soju", "aiwroO", "OovaimnqpsrtklbeI"},
|
---|
[54] | 1119 | })
|
---|
[463] | 1120 | for _, msg := range generateIsupport(dc.srv.prefix(), dc.nick, isupport) {
|
---|
| 1121 | dc.SendMessage(msg)
|
---|
| 1122 | }
|
---|
[555] | 1123 | motdHint := "No MOTD"
|
---|
[553] | 1124 | if uc := dc.upstream(); uc != nil {
|
---|
[555] | 1125 | motdHint = "Use /motd to read the message of the day"
|
---|
[553] | 1126 | dc.SendMessage(&irc.Message{
|
---|
| 1127 | Prefix: dc.srv.prefix(),
|
---|
| 1128 | Command: irc.RPL_UMODEIS,
|
---|
| 1129 | Params: []string{dc.nick, string(uc.modes)},
|
---|
| 1130 | })
|
---|
| 1131 | }
|
---|
[55] | 1132 | dc.SendMessage(&irc.Message{
|
---|
[447] | 1133 | Prefix: dc.srv.prefix(),
|
---|
[13] | 1134 | Command: irc.ERR_NOMOTD,
|
---|
[555] | 1135 | Params: []string{dc.nick, motdHint},
|
---|
[54] | 1136 | })
|
---|
[13] | 1137 |
|
---|
[296] | 1138 | dc.updateNick()
|
---|
[540] | 1139 | dc.updateRealname()
|
---|
[437] | 1140 | dc.updateSupportedCaps()
|
---|
[296] | 1141 |
|
---|
[535] | 1142 | if dc.caps["soju.im/bouncer-networks-notify"] {
|
---|
[551] | 1143 | dc.SendBatch("soju.im/bouncer-networks", nil, nil, func(batchRef irc.TagValue) {
|
---|
| 1144 | dc.user.forEachNetwork(func(network *network) {
|
---|
| 1145 | idStr := fmt.Sprintf("%v", network.ID)
|
---|
| 1146 | attrs := getNetworkAttrs(network)
|
---|
| 1147 | dc.SendMessage(&irc.Message{
|
---|
| 1148 | Tags: irc.Tags{"batch": batchRef},
|
---|
| 1149 | Prefix: dc.srv.prefix(),
|
---|
| 1150 | Command: "BOUNCER",
|
---|
| 1151 | Params: []string{"NETWORK", idStr, attrs.String()},
|
---|
| 1152 | })
|
---|
[535] | 1153 | })
|
---|
| 1154 | })
|
---|
| 1155 | }
|
---|
| 1156 |
|
---|
[73] | 1157 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[478] | 1158 | for _, entry := range uc.channels.innerMap {
|
---|
| 1159 | ch := entry.value.(*upstreamChannel)
|
---|
[284] | 1160 | if !ch.complete {
|
---|
| 1161 | continue
|
---|
| 1162 | }
|
---|
[478] | 1163 | record := uc.network.channels.Value(ch.Name)
|
---|
| 1164 | if record != nil && record.Detached {
|
---|
[284] | 1165 | continue
|
---|
| 1166 | }
|
---|
[132] | 1167 |
|
---|
[284] | 1168 | dc.SendMessage(&irc.Message{
|
---|
| 1169 | Prefix: dc.prefix(),
|
---|
| 1170 | Command: "JOIN",
|
---|
| 1171 | Params: []string{dc.marshalEntity(ch.conn.network, ch.Name)},
|
---|
| 1172 | })
|
---|
| 1173 |
|
---|
| 1174 | forwardChannel(dc, ch)
|
---|
[30] | 1175 | }
|
---|
[143] | 1176 | })
|
---|
[50] | 1177 |
|
---|
[143] | 1178 | dc.forEachNetwork(func(net *network) {
|
---|
[496] | 1179 | if dc.caps["draft/chathistory"] || dc.user.msgStore == nil {
|
---|
| 1180 | return
|
---|
| 1181 | }
|
---|
| 1182 |
|
---|
[253] | 1183 | // Only send history if we're the first connected client with that name
|
---|
| 1184 | // for the network
|
---|
[482] | 1185 | firstClient := true
|
---|
| 1186 | dc.user.forEachDownstream(func(c *downstreamConn) {
|
---|
| 1187 | if c != dc && c.clientName == dc.clientName && c.network == dc.network {
|
---|
| 1188 | firstClient = false
|
---|
| 1189 | }
|
---|
| 1190 | })
|
---|
| 1191 | if firstClient {
|
---|
[485] | 1192 | net.delivered.ForEachTarget(func(target string) {
|
---|
[495] | 1193 | lastDelivered := net.delivered.LoadID(target, dc.clientName)
|
---|
| 1194 | if lastDelivered == "" {
|
---|
| 1195 | return
|
---|
| 1196 | }
|
---|
| 1197 |
|
---|
| 1198 | dc.sendTargetBacklog(net, target, lastDelivered)
|
---|
| 1199 |
|
---|
| 1200 | // Fast-forward history to last message
|
---|
| 1201 | targetCM := net.casemap(target)
|
---|
| 1202 | lastID, err := dc.user.msgStore.LastMsgID(net, targetCM, time.Now())
|
---|
| 1203 | if err != nil {
|
---|
| 1204 | dc.logger.Printf("failed to get last message ID: %v", err)
|
---|
| 1205 | return
|
---|
| 1206 | }
|
---|
| 1207 | net.delivered.StoreID(target, dc.clientName, lastID)
|
---|
[485] | 1208 | })
|
---|
[227] | 1209 | }
|
---|
[253] | 1210 | })
|
---|
[57] | 1211 |
|
---|
[253] | 1212 | return nil
|
---|
| 1213 | }
|
---|
[144] | 1214 |
|
---|
[428] | 1215 | // messageSupportsHistory checks whether the provided message can be sent as
|
---|
| 1216 | // part of an history batch.
|
---|
| 1217 | func (dc *downstreamConn) messageSupportsHistory(msg *irc.Message) bool {
|
---|
| 1218 | // Don't replay all messages, because that would mess up client
|
---|
| 1219 | // state. For instance we just sent the list of users, sending
|
---|
| 1220 | // PART messages for one of these users would be incorrect.
|
---|
| 1221 | // TODO: add support for draft/event-playback
|
---|
| 1222 | switch msg.Command {
|
---|
| 1223 | case "PRIVMSG", "NOTICE":
|
---|
| 1224 | return true
|
---|
| 1225 | }
|
---|
| 1226 | return false
|
---|
| 1227 | }
|
---|
| 1228 |
|
---|
[495] | 1229 | func (dc *downstreamConn) sendTargetBacklog(net *network, target, msgID string) {
|
---|
[423] | 1230 | if dc.caps["draft/chathistory"] || dc.user.msgStore == nil {
|
---|
[319] | 1231 | return
|
---|
| 1232 | }
|
---|
[485] | 1233 |
|
---|
[499] | 1234 | ch := net.channels.Value(target)
|
---|
| 1235 |
|
---|
[452] | 1236 | limit := 4000
|
---|
[484] | 1237 | targetCM := net.casemap(target)
|
---|
[495] | 1238 | history, err := dc.user.msgStore.LoadLatestID(net, targetCM, msgID, limit)
|
---|
[452] | 1239 | if err != nil {
|
---|
[495] | 1240 | dc.logger.Printf("failed to send backlog for %q: %v", target, err)
|
---|
[452] | 1241 | return
|
---|
| 1242 | }
|
---|
[253] | 1243 |
|
---|
[551] | 1244 | dc.SendBatch("chathistory", []string{dc.marshalEntity(net, target)}, nil, func(batchRef irc.TagValue) {
|
---|
| 1245 | for _, msg := range history {
|
---|
| 1246 | if !dc.messageSupportsHistory(msg) {
|
---|
| 1247 | continue
|
---|
| 1248 | }
|
---|
[452] | 1249 |
|
---|
[551] | 1250 | if ch != nil && ch.Detached {
|
---|
| 1251 | if net.detachedMessageNeedsRelay(ch, msg) {
|
---|
| 1252 | dc.relayDetachedMessage(net, msg)
|
---|
| 1253 | }
|
---|
| 1254 | } else {
|
---|
| 1255 | if dc.caps["batch"] {
|
---|
| 1256 | msg.Tags["batch"] = irc.TagValue(batchRef)
|
---|
| 1257 | }
|
---|
| 1258 | dc.SendMessage(dc.marshalMessage(msg, net))
|
---|
[499] | 1259 | }
|
---|
[256] | 1260 | }
|
---|
[551] | 1261 | })
|
---|
[13] | 1262 | }
|
---|
| 1263 |
|
---|
[499] | 1264 | func (dc *downstreamConn) relayDetachedMessage(net *network, msg *irc.Message) {
|
---|
| 1265 | if msg.Command != "PRIVMSG" && msg.Command != "NOTICE" {
|
---|
| 1266 | return
|
---|
| 1267 | }
|
---|
| 1268 |
|
---|
| 1269 | sender := msg.Prefix.Name
|
---|
| 1270 | target, text := msg.Params[0], msg.Params[1]
|
---|
| 1271 | if net.isHighlight(msg) {
|
---|
| 1272 | sendServiceNOTICE(dc, fmt.Sprintf("highlight in %v: <%v> %v", dc.marshalEntity(net, target), sender, text))
|
---|
| 1273 | } else {
|
---|
| 1274 | sendServiceNOTICE(dc, fmt.Sprintf("message in %v: <%v> %v", dc.marshalEntity(net, target), sender, text))
|
---|
| 1275 | }
|
---|
| 1276 | }
|
---|
| 1277 |
|
---|
[103] | 1278 | func (dc *downstreamConn) runUntilRegistered() error {
|
---|
| 1279 | for !dc.registered {
|
---|
[212] | 1280 | msg, err := dc.ReadMessage()
|
---|
[106] | 1281 | if err != nil {
|
---|
[103] | 1282 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
| 1283 | }
|
---|
| 1284 |
|
---|
| 1285 | err = dc.handleMessage(msg)
|
---|
| 1286 | if ircErr, ok := err.(ircError); ok {
|
---|
| 1287 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
| 1288 | dc.SendMessage(ircErr.Message)
|
---|
| 1289 | } else if err != nil {
|
---|
| 1290 | return fmt.Errorf("failed to handle IRC command %q: %v", msg, err)
|
---|
| 1291 | }
|
---|
| 1292 | }
|
---|
| 1293 |
|
---|
| 1294 | return nil
|
---|
| 1295 | }
|
---|
| 1296 |
|
---|
[55] | 1297 | func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
|
---|
[13] | 1298 | switch msg.Command {
|
---|
[111] | 1299 | case "CAP":
|
---|
| 1300 | var subCmd string
|
---|
| 1301 | if err := parseMessageParams(msg, &subCmd); err != nil {
|
---|
| 1302 | return err
|
---|
| 1303 | }
|
---|
| 1304 | if err := dc.handleCapCommand(subCmd, msg.Params[1:]); err != nil {
|
---|
| 1305 | return err
|
---|
| 1306 | }
|
---|
[107] | 1307 | case "PING":
|
---|
[412] | 1308 | var source, destination string
|
---|
| 1309 | if err := parseMessageParams(msg, &source); err != nil {
|
---|
| 1310 | return err
|
---|
| 1311 | }
|
---|
| 1312 | if len(msg.Params) > 1 {
|
---|
| 1313 | destination = msg.Params[1]
|
---|
| 1314 | }
|
---|
| 1315 | if destination != "" && destination != dc.srv.Hostname {
|
---|
| 1316 | return ircError{&irc.Message{
|
---|
| 1317 | Command: irc.ERR_NOSUCHSERVER,
|
---|
[413] | 1318 | Params: []string{dc.nick, destination, "No such server"},
|
---|
[412] | 1319 | }}
|
---|
| 1320 | }
|
---|
[107] | 1321 | dc.SendMessage(&irc.Message{
|
---|
| 1322 | Prefix: dc.srv.prefix(),
|
---|
| 1323 | Command: "PONG",
|
---|
[412] | 1324 | Params: []string{dc.srv.Hostname, source},
|
---|
[107] | 1325 | })
|
---|
| 1326 | return nil
|
---|
[428] | 1327 | case "PONG":
|
---|
| 1328 | if len(msg.Params) == 0 {
|
---|
| 1329 | return newNeedMoreParamsError(msg.Command)
|
---|
| 1330 | }
|
---|
| 1331 | token := msg.Params[len(msg.Params)-1]
|
---|
| 1332 | dc.handlePong(token)
|
---|
[42] | 1333 | case "USER":
|
---|
[13] | 1334 | return ircError{&irc.Message{
|
---|
| 1335 | Command: irc.ERR_ALREADYREGISTERED,
|
---|
[55] | 1336 | Params: []string{dc.nick, "You may not reregister"},
|
---|
[13] | 1337 | }}
|
---|
[42] | 1338 | case "NICK":
|
---|
[429] | 1339 | var rawNick string
|
---|
| 1340 | if err := parseMessageParams(msg, &rawNick); err != nil {
|
---|
[90] | 1341 | return err
|
---|
| 1342 | }
|
---|
| 1343 |
|
---|
[429] | 1344 | nick := rawNick
|
---|
[297] | 1345 | var upstream *upstreamConn
|
---|
| 1346 | if dc.upstream() == nil {
|
---|
| 1347 | uc, unmarshaledNick, err := dc.unmarshalEntity(nick)
|
---|
| 1348 | if err == nil { // NICK nick/network: NICK only on a specific upstream
|
---|
| 1349 | upstream = uc
|
---|
| 1350 | nick = unmarshaledNick
|
---|
| 1351 | }
|
---|
| 1352 | }
|
---|
| 1353 |
|
---|
[404] | 1354 | if strings.ContainsAny(nick, illegalNickChars) {
|
---|
| 1355 | return ircError{&irc.Message{
|
---|
| 1356 | Command: irc.ERR_ERRONEUSNICKNAME,
|
---|
[430] | 1357 | Params: []string{dc.nick, rawNick, "contains illegal characters"},
|
---|
[404] | 1358 | }}
|
---|
| 1359 | }
|
---|
[478] | 1360 | if casemapASCII(nick) == serviceNickCM {
|
---|
[429] | 1361 | return ircError{&irc.Message{
|
---|
| 1362 | Command: irc.ERR_NICKNAMEINUSE,
|
---|
| 1363 | Params: []string{dc.nick, rawNick, "Nickname reserved for bouncer service"},
|
---|
| 1364 | }}
|
---|
| 1365 | }
|
---|
[404] | 1366 |
|
---|
[90] | 1367 | var err error
|
---|
| 1368 | dc.forEachNetwork(func(n *network) {
|
---|
[297] | 1369 | if err != nil || (upstream != nil && upstream.network != n) {
|
---|
[90] | 1370 | return
|
---|
| 1371 | }
|
---|
| 1372 | n.Nick = nick
|
---|
[421] | 1373 | err = dc.srv.db.StoreNetwork(dc.user.ID, &n.Network)
|
---|
[90] | 1374 | })
|
---|
| 1375 | if err != nil {
|
---|
| 1376 | return err
|
---|
| 1377 | }
|
---|
| 1378 |
|
---|
[73] | 1379 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[297] | 1380 | if upstream != nil && upstream != uc {
|
---|
| 1381 | return
|
---|
| 1382 | }
|
---|
[301] | 1383 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[297] | 1384 | Command: "NICK",
|
---|
| 1385 | Params: []string{nick},
|
---|
| 1386 | })
|
---|
[42] | 1387 | })
|
---|
[296] | 1388 |
|
---|
[512] | 1389 | if dc.upstream() == nil && upstream == nil && dc.nick != nick {
|
---|
[296] | 1390 | dc.SendMessage(&irc.Message{
|
---|
| 1391 | Prefix: dc.prefix(),
|
---|
| 1392 | Command: "NICK",
|
---|
| 1393 | Params: []string{nick},
|
---|
| 1394 | })
|
---|
| 1395 | dc.nick = nick
|
---|
[478] | 1396 | dc.nickCM = casemapASCII(dc.nick)
|
---|
[296] | 1397 | }
|
---|
[540] | 1398 | case "SETNAME":
|
---|
| 1399 | var realname string
|
---|
| 1400 | if err := parseMessageParams(msg, &realname); err != nil {
|
---|
| 1401 | return err
|
---|
| 1402 | }
|
---|
| 1403 |
|
---|
[568] | 1404 | // If the client just resets to the default, just wipe the per-network
|
---|
| 1405 | // preference
|
---|
| 1406 | storeRealname := realname
|
---|
| 1407 | if realname == dc.user.Realname {
|
---|
| 1408 | storeRealname = ""
|
---|
| 1409 | }
|
---|
| 1410 |
|
---|
[540] | 1411 | var storeErr error
|
---|
| 1412 | var needUpdate []Network
|
---|
| 1413 | dc.forEachNetwork(func(n *network) {
|
---|
| 1414 | // We only need to call updateNetwork for upstreams that don't
|
---|
| 1415 | // support setname
|
---|
| 1416 | if uc := n.conn; uc != nil && uc.caps["setname"] {
|
---|
| 1417 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
| 1418 | Command: "SETNAME",
|
---|
| 1419 | Params: []string{realname},
|
---|
| 1420 | })
|
---|
| 1421 |
|
---|
[568] | 1422 | n.Realname = storeRealname
|
---|
[540] | 1423 | if err := dc.srv.db.StoreNetwork(dc.user.ID, &n.Network); err != nil {
|
---|
| 1424 | dc.logger.Printf("failed to store network realname: %v", err)
|
---|
| 1425 | storeErr = err
|
---|
| 1426 | }
|
---|
| 1427 | return
|
---|
| 1428 | }
|
---|
| 1429 |
|
---|
| 1430 | record := n.Network // copy network record because we'll mutate it
|
---|
[568] | 1431 | record.Realname = storeRealname
|
---|
[540] | 1432 | needUpdate = append(needUpdate, record)
|
---|
| 1433 | })
|
---|
| 1434 |
|
---|
| 1435 | // Walk the network list as a second step, because updateNetwork
|
---|
| 1436 | // mutates the original list
|
---|
| 1437 | for _, record := range needUpdate {
|
---|
| 1438 | if _, err := dc.user.updateNetwork(&record); err != nil {
|
---|
| 1439 | dc.logger.Printf("failed to update network realname: %v", err)
|
---|
| 1440 | storeErr = err
|
---|
| 1441 | }
|
---|
| 1442 | }
|
---|
| 1443 | if storeErr != nil {
|
---|
| 1444 | return ircError{&irc.Message{
|
---|
| 1445 | Command: "FAIL",
|
---|
| 1446 | Params: []string{"SETNAME", "CANNOT_CHANGE_REALNAME", "Failed to update realname"},
|
---|
| 1447 | }}
|
---|
| 1448 | }
|
---|
| 1449 |
|
---|
| 1450 | if dc.upstream() == nil && dc.caps["setname"] {
|
---|
| 1451 | dc.SendMessage(&irc.Message{
|
---|
| 1452 | Prefix: dc.prefix(),
|
---|
| 1453 | Command: "SETNAME",
|
---|
| 1454 | Params: []string{realname},
|
---|
| 1455 | })
|
---|
| 1456 | }
|
---|
[146] | 1457 | case "JOIN":
|
---|
| 1458 | var namesStr string
|
---|
| 1459 | if err := parseMessageParams(msg, &namesStr); err != nil {
|
---|
[48] | 1460 | return err
|
---|
| 1461 | }
|
---|
| 1462 |
|
---|
[146] | 1463 | var keys []string
|
---|
| 1464 | if len(msg.Params) > 1 {
|
---|
| 1465 | keys = strings.Split(msg.Params[1], ",")
|
---|
| 1466 | }
|
---|
| 1467 |
|
---|
| 1468 | for i, name := range strings.Split(namesStr, ",") {
|
---|
[145] | 1469 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1470 | if err != nil {
|
---|
[158] | 1471 | return err
|
---|
[145] | 1472 | }
|
---|
[48] | 1473 |
|
---|
[146] | 1474 | var key string
|
---|
| 1475 | if len(keys) > i {
|
---|
| 1476 | key = keys[i]
|
---|
| 1477 | }
|
---|
| 1478 |
|
---|
[545] | 1479 | if !uc.isChannel(upstreamName) {
|
---|
| 1480 | dc.SendMessage(&irc.Message{
|
---|
| 1481 | Prefix: dc.srv.prefix(),
|
---|
| 1482 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
| 1483 | Params: []string{name, "Not a channel name"},
|
---|
| 1484 | })
|
---|
| 1485 | continue
|
---|
| 1486 | }
|
---|
| 1487 |
|
---|
[146] | 1488 | params := []string{upstreamName}
|
---|
| 1489 | if key != "" {
|
---|
| 1490 | params = append(params, key)
|
---|
| 1491 | }
|
---|
[301] | 1492 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[146] | 1493 | Command: "JOIN",
|
---|
| 1494 | Params: params,
|
---|
[145] | 1495 | })
|
---|
[89] | 1496 |
|
---|
[478] | 1497 | ch := uc.network.channels.Value(upstreamName)
|
---|
| 1498 | if ch != nil {
|
---|
[285] | 1499 | // Don't clear the channel key if there's one set
|
---|
| 1500 | // TODO: add a way to unset the channel key
|
---|
[435] | 1501 | if key != "" {
|
---|
| 1502 | ch.Key = key
|
---|
| 1503 | }
|
---|
| 1504 | uc.network.attach(ch)
|
---|
| 1505 | } else {
|
---|
| 1506 | ch = &Channel{
|
---|
| 1507 | Name: upstreamName,
|
---|
| 1508 | Key: key,
|
---|
| 1509 | }
|
---|
[478] | 1510 | uc.network.channels.SetValue(upstreamName, ch)
|
---|
[285] | 1511 | }
|
---|
[435] | 1512 | if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
|
---|
[222] | 1513 | dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
|
---|
[89] | 1514 | }
|
---|
| 1515 | }
|
---|
[146] | 1516 | case "PART":
|
---|
| 1517 | var namesStr string
|
---|
| 1518 | if err := parseMessageParams(msg, &namesStr); err != nil {
|
---|
| 1519 | return err
|
---|
| 1520 | }
|
---|
| 1521 |
|
---|
| 1522 | var reason string
|
---|
| 1523 | if len(msg.Params) > 1 {
|
---|
| 1524 | reason = msg.Params[1]
|
---|
| 1525 | }
|
---|
| 1526 |
|
---|
| 1527 | for _, name := range strings.Split(namesStr, ",") {
|
---|
| 1528 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1529 | if err != nil {
|
---|
[158] | 1530 | return err
|
---|
[146] | 1531 | }
|
---|
| 1532 |
|
---|
[284] | 1533 | if strings.EqualFold(reason, "detach") {
|
---|
[478] | 1534 | ch := uc.network.channels.Value(upstreamName)
|
---|
| 1535 | if ch != nil {
|
---|
[435] | 1536 | uc.network.detach(ch)
|
---|
| 1537 | } else {
|
---|
| 1538 | ch = &Channel{
|
---|
| 1539 | Name: name,
|
---|
| 1540 | Detached: true,
|
---|
| 1541 | }
|
---|
[478] | 1542 | uc.network.channels.SetValue(upstreamName, ch)
|
---|
[284] | 1543 | }
|
---|
[435] | 1544 | if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
|
---|
| 1545 | dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
|
---|
| 1546 | }
|
---|
[284] | 1547 | } else {
|
---|
| 1548 | params := []string{upstreamName}
|
---|
| 1549 | if reason != "" {
|
---|
| 1550 | params = append(params, reason)
|
---|
| 1551 | }
|
---|
[301] | 1552 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[284] | 1553 | Command: "PART",
|
---|
| 1554 | Params: params,
|
---|
| 1555 | })
|
---|
[146] | 1556 |
|
---|
[284] | 1557 | if err := uc.network.deleteChannel(upstreamName); err != nil {
|
---|
| 1558 | dc.logger.Printf("failed to delete channel %q: %v", upstreamName, err)
|
---|
| 1559 | }
|
---|
[146] | 1560 | }
|
---|
| 1561 | }
|
---|
[159] | 1562 | case "KICK":
|
---|
| 1563 | var channelStr, userStr string
|
---|
| 1564 | if err := parseMessageParams(msg, &channelStr, &userStr); err != nil {
|
---|
| 1565 | return err
|
---|
| 1566 | }
|
---|
| 1567 |
|
---|
| 1568 | channels := strings.Split(channelStr, ",")
|
---|
| 1569 | users := strings.Split(userStr, ",")
|
---|
| 1570 |
|
---|
| 1571 | var reason string
|
---|
| 1572 | if len(msg.Params) > 2 {
|
---|
| 1573 | reason = msg.Params[2]
|
---|
| 1574 | }
|
---|
| 1575 |
|
---|
| 1576 | if len(channels) != 1 && len(channels) != len(users) {
|
---|
| 1577 | return ircError{&irc.Message{
|
---|
| 1578 | Command: irc.ERR_BADCHANMASK,
|
---|
| 1579 | Params: []string{dc.nick, channelStr, "Bad channel mask"},
|
---|
| 1580 | }}
|
---|
| 1581 | }
|
---|
| 1582 |
|
---|
| 1583 | for i, user := range users {
|
---|
| 1584 | var channel string
|
---|
| 1585 | if len(channels) == 1 {
|
---|
| 1586 | channel = channels[0]
|
---|
| 1587 | } else {
|
---|
| 1588 | channel = channels[i]
|
---|
| 1589 | }
|
---|
| 1590 |
|
---|
| 1591 | ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1592 | if err != nil {
|
---|
| 1593 | return err
|
---|
| 1594 | }
|
---|
| 1595 |
|
---|
| 1596 | ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
---|
| 1597 | if err != nil {
|
---|
| 1598 | return err
|
---|
| 1599 | }
|
---|
| 1600 |
|
---|
| 1601 | if ucChannel != ucUser {
|
---|
| 1602 | return ircError{&irc.Message{
|
---|
| 1603 | Command: irc.ERR_USERNOTINCHANNEL,
|
---|
[400] | 1604 | Params: []string{dc.nick, user, channel, "They are on another network"},
|
---|
[159] | 1605 | }}
|
---|
| 1606 | }
|
---|
| 1607 | uc := ucChannel
|
---|
| 1608 |
|
---|
| 1609 | params := []string{upstreamChannel, upstreamUser}
|
---|
| 1610 | if reason != "" {
|
---|
| 1611 | params = append(params, reason)
|
---|
| 1612 | }
|
---|
[301] | 1613 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[159] | 1614 | Command: "KICK",
|
---|
| 1615 | Params: params,
|
---|
| 1616 | })
|
---|
| 1617 | }
|
---|
[69] | 1618 | case "MODE":
|
---|
[46] | 1619 | var name string
|
---|
| 1620 | if err := parseMessageParams(msg, &name); err != nil {
|
---|
| 1621 | return err
|
---|
| 1622 | }
|
---|
| 1623 |
|
---|
| 1624 | var modeStr string
|
---|
| 1625 | if len(msg.Params) > 1 {
|
---|
| 1626 | modeStr = msg.Params[1]
|
---|
| 1627 | }
|
---|
| 1628 |
|
---|
[478] | 1629 | if casemapASCII(name) == dc.nickCM {
|
---|
[46] | 1630 | if modeStr != "" {
|
---|
[554] | 1631 | if uc := dc.upstream(); uc != nil {
|
---|
[301] | 1632 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[69] | 1633 | Command: "MODE",
|
---|
| 1634 | Params: []string{uc.nick, modeStr},
|
---|
| 1635 | })
|
---|
[554] | 1636 | } else {
|
---|
| 1637 | dc.SendMessage(&irc.Message{
|
---|
| 1638 | Prefix: dc.srv.prefix(),
|
---|
| 1639 | Command: irc.ERR_UMODEUNKNOWNFLAG,
|
---|
| 1640 | Params: []string{dc.nick, "Cannot change user mode in multi-upstream mode"},
|
---|
| 1641 | })
|
---|
| 1642 | }
|
---|
[46] | 1643 | } else {
|
---|
[553] | 1644 | var userMode string
|
---|
| 1645 | if uc := dc.upstream(); uc != nil {
|
---|
| 1646 | userMode = string(uc.modes)
|
---|
| 1647 | }
|
---|
| 1648 |
|
---|
[55] | 1649 | dc.SendMessage(&irc.Message{
|
---|
| 1650 | Prefix: dc.srv.prefix(),
|
---|
[46] | 1651 | Command: irc.RPL_UMODEIS,
|
---|
[553] | 1652 | Params: []string{dc.nick, userMode},
|
---|
[54] | 1653 | })
|
---|
[46] | 1654 | }
|
---|
[139] | 1655 | return nil
|
---|
[46] | 1656 | }
|
---|
[139] | 1657 |
|
---|
| 1658 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1659 | if err != nil {
|
---|
| 1660 | return err
|
---|
| 1661 | }
|
---|
| 1662 |
|
---|
| 1663 | if !uc.isChannel(upstreamName) {
|
---|
| 1664 | return ircError{&irc.Message{
|
---|
| 1665 | Command: irc.ERR_USERSDONTMATCH,
|
---|
| 1666 | Params: []string{dc.nick, "Cannot change mode for other users"},
|
---|
| 1667 | }}
|
---|
| 1668 | }
|
---|
| 1669 |
|
---|
| 1670 | if modeStr != "" {
|
---|
| 1671 | params := []string{upstreamName, modeStr}
|
---|
| 1672 | params = append(params, msg.Params[2:]...)
|
---|
[301] | 1673 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[139] | 1674 | Command: "MODE",
|
---|
| 1675 | Params: params,
|
---|
| 1676 | })
|
---|
| 1677 | } else {
|
---|
[478] | 1678 | ch := uc.channels.Value(upstreamName)
|
---|
| 1679 | if ch == nil {
|
---|
[139] | 1680 | return ircError{&irc.Message{
|
---|
| 1681 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
| 1682 | Params: []string{dc.nick, name, "No such channel"},
|
---|
| 1683 | }}
|
---|
| 1684 | }
|
---|
| 1685 |
|
---|
| 1686 | if ch.modes == nil {
|
---|
| 1687 | // we haven't received the initial RPL_CHANNELMODEIS yet
|
---|
| 1688 | // ignore the request, we will broadcast the modes later when we receive RPL_CHANNELMODEIS
|
---|
| 1689 | return nil
|
---|
| 1690 | }
|
---|
| 1691 |
|
---|
| 1692 | modeStr, modeParams := ch.modes.Format()
|
---|
| 1693 | params := []string{dc.nick, name, modeStr}
|
---|
| 1694 | params = append(params, modeParams...)
|
---|
| 1695 |
|
---|
| 1696 | dc.SendMessage(&irc.Message{
|
---|
| 1697 | Prefix: dc.srv.prefix(),
|
---|
| 1698 | Command: irc.RPL_CHANNELMODEIS,
|
---|
| 1699 | Params: params,
|
---|
| 1700 | })
|
---|
[162] | 1701 | if ch.creationTime != "" {
|
---|
| 1702 | dc.SendMessage(&irc.Message{
|
---|
| 1703 | Prefix: dc.srv.prefix(),
|
---|
| 1704 | Command: rpl_creationtime,
|
---|
| 1705 | Params: []string{dc.nick, name, ch.creationTime},
|
---|
| 1706 | })
|
---|
| 1707 | }
|
---|
[139] | 1708 | }
|
---|
[160] | 1709 | case "TOPIC":
|
---|
| 1710 | var channel string
|
---|
| 1711 | if err := parseMessageParams(msg, &channel); err != nil {
|
---|
| 1712 | return err
|
---|
| 1713 | }
|
---|
| 1714 |
|
---|
[478] | 1715 | uc, upstreamName, err := dc.unmarshalEntity(channel)
|
---|
[160] | 1716 | if err != nil {
|
---|
| 1717 | return err
|
---|
| 1718 | }
|
---|
| 1719 |
|
---|
| 1720 | if len(msg.Params) > 1 { // setting topic
|
---|
| 1721 | topic := msg.Params[1]
|
---|
[301] | 1722 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[160] | 1723 | Command: "TOPIC",
|
---|
[478] | 1724 | Params: []string{upstreamName, topic},
|
---|
[160] | 1725 | })
|
---|
| 1726 | } else { // getting topic
|
---|
[478] | 1727 | ch := uc.channels.Value(upstreamName)
|
---|
| 1728 | if ch == nil {
|
---|
[160] | 1729 | return ircError{&irc.Message{
|
---|
| 1730 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
[478] | 1731 | Params: []string{dc.nick, upstreamName, "No such channel"},
|
---|
[160] | 1732 | }}
|
---|
| 1733 | }
|
---|
| 1734 | sendTopic(dc, ch)
|
---|
| 1735 | }
|
---|
[177] | 1736 | case "LIST":
|
---|
| 1737 | // TODO: support ELIST when supported by all upstreams
|
---|
| 1738 |
|
---|
| 1739 | pl := pendingLIST{
|
---|
| 1740 | downstreamID: dc.id,
|
---|
| 1741 | pendingCommands: make(map[int64]*irc.Message),
|
---|
| 1742 | }
|
---|
[298] | 1743 | var upstream *upstreamConn
|
---|
[177] | 1744 | var upstreamChannels map[int64][]string
|
---|
| 1745 | if len(msg.Params) > 0 {
|
---|
[298] | 1746 | uc, upstreamMask, err := dc.unmarshalEntity(msg.Params[0])
|
---|
| 1747 | if err == nil && upstreamMask == "*" { // LIST */network: send LIST only to one network
|
---|
| 1748 | upstream = uc
|
---|
| 1749 | } else {
|
---|
| 1750 | upstreamChannels = make(map[int64][]string)
|
---|
| 1751 | channels := strings.Split(msg.Params[0], ",")
|
---|
| 1752 | for _, channel := range channels {
|
---|
| 1753 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1754 | if err != nil {
|
---|
| 1755 | return err
|
---|
| 1756 | }
|
---|
| 1757 | upstreamChannels[uc.network.ID] = append(upstreamChannels[uc.network.ID], upstreamChannel)
|
---|
[177] | 1758 | }
|
---|
| 1759 | }
|
---|
| 1760 | }
|
---|
| 1761 |
|
---|
| 1762 | dc.user.pendingLISTs = append(dc.user.pendingLISTs, pl)
|
---|
| 1763 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[298] | 1764 | if upstream != nil && upstream != uc {
|
---|
| 1765 | return
|
---|
| 1766 | }
|
---|
[177] | 1767 | var params []string
|
---|
| 1768 | if upstreamChannels != nil {
|
---|
| 1769 | if channels, ok := upstreamChannels[uc.network.ID]; ok {
|
---|
| 1770 | params = []string{strings.Join(channels, ",")}
|
---|
| 1771 | } else {
|
---|
| 1772 | return
|
---|
| 1773 | }
|
---|
| 1774 | }
|
---|
| 1775 | pl.pendingCommands[uc.network.ID] = &irc.Message{
|
---|
| 1776 | Command: "LIST",
|
---|
| 1777 | Params: params,
|
---|
| 1778 | }
|
---|
[181] | 1779 | uc.trySendLIST(dc.id)
|
---|
[177] | 1780 | })
|
---|
[140] | 1781 | case "NAMES":
|
---|
| 1782 | if len(msg.Params) == 0 {
|
---|
| 1783 | dc.SendMessage(&irc.Message{
|
---|
| 1784 | Prefix: dc.srv.prefix(),
|
---|
| 1785 | Command: irc.RPL_ENDOFNAMES,
|
---|
| 1786 | Params: []string{dc.nick, "*", "End of /NAMES list"},
|
---|
| 1787 | })
|
---|
| 1788 | return nil
|
---|
| 1789 | }
|
---|
| 1790 |
|
---|
| 1791 | channels := strings.Split(msg.Params[0], ",")
|
---|
| 1792 | for _, channel := range channels {
|
---|
[478] | 1793 | uc, upstreamName, err := dc.unmarshalEntity(channel)
|
---|
[140] | 1794 | if err != nil {
|
---|
| 1795 | return err
|
---|
| 1796 | }
|
---|
| 1797 |
|
---|
[478] | 1798 | ch := uc.channels.Value(upstreamName)
|
---|
| 1799 | if ch != nil {
|
---|
[140] | 1800 | sendNames(dc, ch)
|
---|
| 1801 | } else {
|
---|
| 1802 | // NAMES on a channel we have not joined, ask upstream
|
---|
[176] | 1803 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[140] | 1804 | Command: "NAMES",
|
---|
[478] | 1805 | Params: []string{upstreamName},
|
---|
[140] | 1806 | })
|
---|
| 1807 | }
|
---|
| 1808 | }
|
---|
[127] | 1809 | case "WHO":
|
---|
| 1810 | if len(msg.Params) == 0 {
|
---|
| 1811 | // TODO: support WHO without parameters
|
---|
| 1812 | dc.SendMessage(&irc.Message{
|
---|
| 1813 | Prefix: dc.srv.prefix(),
|
---|
| 1814 | Command: irc.RPL_ENDOFWHO,
|
---|
[140] | 1815 | Params: []string{dc.nick, "*", "End of /WHO list"},
|
---|
[127] | 1816 | })
|
---|
| 1817 | return nil
|
---|
| 1818 | }
|
---|
| 1819 |
|
---|
| 1820 | // TODO: support WHO masks
|
---|
| 1821 | entity := msg.Params[0]
|
---|
[478] | 1822 | entityCM := casemapASCII(entity)
|
---|
[127] | 1823 |
|
---|
[520] | 1824 | if dc.network == nil && entityCM == dc.nickCM {
|
---|
[142] | 1825 | // TODO: support AWAY (H/G) in self WHO reply
|
---|
| 1826 | dc.SendMessage(&irc.Message{
|
---|
| 1827 | Prefix: dc.srv.prefix(),
|
---|
| 1828 | Command: irc.RPL_WHOREPLY,
|
---|
[184] | 1829 | Params: []string{dc.nick, "*", dc.user.Username, dc.hostname, dc.srv.Hostname, dc.nick, "H", "0 " + dc.realname},
|
---|
[142] | 1830 | })
|
---|
| 1831 | dc.SendMessage(&irc.Message{
|
---|
| 1832 | Prefix: dc.srv.prefix(),
|
---|
| 1833 | Command: irc.RPL_ENDOFWHO,
|
---|
| 1834 | Params: []string{dc.nick, dc.nick, "End of /WHO list"},
|
---|
| 1835 | })
|
---|
| 1836 | return nil
|
---|
| 1837 | }
|
---|
[478] | 1838 | if entityCM == serviceNickCM {
|
---|
[343] | 1839 | dc.SendMessage(&irc.Message{
|
---|
| 1840 | Prefix: dc.srv.prefix(),
|
---|
| 1841 | Command: irc.RPL_WHOREPLY,
|
---|
| 1842 | Params: []string{serviceNick, "*", servicePrefix.User, servicePrefix.Host, dc.srv.Hostname, serviceNick, "H", "0 " + serviceRealname},
|
---|
| 1843 | })
|
---|
| 1844 | dc.SendMessage(&irc.Message{
|
---|
| 1845 | Prefix: dc.srv.prefix(),
|
---|
| 1846 | Command: irc.RPL_ENDOFWHO,
|
---|
| 1847 | Params: []string{dc.nick, serviceNick, "End of /WHO list"},
|
---|
| 1848 | })
|
---|
| 1849 | return nil
|
---|
| 1850 | }
|
---|
[142] | 1851 |
|
---|
[127] | 1852 | uc, upstreamName, err := dc.unmarshalEntity(entity)
|
---|
| 1853 | if err != nil {
|
---|
| 1854 | return err
|
---|
| 1855 | }
|
---|
| 1856 |
|
---|
| 1857 | var params []string
|
---|
| 1858 | if len(msg.Params) == 2 {
|
---|
| 1859 | params = []string{upstreamName, msg.Params[1]}
|
---|
| 1860 | } else {
|
---|
| 1861 | params = []string{upstreamName}
|
---|
| 1862 | }
|
---|
| 1863 |
|
---|
[176] | 1864 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[127] | 1865 | Command: "WHO",
|
---|
| 1866 | Params: params,
|
---|
| 1867 | })
|
---|
[128] | 1868 | case "WHOIS":
|
---|
| 1869 | if len(msg.Params) == 0 {
|
---|
| 1870 | return ircError{&irc.Message{
|
---|
| 1871 | Command: irc.ERR_NONICKNAMEGIVEN,
|
---|
| 1872 | Params: []string{dc.nick, "No nickname given"},
|
---|
| 1873 | }}
|
---|
| 1874 | }
|
---|
| 1875 |
|
---|
| 1876 | var target, mask string
|
---|
| 1877 | if len(msg.Params) == 1 {
|
---|
| 1878 | target = ""
|
---|
| 1879 | mask = msg.Params[0]
|
---|
| 1880 | } else {
|
---|
| 1881 | target = msg.Params[0]
|
---|
| 1882 | mask = msg.Params[1]
|
---|
| 1883 | }
|
---|
| 1884 | // TODO: support multiple WHOIS users
|
---|
| 1885 | if i := strings.IndexByte(mask, ','); i >= 0 {
|
---|
| 1886 | mask = mask[:i]
|
---|
| 1887 | }
|
---|
| 1888 |
|
---|
[520] | 1889 | if dc.network == nil && casemapASCII(mask) == dc.nickCM {
|
---|
[142] | 1890 | dc.SendMessage(&irc.Message{
|
---|
| 1891 | Prefix: dc.srv.prefix(),
|
---|
| 1892 | Command: irc.RPL_WHOISUSER,
|
---|
[184] | 1893 | Params: []string{dc.nick, dc.nick, dc.user.Username, dc.hostname, "*", dc.realname},
|
---|
[142] | 1894 | })
|
---|
| 1895 | dc.SendMessage(&irc.Message{
|
---|
| 1896 | Prefix: dc.srv.prefix(),
|
---|
| 1897 | Command: irc.RPL_WHOISSERVER,
|
---|
| 1898 | Params: []string{dc.nick, dc.nick, dc.srv.Hostname, "soju"},
|
---|
| 1899 | })
|
---|
| 1900 | dc.SendMessage(&irc.Message{
|
---|
| 1901 | Prefix: dc.srv.prefix(),
|
---|
| 1902 | Command: irc.RPL_ENDOFWHOIS,
|
---|
| 1903 | Params: []string{dc.nick, dc.nick, "End of /WHOIS list"},
|
---|
| 1904 | })
|
---|
| 1905 | return nil
|
---|
| 1906 | }
|
---|
| 1907 |
|
---|
[128] | 1908 | // TODO: support WHOIS masks
|
---|
| 1909 | uc, upstreamNick, err := dc.unmarshalEntity(mask)
|
---|
| 1910 | if err != nil {
|
---|
| 1911 | return err
|
---|
| 1912 | }
|
---|
| 1913 |
|
---|
| 1914 | var params []string
|
---|
| 1915 | if target != "" {
|
---|
[299] | 1916 | if target == mask { // WHOIS nick nick
|
---|
| 1917 | params = []string{upstreamNick, upstreamNick}
|
---|
| 1918 | } else {
|
---|
| 1919 | params = []string{target, upstreamNick}
|
---|
| 1920 | }
|
---|
[128] | 1921 | } else {
|
---|
| 1922 | params = []string{upstreamNick}
|
---|
| 1923 | }
|
---|
| 1924 |
|
---|
[176] | 1925 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[128] | 1926 | Command: "WHOIS",
|
---|
| 1927 | Params: params,
|
---|
| 1928 | })
|
---|
[562] | 1929 | case "PRIVMSG", "NOTICE":
|
---|
[58] | 1930 | var targetsStr, text string
|
---|
| 1931 | if err := parseMessageParams(msg, &targetsStr, &text); err != nil {
|
---|
| 1932 | return err
|
---|
| 1933 | }
|
---|
[303] | 1934 | tags := copyClientTags(msg.Tags)
|
---|
[58] | 1935 |
|
---|
| 1936 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
[563] | 1937 | if name == "$"+dc.srv.Hostname || (name == "$*" && dc.network == nil) {
|
---|
| 1938 | // "$" means a server mask follows. If it's the bouncer's
|
---|
| 1939 | // hostname, broadcast the message to all bouncer users.
|
---|
| 1940 | if !dc.user.Admin {
|
---|
| 1941 | return ircError{&irc.Message{
|
---|
| 1942 | Prefix: dc.srv.prefix(),
|
---|
| 1943 | Command: irc.ERR_BADMASK,
|
---|
| 1944 | Params: []string{dc.nick, name, "Permission denied to broadcast message to all bouncer users"},
|
---|
| 1945 | }}
|
---|
| 1946 | }
|
---|
| 1947 |
|
---|
| 1948 | dc.logger.Printf("broadcasting bouncer-wide %v: %v", msg.Command, text)
|
---|
| 1949 |
|
---|
| 1950 | broadcastTags := tags.Copy()
|
---|
| 1951 | broadcastTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
| 1952 | broadcastMsg := &irc.Message{
|
---|
| 1953 | Tags: broadcastTags,
|
---|
| 1954 | Prefix: servicePrefix,
|
---|
| 1955 | Command: msg.Command,
|
---|
| 1956 | Params: []string{name, text},
|
---|
| 1957 | }
|
---|
| 1958 | dc.srv.forEachUser(func(u *user) {
|
---|
| 1959 | u.events <- eventBroadcast{broadcastMsg}
|
---|
| 1960 | })
|
---|
| 1961 | continue
|
---|
| 1962 | }
|
---|
| 1963 |
|
---|
[529] | 1964 | if dc.network == nil && casemapASCII(name) == dc.nickCM {
|
---|
| 1965 | dc.SendMessage(msg)
|
---|
| 1966 | continue
|
---|
| 1967 | }
|
---|
| 1968 |
|
---|
[562] | 1969 | if msg.Command == "PRIVMSG" && casemapASCII(name) == serviceNickCM {
|
---|
[431] | 1970 | if dc.caps["echo-message"] {
|
---|
| 1971 | echoTags := tags.Copy()
|
---|
| 1972 | echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
| 1973 | dc.SendMessage(&irc.Message{
|
---|
| 1974 | Tags: echoTags,
|
---|
| 1975 | Prefix: dc.prefix(),
|
---|
[562] | 1976 | Command: msg.Command,
|
---|
[431] | 1977 | Params: []string{name, text},
|
---|
| 1978 | })
|
---|
| 1979 | }
|
---|
[117] | 1980 | handleServicePRIVMSG(dc, text)
|
---|
| 1981 | continue
|
---|
| 1982 | }
|
---|
| 1983 |
|
---|
[127] | 1984 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
[58] | 1985 | if err != nil {
|
---|
| 1986 | return err
|
---|
| 1987 | }
|
---|
| 1988 |
|
---|
[562] | 1989 | if msg.Command == "PRIVMSG" && uc.network.casemap(upstreamName) == "nickserv" {
|
---|
[95] | 1990 | dc.handleNickServPRIVMSG(uc, text)
|
---|
| 1991 | }
|
---|
| 1992 |
|
---|
[268] | 1993 | unmarshaledText := text
|
---|
| 1994 | if uc.isChannel(upstreamName) {
|
---|
| 1995 | unmarshaledText = dc.unmarshalText(uc, text)
|
---|
| 1996 | }
|
---|
[301] | 1997 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[303] | 1998 | Tags: tags,
|
---|
[562] | 1999 | Command: msg.Command,
|
---|
[268] | 2000 | Params: []string{upstreamName, unmarshaledText},
|
---|
[60] | 2001 | })
|
---|
[105] | 2002 |
|
---|
[303] | 2003 | echoTags := tags.Copy()
|
---|
| 2004 | echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
[559] | 2005 | if uc.account != "" {
|
---|
| 2006 | echoTags["account"] = irc.TagValue(uc.account)
|
---|
| 2007 | }
|
---|
[113] | 2008 | echoMsg := &irc.Message{
|
---|
[303] | 2009 | Tags: echoTags,
|
---|
[113] | 2010 | Prefix: &irc.Prefix{
|
---|
| 2011 | Name: uc.nick,
|
---|
| 2012 | User: uc.username,
|
---|
| 2013 | },
|
---|
[562] | 2014 | Command: msg.Command,
|
---|
[113] | 2015 | Params: []string{upstreamName, text},
|
---|
| 2016 | }
|
---|
[239] | 2017 | uc.produce(upstreamName, echoMsg, dc)
|
---|
[435] | 2018 |
|
---|
| 2019 | uc.updateChannelAutoDetach(upstreamName)
|
---|
[58] | 2020 | }
|
---|
[303] | 2021 | case "TAGMSG":
|
---|
| 2022 | var targetsStr string
|
---|
| 2023 | if err := parseMessageParams(msg, &targetsStr); err != nil {
|
---|
| 2024 | return err
|
---|
| 2025 | }
|
---|
| 2026 | tags := copyClientTags(msg.Tags)
|
---|
| 2027 |
|
---|
| 2028 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
| 2029 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 2030 | if err != nil {
|
---|
| 2031 | return err
|
---|
| 2032 | }
|
---|
[427] | 2033 | if _, ok := uc.caps["message-tags"]; !ok {
|
---|
| 2034 | continue
|
---|
| 2035 | }
|
---|
[303] | 2036 |
|
---|
| 2037 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
| 2038 | Tags: tags,
|
---|
| 2039 | Command: "TAGMSG",
|
---|
| 2040 | Params: []string{upstreamName},
|
---|
| 2041 | })
|
---|
[435] | 2042 |
|
---|
| 2043 | uc.updateChannelAutoDetach(upstreamName)
|
---|
[303] | 2044 | }
|
---|
[163] | 2045 | case "INVITE":
|
---|
| 2046 | var user, channel string
|
---|
| 2047 | if err := parseMessageParams(msg, &user, &channel); err != nil {
|
---|
| 2048 | return err
|
---|
| 2049 | }
|
---|
| 2050 |
|
---|
| 2051 | ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 2052 | if err != nil {
|
---|
| 2053 | return err
|
---|
| 2054 | }
|
---|
| 2055 |
|
---|
| 2056 | ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
---|
| 2057 | if err != nil {
|
---|
| 2058 | return err
|
---|
| 2059 | }
|
---|
| 2060 |
|
---|
| 2061 | if ucChannel != ucUser {
|
---|
| 2062 | return ircError{&irc.Message{
|
---|
| 2063 | Command: irc.ERR_USERNOTINCHANNEL,
|
---|
[401] | 2064 | Params: []string{dc.nick, user, channel, "They are on another network"},
|
---|
[163] | 2065 | }}
|
---|
| 2066 | }
|
---|
| 2067 | uc := ucChannel
|
---|
| 2068 |
|
---|
[176] | 2069 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[163] | 2070 | Command: "INVITE",
|
---|
| 2071 | Params: []string{upstreamUser, upstreamChannel},
|
---|
| 2072 | })
|
---|
[319] | 2073 | case "CHATHISTORY":
|
---|
| 2074 | var subcommand string
|
---|
| 2075 | if err := parseMessageParams(msg, &subcommand); err != nil {
|
---|
| 2076 | return err
|
---|
| 2077 | }
|
---|
[516] | 2078 | var target, limitStr string
|
---|
| 2079 | var boundsStr [2]string
|
---|
| 2080 | switch subcommand {
|
---|
| 2081 | case "AFTER", "BEFORE":
|
---|
| 2082 | if err := parseMessageParams(msg, nil, &target, &boundsStr[0], &limitStr); err != nil {
|
---|
| 2083 | return err
|
---|
| 2084 | }
|
---|
| 2085 | case "BETWEEN":
|
---|
| 2086 | if err := parseMessageParams(msg, nil, &target, &boundsStr[0], &boundsStr[1], &limitStr); err != nil {
|
---|
| 2087 | return err
|
---|
| 2088 | }
|
---|
[549] | 2089 | case "TARGETS":
|
---|
| 2090 | if err := parseMessageParams(msg, nil, &boundsStr[0], &boundsStr[1], &limitStr); err != nil {
|
---|
| 2091 | return err
|
---|
| 2092 | }
|
---|
[516] | 2093 | default:
|
---|
| 2094 | // TODO: support LATEST, AROUND
|
---|
[319] | 2095 | return ircError{&irc.Message{
|
---|
| 2096 | Command: "FAIL",
|
---|
[516] | 2097 | Params: []string{"CHATHISTORY", "INVALID_PARAMS", subcommand, "Unknown command"},
|
---|
[319] | 2098 | }}
|
---|
| 2099 | }
|
---|
| 2100 |
|
---|
[441] | 2101 | store, ok := dc.user.msgStore.(chatHistoryMessageStore)
|
---|
| 2102 | if !ok {
|
---|
[319] | 2103 | return ircError{&irc.Message{
|
---|
| 2104 | Command: irc.ERR_UNKNOWNCOMMAND,
|
---|
[456] | 2105 | Params: []string{dc.nick, "CHATHISTORY", "Unknown command"},
|
---|
[319] | 2106 | }}
|
---|
| 2107 | }
|
---|
| 2108 |
|
---|
| 2109 | uc, entity, err := dc.unmarshalEntity(target)
|
---|
| 2110 | if err != nil {
|
---|
| 2111 | return err
|
---|
| 2112 | }
|
---|
[479] | 2113 | entity = uc.network.casemap(entity)
|
---|
[319] | 2114 |
|
---|
| 2115 | // TODO: support msgid criteria
|
---|
[516] | 2116 | var bounds [2]time.Time
|
---|
| 2117 | bounds[0] = parseChatHistoryBound(boundsStr[0])
|
---|
| 2118 | if bounds[0].IsZero() {
|
---|
[319] | 2119 | return ircError{&irc.Message{
|
---|
| 2120 | Command: "FAIL",
|
---|
[516] | 2121 | Params: []string{"CHATHISTORY", "INVALID_PARAMS", subcommand, boundsStr[0], "Invalid first bound"},
|
---|
[319] | 2122 | }}
|
---|
| 2123 | }
|
---|
| 2124 |
|
---|
[516] | 2125 | if boundsStr[1] != "" {
|
---|
| 2126 | bounds[1] = parseChatHistoryBound(boundsStr[1])
|
---|
| 2127 | if bounds[1].IsZero() {
|
---|
| 2128 | return ircError{&irc.Message{
|
---|
| 2129 | Command: "FAIL",
|
---|
| 2130 | Params: []string{"CHATHISTORY", "INVALID_PARAMS", subcommand, boundsStr[1], "Invalid second bound"},
|
---|
| 2131 | }}
|
---|
| 2132 | }
|
---|
[319] | 2133 | }
|
---|
| 2134 |
|
---|
| 2135 | limit, err := strconv.Atoi(limitStr)
|
---|
| 2136 | if err != nil || limit < 0 || limit > dc.srv.HistoryLimit {
|
---|
| 2137 | return ircError{&irc.Message{
|
---|
| 2138 | Command: "FAIL",
|
---|
[456] | 2139 | Params: []string{"CHATHISTORY", "INVALID_PARAMS", subcommand, limitStr, "Invalid limit"},
|
---|
[319] | 2140 | }}
|
---|
| 2141 | }
|
---|
| 2142 |
|
---|
[387] | 2143 | var history []*irc.Message
|
---|
[319] | 2144 | switch subcommand {
|
---|
| 2145 | case "BEFORE":
|
---|
[516] | 2146 | history, err = store.LoadBeforeTime(uc.network, entity, bounds[0], time.Time{}, limit)
|
---|
[360] | 2147 | case "AFTER":
|
---|
[516] | 2148 | history, err = store.LoadAfterTime(uc.network, entity, bounds[0], time.Now(), limit)
|
---|
| 2149 | case "BETWEEN":
|
---|
| 2150 | if bounds[0].Before(bounds[1]) {
|
---|
| 2151 | history, err = store.LoadAfterTime(uc.network, entity, bounds[0], bounds[1], limit)
|
---|
| 2152 | } else {
|
---|
| 2153 | history, err = store.LoadBeforeTime(uc.network, entity, bounds[0], bounds[1], limit)
|
---|
| 2154 | }
|
---|
[549] | 2155 | case "TARGETS":
|
---|
| 2156 | // TODO: support TARGETS in multi-upstream mode
|
---|
| 2157 | targets, err := store.ListTargets(uc.network, bounds[0], bounds[1], limit)
|
---|
| 2158 | if err != nil {
|
---|
| 2159 | dc.logger.Printf("failed fetching targets for chathistory: %v", target, err)
|
---|
| 2160 | return ircError{&irc.Message{
|
---|
| 2161 | Command: "FAIL",
|
---|
| 2162 | Params: []string{"CHATHISTORY", "MESSAGE_ERROR", subcommand, "Failed to retrieve targets"},
|
---|
| 2163 | }}
|
---|
| 2164 | }
|
---|
| 2165 |
|
---|
[551] | 2166 | dc.SendBatch("draft/chathistory-targets", nil, nil, func(batchRef irc.TagValue) {
|
---|
| 2167 | for _, target := range targets {
|
---|
| 2168 | if ch := uc.network.channels.Value(target.Name); ch != nil && ch.Detached {
|
---|
| 2169 | continue
|
---|
| 2170 | }
|
---|
[549] | 2171 |
|
---|
[551] | 2172 | dc.SendMessage(&irc.Message{
|
---|
| 2173 | Tags: irc.Tags{"batch": batchRef},
|
---|
| 2174 | Prefix: dc.srv.prefix(),
|
---|
| 2175 | Command: "CHATHISTORY",
|
---|
| 2176 | Params: []string{"TARGETS", target.Name, target.LatestMessage.UTC().Format(serverTimeLayout)},
|
---|
| 2177 | })
|
---|
[550] | 2178 | }
|
---|
[549] | 2179 | })
|
---|
| 2180 |
|
---|
| 2181 | return nil
|
---|
[319] | 2182 | }
|
---|
[387] | 2183 | if err != nil {
|
---|
[515] | 2184 | dc.logger.Printf("failed fetching %q messages for chathistory: %v", target, err)
|
---|
[387] | 2185 | return newChatHistoryError(subcommand, target)
|
---|
| 2186 | }
|
---|
| 2187 |
|
---|
[551] | 2188 | dc.SendBatch("chathistory", []string{target}, nil, func(batchRef irc.TagValue) {
|
---|
| 2189 | for _, msg := range history {
|
---|
| 2190 | msg.Tags["batch"] = batchRef
|
---|
| 2191 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
| 2192 | }
|
---|
[387] | 2193 | })
|
---|
[532] | 2194 | case "BOUNCER":
|
---|
| 2195 | var subcommand string
|
---|
| 2196 | if err := parseMessageParams(msg, &subcommand); err != nil {
|
---|
| 2197 | return err
|
---|
| 2198 | }
|
---|
| 2199 |
|
---|
| 2200 | switch strings.ToUpper(subcommand) {
|
---|
| 2201 | case "LISTNETWORKS":
|
---|
[551] | 2202 | dc.SendBatch("soju.im/bouncer-networks", nil, nil, func(batchRef irc.TagValue) {
|
---|
| 2203 | dc.user.forEachNetwork(func(network *network) {
|
---|
| 2204 | idStr := fmt.Sprintf("%v", network.ID)
|
---|
| 2205 | attrs := getNetworkAttrs(network)
|
---|
| 2206 | dc.SendMessage(&irc.Message{
|
---|
| 2207 | Tags: irc.Tags{"batch": batchRef},
|
---|
| 2208 | Prefix: dc.srv.prefix(),
|
---|
| 2209 | Command: "BOUNCER",
|
---|
| 2210 | Params: []string{"NETWORK", idStr, attrs.String()},
|
---|
| 2211 | })
|
---|
[532] | 2212 | })
|
---|
| 2213 | })
|
---|
| 2214 | case "ADDNETWORK":
|
---|
| 2215 | var attrsStr string
|
---|
| 2216 | if err := parseMessageParams(msg, nil, &attrsStr); err != nil {
|
---|
| 2217 | return err
|
---|
| 2218 | }
|
---|
| 2219 | attrs := irc.ParseTags(attrsStr)
|
---|
| 2220 |
|
---|
| 2221 | host, ok := attrs.GetTag("host")
|
---|
| 2222 | if !ok {
|
---|
| 2223 | return ircError{&irc.Message{
|
---|
| 2224 | Command: "FAIL",
|
---|
| 2225 | Params: []string{"BOUNCER", "NEED_ATTRIBUTE", subcommand, "host", "Missing required host attribute"},
|
---|
| 2226 | }}
|
---|
| 2227 | }
|
---|
| 2228 |
|
---|
| 2229 | addr := host
|
---|
| 2230 | if port, ok := attrs.GetTag("port"); ok {
|
---|
| 2231 | addr += ":" + port
|
---|
| 2232 | }
|
---|
| 2233 |
|
---|
| 2234 | if tlsStr, ok := attrs.GetTag("tls"); ok && tlsStr == "0" {
|
---|
| 2235 | addr = "irc+insecure://" + tlsStr
|
---|
| 2236 | }
|
---|
| 2237 |
|
---|
| 2238 | nick, ok := attrs.GetTag("nickname")
|
---|
| 2239 | if !ok {
|
---|
| 2240 | nick = dc.nick
|
---|
| 2241 | }
|
---|
| 2242 |
|
---|
| 2243 | username, _ := attrs.GetTag("username")
|
---|
| 2244 | realname, _ := attrs.GetTag("realname")
|
---|
[533] | 2245 | pass, _ := attrs.GetTag("pass")
|
---|
[532] | 2246 |
|
---|
[568] | 2247 | if realname == dc.user.Realname {
|
---|
| 2248 | realname = ""
|
---|
| 2249 | }
|
---|
| 2250 |
|
---|
[532] | 2251 | // TODO: reject unknown attributes
|
---|
| 2252 |
|
---|
| 2253 | record := &Network{
|
---|
| 2254 | Addr: addr,
|
---|
| 2255 | Nick: nick,
|
---|
| 2256 | Username: username,
|
---|
| 2257 | Realname: realname,
|
---|
[533] | 2258 | Pass: pass,
|
---|
[542] | 2259 | Enabled: true,
|
---|
[532] | 2260 | }
|
---|
| 2261 | network, err := dc.user.createNetwork(record)
|
---|
| 2262 | if err != nil {
|
---|
| 2263 | return ircError{&irc.Message{
|
---|
| 2264 | Command: "FAIL",
|
---|
| 2265 | Params: []string{"BOUNCER", "UNKNOWN_ERROR", subcommand, fmt.Sprintf("Failed to create network: %v", err)},
|
---|
| 2266 | }}
|
---|
| 2267 | }
|
---|
| 2268 |
|
---|
| 2269 | dc.SendMessage(&irc.Message{
|
---|
| 2270 | Prefix: dc.srv.prefix(),
|
---|
| 2271 | Command: "BOUNCER",
|
---|
| 2272 | Params: []string{"ADDNETWORK", fmt.Sprintf("%v", network.ID)},
|
---|
| 2273 | })
|
---|
| 2274 | case "CHANGENETWORK":
|
---|
| 2275 | var idStr, attrsStr string
|
---|
| 2276 | if err := parseMessageParams(msg, nil, &idStr, &attrsStr); err != nil {
|
---|
| 2277 | return err
|
---|
| 2278 | }
|
---|
[535] | 2279 | id, err := parseBouncerNetID(subcommand, idStr)
|
---|
[532] | 2280 | if err != nil {
|
---|
| 2281 | return err
|
---|
| 2282 | }
|
---|
| 2283 | attrs := irc.ParseTags(attrsStr)
|
---|
| 2284 |
|
---|
| 2285 | net := dc.user.getNetworkByID(id)
|
---|
| 2286 | if net == nil {
|
---|
| 2287 | return ircError{&irc.Message{
|
---|
| 2288 | Command: "FAIL",
|
---|
[535] | 2289 | Params: []string{"BOUNCER", "INVALID_NETID", subcommand, idStr, "Invalid network ID"},
|
---|
[532] | 2290 | }}
|
---|
| 2291 | }
|
---|
| 2292 |
|
---|
| 2293 | record := net.Network // copy network record because we'll mutate it
|
---|
| 2294 | for k, v := range attrs {
|
---|
| 2295 | s := string(v)
|
---|
| 2296 | switch k {
|
---|
| 2297 | // TODO: host, port, tls
|
---|
| 2298 | case "nickname":
|
---|
| 2299 | record.Nick = s
|
---|
| 2300 | case "username":
|
---|
| 2301 | record.Username = s
|
---|
| 2302 | case "realname":
|
---|
| 2303 | record.Realname = s
|
---|
[533] | 2304 | case "pass":
|
---|
| 2305 | record.Pass = s
|
---|
[532] | 2306 | default:
|
---|
| 2307 | return ircError{&irc.Message{
|
---|
| 2308 | Command: "FAIL",
|
---|
| 2309 | Params: []string{"BOUNCER", "UNKNOWN_ATTRIBUTE", subcommand, k, "Unknown attribute"},
|
---|
| 2310 | }}
|
---|
| 2311 | }
|
---|
| 2312 | }
|
---|
| 2313 |
|
---|
| 2314 | _, err = dc.user.updateNetwork(&record)
|
---|
| 2315 | if err != nil {
|
---|
| 2316 | return ircError{&irc.Message{
|
---|
| 2317 | Command: "FAIL",
|
---|
| 2318 | Params: []string{"BOUNCER", "UNKNOWN_ERROR", subcommand, fmt.Sprintf("Failed to update network: %v", err)},
|
---|
| 2319 | }}
|
---|
| 2320 | }
|
---|
| 2321 |
|
---|
| 2322 | dc.SendMessage(&irc.Message{
|
---|
| 2323 | Prefix: dc.srv.prefix(),
|
---|
| 2324 | Command: "BOUNCER",
|
---|
| 2325 | Params: []string{"CHANGENETWORK", idStr},
|
---|
| 2326 | })
|
---|
| 2327 | case "DELNETWORK":
|
---|
| 2328 | var idStr string
|
---|
| 2329 | if err := parseMessageParams(msg, nil, &idStr); err != nil {
|
---|
| 2330 | return err
|
---|
| 2331 | }
|
---|
[535] | 2332 | id, err := parseBouncerNetID(subcommand, idStr)
|
---|
[532] | 2333 | if err != nil {
|
---|
| 2334 | return err
|
---|
| 2335 | }
|
---|
| 2336 |
|
---|
| 2337 | net := dc.user.getNetworkByID(id)
|
---|
| 2338 | if net == nil {
|
---|
| 2339 | return ircError{&irc.Message{
|
---|
| 2340 | Command: "FAIL",
|
---|
[535] | 2341 | Params: []string{"BOUNCER", "INVALID_NETID", subcommand, idStr, "Invalid network ID"},
|
---|
[532] | 2342 | }}
|
---|
| 2343 | }
|
---|
| 2344 |
|
---|
| 2345 | if err := dc.user.deleteNetwork(net.ID); err != nil {
|
---|
| 2346 | return err
|
---|
| 2347 | }
|
---|
| 2348 |
|
---|
| 2349 | dc.SendMessage(&irc.Message{
|
---|
| 2350 | Prefix: dc.srv.prefix(),
|
---|
| 2351 | Command: "BOUNCER",
|
---|
| 2352 | Params: []string{"DELNETWORK", idStr},
|
---|
| 2353 | })
|
---|
| 2354 | default:
|
---|
| 2355 | return ircError{&irc.Message{
|
---|
| 2356 | Command: "FAIL",
|
---|
| 2357 | Params: []string{"BOUNCER", "UNKNOWN_COMMAND", subcommand, "Unknown subcommand"},
|
---|
| 2358 | }}
|
---|
| 2359 | }
|
---|
[13] | 2360 | default:
|
---|
[55] | 2361 | dc.logger.Printf("unhandled message: %v", msg)
|
---|
[547] | 2362 |
|
---|
| 2363 | // Only forward unknown commands in single-upstream mode
|
---|
| 2364 | uc := dc.upstream()
|
---|
| 2365 | if uc == nil {
|
---|
| 2366 | return newUnknownCommandError(msg.Command)
|
---|
| 2367 | }
|
---|
| 2368 |
|
---|
| 2369 | uc.SendMessageLabeled(dc.id, msg)
|
---|
[13] | 2370 | }
|
---|
[42] | 2371 | return nil
|
---|
[13] | 2372 | }
|
---|
[95] | 2373 |
|
---|
| 2374 | func (dc *downstreamConn) handleNickServPRIVMSG(uc *upstreamConn, text string) {
|
---|
| 2375 | username, password, ok := parseNickServCredentials(text, uc.nick)
|
---|
| 2376 | if !ok {
|
---|
| 2377 | return
|
---|
| 2378 | }
|
---|
| 2379 |
|
---|
[307] | 2380 | // User may have e.g. EXTERNAL mechanism configured. We do not want to
|
---|
| 2381 | // automatically erase the key pair or any other credentials.
|
---|
| 2382 | if uc.network.SASL.Mechanism != "" && uc.network.SASL.Mechanism != "PLAIN" {
|
---|
| 2383 | return
|
---|
| 2384 | }
|
---|
| 2385 |
|
---|
[95] | 2386 | dc.logger.Printf("auto-saving NickServ credentials with username %q", username)
|
---|
| 2387 | n := uc.network
|
---|
| 2388 | n.SASL.Mechanism = "PLAIN"
|
---|
| 2389 | n.SASL.Plain.Username = username
|
---|
| 2390 | n.SASL.Plain.Password = password
|
---|
[421] | 2391 | if err := dc.srv.db.StoreNetwork(dc.user.ID, &n.Network); err != nil {
|
---|
[95] | 2392 | dc.logger.Printf("failed to save NickServ credentials: %v", err)
|
---|
| 2393 | }
|
---|
| 2394 | }
|
---|
| 2395 |
|
---|
| 2396 | func parseNickServCredentials(text, nick string) (username, password string, ok bool) {
|
---|
| 2397 | fields := strings.Fields(text)
|
---|
| 2398 | if len(fields) < 2 {
|
---|
| 2399 | return "", "", false
|
---|
| 2400 | }
|
---|
| 2401 | cmd := strings.ToUpper(fields[0])
|
---|
| 2402 | params := fields[1:]
|
---|
| 2403 | switch cmd {
|
---|
| 2404 | case "REGISTER":
|
---|
| 2405 | username = nick
|
---|
| 2406 | password = params[0]
|
---|
| 2407 | case "IDENTIFY":
|
---|
| 2408 | if len(params) == 1 {
|
---|
| 2409 | username = nick
|
---|
[182] | 2410 | password = params[0]
|
---|
[95] | 2411 | } else {
|
---|
| 2412 | username = params[0]
|
---|
[182] | 2413 | password = params[1]
|
---|
[95] | 2414 | }
|
---|
[182] | 2415 | case "SET":
|
---|
| 2416 | if len(params) == 2 && strings.EqualFold(params[0], "PASSWORD") {
|
---|
| 2417 | username = nick
|
---|
| 2418 | password = params[1]
|
---|
| 2419 | }
|
---|
[340] | 2420 | default:
|
---|
| 2421 | return "", "", false
|
---|
[95] | 2422 | }
|
---|
| 2423 | return username, password, true
|
---|
| 2424 | }
|
---|