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