[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) {
|
---|
| 316 | netName, entity, _, _, err := parseMsgID(id)
|
---|
| 317 | if err != nil {
|
---|
| 318 | dc.logger.Printf("failed to ACK message ID %q: %v", id, err)
|
---|
| 319 | return
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | network := dc.user.getNetwork(netName)
|
---|
| 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 | }
|
---|
| 558 |
|
---|
[275] | 559 | caps := make([]string, 0, len(dc.supportedCaps))
|
---|
| 560 | for k, v := range dc.supportedCaps {
|
---|
| 561 | if dc.capVersion >= 302 && v != "" {
|
---|
[276] | 562 | caps = append(caps, k+"="+v)
|
---|
[275] | 563 | } else {
|
---|
| 564 | caps = append(caps, k)
|
---|
| 565 | }
|
---|
[112] | 566 | }
|
---|
[108] | 567 |
|
---|
| 568 | // TODO: multi-line replies
|
---|
| 569 | dc.SendMessage(&irc.Message{
|
---|
| 570 | Prefix: dc.srv.prefix(),
|
---|
| 571 | Command: "CAP",
|
---|
| 572 | Params: []string{replyTo, "LS", strings.Join(caps, " ")},
|
---|
| 573 | })
|
---|
| 574 |
|
---|
[275] | 575 | if dc.capVersion >= 302 {
|
---|
| 576 | // CAP version 302 implicitly enables cap-notify
|
---|
| 577 | dc.caps["cap-notify"] = true
|
---|
| 578 | }
|
---|
| 579 |
|
---|
[108] | 580 | if !dc.registered {
|
---|
| 581 | dc.negociatingCaps = true
|
---|
| 582 | }
|
---|
| 583 | case "LIST":
|
---|
| 584 | var caps []string
|
---|
| 585 | for name := range dc.caps {
|
---|
| 586 | caps = append(caps, name)
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 | // TODO: multi-line replies
|
---|
| 590 | dc.SendMessage(&irc.Message{
|
---|
| 591 | Prefix: dc.srv.prefix(),
|
---|
| 592 | Command: "CAP",
|
---|
| 593 | Params: []string{replyTo, "LIST", strings.Join(caps, " ")},
|
---|
| 594 | })
|
---|
| 595 | case "REQ":
|
---|
| 596 | if len(args) == 0 {
|
---|
| 597 | return ircError{&irc.Message{
|
---|
| 598 | Command: err_invalidcapcmd,
|
---|
| 599 | Params: []string{replyTo, cmd, "Missing argument in CAP REQ command"},
|
---|
| 600 | }}
|
---|
| 601 | }
|
---|
| 602 |
|
---|
[275] | 603 | // TODO: atomically ack/nak the whole capability set
|
---|
[108] | 604 | caps := strings.Fields(args[0])
|
---|
| 605 | ack := true
|
---|
| 606 | for _, name := range caps {
|
---|
| 607 | name = strings.ToLower(name)
|
---|
| 608 | enable := !strings.HasPrefix(name, "-")
|
---|
| 609 | if !enable {
|
---|
| 610 | name = strings.TrimPrefix(name, "-")
|
---|
| 611 | }
|
---|
| 612 |
|
---|
[275] | 613 | if enable == dc.caps[name] {
|
---|
[108] | 614 | continue
|
---|
| 615 | }
|
---|
| 616 |
|
---|
[275] | 617 | _, ok := dc.supportedCaps[name]
|
---|
| 618 | if !ok {
|
---|
[108] | 619 | ack = false
|
---|
[275] | 620 | break
|
---|
[108] | 621 | }
|
---|
[275] | 622 |
|
---|
| 623 | if name == "cap-notify" && dc.capVersion >= 302 && !enable {
|
---|
| 624 | // cap-notify cannot be disabled with CAP version 302
|
---|
| 625 | ack = false
|
---|
| 626 | break
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | dc.caps[name] = enable
|
---|
[108] | 630 | }
|
---|
| 631 |
|
---|
| 632 | reply := "NAK"
|
---|
| 633 | if ack {
|
---|
| 634 | reply = "ACK"
|
---|
| 635 | }
|
---|
| 636 | dc.SendMessage(&irc.Message{
|
---|
| 637 | Prefix: dc.srv.prefix(),
|
---|
| 638 | Command: "CAP",
|
---|
| 639 | Params: []string{replyTo, reply, args[0]},
|
---|
| 640 | })
|
---|
| 641 | case "END":
|
---|
| 642 | dc.negociatingCaps = false
|
---|
| 643 | default:
|
---|
| 644 | return ircError{&irc.Message{
|
---|
| 645 | Command: err_invalidcapcmd,
|
---|
| 646 | Params: []string{replyTo, cmd, "Unknown CAP command"},
|
---|
| 647 | }}
|
---|
| 648 | }
|
---|
| 649 | return nil
|
---|
| 650 | }
|
---|
| 651 |
|
---|
[275] | 652 | func (dc *downstreamConn) setSupportedCap(name, value string) {
|
---|
| 653 | prevValue, hasPrev := dc.supportedCaps[name]
|
---|
| 654 | changed := !hasPrev || prevValue != value
|
---|
| 655 | dc.supportedCaps[name] = value
|
---|
| 656 |
|
---|
| 657 | if !dc.caps["cap-notify"] || !changed {
|
---|
| 658 | return
|
---|
| 659 | }
|
---|
| 660 |
|
---|
| 661 | replyTo := dc.nick
|
---|
| 662 | if !dc.registered {
|
---|
| 663 | replyTo = "*"
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | cap := name
|
---|
| 667 | if value != "" && dc.capVersion >= 302 {
|
---|
| 668 | cap = name + "=" + value
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | dc.SendMessage(&irc.Message{
|
---|
| 672 | Prefix: dc.srv.prefix(),
|
---|
| 673 | Command: "CAP",
|
---|
| 674 | Params: []string{replyTo, "NEW", cap},
|
---|
| 675 | })
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | func (dc *downstreamConn) unsetSupportedCap(name string) {
|
---|
| 679 | _, hasPrev := dc.supportedCaps[name]
|
---|
| 680 | delete(dc.supportedCaps, name)
|
---|
| 681 | delete(dc.caps, name)
|
---|
| 682 |
|
---|
| 683 | if !dc.caps["cap-notify"] || !hasPrev {
|
---|
| 684 | return
|
---|
| 685 | }
|
---|
| 686 |
|
---|
| 687 | replyTo := dc.nick
|
---|
| 688 | if !dc.registered {
|
---|
| 689 | replyTo = "*"
|
---|
| 690 | }
|
---|
| 691 |
|
---|
| 692 | dc.SendMessage(&irc.Message{
|
---|
| 693 | Prefix: dc.srv.prefix(),
|
---|
| 694 | Command: "CAP",
|
---|
| 695 | Params: []string{replyTo, "DEL", name},
|
---|
| 696 | })
|
---|
| 697 | }
|
---|
| 698 |
|
---|
[276] | 699 | func (dc *downstreamConn) updateSupportedCaps() {
|
---|
[292] | 700 | supportedCaps := make(map[string]bool)
|
---|
| 701 | for cap := range needAllDownstreamCaps {
|
---|
| 702 | supportedCaps[cap] = true
|
---|
| 703 | }
|
---|
[276] | 704 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[292] | 705 | for cap, supported := range supportedCaps {
|
---|
| 706 | supportedCaps[cap] = supported && uc.caps[cap]
|
---|
| 707 | }
|
---|
[276] | 708 | })
|
---|
| 709 |
|
---|
[292] | 710 | for cap, supported := range supportedCaps {
|
---|
| 711 | if supported {
|
---|
| 712 | dc.setSupportedCap(cap, needAllDownstreamCaps[cap])
|
---|
| 713 | } else {
|
---|
| 714 | dc.unsetSupportedCap(cap)
|
---|
| 715 | }
|
---|
[276] | 716 | }
|
---|
| 717 | }
|
---|
| 718 |
|
---|
[296] | 719 | func (dc *downstreamConn) updateNick() {
|
---|
| 720 | if uc := dc.upstream(); uc != nil && uc.nick != dc.nick {
|
---|
| 721 | dc.SendMessage(&irc.Message{
|
---|
| 722 | Prefix: dc.prefix(),
|
---|
| 723 | Command: "NICK",
|
---|
| 724 | Params: []string{uc.nick},
|
---|
| 725 | })
|
---|
| 726 | dc.nick = uc.nick
|
---|
| 727 | }
|
---|
| 728 | }
|
---|
| 729 |
|
---|
[91] | 730 | func sanityCheckServer(addr string) error {
|
---|
| 731 | dialer := net.Dialer{Timeout: 30 * time.Second}
|
---|
| 732 | conn, err := tls.DialWithDialer(&dialer, "tcp", addr, nil)
|
---|
| 733 | if err != nil {
|
---|
| 734 | return err
|
---|
| 735 | }
|
---|
| 736 | return conn.Close()
|
---|
| 737 | }
|
---|
| 738 |
|
---|
[183] | 739 | func unmarshalUsername(rawUsername string) (username, client, network string) {
|
---|
[112] | 740 | username = rawUsername
|
---|
[183] | 741 |
|
---|
| 742 | i := strings.IndexAny(username, "/@")
|
---|
| 743 | j := strings.LastIndexAny(username, "/@")
|
---|
| 744 | if i >= 0 {
|
---|
| 745 | username = rawUsername[:i]
|
---|
[73] | 746 | }
|
---|
[183] | 747 | if j >= 0 {
|
---|
[190] | 748 | if rawUsername[j] == '@' {
|
---|
| 749 | client = rawUsername[j+1:]
|
---|
| 750 | } else {
|
---|
| 751 | network = rawUsername[j+1:]
|
---|
| 752 | }
|
---|
[73] | 753 | }
|
---|
[183] | 754 | if i >= 0 && j >= 0 && i < j {
|
---|
[190] | 755 | if rawUsername[i] == '@' {
|
---|
| 756 | client = rawUsername[i+1 : j]
|
---|
| 757 | } else {
|
---|
| 758 | network = rawUsername[i+1 : j]
|
---|
| 759 | }
|
---|
[183] | 760 | }
|
---|
| 761 |
|
---|
| 762 | return username, client, network
|
---|
[112] | 763 | }
|
---|
[73] | 764 |
|
---|
[168] | 765 | func (dc *downstreamConn) authenticate(username, password string) error {
|
---|
[183] | 766 | username, clientName, networkName := unmarshalUsername(username)
|
---|
[168] | 767 |
|
---|
[173] | 768 | u, err := dc.srv.db.GetUser(username)
|
---|
| 769 | if err != nil {
|
---|
| 770 | dc.logger.Printf("failed authentication for %q: %v", username, err)
|
---|
[168] | 771 | return errAuthFailed
|
---|
| 772 | }
|
---|
| 773 |
|
---|
[322] | 774 | // Password auth disabled
|
---|
| 775 | if u.Password == "" {
|
---|
| 776 | return errAuthFailed
|
---|
| 777 | }
|
---|
| 778 |
|
---|
[173] | 779 | err = bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
---|
[168] | 780 | if err != nil {
|
---|
| 781 | dc.logger.Printf("failed authentication for %q: %v", username, err)
|
---|
| 782 | return errAuthFailed
|
---|
| 783 | }
|
---|
| 784 |
|
---|
[173] | 785 | dc.user = dc.srv.getUser(username)
|
---|
| 786 | if dc.user == nil {
|
---|
| 787 | dc.logger.Printf("failed authentication for %q: user not active", username)
|
---|
| 788 | return errAuthFailed
|
---|
| 789 | }
|
---|
[183] | 790 | dc.clientName = clientName
|
---|
[168] | 791 | dc.networkName = networkName
|
---|
| 792 | return nil
|
---|
| 793 | }
|
---|
| 794 |
|
---|
| 795 | func (dc *downstreamConn) register() error {
|
---|
| 796 | if dc.registered {
|
---|
| 797 | return fmt.Errorf("tried to register twice")
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | password := dc.password
|
---|
| 801 | dc.password = ""
|
---|
| 802 | if dc.user == nil {
|
---|
| 803 | if err := dc.authenticate(dc.rawUsername, password); err != nil {
|
---|
| 804 | return err
|
---|
| 805 | }
|
---|
| 806 | }
|
---|
| 807 |
|
---|
[183] | 808 | if dc.clientName == "" && dc.networkName == "" {
|
---|
| 809 | _, dc.clientName, dc.networkName = unmarshalUsername(dc.rawUsername)
|
---|
[168] | 810 | }
|
---|
| 811 |
|
---|
| 812 | dc.registered = true
|
---|
[184] | 813 | dc.logger.Printf("registration complete for user %q", dc.user.Username)
|
---|
[168] | 814 | return nil
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | func (dc *downstreamConn) loadNetwork() error {
|
---|
| 818 | if dc.networkName == "" {
|
---|
[112] | 819 | return nil
|
---|
| 820 | }
|
---|
[85] | 821 |
|
---|
[168] | 822 | network := dc.user.getNetwork(dc.networkName)
|
---|
[112] | 823 | if network == nil {
|
---|
[168] | 824 | addr := dc.networkName
|
---|
[112] | 825 | if !strings.ContainsRune(addr, ':') {
|
---|
| 826 | addr = addr + ":6697"
|
---|
| 827 | }
|
---|
| 828 |
|
---|
| 829 | dc.logger.Printf("trying to connect to new network %q", addr)
|
---|
| 830 | if err := sanityCheckServer(addr); err != nil {
|
---|
| 831 | dc.logger.Printf("failed to connect to %q: %v", addr, err)
|
---|
| 832 | return ircError{&irc.Message{
|
---|
| 833 | Command: irc.ERR_PASSWDMISMATCH,
|
---|
[168] | 834 | Params: []string{"*", fmt.Sprintf("Failed to connect to %q", dc.networkName)},
|
---|
[112] | 835 | }}
|
---|
| 836 | }
|
---|
| 837 |
|
---|
[354] | 838 | // Some clients only allow specifying the nickname (and use the
|
---|
| 839 | // nickname as a username too). Strip the network name from the
|
---|
| 840 | // nickname when auto-saving networks.
|
---|
| 841 | nick, _, _ := unmarshalUsername(dc.nick)
|
---|
| 842 |
|
---|
[168] | 843 | dc.logger.Printf("auto-saving network %q", dc.networkName)
|
---|
[112] | 844 | var err error
|
---|
[120] | 845 | network, err = dc.user.createNetwork(&Network{
|
---|
[168] | 846 | Addr: dc.networkName,
|
---|
[354] | 847 | Nick: nick,
|
---|
[120] | 848 | })
|
---|
[112] | 849 | if err != nil {
|
---|
| 850 | return err
|
---|
| 851 | }
|
---|
| 852 | }
|
---|
| 853 |
|
---|
| 854 | dc.network = network
|
---|
| 855 | return nil
|
---|
| 856 | }
|
---|
| 857 |
|
---|
[168] | 858 | func (dc *downstreamConn) welcome() error {
|
---|
| 859 | if dc.user == nil || !dc.registered {
|
---|
| 860 | panic("tried to welcome an unregistered connection")
|
---|
[37] | 861 | }
|
---|
| 862 |
|
---|
[168] | 863 | // TODO: doing this might take some time. We should do it in dc.register
|
---|
| 864 | // instead, but we'll potentially be adding a new network and this must be
|
---|
| 865 | // done in the user goroutine.
|
---|
| 866 | if err := dc.loadNetwork(); err != nil {
|
---|
| 867 | return err
|
---|
[85] | 868 | }
|
---|
| 869 |
|
---|
[55] | 870 | dc.SendMessage(&irc.Message{
|
---|
| 871 | Prefix: dc.srv.prefix(),
|
---|
[13] | 872 | Command: irc.RPL_WELCOME,
|
---|
[98] | 873 | Params: []string{dc.nick, "Welcome to soju, " + dc.nick},
|
---|
[54] | 874 | })
|
---|
[55] | 875 | dc.SendMessage(&irc.Message{
|
---|
| 876 | Prefix: dc.srv.prefix(),
|
---|
[13] | 877 | Command: irc.RPL_YOURHOST,
|
---|
[55] | 878 | Params: []string{dc.nick, "Your host is " + dc.srv.Hostname},
|
---|
[54] | 879 | })
|
---|
[55] | 880 | dc.SendMessage(&irc.Message{
|
---|
| 881 | Prefix: dc.srv.prefix(),
|
---|
[13] | 882 | Command: irc.RPL_CREATED,
|
---|
[55] | 883 | Params: []string{dc.nick, "Who cares when the server was created?"},
|
---|
[54] | 884 | })
|
---|
[55] | 885 | dc.SendMessage(&irc.Message{
|
---|
| 886 | Prefix: dc.srv.prefix(),
|
---|
[13] | 887 | Command: irc.RPL_MYINFO,
|
---|
[98] | 888 | Params: []string{dc.nick, dc.srv.Hostname, "soju", "aiwroO", "OovaimnqpsrtklbeI"},
|
---|
[54] | 889 | })
|
---|
[93] | 890 | // TODO: RPL_ISUPPORT
|
---|
[319] | 891 | // TODO: send CHATHISTORY in RPL_ISUPPORT when implemented
|
---|
[55] | 892 | dc.SendMessage(&irc.Message{
|
---|
| 893 | Prefix: dc.srv.prefix(),
|
---|
[13] | 894 | Command: irc.ERR_NOMOTD,
|
---|
[55] | 895 | Params: []string{dc.nick, "No MOTD"},
|
---|
[54] | 896 | })
|
---|
[13] | 897 |
|
---|
[296] | 898 | dc.updateNick()
|
---|
| 899 |
|
---|
[73] | 900 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[30] | 901 | for _, ch := range uc.channels {
|
---|
[284] | 902 | if !ch.complete {
|
---|
| 903 | continue
|
---|
| 904 | }
|
---|
| 905 | if record, ok := uc.network.channels[ch.Name]; ok && record.Detached {
|
---|
| 906 | continue
|
---|
| 907 | }
|
---|
[132] | 908 |
|
---|
[284] | 909 | dc.SendMessage(&irc.Message{
|
---|
| 910 | Prefix: dc.prefix(),
|
---|
| 911 | Command: "JOIN",
|
---|
| 912 | Params: []string{dc.marshalEntity(ch.conn.network, ch.Name)},
|
---|
| 913 | })
|
---|
| 914 |
|
---|
| 915 | forwardChannel(dc, ch)
|
---|
[30] | 916 | }
|
---|
[143] | 917 | })
|
---|
[50] | 918 |
|
---|
[143] | 919 | dc.forEachNetwork(func(net *network) {
|
---|
[253] | 920 | // Only send history if we're the first connected client with that name
|
---|
| 921 | // for the network
|
---|
| 922 | if _, ok := net.offlineClients[dc.clientName]; ok {
|
---|
| 923 | dc.sendNetworkHistory(net)
|
---|
| 924 | delete(net.offlineClients, dc.clientName)
|
---|
[227] | 925 | }
|
---|
[409] | 926 |
|
---|
| 927 | // Fast-forward history to last message
|
---|
| 928 | for target, history := range net.history {
|
---|
| 929 | if ch, ok := net.channels[target]; ok && ch.Detached {
|
---|
| 930 | continue
|
---|
| 931 | }
|
---|
| 932 |
|
---|
[423] | 933 | lastID, err := dc.user.msgStore.LastMsgID(net, target, time.Now())
|
---|
[409] | 934 | if err != nil {
|
---|
| 935 | dc.logger.Printf("failed to get last message ID: %v", err)
|
---|
| 936 | continue
|
---|
| 937 | }
|
---|
| 938 | history.clients[dc.clientName] = lastID
|
---|
| 939 | }
|
---|
[253] | 940 | })
|
---|
[57] | 941 |
|
---|
[253] | 942 | return nil
|
---|
| 943 | }
|
---|
[144] | 944 |
|
---|
[428] | 945 | // messageSupportsHistory checks whether the provided message can be sent as
|
---|
| 946 | // part of an history batch.
|
---|
| 947 | func (dc *downstreamConn) messageSupportsHistory(msg *irc.Message) bool {
|
---|
| 948 | // Don't replay all messages, because that would mess up client
|
---|
| 949 | // state. For instance we just sent the list of users, sending
|
---|
| 950 | // PART messages for one of these users would be incorrect.
|
---|
| 951 | // TODO: add support for draft/event-playback
|
---|
| 952 | switch msg.Command {
|
---|
| 953 | case "PRIVMSG", "NOTICE":
|
---|
| 954 | return true
|
---|
| 955 | }
|
---|
| 956 | return false
|
---|
| 957 | }
|
---|
| 958 |
|
---|
[253] | 959 | func (dc *downstreamConn) sendNetworkHistory(net *network) {
|
---|
[423] | 960 | if dc.caps["draft/chathistory"] || dc.user.msgStore == nil {
|
---|
[319] | 961 | return
|
---|
| 962 | }
|
---|
[253] | 963 | for target, history := range net.history {
|
---|
[284] | 964 | if ch, ok := net.channels[target]; ok && ch.Detached {
|
---|
| 965 | continue
|
---|
| 966 | }
|
---|
| 967 |
|
---|
[409] | 968 | lastDelivered, ok := history.clients[dc.clientName]
|
---|
[253] | 969 | if !ok {
|
---|
| 970 | continue
|
---|
| 971 | }
|
---|
| 972 |
|
---|
[409] | 973 | limit := 4000
|
---|
[423] | 974 | history, err := dc.user.msgStore.LoadLatestID(net, target, lastDelivered, limit)
|
---|
[409] | 975 | if err != nil {
|
---|
| 976 | dc.logger.Printf("failed to send implicit history for %q: %v", target, err)
|
---|
| 977 | continue
|
---|
| 978 | }
|
---|
[253] | 979 |
|
---|
[256] | 980 | batchRef := "history"
|
---|
| 981 | if dc.caps["batch"] {
|
---|
| 982 | dc.SendMessage(&irc.Message{
|
---|
| 983 | Prefix: dc.srv.prefix(),
|
---|
| 984 | Command: "BATCH",
|
---|
[260] | 985 | Params: []string{"+" + batchRef, "chathistory", dc.marshalEntity(net, target)},
|
---|
[256] | 986 | })
|
---|
| 987 | }
|
---|
| 988 |
|
---|
[409] | 989 | for _, msg := range history {
|
---|
[428] | 990 | if !dc.messageSupportsHistory(msg) {
|
---|
[245] | 991 | continue
|
---|
| 992 | }
|
---|
| 993 |
|
---|
[256] | 994 | if dc.caps["batch"] {
|
---|
| 995 | msg.Tags["batch"] = irc.TagValue(batchRef)
|
---|
| 996 | }
|
---|
[261] | 997 | dc.SendMessage(dc.marshalMessage(msg, net))
|
---|
[227] | 998 | }
|
---|
[256] | 999 |
|
---|
| 1000 | if dc.caps["batch"] {
|
---|
| 1001 | dc.SendMessage(&irc.Message{
|
---|
| 1002 | Prefix: dc.srv.prefix(),
|
---|
| 1003 | Command: "BATCH",
|
---|
| 1004 | Params: []string{"-" + batchRef},
|
---|
| 1005 | })
|
---|
| 1006 | }
|
---|
[253] | 1007 | }
|
---|
[13] | 1008 | }
|
---|
| 1009 |
|
---|
[103] | 1010 | func (dc *downstreamConn) runUntilRegistered() error {
|
---|
| 1011 | for !dc.registered {
|
---|
[212] | 1012 | msg, err := dc.ReadMessage()
|
---|
[106] | 1013 | if err != nil {
|
---|
[103] | 1014 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
| 1015 | }
|
---|
| 1016 |
|
---|
| 1017 | err = dc.handleMessage(msg)
|
---|
| 1018 | if ircErr, ok := err.(ircError); ok {
|
---|
| 1019 | ircErr.Message.Prefix = dc.srv.prefix()
|
---|
| 1020 | dc.SendMessage(ircErr.Message)
|
---|
| 1021 | } else if err != nil {
|
---|
| 1022 | return fmt.Errorf("failed to handle IRC command %q: %v", msg, err)
|
---|
| 1023 | }
|
---|
| 1024 | }
|
---|
| 1025 |
|
---|
| 1026 | return nil
|
---|
| 1027 | }
|
---|
| 1028 |
|
---|
[55] | 1029 | func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
|
---|
[13] | 1030 | switch msg.Command {
|
---|
[111] | 1031 | case "CAP":
|
---|
| 1032 | var subCmd string
|
---|
| 1033 | if err := parseMessageParams(msg, &subCmd); err != nil {
|
---|
| 1034 | return err
|
---|
| 1035 | }
|
---|
| 1036 | if err := dc.handleCapCommand(subCmd, msg.Params[1:]); err != nil {
|
---|
| 1037 | return err
|
---|
| 1038 | }
|
---|
[107] | 1039 | case "PING":
|
---|
[412] | 1040 | var source, destination string
|
---|
| 1041 | if err := parseMessageParams(msg, &source); err != nil {
|
---|
| 1042 | return err
|
---|
| 1043 | }
|
---|
| 1044 | if len(msg.Params) > 1 {
|
---|
| 1045 | destination = msg.Params[1]
|
---|
| 1046 | }
|
---|
| 1047 | if destination != "" && destination != dc.srv.Hostname {
|
---|
| 1048 | return ircError{&irc.Message{
|
---|
| 1049 | Command: irc.ERR_NOSUCHSERVER,
|
---|
[413] | 1050 | Params: []string{dc.nick, destination, "No such server"},
|
---|
[412] | 1051 | }}
|
---|
| 1052 | }
|
---|
[107] | 1053 | dc.SendMessage(&irc.Message{
|
---|
| 1054 | Prefix: dc.srv.prefix(),
|
---|
| 1055 | Command: "PONG",
|
---|
[412] | 1056 | Params: []string{dc.srv.Hostname, source},
|
---|
[107] | 1057 | })
|
---|
| 1058 | return nil
|
---|
[428] | 1059 | case "PONG":
|
---|
| 1060 | if len(msg.Params) == 0 {
|
---|
| 1061 | return newNeedMoreParamsError(msg.Command)
|
---|
| 1062 | }
|
---|
| 1063 | token := msg.Params[len(msg.Params)-1]
|
---|
| 1064 | dc.handlePong(token)
|
---|
[42] | 1065 | case "USER":
|
---|
[13] | 1066 | return ircError{&irc.Message{
|
---|
| 1067 | Command: irc.ERR_ALREADYREGISTERED,
|
---|
[55] | 1068 | Params: []string{dc.nick, "You may not reregister"},
|
---|
[13] | 1069 | }}
|
---|
[42] | 1070 | case "NICK":
|
---|
[429] | 1071 | var rawNick string
|
---|
| 1072 | if err := parseMessageParams(msg, &rawNick); err != nil {
|
---|
[90] | 1073 | return err
|
---|
| 1074 | }
|
---|
| 1075 |
|
---|
[429] | 1076 | nick := rawNick
|
---|
[297] | 1077 | var upstream *upstreamConn
|
---|
| 1078 | if dc.upstream() == nil {
|
---|
| 1079 | uc, unmarshaledNick, err := dc.unmarshalEntity(nick)
|
---|
| 1080 | if err == nil { // NICK nick/network: NICK only on a specific upstream
|
---|
| 1081 | upstream = uc
|
---|
| 1082 | nick = unmarshaledNick
|
---|
| 1083 | }
|
---|
| 1084 | }
|
---|
| 1085 |
|
---|
[404] | 1086 | if strings.ContainsAny(nick, illegalNickChars) {
|
---|
| 1087 | return ircError{&irc.Message{
|
---|
| 1088 | Command: irc.ERR_ERRONEUSNICKNAME,
|
---|
[430] | 1089 | Params: []string{dc.nick, rawNick, "contains illegal characters"},
|
---|
[404] | 1090 | }}
|
---|
| 1091 | }
|
---|
[429] | 1092 | if nick == serviceNick {
|
---|
| 1093 | return ircError{&irc.Message{
|
---|
| 1094 | Command: irc.ERR_NICKNAMEINUSE,
|
---|
| 1095 | Params: []string{dc.nick, rawNick, "Nickname reserved for bouncer service"},
|
---|
| 1096 | }}
|
---|
| 1097 | }
|
---|
[404] | 1098 |
|
---|
[90] | 1099 | var err error
|
---|
| 1100 | dc.forEachNetwork(func(n *network) {
|
---|
[297] | 1101 | if err != nil || (upstream != nil && upstream.network != n) {
|
---|
[90] | 1102 | return
|
---|
| 1103 | }
|
---|
| 1104 | n.Nick = nick
|
---|
[421] | 1105 | err = dc.srv.db.StoreNetwork(dc.user.ID, &n.Network)
|
---|
[90] | 1106 | })
|
---|
| 1107 | if err != nil {
|
---|
| 1108 | return err
|
---|
| 1109 | }
|
---|
| 1110 |
|
---|
[73] | 1111 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[297] | 1112 | if upstream != nil && upstream != uc {
|
---|
| 1113 | return
|
---|
| 1114 | }
|
---|
[301] | 1115 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[297] | 1116 | Command: "NICK",
|
---|
| 1117 | Params: []string{nick},
|
---|
| 1118 | })
|
---|
[42] | 1119 | })
|
---|
[296] | 1120 |
|
---|
| 1121 | if dc.upstream() == nil && dc.nick != nick {
|
---|
| 1122 | dc.SendMessage(&irc.Message{
|
---|
| 1123 | Prefix: dc.prefix(),
|
---|
| 1124 | Command: "NICK",
|
---|
| 1125 | Params: []string{nick},
|
---|
| 1126 | })
|
---|
| 1127 | dc.nick = nick
|
---|
| 1128 | }
|
---|
[146] | 1129 | case "JOIN":
|
---|
| 1130 | var namesStr string
|
---|
| 1131 | if err := parseMessageParams(msg, &namesStr); err != nil {
|
---|
[48] | 1132 | return err
|
---|
| 1133 | }
|
---|
| 1134 |
|
---|
[146] | 1135 | var keys []string
|
---|
| 1136 | if len(msg.Params) > 1 {
|
---|
| 1137 | keys = strings.Split(msg.Params[1], ",")
|
---|
| 1138 | }
|
---|
| 1139 |
|
---|
| 1140 | for i, name := range strings.Split(namesStr, ",") {
|
---|
[145] | 1141 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1142 | if err != nil {
|
---|
[158] | 1143 | return err
|
---|
[145] | 1144 | }
|
---|
[48] | 1145 |
|
---|
[146] | 1146 | var key string
|
---|
| 1147 | if len(keys) > i {
|
---|
| 1148 | key = keys[i]
|
---|
| 1149 | }
|
---|
| 1150 |
|
---|
| 1151 | params := []string{upstreamName}
|
---|
| 1152 | if key != "" {
|
---|
| 1153 | params = append(params, key)
|
---|
| 1154 | }
|
---|
[301] | 1155 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[146] | 1156 | Command: "JOIN",
|
---|
| 1157 | Params: params,
|
---|
[145] | 1158 | })
|
---|
[89] | 1159 |
|
---|
[435] | 1160 | var ch *Channel
|
---|
| 1161 | var ok bool
|
---|
| 1162 | if ch, ok = uc.network.channels[upstreamName]; ok {
|
---|
[285] | 1163 | // Don't clear the channel key if there's one set
|
---|
| 1164 | // TODO: add a way to unset the channel key
|
---|
[435] | 1165 | if key != "" {
|
---|
| 1166 | ch.Key = key
|
---|
| 1167 | }
|
---|
| 1168 | uc.network.attach(ch)
|
---|
| 1169 | } else {
|
---|
| 1170 | ch = &Channel{
|
---|
| 1171 | Name: upstreamName,
|
---|
| 1172 | Key: key,
|
---|
| 1173 | }
|
---|
| 1174 | uc.network.channels[upstreamName] = ch
|
---|
[285] | 1175 | }
|
---|
[435] | 1176 | if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
|
---|
[222] | 1177 | dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
|
---|
[89] | 1178 | }
|
---|
| 1179 | }
|
---|
[146] | 1180 | case "PART":
|
---|
| 1181 | var namesStr string
|
---|
| 1182 | if err := parseMessageParams(msg, &namesStr); err != nil {
|
---|
| 1183 | return err
|
---|
| 1184 | }
|
---|
| 1185 |
|
---|
| 1186 | var reason string
|
---|
| 1187 | if len(msg.Params) > 1 {
|
---|
| 1188 | reason = msg.Params[1]
|
---|
| 1189 | }
|
---|
| 1190 |
|
---|
| 1191 | for _, name := range strings.Split(namesStr, ",") {
|
---|
| 1192 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1193 | if err != nil {
|
---|
[158] | 1194 | return err
|
---|
[146] | 1195 | }
|
---|
| 1196 |
|
---|
[284] | 1197 | if strings.EqualFold(reason, "detach") {
|
---|
[435] | 1198 | var ch *Channel
|
---|
| 1199 | var ok bool
|
---|
| 1200 | if ch, ok = uc.network.channels[upstreamName]; ok {
|
---|
| 1201 | uc.network.detach(ch)
|
---|
| 1202 | } else {
|
---|
| 1203 | ch = &Channel{
|
---|
| 1204 | Name: name,
|
---|
| 1205 | Detached: true,
|
---|
| 1206 | }
|
---|
| 1207 | uc.network.channels[upstreamName] = ch
|
---|
[284] | 1208 | }
|
---|
[435] | 1209 | if err := dc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
|
---|
| 1210 | dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
|
---|
| 1211 | }
|
---|
[284] | 1212 | } else {
|
---|
| 1213 | params := []string{upstreamName}
|
---|
| 1214 | if reason != "" {
|
---|
| 1215 | params = append(params, reason)
|
---|
| 1216 | }
|
---|
[301] | 1217 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[284] | 1218 | Command: "PART",
|
---|
| 1219 | Params: params,
|
---|
| 1220 | })
|
---|
[146] | 1221 |
|
---|
[284] | 1222 | if err := uc.network.deleteChannel(upstreamName); err != nil {
|
---|
| 1223 | dc.logger.Printf("failed to delete channel %q: %v", upstreamName, err)
|
---|
| 1224 | }
|
---|
[146] | 1225 | }
|
---|
| 1226 | }
|
---|
[159] | 1227 | case "KICK":
|
---|
| 1228 | var channelStr, userStr string
|
---|
| 1229 | if err := parseMessageParams(msg, &channelStr, &userStr); err != nil {
|
---|
| 1230 | return err
|
---|
| 1231 | }
|
---|
| 1232 |
|
---|
| 1233 | channels := strings.Split(channelStr, ",")
|
---|
| 1234 | users := strings.Split(userStr, ",")
|
---|
| 1235 |
|
---|
| 1236 | var reason string
|
---|
| 1237 | if len(msg.Params) > 2 {
|
---|
| 1238 | reason = msg.Params[2]
|
---|
| 1239 | }
|
---|
| 1240 |
|
---|
| 1241 | if len(channels) != 1 && len(channels) != len(users) {
|
---|
| 1242 | return ircError{&irc.Message{
|
---|
| 1243 | Command: irc.ERR_BADCHANMASK,
|
---|
| 1244 | Params: []string{dc.nick, channelStr, "Bad channel mask"},
|
---|
| 1245 | }}
|
---|
| 1246 | }
|
---|
| 1247 |
|
---|
| 1248 | for i, user := range users {
|
---|
| 1249 | var channel string
|
---|
| 1250 | if len(channels) == 1 {
|
---|
| 1251 | channel = channels[0]
|
---|
| 1252 | } else {
|
---|
| 1253 | channel = channels[i]
|
---|
| 1254 | }
|
---|
| 1255 |
|
---|
| 1256 | ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1257 | if err != nil {
|
---|
| 1258 | return err
|
---|
| 1259 | }
|
---|
| 1260 |
|
---|
| 1261 | ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
---|
| 1262 | if err != nil {
|
---|
| 1263 | return err
|
---|
| 1264 | }
|
---|
| 1265 |
|
---|
| 1266 | if ucChannel != ucUser {
|
---|
| 1267 | return ircError{&irc.Message{
|
---|
| 1268 | Command: irc.ERR_USERNOTINCHANNEL,
|
---|
[400] | 1269 | Params: []string{dc.nick, user, channel, "They are on another network"},
|
---|
[159] | 1270 | }}
|
---|
| 1271 | }
|
---|
| 1272 | uc := ucChannel
|
---|
| 1273 |
|
---|
| 1274 | params := []string{upstreamChannel, upstreamUser}
|
---|
| 1275 | if reason != "" {
|
---|
| 1276 | params = append(params, reason)
|
---|
| 1277 | }
|
---|
[301] | 1278 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[159] | 1279 | Command: "KICK",
|
---|
| 1280 | Params: params,
|
---|
| 1281 | })
|
---|
| 1282 | }
|
---|
[69] | 1283 | case "MODE":
|
---|
[46] | 1284 | var name string
|
---|
| 1285 | if err := parseMessageParams(msg, &name); err != nil {
|
---|
| 1286 | return err
|
---|
| 1287 | }
|
---|
| 1288 |
|
---|
| 1289 | var modeStr string
|
---|
| 1290 | if len(msg.Params) > 1 {
|
---|
| 1291 | modeStr = msg.Params[1]
|
---|
| 1292 | }
|
---|
| 1293 |
|
---|
[139] | 1294 | if name == dc.nick {
|
---|
[46] | 1295 | if modeStr != "" {
|
---|
[73] | 1296 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[301] | 1297 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[69] | 1298 | Command: "MODE",
|
---|
| 1299 | Params: []string{uc.nick, modeStr},
|
---|
| 1300 | })
|
---|
[46] | 1301 | })
|
---|
| 1302 | } else {
|
---|
[55] | 1303 | dc.SendMessage(&irc.Message{
|
---|
| 1304 | Prefix: dc.srv.prefix(),
|
---|
[46] | 1305 | Command: irc.RPL_UMODEIS,
|
---|
[129] | 1306 | Params: []string{dc.nick, ""}, // TODO
|
---|
[54] | 1307 | })
|
---|
[46] | 1308 | }
|
---|
[139] | 1309 | return nil
|
---|
[46] | 1310 | }
|
---|
[139] | 1311 |
|
---|
| 1312 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1313 | if err != nil {
|
---|
| 1314 | return err
|
---|
| 1315 | }
|
---|
| 1316 |
|
---|
| 1317 | if !uc.isChannel(upstreamName) {
|
---|
| 1318 | return ircError{&irc.Message{
|
---|
| 1319 | Command: irc.ERR_USERSDONTMATCH,
|
---|
| 1320 | Params: []string{dc.nick, "Cannot change mode for other users"},
|
---|
| 1321 | }}
|
---|
| 1322 | }
|
---|
| 1323 |
|
---|
| 1324 | if modeStr != "" {
|
---|
| 1325 | params := []string{upstreamName, modeStr}
|
---|
| 1326 | params = append(params, msg.Params[2:]...)
|
---|
[301] | 1327 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[139] | 1328 | Command: "MODE",
|
---|
| 1329 | Params: params,
|
---|
| 1330 | })
|
---|
| 1331 | } else {
|
---|
| 1332 | ch, ok := uc.channels[upstreamName]
|
---|
| 1333 | if !ok {
|
---|
| 1334 | return ircError{&irc.Message{
|
---|
| 1335 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
| 1336 | Params: []string{dc.nick, name, "No such channel"},
|
---|
| 1337 | }}
|
---|
| 1338 | }
|
---|
| 1339 |
|
---|
| 1340 | if ch.modes == nil {
|
---|
| 1341 | // we haven't received the initial RPL_CHANNELMODEIS yet
|
---|
| 1342 | // ignore the request, we will broadcast the modes later when we receive RPL_CHANNELMODEIS
|
---|
| 1343 | return nil
|
---|
| 1344 | }
|
---|
| 1345 |
|
---|
| 1346 | modeStr, modeParams := ch.modes.Format()
|
---|
| 1347 | params := []string{dc.nick, name, modeStr}
|
---|
| 1348 | params = append(params, modeParams...)
|
---|
| 1349 |
|
---|
| 1350 | dc.SendMessage(&irc.Message{
|
---|
| 1351 | Prefix: dc.srv.prefix(),
|
---|
| 1352 | Command: irc.RPL_CHANNELMODEIS,
|
---|
| 1353 | Params: params,
|
---|
| 1354 | })
|
---|
[162] | 1355 | if ch.creationTime != "" {
|
---|
| 1356 | dc.SendMessage(&irc.Message{
|
---|
| 1357 | Prefix: dc.srv.prefix(),
|
---|
| 1358 | Command: rpl_creationtime,
|
---|
| 1359 | Params: []string{dc.nick, name, ch.creationTime},
|
---|
| 1360 | })
|
---|
| 1361 | }
|
---|
[139] | 1362 | }
|
---|
[160] | 1363 | case "TOPIC":
|
---|
| 1364 | var channel string
|
---|
| 1365 | if err := parseMessageParams(msg, &channel); err != nil {
|
---|
| 1366 | return err
|
---|
| 1367 | }
|
---|
| 1368 |
|
---|
| 1369 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1370 | if err != nil {
|
---|
| 1371 | return err
|
---|
| 1372 | }
|
---|
| 1373 |
|
---|
| 1374 | if len(msg.Params) > 1 { // setting topic
|
---|
| 1375 | topic := msg.Params[1]
|
---|
[301] | 1376 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[160] | 1377 | Command: "TOPIC",
|
---|
| 1378 | Params: []string{upstreamChannel, topic},
|
---|
| 1379 | })
|
---|
| 1380 | } else { // getting topic
|
---|
| 1381 | ch, ok := uc.channels[upstreamChannel]
|
---|
| 1382 | if !ok {
|
---|
| 1383 | return ircError{&irc.Message{
|
---|
| 1384 | Command: irc.ERR_NOSUCHCHANNEL,
|
---|
| 1385 | Params: []string{dc.nick, upstreamChannel, "No such channel"},
|
---|
| 1386 | }}
|
---|
| 1387 | }
|
---|
| 1388 | sendTopic(dc, ch)
|
---|
| 1389 | }
|
---|
[177] | 1390 | case "LIST":
|
---|
| 1391 | // TODO: support ELIST when supported by all upstreams
|
---|
| 1392 |
|
---|
| 1393 | pl := pendingLIST{
|
---|
| 1394 | downstreamID: dc.id,
|
---|
| 1395 | pendingCommands: make(map[int64]*irc.Message),
|
---|
| 1396 | }
|
---|
[298] | 1397 | var upstream *upstreamConn
|
---|
[177] | 1398 | var upstreamChannels map[int64][]string
|
---|
| 1399 | if len(msg.Params) > 0 {
|
---|
[298] | 1400 | uc, upstreamMask, err := dc.unmarshalEntity(msg.Params[0])
|
---|
| 1401 | if err == nil && upstreamMask == "*" { // LIST */network: send LIST only to one network
|
---|
| 1402 | upstream = uc
|
---|
| 1403 | } else {
|
---|
| 1404 | upstreamChannels = make(map[int64][]string)
|
---|
| 1405 | channels := strings.Split(msg.Params[0], ",")
|
---|
| 1406 | for _, channel := range channels {
|
---|
| 1407 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1408 | if err != nil {
|
---|
| 1409 | return err
|
---|
| 1410 | }
|
---|
| 1411 | upstreamChannels[uc.network.ID] = append(upstreamChannels[uc.network.ID], upstreamChannel)
|
---|
[177] | 1412 | }
|
---|
| 1413 | }
|
---|
| 1414 | }
|
---|
| 1415 |
|
---|
| 1416 | dc.user.pendingLISTs = append(dc.user.pendingLISTs, pl)
|
---|
| 1417 | dc.forEachUpstream(func(uc *upstreamConn) {
|
---|
[298] | 1418 | if upstream != nil && upstream != uc {
|
---|
| 1419 | return
|
---|
| 1420 | }
|
---|
[177] | 1421 | var params []string
|
---|
| 1422 | if upstreamChannels != nil {
|
---|
| 1423 | if channels, ok := upstreamChannels[uc.network.ID]; ok {
|
---|
| 1424 | params = []string{strings.Join(channels, ",")}
|
---|
| 1425 | } else {
|
---|
| 1426 | return
|
---|
| 1427 | }
|
---|
| 1428 | }
|
---|
| 1429 | pl.pendingCommands[uc.network.ID] = &irc.Message{
|
---|
| 1430 | Command: "LIST",
|
---|
| 1431 | Params: params,
|
---|
| 1432 | }
|
---|
[181] | 1433 | uc.trySendLIST(dc.id)
|
---|
[177] | 1434 | })
|
---|
[140] | 1435 | case "NAMES":
|
---|
| 1436 | if len(msg.Params) == 0 {
|
---|
| 1437 | dc.SendMessage(&irc.Message{
|
---|
| 1438 | Prefix: dc.srv.prefix(),
|
---|
| 1439 | Command: irc.RPL_ENDOFNAMES,
|
---|
| 1440 | Params: []string{dc.nick, "*", "End of /NAMES list"},
|
---|
| 1441 | })
|
---|
| 1442 | return nil
|
---|
| 1443 | }
|
---|
| 1444 |
|
---|
| 1445 | channels := strings.Split(msg.Params[0], ",")
|
---|
| 1446 | for _, channel := range channels {
|
---|
| 1447 | uc, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1448 | if err != nil {
|
---|
| 1449 | return err
|
---|
| 1450 | }
|
---|
| 1451 |
|
---|
| 1452 | ch, ok := uc.channels[upstreamChannel]
|
---|
| 1453 | if ok {
|
---|
| 1454 | sendNames(dc, ch)
|
---|
| 1455 | } else {
|
---|
| 1456 | // NAMES on a channel we have not joined, ask upstream
|
---|
[176] | 1457 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[140] | 1458 | Command: "NAMES",
|
---|
| 1459 | Params: []string{upstreamChannel},
|
---|
| 1460 | })
|
---|
| 1461 | }
|
---|
| 1462 | }
|
---|
[127] | 1463 | case "WHO":
|
---|
| 1464 | if len(msg.Params) == 0 {
|
---|
| 1465 | // TODO: support WHO without parameters
|
---|
| 1466 | dc.SendMessage(&irc.Message{
|
---|
| 1467 | Prefix: dc.srv.prefix(),
|
---|
| 1468 | Command: irc.RPL_ENDOFWHO,
|
---|
[140] | 1469 | Params: []string{dc.nick, "*", "End of /WHO list"},
|
---|
[127] | 1470 | })
|
---|
| 1471 | return nil
|
---|
| 1472 | }
|
---|
| 1473 |
|
---|
| 1474 | // TODO: support WHO masks
|
---|
| 1475 | entity := msg.Params[0]
|
---|
| 1476 |
|
---|
[142] | 1477 | if entity == dc.nick {
|
---|
| 1478 | // TODO: support AWAY (H/G) in self WHO reply
|
---|
| 1479 | dc.SendMessage(&irc.Message{
|
---|
| 1480 | Prefix: dc.srv.prefix(),
|
---|
| 1481 | Command: irc.RPL_WHOREPLY,
|
---|
[184] | 1482 | Params: []string{dc.nick, "*", dc.user.Username, dc.hostname, dc.srv.Hostname, dc.nick, "H", "0 " + dc.realname},
|
---|
[142] | 1483 | })
|
---|
| 1484 | dc.SendMessage(&irc.Message{
|
---|
| 1485 | Prefix: dc.srv.prefix(),
|
---|
| 1486 | Command: irc.RPL_ENDOFWHO,
|
---|
| 1487 | Params: []string{dc.nick, dc.nick, "End of /WHO list"},
|
---|
| 1488 | })
|
---|
| 1489 | return nil
|
---|
| 1490 | }
|
---|
[343] | 1491 | if entity == serviceNick {
|
---|
| 1492 | dc.SendMessage(&irc.Message{
|
---|
| 1493 | Prefix: dc.srv.prefix(),
|
---|
| 1494 | Command: irc.RPL_WHOREPLY,
|
---|
| 1495 | Params: []string{serviceNick, "*", servicePrefix.User, servicePrefix.Host, dc.srv.Hostname, serviceNick, "H", "0 " + serviceRealname},
|
---|
| 1496 | })
|
---|
| 1497 | dc.SendMessage(&irc.Message{
|
---|
| 1498 | Prefix: dc.srv.prefix(),
|
---|
| 1499 | Command: irc.RPL_ENDOFWHO,
|
---|
| 1500 | Params: []string{dc.nick, serviceNick, "End of /WHO list"},
|
---|
| 1501 | })
|
---|
| 1502 | return nil
|
---|
| 1503 | }
|
---|
[142] | 1504 |
|
---|
[127] | 1505 | uc, upstreamName, err := dc.unmarshalEntity(entity)
|
---|
| 1506 | if err != nil {
|
---|
| 1507 | return err
|
---|
| 1508 | }
|
---|
| 1509 |
|
---|
| 1510 | var params []string
|
---|
| 1511 | if len(msg.Params) == 2 {
|
---|
| 1512 | params = []string{upstreamName, msg.Params[1]}
|
---|
| 1513 | } else {
|
---|
| 1514 | params = []string{upstreamName}
|
---|
| 1515 | }
|
---|
| 1516 |
|
---|
[176] | 1517 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[127] | 1518 | Command: "WHO",
|
---|
| 1519 | Params: params,
|
---|
| 1520 | })
|
---|
[128] | 1521 | case "WHOIS":
|
---|
| 1522 | if len(msg.Params) == 0 {
|
---|
| 1523 | return ircError{&irc.Message{
|
---|
| 1524 | Command: irc.ERR_NONICKNAMEGIVEN,
|
---|
| 1525 | Params: []string{dc.nick, "No nickname given"},
|
---|
| 1526 | }}
|
---|
| 1527 | }
|
---|
| 1528 |
|
---|
| 1529 | var target, mask string
|
---|
| 1530 | if len(msg.Params) == 1 {
|
---|
| 1531 | target = ""
|
---|
| 1532 | mask = msg.Params[0]
|
---|
| 1533 | } else {
|
---|
| 1534 | target = msg.Params[0]
|
---|
| 1535 | mask = msg.Params[1]
|
---|
| 1536 | }
|
---|
| 1537 | // TODO: support multiple WHOIS users
|
---|
| 1538 | if i := strings.IndexByte(mask, ','); i >= 0 {
|
---|
| 1539 | mask = mask[:i]
|
---|
| 1540 | }
|
---|
| 1541 |
|
---|
[142] | 1542 | if mask == dc.nick {
|
---|
| 1543 | dc.SendMessage(&irc.Message{
|
---|
| 1544 | Prefix: dc.srv.prefix(),
|
---|
| 1545 | Command: irc.RPL_WHOISUSER,
|
---|
[184] | 1546 | Params: []string{dc.nick, dc.nick, dc.user.Username, dc.hostname, "*", dc.realname},
|
---|
[142] | 1547 | })
|
---|
| 1548 | dc.SendMessage(&irc.Message{
|
---|
| 1549 | Prefix: dc.srv.prefix(),
|
---|
| 1550 | Command: irc.RPL_WHOISSERVER,
|
---|
| 1551 | Params: []string{dc.nick, dc.nick, dc.srv.Hostname, "soju"},
|
---|
| 1552 | })
|
---|
| 1553 | dc.SendMessage(&irc.Message{
|
---|
| 1554 | Prefix: dc.srv.prefix(),
|
---|
| 1555 | Command: irc.RPL_ENDOFWHOIS,
|
---|
| 1556 | Params: []string{dc.nick, dc.nick, "End of /WHOIS list"},
|
---|
| 1557 | })
|
---|
| 1558 | return nil
|
---|
| 1559 | }
|
---|
| 1560 |
|
---|
[128] | 1561 | // TODO: support WHOIS masks
|
---|
| 1562 | uc, upstreamNick, err := dc.unmarshalEntity(mask)
|
---|
| 1563 | if err != nil {
|
---|
| 1564 | return err
|
---|
| 1565 | }
|
---|
| 1566 |
|
---|
| 1567 | var params []string
|
---|
| 1568 | if target != "" {
|
---|
[299] | 1569 | if target == mask { // WHOIS nick nick
|
---|
| 1570 | params = []string{upstreamNick, upstreamNick}
|
---|
| 1571 | } else {
|
---|
| 1572 | params = []string{target, upstreamNick}
|
---|
| 1573 | }
|
---|
[128] | 1574 | } else {
|
---|
| 1575 | params = []string{upstreamNick}
|
---|
| 1576 | }
|
---|
| 1577 |
|
---|
[176] | 1578 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[128] | 1579 | Command: "WHOIS",
|
---|
| 1580 | Params: params,
|
---|
| 1581 | })
|
---|
[58] | 1582 | case "PRIVMSG":
|
---|
| 1583 | var targetsStr, text string
|
---|
| 1584 | if err := parseMessageParams(msg, &targetsStr, &text); err != nil {
|
---|
| 1585 | return err
|
---|
| 1586 | }
|
---|
[303] | 1587 | tags := copyClientTags(msg.Tags)
|
---|
[58] | 1588 |
|
---|
| 1589 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
[117] | 1590 | if name == serviceNick {
|
---|
[431] | 1591 | if dc.caps["echo-message"] {
|
---|
| 1592 | echoTags := tags.Copy()
|
---|
| 1593 | echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
| 1594 | dc.SendMessage(&irc.Message{
|
---|
| 1595 | Tags: echoTags,
|
---|
| 1596 | Prefix: dc.prefix(),
|
---|
| 1597 | Command: "PRIVMSG",
|
---|
| 1598 | Params: []string{name, text},
|
---|
| 1599 | })
|
---|
| 1600 | }
|
---|
[117] | 1601 | handleServicePRIVMSG(dc, text)
|
---|
| 1602 | continue
|
---|
| 1603 | }
|
---|
| 1604 |
|
---|
[127] | 1605 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
[58] | 1606 | if err != nil {
|
---|
| 1607 | return err
|
---|
| 1608 | }
|
---|
| 1609 |
|
---|
[95] | 1610 | if upstreamName == "NickServ" {
|
---|
| 1611 | dc.handleNickServPRIVMSG(uc, text)
|
---|
| 1612 | }
|
---|
| 1613 |
|
---|
[268] | 1614 | unmarshaledText := text
|
---|
| 1615 | if uc.isChannel(upstreamName) {
|
---|
| 1616 | unmarshaledText = dc.unmarshalText(uc, text)
|
---|
| 1617 | }
|
---|
[301] | 1618 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[303] | 1619 | Tags: tags,
|
---|
[58] | 1620 | Command: "PRIVMSG",
|
---|
[268] | 1621 | Params: []string{upstreamName, unmarshaledText},
|
---|
[60] | 1622 | })
|
---|
[105] | 1623 |
|
---|
[303] | 1624 | echoTags := tags.Copy()
|
---|
| 1625 | echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
---|
[113] | 1626 | echoMsg := &irc.Message{
|
---|
[303] | 1627 | Tags: echoTags,
|
---|
[113] | 1628 | Prefix: &irc.Prefix{
|
---|
| 1629 | Name: uc.nick,
|
---|
| 1630 | User: uc.username,
|
---|
| 1631 | },
|
---|
[114] | 1632 | Command: "PRIVMSG",
|
---|
[113] | 1633 | Params: []string{upstreamName, text},
|
---|
| 1634 | }
|
---|
[239] | 1635 | uc.produce(upstreamName, echoMsg, dc)
|
---|
[435] | 1636 |
|
---|
| 1637 | uc.updateChannelAutoDetach(upstreamName)
|
---|
[58] | 1638 | }
|
---|
[164] | 1639 | case "NOTICE":
|
---|
| 1640 | var targetsStr, text string
|
---|
| 1641 | if err := parseMessageParams(msg, &targetsStr, &text); err != nil {
|
---|
| 1642 | return err
|
---|
| 1643 | }
|
---|
[303] | 1644 | tags := copyClientTags(msg.Tags)
|
---|
[164] | 1645 |
|
---|
| 1646 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
| 1647 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1648 | if err != nil {
|
---|
| 1649 | return err
|
---|
| 1650 | }
|
---|
| 1651 |
|
---|
[268] | 1652 | unmarshaledText := text
|
---|
| 1653 | if uc.isChannel(upstreamName) {
|
---|
| 1654 | unmarshaledText = dc.unmarshalText(uc, text)
|
---|
| 1655 | }
|
---|
[301] | 1656 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[303] | 1657 | Tags: tags,
|
---|
[164] | 1658 | Command: "NOTICE",
|
---|
[268] | 1659 | Params: []string{upstreamName, unmarshaledText},
|
---|
[164] | 1660 | })
|
---|
[435] | 1661 |
|
---|
| 1662 | uc.updateChannelAutoDetach(upstreamName)
|
---|
[164] | 1663 | }
|
---|
[303] | 1664 | case "TAGMSG":
|
---|
| 1665 | var targetsStr string
|
---|
| 1666 | if err := parseMessageParams(msg, &targetsStr); err != nil {
|
---|
| 1667 | return err
|
---|
| 1668 | }
|
---|
| 1669 | tags := copyClientTags(msg.Tags)
|
---|
| 1670 |
|
---|
| 1671 | for _, name := range strings.Split(targetsStr, ",") {
|
---|
| 1672 | uc, upstreamName, err := dc.unmarshalEntity(name)
|
---|
| 1673 | if err != nil {
|
---|
| 1674 | return err
|
---|
| 1675 | }
|
---|
[427] | 1676 | if _, ok := uc.caps["message-tags"]; !ok {
|
---|
| 1677 | continue
|
---|
| 1678 | }
|
---|
[303] | 1679 |
|
---|
| 1680 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
| 1681 | Tags: tags,
|
---|
| 1682 | Command: "TAGMSG",
|
---|
| 1683 | Params: []string{upstreamName},
|
---|
| 1684 | })
|
---|
[435] | 1685 |
|
---|
| 1686 | uc.updateChannelAutoDetach(upstreamName)
|
---|
[303] | 1687 | }
|
---|
[163] | 1688 | case "INVITE":
|
---|
| 1689 | var user, channel string
|
---|
| 1690 | if err := parseMessageParams(msg, &user, &channel); err != nil {
|
---|
| 1691 | return err
|
---|
| 1692 | }
|
---|
| 1693 |
|
---|
| 1694 | ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
---|
| 1695 | if err != nil {
|
---|
| 1696 | return err
|
---|
| 1697 | }
|
---|
| 1698 |
|
---|
| 1699 | ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
---|
| 1700 | if err != nil {
|
---|
| 1701 | return err
|
---|
| 1702 | }
|
---|
| 1703 |
|
---|
| 1704 | if ucChannel != ucUser {
|
---|
| 1705 | return ircError{&irc.Message{
|
---|
| 1706 | Command: irc.ERR_USERNOTINCHANNEL,
|
---|
[401] | 1707 | Params: []string{dc.nick, user, channel, "They are on another network"},
|
---|
[163] | 1708 | }}
|
---|
| 1709 | }
|
---|
| 1710 | uc := ucChannel
|
---|
| 1711 |
|
---|
[176] | 1712 | uc.SendMessageLabeled(dc.id, &irc.Message{
|
---|
[163] | 1713 | Command: "INVITE",
|
---|
| 1714 | Params: []string{upstreamUser, upstreamChannel},
|
---|
| 1715 | })
|
---|
[319] | 1716 | case "CHATHISTORY":
|
---|
| 1717 | var subcommand string
|
---|
| 1718 | if err := parseMessageParams(msg, &subcommand); err != nil {
|
---|
| 1719 | return err
|
---|
| 1720 | }
|
---|
| 1721 | var target, criteria, limitStr string
|
---|
| 1722 | if err := parseMessageParams(msg, nil, &target, &criteria, &limitStr); err != nil {
|
---|
| 1723 | return ircError{&irc.Message{
|
---|
| 1724 | Command: "FAIL",
|
---|
| 1725 | Params: []string{"CHATHISTORY", "NEED_MORE_PARAMS", subcommand, "Missing parameters"},
|
---|
| 1726 | }}
|
---|
| 1727 | }
|
---|
| 1728 |
|
---|
[423] | 1729 | if dc.user.msgStore == nil {
|
---|
[319] | 1730 | return ircError{&irc.Message{
|
---|
| 1731 | Command: irc.ERR_UNKNOWNCOMMAND,
|
---|
| 1732 | Params: []string{dc.nick, subcommand, "Unknown command"},
|
---|
| 1733 | }}
|
---|
| 1734 | }
|
---|
| 1735 |
|
---|
| 1736 | uc, entity, err := dc.unmarshalEntity(target)
|
---|
| 1737 | if err != nil {
|
---|
| 1738 | return err
|
---|
| 1739 | }
|
---|
| 1740 |
|
---|
| 1741 | // TODO: support msgid criteria
|
---|
| 1742 | criteriaParts := strings.SplitN(criteria, "=", 2)
|
---|
| 1743 | if len(criteriaParts) != 2 || criteriaParts[0] != "timestamp" {
|
---|
| 1744 | return ircError{&irc.Message{
|
---|
| 1745 | Command: "FAIL",
|
---|
| 1746 | Params: []string{"CHATHISTORY", "UNKNOWN_CRITERIA", criteria, "Unknown criteria"},
|
---|
| 1747 | }}
|
---|
| 1748 | }
|
---|
| 1749 |
|
---|
| 1750 | timestamp, err := time.Parse(serverTimeLayout, criteriaParts[1])
|
---|
| 1751 | if err != nil {
|
---|
| 1752 | return ircError{&irc.Message{
|
---|
| 1753 | Command: "FAIL",
|
---|
| 1754 | Params: []string{"CHATHISTORY", "INVALID_CRITERIA", criteria, "Invalid criteria"},
|
---|
| 1755 | }}
|
---|
| 1756 | }
|
---|
| 1757 |
|
---|
| 1758 | limit, err := strconv.Atoi(limitStr)
|
---|
| 1759 | if err != nil || limit < 0 || limit > dc.srv.HistoryLimit {
|
---|
| 1760 | return ircError{&irc.Message{
|
---|
| 1761 | Command: "FAIL",
|
---|
| 1762 | Params: []string{"CHATHISTORY", "INVALID_LIMIT", limitStr, "Invalid limit"},
|
---|
| 1763 | }}
|
---|
| 1764 | }
|
---|
| 1765 |
|
---|
[387] | 1766 | var history []*irc.Message
|
---|
[319] | 1767 | switch subcommand {
|
---|
| 1768 | case "BEFORE":
|
---|
[423] | 1769 | history, err = dc.user.msgStore.LoadBeforeTime(uc.network, entity, timestamp, limit)
|
---|
[360] | 1770 | case "AFTER":
|
---|
[423] | 1771 | history, err = dc.user.msgStore.LoadAfterTime(uc.network, entity, timestamp, limit)
|
---|
[319] | 1772 | default:
|
---|
[360] | 1773 | // TODO: support LATEST, BETWEEN
|
---|
[319] | 1774 | return ircError{&irc.Message{
|
---|
| 1775 | Command: "FAIL",
|
---|
| 1776 | Params: []string{"CHATHISTORY", "UNKNOWN_COMMAND", subcommand, "Unknown command"},
|
---|
| 1777 | }}
|
---|
| 1778 | }
|
---|
[387] | 1779 | if err != nil {
|
---|
| 1780 | dc.logger.Printf("failed parsing log messages for chathistory: %v", err)
|
---|
| 1781 | return newChatHistoryError(subcommand, target)
|
---|
| 1782 | }
|
---|
| 1783 |
|
---|
| 1784 | batchRef := "history"
|
---|
| 1785 | dc.SendMessage(&irc.Message{
|
---|
| 1786 | Prefix: dc.srv.prefix(),
|
---|
| 1787 | Command: "BATCH",
|
---|
| 1788 | Params: []string{"+" + batchRef, "chathistory", target},
|
---|
| 1789 | })
|
---|
| 1790 |
|
---|
| 1791 | for _, msg := range history {
|
---|
| 1792 | msg.Tags["batch"] = irc.TagValue(batchRef)
|
---|
| 1793 | dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
---|
| 1794 | }
|
---|
| 1795 |
|
---|
| 1796 | dc.SendMessage(&irc.Message{
|
---|
| 1797 | Prefix: dc.srv.prefix(),
|
---|
| 1798 | Command: "BATCH",
|
---|
| 1799 | Params: []string{"-" + batchRef},
|
---|
| 1800 | })
|
---|
[13] | 1801 | default:
|
---|
[55] | 1802 | dc.logger.Printf("unhandled message: %v", msg)
|
---|
[13] | 1803 | return newUnknownCommandError(msg.Command)
|
---|
| 1804 | }
|
---|
[42] | 1805 | return nil
|
---|
[13] | 1806 | }
|
---|
[95] | 1807 |
|
---|
| 1808 | func (dc *downstreamConn) handleNickServPRIVMSG(uc *upstreamConn, text string) {
|
---|
| 1809 | username, password, ok := parseNickServCredentials(text, uc.nick)
|
---|
| 1810 | if !ok {
|
---|
| 1811 | return
|
---|
| 1812 | }
|
---|
| 1813 |
|
---|
[307] | 1814 | // User may have e.g. EXTERNAL mechanism configured. We do not want to
|
---|
| 1815 | // automatically erase the key pair or any other credentials.
|
---|
| 1816 | if uc.network.SASL.Mechanism != "" && uc.network.SASL.Mechanism != "PLAIN" {
|
---|
| 1817 | return
|
---|
| 1818 | }
|
---|
| 1819 |
|
---|
[95] | 1820 | dc.logger.Printf("auto-saving NickServ credentials with username %q", username)
|
---|
| 1821 | n := uc.network
|
---|
| 1822 | n.SASL.Mechanism = "PLAIN"
|
---|
| 1823 | n.SASL.Plain.Username = username
|
---|
| 1824 | n.SASL.Plain.Password = password
|
---|
[421] | 1825 | if err := dc.srv.db.StoreNetwork(dc.user.ID, &n.Network); err != nil {
|
---|
[95] | 1826 | dc.logger.Printf("failed to save NickServ credentials: %v", err)
|
---|
| 1827 | }
|
---|
| 1828 | }
|
---|
| 1829 |
|
---|
| 1830 | func parseNickServCredentials(text, nick string) (username, password string, ok bool) {
|
---|
| 1831 | fields := strings.Fields(text)
|
---|
| 1832 | if len(fields) < 2 {
|
---|
| 1833 | return "", "", false
|
---|
| 1834 | }
|
---|
| 1835 | cmd := strings.ToUpper(fields[0])
|
---|
| 1836 | params := fields[1:]
|
---|
| 1837 | switch cmd {
|
---|
| 1838 | case "REGISTER":
|
---|
| 1839 | username = nick
|
---|
| 1840 | password = params[0]
|
---|
| 1841 | case "IDENTIFY":
|
---|
| 1842 | if len(params) == 1 {
|
---|
| 1843 | username = nick
|
---|
[182] | 1844 | password = params[0]
|
---|
[95] | 1845 | } else {
|
---|
| 1846 | username = params[0]
|
---|
[182] | 1847 | password = params[1]
|
---|
[95] | 1848 | }
|
---|
[182] | 1849 | case "SET":
|
---|
| 1850 | if len(params) == 2 && strings.EqualFold(params[0], "PASSWORD") {
|
---|
| 1851 | username = nick
|
---|
| 1852 | password = params[1]
|
---|
| 1853 | }
|
---|
[340] | 1854 | default:
|
---|
| 1855 | return "", "", false
|
---|
[95] | 1856 | }
|
---|
| 1857 | return username, password, true
|
---|
| 1858 | }
|
---|