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