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