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