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