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