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