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