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