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