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