[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 |
|
---|
[55] | 882 | dc.SendMessage(&irc.Message{
|
---|
| 883 | Prefix: dc.srv.prefix(),
|
---|
[13] | 884 | Command: irc.RPL_WELCOME,
|
---|
[98] | 885 | Params: []string{dc.nick, "Welcome to soju, " + dc.nick},
|
---|
[54] | 886 | })
|
---|
[55] | 887 | dc.SendMessage(&irc.Message{
|
---|
| 888 | Prefix: dc.srv.prefix(),
|
---|
[13] | 889 | Command: irc.RPL_YOURHOST,
|
---|
[55] | 890 | Params: []string{dc.nick, "Your host is " + dc.srv.Hostname},
|
---|
[54] | 891 | })
|
---|
[55] | 892 | dc.SendMessage(&irc.Message{
|
---|
| 893 | Prefix: dc.srv.prefix(),
|
---|
[13] | 894 | Command: irc.RPL_CREATED,
|
---|
[55] | 895 | Params: []string{dc.nick, "Who cares when the server was created?"},
|
---|
[54] | 896 | })
|
---|
[55] | 897 | dc.SendMessage(&irc.Message{
|
---|
| 898 | Prefix: dc.srv.prefix(),
|
---|
[13] | 899 | Command: irc.RPL_MYINFO,
|
---|
[98] | 900 | Params: []string{dc.nick, dc.srv.Hostname, "soju", "aiwroO", "OovaimnqpsrtklbeI"},
|
---|
[54] | 901 | })
|
---|
[446] | 902 | // TODO: other RPL_ISUPPORT tokens
|
---|
[55] | 903 | dc.SendMessage(&irc.Message{
|
---|
[446] | 904 | Prefix: dc.srv.prefix(),
|
---|
| 905 | Command: irc.RPL_ISUPPORT,
|
---|
| 906 | Params: append(append([]string{dc.nick}, isupport...), "are supported"),
|
---|
| 907 | })
|
---|
| 908 | dc.SendMessage(&irc.Message{
|
---|
[55] | 909 | Prefix: dc.srv.prefix(),
|
---|
[13] | 910 | Command: irc.ERR_NOMOTD,
|
---|
[55] | 911 | Params: []string{dc.nick, "No MOTD"},
|
---|
[54] | 912 | })
|
---|
[13] | 913 |
|
---|
[296] | 914 | dc.updateNick()
|
---|
[437] | 915 | dc.updateSupportedCaps()
|
---|
[296] | 916 |
|
---|
[73] | 917 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[30] | 918 | for _, ch := range uc.channels {
|
---|
[284] | 919 | if !ch.complete {
|
---|
| 920 | continue
|
---|
| 921 | }
|
---|
| 922 | if record, ok := uc.network.channels[ch.Name]; ok && record.Detached {
|
---|
| 923 | continue
|
---|
| 924 | }
|
---|
[132] | 925 |
|
---|
[284] | 926 | dc.SendMessage(&irc.Message{
|
---|
| 927 | Prefix: dc.prefix(),
|
---|
| 928 | Command: "JOIN",
|
---|
| 929 | Params: []string{dc.marshalEntity(ch.conn.network, ch.Name)},
|
---|
| 930 | })
|
---|
| 931 |
|
---|
| 932 | forwardChannel(dc, ch)
|
---|
[30] | 933 | }
|
---|
[143] | 934 | })
|
---|
[50] | 935 |
|
---|
[143] | 936 | dc.forEachNetwork(func(net *network) {
|
---|
[253] | 937 | // Only send history if we're the first connected client with that name
|
---|
| 938 | // for the network
|
---|
| 939 | if _, ok := net.offlineClients[dc.clientName]; ok {
|
---|
| 940 | dc.sendNetworkHistory(net)
|
---|
| 941 | delete(net.offlineClients, dc.clientName)
|
---|
[227] | 942 | }
|
---|
[409] | 943 |
|
---|
| 944 | // Fast-forward history to last message
|
---|
| 945 | for target, history := range net.history {
|
---|
| 946 | if ch, ok := net.channels[target]; ok && ch.Detached {
|
---|
| 947 | continue
|
---|
| 948 | }
|
---|
| 949 |
|
---|
[423] | 950 | lastID, err := dc.user.msgStore.LastMsgID(net, target, time.Now())
|
---|
[409] | 951 | if err != nil {
|
---|
| 952 | dc.logger.Printf("failed to get last message ID: %v", err)
|
---|
| 953 | continue
|
---|
| 954 | }
|
---|
| 955 | history.clients[dc.clientName] = lastID
|
---|
| 956 | }
|
---|
[253] | 957 | })
|
---|
[57] | 958 |
|
---|
[253] | 959 | return nil
|
---|
| 960 | }
|
---|
[144] | 961 |
|
---|
[428] | 962 | // messageSupportsHistory checks whether the provided message can be sent as
|
---|
| 963 | // part of an history batch.
|
---|
| 964 | func (dc *downstreamConn) messageSupportsHistory(msg *irc.Message) bool {
|
---|
| 965 | // Don't replay all messages, because that would mess up client
|
---|
| 966 | // state. For instance we just sent the list of users, sending
|
---|
| 967 | // PART messages for one of these users would be incorrect.
|
---|
| 968 | // TODO: add support for draft/event-playback
|
---|
| 969 | switch msg.Command {
|
---|
| 970 | case "PRIVMSG", "NOTICE":
|
---|
| 971 | return true
|
---|
| 972 | }
|
---|
| 973 | return false
|
---|
| 974 | }
|
---|
| 975 |
|
---|
[253] | 976 | func (dc *downstreamConn) sendNetworkHistory(net *network) {
|
---|
[423] | 977 | if dc.caps["draft/chathistory"] || dc.user.msgStore == nil {
|
---|
[319] | 978 | return
|
---|
| 979 | }
|
---|
[253] | 980 | for target, history := range net.history {
|
---|
[284] | 981 | if ch, ok := net.channels[target]; ok && ch.Detached {
|
---|
| 982 | continue
|
---|
| 983 | }
|
---|
| 984 |
|
---|
[409] | 985 | lastDelivered, ok := history.clients[dc.clientName]
|
---|
[253] | 986 | if !ok {
|
---|
| 987 | continue
|
---|
| 988 | }
|
---|
| 989 |
|
---|
[409] | 990 | limit := 4000
|
---|
[423] | 991 | history, err := dc.user.msgStore.LoadLatestID(net, target, lastDelivered, limit)
|
---|
[409] | 992 | if err != nil {
|
---|
| 993 | dc.logger.Printf("failed to send implicit history for %q: %v", target, err)
|
---|
| 994 | continue
|
---|
| 995 | }
|
---|
[253] | 996 |
|
---|
[256] | 997 | batchRef := "history"
|
---|
| 998 | if dc.caps["batch"] {
|
---|
| 999 | dc.SendMessage(&irc.Message{
|
---|
| 1000 | Prefix: dc.srv.prefix(),
|
---|
| 1001 | Command: "BATCH",
|
---|
[260] | 1002 | Params: []string{"+" + batchRef, "chathistory", dc.marshalEntity(net, target)},
|
---|
[256] | 1003 | })
|
---|
| 1004 | }
|
---|
| 1005 |
|
---|
[409] | 1006 | for _, msg := range history {
|
---|
[428] | 1007 | if !dc.messageSupportsHistory(msg) {
|
---|
[245] | 1008 | continue
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
[256] | 1011 | if dc.caps["batch"] {
|
---|
| 1012 | msg.Tags["batch"] = irc.TagValue(batchRef)
|
---|
| 1013 | }
|
---|
[261] | 1014 | dc.SendMessage(dc.marshalMessage(msg, net))
|
---|
[227] | 1015 | }
|
---|
[256] | 1016 |
|
---|
| 1017 | if dc.caps["batch"] {
|
---|
| 1018 | dc.SendMessage(&irc.Message{
|
---|
| 1019 | Prefix: dc.srv.prefix(),
|
---|
| 1020 | Command: "BATCH",
|
---|
| 1021 | Params: []string{"-" + batchRef},
|
---|
| 1022 | })
|
---|
| 1023 | }
|
---|
[253] | 1024 | }
|
---|
[13] | 1025 | }
|
---|
| 1026 |
|
---|
[103] | 1027 | func (dc *downstreamConn) runUntilRegistered() error {
|
---|
| 1028 | for !dc.registered {
|
---|
[212] | 1029 | msg, err := dc.ReadMessage()
|
---|
[106] | 1030 | if err != nil {
|
---|
[103] | 1031 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
| 1032 | }
|
---|
| 1033 |
|
---|
| 1034 | err = dc.handleMessage(msg)
|
---|
| 1035 | if ircErr, ok := err.(ircError); ok {
|
---|
| 1036 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
| 1037 | dc.SendMessage(ircErr.Message)
|
---|
| 1038 | } else if err != nil {
|
---|
| 1039 | return fmt.Errorf("failed to handle IRC command %q: %v", msg, err)
|
---|
| 1040 | }
|
---|
| 1041 | }
|
---|
| 1042 |
|
---|
| 1043 | return nil
|
---|
| 1044 | }
|
---|
| 1045 |
|
---|
[55] | 1046 | func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
|
---|
[13] | 1047 | switch msg.Command {
|
---|
[111] | 1048 | case "CAP":
|
---|
| 1049 | var subCmd string
|
---|
| 1050 | if err := parseMessageParams(msg, &subCmd); err != nil {
|
---|
| 1051 | return err
|
---|
| 1052 | }
|
---|
| 1053 | if err := dc.handleCapCommand(subCmd, msg.Params[1:]); err != nil {
|
---|
| 1054 | return err
|
---|
| 1055 | }
|
---|
[107] | 1056 | case "PING":
|
---|
[412] | 1057 | var source, destination string
|
---|
| 1058 | if err := parseMessageParams(msg, &source); err != nil {
|
---|
| 1059 | return err
|
---|
| 1060 | }
|
---|
| 1061 | if len(msg.Params) > 1 {
|
---|
| 1062 | destination = msg.Params[1]
|
---|
| 1063 | }
|
---|
| 1064 | if destination != "" && destination != dc.srv.Hostname {
|
---|
| 1065 | return ircError{&irc.Message{
|
---|
| 1066 | Command: irc.ERR_NOSUCHSERVER,
|
---|
[413] | 1067 | Params: []string{dc.nick, destination, "No such server"},
|
---|
[412] | 1068 | }}
|
---|
| 1069 | }
|
---|
[107] | 1070 | dc.SendMessage(&irc.Message{
|
---|
| 1071 | Prefix: dc.srv.prefix(),
|
---|
| 1072 | Command: "PONG",
|
---|
[412] | 1073 | Params: []string{dc.srv.Hostname, source},
|
---|
[107] | 1074 | })
|
---|
| 1075 | return nil
|
---|
[428] | 1076 | case "PONG":
|
---|
| 1077 | if len(msg.Params) == 0 {
|
---|
| 1078 | return newNeedMoreParamsError(msg.Command)
|
---|
| 1079 | }
|
---|
| 1080 | token := msg.Params[len(msg.Params)-1]
|
---|
| 1081 | dc.handlePong(token)
|
---|
[42] | 1082 | case "USER":
|
---|
[13] | 1083 | return ircError{&irc.Message{
|
---|
| 1084 | Command: irc.ERR_ALREADYREGISTERED,
|
---|
[55] | 1085 | Params: []string{dc.nick, "You may not reregister"},
|
---|
[13] | 1086 | }}
|
---|
[42] | 1087 | case "NICK":
|
---|
[429] | 1088 | var rawNick string
|
---|
| 1089 | if err := parseMessageParams(msg, &rawNick); err != nil {
|
---|
[90] | 1090 | return err
|
---|
| 1091 | }
|
---|
| 1092 |
|
---|
[429] | 1093 | nick := rawNick
|
---|
[297] | 1094 | var upstream *upstreamConn
|
---|
| 1095 | if dc.upstream() == nil {
|
---|
| 1096 | uc, unmarshaledNick, err := dc.unmarshalEntity(nick)
|
---|
| 1097 | if err == nil { // NICK nick/network: NICK only on a specific upstream
|
---|
| 1098 | upstream = uc
|
---|
| 1099 | nick = unmarshaledNick
|
---|
| 1100 | }
|
---|
| 1101 | }
|
---|
| 1102 |
|
---|
[404] | 1103 | if strings.ContainsAny(nick, illegalNickChars) {
|
---|
| 1104 | return ircError{&irc.Message{
|
---|
| 1105 | Command: irc.ERR_ERRONEUSNICKNAME,
|
---|
[430] | 1106 | Params: []string{dc.nick, rawNick, "contains illegal characters"},
|
---|
[404] | 1107 | }}
|
---|
| 1108 | }
|
---|
[429] | 1109 | if nick == serviceNick {
|
---|
| 1110 | return ircError{&irc.Message{
|
---|
| 1111 | Command: irc.ERR_NICKNAMEINUSE,
|
---|
| 1112 | Params: []string{dc.nick, rawNick, "Nickname reserved for bouncer service"},
|
---|
| 1113 | }}
|
---|
| 1114 | }
|
---|
[404] | 1115 |
|
---|
[90] | 1116 | var err error
|
---|
| 1117 | dc.forEachNetwork(func(n *network) {
|
---|
[297] | 1118 | if err != nil || (upstream != nil && upstream.network != n) {
|
---|
[90] | 1119 | return
|
---|
| 1120 | }
|
---|
| 1121 | n.Nick = nick
|
---|
[421] | 1122 | err = dc.srv.db.StoreNetwork(dc.user.ID, &n.Network)
|
---|
[90] | 1123 | })
|
---|
| 1124 | if err != nil {
|
---|
| 1125 | return err
|
---|
| 1126 | }
|
---|
| 1127 |
|
---|
[73] | 1128 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[297] | 1129 | if upstream != nil && upstream != uc {
|
---|
| 1130 | return
|
---|
| 1131 | }
|
---|
[301] | 1132 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[297] | 1133 | Command: "NICK",
|
---|
| 1134 | Params: []string{nick},
|
---|
| 1135 | })
|
---|
[42] | 1136 | })
|
---|
[296] | 1137 |
|
---|
| 1138 | if dc.upstream() == nil && dc.nick != nick {
|
---|
| 1139 | dc.SendMessage(&irc.Message{
|
---|
| 1140 | Prefix: dc.prefix(),
|
---|
| 1141 | Command: "NICK",
|
---|
| 1142 | Params: []string{nick},
|
---|
| 1143 | })
|
---|
| 1144 | dc.nick = nick
|
---|
| 1145 | }
|
---|
[146] | 1146 | case "JOIN":
|
---|
| 1147 | var namesStr string
|
---|
| 1148 | if err := parseMessageParams(msg, &namesStr); err != nil {
|
---|
[48] | 1149 | return err
|
---|
| 1150 | }
|
---|
| 1151 |
|
---|
[146] | 1152 | var keys []string
|
---|
| 1153 | if len(msg.Params) > 1 {
|
---|
| 1154 | keys = strings.Split(msg.Params[1], ",")
|
---|
| 1155 | }
|
---|
| 1156 |
|
---|
| 1157 | for i, name := range strings.Split(namesStr, ",") {
|
---|
[145] | 1158 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1159 | if err != nil {
|
---|
[158] | 1160 | return err
|
---|
[145] | 1161 | }
|
---|
[48] | 1162 |
|
---|
[146] | 1163 | var key string
|
---|
| 1164 | if len(keys) > i {
|
---|
| 1165 | key = keys[i]
|
---|
| 1166 | }
|
---|
| 1167 |
|
---|
| 1168 | params := []string{upstreamName}
|
---|
| 1169 | if key != "" {
|
---|
| 1170 | params = append(params, key)
|
---|
| 1171 | }
|
---|
[301] | 1172 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[146] | 1173 | Command: "JOIN",
|
---|
| 1174 | Params: params,
|
---|
[145] | 1175 | })
|
---|
[89] | 1176 |
|
---|
[435] | 1177 | var ch *Channel
|
---|
| 1178 | var ok bool
|
---|
| 1179 | if ch, ok = uc.network.channels[upstreamName]; ok {
|
---|
[285] | 1180 | // Don't clear the channel key if there's one set
|
---|
| 1181 | // TODO: add a way to unset the channel key
|
---|
[435] | 1182 | if key != "" {
|
---|
| 1183 | ch.Key = key
|
---|
| 1184 | }
|
---|
| 1185 | uc.network.attach(ch)
|
---|
| 1186 | } else {
|
---|
| 1187 | ch = &Channel{
|
---|
| 1188 | Name: upstreamName,
|
---|
| 1189 | Key: key,
|
---|
| 1190 | }
|
---|
| 1191 | uc.network.channels[upstreamName] = ch
|
---|
[285] | 1192 | }
|
---|
[435] | 1193 | if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
|
---|
[222] | 1194 | dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
|
---|
[89] | 1195 | }
|
---|
| 1196 | }
|
---|
[146] | 1197 | case "PART":
|
---|
| 1198 | var namesStr string
|
---|
| 1199 | if err := parseMessageParams(msg, &namesStr); err != nil {
|
---|
| 1200 | return err
|
---|
| 1201 | }
|
---|
| 1202 |
|
---|
| 1203 | var reason string
|
---|
| 1204 | if len(msg.Params) > 1 {
|
---|
| 1205 | reason = msg.Params[1]
|
---|
| 1206 | }
|
---|
| 1207 |
|
---|
| 1208 | for _, name := range strings.Split(namesStr, ",") {
|
---|
| 1209 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1210 | if err != nil {
|
---|
[158] | 1211 | return err
|
---|
[146] | 1212 | }
|
---|
| 1213 |
|
---|
[284] | 1214 | if strings.EqualFold(reason, "detach") {
|
---|
[435] | 1215 | var ch *Channel
|
---|
| 1216 | var ok bool
|
---|
| 1217 | if ch, ok = uc.network.channels[upstreamName]; ok {
|
---|
| 1218 | uc.network.detach(ch)
|
---|
| 1219 | } else {
|
---|
| 1220 | ch = &Channel{
|
---|
| 1221 | Name: name,
|
---|
| 1222 | Detached: true,
|
---|
| 1223 | }
|
---|
| 1224 | uc.network.channels[upstreamName] = ch
|
---|
[284] | 1225 | }
|
---|
[435] | 1226 | if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
|
---|
| 1227 | dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
|
---|
| 1228 | }
|
---|
[284] | 1229 | } else {
|
---|
| 1230 | params := []string{upstreamName}
|
---|
| 1231 | if reason != "" {
|
---|
| 1232 | params = append(params, reason)
|
---|
| 1233 | }
|
---|
[301] | 1234 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[284] | 1235 | Command: "PART",
|
---|
| 1236 | Params: params,
|
---|
| 1237 | })
|
---|
[146] | 1238 |
|
---|
[284] | 1239 | if err := uc.network.deleteChannel(upstreamName); err != nil {
|
---|
| 1240 | dc.logger.Printf("failed to delete channel %q: %v", upstreamName, err)
|
---|
| 1241 | }
|
---|
[146] | 1242 | }
|
---|
| 1243 | }
|
---|
[159] | 1244 | case "KICK":
|
---|
| 1245 | var channelStr, userStr string
|
---|
| 1246 | if err := parseMessageParams(msg, &channelStr, &userStr); err != nil {
|
---|
| 1247 | return err
|
---|
| 1248 | }
|
---|
| 1249 |
|
---|
| 1250 | channels := strings.Split(channelStr, ",")
|
---|
| 1251 | users := strings.Split(userStr, ",")
|
---|
| 1252 |
|
---|
| 1253 | var reason string
|
---|
| 1254 | if len(msg.Params) > 2 {
|
---|
| 1255 | reason = msg.Params[2]
|
---|
| 1256 | }
|
---|
| 1257 |
|
---|
| 1258 | if len(channels) != 1 && len(channels) != len(users) {
|
---|
| 1259 | return ircError{&irc.Message{
|
---|
| 1260 | Command: irc.ERR_BADCHANMASK,
|
---|
| 1261 | Params: []string{dc.nick, channelStr, "Bad channel mask"},
|
---|
| 1262 | }}
|
---|
| 1263 | }
|
---|
| 1264 |
|
---|
| 1265 | for i, user := range users {
|
---|
| 1266 | var channel string
|
---|
| 1267 | if len(channels) == 1 {
|
---|
| 1268 | channel = channels[0]
|
---|
| 1269 | } else {
|
---|
| 1270 | channel = channels[i]
|
---|
| 1271 | }
|
---|
| 1272 |
|
---|
| 1273 | ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1274 | if err != nil {
|
---|
| 1275 | return err
|
---|
| 1276 | }
|
---|
| 1277 |
|
---|
| 1278 | ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
---|
| 1279 | if err != nil {
|
---|
| 1280 | return err
|
---|
| 1281 | }
|
---|
| 1282 |
|
---|
| 1283 | if ucChannel != ucUser {
|
---|
| 1284 | return ircError{&irc.Message{
|
---|
| 1285 | Command: irc.ERR_USERNOTINCHANNEL,
|
---|
[400] | 1286 | Params: []string{dc.nick, user, channel, "They are on another network"},
|
---|
[159] | 1287 | }}
|
---|
| 1288 | }
|
---|
| 1289 | uc := ucChannel
|
---|
| 1290 |
|
---|
| 1291 | params := []string{upstreamChannel, upstreamUser}
|
---|
| 1292 | if reason != "" {
|
---|
| 1293 | params = append(params, reason)
|
---|
| 1294 | }
|
---|
[301] | 1295 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[159] | 1296 | Command: "KICK",
|
---|
| 1297 | Params: params,
|
---|
| 1298 | })
|
---|
| 1299 | }
|
---|
[69] | 1300 | case "MODE":
|
---|
[46] | 1301 | var name string
|
---|
| 1302 | if err := parseMessageParams(msg, &name); err != nil {
|
---|
| 1303 | return err
|
---|
| 1304 | }
|
---|
| 1305 |
|
---|
| 1306 | var modeStr string
|
---|
| 1307 | if len(msg.Params) > 1 {
|
---|
| 1308 | modeStr = msg.Params[1]
|
---|
| 1309 | }
|
---|
| 1310 |
|
---|
[139] | 1311 | if name == dc.nick {
|
---|
[46] | 1312 | if modeStr != "" {
|
---|
[73] | 1313 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[301] | 1314 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[69] | 1315 | Command: "MODE",
|
---|
| 1316 | Params: []string{uc.nick, modeStr},
|
---|
| 1317 | })
|
---|
[46] | 1318 | })
|
---|
| 1319 | } else {
|
---|
[55] | 1320 | dc.SendMessage(&irc.Message{
|
---|
| 1321 | Prefix: dc.srv.prefix(),
|
---|
[46] | 1322 | Command: irc.RPL_UMODEIS,
|
---|
[129] | 1323 | Params: []string{dc.nick, ""}, // TODO
|
---|
[54] | 1324 | })
|
---|
[46] | 1325 | }
|
---|
[139] | 1326 | return nil
|
---|
[46] | 1327 | }
|
---|
[139] | 1328 |
|
---|
| 1329 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1330 | if err != nil {
|
---|
| 1331 | return err
|
---|
| 1332 | }
|
---|
| 1333 |
|
---|
| 1334 | if !uc.isChannel(upstreamName) {
|
---|
| 1335 | return ircError{&irc.Message{
|
---|
| 1336 | Command: irc.ERR_USERSDONTMATCH,
|
---|
| 1337 | Params: []string{dc.nick, "Cannot change mode for other users"},
|
---|
| 1338 | }}
|
---|
| 1339 | }
|
---|
| 1340 |
|
---|
| 1341 | if modeStr != "" {
|
---|
| 1342 | params := []string{upstreamName, modeStr}
|
---|
| 1343 | params = append(params, msg.Params[2:]...)
|
---|
[301] | 1344 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[139] | 1345 | Command: "MODE",
|
---|
| 1346 | Params: params,
|
---|
| 1347 | })
|
---|
| 1348 | } else {
|
---|
| 1349 | ch, ok := uc.channels[upstreamName]
|
---|
| 1350 | if !ok {
|
---|
| 1351 | return ircError{&irc.Message{
|
---|
| 1352 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
| 1353 | Params: []string{dc.nick, name, "No such channel"},
|
---|
| 1354 | }}
|
---|
| 1355 | }
|
---|
| 1356 |
|
---|
| 1357 | if ch.modes == nil {
|
---|
| 1358 | // we haven't received the initial RPL_CHANNELMODEIS yet
|
---|
| 1359 | // ignore the request, we will broadcast the modes later when we receive RPL_CHANNELMODEIS
|
---|
| 1360 | return nil
|
---|
| 1361 | }
|
---|
| 1362 |
|
---|
| 1363 | modeStr, modeParams := ch.modes.Format()
|
---|
| 1364 | params := []string{dc.nick, name, modeStr}
|
---|
| 1365 | params = append(params, modeParams...)
|
---|
| 1366 |
|
---|
| 1367 | dc.SendMessage(&irc.Message{
|
---|
| 1368 | Prefix: dc.srv.prefix(),
|
---|
| 1369 | Command: irc.RPL_CHANNELMODEIS,
|
---|
| 1370 | Params: params,
|
---|
| 1371 | })
|
---|
[162] | 1372 | if ch.creationTime != "" {
|
---|
| 1373 | dc.SendMessage(&irc.Message{
|
---|
| 1374 | Prefix: dc.srv.prefix(),
|
---|
| 1375 | Command: rpl_creationtime,
|
---|
| 1376 | Params: []string{dc.nick, name, ch.creationTime},
|
---|
| 1377 | })
|
---|
| 1378 | }
|
---|
[139] | 1379 | }
|
---|
[160] | 1380 | case "TOPIC":
|
---|
| 1381 | var channel string
|
---|
| 1382 | if err := parseMessageParams(msg, &channel); err != nil {
|
---|
| 1383 | return err
|
---|
| 1384 | }
|
---|
| 1385 |
|
---|
| 1386 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1387 | if err != nil {
|
---|
| 1388 | return err
|
---|
| 1389 | }
|
---|
| 1390 |
|
---|
| 1391 | if len(msg.Params) > 1 { // setting topic
|
---|
| 1392 | topic := msg.Params[1]
|
---|
[301] | 1393 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[160] | 1394 | Command: "TOPIC",
|
---|
| 1395 | Params: []string{upstreamChannel, topic},
|
---|
| 1396 | })
|
---|
| 1397 | } else { // getting topic
|
---|
| 1398 | ch, ok := uc.channels[upstreamChannel]
|
---|
| 1399 | if !ok {
|
---|
| 1400 | return ircError{&irc.Message{
|
---|
| 1401 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
| 1402 | Params: []string{dc.nick, upstreamChannel, "No such channel"},
|
---|
| 1403 | }}
|
---|
| 1404 | }
|
---|
| 1405 | sendTopic(dc, ch)
|
---|
| 1406 | }
|
---|
[177] | 1407 | case "LIST":
|
---|
| 1408 | // TODO: support ELIST when supported by all upstreams
|
---|
| 1409 |
|
---|
| 1410 | pl := pendingLIST{
|
---|
| 1411 | downstreamID: dc.id,
|
---|
| 1412 | pendingCommands: make(map[int64]*irc.Message),
|
---|
| 1413 | }
|
---|
[298] | 1414 | var upstream *upstreamConn
|
---|
[177] | 1415 | var upstreamChannels map[int64][]string
|
---|
| 1416 | if len(msg.Params) > 0 {
|
---|
[298] | 1417 | uc, upstreamMask, err := dc.unmarshalEntity(msg.Params[0])
|
---|
| 1418 | if err == nil && upstreamMask == "*" { // LIST */network: send LIST only to one network
|
---|
| 1419 | upstream = uc
|
---|
| 1420 | } else {
|
---|
| 1421 | upstreamChannels = make(map[int64][]string)
|
---|
| 1422 | channels := strings.Split(msg.Params[0], ",")
|
---|
| 1423 | for _, channel := range channels {
|
---|
| 1424 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1425 | if err != nil {
|
---|
| 1426 | return err
|
---|
| 1427 | }
|
---|
| 1428 | upstreamChannels[uc.network.ID] = append(upstreamChannels[uc.network.ID], upstreamChannel)
|
---|
[177] | 1429 | }
|
---|
| 1430 | }
|
---|
| 1431 | }
|
---|
| 1432 |
|
---|
| 1433 | dc.user.pendingLISTs = append(dc.user.pendingLISTs, pl)
|
---|
| 1434 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[298] | 1435 | if upstream != nil && upstream != uc {
|
---|
| 1436 | return
|
---|
| 1437 | }
|
---|
[177] | 1438 | var params []string
|
---|
| 1439 | if upstreamChannels != nil {
|
---|
| 1440 | if channels, ok := upstreamChannels[uc.network.ID]; ok {
|
---|
| 1441 | params = []string{strings.Join(channels, ",")}
|
---|
| 1442 | } else {
|
---|
| 1443 | return
|
---|
| 1444 | }
|
---|
| 1445 | }
|
---|
| 1446 | pl.pendingCommands[uc.network.ID] = &irc.Message{
|
---|
| 1447 | Command: "LIST",
|
---|
| 1448 | Params: params,
|
---|
| 1449 | }
|
---|
[181] | 1450 | uc.trySendLIST(dc.id)
|
---|
[177] | 1451 | })
|
---|
[140] | 1452 | case "NAMES":
|
---|
| 1453 | if len(msg.Params) == 0 {
|
---|
| 1454 | dc.SendMessage(&irc.Message{
|
---|
| 1455 | Prefix: dc.srv.prefix(),
|
---|
| 1456 | Command: irc.RPL_ENDOFNAMES,
|
---|
| 1457 | Params: []string{dc.nick, "*", "End of /NAMES list"},
|
---|
| 1458 | })
|
---|
| 1459 | return nil
|
---|
| 1460 | }
|
---|
| 1461 |
|
---|
| 1462 | channels := strings.Split(msg.Params[0], ",")
|
---|
| 1463 | for _, channel := range channels {
|
---|
| 1464 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1465 | if err != nil {
|
---|
| 1466 | return err
|
---|
| 1467 | }
|
---|
| 1468 |
|
---|
| 1469 | ch, ok := uc.channels[upstreamChannel]
|
---|
| 1470 | if ok {
|
---|
| 1471 | sendNames(dc, ch)
|
---|
| 1472 | } else {
|
---|
| 1473 | // NAMES on a channel we have not joined, ask upstream
|
---|
[176] | 1474 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[140] | 1475 | Command: "NAMES",
|
---|
| 1476 | Params: []string{upstreamChannel},
|
---|
| 1477 | })
|
---|
| 1478 | }
|
---|
| 1479 | }
|
---|
[127] | 1480 | case "WHO":
|
---|
| 1481 | if len(msg.Params) == 0 {
|
---|
| 1482 | // TODO: support WHO without parameters
|
---|
| 1483 | dc.SendMessage(&irc.Message{
|
---|
| 1484 | Prefix: dc.srv.prefix(),
|
---|
| 1485 | Command: irc.RPL_ENDOFWHO,
|
---|
[140] | 1486 | Params: []string{dc.nick, "*", "End of /WHO list"},
|
---|
[127] | 1487 | })
|
---|
| 1488 | return nil
|
---|
| 1489 | }
|
---|
| 1490 |
|
---|
| 1491 | // TODO: support WHO masks
|
---|
| 1492 | entity := msg.Params[0]
|
---|
| 1493 |
|
---|
[142] | 1494 | if entity == dc.nick {
|
---|
| 1495 | // TODO: support AWAY (H/G) in self WHO reply
|
---|
| 1496 | dc.SendMessage(&irc.Message{
|
---|
| 1497 | Prefix: dc.srv.prefix(),
|
---|
| 1498 | Command: irc.RPL_WHOREPLY,
|
---|
[184] | 1499 | Params: []string{dc.nick, "*", dc.user.Username, dc.hostname, dc.srv.Hostname, dc.nick, "H", "0 " + dc.realname},
|
---|
[142] | 1500 | })
|
---|
| 1501 | dc.SendMessage(&irc.Message{
|
---|
| 1502 | Prefix: dc.srv.prefix(),
|
---|
| 1503 | Command: irc.RPL_ENDOFWHO,
|
---|
| 1504 | Params: []string{dc.nick, dc.nick, "End of /WHO list"},
|
---|
| 1505 | })
|
---|
| 1506 | return nil
|
---|
| 1507 | }
|
---|
[343] | 1508 | if entity == serviceNick {
|
---|
| 1509 | dc.SendMessage(&irc.Message{
|
---|
| 1510 | Prefix: dc.srv.prefix(),
|
---|
| 1511 | Command: irc.RPL_WHOREPLY,
|
---|
| 1512 | Params: []string{serviceNick, "*", servicePrefix.User, servicePrefix.Host, dc.srv.Hostname, serviceNick, "H", "0 " + serviceRealname},
|
---|
| 1513 | })
|
---|
| 1514 | dc.SendMessage(&irc.Message{
|
---|
| 1515 | Prefix: dc.srv.prefix(),
|
---|
| 1516 | Command: irc.RPL_ENDOFWHO,
|
---|
| 1517 | Params: []string{dc.nick, serviceNick, "End of /WHO list"},
|
---|
| 1518 | })
|
---|
| 1519 | return nil
|
---|
| 1520 | }
|
---|
[142] | 1521 |
|
---|
[127] | 1522 | uc, upstreamName, err := dc.unmarshalEntity(entity)
|
---|
| 1523 | if err != nil {
|
---|
| 1524 | return err
|
---|
| 1525 | }
|
---|
| 1526 |
|
---|
| 1527 | var params []string
|
---|
| 1528 | if len(msg.Params) == 2 {
|
---|
| 1529 | params = []string{upstreamName, msg.Params[1]}
|
---|
| 1530 | } else {
|
---|
| 1531 | params = []string{upstreamName}
|
---|
| 1532 | }
|
---|
| 1533 |
|
---|
[176] | 1534 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[127] | 1535 | Command: "WHO",
|
---|
| 1536 | Params: params,
|
---|
| 1537 | })
|
---|
[128] | 1538 | case "WHOIS":
|
---|
| 1539 | if len(msg.Params) == 0 {
|
---|
| 1540 | return ircError{&irc.Message{
|
---|
| 1541 | Command: irc.ERR_NONICKNAMEGIVEN,
|
---|
| 1542 | Params: []string{dc.nick, "No nickname given"},
|
---|
| 1543 | }}
|
---|
| 1544 | }
|
---|
| 1545 |
|
---|
| 1546 | var target, mask string
|
---|
| 1547 | if len(msg.Params) == 1 {
|
---|
| 1548 | target = ""
|
---|
| 1549 | mask = msg.Params[0]
|
---|
| 1550 | } else {
|
---|
| 1551 | target = msg.Params[0]
|
---|
| 1552 | mask = msg.Params[1]
|
---|
| 1553 | }
|
---|
| 1554 | // TODO: support multiple WHOIS users
|
---|
| 1555 | if i := strings.IndexByte(mask, ','); i >= 0 {
|
---|
| 1556 | mask = mask[:i]
|
---|
| 1557 | }
|
---|
| 1558 |
|
---|
[142] | 1559 | if mask == dc.nick {
|
---|
| 1560 | dc.SendMessage(&irc.Message{
|
---|
| 1561 | Prefix: dc.srv.prefix(),
|
---|
| 1562 | Command: irc.RPL_WHOISUSER,
|
---|
[184] | 1563 | Params: []string{dc.nick, dc.nick, dc.user.Username, dc.hostname, "*", dc.realname},
|
---|
[142] | 1564 | })
|
---|
| 1565 | dc.SendMessage(&irc.Message{
|
---|
| 1566 | Prefix: dc.srv.prefix(),
|
---|
| 1567 | Command: irc.RPL_WHOISSERVER,
|
---|
| 1568 | Params: []string{dc.nick, dc.nick, dc.srv.Hostname, "soju"},
|
---|
| 1569 | })
|
---|
| 1570 | dc.SendMessage(&irc.Message{
|
---|
| 1571 | Prefix: dc.srv.prefix(),
|
---|
| 1572 | Command: irc.RPL_ENDOFWHOIS,
|
---|
| 1573 | Params: []string{dc.nick, dc.nick, "End of /WHOIS list"},
|
---|
| 1574 | })
|
---|
| 1575 | return nil
|
---|
| 1576 | }
|
---|
| 1577 |
|
---|
[128] | 1578 | // TODO: support WHOIS masks
|
---|
| 1579 | uc, upstreamNick, err := dc.unmarshalEntity(mask)
|
---|
| 1580 | if err != nil {
|
---|
| 1581 | return err
|
---|
| 1582 | }
|
---|
| 1583 |
|
---|
| 1584 | var params []string
|
---|
| 1585 | if target != "" {
|
---|
[299] | 1586 | if target == mask { // WHOIS nick nick
|
---|
| 1587 | params = []string{upstreamNick, upstreamNick}
|
---|
| 1588 | } else {
|
---|
| 1589 | params = []string{target, upstreamNick}
|
---|
| 1590 | }
|
---|
[128] | 1591 | } else {
|
---|
| 1592 | params = []string{upstreamNick}
|
---|
| 1593 | }
|
---|
| 1594 |
|
---|
[176] | 1595 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[128] | 1596 | Command: "WHOIS",
|
---|
| 1597 | Params: params,
|
---|
| 1598 | })
|
---|
[58] | 1599 | case "PRIVMSG":
|
---|
| 1600 | var targetsStr, text string
|
---|
| 1601 | if err := parseMessageParams(msg, &targetsStr, &text); err != nil {
|
---|
| 1602 | return err
|
---|
| 1603 | }
|
---|
[303] | 1604 | tags := copyClientTags(msg.Tags)
|
---|
[58] | 1605 |
|
---|
| 1606 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
[117] | 1607 | if name == serviceNick {
|
---|
[431] | 1608 | if dc.caps["echo-message"] {
|
---|
| 1609 | echoTags := tags.Copy()
|
---|
| 1610 | echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
| 1611 | dc.SendMessage(&irc.Message{
|
---|
| 1612 | Tags: echoTags,
|
---|
| 1613 | Prefix: dc.prefix(),
|
---|
| 1614 | Command: "PRIVMSG",
|
---|
| 1615 | Params: []string{name, text},
|
---|
| 1616 | })
|
---|
| 1617 | }
|
---|
[117] | 1618 | handleServicePRIVMSG(dc, text)
|
---|
| 1619 | continue
|
---|
| 1620 | }
|
---|
| 1621 |
|
---|
[127] | 1622 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
[58] | 1623 | if err != nil {
|
---|
| 1624 | return err
|
---|
| 1625 | }
|
---|
| 1626 |
|
---|
[95] | 1627 | if upstreamName == "NickServ" {
|
---|
| 1628 | dc.handleNickServPRIVMSG(uc, text)
|
---|
| 1629 | }
|
---|
| 1630 |
|
---|
[268] | 1631 | unmarshaledText := text
|
---|
| 1632 | if uc.isChannel(upstreamName) {
|
---|
| 1633 | unmarshaledText = dc.unmarshalText(uc, text)
|
---|
| 1634 | }
|
---|
[301] | 1635 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[303] | 1636 | Tags: tags,
|
---|
[58] | 1637 | Command: "PRIVMSG",
|
---|
[268] | 1638 | Params: []string{upstreamName, unmarshaledText},
|
---|
[60] | 1639 | })
|
---|
[105] | 1640 |
|
---|
[303] | 1641 | echoTags := tags.Copy()
|
---|
| 1642 | echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
[113] | 1643 | echoMsg := &irc.Message{
|
---|
[303] | 1644 | Tags: echoTags,
|
---|
[113] | 1645 | Prefix: &irc.Prefix{
|
---|
| 1646 | Name: uc.nick,
|
---|
| 1647 | User: uc.username,
|
---|
| 1648 | },
|
---|
[114] | 1649 | Command: "PRIVMSG",
|
---|
[113] | 1650 | Params: []string{upstreamName, text},
|
---|
| 1651 | }
|
---|
[239] | 1652 | uc.produce(upstreamName, echoMsg, dc)
|
---|
[435] | 1653 |
|
---|
| 1654 | uc.updateChannelAutoDetach(upstreamName)
|
---|
[58] | 1655 | }
|
---|
[164] | 1656 | case "NOTICE":
|
---|
| 1657 | var targetsStr, text string
|
---|
| 1658 | if err := parseMessageParams(msg, &targetsStr, &text); err != nil {
|
---|
| 1659 | return err
|
---|
| 1660 | }
|
---|
[303] | 1661 | tags := copyClientTags(msg.Tags)
|
---|
[164] | 1662 |
|
---|
| 1663 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
| 1664 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1665 | if err != nil {
|
---|
| 1666 | return err
|
---|
| 1667 | }
|
---|
| 1668 |
|
---|
[268] | 1669 | unmarshaledText := text
|
---|
| 1670 | if uc.isChannel(upstreamName) {
|
---|
| 1671 | unmarshaledText = dc.unmarshalText(uc, text)
|
---|
| 1672 | }
|
---|
[301] | 1673 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[303] | 1674 | Tags: tags,
|
---|
[164] | 1675 | Command: "NOTICE",
|
---|
[268] | 1676 | Params: []string{upstreamName, unmarshaledText},
|
---|
[164] | 1677 | })
|
---|
[435] | 1678 |
|
---|
| 1679 | uc.updateChannelAutoDetach(upstreamName)
|
---|
[164] | 1680 | }
|
---|
[303] | 1681 | case "TAGMSG":
|
---|
| 1682 | var targetsStr string
|
---|
| 1683 | if err := parseMessageParams(msg, &targetsStr); err != nil {
|
---|
| 1684 | return err
|
---|
| 1685 | }
|
---|
| 1686 | tags := copyClientTags(msg.Tags)
|
---|
| 1687 |
|
---|
| 1688 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
| 1689 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1690 | if err != nil {
|
---|
| 1691 | return err
|
---|
| 1692 | }
|
---|
[427] | 1693 | if _, ok := uc.caps["message-tags"]; !ok {
|
---|
| 1694 | continue
|
---|
| 1695 | }
|
---|
[303] | 1696 |
|
---|
| 1697 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
| 1698 | Tags: tags,
|
---|
| 1699 | Command: "TAGMSG",
|
---|
| 1700 | Params: []string{upstreamName},
|
---|
| 1701 | })
|
---|
[435] | 1702 |
|
---|
| 1703 | uc.updateChannelAutoDetach(upstreamName)
|
---|
[303] | 1704 | }
|
---|
[163] | 1705 | case "INVITE":
|
---|
| 1706 | var user, channel string
|
---|
| 1707 | if err := parseMessageParams(msg, &user, &channel); err != nil {
|
---|
| 1708 | return err
|
---|
| 1709 | }
|
---|
| 1710 |
|
---|
| 1711 | ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1712 | if err != nil {
|
---|
| 1713 | return err
|
---|
| 1714 | }
|
---|
| 1715 |
|
---|
| 1716 | ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
---|
| 1717 | if err != nil {
|
---|
| 1718 | return err
|
---|
| 1719 | }
|
---|
| 1720 |
|
---|
| 1721 | if ucChannel != ucUser {
|
---|
| 1722 | return ircError{&irc.Message{
|
---|
| 1723 | Command: irc.ERR_USERNOTINCHANNEL,
|
---|
[401] | 1724 | Params: []string{dc.nick, user, channel, "They are on another network"},
|
---|
[163] | 1725 | }}
|
---|
| 1726 | }
|
---|
| 1727 | uc := ucChannel
|
---|
| 1728 |
|
---|
[176] | 1729 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[163] | 1730 | Command: "INVITE",
|
---|
| 1731 | Params: []string{upstreamUser, upstreamChannel},
|
---|
| 1732 | })
|
---|
[319] | 1733 | case "CHATHISTORY":
|
---|
| 1734 | var subcommand string
|
---|
| 1735 | if err := parseMessageParams(msg, &subcommand); err != nil {
|
---|
| 1736 | return err
|
---|
| 1737 | }
|
---|
| 1738 | var target, criteria, limitStr string
|
---|
| 1739 | if err := parseMessageParams(msg, nil, &target, &criteria, &limitStr); err != nil {
|
---|
| 1740 | return ircError{&irc.Message{
|
---|
| 1741 | Command: "FAIL",
|
---|
| 1742 | Params: []string{"CHATHISTORY", "NEED_MORE_PARAMS", subcommand, "Missing parameters"},
|
---|
| 1743 | }}
|
---|
| 1744 | }
|
---|
| 1745 |
|
---|
[441] | 1746 | store, ok := dc.user.msgStore.(chatHistoryMessageStore)
|
---|
| 1747 | if !ok {
|
---|
[319] | 1748 | return ircError{&irc.Message{
|
---|
| 1749 | Command: irc.ERR_UNKNOWNCOMMAND,
|
---|
| 1750 | Params: []string{dc.nick, subcommand, "Unknown command"},
|
---|
| 1751 | }}
|
---|
| 1752 | }
|
---|
| 1753 |
|
---|
| 1754 | uc, entity, err := dc.unmarshalEntity(target)
|
---|
| 1755 | if err != nil {
|
---|
| 1756 | return err
|
---|
| 1757 | }
|
---|
| 1758 |
|
---|
| 1759 | // TODO: support msgid criteria
|
---|
| 1760 | criteriaParts := strings.SplitN(criteria, "=", 2)
|
---|
| 1761 | if len(criteriaParts) != 2 || criteriaParts[0] != "timestamp" {
|
---|
| 1762 | return ircError{&irc.Message{
|
---|
| 1763 | Command: "FAIL",
|
---|
| 1764 | Params: []string{"CHATHISTORY", "UNKNOWN_CRITERIA", criteria, "Unknown criteria"},
|
---|
| 1765 | }}
|
---|
| 1766 | }
|
---|
| 1767 |
|
---|
| 1768 | timestamp, err := time.Parse(serverTimeLayout, criteriaParts[1])
|
---|
| 1769 | if err != nil {
|
---|
| 1770 | return ircError{&irc.Message{
|
---|
| 1771 | Command: "FAIL",
|
---|
| 1772 | Params: []string{"CHATHISTORY", "INVALID_CRITERIA", criteria, "Invalid criteria"},
|
---|
| 1773 | }}
|
---|
| 1774 | }
|
---|
| 1775 |
|
---|
| 1776 | limit, err := strconv.Atoi(limitStr)
|
---|
| 1777 | if err != nil || limit < 0 || limit > dc.srv.HistoryLimit {
|
---|
| 1778 | return ircError{&irc.Message{
|
---|
| 1779 | Command: "FAIL",
|
---|
| 1780 | Params: []string{"CHATHISTORY", "INVALID_LIMIT", limitStr, "Invalid limit"},
|
---|
| 1781 | }}
|
---|
| 1782 | }
|
---|
| 1783 |
|
---|
[387] | 1784 | var history []*irc.Message
|
---|
[319] | 1785 | switch subcommand {
|
---|
| 1786 | case "BEFORE":
|
---|
[441] | 1787 | history, err = store.LoadBeforeTime(uc.network, entity, timestamp, limit)
|
---|
[360] | 1788 | case "AFTER":
|
---|
[441] | 1789 | history, err = store.LoadAfterTime(uc.network, entity, timestamp, limit)
|
---|
[319] | 1790 | default:
|
---|
[360] | 1791 | // TODO: support LATEST, BETWEEN
|
---|
[319] | 1792 | return ircError{&irc.Message{
|
---|
| 1793 | Command: "FAIL",
|
---|
| 1794 | Params: []string{"CHATHISTORY", "UNKNOWN_COMMAND", subcommand, "Unknown command"},
|
---|
| 1795 | }}
|
---|
| 1796 | }
|
---|
[387] | 1797 | if err != nil {
|
---|
| 1798 | dc.logger.Printf("failed parsing log messages for chathistory: %v", err)
|
---|
| 1799 | return newChatHistoryError(subcommand, target)
|
---|
| 1800 | }
|
---|
| 1801 |
|
---|
| 1802 | batchRef := "history"
|
---|
| 1803 | dc.SendMessage(&irc.Message{
|
---|
| 1804 | Prefix: dc.srv.prefix(),
|
---|
| 1805 | Command: "BATCH",
|
---|
| 1806 | Params: []string{"+" + batchRef, "chathistory", target},
|
---|
| 1807 | })
|
---|
| 1808 |
|
---|
| 1809 | for _, msg := range history {
|
---|
| 1810 | msg.Tags["batch"] = irc.TagValue(batchRef)
|
---|
| 1811 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
| 1812 | }
|
---|
| 1813 |
|
---|
| 1814 | dc.SendMessage(&irc.Message{
|
---|
| 1815 | Prefix: dc.srv.prefix(),
|
---|
| 1816 | Command: "BATCH",
|
---|
| 1817 | Params: []string{"-" + batchRef},
|
---|
| 1818 | })
|
---|
[13] | 1819 | default:
|
---|
[55] | 1820 | dc.logger.Printf("unhandled message: %v", msg)
|
---|
[13] | 1821 | return newUnknownCommandError(msg.Command)
|
---|
| 1822 | }
|
---|
[42] | 1823 | return nil
|
---|
[13] | 1824 | }
|
---|
[95] | 1825 |
|
---|
| 1826 | func (dc *downstreamConn) handleNickServPRIVMSG(uc *upstreamConn, text string) {
|
---|
| 1827 | username, password, ok := parseNickServCredentials(text, uc.nick)
|
---|
| 1828 | if !ok {
|
---|
| 1829 | return
|
---|
| 1830 | }
|
---|
| 1831 |
|
---|
[307] | 1832 | // User may have e.g. EXTERNAL mechanism configured. We do not want to
|
---|
| 1833 | // automatically erase the key pair or any other credentials.
|
---|
| 1834 | if uc.network.SASL.Mechanism != "" && uc.network.SASL.Mechanism != "PLAIN" {
|
---|
| 1835 | return
|
---|
| 1836 | }
|
---|
| 1837 |
|
---|
[95] | 1838 | dc.logger.Printf("auto-saving NickServ credentials with username %q", username)
|
---|
| 1839 | n := uc.network
|
---|
| 1840 | n.SASL.Mechanism = "PLAIN"
|
---|
| 1841 | n.SASL.Plain.Username = username
|
---|
| 1842 | n.SASL.Plain.Password = password
|
---|
[421] | 1843 | if err := dc.srv.db.StoreNetwork(dc.user.ID, &n.Network); err != nil {
|
---|
[95] | 1844 | dc.logger.Printf("failed to save NickServ credentials: %v", err)
|
---|
| 1845 | }
|
---|
| 1846 | }
|
---|
| 1847 |
|
---|
| 1848 | func parseNickServCredentials(text, nick string) (username, password string, ok bool) {
|
---|
| 1849 | fields := strings.Fields(text)
|
---|
| 1850 | if len(fields) < 2 {
|
---|
| 1851 | return "", "", false
|
---|
| 1852 | }
|
---|
| 1853 | cmd := strings.ToUpper(fields[0])
|
---|
| 1854 | params := fields[1:]
|
---|
| 1855 | switch cmd {
|
---|
| 1856 | case "REGISTER":
|
---|
| 1857 | username = nick
|
---|
| 1858 | password = params[0]
|
---|
| 1859 | case "IDENTIFY":
|
---|
| 1860 | if len(params) == 1 {
|
---|
| 1861 | username = nick
|
---|
[182] | 1862 | password = params[0]
|
---|
[95] | 1863 | } else {
|
---|
| 1864 | username = params[0]
|
---|
[182] | 1865 | password = params[1]
|
---|
[95] | 1866 | }
|
---|
[182] | 1867 | case "SET":
|
---|
| 1868 | if len(params) == 2 && strings.EqualFold(params[0], "PASSWORD") {
|
---|
| 1869 | username = nick
|
---|
| 1870 | password = params[1]
|
---|
| 1871 | }
|
---|
[340] | 1872 | default:
|
---|
| 1873 | return "", "", false
|
---|
[95] | 1874 | }
|
---|
| 1875 | return username, password, true
|
---|
| 1876 | }
|
---|