[98] | 1 | package soju
|
---|
[13] | 2 |
|
---|
| 3 | import (
|
---|
| 4 | "crypto/tls"
|
---|
[95] | 5 | "encoding/base64"
|
---|
[155] | 6 | "errors"
|
---|
[13] | 7 | "fmt"
|
---|
| 8 | "io"
|
---|
| 9 | "net"
|
---|
[178] | 10 | "os"
|
---|
| 11 | "path/filepath"
|
---|
[19] | 12 | "strconv"
|
---|
[17] | 13 | "strings"
|
---|
[19] | 14 | "time"
|
---|
[13] | 15 |
|
---|
[95] | 16 | "github.com/emersion/go-sasl"
|
---|
[13] | 17 | "gopkg.in/irc.v3"
|
---|
| 18 | )
|
---|
| 19 |
|
---|
[19] | 20 | type upstreamChannel struct {
|
---|
[162] | 21 | Name string
|
---|
| 22 | conn *upstreamConn
|
---|
| 23 | Topic string
|
---|
| 24 | TopicWho string
|
---|
| 25 | TopicTime time.Time
|
---|
| 26 | Status channelStatus
|
---|
| 27 | modes channelModes
|
---|
| 28 | creationTime string
|
---|
| 29 | Members map[string]*membership
|
---|
| 30 | complete bool
|
---|
[19] | 31 | }
|
---|
| 32 |
|
---|
[13] | 33 | type upstreamConn struct {
|
---|
[77] | 34 | network *network
|
---|
[21] | 35 | logger Logger
|
---|
[19] | 36 | net net.Conn
|
---|
| 37 | irc *irc.Conn
|
---|
| 38 | srv *Server
|
---|
[37] | 39 | user *user
|
---|
[102] | 40 | outgoing chan<- *irc.Message
|
---|
[175] | 41 | closed chan struct{}
|
---|
[16] | 42 |
|
---|
| 43 | serverName string
|
---|
| 44 | availableUserModes string
|
---|
[139] | 45 | availableChannelModes map[byte]channelModeType
|
---|
| 46 | availableChannelTypes string
|
---|
| 47 | availableMemberships []membership
|
---|
[19] | 48 |
|
---|
| 49 | registered bool
|
---|
[42] | 50 | nick string
|
---|
[77] | 51 | username string
|
---|
| 52 | realname string
|
---|
[139] | 53 | modes userModes
|
---|
[19] | 54 | channels map[string]*upstreamChannel
|
---|
[92] | 55 | caps map[string]string
|
---|
[153] | 56 | batches map[string]batch
|
---|
[95] | 57 |
|
---|
[155] | 58 | tagsSupported bool
|
---|
| 59 | labelsSupported bool
|
---|
[161] | 60 | nextLabelID uint64
|
---|
[152] | 61 |
|
---|
[95] | 62 | saslClient sasl.Client
|
---|
| 63 | saslStarted bool
|
---|
[177] | 64 |
|
---|
| 65 | // set of LIST commands in progress, per downstream
|
---|
| 66 | pendingLISTDownstreamSet map[uint64]struct{}
|
---|
[178] | 67 |
|
---|
| 68 | logs map[string]entityLog
|
---|
[13] | 69 | }
|
---|
| 70 |
|
---|
[178] | 71 | type entityLog struct {
|
---|
| 72 | name string
|
---|
| 73 | file *os.File
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[77] | 76 | func connectToUpstream(network *network) (*upstreamConn, error) {
|
---|
| 77 | logger := &prefixLogger{network.user.srv.Logger, fmt.Sprintf("upstream %q: ", network.Addr)}
|
---|
[33] | 78 |
|
---|
[77] | 79 | addr := network.Addr
|
---|
| 80 | if !strings.ContainsRune(addr, ':') {
|
---|
| 81 | addr = addr + ":6697"
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | logger.Printf("connecting to TLS server at address %q", addr)
|
---|
| 85 | netConn, err := tls.Dial("tcp", addr, nil)
|
---|
[33] | 86 | if err != nil {
|
---|
[77] | 87 | return nil, fmt.Errorf("failed to dial %q: %v", addr, err)
|
---|
[33] | 88 | }
|
---|
| 89 |
|
---|
[67] | 90 | setKeepAlive(netConn)
|
---|
| 91 |
|
---|
[102] | 92 | outgoing := make(chan *irc.Message, 64)
|
---|
[55] | 93 | uc := &upstreamConn{
|
---|
[177] | 94 | network: network,
|
---|
| 95 | logger: logger,
|
---|
| 96 | net: netConn,
|
---|
| 97 | irc: irc.NewConn(netConn),
|
---|
| 98 | srv: network.user.srv,
|
---|
| 99 | user: network.user,
|
---|
| 100 | outgoing: outgoing,
|
---|
[186] | 101 | closed: make(chan struct{}),
|
---|
[177] | 102 | channels: make(map[string]*upstreamChannel),
|
---|
| 103 | caps: make(map[string]string),
|
---|
| 104 | batches: make(map[string]batch),
|
---|
| 105 | availableChannelTypes: stdChannelTypes,
|
---|
| 106 | availableChannelModes: stdChannelModes,
|
---|
| 107 | availableMemberships: stdMemberships,
|
---|
| 108 | pendingLISTDownstreamSet: make(map[uint64]struct{}),
|
---|
[178] | 109 | logs: make(map[string]entityLog),
|
---|
[33] | 110 | }
|
---|
| 111 |
|
---|
| 112 | go func() {
|
---|
[175] | 113 | for {
|
---|
| 114 | var closed bool
|
---|
| 115 | select {
|
---|
| 116 | case msg := <-outgoing:
|
---|
| 117 | if uc.srv.Debug {
|
---|
| 118 | uc.logger.Printf("sent: %v", msg)
|
---|
| 119 | }
|
---|
| 120 | if err := uc.irc.WriteMessage(msg); err != nil {
|
---|
| 121 | uc.logger.Printf("failed to write message: %v", err)
|
---|
| 122 | }
|
---|
| 123 | case <-uc.closed:
|
---|
| 124 | closed = true
|
---|
[64] | 125 | }
|
---|
[175] | 126 | if closed {
|
---|
| 127 | break
|
---|
[33] | 128 | }
|
---|
| 129 | }
|
---|
[55] | 130 | if err := uc.net.Close(); err != nil {
|
---|
| 131 | uc.logger.Printf("failed to close connection: %v", err)
|
---|
[45] | 132 | } else {
|
---|
[55] | 133 | uc.logger.Printf("connection closed")
|
---|
[45] | 134 | }
|
---|
[33] | 135 | }()
|
---|
| 136 |
|
---|
[55] | 137 | return uc, nil
|
---|
[33] | 138 | }
|
---|
| 139 |
|
---|
[175] | 140 | func (uc *upstreamConn) isClosed() bool {
|
---|
| 141 | select {
|
---|
| 142 | case <-uc.closed:
|
---|
| 143 | return true
|
---|
| 144 | default:
|
---|
| 145 | return false
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[180] | 149 | // Close closes the connection. It is safe to call from any goroutine.
|
---|
[55] | 150 | func (uc *upstreamConn) Close() error {
|
---|
[175] | 151 | if uc.isClosed() {
|
---|
[33] | 152 | return fmt.Errorf("upstream connection already closed")
|
---|
| 153 | }
|
---|
[175] | 154 | close(uc.closed)
|
---|
[33] | 155 | return nil
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[73] | 158 | func (uc *upstreamConn) forEachDownstream(f func(*downstreamConn)) {
|
---|
| 159 | uc.user.forEachDownstream(func(dc *downstreamConn) {
|
---|
[77] | 160 | if dc.network != nil && dc.network != uc.network {
|
---|
[73] | 161 | return
|
---|
| 162 | }
|
---|
| 163 | f(dc)
|
---|
| 164 | })
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[161] | 167 | func (uc *upstreamConn) forEachDownstreamByID(id uint64, f func(*downstreamConn)) {
|
---|
[155] | 168 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 169 | if id != 0 && id != dc.id {
|
---|
| 170 | return
|
---|
| 171 | }
|
---|
| 172 | f(dc)
|
---|
| 173 | })
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[55] | 176 | func (uc *upstreamConn) getChannel(name string) (*upstreamChannel, error) {
|
---|
| 177 | ch, ok := uc.channels[name]
|
---|
[19] | 178 | if !ok {
|
---|
| 179 | return nil, fmt.Errorf("unknown channel %q", name)
|
---|
| 180 | }
|
---|
| 181 | return ch, nil
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[129] | 184 | func (uc *upstreamConn) isChannel(entity string) bool {
|
---|
[139] | 185 | if i := strings.IndexByte(uc.availableChannelTypes, entity[0]); i >= 0 {
|
---|
| 186 | return true
|
---|
[129] | 187 | }
|
---|
| 188 | return false
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[181] | 191 | func (uc *upstreamConn) getPendingLIST() *pendingLIST {
|
---|
[177] | 192 | for _, pl := range uc.user.pendingLISTs {
|
---|
| 193 | if _, ok := pl.pendingCommands[uc.network.ID]; !ok {
|
---|
| 194 | continue
|
---|
| 195 | }
|
---|
| 196 | return &pl
|
---|
| 197 | }
|
---|
| 198 | return nil
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[181] | 201 | func (uc *upstreamConn) endPendingLISTs(all bool) (found bool) {
|
---|
[177] | 202 | found = false
|
---|
| 203 | for i := 0; i < len(uc.user.pendingLISTs); i++ {
|
---|
| 204 | pl := uc.user.pendingLISTs[i]
|
---|
| 205 | if _, ok := pl.pendingCommands[uc.network.ID]; !ok {
|
---|
| 206 | continue
|
---|
| 207 | }
|
---|
| 208 | delete(pl.pendingCommands, uc.network.ID)
|
---|
| 209 | if len(pl.pendingCommands) == 0 {
|
---|
| 210 | uc.user.pendingLISTs = append(uc.user.pendingLISTs[:i], uc.user.pendingLISTs[i+1:]...)
|
---|
| 211 | i--
|
---|
| 212 | uc.forEachDownstreamByID(pl.downstreamID, func(dc *downstreamConn) {
|
---|
| 213 | dc.SendMessage(&irc.Message{
|
---|
| 214 | Prefix: dc.srv.prefix(),
|
---|
| 215 | Command: irc.RPL_LISTEND,
|
---|
| 216 | Params: []string{dc.nick, "End of /LIST"},
|
---|
| 217 | })
|
---|
| 218 | })
|
---|
| 219 | }
|
---|
| 220 | found = true
|
---|
| 221 | if !all {
|
---|
| 222 | delete(uc.pendingLISTDownstreamSet, pl.downstreamID)
|
---|
| 223 | uc.user.forEachUpstream(func(uc *upstreamConn) {
|
---|
[181] | 224 | uc.trySendLIST(pl.downstreamID)
|
---|
[177] | 225 | })
|
---|
| 226 | return
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | return
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[181] | 232 | func (uc *upstreamConn) trySendLIST(downstreamID uint64) {
|
---|
[177] | 233 | // must be called with a lock in uc.user.pendingLISTsLock
|
---|
| 234 |
|
---|
| 235 | if _, ok := uc.pendingLISTDownstreamSet[downstreamID]; ok {
|
---|
| 236 | // a LIST command is already pending
|
---|
| 237 | // we will try again when that command is completed
|
---|
| 238 | return
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | for _, pl := range uc.user.pendingLISTs {
|
---|
| 242 | if pl.downstreamID != downstreamID {
|
---|
| 243 | continue
|
---|
| 244 | }
|
---|
| 245 | // this is the first pending LIST command list of the downstream
|
---|
| 246 | listCommand, ok := pl.pendingCommands[uc.network.ID]
|
---|
| 247 | if !ok {
|
---|
| 248 | // there is no command for this upstream in these LIST commands
|
---|
| 249 | // do not send anything
|
---|
| 250 | continue
|
---|
| 251 | }
|
---|
| 252 | // there is a command for this upstream in these LIST commands
|
---|
| 253 | // send it now
|
---|
| 254 |
|
---|
| 255 | uc.SendMessageLabeled(downstreamID, listCommand)
|
---|
| 256 |
|
---|
| 257 | uc.pendingLISTDownstreamSet[downstreamID] = struct{}{}
|
---|
| 258 | return
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[139] | 262 | func (uc *upstreamConn) parseMembershipPrefix(s string) (membership *membership, nick string) {
|
---|
| 263 | for _, m := range uc.availableMemberships {
|
---|
| 264 | if m.Prefix == s[0] {
|
---|
| 265 | return &m, s[1:]
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 | return nil, s
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[55] | 271 | func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
|
---|
[155] | 272 | var label string
|
---|
| 273 | if l, ok := msg.GetTag("label"); ok {
|
---|
| 274 | label = l
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[153] | 277 | var msgBatch *batch
|
---|
| 278 | if batchName, ok := msg.GetTag("batch"); ok {
|
---|
| 279 | b, ok := uc.batches[batchName]
|
---|
| 280 | if !ok {
|
---|
| 281 | return fmt.Errorf("unexpected batch reference: batch was not defined: %q", batchName)
|
---|
| 282 | }
|
---|
| 283 | msgBatch = &b
|
---|
[155] | 284 | if label == "" {
|
---|
| 285 | label = msgBatch.Label
|
---|
| 286 | }
|
---|
[153] | 287 | }
|
---|
| 288 |
|
---|
[161] | 289 | var downstreamID uint64 = 0
|
---|
[155] | 290 | if label != "" {
|
---|
| 291 | var labelOffset uint64
|
---|
[161] | 292 | n, err := fmt.Sscanf(label, "sd-%d-%d", &downstreamID, &labelOffset)
|
---|
[155] | 293 | if err == nil && n < 2 {
|
---|
| 294 | err = errors.New("not enough arguments")
|
---|
| 295 | }
|
---|
| 296 | if err != nil {
|
---|
| 297 | return fmt.Errorf("unexpected message label: invalid downstream reference for label %q: %v", label, err)
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[13] | 301 | switch msg.Command {
|
---|
| 302 | case "PING":
|
---|
[60] | 303 | uc.SendMessage(&irc.Message{
|
---|
[13] | 304 | Command: "PONG",
|
---|
[68] | 305 | Params: msg.Params,
|
---|
[60] | 306 | })
|
---|
[33] | 307 | return nil
|
---|
[18] | 308 | case "NOTICE":
|
---|
[55] | 309 | uc.logger.Print(msg)
|
---|
[97] | 310 |
|
---|
[171] | 311 | if msg.Prefix.User == "" && msg.Prefix.Host == "" { // server message
|
---|
| 312 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 313 | dc.SendMessage(msg)
|
---|
| 314 | })
|
---|
| 315 | } else { // regular user NOTICE
|
---|
| 316 | var nick, text string
|
---|
| 317 | if err := parseMessageParams(msg, &nick, &text); err != nil {
|
---|
| 318 | return err
|
---|
| 319 | }
|
---|
| 320 |
|
---|
[178] | 321 | target := nick
|
---|
| 322 | if nick == uc.nick {
|
---|
| 323 | target = msg.Prefix.Name
|
---|
| 324 | }
|
---|
[187] | 325 | uc.appendLog(target, "<%s> %s", msg.Prefix.Name, text)
|
---|
[178] | 326 |
|
---|
[171] | 327 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 328 | dc.SendMessage(&irc.Message{
|
---|
| 329 | Prefix: dc.marshalUserPrefix(uc, msg.Prefix),
|
---|
| 330 | Command: "NOTICE",
|
---|
| 331 | Params: []string{dc.marshalEntity(uc, nick), text},
|
---|
| 332 | })
|
---|
| 333 | })
|
---|
| 334 | }
|
---|
[92] | 335 | case "CAP":
|
---|
[95] | 336 | var subCmd string
|
---|
| 337 | if err := parseMessageParams(msg, nil, &subCmd); err != nil {
|
---|
| 338 | return err
|
---|
[92] | 339 | }
|
---|
[95] | 340 | subCmd = strings.ToUpper(subCmd)
|
---|
| 341 | subParams := msg.Params[2:]
|
---|
| 342 | switch subCmd {
|
---|
| 343 | case "LS":
|
---|
| 344 | if len(subParams) < 1 {
|
---|
| 345 | return newNeedMoreParamsError(msg.Command)
|
---|
| 346 | }
|
---|
| 347 | caps := strings.Fields(subParams[len(subParams)-1])
|
---|
| 348 | more := len(subParams) >= 2 && msg.Params[len(subParams)-2] == "*"
|
---|
[92] | 349 |
|
---|
[95] | 350 | for _, s := range caps {
|
---|
| 351 | kv := strings.SplitN(s, "=", 2)
|
---|
| 352 | k := strings.ToLower(kv[0])
|
---|
| 353 | var v string
|
---|
| 354 | if len(kv) == 2 {
|
---|
| 355 | v = kv[1]
|
---|
| 356 | }
|
---|
| 357 | uc.caps[k] = v
|
---|
[92] | 358 | }
|
---|
| 359 |
|
---|
[95] | 360 | if more {
|
---|
| 361 | break // wait to receive all capabilities
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[152] | 364 | requestCaps := make([]string, 0, 16)
|
---|
[155] | 365 | for _, c := range []string{"message-tags", "batch", "labeled-response"} {
|
---|
[152] | 366 | if _, ok := uc.caps[c]; ok {
|
---|
| 367 | requestCaps = append(requestCaps, c)
|
---|
| 368 | }
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[95] | 371 | if uc.requestSASL() {
|
---|
[152] | 372 | requestCaps = append(requestCaps, "sasl")
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | if len(requestCaps) > 0 {
|
---|
[95] | 376 | uc.SendMessage(&irc.Message{
|
---|
| 377 | Command: "CAP",
|
---|
[152] | 378 | Params: []string{"REQ", strings.Join(requestCaps, " ")},
|
---|
[95] | 379 | })
|
---|
[152] | 380 | }
|
---|
| 381 |
|
---|
| 382 | if uc.requestSASL() {
|
---|
[95] | 383 | break // we'll send CAP END after authentication is completed
|
---|
| 384 | }
|
---|
| 385 |
|
---|
[92] | 386 | uc.SendMessage(&irc.Message{
|
---|
| 387 | Command: "CAP",
|
---|
| 388 | Params: []string{"END"},
|
---|
| 389 | })
|
---|
[95] | 390 | case "ACK", "NAK":
|
---|
| 391 | if len(subParams) < 1 {
|
---|
| 392 | return newNeedMoreParamsError(msg.Command)
|
---|
| 393 | }
|
---|
| 394 | caps := strings.Fields(subParams[0])
|
---|
| 395 |
|
---|
| 396 | for _, name := range caps {
|
---|
| 397 | if err := uc.handleCapAck(strings.ToLower(name), subCmd == "ACK"); err != nil {
|
---|
| 398 | return err
|
---|
| 399 | }
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | if uc.saslClient == nil {
|
---|
| 403 | uc.SendMessage(&irc.Message{
|
---|
| 404 | Command: "CAP",
|
---|
| 405 | Params: []string{"END"},
|
---|
| 406 | })
|
---|
| 407 | }
|
---|
| 408 | default:
|
---|
| 409 | uc.logger.Printf("unhandled message: %v", msg)
|
---|
[92] | 410 | }
|
---|
[95] | 411 | case "AUTHENTICATE":
|
---|
| 412 | if uc.saslClient == nil {
|
---|
| 413 | return fmt.Errorf("received unexpected AUTHENTICATE message")
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | // TODO: if a challenge is 400 bytes long, buffer it
|
---|
| 417 | var challengeStr string
|
---|
| 418 | if err := parseMessageParams(msg, &challengeStr); err != nil {
|
---|
| 419 | uc.SendMessage(&irc.Message{
|
---|
| 420 | Command: "AUTHENTICATE",
|
---|
| 421 | Params: []string{"*"},
|
---|
| 422 | })
|
---|
| 423 | return err
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | var challenge []byte
|
---|
| 427 | if challengeStr != "+" {
|
---|
| 428 | var err error
|
---|
| 429 | challenge, err = base64.StdEncoding.DecodeString(challengeStr)
|
---|
| 430 | if err != nil {
|
---|
| 431 | uc.SendMessage(&irc.Message{
|
---|
| 432 | Command: "AUTHENTICATE",
|
---|
| 433 | Params: []string{"*"},
|
---|
| 434 | })
|
---|
| 435 | return err
|
---|
| 436 | }
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | var resp []byte
|
---|
| 440 | var err error
|
---|
| 441 | if !uc.saslStarted {
|
---|
| 442 | _, resp, err = uc.saslClient.Start()
|
---|
| 443 | uc.saslStarted = true
|
---|
| 444 | } else {
|
---|
| 445 | resp, err = uc.saslClient.Next(challenge)
|
---|
| 446 | }
|
---|
| 447 | if err != nil {
|
---|
| 448 | uc.SendMessage(&irc.Message{
|
---|
| 449 | Command: "AUTHENTICATE",
|
---|
| 450 | Params: []string{"*"},
|
---|
| 451 | })
|
---|
| 452 | return err
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | // TODO: send response in multiple chunks if >= 400 bytes
|
---|
| 456 | var respStr = "+"
|
---|
| 457 | if resp != nil {
|
---|
| 458 | respStr = base64.StdEncoding.EncodeToString(resp)
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | uc.SendMessage(&irc.Message{
|
---|
| 462 | Command: "AUTHENTICATE",
|
---|
| 463 | Params: []string{respStr},
|
---|
| 464 | })
|
---|
[125] | 465 | case irc.RPL_LOGGEDIN:
|
---|
[95] | 466 | var account string
|
---|
| 467 | if err := parseMessageParams(msg, nil, nil, &account); err != nil {
|
---|
| 468 | return err
|
---|
| 469 | }
|
---|
| 470 | uc.logger.Printf("logged in with account %q", account)
|
---|
[125] | 471 | case irc.RPL_LOGGEDOUT:
|
---|
[95] | 472 | uc.logger.Printf("logged out")
|
---|
[125] | 473 | case irc.ERR_NICKLOCKED, irc.RPL_SASLSUCCESS, irc.ERR_SASLFAIL, irc.ERR_SASLTOOLONG, irc.ERR_SASLABORTED:
|
---|
[95] | 474 | var info string
|
---|
| 475 | if err := parseMessageParams(msg, nil, &info); err != nil {
|
---|
| 476 | return err
|
---|
| 477 | }
|
---|
| 478 | switch msg.Command {
|
---|
[125] | 479 | case irc.ERR_NICKLOCKED:
|
---|
[95] | 480 | uc.logger.Printf("invalid nick used with SASL authentication: %v", info)
|
---|
[125] | 481 | case irc.ERR_SASLFAIL:
|
---|
[95] | 482 | uc.logger.Printf("SASL authentication failed: %v", info)
|
---|
[125] | 483 | case irc.ERR_SASLTOOLONG:
|
---|
[95] | 484 | uc.logger.Printf("SASL message too long: %v", info)
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | uc.saslClient = nil
|
---|
| 488 | uc.saslStarted = false
|
---|
| 489 |
|
---|
| 490 | uc.SendMessage(&irc.Message{
|
---|
| 491 | Command: "CAP",
|
---|
| 492 | Params: []string{"END"},
|
---|
| 493 | })
|
---|
[14] | 494 | case irc.RPL_WELCOME:
|
---|
[55] | 495 | uc.registered = true
|
---|
| 496 | uc.logger.Printf("connection registered")
|
---|
[19] | 497 |
|
---|
[77] | 498 | channels, err := uc.srv.db.ListChannels(uc.network.ID)
|
---|
| 499 | if err != nil {
|
---|
| 500 | uc.logger.Printf("failed to list channels from database: %v", err)
|
---|
| 501 | break
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | for _, ch := range channels {
|
---|
[146] | 505 | params := []string{ch.Name}
|
---|
| 506 | if ch.Key != "" {
|
---|
| 507 | params = append(params, ch.Key)
|
---|
| 508 | }
|
---|
[60] | 509 | uc.SendMessage(&irc.Message{
|
---|
[19] | 510 | Command: "JOIN",
|
---|
[146] | 511 | Params: params,
|
---|
[60] | 512 | })
|
---|
[19] | 513 | }
|
---|
[16] | 514 | case irc.RPL_MYINFO:
|
---|
[139] | 515 | if err := parseMessageParams(msg, nil, &uc.serverName, nil, &uc.availableUserModes, nil); err != nil {
|
---|
[43] | 516 | return err
|
---|
[16] | 517 | }
|
---|
[139] | 518 | case irc.RPL_ISUPPORT:
|
---|
| 519 | if err := parseMessageParams(msg, nil, nil); err != nil {
|
---|
| 520 | return err
|
---|
[16] | 521 | }
|
---|
[139] | 522 | for _, token := range msg.Params[1 : len(msg.Params)-1] {
|
---|
| 523 | negate := false
|
---|
| 524 | parameter := token
|
---|
| 525 | value := ""
|
---|
| 526 | if strings.HasPrefix(token, "-") {
|
---|
| 527 | negate = true
|
---|
| 528 | token = token[1:]
|
---|
| 529 | } else {
|
---|
| 530 | if i := strings.IndexByte(token, '='); i >= 0 {
|
---|
| 531 | parameter = token[:i]
|
---|
| 532 | value = token[i+1:]
|
---|
| 533 | }
|
---|
| 534 | }
|
---|
| 535 | if !negate {
|
---|
| 536 | switch parameter {
|
---|
| 537 | case "CHANMODES":
|
---|
| 538 | parts := strings.SplitN(value, ",", 5)
|
---|
| 539 | if len(parts) < 4 {
|
---|
| 540 | return fmt.Errorf("malformed ISUPPORT CHANMODES value: %v", value)
|
---|
| 541 | }
|
---|
| 542 | modes := make(map[byte]channelModeType)
|
---|
| 543 | for i, mt := range []channelModeType{modeTypeA, modeTypeB, modeTypeC, modeTypeD} {
|
---|
| 544 | for j := 0; j < len(parts[i]); j++ {
|
---|
| 545 | mode := parts[i][j]
|
---|
| 546 | modes[mode] = mt
|
---|
| 547 | }
|
---|
| 548 | }
|
---|
| 549 | uc.availableChannelModes = modes
|
---|
| 550 | case "CHANTYPES":
|
---|
| 551 | uc.availableChannelTypes = value
|
---|
| 552 | case "PREFIX":
|
---|
| 553 | if value == "" {
|
---|
| 554 | uc.availableMemberships = nil
|
---|
| 555 | } else {
|
---|
| 556 | if value[0] != '(' {
|
---|
| 557 | return fmt.Errorf("malformed ISUPPORT PREFIX value: %v", value)
|
---|
| 558 | }
|
---|
| 559 | sep := strings.IndexByte(value, ')')
|
---|
| 560 | if sep < 0 || len(value) != sep*2 {
|
---|
| 561 | return fmt.Errorf("malformed ISUPPORT PREFIX value: %v", value)
|
---|
| 562 | }
|
---|
| 563 | memberships := make([]membership, len(value)/2-1)
|
---|
| 564 | for i := range memberships {
|
---|
| 565 | memberships[i] = membership{
|
---|
| 566 | Mode: value[i+1],
|
---|
| 567 | Prefix: value[sep+i+1],
|
---|
| 568 | }
|
---|
| 569 | }
|
---|
| 570 | uc.availableMemberships = memberships
|
---|
| 571 | }
|
---|
| 572 | }
|
---|
| 573 | } else {
|
---|
| 574 | // TODO: handle ISUPPORT negations
|
---|
| 575 | }
|
---|
| 576 | }
|
---|
[153] | 577 | case "BATCH":
|
---|
| 578 | var tag string
|
---|
| 579 | if err := parseMessageParams(msg, &tag); err != nil {
|
---|
| 580 | return err
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | if strings.HasPrefix(tag, "+") {
|
---|
| 584 | tag = tag[1:]
|
---|
| 585 | if _, ok := uc.batches[tag]; ok {
|
---|
| 586 | return fmt.Errorf("unexpected BATCH reference tag: batch was already defined: %q", tag)
|
---|
| 587 | }
|
---|
| 588 | var batchType string
|
---|
| 589 | if err := parseMessageParams(msg, nil, &batchType); err != nil {
|
---|
| 590 | return err
|
---|
| 591 | }
|
---|
[155] | 592 | label := label
|
---|
| 593 | if label == "" && msgBatch != nil {
|
---|
| 594 | label = msgBatch.Label
|
---|
| 595 | }
|
---|
[153] | 596 | uc.batches[tag] = batch{
|
---|
| 597 | Type: batchType,
|
---|
| 598 | Params: msg.Params[2:],
|
---|
| 599 | Outer: msgBatch,
|
---|
[155] | 600 | Label: label,
|
---|
[153] | 601 | }
|
---|
| 602 | } else if strings.HasPrefix(tag, "-") {
|
---|
| 603 | tag = tag[1:]
|
---|
| 604 | if _, ok := uc.batches[tag]; !ok {
|
---|
| 605 | return fmt.Errorf("unknown BATCH reference tag: %q", tag)
|
---|
| 606 | }
|
---|
| 607 | delete(uc.batches, tag)
|
---|
| 608 | } else {
|
---|
| 609 | return fmt.Errorf("unexpected BATCH reference tag: missing +/- prefix: %q", tag)
|
---|
| 610 | }
|
---|
[42] | 611 | case "NICK":
|
---|
[83] | 612 | if msg.Prefix == nil {
|
---|
| 613 | return fmt.Errorf("expected a prefix")
|
---|
| 614 | }
|
---|
| 615 |
|
---|
[43] | 616 | var newNick string
|
---|
| 617 | if err := parseMessageParams(msg, &newNick); err != nil {
|
---|
| 618 | return err
|
---|
[42] | 619 | }
|
---|
| 620 |
|
---|
[55] | 621 | if msg.Prefix.Name == uc.nick {
|
---|
| 622 | uc.logger.Printf("changed nick from %q to %q", uc.nick, newNick)
|
---|
| 623 | uc.nick = newNick
|
---|
[42] | 624 | }
|
---|
| 625 |
|
---|
[55] | 626 | for _, ch := range uc.channels {
|
---|
[42] | 627 | if membership, ok := ch.Members[msg.Prefix.Name]; ok {
|
---|
| 628 | delete(ch.Members, msg.Prefix.Name)
|
---|
| 629 | ch.Members[newNick] = membership
|
---|
[187] | 630 | uc.appendLog(ch.Name, "*** %s is now known as %s", msg.Prefix.Name, newNick)
|
---|
[42] | 631 | }
|
---|
| 632 | }
|
---|
[82] | 633 |
|
---|
| 634 | if msg.Prefix.Name != uc.nick {
|
---|
| 635 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 636 | dc.SendMessage(&irc.Message{
|
---|
| 637 | Prefix: dc.marshalUserPrefix(uc, msg.Prefix),
|
---|
| 638 | Command: "NICK",
|
---|
| 639 | Params: []string{newNick},
|
---|
| 640 | })
|
---|
| 641 | })
|
---|
| 642 | }
|
---|
[69] | 643 | case "JOIN":
|
---|
| 644 | if msg.Prefix == nil {
|
---|
| 645 | return fmt.Errorf("expected a prefix")
|
---|
| 646 | }
|
---|
[42] | 647 |
|
---|
[43] | 648 | var channels string
|
---|
| 649 | if err := parseMessageParams(msg, &channels); err != nil {
|
---|
| 650 | return err
|
---|
[19] | 651 | }
|
---|
[34] | 652 |
|
---|
[43] | 653 | for _, ch := range strings.Split(channels, ",") {
|
---|
[55] | 654 | if msg.Prefix.Name == uc.nick {
|
---|
| 655 | uc.logger.Printf("joined channel %q", ch)
|
---|
| 656 | uc.channels[ch] = &upstreamChannel{
|
---|
[34] | 657 | Name: ch,
|
---|
[55] | 658 | conn: uc,
|
---|
[139] | 659 | Members: make(map[string]*membership),
|
---|
[34] | 660 | }
|
---|
[139] | 661 |
|
---|
| 662 | uc.SendMessage(&irc.Message{
|
---|
| 663 | Command: "MODE",
|
---|
| 664 | Params: []string{ch},
|
---|
| 665 | })
|
---|
[34] | 666 | } else {
|
---|
[55] | 667 | ch, err := uc.getChannel(ch)
|
---|
[34] | 668 | if err != nil {
|
---|
| 669 | return err
|
---|
| 670 | }
|
---|
[139] | 671 | ch.Members[msg.Prefix.Name] = nil
|
---|
[19] | 672 | }
|
---|
[69] | 673 |
|
---|
[187] | 674 | uc.appendLog(ch, "*** Joins: %s (%s@%s)", msg.Prefix.Name, msg.Prefix.User, msg.Prefix.Host)
|
---|
[178] | 675 |
|
---|
[73] | 676 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[69] | 677 | dc.SendMessage(&irc.Message{
|
---|
| 678 | Prefix: dc.marshalUserPrefix(uc, msg.Prefix),
|
---|
| 679 | Command: "JOIN",
|
---|
| 680 | Params: []string{dc.marshalChannel(uc, ch)},
|
---|
| 681 | })
|
---|
| 682 | })
|
---|
[19] | 683 | }
|
---|
[69] | 684 | case "PART":
|
---|
| 685 | if msg.Prefix == nil {
|
---|
| 686 | return fmt.Errorf("expected a prefix")
|
---|
| 687 | }
|
---|
[34] | 688 |
|
---|
[43] | 689 | var channels string
|
---|
| 690 | if err := parseMessageParams(msg, &channels); err != nil {
|
---|
| 691 | return err
|
---|
[34] | 692 | }
|
---|
| 693 |
|
---|
[178] | 694 | var reason string
|
---|
| 695 | if len(msg.Params) > 1 {
|
---|
| 696 | reason = msg.Params[1]
|
---|
| 697 | }
|
---|
| 698 |
|
---|
[43] | 699 | for _, ch := range strings.Split(channels, ",") {
|
---|
[55] | 700 | if msg.Prefix.Name == uc.nick {
|
---|
| 701 | uc.logger.Printf("parted channel %q", ch)
|
---|
| 702 | delete(uc.channels, ch)
|
---|
[34] | 703 | } else {
|
---|
[55] | 704 | ch, err := uc.getChannel(ch)
|
---|
[34] | 705 | if err != nil {
|
---|
| 706 | return err
|
---|
| 707 | }
|
---|
| 708 | delete(ch.Members, msg.Prefix.Name)
|
---|
| 709 | }
|
---|
[69] | 710 |
|
---|
[187] | 711 | uc.appendLog(ch, "*** Parts: %s (%s@%s) (%s)", msg.Prefix.Name, msg.Prefix.User, msg.Prefix.Host, reason)
|
---|
[178] | 712 |
|
---|
[73] | 713 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[69] | 714 | dc.SendMessage(&irc.Message{
|
---|
| 715 | Prefix: dc.marshalUserPrefix(uc, msg.Prefix),
|
---|
| 716 | Command: "PART",
|
---|
| 717 | Params: []string{dc.marshalChannel(uc, ch)},
|
---|
| 718 | })
|
---|
| 719 | })
|
---|
[34] | 720 | }
|
---|
[159] | 721 | case "KICK":
|
---|
| 722 | if msg.Prefix == nil {
|
---|
| 723 | return fmt.Errorf("expected a prefix")
|
---|
| 724 | }
|
---|
| 725 |
|
---|
| 726 | var channel, user string
|
---|
| 727 | if err := parseMessageParams(msg, &channel, &user); err != nil {
|
---|
| 728 | return err
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | var reason string
|
---|
| 732 | if len(msg.Params) > 2 {
|
---|
| 733 | reason = msg.Params[1]
|
---|
| 734 | }
|
---|
| 735 |
|
---|
| 736 | if user == uc.nick {
|
---|
| 737 | uc.logger.Printf("kicked from channel %q by %s", channel, msg.Prefix.Name)
|
---|
| 738 | delete(uc.channels, channel)
|
---|
| 739 | } else {
|
---|
| 740 | ch, err := uc.getChannel(channel)
|
---|
| 741 | if err != nil {
|
---|
| 742 | return err
|
---|
| 743 | }
|
---|
| 744 | delete(ch.Members, user)
|
---|
| 745 | }
|
---|
| 746 |
|
---|
[187] | 747 | uc.appendLog(channel, "*** %s was kicked by %s (%s)", user, msg.Prefix.Name, reason)
|
---|
[178] | 748 |
|
---|
[159] | 749 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 750 | params := []string{dc.marshalChannel(uc, channel), dc.marshalNick(uc, user)}
|
---|
| 751 | if reason != "" {
|
---|
| 752 | params = append(params, reason)
|
---|
| 753 | }
|
---|
| 754 | dc.SendMessage(&irc.Message{
|
---|
| 755 | Prefix: dc.marshalUserPrefix(uc, msg.Prefix),
|
---|
| 756 | Command: "KICK",
|
---|
| 757 | Params: params,
|
---|
| 758 | })
|
---|
| 759 | })
|
---|
[83] | 760 | case "QUIT":
|
---|
| 761 | if msg.Prefix == nil {
|
---|
| 762 | return fmt.Errorf("expected a prefix")
|
---|
| 763 | }
|
---|
| 764 |
|
---|
[178] | 765 | var reason string
|
---|
| 766 | if len(msg.Params) > 0 {
|
---|
| 767 | reason = msg.Params[0]
|
---|
| 768 | }
|
---|
| 769 |
|
---|
[83] | 770 | if msg.Prefix.Name == uc.nick {
|
---|
| 771 | uc.logger.Printf("quit")
|
---|
| 772 | }
|
---|
| 773 |
|
---|
| 774 | for _, ch := range uc.channels {
|
---|
[178] | 775 | if _, ok := ch.Members[msg.Prefix.Name]; ok {
|
---|
| 776 | delete(ch.Members, msg.Prefix.Name)
|
---|
| 777 |
|
---|
[187] | 778 | uc.appendLog(ch.Name, "*** Quits: %s (%s@%s) (%s)", msg.Prefix.Name, msg.Prefix.User, msg.Prefix.Host, reason)
|
---|
[178] | 779 | }
|
---|
[83] | 780 | }
|
---|
| 781 |
|
---|
| 782 | if msg.Prefix.Name != uc.nick {
|
---|
| 783 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 784 | dc.SendMessage(&irc.Message{
|
---|
| 785 | Prefix: dc.marshalUserPrefix(uc, msg.Prefix),
|
---|
| 786 | Command: "QUIT",
|
---|
| 787 | Params: msg.Params,
|
---|
| 788 | })
|
---|
| 789 | })
|
---|
| 790 | }
|
---|
[19] | 791 | case irc.RPL_TOPIC, irc.RPL_NOTOPIC:
|
---|
[43] | 792 | var name, topic string
|
---|
| 793 | if err := parseMessageParams(msg, nil, &name, &topic); err != nil {
|
---|
| 794 | return err
|
---|
[19] | 795 | }
|
---|
[55] | 796 | ch, err := uc.getChannel(name)
|
---|
[19] | 797 | if err != nil {
|
---|
| 798 | return err
|
---|
| 799 | }
|
---|
| 800 | if msg.Command == irc.RPL_TOPIC {
|
---|
[43] | 801 | ch.Topic = topic
|
---|
[19] | 802 | } else {
|
---|
| 803 | ch.Topic = ""
|
---|
| 804 | }
|
---|
| 805 | case "TOPIC":
|
---|
[43] | 806 | var name string
|
---|
[74] | 807 | if err := parseMessageParams(msg, &name); err != nil {
|
---|
[43] | 808 | return err
|
---|
[19] | 809 | }
|
---|
[55] | 810 | ch, err := uc.getChannel(name)
|
---|
[19] | 811 | if err != nil {
|
---|
| 812 | return err
|
---|
| 813 | }
|
---|
| 814 | if len(msg.Params) > 1 {
|
---|
| 815 | ch.Topic = msg.Params[1]
|
---|
| 816 | } else {
|
---|
| 817 | ch.Topic = ""
|
---|
| 818 | }
|
---|
[74] | 819 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 820 | params := []string{dc.marshalChannel(uc, name)}
|
---|
| 821 | if ch.Topic != "" {
|
---|
| 822 | params = append(params, ch.Topic)
|
---|
| 823 | }
|
---|
| 824 | dc.SendMessage(&irc.Message{
|
---|
| 825 | Prefix: dc.marshalUserPrefix(uc, msg.Prefix),
|
---|
| 826 | Command: "TOPIC",
|
---|
| 827 | Params: params,
|
---|
| 828 | })
|
---|
| 829 | })
|
---|
[139] | 830 | case "MODE":
|
---|
| 831 | var name, modeStr string
|
---|
| 832 | if err := parseMessageParams(msg, &name, &modeStr); err != nil {
|
---|
| 833 | return err
|
---|
| 834 | }
|
---|
| 835 |
|
---|
| 836 | if !uc.isChannel(name) { // user mode change
|
---|
| 837 | if name != uc.nick {
|
---|
| 838 | return fmt.Errorf("received MODE message for unknown nick %q", name)
|
---|
| 839 | }
|
---|
| 840 | return uc.modes.Apply(modeStr)
|
---|
| 841 | // TODO: notify downstreams about user mode change?
|
---|
| 842 | } else { // channel mode change
|
---|
| 843 | ch, err := uc.getChannel(name)
|
---|
| 844 | if err != nil {
|
---|
| 845 | return err
|
---|
| 846 | }
|
---|
| 847 |
|
---|
| 848 | if ch.modes != nil {
|
---|
| 849 | if err := ch.modes.Apply(uc.availableChannelModes, modeStr, msg.Params[2:]...); err != nil {
|
---|
| 850 | return err
|
---|
| 851 | }
|
---|
| 852 | }
|
---|
| 853 |
|
---|
[178] | 854 | modeMsg := modeStr
|
---|
| 855 | for _, v := range msg.Params[2:] {
|
---|
| 856 | modeMsg += " " + v
|
---|
| 857 | }
|
---|
[187] | 858 | uc.appendLog(ch.Name, "*** %s sets mode: %s", msg.Prefix.Name, modeMsg)
|
---|
[178] | 859 |
|
---|
[139] | 860 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 861 | params := []string{dc.marshalChannel(uc, name), modeStr}
|
---|
| 862 | params = append(params, msg.Params[2:]...)
|
---|
| 863 |
|
---|
| 864 | dc.SendMessage(&irc.Message{
|
---|
| 865 | Prefix: dc.marshalUserPrefix(uc, msg.Prefix),
|
---|
| 866 | Command: "MODE",
|
---|
| 867 | Params: params,
|
---|
| 868 | })
|
---|
| 869 | })
|
---|
| 870 | }
|
---|
| 871 | case irc.RPL_UMODEIS:
|
---|
| 872 | if err := parseMessageParams(msg, nil); err != nil {
|
---|
| 873 | return err
|
---|
| 874 | }
|
---|
| 875 | modeStr := ""
|
---|
| 876 | if len(msg.Params) > 1 {
|
---|
| 877 | modeStr = msg.Params[1]
|
---|
| 878 | }
|
---|
| 879 |
|
---|
| 880 | uc.modes = ""
|
---|
| 881 | if err := uc.modes.Apply(modeStr); err != nil {
|
---|
| 882 | return err
|
---|
| 883 | }
|
---|
| 884 | // TODO: send RPL_UMODEIS to downstream connections when applicable
|
---|
| 885 | case irc.RPL_CHANNELMODEIS:
|
---|
| 886 | var channel string
|
---|
| 887 | if err := parseMessageParams(msg, nil, &channel); err != nil {
|
---|
| 888 | return err
|
---|
| 889 | }
|
---|
| 890 | modeStr := ""
|
---|
| 891 | if len(msg.Params) > 2 {
|
---|
| 892 | modeStr = msg.Params[2]
|
---|
| 893 | }
|
---|
| 894 |
|
---|
| 895 | ch, err := uc.getChannel(channel)
|
---|
| 896 | if err != nil {
|
---|
| 897 | return err
|
---|
| 898 | }
|
---|
| 899 |
|
---|
| 900 | firstMode := ch.modes == nil
|
---|
| 901 | ch.modes = make(map[byte]string)
|
---|
| 902 | if err := ch.modes.Apply(uc.availableChannelModes, modeStr, msg.Params[3:]...); err != nil {
|
---|
| 903 | return err
|
---|
| 904 | }
|
---|
| 905 | if firstMode {
|
---|
| 906 | modeStr, modeParams := ch.modes.Format()
|
---|
| 907 |
|
---|
| 908 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 909 | params := []string{dc.nick, dc.marshalChannel(uc, channel), modeStr}
|
---|
| 910 | params = append(params, modeParams...)
|
---|
| 911 |
|
---|
| 912 | dc.SendMessage(&irc.Message{
|
---|
| 913 | Prefix: dc.srv.prefix(),
|
---|
| 914 | Command: irc.RPL_CHANNELMODEIS,
|
---|
| 915 | Params: params,
|
---|
| 916 | })
|
---|
| 917 | })
|
---|
| 918 | }
|
---|
[162] | 919 | case rpl_creationtime:
|
---|
| 920 | var channel, creationTime string
|
---|
| 921 | if err := parseMessageParams(msg, nil, &channel, &creationTime); err != nil {
|
---|
| 922 | return err
|
---|
| 923 | }
|
---|
| 924 |
|
---|
| 925 | ch, err := uc.getChannel(channel)
|
---|
| 926 | if err != nil {
|
---|
| 927 | return err
|
---|
| 928 | }
|
---|
| 929 |
|
---|
| 930 | firstCreationTime := ch.creationTime == ""
|
---|
| 931 | ch.creationTime = creationTime
|
---|
| 932 | if firstCreationTime {
|
---|
| 933 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 934 | dc.SendMessage(&irc.Message{
|
---|
| 935 | Prefix: dc.srv.prefix(),
|
---|
| 936 | Command: rpl_creationtime,
|
---|
| 937 | Params: []string{dc.nick, channel, creationTime},
|
---|
| 938 | })
|
---|
| 939 | })
|
---|
| 940 | }
|
---|
[19] | 941 | case rpl_topicwhotime:
|
---|
[43] | 942 | var name, who, timeStr string
|
---|
| 943 | if err := parseMessageParams(msg, nil, &name, &who, &timeStr); err != nil {
|
---|
| 944 | return err
|
---|
[19] | 945 | }
|
---|
[55] | 946 | ch, err := uc.getChannel(name)
|
---|
[19] | 947 | if err != nil {
|
---|
| 948 | return err
|
---|
| 949 | }
|
---|
[43] | 950 | ch.TopicWho = who
|
---|
| 951 | sec, err := strconv.ParseInt(timeStr, 10, 64)
|
---|
[19] | 952 | if err != nil {
|
---|
| 953 | return fmt.Errorf("failed to parse topic time: %v", err)
|
---|
| 954 | }
|
---|
| 955 | ch.TopicTime = time.Unix(sec, 0)
|
---|
[177] | 956 | case irc.RPL_LIST:
|
---|
| 957 | var channel, clients, topic string
|
---|
| 958 | if err := parseMessageParams(msg, nil, &channel, &clients, &topic); err != nil {
|
---|
| 959 | return err
|
---|
| 960 | }
|
---|
| 961 |
|
---|
[181] | 962 | pl := uc.getPendingLIST()
|
---|
[177] | 963 | if pl == nil {
|
---|
| 964 | return fmt.Errorf("unexpected RPL_LIST: no matching pending LIST")
|
---|
| 965 | }
|
---|
| 966 |
|
---|
| 967 | uc.forEachDownstreamByID(pl.downstreamID, func(dc *downstreamConn) {
|
---|
| 968 | dc.SendMessage(&irc.Message{
|
---|
| 969 | Prefix: dc.srv.prefix(),
|
---|
| 970 | Command: irc.RPL_LIST,
|
---|
| 971 | Params: []string{dc.nick, dc.marshalChannel(uc, channel), clients, topic},
|
---|
| 972 | })
|
---|
| 973 | })
|
---|
| 974 | case irc.RPL_LISTEND:
|
---|
[181] | 975 | ok := uc.endPendingLISTs(false)
|
---|
[177] | 976 | if !ok {
|
---|
| 977 | return fmt.Errorf("unexpected RPL_LISTEND: no matching pending LIST")
|
---|
| 978 | }
|
---|
[19] | 979 | case irc.RPL_NAMREPLY:
|
---|
[43] | 980 | var name, statusStr, members string
|
---|
| 981 | if err := parseMessageParams(msg, nil, &statusStr, &name, &members); err != nil {
|
---|
| 982 | return err
|
---|
[19] | 983 | }
|
---|
[140] | 984 |
|
---|
| 985 | ch, ok := uc.channels[name]
|
---|
| 986 | if !ok {
|
---|
| 987 | // NAMES on a channel we have not joined, forward to downstream
|
---|
[161] | 988 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[140] | 989 | channel := dc.marshalChannel(uc, name)
|
---|
[174] | 990 | members := splitSpace(members)
|
---|
[140] | 991 | for i, member := range members {
|
---|
| 992 | membership, nick := uc.parseMembershipPrefix(member)
|
---|
| 993 | members[i] = membership.String() + dc.marshalNick(uc, nick)
|
---|
| 994 | }
|
---|
| 995 | memberStr := strings.Join(members, " ")
|
---|
| 996 |
|
---|
| 997 | dc.SendMessage(&irc.Message{
|
---|
| 998 | Prefix: dc.srv.prefix(),
|
---|
| 999 | Command: irc.RPL_NAMREPLY,
|
---|
| 1000 | Params: []string{dc.nick, statusStr, channel, memberStr},
|
---|
| 1001 | })
|
---|
| 1002 | })
|
---|
| 1003 | return nil
|
---|
[19] | 1004 | }
|
---|
| 1005 |
|
---|
[43] | 1006 | status, err := parseChannelStatus(statusStr)
|
---|
[19] | 1007 | if err != nil {
|
---|
| 1008 | return err
|
---|
| 1009 | }
|
---|
| 1010 | ch.Status = status
|
---|
| 1011 |
|
---|
[174] | 1012 | for _, s := range splitSpace(members) {
|
---|
[139] | 1013 | membership, nick := uc.parseMembershipPrefix(s)
|
---|
[19] | 1014 | ch.Members[nick] = membership
|
---|
| 1015 | }
|
---|
| 1016 | case irc.RPL_ENDOFNAMES:
|
---|
[43] | 1017 | var name string
|
---|
| 1018 | if err := parseMessageParams(msg, nil, &name); err != nil {
|
---|
| 1019 | return err
|
---|
[25] | 1020 | }
|
---|
[140] | 1021 |
|
---|
| 1022 | ch, ok := uc.channels[name]
|
---|
| 1023 | if !ok {
|
---|
| 1024 | // NAMES on a channel we have not joined, forward to downstream
|
---|
[161] | 1025 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[140] | 1026 | channel := dc.marshalChannel(uc, name)
|
---|
| 1027 |
|
---|
| 1028 | dc.SendMessage(&irc.Message{
|
---|
| 1029 | Prefix: dc.srv.prefix(),
|
---|
| 1030 | Command: irc.RPL_ENDOFNAMES,
|
---|
| 1031 | Params: []string{dc.nick, channel, "End of /NAMES list"},
|
---|
| 1032 | })
|
---|
| 1033 | })
|
---|
| 1034 | return nil
|
---|
[25] | 1035 | }
|
---|
| 1036 |
|
---|
[34] | 1037 | if ch.complete {
|
---|
| 1038 | return fmt.Errorf("received unexpected RPL_ENDOFNAMES")
|
---|
| 1039 | }
|
---|
[25] | 1040 | ch.complete = true
|
---|
[27] | 1041 |
|
---|
[73] | 1042 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
[27] | 1043 | forwardChannel(dc, ch)
|
---|
[40] | 1044 | })
|
---|
[127] | 1045 | case irc.RPL_WHOREPLY:
|
---|
| 1046 | var channel, username, host, server, nick, mode, trailing string
|
---|
| 1047 | if err := parseMessageParams(msg, nil, &channel, &username, &host, &server, &nick, &mode, &trailing); err != nil {
|
---|
| 1048 | return err
|
---|
| 1049 | }
|
---|
| 1050 |
|
---|
| 1051 | parts := strings.SplitN(trailing, " ", 2)
|
---|
| 1052 | if len(parts) != 2 {
|
---|
| 1053 | return fmt.Errorf("received malformed RPL_WHOREPLY: wrong trailing parameter: %s", trailing)
|
---|
| 1054 | }
|
---|
| 1055 | realname := parts[1]
|
---|
| 1056 | hops, err := strconv.Atoi(parts[0])
|
---|
| 1057 | if err != nil {
|
---|
| 1058 | return fmt.Errorf("received malformed RPL_WHOREPLY: wrong hop count: %s", parts[0])
|
---|
| 1059 | }
|
---|
| 1060 | hops++
|
---|
| 1061 |
|
---|
| 1062 | trailing = strconv.Itoa(hops) + " " + realname
|
---|
| 1063 |
|
---|
[161] | 1064 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[127] | 1065 | channel := channel
|
---|
| 1066 | if channel != "*" {
|
---|
| 1067 | channel = dc.marshalChannel(uc, channel)
|
---|
| 1068 | }
|
---|
| 1069 | nick := dc.marshalNick(uc, nick)
|
---|
| 1070 | dc.SendMessage(&irc.Message{
|
---|
| 1071 | Prefix: dc.srv.prefix(),
|
---|
| 1072 | Command: irc.RPL_WHOREPLY,
|
---|
| 1073 | Params: []string{dc.nick, channel, username, host, server, nick, mode, trailing},
|
---|
| 1074 | })
|
---|
| 1075 | })
|
---|
| 1076 | case irc.RPL_ENDOFWHO:
|
---|
| 1077 | var name string
|
---|
| 1078 | if err := parseMessageParams(msg, nil, &name); err != nil {
|
---|
| 1079 | return err
|
---|
| 1080 | }
|
---|
| 1081 |
|
---|
[161] | 1082 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[127] | 1083 | name := name
|
---|
| 1084 | if name != "*" {
|
---|
| 1085 | // TODO: support WHO masks
|
---|
| 1086 | name = dc.marshalEntity(uc, name)
|
---|
| 1087 | }
|
---|
| 1088 | dc.SendMessage(&irc.Message{
|
---|
| 1089 | Prefix: dc.srv.prefix(),
|
---|
| 1090 | Command: irc.RPL_ENDOFWHO,
|
---|
[142] | 1091 | Params: []string{dc.nick, name, "End of /WHO list"},
|
---|
[127] | 1092 | })
|
---|
| 1093 | })
|
---|
[128] | 1094 | case irc.RPL_WHOISUSER:
|
---|
| 1095 | var nick, username, host, realname string
|
---|
| 1096 | if err := parseMessageParams(msg, nil, &nick, &username, &host, nil, &realname); err != nil {
|
---|
| 1097 | return err
|
---|
| 1098 | }
|
---|
| 1099 |
|
---|
[161] | 1100 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[128] | 1101 | nick := dc.marshalNick(uc, nick)
|
---|
| 1102 | dc.SendMessage(&irc.Message{
|
---|
| 1103 | Prefix: dc.srv.prefix(),
|
---|
| 1104 | Command: irc.RPL_WHOISUSER,
|
---|
| 1105 | Params: []string{dc.nick, nick, username, host, "*", realname},
|
---|
| 1106 | })
|
---|
| 1107 | })
|
---|
| 1108 | case irc.RPL_WHOISSERVER:
|
---|
| 1109 | var nick, server, serverInfo string
|
---|
| 1110 | if err := parseMessageParams(msg, nil, &nick, &server, &serverInfo); err != nil {
|
---|
| 1111 | return err
|
---|
| 1112 | }
|
---|
| 1113 |
|
---|
[161] | 1114 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[128] | 1115 | nick := dc.marshalNick(uc, nick)
|
---|
| 1116 | dc.SendMessage(&irc.Message{
|
---|
| 1117 | Prefix: dc.srv.prefix(),
|
---|
| 1118 | Command: irc.RPL_WHOISSERVER,
|
---|
| 1119 | Params: []string{dc.nick, nick, server, serverInfo},
|
---|
| 1120 | })
|
---|
| 1121 | })
|
---|
| 1122 | case irc.RPL_WHOISOPERATOR:
|
---|
| 1123 | var nick string
|
---|
| 1124 | if err := parseMessageParams(msg, nil, &nick); err != nil {
|
---|
| 1125 | return err
|
---|
| 1126 | }
|
---|
| 1127 |
|
---|
[161] | 1128 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[128] | 1129 | nick := dc.marshalNick(uc, nick)
|
---|
| 1130 | dc.SendMessage(&irc.Message{
|
---|
| 1131 | Prefix: dc.srv.prefix(),
|
---|
| 1132 | Command: irc.RPL_WHOISOPERATOR,
|
---|
| 1133 | Params: []string{dc.nick, nick, "is an IRC operator"},
|
---|
| 1134 | })
|
---|
| 1135 | })
|
---|
| 1136 | case irc.RPL_WHOISIDLE:
|
---|
| 1137 | var nick string
|
---|
| 1138 | if err := parseMessageParams(msg, nil, &nick, nil); err != nil {
|
---|
| 1139 | return err
|
---|
| 1140 | }
|
---|
| 1141 |
|
---|
[161] | 1142 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[128] | 1143 | nick := dc.marshalNick(uc, nick)
|
---|
| 1144 | params := []string{dc.nick, nick}
|
---|
| 1145 | params = append(params, msg.Params[2:]...)
|
---|
| 1146 | dc.SendMessage(&irc.Message{
|
---|
| 1147 | Prefix: dc.srv.prefix(),
|
---|
| 1148 | Command: irc.RPL_WHOISIDLE,
|
---|
| 1149 | Params: params,
|
---|
| 1150 | })
|
---|
| 1151 | })
|
---|
| 1152 | case irc.RPL_WHOISCHANNELS:
|
---|
| 1153 | var nick, channelList string
|
---|
| 1154 | if err := parseMessageParams(msg, nil, &nick, &channelList); err != nil {
|
---|
| 1155 | return err
|
---|
| 1156 | }
|
---|
[174] | 1157 | channels := splitSpace(channelList)
|
---|
[128] | 1158 |
|
---|
[161] | 1159 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[128] | 1160 | nick := dc.marshalNick(uc, nick)
|
---|
| 1161 | channelList := make([]string, len(channels))
|
---|
| 1162 | for i, channel := range channels {
|
---|
[139] | 1163 | prefix, channel := uc.parseMembershipPrefix(channel)
|
---|
[128] | 1164 | channel = dc.marshalChannel(uc, channel)
|
---|
| 1165 | channelList[i] = prefix.String() + channel
|
---|
| 1166 | }
|
---|
| 1167 | channels := strings.Join(channelList, " ")
|
---|
| 1168 | dc.SendMessage(&irc.Message{
|
---|
| 1169 | Prefix: dc.srv.prefix(),
|
---|
| 1170 | Command: irc.RPL_WHOISCHANNELS,
|
---|
| 1171 | Params: []string{dc.nick, nick, channels},
|
---|
| 1172 | })
|
---|
| 1173 | })
|
---|
| 1174 | case irc.RPL_ENDOFWHOIS:
|
---|
| 1175 | var nick string
|
---|
| 1176 | if err := parseMessageParams(msg, nil, &nick); err != nil {
|
---|
| 1177 | return err
|
---|
| 1178 | }
|
---|
| 1179 |
|
---|
[161] | 1180 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
[128] | 1181 | nick := dc.marshalNick(uc, nick)
|
---|
| 1182 | dc.SendMessage(&irc.Message{
|
---|
| 1183 | Prefix: dc.srv.prefix(),
|
---|
| 1184 | Command: irc.RPL_ENDOFWHOIS,
|
---|
[142] | 1185 | Params: []string{dc.nick, nick, "End of /WHOIS list"},
|
---|
[128] | 1186 | })
|
---|
| 1187 | })
|
---|
[36] | 1188 | case "PRIVMSG":
|
---|
[117] | 1189 | if msg.Prefix == nil {
|
---|
| 1190 | return fmt.Errorf("expected a prefix")
|
---|
| 1191 | }
|
---|
| 1192 |
|
---|
[178] | 1193 | var nick, text string
|
---|
| 1194 | if err := parseMessageParams(msg, &nick, &text); err != nil {
|
---|
[69] | 1195 | return err
|
---|
| 1196 | }
|
---|
[117] | 1197 |
|
---|
| 1198 | if msg.Prefix.Name == serviceNick {
|
---|
| 1199 | uc.logger.Printf("skipping PRIVMSG from soju's service: %v", msg)
|
---|
| 1200 | break
|
---|
| 1201 | }
|
---|
| 1202 | if nick == serviceNick {
|
---|
| 1203 | uc.logger.Printf("skipping PRIVMSG to soju's service: %v", msg)
|
---|
| 1204 | break
|
---|
| 1205 | }
|
---|
| 1206 |
|
---|
[178] | 1207 | target := nick
|
---|
| 1208 | if nick == uc.nick {
|
---|
| 1209 | target = msg.Prefix.Name
|
---|
| 1210 | }
|
---|
[187] | 1211 | uc.appendLog(target, "<%s> %s", msg.Prefix.Name, text)
|
---|
[178] | 1212 |
|
---|
[143] | 1213 | uc.network.ring.Produce(msg)
|
---|
[115] | 1214 | case "INVITE":
|
---|
| 1215 | var nick string
|
---|
| 1216 | var channel string
|
---|
| 1217 | if err := parseMessageParams(msg, &nick, &channel); err != nil {
|
---|
| 1218 | return err
|
---|
| 1219 | }
|
---|
| 1220 |
|
---|
| 1221 | uc.forEachDownstream(func(dc *downstreamConn) {
|
---|
| 1222 | dc.SendMessage(&irc.Message{
|
---|
| 1223 | Prefix: dc.marshalUserPrefix(uc, msg.Prefix),
|
---|
| 1224 | Command: "INVITE",
|
---|
| 1225 | Params: []string{dc.marshalNick(uc, nick), dc.marshalChannel(uc, channel)},
|
---|
| 1226 | })
|
---|
| 1227 | })
|
---|
[163] | 1228 | case irc.RPL_INVITING:
|
---|
| 1229 | var nick string
|
---|
| 1230 | var channel string
|
---|
| 1231 | if err := parseMessageParams(msg, &nick, &channel); err != nil {
|
---|
| 1232 | return err
|
---|
| 1233 | }
|
---|
| 1234 |
|
---|
| 1235 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1236 | dc.SendMessage(&irc.Message{
|
---|
| 1237 | Prefix: dc.srv.prefix(),
|
---|
| 1238 | Command: irc.RPL_INVITING,
|
---|
| 1239 | Params: []string{dc.nick, dc.marshalNick(uc, nick), dc.marshalChannel(uc, channel)},
|
---|
| 1240 | })
|
---|
| 1241 | })
|
---|
[177] | 1242 | case irc.ERR_UNKNOWNCOMMAND, irc.RPL_TRYAGAIN:
|
---|
| 1243 | var command, reason string
|
---|
| 1244 | if err := parseMessageParams(msg, nil, &command, &reason); err != nil {
|
---|
| 1245 | return err
|
---|
| 1246 | }
|
---|
| 1247 |
|
---|
| 1248 | if command == "LIST" {
|
---|
[181] | 1249 | ok := uc.endPendingLISTs(false)
|
---|
[177] | 1250 | if !ok {
|
---|
| 1251 | return fmt.Errorf("unexpected response for LIST: %q: no matching pending LIST", msg.Command)
|
---|
| 1252 | }
|
---|
| 1253 | uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
---|
| 1254 | dc.SendMessage(&irc.Message{
|
---|
| 1255 | Prefix: uc.srv.prefix(),
|
---|
| 1256 | Command: msg.Command,
|
---|
| 1257 | Params: []string{dc.nick, "LIST", reason},
|
---|
| 1258 | })
|
---|
| 1259 | })
|
---|
| 1260 | }
|
---|
[152] | 1261 | case "TAGMSG":
|
---|
| 1262 | // TODO: relay to downstream connections that accept message-tags
|
---|
[155] | 1263 | case "ACK":
|
---|
| 1264 | // Ignore
|
---|
[16] | 1265 | case irc.RPL_YOURHOST, irc.RPL_CREATED:
|
---|
[14] | 1266 | // Ignore
|
---|
| 1267 | case irc.RPL_LUSERCLIENT, irc.RPL_LUSEROP, irc.RPL_LUSERUNKNOWN, irc.RPL_LUSERCHANNELS, irc.RPL_LUSERME:
|
---|
| 1268 | // Ignore
|
---|
| 1269 | case irc.RPL_MOTDSTART, irc.RPL_MOTD, irc.RPL_ENDOFMOTD:
|
---|
| 1270 | // Ignore
|
---|
[177] | 1271 | case irc.RPL_LISTSTART:
|
---|
| 1272 | // Ignore
|
---|
[14] | 1273 | case rpl_localusers, rpl_globalusers:
|
---|
| 1274 | // Ignore
|
---|
[96] | 1275 | case irc.RPL_STATSVLINE, rpl_statsping, irc.RPL_STATSBLINE, irc.RPL_STATSDLINE:
|
---|
[14] | 1276 | // Ignore
|
---|
[13] | 1277 | default:
|
---|
[95] | 1278 | uc.logger.Printf("unhandled message: %v", msg)
|
---|
[13] | 1279 | }
|
---|
[14] | 1280 | return nil
|
---|
[13] | 1281 | }
|
---|
| 1282 |
|
---|
[174] | 1283 | func splitSpace(s string) []string {
|
---|
| 1284 | return strings.FieldsFunc(s, func(r rune) bool {
|
---|
| 1285 | return r == ' '
|
---|
| 1286 | })
|
---|
| 1287 | }
|
---|
| 1288 |
|
---|
[55] | 1289 | func (uc *upstreamConn) register() {
|
---|
[77] | 1290 | uc.nick = uc.network.Nick
|
---|
| 1291 | uc.username = uc.network.Username
|
---|
| 1292 | if uc.username == "" {
|
---|
| 1293 | uc.username = uc.nick
|
---|
| 1294 | }
|
---|
| 1295 | uc.realname = uc.network.Realname
|
---|
| 1296 | if uc.realname == "" {
|
---|
| 1297 | uc.realname = uc.nick
|
---|
| 1298 | }
|
---|
| 1299 |
|
---|
[60] | 1300 | uc.SendMessage(&irc.Message{
|
---|
[92] | 1301 | Command: "CAP",
|
---|
| 1302 | Params: []string{"LS", "302"},
|
---|
| 1303 | })
|
---|
| 1304 |
|
---|
[93] | 1305 | if uc.network.Pass != "" {
|
---|
| 1306 | uc.SendMessage(&irc.Message{
|
---|
| 1307 | Command: "PASS",
|
---|
| 1308 | Params: []string{uc.network.Pass},
|
---|
| 1309 | })
|
---|
| 1310 | }
|
---|
| 1311 |
|
---|
[92] | 1312 | uc.SendMessage(&irc.Message{
|
---|
[13] | 1313 | Command: "NICK",
|
---|
[69] | 1314 | Params: []string{uc.nick},
|
---|
[60] | 1315 | })
|
---|
| 1316 | uc.SendMessage(&irc.Message{
|
---|
[13] | 1317 | Command: "USER",
|
---|
[77] | 1318 | Params: []string{uc.username, "0", "*", uc.realname},
|
---|
[60] | 1319 | })
|
---|
[44] | 1320 | }
|
---|
[13] | 1321 |
|
---|
[95] | 1322 | func (uc *upstreamConn) requestSASL() bool {
|
---|
| 1323 | if uc.network.SASL.Mechanism == "" {
|
---|
| 1324 | return false
|
---|
| 1325 | }
|
---|
| 1326 |
|
---|
| 1327 | v, ok := uc.caps["sasl"]
|
---|
| 1328 | if !ok {
|
---|
| 1329 | return false
|
---|
| 1330 | }
|
---|
| 1331 | if v != "" {
|
---|
| 1332 | mechanisms := strings.Split(v, ",")
|
---|
| 1333 | found := false
|
---|
| 1334 | for _, mech := range mechanisms {
|
---|
| 1335 | if strings.EqualFold(mech, uc.network.SASL.Mechanism) {
|
---|
| 1336 | found = true
|
---|
| 1337 | break
|
---|
| 1338 | }
|
---|
| 1339 | }
|
---|
| 1340 | if !found {
|
---|
| 1341 | return false
|
---|
| 1342 | }
|
---|
| 1343 | }
|
---|
| 1344 |
|
---|
| 1345 | return true
|
---|
| 1346 | }
|
---|
| 1347 |
|
---|
| 1348 | func (uc *upstreamConn) handleCapAck(name string, ok bool) error {
|
---|
| 1349 | auth := &uc.network.SASL
|
---|
| 1350 | switch name {
|
---|
| 1351 | case "sasl":
|
---|
| 1352 | if !ok {
|
---|
| 1353 | uc.logger.Printf("server refused to acknowledge the SASL capability")
|
---|
| 1354 | return nil
|
---|
| 1355 | }
|
---|
| 1356 |
|
---|
| 1357 | switch auth.Mechanism {
|
---|
| 1358 | case "PLAIN":
|
---|
| 1359 | uc.logger.Printf("starting SASL PLAIN authentication with username %q", auth.Plain.Username)
|
---|
| 1360 | uc.saslClient = sasl.NewPlainClient("", auth.Plain.Username, auth.Plain.Password)
|
---|
| 1361 | default:
|
---|
| 1362 | return fmt.Errorf("unsupported SASL mechanism %q", name)
|
---|
| 1363 | }
|
---|
| 1364 |
|
---|
| 1365 | uc.SendMessage(&irc.Message{
|
---|
| 1366 | Command: "AUTHENTICATE",
|
---|
| 1367 | Params: []string{auth.Mechanism},
|
---|
| 1368 | })
|
---|
[152] | 1369 | case "message-tags":
|
---|
| 1370 | uc.tagsSupported = ok
|
---|
[155] | 1371 | case "labeled-response":
|
---|
| 1372 | uc.labelsSupported = ok
|
---|
[95] | 1373 | }
|
---|
| 1374 | return nil
|
---|
| 1375 | }
|
---|
| 1376 |
|
---|
[165] | 1377 | func (uc *upstreamConn) readMessages(ch chan<- event) error {
|
---|
[13] | 1378 | for {
|
---|
[55] | 1379 | msg, err := uc.irc.ReadMessage()
|
---|
[13] | 1380 | if err == io.EOF {
|
---|
| 1381 | break
|
---|
| 1382 | } else if err != nil {
|
---|
| 1383 | return fmt.Errorf("failed to read IRC command: %v", err)
|
---|
| 1384 | }
|
---|
| 1385 |
|
---|
[64] | 1386 | if uc.srv.Debug {
|
---|
| 1387 | uc.logger.Printf("received: %v", msg)
|
---|
| 1388 | }
|
---|
| 1389 |
|
---|
[165] | 1390 | ch <- eventUpstreamMessage{msg, uc}
|
---|
[13] | 1391 | }
|
---|
| 1392 |
|
---|
[45] | 1393 | return nil
|
---|
[13] | 1394 | }
|
---|
[60] | 1395 |
|
---|
[180] | 1396 | // SendMessage queues a new outgoing message. It is safe to call from any
|
---|
| 1397 | // goroutine.
|
---|
[60] | 1398 | func (uc *upstreamConn) SendMessage(msg *irc.Message) {
|
---|
[102] | 1399 | uc.outgoing <- msg
|
---|
[60] | 1400 | }
|
---|
[155] | 1401 |
|
---|
[176] | 1402 | func (uc *upstreamConn) SendMessageLabeled(downstreamID uint64, msg *irc.Message) {
|
---|
[155] | 1403 | if uc.labelsSupported {
|
---|
| 1404 | if msg.Tags == nil {
|
---|
| 1405 | msg.Tags = make(map[string]irc.TagValue)
|
---|
| 1406 | }
|
---|
[176] | 1407 | msg.Tags["label"] = irc.TagValue(fmt.Sprintf("sd-%d-%d", downstreamID, uc.nextLabelID))
|
---|
[161] | 1408 | uc.nextLabelID++
|
---|
[155] | 1409 | }
|
---|
| 1410 | uc.SendMessage(msg)
|
---|
| 1411 | }
|
---|
[178] | 1412 |
|
---|
| 1413 | // TODO: handle moving logs when a network name changes, when support for this is added
|
---|
[187] | 1414 | func (uc *upstreamConn) appendLog(entity string, format string, a ...interface{}) {
|
---|
[178] | 1415 | if uc.srv.LogPath == "" {
|
---|
| 1416 | return
|
---|
| 1417 | }
|
---|
| 1418 | // TODO: enforce maximum open file handles (LRU cache of file handles)
|
---|
| 1419 | // TODO: handle non-monotonic clock behaviour
|
---|
| 1420 | now := time.Now()
|
---|
| 1421 | year, month, day := now.Date()
|
---|
| 1422 | name := fmt.Sprintf("%04d-%02d-%02d.log", year, month, day)
|
---|
| 1423 | log, ok := uc.logs[entity]
|
---|
| 1424 | if !ok || log.name != name {
|
---|
| 1425 | if ok {
|
---|
| 1426 | log.file.Close()
|
---|
| 1427 | delete(uc.logs, entity)
|
---|
| 1428 | }
|
---|
| 1429 | // TODO: handle/forbid network/entity names with illegal path characters
|
---|
| 1430 | dir := filepath.Join(uc.srv.LogPath, uc.user.Username, uc.network.Name, entity)
|
---|
| 1431 | if err := os.MkdirAll(dir, 0600); err != nil {
|
---|
| 1432 | uc.logger.Printf("failed to log message: could not create logs directory %q: %v", dir, err)
|
---|
| 1433 | return
|
---|
| 1434 | }
|
---|
| 1435 | path := filepath.Join(dir, name)
|
---|
| 1436 | f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
|
---|
| 1437 | if err != nil {
|
---|
| 1438 | uc.logger.Printf("failed to log message: could not open or create log file %q: %v", path, err)
|
---|
| 1439 | return
|
---|
| 1440 | }
|
---|
| 1441 | log = entityLog{
|
---|
| 1442 | name: name,
|
---|
| 1443 | file: f,
|
---|
| 1444 | }
|
---|
| 1445 | uc.logs[entity] = log
|
---|
| 1446 | }
|
---|
| 1447 |
|
---|
| 1448 | format = "[%02d:%02d:%02d] " + format + "\n"
|
---|
| 1449 | args := []interface{}{now.Hour(), now.Minute(), now.Second()}
|
---|
| 1450 | args = append(args, a...)
|
---|
| 1451 |
|
---|
| 1452 | if _, err := fmt.Fprintf(log.file, format, args...); err != nil {
|
---|
| 1453 | uc.logger.Printf("failed to log message to %q: %v", log.name, err)
|
---|
| 1454 | }
|
---|
| 1455 | }
|
---|