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