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