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