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