[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 |
|
---|
[85] | 48 | var errAuthFailed = ircError{&irc.Message{
|
---|
| 49 | Command: irc.ERR_PASSWDMISMATCH,
|
---|
| 50 | Params: []string{"*", "Invalid username or password"},
|
---|
| 51 | }}
|
---|
[13] | 52 |
|
---|
[275] | 53 | // permanentDownstreamCaps is the list of always-supported downstream
|
---|
| 54 | // capabilities.
|
---|
| 55 | var permanentDownstreamCaps = map[string]string{
|
---|
[276] | 56 | "batch": "",
|
---|
| 57 | "cap-notify": "",
|
---|
[275] | 58 | "echo-message": "",
|
---|
| 59 | "message-tags": "",
|
---|
[276] | 60 | "sasl": "PLAIN",
|
---|
| 61 | "server-time": "",
|
---|
[275] | 62 | }
|
---|
| 63 |
|
---|
[292] | 64 | // needAllDownstreamCaps is the list of downstream capabilities that
|
---|
| 65 | // require support from all upstreams to be enabled
|
---|
| 66 | var needAllDownstreamCaps = map[string]string{
|
---|
| 67 | "away-notify": "",
|
---|
| 68 | "multi-prefix": "",
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[13] | 71 | type downstreamConn struct {
|
---|
[210] | 72 | conn
|
---|
[22] | 73 |
|
---|
[210] | 74 | id uint64
|
---|
| 75 |
|
---|
[100] | 76 | registered bool
|
---|
| 77 | user *user
|
---|
| 78 | nick string
|
---|
| 79 | rawUsername string
|
---|
[168] | 80 | networkName string
|
---|
[183] | 81 | clientName string
|
---|
[100] | 82 | realname string
|
---|
[141] | 83 | hostname string
|
---|
[100] | 84 | password string // empty after authentication
|
---|
| 85 | network *network // can be nil
|
---|
[105] | 86 |
|
---|
[108] | 87 | negociatingCaps bool
|
---|
| 88 | capVersion int
|
---|
[275] | 89 | supportedCaps map[string]string
|
---|
[236] | 90 | caps map[string]bool
|
---|
[108] | 91 |
|
---|
[112] | 92 | saslServer sasl.Server
|
---|
[13] | 93 | }
|
---|
| 94 |
|
---|
[154] | 95 | func newDownstreamConn(srv *Server, netConn net.Conn, id uint64) *downstreamConn {
|
---|
[210] | 96 | logger := &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", netConn.RemoteAddr())}
|
---|
[55] | 97 | dc := &downstreamConn{
|
---|
[276] | 98 | conn: *newConn(srv, netConn, logger),
|
---|
| 99 | id: id,
|
---|
[275] | 100 | supportedCaps: make(map[string]string),
|
---|
[276] | 101 | caps: make(map[string]bool),
|
---|
[22] | 102 | }
|
---|
[141] | 103 | dc.hostname = netConn.RemoteAddr().String()
|
---|
| 104 | if host, _, err := net.SplitHostPort(dc.hostname); err == nil {
|
---|
| 105 | dc.hostname = host
|
---|
| 106 | }
|
---|
[275] | 107 | for k, v := range permanentDownstreamCaps {
|
---|
| 108 | dc.supportedCaps[k] = v
|
---|
| 109 | }
|
---|
[55] | 110 | return dc
|
---|
[22] | 111 | }
|
---|
| 112 |
|
---|
[55] | 113 | func (dc *downstreamConn) prefix() *irc.Prefix {
|
---|
[27] | 114 | return &irc.Prefix{
|
---|
[55] | 115 | Name: dc.nick,
|
---|
[184] | 116 | User: dc.user.Username,
|
---|
[141] | 117 | Host: dc.hostname,
|
---|
[27] | 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[90] | 121 | func (dc *downstreamConn) forEachNetwork(f func(*network)) {
|
---|
| 122 | if dc.network != nil {
|
---|
| 123 | f(dc.network)
|
---|
| 124 | } else {
|
---|
| 125 | dc.user.forEachNetwork(f)
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[73] | 129 | func (dc *downstreamConn) forEachUpstream(f func(*upstreamConn)) {
|
---|
| 130 | dc.user.forEachUpstream(func(uc *upstreamConn) {
|
---|
[77] | 131 | if dc.network != nil && uc.network != dc.network {
|
---|
[73] | 132 | return
|
---|
| 133 | }
|
---|
| 134 | f(uc)
|
---|
| 135 | })
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[89] | 138 | // upstream returns the upstream connection, if any. If there are zero or if
|
---|
| 139 | // there are multiple upstream connections, it returns nil.
|
---|
| 140 | func (dc *downstreamConn) upstream() *upstreamConn {
|
---|
| 141 | if dc.network == nil {
|
---|
| 142 | return nil
|
---|
| 143 | }
|
---|
[279] | 144 | return dc.network.conn
|
---|
[89] | 145 | }
|
---|
| 146 |
|
---|
[260] | 147 | func isOurNick(net *network, nick string) bool {
|
---|
| 148 | // TODO: this doesn't account for nick changes
|
---|
| 149 | if net.conn != nil {
|
---|
| 150 | return nick == net.conn.nick
|
---|
| 151 | }
|
---|
| 152 | // We're not currently connected to the upstream connection, so we don't
|
---|
| 153 | // know whether this name is our nickname. Best-effort: use the network's
|
---|
| 154 | // configured nickname and hope it was the one being used when we were
|
---|
| 155 | // connected.
|
---|
| 156 | return nick == net.Nick
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[249] | 159 | // marshalEntity converts an upstream entity name (ie. channel or nick) into a
|
---|
| 160 | // downstream entity name.
|
---|
| 161 | //
|
---|
| 162 | // This involves adding a "/<network>" suffix if the entity isn't the current
|
---|
| 163 | // user.
|
---|
[260] | 164 | func (dc *downstreamConn) marshalEntity(net *network, name string) string {
|
---|
[289] | 165 | if isOurNick(net, name) {
|
---|
| 166 | return dc.nick
|
---|
| 167 | }
|
---|
[257] | 168 | if dc.network != nil {
|
---|
[260] | 169 | if dc.network != net {
|
---|
[258] | 170 | panic("soju: tried to marshal an entity for another network")
|
---|
| 171 | }
|
---|
[257] | 172 | return name
|
---|
[119] | 173 | }
|
---|
[260] | 174 | return name + "/" + net.GetName()
|
---|
[119] | 175 | }
|
---|
| 176 |
|
---|
[260] | 177 | func (dc *downstreamConn) marshalUserPrefix(net *network, prefix *irc.Prefix) *irc.Prefix {
|
---|
| 178 | if isOurNick(net, prefix.Name) {
|
---|
[257] | 179 | return dc.prefix()
|
---|
| 180 | }
|
---|
[130] | 181 | if dc.network != nil {
|
---|
[260] | 182 | if dc.network != net {
|
---|
[258] | 183 | panic("soju: tried to marshal a user prefix for another network")
|
---|
| 184 | }
|
---|
[257] | 185 | return prefix
|
---|
[119] | 186 | }
|
---|
[257] | 187 | return &irc.Prefix{
|
---|
[260] | 188 | Name: prefix.Name + "/" + net.GetName(),
|
---|
[257] | 189 | User: prefix.User,
|
---|
| 190 | Host: prefix.Host,
|
---|
| 191 | }
|
---|
[119] | 192 | }
|
---|
| 193 |
|
---|
[249] | 194 | // unmarshalEntity converts a downstream entity name (ie. channel or nick) into
|
---|
| 195 | // an upstream entity name.
|
---|
| 196 | //
|
---|
| 197 | // This involves removing the "/<network>" suffix.
|
---|
[127] | 198 | func (dc *downstreamConn) unmarshalEntity(name string) (*upstreamConn, string, error) {
|
---|
[89] | 199 | if uc := dc.upstream(); uc != nil {
|
---|
| 200 | return uc, name, nil
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[127] | 203 | var conn *upstreamConn
|
---|
[119] | 204 | if i := strings.LastIndexByte(name, '/'); i >= 0 {
|
---|
[127] | 205 | network := name[i+1:]
|
---|
[119] | 206 | name = name[:i]
|
---|
| 207 |
|
---|
| 208 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
| 209 | if network != uc.network.GetName() {
|
---|
| 210 | return
|
---|
| 211 | }
|
---|
| 212 | conn = uc
|
---|
| 213 | })
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[127] | 216 | if conn == nil {
|
---|
[73] | 217 | return nil, "", ircError{&irc.Message{
|
---|
| 218 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
| 219 | Params: []string{name, "No such channel"},
|
---|
| 220 | }}
|
---|
[69] | 221 | }
|
---|
[127] | 222 | return conn, name, nil
|
---|
[69] | 223 | }
|
---|
| 224 |
|
---|
[268] | 225 | func (dc *downstreamConn) unmarshalText(uc *upstreamConn, text string) string {
|
---|
| 226 | if dc.upstream() != nil {
|
---|
| 227 | return text
|
---|
| 228 | }
|
---|
| 229 | // TODO: smarter parsing that ignores URLs
|
---|
| 230 | return strings.ReplaceAll(text, "/"+uc.network.GetName(), "")
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[165] | 233 | func (dc *downstreamConn) readMessages(ch chan<- event) error {
|
---|
[22] | 234 | for {
|
---|
[210] | 235 | msg, err := dc.ReadMessage()
|
---|
[22] | 236 | if err == io.EOF {
|
---|
| 237 | break
|
---|
| 238 | } else if err != nil {
|
---|
| 239 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[165] | 242 | ch <- eventDownstreamMessage{msg, dc}
|
---|
[22] | 243 | }
|
---|
| 244 |
|
---|
[45] | 245 | return nil
|
---|
[22] | 246 | }
|
---|
| 247 |
|
---|
[230] | 248 | // SendMessage sends an outgoing message.
|
---|
| 249 | //
|
---|
| 250 | // This can only called from the user goroutine.
|
---|
[55] | 251 | func (dc *downstreamConn) SendMessage(msg *irc.Message) {
|
---|
[230] | 252 | if !dc.caps["message-tags"] {
|
---|
[303] | 253 | if msg.Command == "TAGMSG" {
|
---|
| 254 | return
|
---|
| 255 | }
|
---|
[216] | 256 | msg = msg.Copy()
|
---|
| 257 | for name := range msg.Tags {
|
---|
| 258 | supported := false
|
---|
| 259 | switch name {
|
---|
| 260 | case "time":
|
---|
[230] | 261 | supported = dc.caps["server-time"]
|
---|
[216] | 262 | }
|
---|
| 263 | if !supported {
|
---|
| 264 | delete(msg.Tags, name)
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[210] | 269 | dc.conn.SendMessage(msg)
|
---|
[54] | 270 | }
|
---|
| 271 |
|
---|
[245] | 272 | // marshalMessage re-formats a message coming from an upstream connection so
|
---|
| 273 | // that it's suitable for being sent on this downstream connection. Only
|
---|
[293] | 274 | // messages that may appear in logs are supported, except MODE.
|
---|
[261] | 275 | func (dc *downstreamConn) marshalMessage(msg *irc.Message, net *network) *irc.Message {
|
---|
[227] | 276 | msg = msg.Copy()
|
---|
[261] | 277 | msg.Prefix = dc.marshalUserPrefix(net, msg.Prefix)
|
---|
[245] | 278 |
|
---|
[227] | 279 | switch msg.Command {
|
---|
[303] | 280 | case "PRIVMSG", "NOTICE", "TAGMSG":
|
---|
[261] | 281 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
[245] | 282 | case "NICK":
|
---|
| 283 | // Nick change for another user
|
---|
[261] | 284 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
[245] | 285 | case "JOIN", "PART":
|
---|
[261] | 286 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
[245] | 287 | case "KICK":
|
---|
[261] | 288 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
| 289 | msg.Params[1] = dc.marshalEntity(net, msg.Params[1])
|
---|
[245] | 290 | case "TOPIC":
|
---|
[261] | 291 | msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
---|
[245] | 292 | case "QUIT":
|
---|
[262] | 293 | // This space is intentionally left blank
|
---|
[227] | 294 | default:
|
---|
| 295 | panic(fmt.Sprintf("unexpected %q message", msg.Command))
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[245] | 298 | return msg
|
---|
[227] | 299 | }
|
---|
| 300 |
|
---|
[55] | 301 | func (dc *downstreamConn) handleMessage(msg *irc.Message) error {
|
---|
[13] | 302 | switch msg.Command {
|
---|
[28] | 303 | case "QUIT":
|
---|
[55] | 304 | return dc.Close()
|
---|
[13] | 305 | default:
|
---|
[55] | 306 | if dc.registered {
|
---|
| 307 | return dc.handleMessageRegistered(msg)
|
---|
[13] | 308 | } else {
|
---|
[55] | 309 | return dc.handleMessageUnregistered(msg)
|
---|
[13] | 310 | }
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[55] | 314 | func (dc *downstreamConn) handleMessageUnregistered(msg *irc.Message) error {
|
---|
[13] | 315 | switch msg.Command {
|
---|
| 316 | case "NICK":
|
---|
[117] | 317 | var nick string
|
---|
| 318 | if err := parseMessageParams(msg, &nick); err != nil {
|
---|
[43] | 319 | return err
|
---|
[13] | 320 | }
|
---|
[117] | 321 | if nick == serviceNick {
|
---|
| 322 | return ircError{&irc.Message{
|
---|
| 323 | Command: irc.ERR_NICKNAMEINUSE,
|
---|
| 324 | Params: []string{dc.nick, nick, "Nickname reserved for bouncer service"},
|
---|
| 325 | }}
|
---|
| 326 | }
|
---|
| 327 | dc.nick = nick
|
---|
[13] | 328 | case "USER":
|
---|
[117] | 329 | if err := parseMessageParams(msg, &dc.rawUsername, nil, nil, &dc.realname); err != nil {
|
---|
[43] | 330 | return err
|
---|
[13] | 331 | }
|
---|
[85] | 332 | case "PASS":
|
---|
| 333 | if err := parseMessageParams(msg, &dc.password); err != nil {
|
---|
| 334 | return err
|
---|
| 335 | }
|
---|
[108] | 336 | case "CAP":
|
---|
| 337 | var subCmd string
|
---|
| 338 | if err := parseMessageParams(msg, &subCmd); err != nil {
|
---|
| 339 | return err
|
---|
| 340 | }
|
---|
| 341 | if err := dc.handleCapCommand(subCmd, msg.Params[1:]); err != nil {
|
---|
| 342 | return err
|
---|
| 343 | }
|
---|
[112] | 344 | case "AUTHENTICATE":
|
---|
[230] | 345 | if !dc.caps["sasl"] {
|
---|
[112] | 346 | return ircError{&irc.Message{
|
---|
[125] | 347 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 348 | Params: []string{"*", "AUTHENTICATE requires the \"sasl\" capability to be enabled"},
|
---|
| 349 | }}
|
---|
| 350 | }
|
---|
| 351 | if len(msg.Params) == 0 {
|
---|
| 352 | return ircError{&irc.Message{
|
---|
[125] | 353 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 354 | Params: []string{"*", "Missing AUTHENTICATE argument"},
|
---|
| 355 | }}
|
---|
| 356 | }
|
---|
| 357 | if dc.nick == "" {
|
---|
| 358 | return ircError{&irc.Message{
|
---|
[125] | 359 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 360 | Params: []string{"*", "Expected NICK command before AUTHENTICATE"},
|
---|
| 361 | }}
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | var resp []byte
|
---|
| 365 | if dc.saslServer == nil {
|
---|
| 366 | mech := strings.ToUpper(msg.Params[0])
|
---|
| 367 | switch mech {
|
---|
| 368 | case "PLAIN":
|
---|
| 369 | dc.saslServer = sasl.NewPlainServer(sasl.PlainAuthenticator(func(identity, username, password string) error {
|
---|
| 370 | return dc.authenticate(username, password)
|
---|
| 371 | }))
|
---|
| 372 | default:
|
---|
| 373 | return ircError{&irc.Message{
|
---|
[125] | 374 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 375 | Params: []string{"*", fmt.Sprintf("Unsupported SASL mechanism %q", mech)},
|
---|
| 376 | }}
|
---|
| 377 | }
|
---|
| 378 | } else if msg.Params[0] == "*" {
|
---|
| 379 | dc.saslServer = nil
|
---|
| 380 | return ircError{&irc.Message{
|
---|
[125] | 381 | Command: irc.ERR_SASLABORTED,
|
---|
[112] | 382 | Params: []string{"*", "SASL authentication aborted"},
|
---|
| 383 | }}
|
---|
| 384 | } else if msg.Params[0] == "+" {
|
---|
| 385 | resp = nil
|
---|
| 386 | } else {
|
---|
| 387 | // TODO: multi-line messages
|
---|
| 388 | var err error
|
---|
| 389 | resp, err = base64.StdEncoding.DecodeString(msg.Params[0])
|
---|
| 390 | if err != nil {
|
---|
| 391 | dc.saslServer = nil
|
---|
| 392 | return ircError{&irc.Message{
|
---|
[125] | 393 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 394 | Params: []string{"*", "Invalid base64-encoded response"},
|
---|
| 395 | }}
|
---|
| 396 | }
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | challenge, done, err := dc.saslServer.Next(resp)
|
---|
| 400 | if err != nil {
|
---|
| 401 | dc.saslServer = nil
|
---|
| 402 | if ircErr, ok := err.(ircError); ok && ircErr.Message.Command == irc.ERR_PASSWDMISMATCH {
|
---|
| 403 | return ircError{&irc.Message{
|
---|
[125] | 404 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 405 | Params: []string{"*", ircErr.Message.Params[1]},
|
---|
| 406 | }}
|
---|
| 407 | }
|
---|
| 408 | dc.SendMessage(&irc.Message{
|
---|
| 409 | Prefix: dc.srv.prefix(),
|
---|
[125] | 410 | Command: irc.ERR_SASLFAIL,
|
---|
[112] | 411 | Params: []string{"*", "SASL error"},
|
---|
| 412 | })
|
---|
| 413 | return fmt.Errorf("SASL authentication failed: %v", err)
|
---|
| 414 | } else if done {
|
---|
| 415 | dc.saslServer = nil
|
---|
| 416 | dc.SendMessage(&irc.Message{
|
---|
| 417 | Prefix: dc.srv.prefix(),
|
---|
[125] | 418 | Command: irc.RPL_LOGGEDIN,
|
---|
[306] | 419 | Params: []string{dc.nick, dc.prefix().String(), dc.user.Username, "You are now logged in"},
|
---|
[112] | 420 | })
|
---|
| 421 | dc.SendMessage(&irc.Message{
|
---|
| 422 | Prefix: dc.srv.prefix(),
|
---|
[125] | 423 | Command: irc.RPL_SASLSUCCESS,
|
---|
[112] | 424 | Params: []string{dc.nick, "SASL authentication successful"},
|
---|
| 425 | })
|
---|
| 426 | } else {
|
---|
| 427 | challengeStr := "+"
|
---|
[135] | 428 | if len(challenge) > 0 {
|
---|
[112] | 429 | challengeStr = base64.StdEncoding.EncodeToString(challenge)
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | // TODO: multi-line messages
|
---|
| 433 | dc.SendMessage(&irc.Message{
|
---|
| 434 | Prefix: dc.srv.prefix(),
|
---|
| 435 | Command: "AUTHENTICATE",
|
---|
| 436 | Params: []string{challengeStr},
|
---|
| 437 | })
|
---|
| 438 | }
|
---|
[13] | 439 | default:
|
---|
[55] | 440 | dc.logger.Printf("unhandled message: %v", msg)
|
---|
[13] | 441 | return newUnknownCommandError(msg.Command)
|
---|
| 442 | }
|
---|
[108] | 443 | if dc.rawUsername != "" && dc.nick != "" && !dc.negociatingCaps {
|
---|
[55] | 444 | return dc.register()
|
---|
[13] | 445 | }
|
---|
| 446 | return nil
|
---|
| 447 | }
|
---|
| 448 |
|
---|
[108] | 449 | func (dc *downstreamConn) handleCapCommand(cmd string, args []string) error {
|
---|
[111] | 450 | cmd = strings.ToUpper(cmd)
|
---|
| 451 |
|
---|
[108] | 452 | replyTo := dc.nick
|
---|
| 453 | if !dc.registered {
|
---|
| 454 | replyTo = "*"
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | switch cmd {
|
---|
| 458 | case "LS":
|
---|
| 459 | if len(args) > 0 {
|
---|
| 460 | var err error
|
---|
| 461 | if dc.capVersion, err = strconv.Atoi(args[0]); err != nil {
|
---|
| 462 | return err
|
---|
| 463 | }
|
---|
| 464 | }
|
---|
| 465 |
|
---|
[275] | 466 | caps := make([]string, 0, len(dc.supportedCaps))
|
---|
| 467 | for k, v := range dc.supportedCaps {
|
---|
| 468 | if dc.capVersion >= 302 && v != "" {
|
---|
[276] | 469 | caps = append(caps, k+"="+v)
|
---|
[275] | 470 | } else {
|
---|
| 471 | caps = append(caps, k)
|
---|
| 472 | }
|
---|
[112] | 473 | }
|
---|
[108] | 474 |
|
---|
| 475 | // TODO: multi-line replies
|
---|
| 476 | dc.SendMessage(&irc.Message{
|
---|
| 477 | Prefix: dc.srv.prefix(),
|
---|
| 478 | Command: "CAP",
|
---|
| 479 | Params: []string{replyTo, "LS", strings.Join(caps, " ")},
|
---|
| 480 | })
|
---|
| 481 |
|
---|
[275] | 482 | if dc.capVersion >= 302 {
|
---|
| 483 | // CAP version 302 implicitly enables cap-notify
|
---|
| 484 | dc.caps["cap-notify"] = true
|
---|
| 485 | }
|
---|
| 486 |
|
---|
[108] | 487 | if !dc.registered {
|
---|
| 488 | dc.negociatingCaps = true
|
---|
| 489 | }
|
---|
| 490 | case "LIST":
|
---|
| 491 | var caps []string
|
---|
| 492 | for name := range dc.caps {
|
---|
| 493 | caps = append(caps, name)
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | // TODO: multi-line replies
|
---|
| 497 | dc.SendMessage(&irc.Message{
|
---|
| 498 | Prefix: dc.srv.prefix(),
|
---|
| 499 | Command: "CAP",
|
---|
| 500 | Params: []string{replyTo, "LIST", strings.Join(caps, " ")},
|
---|
| 501 | })
|
---|
| 502 | case "REQ":
|
---|
| 503 | if len(args) == 0 {
|
---|
| 504 | return ircError{&irc.Message{
|
---|
| 505 | Command: err_invalidcapcmd,
|
---|
| 506 | Params: []string{replyTo, cmd, "Missing argument in CAP REQ command"},
|
---|
| 507 | }}
|
---|
| 508 | }
|
---|
| 509 |
|
---|
[275] | 510 | // TODO: atomically ack/nak the whole capability set
|
---|
[108] | 511 | caps := strings.Fields(args[0])
|
---|
| 512 | ack := true
|
---|
| 513 | for _, name := range caps {
|
---|
| 514 | name = strings.ToLower(name)
|
---|
| 515 | enable := !strings.HasPrefix(name, "-")
|
---|
| 516 | if !enable {
|
---|
| 517 | name = strings.TrimPrefix(name, "-")
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[275] | 520 | if enable == dc.caps[name] {
|
---|
[108] | 521 | continue
|
---|
| 522 | }
|
---|
| 523 |
|
---|
[275] | 524 | _, ok := dc.supportedCaps[name]
|
---|
| 525 | if !ok {
|
---|
[108] | 526 | ack = false
|
---|
[275] | 527 | break
|
---|
[108] | 528 | }
|
---|
[275] | 529 |
|
---|
| 530 | if name == "cap-notify" && dc.capVersion >= 302 && !enable {
|
---|
| 531 | // cap-notify cannot be disabled with CAP version 302
|
---|
| 532 | ack = false
|
---|
| 533 | break
|
---|
| 534 | }
|
---|
| 535 |
|
---|
| 536 | dc.caps[name] = enable
|
---|
[108] | 537 | }
|
---|
| 538 |
|
---|
| 539 | reply := "NAK"
|
---|
| 540 | if ack {
|
---|
| 541 | reply = "ACK"
|
---|
| 542 | }
|
---|
| 543 | dc.SendMessage(&irc.Message{
|
---|
| 544 | Prefix: dc.srv.prefix(),
|
---|
| 545 | Command: "CAP",
|
---|
| 546 | Params: []string{replyTo, reply, args[0]},
|
---|
| 547 | })
|
---|
| 548 | case "END":
|
---|
| 549 | dc.negociatingCaps = false
|
---|
| 550 | default:
|
---|
| 551 | return ircError{&irc.Message{
|
---|
| 552 | Command: err_invalidcapcmd,
|
---|
| 553 | Params: []string{replyTo, cmd, "Unknown CAP command"},
|
---|
| 554 | }}
|
---|
| 555 | }
|
---|
| 556 | return nil
|
---|
| 557 | }
|
---|
| 558 |
|
---|
[275] | 559 | func (dc *downstreamConn) setSupportedCap(name, value string) {
|
---|
| 560 | prevValue, hasPrev := dc.supportedCaps[name]
|
---|
| 561 | changed := !hasPrev || prevValue != value
|
---|
| 562 | dc.supportedCaps[name] = value
|
---|
| 563 |
|
---|
| 564 | if !dc.caps["cap-notify"] || !changed {
|
---|
| 565 | return
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | replyTo := dc.nick
|
---|
| 569 | if !dc.registered {
|
---|
| 570 | replyTo = "*"
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | cap := name
|
---|
| 574 | if value != "" && dc.capVersion >= 302 {
|
---|
| 575 | cap = name + "=" + value
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | dc.SendMessage(&irc.Message{
|
---|
| 579 | Prefix: dc.srv.prefix(),
|
---|
| 580 | Command: "CAP",
|
---|
| 581 | Params: []string{replyTo, "NEW", cap},
|
---|
| 582 | })
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | func (dc *downstreamConn) unsetSupportedCap(name string) {
|
---|
| 586 | _, hasPrev := dc.supportedCaps[name]
|
---|
| 587 | delete(dc.supportedCaps, name)
|
---|
| 588 | delete(dc.caps, name)
|
---|
| 589 |
|
---|
| 590 | if !dc.caps["cap-notify"] || !hasPrev {
|
---|
| 591 | return
|
---|
| 592 | }
|
---|
| 593 |
|
---|
| 594 | replyTo := dc.nick
|
---|
| 595 | if !dc.registered {
|
---|
| 596 | replyTo = "*"
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | dc.SendMessage(&irc.Message{
|
---|
| 600 | Prefix: dc.srv.prefix(),
|
---|
| 601 | Command: "CAP",
|
---|
| 602 | Params: []string{replyTo, "DEL", name},
|
---|
| 603 | })
|
---|
| 604 | }
|
---|
| 605 |
|
---|
[276] | 606 | func (dc *downstreamConn) updateSupportedCaps() {
|
---|
[292] | 607 | supportedCaps := make(map[string]bool)
|
---|
| 608 | for cap := range needAllDownstreamCaps {
|
---|
| 609 | supportedCaps[cap] = true
|
---|
| 610 | }
|
---|
[276] | 611 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[292] | 612 | for cap, supported := range supportedCaps {
|
---|
| 613 | supportedCaps[cap] = supported && uc.caps[cap]
|
---|
| 614 | }
|
---|
[276] | 615 | })
|
---|
| 616 |
|
---|
[292] | 617 | for cap, supported := range supportedCaps {
|
---|
| 618 | if supported {
|
---|
| 619 | dc.setSupportedCap(cap, needAllDownstreamCaps[cap])
|
---|
| 620 | } else {
|
---|
| 621 | dc.unsetSupportedCap(cap)
|
---|
| 622 | }
|
---|
[276] | 623 | }
|
---|
| 624 | }
|
---|
| 625 |
|
---|
[296] | 626 | func (dc *downstreamConn) updateNick() {
|
---|
| 627 | if uc := dc.upstream(); uc != nil && uc.nick != dc.nick {
|
---|
| 628 | dc.SendMessage(&irc.Message{
|
---|
| 629 | Prefix: dc.prefix(),
|
---|
| 630 | Command: "NICK",
|
---|
| 631 | Params: []string{uc.nick},
|
---|
| 632 | })
|
---|
| 633 | dc.nick = uc.nick
|
---|
| 634 | }
|
---|
| 635 | }
|
---|
| 636 |
|
---|
[91] | 637 | func sanityCheckServer(addr string) error {
|
---|
| 638 | dialer := net.Dialer{Timeout: 30 * time.Second}
|
---|
| 639 | conn, err := tls.DialWithDialer(&dialer, "tcp", addr, nil)
|
---|
| 640 | if err != nil {
|
---|
| 641 | return err
|
---|
| 642 | }
|
---|
| 643 | return conn.Close()
|
---|
| 644 | }
|
---|
| 645 |
|
---|
[183] | 646 | func unmarshalUsername(rawUsername string) (username, client, network string) {
|
---|
[112] | 647 | username = rawUsername
|
---|
[183] | 648 |
|
---|
| 649 | i := strings.IndexAny(username, "/@")
|
---|
| 650 | j := strings.LastIndexAny(username, "/@")
|
---|
| 651 | if i >= 0 {
|
---|
| 652 | username = rawUsername[:i]
|
---|
[73] | 653 | }
|
---|
[183] | 654 | if j >= 0 {
|
---|
[190] | 655 | if rawUsername[j] == '@' {
|
---|
| 656 | client = rawUsername[j+1:]
|
---|
| 657 | } else {
|
---|
| 658 | network = rawUsername[j+1:]
|
---|
| 659 | }
|
---|
[73] | 660 | }
|
---|
[183] | 661 | if i >= 0 && j >= 0 && i < j {
|
---|
[190] | 662 | if rawUsername[i] == '@' {
|
---|
| 663 | client = rawUsername[i+1 : j]
|
---|
| 664 | } else {
|
---|
| 665 | network = rawUsername[i+1 : j]
|
---|
| 666 | }
|
---|
[183] | 667 | }
|
---|
| 668 |
|
---|
| 669 | return username, client, network
|
---|
[112] | 670 | }
|
---|
[73] | 671 |
|
---|
[168] | 672 | func (dc *downstreamConn) authenticate(username, password string) error {
|
---|
[183] | 673 | username, clientName, networkName := unmarshalUsername(username)
|
---|
[168] | 674 |
|
---|
[173] | 675 | u, err := dc.srv.db.GetUser(username)
|
---|
| 676 | if err != nil {
|
---|
| 677 | dc.logger.Printf("failed authentication for %q: %v", username, err)
|
---|
[168] | 678 | return errAuthFailed
|
---|
| 679 | }
|
---|
| 680 |
|
---|
[173] | 681 | err = bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
---|
[168] | 682 | if err != nil {
|
---|
| 683 | dc.logger.Printf("failed authentication for %q: %v", username, err)
|
---|
| 684 | return errAuthFailed
|
---|
| 685 | }
|
---|
| 686 |
|
---|
[173] | 687 | dc.user = dc.srv.getUser(username)
|
---|
| 688 | if dc.user == nil {
|
---|
| 689 | dc.logger.Printf("failed authentication for %q: user not active", username)
|
---|
| 690 | return errAuthFailed
|
---|
| 691 | }
|
---|
[183] | 692 | dc.clientName = clientName
|
---|
[168] | 693 | dc.networkName = networkName
|
---|
| 694 | return nil
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | func (dc *downstreamConn) register() error {
|
---|
| 698 | if dc.registered {
|
---|
| 699 | return fmt.Errorf("tried to register twice")
|
---|
| 700 | }
|
---|
| 701 |
|
---|
| 702 | password := dc.password
|
---|
| 703 | dc.password = ""
|
---|
| 704 | if dc.user == nil {
|
---|
| 705 | if err := dc.authenticate(dc.rawUsername, password); err != nil {
|
---|
| 706 | return err
|
---|
| 707 | }
|
---|
| 708 | }
|
---|
| 709 |
|
---|
[183] | 710 | if dc.clientName == "" && dc.networkName == "" {
|
---|
| 711 | _, dc.clientName, dc.networkName = unmarshalUsername(dc.rawUsername)
|
---|
[168] | 712 | }
|
---|
| 713 |
|
---|
| 714 | dc.registered = true
|
---|
[184] | 715 | dc.logger.Printf("registration complete for user %q", dc.user.Username)
|
---|
[168] | 716 | return nil
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | func (dc *downstreamConn) loadNetwork() error {
|
---|
| 720 | if dc.networkName == "" {
|
---|
[112] | 721 | return nil
|
---|
| 722 | }
|
---|
[85] | 723 |
|
---|
[168] | 724 | network := dc.user.getNetwork(dc.networkName)
|
---|
[112] | 725 | if network == nil {
|
---|
[168] | 726 | addr := dc.networkName
|
---|
[112] | 727 | if !strings.ContainsRune(addr, ':') {
|
---|
| 728 | addr = addr + ":6697"
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | dc.logger.Printf("trying to connect to new network %q", addr)
|
---|
| 732 | if err := sanityCheckServer(addr); err != nil {
|
---|
| 733 | dc.logger.Printf("failed to connect to %q: %v", addr, err)
|
---|
| 734 | return ircError{&irc.Message{
|
---|
| 735 | Command: irc.ERR_PASSWDMISMATCH,
|
---|
[168] | 736 | Params: []string{"*", fmt.Sprintf("Failed to connect to %q", dc.networkName)},
|
---|
[112] | 737 | }}
|
---|
| 738 | }
|
---|
| 739 |
|
---|
[168] | 740 | dc.logger.Printf("auto-saving network %q", dc.networkName)
|
---|
[112] | 741 | var err error
|
---|
[120] | 742 | network, err = dc.user.createNetwork(&Network{
|
---|
[168] | 743 | Addr: dc.networkName,
|
---|
[120] | 744 | Nick: dc.nick,
|
---|
| 745 | })
|
---|
[112] | 746 | if err != nil {
|
---|
| 747 | return err
|
---|
| 748 | }
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | dc.network = network
|
---|
| 752 | return nil
|
---|
| 753 | }
|
---|
| 754 |
|
---|
[168] | 755 | func (dc *downstreamConn) welcome() error {
|
---|
| 756 | if dc.user == nil || !dc.registered {
|
---|
| 757 | panic("tried to welcome an unregistered connection")
|
---|
[37] | 758 | }
|
---|
| 759 |
|
---|
[168] | 760 | // TODO: doing this might take some time. We should do it in dc.register
|
---|
| 761 | // instead, but we'll potentially be adding a new network and this must be
|
---|
| 762 | // done in the user goroutine.
|
---|
| 763 | if err := dc.loadNetwork(); err != nil {
|
---|
| 764 | return err
|
---|
[85] | 765 | }
|
---|
| 766 |
|
---|
[55] | 767 | dc.SendMessage(&irc.Message{
|
---|
| 768 | Prefix: dc.srv.prefix(),
|
---|
[13] | 769 | Command: irc.RPL_WELCOME,
|
---|
[98] | 770 | Params: []string{dc.nick, "Welcome to soju, " + dc.nick},
|
---|
[54] | 771 | })
|
---|
[55] | 772 | dc.SendMessage(&irc.Message{
|
---|
| 773 | Prefix: dc.srv.prefix(),
|
---|
[13] | 774 | Command: irc.RPL_YOURHOST,
|
---|
[55] | 775 | Params: []string{dc.nick, "Your host is " + dc.srv.Hostname},
|
---|
[54] | 776 | })
|
---|
[55] | 777 | dc.SendMessage(&irc.Message{
|
---|
| 778 | Prefix: dc.srv.prefix(),
|
---|
[13] | 779 | Command: irc.RPL_CREATED,
|
---|
[55] | 780 | Params: []string{dc.nick, "Who cares when the server was created?"},
|
---|
[54] | 781 | })
|
---|
[55] | 782 | dc.SendMessage(&irc.Message{
|
---|
| 783 | Prefix: dc.srv.prefix(),
|
---|
[13] | 784 | Command: irc.RPL_MYINFO,
|
---|
[98] | 785 | Params: []string{dc.nick, dc.srv.Hostname, "soju", "aiwroO", "OovaimnqpsrtklbeI"},
|
---|
[54] | 786 | })
|
---|
[93] | 787 | // TODO: RPL_ISUPPORT
|
---|
[55] | 788 | dc.SendMessage(&irc.Message{
|
---|
| 789 | Prefix: dc.srv.prefix(),
|
---|
[13] | 790 | Command: irc.ERR_NOMOTD,
|
---|
[55] | 791 | Params: []string{dc.nick, "No MOTD"},
|
---|
[54] | 792 | })
|
---|
[13] | 793 |
|
---|
[296] | 794 | dc.updateNick()
|
---|
| 795 |
|
---|
[73] | 796 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[30] | 797 | for _, ch := range uc.channels {
|
---|
[284] | 798 | if !ch.complete {
|
---|
| 799 | continue
|
---|
| 800 | }
|
---|
| 801 | if record, ok := uc.network.channels[ch.Name]; ok && record.Detached {
|
---|
| 802 | continue
|
---|
| 803 | }
|
---|
[132] | 804 |
|
---|
[284] | 805 | dc.SendMessage(&irc.Message{
|
---|
| 806 | Prefix: dc.prefix(),
|
---|
| 807 | Command: "JOIN",
|
---|
| 808 | Params: []string{dc.marshalEntity(ch.conn.network, ch.Name)},
|
---|
| 809 | })
|
---|
| 810 |
|
---|
| 811 | forwardChannel(dc, ch)
|
---|
[30] | 812 | }
|
---|
[143] | 813 | })
|
---|
[50] | 814 |
|
---|
[143] | 815 | dc.forEachNetwork(func(net *network) {
|
---|
[253] | 816 | // Only send history if we're the first connected client with that name
|
---|
| 817 | // for the network
|
---|
| 818 | if _, ok := net.offlineClients[dc.clientName]; ok {
|
---|
| 819 | dc.sendNetworkHistory(net)
|
---|
| 820 | delete(net.offlineClients, dc.clientName)
|
---|
[227] | 821 | }
|
---|
[253] | 822 | })
|
---|
[57] | 823 |
|
---|
[253] | 824 | return nil
|
---|
| 825 | }
|
---|
[144] | 826 |
|
---|
[253] | 827 | func (dc *downstreamConn) sendNetworkHistory(net *network) {
|
---|
| 828 | for target, history := range net.history {
|
---|
[284] | 829 | if ch, ok := net.channels[target]; ok && ch.Detached {
|
---|
| 830 | continue
|
---|
| 831 | }
|
---|
| 832 |
|
---|
[253] | 833 | seq, ok := history.offlineClients[dc.clientName]
|
---|
| 834 | if !ok {
|
---|
| 835 | continue
|
---|
| 836 | }
|
---|
| 837 | delete(history.offlineClients, dc.clientName)
|
---|
| 838 |
|
---|
| 839 | // If all clients have received history, no need to keep the
|
---|
| 840 | // ring buffer around
|
---|
| 841 | if len(history.offlineClients) == 0 {
|
---|
| 842 | delete(net.history, target)
|
---|
| 843 | }
|
---|
| 844 |
|
---|
| 845 | consumer := history.ring.NewConsumer(seq)
|
---|
| 846 |
|
---|
[256] | 847 | batchRef := "history"
|
---|
| 848 | if dc.caps["batch"] {
|
---|
| 849 | dc.SendMessage(&irc.Message{
|
---|
| 850 | Prefix: dc.srv.prefix(),
|
---|
| 851 | Command: "BATCH",
|
---|
[260] | 852 | Params: []string{"+" + batchRef, "chathistory", dc.marshalEntity(net, target)},
|
---|
[256] | 853 | })
|
---|
| 854 | }
|
---|
| 855 |
|
---|
[227] | 856 | for {
|
---|
[233] | 857 | msg := consumer.Consume()
|
---|
[227] | 858 | if msg == nil {
|
---|
| 859 | break
|
---|
[204] | 860 | }
|
---|
| 861 |
|
---|
[245] | 862 | // Don't replay all messages, because that would mess up client
|
---|
| 863 | // state. For instance we just sent the list of users, sending
|
---|
| 864 | // PART messages for one of these users would be incorrect.
|
---|
| 865 | ignore := true
|
---|
| 866 | switch msg.Command {
|
---|
| 867 | case "PRIVMSG", "NOTICE":
|
---|
| 868 | ignore = false
|
---|
| 869 | }
|
---|
| 870 | if ignore {
|
---|
| 871 | continue
|
---|
| 872 | }
|
---|
| 873 |
|
---|
[256] | 874 | if dc.caps["batch"] {
|
---|
| 875 | msg = msg.Copy()
|
---|
| 876 | msg.Tags["batch"] = irc.TagValue(batchRef)
|
---|
| 877 | }
|
---|
| 878 |
|
---|
[261] | 879 | dc.SendMessage(dc.marshalMessage(msg, net))
|
---|
[227] | 880 | }
|
---|
[256] | 881 |
|
---|
| 882 | if dc.caps["batch"] {
|
---|
| 883 | dc.SendMessage(&irc.Message{
|
---|
| 884 | Prefix: dc.srv.prefix(),
|
---|
| 885 | Command: "BATCH",
|
---|
| 886 | Params: []string{"-" + batchRef},
|
---|
| 887 | })
|
---|
| 888 | }
|
---|
[253] | 889 | }
|
---|
[13] | 890 | }
|
---|
| 891 |
|
---|
[103] | 892 | func (dc *downstreamConn) runUntilRegistered() error {
|
---|
| 893 | for !dc.registered {
|
---|
[212] | 894 | msg, err := dc.ReadMessage()
|
---|
[106] | 895 | if err != nil {
|
---|
[103] | 896 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
| 897 | }
|
---|
| 898 |
|
---|
| 899 | err = dc.handleMessage(msg)
|
---|
| 900 | if ircErr, ok := err.(ircError); ok {
|
---|
| 901 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
| 902 | dc.SendMessage(ircErr.Message)
|
---|
| 903 | } else if err != nil {
|
---|
| 904 | return fmt.Errorf("failed to handle IRC command %q: %v", msg, err)
|
---|
| 905 | }
|
---|
| 906 | }
|
---|
| 907 |
|
---|
| 908 | return nil
|
---|
| 909 | }
|
---|
| 910 |
|
---|
[55] | 911 | func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
|
---|
[13] | 912 | switch msg.Command {
|
---|
[111] | 913 | case "CAP":
|
---|
| 914 | var subCmd string
|
---|
| 915 | if err := parseMessageParams(msg, &subCmd); err != nil {
|
---|
| 916 | return err
|
---|
| 917 | }
|
---|
| 918 | if err := dc.handleCapCommand(subCmd, msg.Params[1:]); err != nil {
|
---|
| 919 | return err
|
---|
| 920 | }
|
---|
[107] | 921 | case "PING":
|
---|
| 922 | dc.SendMessage(&irc.Message{
|
---|
| 923 | Prefix: dc.srv.prefix(),
|
---|
| 924 | Command: "PONG",
|
---|
| 925 | Params: msg.Params,
|
---|
| 926 | })
|
---|
| 927 | return nil
|
---|
[42] | 928 | case "USER":
|
---|
[13] | 929 | return ircError{&irc.Message{
|
---|
| 930 | Command: irc.ERR_ALREADYREGISTERED,
|
---|
[55] | 931 | Params: []string{dc.nick, "You may not reregister"},
|
---|
[13] | 932 | }}
|
---|
[42] | 933 | case "NICK":
|
---|
[90] | 934 | var nick string
|
---|
| 935 | if err := parseMessageParams(msg, &nick); err != nil {
|
---|
| 936 | return err
|
---|
| 937 | }
|
---|
| 938 |
|
---|
[297] | 939 | var upstream *upstreamConn
|
---|
| 940 | if dc.upstream() == nil {
|
---|
| 941 | uc, unmarshaledNick, err := dc.unmarshalEntity(nick)
|
---|
| 942 | if err == nil { // NICK nick/network: NICK only on a specific upstream
|
---|
| 943 | upstream = uc
|
---|
| 944 | nick = unmarshaledNick
|
---|
| 945 | }
|
---|
| 946 | }
|
---|
| 947 |
|
---|
[90] | 948 | var err error
|
---|
| 949 | dc.forEachNetwork(func(n *network) {
|
---|
[297] | 950 | if err != nil || (upstream != nil && upstream.network != n) {
|
---|
[90] | 951 | return
|
---|
| 952 | }
|
---|
| 953 | n.Nick = nick
|
---|
| 954 | err = dc.srv.db.StoreNetwork(dc.user.Username, &n.Network)
|
---|
| 955 | })
|
---|
| 956 | if err != nil {
|
---|
| 957 | return err
|
---|
| 958 | }
|
---|
| 959 |
|
---|
[73] | 960 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[297] | 961 | if upstream != nil && upstream != uc {
|
---|
| 962 | return
|
---|
| 963 | }
|
---|
[301] | 964 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[297] | 965 | Command: "NICK",
|
---|
| 966 | Params: []string{nick},
|
---|
| 967 | })
|
---|
[42] | 968 | })
|
---|
[296] | 969 |
|
---|
| 970 | if dc.upstream() == nil && dc.nick != nick {
|
---|
| 971 | dc.SendMessage(&irc.Message{
|
---|
| 972 | Prefix: dc.prefix(),
|
---|
| 973 | Command: "NICK",
|
---|
| 974 | Params: []string{nick},
|
---|
| 975 | })
|
---|
| 976 | dc.nick = nick
|
---|
| 977 | }
|
---|
[146] | 978 | case "JOIN":
|
---|
| 979 | var namesStr string
|
---|
| 980 | if err := parseMessageParams(msg, &namesStr); err != nil {
|
---|
[48] | 981 | return err
|
---|
| 982 | }
|
---|
| 983 |
|
---|
[146] | 984 | var keys []string
|
---|
| 985 | if len(msg.Params) > 1 {
|
---|
| 986 | keys = strings.Split(msg.Params[1], ",")
|
---|
| 987 | }
|
---|
| 988 |
|
---|
| 989 | for i, name := range strings.Split(namesStr, ",") {
|
---|
[145] | 990 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 991 | if err != nil {
|
---|
[158] | 992 | return err
|
---|
[145] | 993 | }
|
---|
[48] | 994 |
|
---|
[146] | 995 | var key string
|
---|
| 996 | if len(keys) > i {
|
---|
| 997 | key = keys[i]
|
---|
| 998 | }
|
---|
| 999 |
|
---|
| 1000 | params := []string{upstreamName}
|
---|
| 1001 | if key != "" {
|
---|
| 1002 | params = append(params, key)
|
---|
| 1003 | }
|
---|
[301] | 1004 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[146] | 1005 | Command: "JOIN",
|
---|
| 1006 | Params: params,
|
---|
[145] | 1007 | })
|
---|
[89] | 1008 |
|
---|
[284] | 1009 | ch := &Channel{Name: upstreamName, Key: key, Detached: false}
|
---|
[285] | 1010 | if current, ok := uc.network.channels[ch.Name]; ok && key == "" {
|
---|
| 1011 | // Don't clear the channel key if there's one set
|
---|
| 1012 | // TODO: add a way to unset the channel key
|
---|
| 1013 | ch.Key = current.Key
|
---|
| 1014 | }
|
---|
[222] | 1015 | if err := uc.network.createUpdateChannel(ch); err != nil {
|
---|
| 1016 | dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
|
---|
[89] | 1017 | }
|
---|
| 1018 | }
|
---|
[146] | 1019 | case "PART":
|
---|
| 1020 | var namesStr string
|
---|
| 1021 | if err := parseMessageParams(msg, &namesStr); err != nil {
|
---|
| 1022 | return err
|
---|
| 1023 | }
|
---|
| 1024 |
|
---|
| 1025 | var reason string
|
---|
| 1026 | if len(msg.Params) > 1 {
|
---|
| 1027 | reason = msg.Params[1]
|
---|
| 1028 | }
|
---|
| 1029 |
|
---|
| 1030 | for _, name := range strings.Split(namesStr, ",") {
|
---|
| 1031 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1032 | if err != nil {
|
---|
[158] | 1033 | return err
|
---|
[146] | 1034 | }
|
---|
| 1035 |
|
---|
[284] | 1036 | if strings.EqualFold(reason, "detach") {
|
---|
| 1037 | ch := &Channel{Name: upstreamName, Detached: true}
|
---|
| 1038 | if err := uc.network.createUpdateChannel(ch); err != nil {
|
---|
| 1039 | dc.logger.Printf("failed to detach channel %q: %v", upstreamName, err)
|
---|
| 1040 | }
|
---|
| 1041 | } else {
|
---|
| 1042 | params := []string{upstreamName}
|
---|
| 1043 | if reason != "" {
|
---|
| 1044 | params = append(params, reason)
|
---|
| 1045 | }
|
---|
[301] | 1046 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[284] | 1047 | Command: "PART",
|
---|
| 1048 | Params: params,
|
---|
| 1049 | })
|
---|
[146] | 1050 |
|
---|
[284] | 1051 | if err := uc.network.deleteChannel(upstreamName); err != nil {
|
---|
| 1052 | dc.logger.Printf("failed to delete channel %q: %v", upstreamName, err)
|
---|
| 1053 | }
|
---|
[146] | 1054 | }
|
---|
| 1055 | }
|
---|
[159] | 1056 | case "KICK":
|
---|
| 1057 | var channelStr, userStr string
|
---|
| 1058 | if err := parseMessageParams(msg, &channelStr, &userStr); err != nil {
|
---|
| 1059 | return err
|
---|
| 1060 | }
|
---|
| 1061 |
|
---|
| 1062 | channels := strings.Split(channelStr, ",")
|
---|
| 1063 | users := strings.Split(userStr, ",")
|
---|
| 1064 |
|
---|
| 1065 | var reason string
|
---|
| 1066 | if len(msg.Params) > 2 {
|
---|
| 1067 | reason = msg.Params[2]
|
---|
| 1068 | }
|
---|
| 1069 |
|
---|
| 1070 | if len(channels) != 1 && len(channels) != len(users) {
|
---|
| 1071 | return ircError{&irc.Message{
|
---|
| 1072 | Command: irc.ERR_BADCHANMASK,
|
---|
| 1073 | Params: []string{dc.nick, channelStr, "Bad channel mask"},
|
---|
| 1074 | }}
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
| 1077 | for i, user := range users {
|
---|
| 1078 | var channel string
|
---|
| 1079 | if len(channels) == 1 {
|
---|
| 1080 | channel = channels[0]
|
---|
| 1081 | } else {
|
---|
| 1082 | channel = channels[i]
|
---|
| 1083 | }
|
---|
| 1084 |
|
---|
| 1085 | ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1086 | if err != nil {
|
---|
| 1087 | return err
|
---|
| 1088 | }
|
---|
| 1089 |
|
---|
| 1090 | ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
---|
| 1091 | if err != nil {
|
---|
| 1092 | return err
|
---|
| 1093 | }
|
---|
| 1094 |
|
---|
| 1095 | if ucChannel != ucUser {
|
---|
| 1096 | return ircError{&irc.Message{
|
---|
| 1097 | Command: irc.ERR_USERNOTINCHANNEL,
|
---|
| 1098 | Params: []string{dc.nick, user, channel, "They aren't on that channel"},
|
---|
| 1099 | }}
|
---|
| 1100 | }
|
---|
| 1101 | uc := ucChannel
|
---|
| 1102 |
|
---|
| 1103 | params := []string{upstreamChannel, upstreamUser}
|
---|
| 1104 | if reason != "" {
|
---|
| 1105 | params = append(params, reason)
|
---|
| 1106 | }
|
---|
[301] | 1107 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[159] | 1108 | Command: "KICK",
|
---|
| 1109 | Params: params,
|
---|
| 1110 | })
|
---|
| 1111 | }
|
---|
[69] | 1112 | case "MODE":
|
---|
[46] | 1113 | var name string
|
---|
| 1114 | if err := parseMessageParams(msg, &name); err != nil {
|
---|
| 1115 | return err
|
---|
| 1116 | }
|
---|
| 1117 |
|
---|
| 1118 | var modeStr string
|
---|
| 1119 | if len(msg.Params) > 1 {
|
---|
| 1120 | modeStr = msg.Params[1]
|
---|
| 1121 | }
|
---|
| 1122 |
|
---|
[139] | 1123 | if name == dc.nick {
|
---|
[46] | 1124 | if modeStr != "" {
|
---|
[73] | 1125 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[301] | 1126 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[69] | 1127 | Command: "MODE",
|
---|
| 1128 | Params: []string{uc.nick, modeStr},
|
---|
| 1129 | })
|
---|
[46] | 1130 | })
|
---|
| 1131 | } else {
|
---|
[55] | 1132 | dc.SendMessage(&irc.Message{
|
---|
| 1133 | Prefix: dc.srv.prefix(),
|
---|
[46] | 1134 | Command: irc.RPL_UMODEIS,
|
---|
[129] | 1135 | Params: []string{dc.nick, ""}, // TODO
|
---|
[54] | 1136 | })
|
---|
[46] | 1137 | }
|
---|
[139] | 1138 | return nil
|
---|
[46] | 1139 | }
|
---|
[139] | 1140 |
|
---|
| 1141 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1142 | if err != nil {
|
---|
| 1143 | return err
|
---|
| 1144 | }
|
---|
| 1145 |
|
---|
| 1146 | if !uc.isChannel(upstreamName) {
|
---|
| 1147 | return ircError{&irc.Message{
|
---|
| 1148 | Command: irc.ERR_USERSDONTMATCH,
|
---|
| 1149 | Params: []string{dc.nick, "Cannot change mode for other users"},
|
---|
| 1150 | }}
|
---|
| 1151 | }
|
---|
| 1152 |
|
---|
| 1153 | if modeStr != "" {
|
---|
| 1154 | params := []string{upstreamName, modeStr}
|
---|
| 1155 | params = append(params, msg.Params[2:]...)
|
---|
[301] | 1156 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[139] | 1157 | Command: "MODE",
|
---|
| 1158 | Params: params,
|
---|
| 1159 | })
|
---|
| 1160 | } else {
|
---|
| 1161 | ch, ok := uc.channels[upstreamName]
|
---|
| 1162 | if !ok {
|
---|
| 1163 | return ircError{&irc.Message{
|
---|
| 1164 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
| 1165 | Params: []string{dc.nick, name, "No such channel"},
|
---|
| 1166 | }}
|
---|
| 1167 | }
|
---|
| 1168 |
|
---|
| 1169 | if ch.modes == nil {
|
---|
| 1170 | // we haven't received the initial RPL_CHANNELMODEIS yet
|
---|
| 1171 | // ignore the request, we will broadcast the modes later when we receive RPL_CHANNELMODEIS
|
---|
| 1172 | return nil
|
---|
| 1173 | }
|
---|
| 1174 |
|
---|
| 1175 | modeStr, modeParams := ch.modes.Format()
|
---|
| 1176 | params := []string{dc.nick, name, modeStr}
|
---|
| 1177 | params = append(params, modeParams...)
|
---|
| 1178 |
|
---|
| 1179 | dc.SendMessage(&irc.Message{
|
---|
| 1180 | Prefix: dc.srv.prefix(),
|
---|
| 1181 | Command: irc.RPL_CHANNELMODEIS,
|
---|
| 1182 | Params: params,
|
---|
| 1183 | })
|
---|
[162] | 1184 | if ch.creationTime != "" {
|
---|
| 1185 | dc.SendMessage(&irc.Message{
|
---|
| 1186 | Prefix: dc.srv.prefix(),
|
---|
| 1187 | Command: rpl_creationtime,
|
---|
| 1188 | Params: []string{dc.nick, name, ch.creationTime},
|
---|
| 1189 | })
|
---|
| 1190 | }
|
---|
[139] | 1191 | }
|
---|
[160] | 1192 | case "TOPIC":
|
---|
| 1193 | var channel string
|
---|
| 1194 | if err := parseMessageParams(msg, &channel); err != nil {
|
---|
| 1195 | return err
|
---|
| 1196 | }
|
---|
| 1197 |
|
---|
| 1198 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1199 | if err != nil {
|
---|
| 1200 | return err
|
---|
| 1201 | }
|
---|
| 1202 |
|
---|
| 1203 | if len(msg.Params) > 1 { // setting topic
|
---|
| 1204 | topic := msg.Params[1]
|
---|
[301] | 1205 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[160] | 1206 | Command: "TOPIC",
|
---|
| 1207 | Params: []string{upstreamChannel, topic},
|
---|
| 1208 | })
|
---|
| 1209 | } else { // getting topic
|
---|
| 1210 | ch, ok := uc.channels[upstreamChannel]
|
---|
| 1211 | if !ok {
|
---|
| 1212 | return ircError{&irc.Message{
|
---|
| 1213 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
| 1214 | Params: []string{dc.nick, upstreamChannel, "No such channel"},
|
---|
| 1215 | }}
|
---|
| 1216 | }
|
---|
| 1217 | sendTopic(dc, ch)
|
---|
| 1218 | }
|
---|
[177] | 1219 | case "LIST":
|
---|
| 1220 | // TODO: support ELIST when supported by all upstreams
|
---|
| 1221 |
|
---|
| 1222 | pl := pendingLIST{
|
---|
| 1223 | downstreamID: dc.id,
|
---|
| 1224 | pendingCommands: make(map[int64]*irc.Message),
|
---|
| 1225 | }
|
---|
[298] | 1226 | var upstream *upstreamConn
|
---|
[177] | 1227 | var upstreamChannels map[int64][]string
|
---|
| 1228 | if len(msg.Params) > 0 {
|
---|
[298] | 1229 | uc, upstreamMask, err := dc.unmarshalEntity(msg.Params[0])
|
---|
| 1230 | if err == nil && upstreamMask == "*" { // LIST */network: send LIST only to one network
|
---|
| 1231 | upstream = uc
|
---|
| 1232 | } else {
|
---|
| 1233 | upstreamChannels = make(map[int64][]string)
|
---|
| 1234 | channels := strings.Split(msg.Params[0], ",")
|
---|
| 1235 | for _, channel := range channels {
|
---|
| 1236 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1237 | if err != nil {
|
---|
| 1238 | return err
|
---|
| 1239 | }
|
---|
| 1240 | upstreamChannels[uc.network.ID] = append(upstreamChannels[uc.network.ID], upstreamChannel)
|
---|
[177] | 1241 | }
|
---|
| 1242 | }
|
---|
| 1243 | }
|
---|
| 1244 |
|
---|
| 1245 | dc.user.pendingLISTs = append(dc.user.pendingLISTs, pl)
|
---|
| 1246 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[298] | 1247 | if upstream != nil && upstream != uc {
|
---|
| 1248 | return
|
---|
| 1249 | }
|
---|
[177] | 1250 | var params []string
|
---|
| 1251 | if upstreamChannels != nil {
|
---|
| 1252 | if channels, ok := upstreamChannels[uc.network.ID]; ok {
|
---|
| 1253 | params = []string{strings.Join(channels, ",")}
|
---|
| 1254 | } else {
|
---|
| 1255 | return
|
---|
| 1256 | }
|
---|
| 1257 | }
|
---|
| 1258 | pl.pendingCommands[uc.network.ID] = &irc.Message{
|
---|
| 1259 | Command: "LIST",
|
---|
| 1260 | Params: params,
|
---|
| 1261 | }
|
---|
[181] | 1262 | uc.trySendLIST(dc.id)
|
---|
[177] | 1263 | })
|
---|
[140] | 1264 | case "NAMES":
|
---|
| 1265 | if len(msg.Params) == 0 {
|
---|
| 1266 | dc.SendMessage(&irc.Message{
|
---|
| 1267 | Prefix: dc.srv.prefix(),
|
---|
| 1268 | Command: irc.RPL_ENDOFNAMES,
|
---|
| 1269 | Params: []string{dc.nick, "*", "End of /NAMES list"},
|
---|
| 1270 | })
|
---|
| 1271 | return nil
|
---|
| 1272 | }
|
---|
| 1273 |
|
---|
| 1274 | channels := strings.Split(msg.Params[0], ",")
|
---|
| 1275 | for _, channel := range channels {
|
---|
| 1276 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1277 | if err != nil {
|
---|
| 1278 | return err
|
---|
| 1279 | }
|
---|
| 1280 |
|
---|
| 1281 | ch, ok := uc.channels[upstreamChannel]
|
---|
| 1282 | if ok {
|
---|
| 1283 | sendNames(dc, ch)
|
---|
| 1284 | } else {
|
---|
| 1285 | // NAMES on a channel we have not joined, ask upstream
|
---|
[176] | 1286 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[140] | 1287 | Command: "NAMES",
|
---|
| 1288 | Params: []string{upstreamChannel},
|
---|
| 1289 | })
|
---|
| 1290 | }
|
---|
| 1291 | }
|
---|
[127] | 1292 | case "WHO":
|
---|
| 1293 | if len(msg.Params) == 0 {
|
---|
| 1294 | // TODO: support WHO without parameters
|
---|
| 1295 | dc.SendMessage(&irc.Message{
|
---|
| 1296 | Prefix: dc.srv.prefix(),
|
---|
| 1297 | Command: irc.RPL_ENDOFWHO,
|
---|
[140] | 1298 | Params: []string{dc.nick, "*", "End of /WHO list"},
|
---|
[127] | 1299 | })
|
---|
| 1300 | return nil
|
---|
| 1301 | }
|
---|
| 1302 |
|
---|
| 1303 | // TODO: support WHO masks
|
---|
| 1304 | entity := msg.Params[0]
|
---|
| 1305 |
|
---|
[142] | 1306 | if entity == dc.nick {
|
---|
| 1307 | // TODO: support AWAY (H/G) in self WHO reply
|
---|
| 1308 | dc.SendMessage(&irc.Message{
|
---|
| 1309 | Prefix: dc.srv.prefix(),
|
---|
| 1310 | Command: irc.RPL_WHOREPLY,
|
---|
[184] | 1311 | Params: []string{dc.nick, "*", dc.user.Username, dc.hostname, dc.srv.Hostname, dc.nick, "H", "0 " + dc.realname},
|
---|
[142] | 1312 | })
|
---|
| 1313 | dc.SendMessage(&irc.Message{
|
---|
| 1314 | Prefix: dc.srv.prefix(),
|
---|
| 1315 | Command: irc.RPL_ENDOFWHO,
|
---|
| 1316 | Params: []string{dc.nick, dc.nick, "End of /WHO list"},
|
---|
| 1317 | })
|
---|
| 1318 | return nil
|
---|
| 1319 | }
|
---|
| 1320 |
|
---|
[127] | 1321 | uc, upstreamName, err := dc.unmarshalEntity(entity)
|
---|
| 1322 | if err != nil {
|
---|
| 1323 | return err
|
---|
| 1324 | }
|
---|
| 1325 |
|
---|
| 1326 | var params []string
|
---|
| 1327 | if len(msg.Params) == 2 {
|
---|
| 1328 | params = []string{upstreamName, msg.Params[1]}
|
---|
| 1329 | } else {
|
---|
| 1330 | params = []string{upstreamName}
|
---|
| 1331 | }
|
---|
| 1332 |
|
---|
[176] | 1333 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[127] | 1334 | Command: "WHO",
|
---|
| 1335 | Params: params,
|
---|
| 1336 | })
|
---|
[128] | 1337 | case "WHOIS":
|
---|
| 1338 | if len(msg.Params) == 0 {
|
---|
| 1339 | return ircError{&irc.Message{
|
---|
| 1340 | Command: irc.ERR_NONICKNAMEGIVEN,
|
---|
| 1341 | Params: []string{dc.nick, "No nickname given"},
|
---|
| 1342 | }}
|
---|
| 1343 | }
|
---|
| 1344 |
|
---|
| 1345 | var target, mask string
|
---|
| 1346 | if len(msg.Params) == 1 {
|
---|
| 1347 | target = ""
|
---|
| 1348 | mask = msg.Params[0]
|
---|
| 1349 | } else {
|
---|
| 1350 | target = msg.Params[0]
|
---|
| 1351 | mask = msg.Params[1]
|
---|
| 1352 | }
|
---|
| 1353 | // TODO: support multiple WHOIS users
|
---|
| 1354 | if i := strings.IndexByte(mask, ','); i >= 0 {
|
---|
| 1355 | mask = mask[:i]
|
---|
| 1356 | }
|
---|
| 1357 |
|
---|
[142] | 1358 | if mask == dc.nick {
|
---|
| 1359 | dc.SendMessage(&irc.Message{
|
---|
| 1360 | Prefix: dc.srv.prefix(),
|
---|
| 1361 | Command: irc.RPL_WHOISUSER,
|
---|
[184] | 1362 | Params: []string{dc.nick, dc.nick, dc.user.Username, dc.hostname, "*", dc.realname},
|
---|
[142] | 1363 | })
|
---|
| 1364 | dc.SendMessage(&irc.Message{
|
---|
| 1365 | Prefix: dc.srv.prefix(),
|
---|
| 1366 | Command: irc.RPL_WHOISSERVER,
|
---|
| 1367 | Params: []string{dc.nick, dc.nick, dc.srv.Hostname, "soju"},
|
---|
| 1368 | })
|
---|
| 1369 | dc.SendMessage(&irc.Message{
|
---|
| 1370 | Prefix: dc.srv.prefix(),
|
---|
| 1371 | Command: irc.RPL_ENDOFWHOIS,
|
---|
| 1372 | Params: []string{dc.nick, dc.nick, "End of /WHOIS list"},
|
---|
| 1373 | })
|
---|
| 1374 | return nil
|
---|
| 1375 | }
|
---|
| 1376 |
|
---|
[128] | 1377 | // TODO: support WHOIS masks
|
---|
| 1378 | uc, upstreamNick, err := dc.unmarshalEntity(mask)
|
---|
| 1379 | if err != nil {
|
---|
| 1380 | return err
|
---|
| 1381 | }
|
---|
| 1382 |
|
---|
| 1383 | var params []string
|
---|
| 1384 | if target != "" {
|
---|
[299] | 1385 | if target == mask { // WHOIS nick nick
|
---|
| 1386 | params = []string{upstreamNick, upstreamNick}
|
---|
| 1387 | } else {
|
---|
| 1388 | params = []string{target, upstreamNick}
|
---|
| 1389 | }
|
---|
[128] | 1390 | } else {
|
---|
| 1391 | params = []string{upstreamNick}
|
---|
| 1392 | }
|
---|
| 1393 |
|
---|
[176] | 1394 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[128] | 1395 | Command: "WHOIS",
|
---|
| 1396 | Params: params,
|
---|
| 1397 | })
|
---|
[58] | 1398 | case "PRIVMSG":
|
---|
| 1399 | var targetsStr, text string
|
---|
| 1400 | if err := parseMessageParams(msg, &targetsStr, &text); err != nil {
|
---|
| 1401 | return err
|
---|
| 1402 | }
|
---|
[303] | 1403 | tags := copyClientTags(msg.Tags)
|
---|
[58] | 1404 |
|
---|
| 1405 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
[117] | 1406 | if name == serviceNick {
|
---|
| 1407 | handleServicePRIVMSG(dc, text)
|
---|
| 1408 | continue
|
---|
| 1409 | }
|
---|
| 1410 |
|
---|
[127] | 1411 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
[58] | 1412 | if err != nil {
|
---|
| 1413 | return err
|
---|
| 1414 | }
|
---|
| 1415 |
|
---|
[95] | 1416 | if upstreamName == "NickServ" {
|
---|
| 1417 | dc.handleNickServPRIVMSG(uc, text)
|
---|
| 1418 | }
|
---|
| 1419 |
|
---|
[268] | 1420 | unmarshaledText := text
|
---|
| 1421 | if uc.isChannel(upstreamName) {
|
---|
| 1422 | unmarshaledText = dc.unmarshalText(uc, text)
|
---|
| 1423 | }
|
---|
[301] | 1424 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[303] | 1425 | Tags: tags,
|
---|
[58] | 1426 | Command: "PRIVMSG",
|
---|
[268] | 1427 | Params: []string{upstreamName, unmarshaledText},
|
---|
[60] | 1428 | })
|
---|
[105] | 1429 |
|
---|
[303] | 1430 | echoTags := tags.Copy()
|
---|
| 1431 | echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
[113] | 1432 | echoMsg := &irc.Message{
|
---|
[303] | 1433 | Tags: echoTags,
|
---|
[113] | 1434 | Prefix: &irc.Prefix{
|
---|
| 1435 | Name: uc.nick,
|
---|
| 1436 | User: uc.username,
|
---|
| 1437 | },
|
---|
[114] | 1438 | Command: "PRIVMSG",
|
---|
[113] | 1439 | Params: []string{upstreamName, text},
|
---|
| 1440 | }
|
---|
[239] | 1441 | uc.produce(upstreamName, echoMsg, dc)
|
---|
[58] | 1442 | }
|
---|
[164] | 1443 | case "NOTICE":
|
---|
| 1444 | var targetsStr, text string
|
---|
| 1445 | if err := parseMessageParams(msg, &targetsStr, &text); err != nil {
|
---|
| 1446 | return err
|
---|
| 1447 | }
|
---|
[303] | 1448 | tags := copyClientTags(msg.Tags)
|
---|
[164] | 1449 |
|
---|
| 1450 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
| 1451 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1452 | if err != nil {
|
---|
| 1453 | return err
|
---|
| 1454 | }
|
---|
| 1455 |
|
---|
[268] | 1456 | unmarshaledText := text
|
---|
| 1457 | if uc.isChannel(upstreamName) {
|
---|
| 1458 | unmarshaledText = dc.unmarshalText(uc, text)
|
---|
| 1459 | }
|
---|
[301] | 1460 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[303] | 1461 | Tags: tags,
|
---|
[164] | 1462 | Command: "NOTICE",
|
---|
[268] | 1463 | Params: []string{upstreamName, unmarshaledText},
|
---|
[164] | 1464 | })
|
---|
| 1465 | }
|
---|
[303] | 1466 | case "TAGMSG":
|
---|
| 1467 | var targetsStr string
|
---|
| 1468 | if err := parseMessageParams(msg, &targetsStr); err != nil {
|
---|
| 1469 | return err
|
---|
| 1470 | }
|
---|
| 1471 | tags := copyClientTags(msg.Tags)
|
---|
| 1472 |
|
---|
| 1473 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
| 1474 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1475 | if err != nil {
|
---|
| 1476 | return err
|
---|
| 1477 | }
|
---|
| 1478 |
|
---|
| 1479 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
| 1480 | Tags: tags,
|
---|
| 1481 | Command: "TAGMSG",
|
---|
| 1482 | Params: []string{upstreamName},
|
---|
| 1483 | })
|
---|
| 1484 | }
|
---|
[163] | 1485 | case "INVITE":
|
---|
| 1486 | var user, channel string
|
---|
| 1487 | if err := parseMessageParams(msg, &user, &channel); err != nil {
|
---|
| 1488 | return err
|
---|
| 1489 | }
|
---|
| 1490 |
|
---|
| 1491 | ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1492 | if err != nil {
|
---|
| 1493 | return err
|
---|
| 1494 | }
|
---|
| 1495 |
|
---|
| 1496 | ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
---|
| 1497 | if err != nil {
|
---|
| 1498 | return err
|
---|
| 1499 | }
|
---|
| 1500 |
|
---|
| 1501 | if ucChannel != ucUser {
|
---|
| 1502 | return ircError{&irc.Message{
|
---|
| 1503 | Command: irc.ERR_USERNOTINCHANNEL,
|
---|
| 1504 | Params: []string{dc.nick, user, channel, "They aren't on that channel"},
|
---|
| 1505 | }}
|
---|
| 1506 | }
|
---|
| 1507 | uc := ucChannel
|
---|
| 1508 |
|
---|
[176] | 1509 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[163] | 1510 | Command: "INVITE",
|
---|
| 1511 | Params: []string{upstreamUser, upstreamChannel},
|
---|
| 1512 | })
|
---|
[13] | 1513 | default:
|
---|
[55] | 1514 | dc.logger.Printf("unhandled message: %v", msg)
|
---|
[13] | 1515 | return newUnknownCommandError(msg.Command)
|
---|
| 1516 | }
|
---|
[42] | 1517 | return nil
|
---|
[13] | 1518 | }
|
---|
[95] | 1519 |
|
---|
| 1520 | func (dc *downstreamConn) handleNickServPRIVMSG(uc *upstreamConn, text string) {
|
---|
| 1521 | username, password, ok := parseNickServCredentials(text, uc.nick)
|
---|
| 1522 | if !ok {
|
---|
| 1523 | return
|
---|
| 1524 | }
|
---|
| 1525 |
|
---|
[307] | 1526 | // User may have e.g. EXTERNAL mechanism configured. We do not want to
|
---|
| 1527 | // automatically erase the key pair or any other credentials.
|
---|
| 1528 | if uc.network.SASL.Mechanism != "" && uc.network.SASL.Mechanism != "PLAIN" {
|
---|
| 1529 | return
|
---|
| 1530 | }
|
---|
| 1531 |
|
---|
[95] | 1532 | dc.logger.Printf("auto-saving NickServ credentials with username %q", username)
|
---|
| 1533 | n := uc.network
|
---|
| 1534 | n.SASL.Mechanism = "PLAIN"
|
---|
| 1535 | n.SASL.Plain.Username = username
|
---|
| 1536 | n.SASL.Plain.Password = password
|
---|
| 1537 | if err := dc.srv.db.StoreNetwork(dc.user.Username, &n.Network); err != nil {
|
---|
| 1538 | dc.logger.Printf("failed to save NickServ credentials: %v", err)
|
---|
| 1539 | }
|
---|
| 1540 | }
|
---|
| 1541 |
|
---|
| 1542 | func parseNickServCredentials(text, nick string) (username, password string, ok bool) {
|
---|
| 1543 | fields := strings.Fields(text)
|
---|
| 1544 | if len(fields) < 2 {
|
---|
| 1545 | return "", "", false
|
---|
| 1546 | }
|
---|
| 1547 | cmd := strings.ToUpper(fields[0])
|
---|
| 1548 | params := fields[1:]
|
---|
| 1549 | switch cmd {
|
---|
| 1550 | case "REGISTER":
|
---|
| 1551 | username = nick
|
---|
| 1552 | password = params[0]
|
---|
| 1553 | case "IDENTIFY":
|
---|
| 1554 | if len(params) == 1 {
|
---|
| 1555 | username = nick
|
---|
[182] | 1556 | password = params[0]
|
---|
[95] | 1557 | } else {
|
---|
| 1558 | username = params[0]
|
---|
[182] | 1559 | password = params[1]
|
---|
[95] | 1560 | }
|
---|
[182] | 1561 | case "SET":
|
---|
| 1562 | if len(params) == 2 && strings.EqualFold(params[0], "PASSWORD") {
|
---|
| 1563 | username = nick
|
---|
| 1564 | password = params[1]
|
---|
| 1565 | }
|
---|
[95] | 1566 | }
|
---|
| 1567 | return username, password, true
|
---|
| 1568 | }
|
---|