2020-03-13 17:13:03 +00:00
|
|
|
package soju
|
2020-02-06 15:18:19 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-29 11:10:54 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/sha256"
|
2020-02-06 15:18:19 +00:00
|
|
|
"crypto/tls"
|
2020-05-29 11:10:54 +00:00
|
|
|
"crypto/x509"
|
2020-03-13 14:12:44 +00:00
|
|
|
"encoding/base64"
|
2020-03-23 02:21:43 +00:00
|
|
|
"errors"
|
2020-02-06 15:18:19 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2020-07-06 14:59:14 +00:00
|
|
|
"net/url"
|
2020-02-06 18:22:04 +00:00
|
|
|
"strconv"
|
2020-02-06 17:07:35 +00:00
|
|
|
"strings"
|
2020-02-06 18:22:04 +00:00
|
|
|
"time"
|
2020-05-01 17:51:22 +00:00
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
2020-02-06 15:18:19 +00:00
|
|
|
|
2020-03-13 14:12:44 +00:00
|
|
|
"github.com/emersion/go-sasl"
|
2020-02-06 15:18:19 +00:00
|
|
|
"gopkg.in/irc.v3"
|
|
|
|
)
|
|
|
|
|
2020-04-30 14:10:39 +00:00
|
|
|
// permanentUpstreamCaps is the static list of upstream capabilities always
|
|
|
|
// requested when supported.
|
|
|
|
var permanentUpstreamCaps = map[string]bool{
|
|
|
|
"away-notify": true,
|
|
|
|
"batch": true,
|
2020-09-08 17:49:06 +00:00
|
|
|
"extended-join": true,
|
2020-04-30 14:10:39 +00:00
|
|
|
"labeled-response": true,
|
|
|
|
"message-tags": true,
|
2020-04-30 21:39:59 +00:00
|
|
|
"multi-prefix": true,
|
2020-04-30 14:10:39 +00:00
|
|
|
"server-time": true,
|
|
|
|
}
|
|
|
|
|
2020-08-19 21:35:12 +00:00
|
|
|
type registrationError string
|
|
|
|
|
|
|
|
func (err registrationError) Error() string {
|
|
|
|
return fmt.Sprintf("registration error: %v", string(err))
|
|
|
|
}
|
|
|
|
|
2020-02-06 18:22:04 +00:00
|
|
|
type upstreamChannel struct {
|
2020-03-26 04:51:47 +00:00
|
|
|
Name string
|
|
|
|
conn *upstreamConn
|
|
|
|
Topic string
|
2020-08-20 08:39:23 +00:00
|
|
|
TopicWho *irc.Prefix
|
2020-03-26 04:51:47 +00:00
|
|
|
TopicTime time.Time
|
|
|
|
Status channelStatus
|
|
|
|
modes channelModes
|
|
|
|
creationTime string
|
2020-04-30 21:39:59 +00:00
|
|
|
Members map[string]*memberships
|
2020-03-26 04:51:47 +00:00
|
|
|
complete bool
|
Add customizable auto-detaching, auto-reattaching, relaying.
This uses the fields added previously to the Channel struct to implement
the actual detaching/reattaching/relaying logic.
The `FilterDefault` values of the messages filters are currently
hardcoded.
The values of the message filters are not currently user-settable.
This introduces a new user event, eventChannelDetach, which stores an
upstreamConn (which might become invalid at the time of processing), and
a channel name, used for auto-detaching. Every time the channel detach
timer is refreshed (by receveing a message, etc.), a new timer is
created on the upstreamChannel, which will dispatch this event after the
duration (and discards the previous timer, if any).
2020-11-30 21:08:33 +00:00
|
|
|
detachTimer *time.Timer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uc *upstreamChannel) updateAutoDetach(dur time.Duration) {
|
|
|
|
if uc.detachTimer != nil {
|
|
|
|
uc.detachTimer.Stop()
|
|
|
|
uc.detachTimer = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if dur == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.detachTimer = time.AfterFunc(dur, func() {
|
|
|
|
uc.conn.network.user.events <- eventChannelDetach{
|
|
|
|
uc: uc.conn,
|
|
|
|
name: uc.Name,
|
|
|
|
}
|
|
|
|
})
|
2020-02-06 18:22:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 15:18:19 +00:00
|
|
|
type upstreamConn struct {
|
2020-04-03 14:34:11 +00:00
|
|
|
conn
|
|
|
|
|
|
|
|
network *network
|
|
|
|
user *user
|
2020-02-06 16:04:49 +00:00
|
|
|
|
|
|
|
serverName string
|
|
|
|
availableUserModes string
|
2020-03-20 23:48:19 +00:00
|
|
|
availableChannelModes map[byte]channelModeType
|
|
|
|
availableChannelTypes string
|
|
|
|
availableMemberships []membership
|
2020-02-06 18:22:04 +00:00
|
|
|
|
2020-04-29 17:40:31 +00:00
|
|
|
registered bool
|
|
|
|
nick string
|
|
|
|
username string
|
|
|
|
realname string
|
|
|
|
modes userModes
|
|
|
|
channels map[string]*upstreamChannel
|
|
|
|
supportedCaps map[string]string
|
2020-04-29 17:45:37 +00:00
|
|
|
caps map[string]bool
|
2020-04-29 17:40:31 +00:00
|
|
|
batches map[string]batch
|
|
|
|
away bool
|
2020-04-29 17:45:37 +00:00
|
|
|
nextLabelID uint64
|
2020-03-23 00:34:34 +00:00
|
|
|
|
2020-03-13 14:12:44 +00:00
|
|
|
saslClient sasl.Client
|
|
|
|
saslStarted bool
|
Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.
Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.
This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)
When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.
Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.
When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.
Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-26 01:40:30 +00:00
|
|
|
|
|
|
|
// set of LIST commands in progress, per downstream
|
|
|
|
pendingLISTDownstreamSet map[uint64]struct{}
|
2020-03-25 22:51:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 17:22:58 +00:00
|
|
|
func connectToUpstream(network *network) (*upstreamConn, error) {
|
|
|
|
logger := &prefixLogger{network.user.srv.Logger, fmt.Sprintf("upstream %q: ", network.Addr)}
|
2020-02-06 21:46:46 +00:00
|
|
|
|
2020-07-06 14:59:14 +00:00
|
|
|
dialer := net.Dialer{Timeout: connectTimeout}
|
2020-04-27 16:02:33 +00:00
|
|
|
|
2020-07-06 14:59:14 +00:00
|
|
|
s := network.Addr
|
|
|
|
if !strings.Contains(s, "://") {
|
|
|
|
// This is a raw domain name, make it an URL with the default scheme
|
|
|
|
s = "ircs://" + s
|
2020-03-04 17:22:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 14:59:14 +00:00
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse upstream server URL: %v", err)
|
|
|
|
}
|
2020-04-01 14:41:17 +00:00
|
|
|
|
2020-04-27 16:02:33 +00:00
|
|
|
var netConn net.Conn
|
2020-07-06 14:59:14 +00:00
|
|
|
switch u.Scheme {
|
2020-04-27 16:02:33 +00:00
|
|
|
case "ircs":
|
2020-07-06 14:59:14 +00:00
|
|
|
addr := u.Host
|
2020-07-06 15:37:52 +00:00
|
|
|
host, _, err := net.SplitHostPort(u.Host)
|
|
|
|
if err != nil {
|
|
|
|
host = u.Host
|
|
|
|
addr = u.Host + ":6697"
|
2020-04-27 16:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.Printf("connecting to TLS server at address %q", addr)
|
2020-05-29 11:10:54 +00:00
|
|
|
|
2020-07-06 15:37:52 +00:00
|
|
|
tlsConfig := &tls.Config{ServerName: host}
|
2020-05-29 11:10:54 +00:00
|
|
|
if network.SASL.Mechanism == "EXTERNAL" {
|
|
|
|
if network.SASL.External.CertBlob == nil {
|
|
|
|
return nil, fmt.Errorf("missing certificate for authentication")
|
|
|
|
}
|
|
|
|
if network.SASL.External.PrivKeyBlob == nil {
|
|
|
|
return nil, fmt.Errorf("missing private key for authentication")
|
|
|
|
}
|
|
|
|
key, err := x509.ParsePKCS8PrivateKey(network.SASL.External.PrivKeyBlob)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse private key: %v", err)
|
|
|
|
}
|
2020-07-06 15:37:52 +00:00
|
|
|
tlsConfig.Certificates = []tls.Certificate{
|
|
|
|
{
|
|
|
|
Certificate: [][]byte{network.SASL.External.CertBlob},
|
|
|
|
PrivateKey: key.(crypto.PrivateKey),
|
2020-05-29 11:10:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
logger.Printf("using TLS client certificate %x", sha256.Sum256(network.SASL.External.CertBlob))
|
|
|
|
}
|
|
|
|
|
2020-07-06 15:37:52 +00:00
|
|
|
netConn, err = dialer.Dial("tcp", addr)
|
2020-07-06 14:59:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to dial %q: %v", addr, err)
|
|
|
|
}
|
2020-07-06 15:37:52 +00:00
|
|
|
|
|
|
|
// Don't do the TLS handshake immediately, because we need to register
|
|
|
|
// the new connection with identd ASAP. See:
|
|
|
|
// https://todo.sr.ht/~emersion/soju/69#event-41859
|
|
|
|
netConn = tls.Client(netConn, tlsConfig)
|
2020-04-27 16:05:28 +00:00
|
|
|
case "irc+insecure":
|
2020-07-06 14:59:14 +00:00
|
|
|
addr := u.Host
|
2020-07-06 14:04:56 +00:00
|
|
|
if _, _, err := net.SplitHostPort(addr); err != nil {
|
2020-04-27 16:05:28 +00:00
|
|
|
addr = addr + ":6667"
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Printf("connecting to plain-text server at address %q", addr)
|
|
|
|
netConn, err = dialer.Dial("tcp", addr)
|
2020-07-06 14:59:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to dial %q: %v", addr, err)
|
|
|
|
}
|
2020-07-22 13:44:19 +00:00
|
|
|
case "irc+unix", "unix":
|
2020-07-06 15:31:11 +00:00
|
|
|
logger.Printf("connecting to Unix socket at path %q", u.Path)
|
|
|
|
netConn, err = dialer.Dial("unix", u.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to connect to Unix socket %q: %v", u.Path, err)
|
|
|
|
}
|
2020-04-27 16:02:33 +00:00
|
|
|
default:
|
2020-07-06 14:59:14 +00:00
|
|
|
return nil, fmt.Errorf("failed to dial %q: unknown scheme: %v", network.Addr, u.Scheme)
|
2020-02-06 21:46:46 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 17:28:29 +00:00
|
|
|
options := connOptions{
|
2020-08-20 07:13:56 +00:00
|
|
|
Logger: logger,
|
2020-08-19 17:28:29 +00:00
|
|
|
RateLimitDelay: upstreamMessageDelay,
|
|
|
|
RateLimitBurst: upstreamMessageBurst,
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:36:42 +00:00
|
|
|
uc := &upstreamConn{
|
2020-08-19 17:28:29 +00:00
|
|
|
conn: *newConn(network.user.srv, newNetIRCConn(netConn), &options),
|
Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.
Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.
This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)
When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.
Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.
When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.
Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-26 01:40:30 +00:00
|
|
|
network: network,
|
|
|
|
user: network.user,
|
|
|
|
channels: make(map[string]*upstreamChannel),
|
2020-04-29 17:40:31 +00:00
|
|
|
supportedCaps: make(map[string]string),
|
2020-04-29 17:45:37 +00:00
|
|
|
caps: make(map[string]bool),
|
Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.
Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.
This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)
When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.
Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.
When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.
Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-26 01:40:30 +00:00
|
|
|
batches: make(map[string]batch),
|
|
|
|
availableChannelTypes: stdChannelTypes,
|
|
|
|
availableChannelModes: stdChannelModes,
|
|
|
|
availableMemberships: stdMemberships,
|
|
|
|
pendingLISTDownstreamSet: make(map[uint64]struct{}),
|
2020-02-06 21:46:46 +00:00
|
|
|
}
|
2020-02-17 11:36:42 +00:00
|
|
|
return uc, nil
|
2020-02-06 21:46:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 14:44:13 +00:00
|
|
|
func (uc *upstreamConn) forEachDownstream(f func(*downstreamConn)) {
|
2020-04-04 02:48:25 +00:00
|
|
|
uc.network.forEachDownstream(f)
|
2020-03-04 14:44:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 01:39:04 +00:00
|
|
|
func (uc *upstreamConn) forEachDownstreamByID(id uint64, f func(*downstreamConn)) {
|
2020-03-23 02:21:43 +00:00
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
if id != 0 && id != dc.id {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f(dc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:36:42 +00:00
|
|
|
func (uc *upstreamConn) getChannel(name string) (*upstreamChannel, error) {
|
|
|
|
ch, ok := uc.channels[name]
|
2020-02-06 18:22:04 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown channel %q", name)
|
|
|
|
}
|
|
|
|
return ch, nil
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 02:05:14 +00:00
|
|
|
func (uc *upstreamConn) isChannel(entity string) bool {
|
2020-03-20 23:48:19 +00:00
|
|
|
if i := strings.IndexByte(uc.availableChannelTypes, entity[0]); i >= 0 {
|
|
|
|
return true
|
2020-03-20 02:05:14 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-28 00:03:00 +00:00
|
|
|
func (uc *upstreamConn) getPendingLIST() *pendingLIST {
|
Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.
Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.
This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)
When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.
Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.
When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.
Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-26 01:40:30 +00:00
|
|
|
for _, pl := range uc.user.pendingLISTs {
|
|
|
|
if _, ok := pl.pendingCommands[uc.network.ID]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return &pl
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-28 00:03:00 +00:00
|
|
|
func (uc *upstreamConn) endPendingLISTs(all bool) (found bool) {
|
Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.
Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.
This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)
When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.
Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.
When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.
Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-26 01:40:30 +00:00
|
|
|
found = false
|
|
|
|
for i := 0; i < len(uc.user.pendingLISTs); i++ {
|
|
|
|
pl := uc.user.pendingLISTs[i]
|
|
|
|
if _, ok := pl.pendingCommands[uc.network.ID]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(pl.pendingCommands, uc.network.ID)
|
|
|
|
if len(pl.pendingCommands) == 0 {
|
|
|
|
uc.user.pendingLISTs = append(uc.user.pendingLISTs[:i], uc.user.pendingLISTs[i+1:]...)
|
|
|
|
i--
|
|
|
|
uc.forEachDownstreamByID(pl.downstreamID, func(dc *downstreamConn) {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_LISTEND,
|
|
|
|
Params: []string{dc.nick, "End of /LIST"},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
found = true
|
|
|
|
if !all {
|
|
|
|
delete(uc.pendingLISTDownstreamSet, pl.downstreamID)
|
|
|
|
uc.user.forEachUpstream(func(uc *upstreamConn) {
|
2020-03-28 00:03:00 +00:00
|
|
|
uc.trySendLIST(pl.downstreamID)
|
Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.
Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.
This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)
When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.
Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.
When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.
Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-26 01:40:30 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-28 00:03:00 +00:00
|
|
|
func (uc *upstreamConn) trySendLIST(downstreamID uint64) {
|
Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.
Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.
This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)
When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.
Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.
When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.
Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-26 01:40:30 +00:00
|
|
|
if _, ok := uc.pendingLISTDownstreamSet[downstreamID]; ok {
|
|
|
|
// a LIST command is already pending
|
|
|
|
// we will try again when that command is completed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pl := range uc.user.pendingLISTs {
|
|
|
|
if pl.downstreamID != downstreamID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// this is the first pending LIST command list of the downstream
|
|
|
|
listCommand, ok := pl.pendingCommands[uc.network.ID]
|
|
|
|
if !ok {
|
|
|
|
// there is no command for this upstream in these LIST commands
|
|
|
|
// do not send anything
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// there is a command for this upstream in these LIST commands
|
|
|
|
// send it now
|
|
|
|
|
|
|
|
uc.SendMessageLabeled(downstreamID, listCommand)
|
|
|
|
|
|
|
|
uc.pendingLISTDownstreamSet[downstreamID] = struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 21:39:59 +00:00
|
|
|
func (uc *upstreamConn) parseMembershipPrefix(s string) (ms *memberships, nick string) {
|
|
|
|
memberships := make(memberships, 0, 4)
|
|
|
|
i := 0
|
2020-03-20 23:48:19 +00:00
|
|
|
for _, m := range uc.availableMemberships {
|
2020-04-30 21:39:59 +00:00
|
|
|
if i >= len(s) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if s[i] == m.Prefix {
|
|
|
|
memberships = append(memberships, m)
|
|
|
|
i++
|
2020-03-20 23:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-30 21:39:59 +00:00
|
|
|
return &memberships, s[i:]
|
2020-03-20 23:48:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 17:51:22 +00:00
|
|
|
func isWordBoundary(r rune) bool {
|
|
|
|
switch r {
|
|
|
|
case '-', '_', '|':
|
|
|
|
return false
|
|
|
|
case '\u00A0':
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return !unicode.IsLetter(r) && !unicode.IsNumber(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isHighlight(text, nick string) bool {
|
|
|
|
for {
|
|
|
|
i := strings.Index(text, nick)
|
|
|
|
if i < 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect word boundaries
|
|
|
|
var left, right rune
|
|
|
|
if i > 0 {
|
|
|
|
left, _ = utf8.DecodeLastRuneInString(text[:i])
|
|
|
|
}
|
|
|
|
if i < len(text) {
|
|
|
|
right, _ = utf8.DecodeRuneInString(text[i+len(nick):])
|
|
|
|
}
|
|
|
|
if isWordBoundary(left) && isWordBoundary(right) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
text = text[i+len(nick):]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:36:42 +00:00
|
|
|
func (uc *upstreamConn) handleMessage(msg *irc.Message) error {
|
2020-03-23 02:21:43 +00:00
|
|
|
var label string
|
|
|
|
if l, ok := msg.GetTag("label"); ok {
|
|
|
|
label = l
|
|
|
|
}
|
|
|
|
|
2020-03-23 02:18:16 +00:00
|
|
|
var msgBatch *batch
|
|
|
|
if batchName, ok := msg.GetTag("batch"); ok {
|
|
|
|
b, ok := uc.batches[batchName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected batch reference: batch was not defined: %q", batchName)
|
|
|
|
}
|
|
|
|
msgBatch = &b
|
2020-03-23 02:21:43 +00:00
|
|
|
if label == "" {
|
|
|
|
label = msgBatch.Label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 01:39:04 +00:00
|
|
|
var downstreamID uint64 = 0
|
2020-03-23 02:21:43 +00:00
|
|
|
if label != "" {
|
|
|
|
var labelOffset uint64
|
2020-03-26 01:39:04 +00:00
|
|
|
n, err := fmt.Sscanf(label, "sd-%d-%d", &downstreamID, &labelOffset)
|
2020-03-23 02:21:43 +00:00
|
|
|
if err == nil && n < 2 {
|
|
|
|
err = errors.New("not enough arguments")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unexpected message label: invalid downstream reference for label %q: %v", label, err)
|
|
|
|
}
|
2020-03-23 02:18:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 18:48:23 +00:00
|
|
|
if _, ok := msg.Tags["time"]; !ok {
|
2020-04-06 20:05:49 +00:00
|
|
|
msg.Tags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
2020-04-03 18:48:23 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 15:18:19 +00:00
|
|
|
switch msg.Command {
|
|
|
|
case "PING":
|
2020-02-17 15:17:31 +00:00
|
|
|
uc.SendMessage(&irc.Message{
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: "PONG",
|
2020-02-18 19:40:32 +00:00
|
|
|
Params: msg.Params,
|
2020-02-17 15:17:31 +00:00
|
|
|
})
|
2020-02-06 21:46:46 +00:00
|
|
|
return nil
|
2020-05-21 05:04:34 +00:00
|
|
|
case "NOTICE", "PRIVMSG", "TAGMSG":
|
2020-04-29 13:00:17 +00:00
|
|
|
if msg.Prefix == nil {
|
|
|
|
return fmt.Errorf("expected a prefix")
|
|
|
|
}
|
|
|
|
|
2020-05-01 16:12:47 +00:00
|
|
|
var entity, text string
|
2020-05-21 05:04:34 +00:00
|
|
|
if msg.Command != "TAGMSG" {
|
|
|
|
if err := parseMessageParams(msg, &entity, &text); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := parseMessageParams(msg, &entity); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-01 16:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Prefix.Name == serviceNick {
|
|
|
|
uc.logger.Printf("skipping %v from soju's service: %v", msg.Command, msg)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if entity == serviceNick {
|
|
|
|
uc.logger.Printf("skipping %v to soju's service: %v", msg.Command, msg)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-03-26 04:53:13 +00:00
|
|
|
if msg.Prefix.User == "" && msg.Prefix.Host == "" { // server message
|
2020-04-06 19:42:55 +00:00
|
|
|
uc.produce("", msg, nil)
|
2020-05-21 05:04:34 +00:00
|
|
|
} else { // regular user message
|
2020-04-03 19:00:19 +00:00
|
|
|
target := entity
|
|
|
|
if target == uc.nick {
|
2020-03-25 22:51:28 +00:00
|
|
|
target = msg.Prefix.Name
|
|
|
|
}
|
2020-05-01 17:05:20 +00:00
|
|
|
|
Add customizable auto-detaching, auto-reattaching, relaying.
This uses the fields added previously to the Channel struct to implement
the actual detaching/reattaching/relaying logic.
The `FilterDefault` values of the messages filters are currently
hardcoded.
The values of the message filters are not currently user-settable.
This introduces a new user event, eventChannelDetach, which stores an
upstreamConn (which might become invalid at the time of processing), and
a channel name, used for auto-detaching. Every time the channel detach
timer is refreshed (by receveing a message, etc.), a new timer is
created on the upstreamChannel, which will dispatch this event after the
duration (and discards the previous timer, if any).
2020-11-30 21:08:33 +00:00
|
|
|
if ch, ok := uc.network.channels[target]; ok {
|
|
|
|
if ch.Detached {
|
|
|
|
uc.handleDetachedMessage(msg.Prefix.Name, text, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
highlight := msg.Prefix.Name != uc.nick && isHighlight(text, uc.nick)
|
|
|
|
if ch.DetachOn == FilterMessage || ch.DetachOn == FilterDefault || (ch.DetachOn == FilterHighlight && highlight) {
|
|
|
|
uc.updateChannelAutoDetach(target)
|
|
|
|
}
|
2020-05-01 17:05:20 +00:00
|
|
|
}
|
Add customizable auto-detaching, auto-reattaching, relaying.
This uses the fields added previously to the Channel struct to implement
the actual detaching/reattaching/relaying logic.
The `FilterDefault` values of the messages filters are currently
hardcoded.
The values of the message filters are not currently user-settable.
This introduces a new user event, eventChannelDetach, which stores an
upstreamConn (which might become invalid at the time of processing), and
a channel name, used for auto-detaching. Every time the channel detach
timer is refreshed (by receveing a message, etc.), a new timer is
created on the upstreamChannel, which will dispatch this event after the
duration (and discards the previous timer, if any).
2020-11-30 21:08:33 +00:00
|
|
|
|
|
|
|
uc.produce(target, msg, nil)
|
2020-03-26 04:53:13 +00:00
|
|
|
}
|
2020-03-13 10:26:43 +00:00
|
|
|
case "CAP":
|
2020-03-13 14:12:44 +00:00
|
|
|
var subCmd string
|
|
|
|
if err := parseMessageParams(msg, nil, &subCmd); err != nil {
|
|
|
|
return err
|
2020-03-13 10:26:43 +00:00
|
|
|
}
|
2020-03-13 14:12:44 +00:00
|
|
|
subCmd = strings.ToUpper(subCmd)
|
|
|
|
subParams := msg.Params[2:]
|
|
|
|
switch subCmd {
|
|
|
|
case "LS":
|
|
|
|
if len(subParams) < 1 {
|
|
|
|
return newNeedMoreParamsError(msg.Command)
|
|
|
|
}
|
2020-04-30 13:27:41 +00:00
|
|
|
caps := subParams[len(subParams)-1]
|
2020-03-13 14:12:44 +00:00
|
|
|
more := len(subParams) >= 2 && msg.Params[len(subParams)-2] == "*"
|
|
|
|
|
2020-04-30 13:27:41 +00:00
|
|
|
uc.handleSupportedCaps(caps)
|
2020-03-13 14:12:44 +00:00
|
|
|
|
|
|
|
if more {
|
|
|
|
break // wait to receive all capabilities
|
|
|
|
}
|
|
|
|
|
2020-04-30 13:27:41 +00:00
|
|
|
uc.requestCaps()
|
2020-03-23 00:34:34 +00:00
|
|
|
|
|
|
|
if uc.requestSASL() {
|
2020-03-13 14:12:44 +00:00
|
|
|
break // we'll send CAP END after authentication is completed
|
|
|
|
}
|
2020-03-13 10:26:43 +00:00
|
|
|
|
|
|
|
uc.SendMessage(&irc.Message{
|
|
|
|
Command: "CAP",
|
|
|
|
Params: []string{"END"},
|
|
|
|
})
|
2020-03-13 14:12:44 +00:00
|
|
|
case "ACK", "NAK":
|
|
|
|
if len(subParams) < 1 {
|
|
|
|
return newNeedMoreParamsError(msg.Command)
|
|
|
|
}
|
|
|
|
caps := strings.Fields(subParams[0])
|
|
|
|
|
|
|
|
for _, name := range caps {
|
|
|
|
if err := uc.handleCapAck(strings.ToLower(name), subCmd == "ACK"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 13:27:41 +00:00
|
|
|
if uc.registered {
|
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
dc.updateSupportedCaps()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
case "NEW":
|
|
|
|
if len(subParams) < 1 {
|
|
|
|
return newNeedMoreParamsError(msg.Command)
|
|
|
|
}
|
|
|
|
uc.handleSupportedCaps(subParams[0])
|
|
|
|
uc.requestCaps()
|
|
|
|
case "DEL":
|
|
|
|
if len(subParams) < 1 {
|
|
|
|
return newNeedMoreParamsError(msg.Command)
|
|
|
|
}
|
|
|
|
caps := strings.Fields(subParams[0])
|
|
|
|
|
|
|
|
for _, c := range caps {
|
|
|
|
delete(uc.supportedCaps, c)
|
|
|
|
delete(uc.caps, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
if uc.registered {
|
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
dc.updateSupportedCaps()
|
|
|
|
})
|
|
|
|
}
|
2020-03-13 14:12:44 +00:00
|
|
|
default:
|
|
|
|
uc.logger.Printf("unhandled message: %v", msg)
|
|
|
|
}
|
|
|
|
case "AUTHENTICATE":
|
|
|
|
if uc.saslClient == nil {
|
|
|
|
return fmt.Errorf("received unexpected AUTHENTICATE message")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: if a challenge is 400 bytes long, buffer it
|
|
|
|
var challengeStr string
|
|
|
|
if err := parseMessageParams(msg, &challengeStr); err != nil {
|
|
|
|
uc.SendMessage(&irc.Message{
|
|
|
|
Command: "AUTHENTICATE",
|
|
|
|
Params: []string{"*"},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var challenge []byte
|
|
|
|
if challengeStr != "+" {
|
|
|
|
var err error
|
|
|
|
challenge, err = base64.StdEncoding.DecodeString(challengeStr)
|
|
|
|
if err != nil {
|
|
|
|
uc.SendMessage(&irc.Message{
|
|
|
|
Command: "AUTHENTICATE",
|
|
|
|
Params: []string{"*"},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp []byte
|
|
|
|
var err error
|
|
|
|
if !uc.saslStarted {
|
|
|
|
_, resp, err = uc.saslClient.Start()
|
|
|
|
uc.saslStarted = true
|
|
|
|
} else {
|
|
|
|
resp, err = uc.saslClient.Next(challenge)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
uc.SendMessage(&irc.Message{
|
|
|
|
Command: "AUTHENTICATE",
|
|
|
|
Params: []string{"*"},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: send response in multiple chunks if >= 400 bytes
|
|
|
|
var respStr = "+"
|
2020-06-05 09:34:29 +00:00
|
|
|
if len(resp) != 0 {
|
2020-03-13 14:12:44 +00:00
|
|
|
respStr = base64.StdEncoding.EncodeToString(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.SendMessage(&irc.Message{
|
|
|
|
Command: "AUTHENTICATE",
|
|
|
|
Params: []string{respStr},
|
|
|
|
})
|
2020-03-19 13:51:45 +00:00
|
|
|
case irc.RPL_LOGGEDIN:
|
2020-03-13 14:12:44 +00:00
|
|
|
var account string
|
|
|
|
if err := parseMessageParams(msg, nil, nil, &account); err != nil {
|
|
|
|
return err
|
2020-03-13 10:26:43 +00:00
|
|
|
}
|
2020-03-13 14:12:44 +00:00
|
|
|
uc.logger.Printf("logged in with account %q", account)
|
2020-03-19 13:51:45 +00:00
|
|
|
case irc.RPL_LOGGEDOUT:
|
2020-03-13 14:12:44 +00:00
|
|
|
uc.logger.Printf("logged out")
|
2020-03-19 13:51:45 +00:00
|
|
|
case irc.ERR_NICKLOCKED, irc.RPL_SASLSUCCESS, irc.ERR_SASLFAIL, irc.ERR_SASLTOOLONG, irc.ERR_SASLABORTED:
|
2020-03-13 14:12:44 +00:00
|
|
|
var info string
|
|
|
|
if err := parseMessageParams(msg, nil, &info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch msg.Command {
|
2020-03-19 13:51:45 +00:00
|
|
|
case irc.ERR_NICKLOCKED:
|
2020-03-13 14:12:44 +00:00
|
|
|
uc.logger.Printf("invalid nick used with SASL authentication: %v", info)
|
2020-03-19 13:51:45 +00:00
|
|
|
case irc.ERR_SASLFAIL:
|
2020-03-13 14:12:44 +00:00
|
|
|
uc.logger.Printf("SASL authentication failed: %v", info)
|
2020-03-19 13:51:45 +00:00
|
|
|
case irc.ERR_SASLTOOLONG:
|
2020-03-13 14:12:44 +00:00
|
|
|
uc.logger.Printf("SASL message too long: %v", info)
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.saslClient = nil
|
|
|
|
uc.saslStarted = false
|
|
|
|
|
|
|
|
uc.SendMessage(&irc.Message{
|
|
|
|
Command: "CAP",
|
|
|
|
Params: []string{"END"},
|
|
|
|
})
|
2020-02-06 15:39:09 +00:00
|
|
|
case irc.RPL_WELCOME:
|
2020-02-17 11:36:42 +00:00
|
|
|
uc.registered = true
|
|
|
|
uc.logger.Printf("connection registered")
|
2020-02-06 18:22:04 +00:00
|
|
|
|
2020-04-29 14:28:33 +00:00
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
dc.updateSupportedCaps()
|
|
|
|
})
|
|
|
|
|
2020-06-03 15:18:57 +00:00
|
|
|
if len(uc.network.channels) > 0 {
|
2020-07-06 09:06:20 +00:00
|
|
|
var channels, keys []string
|
2020-06-03 15:18:57 +00:00
|
|
|
for _, ch := range uc.network.channels {
|
2020-07-06 09:06:20 +00:00
|
|
|
channels = append(channels, ch.Name)
|
2020-06-03 15:18:57 +00:00
|
|
|
keys = append(keys, ch.Key)
|
|
|
|
}
|
2020-07-06 09:06:20 +00:00
|
|
|
|
|
|
|
for _, msg := range join(channels, keys) {
|
|
|
|
uc.SendMessage(msg)
|
|
|
|
}
|
2020-02-06 18:22:04 +00:00
|
|
|
}
|
2020-02-06 16:04:49 +00:00
|
|
|
case irc.RPL_MYINFO:
|
2020-03-20 23:48:19 +00:00
|
|
|
if err := parseMessageParams(msg, nil, &uc.serverName, nil, &uc.availableUserModes, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case irc.RPL_ISUPPORT:
|
|
|
|
if err := parseMessageParams(msg, nil, nil); err != nil {
|
2020-02-07 11:36:02 +00:00
|
|
|
return err
|
2020-02-06 16:04:49 +00:00
|
|
|
}
|
2020-03-20 23:48:19 +00:00
|
|
|
for _, token := range msg.Params[1 : len(msg.Params)-1] {
|
|
|
|
negate := false
|
|
|
|
parameter := token
|
|
|
|
value := ""
|
|
|
|
if strings.HasPrefix(token, "-") {
|
|
|
|
negate = true
|
|
|
|
token = token[1:]
|
|
|
|
} else {
|
|
|
|
if i := strings.IndexByte(token, '='); i >= 0 {
|
|
|
|
parameter = token[:i]
|
|
|
|
value = token[i+1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !negate {
|
|
|
|
switch parameter {
|
|
|
|
case "CHANMODES":
|
|
|
|
parts := strings.SplitN(value, ",", 5)
|
|
|
|
if len(parts) < 4 {
|
|
|
|
return fmt.Errorf("malformed ISUPPORT CHANMODES value: %v", value)
|
|
|
|
}
|
|
|
|
modes := make(map[byte]channelModeType)
|
|
|
|
for i, mt := range []channelModeType{modeTypeA, modeTypeB, modeTypeC, modeTypeD} {
|
|
|
|
for j := 0; j < len(parts[i]); j++ {
|
|
|
|
mode := parts[i][j]
|
|
|
|
modes[mode] = mt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uc.availableChannelModes = modes
|
|
|
|
case "CHANTYPES":
|
|
|
|
uc.availableChannelTypes = value
|
|
|
|
case "PREFIX":
|
|
|
|
if value == "" {
|
|
|
|
uc.availableMemberships = nil
|
|
|
|
} else {
|
|
|
|
if value[0] != '(' {
|
|
|
|
return fmt.Errorf("malformed ISUPPORT PREFIX value: %v", value)
|
|
|
|
}
|
|
|
|
sep := strings.IndexByte(value, ')')
|
|
|
|
if sep < 0 || len(value) != sep*2 {
|
|
|
|
return fmt.Errorf("malformed ISUPPORT PREFIX value: %v", value)
|
|
|
|
}
|
|
|
|
memberships := make([]membership, len(value)/2-1)
|
|
|
|
for i := range memberships {
|
|
|
|
memberships[i] = membership{
|
|
|
|
Mode: value[i+1],
|
|
|
|
Prefix: value[sep+i+1],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uc.availableMemberships = memberships
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: handle ISUPPORT negations
|
|
|
|
}
|
2020-02-06 16:04:49 +00:00
|
|
|
}
|
2020-03-23 02:18:16 +00:00
|
|
|
case "BATCH":
|
|
|
|
var tag string
|
|
|
|
if err := parseMessageParams(msg, &tag); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(tag, "+") {
|
|
|
|
tag = tag[1:]
|
|
|
|
if _, ok := uc.batches[tag]; ok {
|
|
|
|
return fmt.Errorf("unexpected BATCH reference tag: batch was already defined: %q", tag)
|
|
|
|
}
|
|
|
|
var batchType string
|
|
|
|
if err := parseMessageParams(msg, nil, &batchType); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-23 02:21:43 +00:00
|
|
|
label := label
|
|
|
|
if label == "" && msgBatch != nil {
|
|
|
|
label = msgBatch.Label
|
|
|
|
}
|
2020-03-23 02:18:16 +00:00
|
|
|
uc.batches[tag] = batch{
|
|
|
|
Type: batchType,
|
|
|
|
Params: msg.Params[2:],
|
|
|
|
Outer: msgBatch,
|
2020-03-23 02:21:43 +00:00
|
|
|
Label: label,
|
2020-03-23 02:18:16 +00:00
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(tag, "-") {
|
|
|
|
tag = tag[1:]
|
|
|
|
if _, ok := uc.batches[tag]; !ok {
|
|
|
|
return fmt.Errorf("unknown BATCH reference tag: %q", tag)
|
|
|
|
}
|
|
|
|
delete(uc.batches, tag)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unexpected BATCH reference tag: missing +/- prefix: %q", tag)
|
|
|
|
}
|
2020-02-07 11:19:42 +00:00
|
|
|
case "NICK":
|
2020-03-06 17:51:11 +00:00
|
|
|
if msg.Prefix == nil {
|
|
|
|
return fmt.Errorf("expected a prefix")
|
|
|
|
}
|
|
|
|
|
2020-02-07 11:36:02 +00:00
|
|
|
var newNick string
|
|
|
|
if err := parseMessageParams(msg, &newNick); err != nil {
|
|
|
|
return err
|
2020-02-07 11:19:42 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 13:01:11 +00:00
|
|
|
me := false
|
2020-02-17 11:36:42 +00:00
|
|
|
if msg.Prefix.Name == uc.nick {
|
|
|
|
uc.logger.Printf("changed nick from %q to %q", uc.nick, newNick)
|
2020-04-07 13:01:11 +00:00
|
|
|
me = true
|
2020-02-17 11:36:42 +00:00
|
|
|
uc.nick = newNick
|
2020-02-07 11:19:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 11:36:42 +00:00
|
|
|
for _, ch := range uc.channels {
|
2020-04-30 21:39:59 +00:00
|
|
|
if memberships, ok := ch.Members[msg.Prefix.Name]; ok {
|
2020-02-07 11:19:42 +00:00
|
|
|
delete(ch.Members, msg.Prefix.Name)
|
2020-04-30 21:39:59 +00:00
|
|
|
ch.Members[newNick] = memberships
|
2020-04-03 16:59:17 +00:00
|
|
|
uc.appendLog(ch.Name, msg)
|
2020-02-07 11:19:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-06 17:42:55 +00:00
|
|
|
|
2020-04-07 13:01:11 +00:00
|
|
|
if !me {
|
2020-03-06 17:42:55 +00:00
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
2020-04-16 15:23:35 +00:00
|
|
|
dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
2020-03-06 17:42:55 +00:00
|
|
|
})
|
2020-04-30 22:37:42 +00:00
|
|
|
} else {
|
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
dc.updateNick()
|
|
|
|
})
|
2020-03-06 17:42:55 +00:00
|
|
|
}
|
2020-02-06 18:22:04 +00:00
|
|
|
case "JOIN":
|
2020-02-19 17:25:19 +00:00
|
|
|
if msg.Prefix == nil {
|
|
|
|
return fmt.Errorf("expected a prefix")
|
|
|
|
}
|
|
|
|
|
2020-02-07 11:36:02 +00:00
|
|
|
var channels string
|
|
|
|
if err := parseMessageParams(msg, &channels); err != nil {
|
|
|
|
return err
|
2020-02-06 18:22:04 +00:00
|
|
|
}
|
2020-02-07 09:54:03 +00:00
|
|
|
|
2020-02-07 11:36:02 +00:00
|
|
|
for _, ch := range strings.Split(channels, ",") {
|
2020-02-17 11:36:42 +00:00
|
|
|
if msg.Prefix.Name == uc.nick {
|
|
|
|
uc.logger.Printf("joined channel %q", ch)
|
|
|
|
uc.channels[ch] = &upstreamChannel{
|
2020-02-07 09:54:03 +00:00
|
|
|
Name: ch,
|
2020-02-17 11:36:42 +00:00
|
|
|
conn: uc,
|
2020-04-30 21:39:59 +00:00
|
|
|
Members: make(map[string]*memberships),
|
2020-02-07 09:54:03 +00:00
|
|
|
}
|
Add customizable auto-detaching, auto-reattaching, relaying.
This uses the fields added previously to the Channel struct to implement
the actual detaching/reattaching/relaying logic.
The `FilterDefault` values of the messages filters are currently
hardcoded.
The values of the message filters are not currently user-settable.
This introduces a new user event, eventChannelDetach, which stores an
upstreamConn (which might become invalid at the time of processing), and
a channel name, used for auto-detaching. Every time the channel detach
timer is refreshed (by receveing a message, etc.), a new timer is
created on the upstreamChannel, which will dispatch this event after the
duration (and discards the previous timer, if any).
2020-11-30 21:08:33 +00:00
|
|
|
uc.updateChannelAutoDetach(ch)
|
2020-03-20 23:48:19 +00:00
|
|
|
|
|
|
|
uc.SendMessage(&irc.Message{
|
|
|
|
Command: "MODE",
|
|
|
|
Params: []string{ch},
|
|
|
|
})
|
2020-02-07 09:54:03 +00:00
|
|
|
} else {
|
2020-02-17 11:36:42 +00:00
|
|
|
ch, err := uc.getChannel(ch)
|
2020-02-07 09:54:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-21 04:59:49 +00:00
|
|
|
ch.Members[msg.Prefix.Name] = &memberships{}
|
2020-02-06 18:22:04 +00:00
|
|
|
}
|
2020-02-07 09:54:03 +00:00
|
|
|
|
2020-04-07 17:45:29 +00:00
|
|
|
chMsg := msg.Copy()
|
|
|
|
chMsg.Params[0] = ch
|
|
|
|
uc.produce(ch, chMsg, nil)
|
2020-02-19 17:25:19 +00:00
|
|
|
}
|
2020-02-07 09:54:03 +00:00
|
|
|
case "PART":
|
2020-02-19 17:25:19 +00:00
|
|
|
if msg.Prefix == nil {
|
|
|
|
return fmt.Errorf("expected a prefix")
|
|
|
|
}
|
|
|
|
|
2020-02-07 11:36:02 +00:00
|
|
|
var channels string
|
|
|
|
if err := parseMessageParams(msg, &channels); err != nil {
|
|
|
|
return err
|
2020-02-07 09:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 11:36:02 +00:00
|
|
|
for _, ch := range strings.Split(channels, ",") {
|
2020-02-17 11:36:42 +00:00
|
|
|
if msg.Prefix.Name == uc.nick {
|
|
|
|
uc.logger.Printf("parted channel %q", ch)
|
Add customizable auto-detaching, auto-reattaching, relaying.
This uses the fields added previously to the Channel struct to implement
the actual detaching/reattaching/relaying logic.
The `FilterDefault` values of the messages filters are currently
hardcoded.
The values of the message filters are not currently user-settable.
This introduces a new user event, eventChannelDetach, which stores an
upstreamConn (which might become invalid at the time of processing), and
a channel name, used for auto-detaching. Every time the channel detach
timer is refreshed (by receveing a message, etc.), a new timer is
created on the upstreamChannel, which will dispatch this event after the
duration (and discards the previous timer, if any).
2020-11-30 21:08:33 +00:00
|
|
|
if uch, ok := uc.channels[ch]; ok {
|
|
|
|
delete(uc.channels, ch)
|
|
|
|
uch.updateAutoDetach(0)
|
|
|
|
}
|
2020-02-07 09:54:03 +00:00
|
|
|
} else {
|
2020-02-17 11:36:42 +00:00
|
|
|
ch, err := uc.getChannel(ch)
|
2020-02-07 09:54:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(ch.Members, msg.Prefix.Name)
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:45:29 +00:00
|
|
|
chMsg := msg.Copy()
|
|
|
|
chMsg.Params[0] = ch
|
|
|
|
uc.produce(ch, chMsg, nil)
|
2020-02-19 17:25:19 +00:00
|
|
|
}
|
2020-03-25 22:46:36 +00:00
|
|
|
case "KICK":
|
|
|
|
if msg.Prefix == nil {
|
|
|
|
return fmt.Errorf("expected a prefix")
|
|
|
|
}
|
|
|
|
|
|
|
|
var channel, user string
|
|
|
|
if err := parseMessageParams(msg, &channel, &user); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == uc.nick {
|
|
|
|
uc.logger.Printf("kicked from channel %q by %s", channel, msg.Prefix.Name)
|
|
|
|
delete(uc.channels, channel)
|
|
|
|
} else {
|
|
|
|
ch, err := uc.getChannel(channel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(ch.Members, user)
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:45:29 +00:00
|
|
|
uc.produce(channel, msg, nil)
|
2020-03-06 17:51:11 +00:00
|
|
|
case "QUIT":
|
|
|
|
if msg.Prefix == nil {
|
|
|
|
return fmt.Errorf("expected a prefix")
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Prefix.Name == uc.nick {
|
|
|
|
uc.logger.Printf("quit")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ch := range uc.channels {
|
2020-03-25 22:51:28 +00:00
|
|
|
if _, ok := ch.Members[msg.Prefix.Name]; ok {
|
|
|
|
delete(ch.Members, msg.Prefix.Name)
|
|
|
|
|
2020-04-03 16:59:17 +00:00
|
|
|
uc.appendLog(ch.Name, msg)
|
2020-03-25 22:51:28 +00:00
|
|
|
}
|
2020-03-06 17:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Prefix.Name != uc.nick {
|
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
2020-04-16 15:23:35 +00:00
|
|
|
dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
2020-03-06 17:51:11 +00:00
|
|
|
})
|
|
|
|
}
|
2020-02-06 18:22:04 +00:00
|
|
|
case irc.RPL_TOPIC, irc.RPL_NOTOPIC:
|
2020-02-07 11:36:02 +00:00
|
|
|
var name, topic string
|
|
|
|
if err := parseMessageParams(msg, nil, &name, &topic); err != nil {
|
|
|
|
return err
|
2020-02-06 18:22:04 +00:00
|
|
|
}
|
2020-02-17 11:36:42 +00:00
|
|
|
ch, err := uc.getChannel(name)
|
2020-02-06 18:22:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if msg.Command == irc.RPL_TOPIC {
|
2020-02-07 11:36:02 +00:00
|
|
|
ch.Topic = topic
|
2020-02-06 18:22:04 +00:00
|
|
|
} else {
|
|
|
|
ch.Topic = ""
|
|
|
|
}
|
|
|
|
case "TOPIC":
|
2020-08-20 08:39:23 +00:00
|
|
|
if msg.Prefix == nil {
|
|
|
|
return fmt.Errorf("expected a prefix")
|
|
|
|
}
|
|
|
|
|
2020-02-07 11:36:02 +00:00
|
|
|
var name string
|
2020-03-04 14:45:14 +00:00
|
|
|
if err := parseMessageParams(msg, &name); err != nil {
|
2020-02-07 11:36:02 +00:00
|
|
|
return err
|
2020-02-06 18:22:04 +00:00
|
|
|
}
|
2020-02-17 11:36:42 +00:00
|
|
|
ch, err := uc.getChannel(name)
|
2020-02-06 18:22:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(msg.Params) > 1 {
|
|
|
|
ch.Topic = msg.Params[1]
|
2020-08-20 08:39:23 +00:00
|
|
|
ch.TopicWho = msg.Prefix.Copy()
|
|
|
|
ch.TopicTime = time.Now() // TODO use msg.Tags["time"]
|
2020-02-06 18:22:04 +00:00
|
|
|
} else {
|
|
|
|
ch.Topic = ""
|
|
|
|
}
|
2020-04-07 17:45:29 +00:00
|
|
|
uc.produce(ch.Name, msg, nil)
|
2020-03-20 23:48:19 +00:00
|
|
|
case "MODE":
|
|
|
|
var name, modeStr string
|
|
|
|
if err := parseMessageParams(msg, &name, &modeStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !uc.isChannel(name) { // user mode change
|
|
|
|
if name != uc.nick {
|
|
|
|
return fmt.Errorf("received MODE message for unknown nick %q", name)
|
|
|
|
}
|
|
|
|
return uc.modes.Apply(modeStr)
|
|
|
|
// TODO: notify downstreams about user mode change?
|
|
|
|
} else { // channel mode change
|
|
|
|
ch, err := uc.getChannel(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-30 21:42:33 +00:00
|
|
|
needMarshaling, err := applyChannelModes(ch, modeStr, msg.Params[2:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-03-20 23:48:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 21:42:33 +00:00
|
|
|
uc.appendLog(ch.Name, msg)
|
2020-06-12 12:35:26 +00:00
|
|
|
|
|
|
|
if ch, ok := uc.network.channels[name]; !ok || !ch.Detached {
|
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
params := make([]string, len(msg.Params))
|
|
|
|
params[0] = dc.marshalEntity(uc.network, name)
|
|
|
|
params[1] = modeStr
|
|
|
|
|
|
|
|
copy(params[2:], msg.Params[2:])
|
|
|
|
for i, modeParam := range params[2:] {
|
|
|
|
if _, ok := needMarshaling[i]; ok {
|
|
|
|
params[2+i] = dc.marshalEntity(uc.network, modeParam)
|
|
|
|
}
|
2020-04-30 21:42:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 12:35:26 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
|
|
|
Command: "MODE",
|
|
|
|
Params: params,
|
|
|
|
})
|
2020-04-30 21:42:33 +00:00
|
|
|
})
|
2020-06-12 12:35:26 +00:00
|
|
|
}
|
2020-03-20 23:48:19 +00:00
|
|
|
}
|
|
|
|
case irc.RPL_UMODEIS:
|
|
|
|
if err := parseMessageParams(msg, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
modeStr := ""
|
|
|
|
if len(msg.Params) > 1 {
|
|
|
|
modeStr = msg.Params[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.modes = ""
|
|
|
|
if err := uc.modes.Apply(modeStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO: send RPL_UMODEIS to downstream connections when applicable
|
|
|
|
case irc.RPL_CHANNELMODEIS:
|
|
|
|
var channel string
|
|
|
|
if err := parseMessageParams(msg, nil, &channel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
modeStr := ""
|
|
|
|
if len(msg.Params) > 2 {
|
|
|
|
modeStr = msg.Params[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := uc.getChannel(channel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
firstMode := ch.modes == nil
|
|
|
|
ch.modes = make(map[byte]string)
|
2020-04-30 21:42:33 +00:00
|
|
|
if _, err := applyChannelModes(ch, modeStr, msg.Params[3:]); err != nil {
|
2020-03-20 23:48:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if firstMode {
|
2020-06-12 12:35:26 +00:00
|
|
|
if c, ok := uc.network.channels[channel]; !ok || !c.Detached {
|
|
|
|
modeStr, modeParams := ch.modes.Format()
|
2020-03-20 23:48:19 +00:00
|
|
|
|
2020-06-12 12:35:26 +00:00
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
params := []string{dc.nick, dc.marshalEntity(uc.network, channel), modeStr}
|
|
|
|
params = append(params, modeParams...)
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_CHANNELMODEIS,
|
|
|
|
Params: params,
|
|
|
|
})
|
2020-03-20 23:48:19 +00:00
|
|
|
})
|
2020-06-12 12:35:26 +00:00
|
|
|
}
|
2020-03-20 23:48:19 +00:00
|
|
|
}
|
2020-03-26 04:51:47 +00:00
|
|
|
case rpl_creationtime:
|
|
|
|
var channel, creationTime string
|
|
|
|
if err := parseMessageParams(msg, nil, &channel, &creationTime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := uc.getChannel(channel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
firstCreationTime := ch.creationTime == ""
|
|
|
|
ch.creationTime = creationTime
|
|
|
|
if firstCreationTime {
|
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: rpl_creationtime,
|
2020-08-20 07:15:47 +00:00
|
|
|
Params: []string{dc.nick, dc.marshalEntity(uc.network, ch.Name), creationTime},
|
2020-03-26 04:51:47 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2020-02-06 18:22:04 +00:00
|
|
|
case rpl_topicwhotime:
|
2020-02-07 11:36:02 +00:00
|
|
|
var name, who, timeStr string
|
|
|
|
if err := parseMessageParams(msg, nil, &name, &who, &timeStr); err != nil {
|
|
|
|
return err
|
2020-02-06 18:22:04 +00:00
|
|
|
}
|
2020-02-17 11:36:42 +00:00
|
|
|
ch, err := uc.getChannel(name)
|
2020-02-06 18:22:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-20 08:39:23 +00:00
|
|
|
firstTopicWhoTime := ch.TopicWho == nil
|
|
|
|
ch.TopicWho = irc.ParsePrefix(who)
|
2020-02-07 11:36:02 +00:00
|
|
|
sec, err := strconv.ParseInt(timeStr, 10, 64)
|
2020-02-06 18:22:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse topic time: %v", err)
|
|
|
|
}
|
|
|
|
ch.TopicTime = time.Unix(sec, 0)
|
2020-08-20 08:39:23 +00:00
|
|
|
if firstTopicWhoTime {
|
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
topicWho := dc.marshalUserPrefix(uc.network, ch.TopicWho)
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: rpl_topicwhotime,
|
|
|
|
Params: []string{
|
|
|
|
dc.nick,
|
|
|
|
dc.marshalEntity(uc.network, ch.Name),
|
|
|
|
topicWho.String(),
|
|
|
|
timeStr,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.
Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.
This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)
When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.
Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.
When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.
Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-26 01:40:30 +00:00
|
|
|
case irc.RPL_LIST:
|
|
|
|
var channel, clients, topic string
|
|
|
|
if err := parseMessageParams(msg, nil, &channel, &clients, &topic); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-28 00:03:00 +00:00
|
|
|
pl := uc.getPendingLIST()
|
Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.
Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.
This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)
When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.
Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.
When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.
Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-26 01:40:30 +00:00
|
|
|
if pl == nil {
|
|
|
|
return fmt.Errorf("unexpected RPL_LIST: no matching pending LIST")
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.forEachDownstreamByID(pl.downstreamID, func(dc *downstreamConn) {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_LIST,
|
2020-04-16 15:19:00 +00:00
|
|
|
Params: []string{dc.nick, dc.marshalEntity(uc.network, channel), clients, topic},
|
Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.
Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.
This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)
When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.
Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.
When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.
Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-26 01:40:30 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
case irc.RPL_LISTEND:
|
2020-03-28 00:03:00 +00:00
|
|
|
ok := uc.endPendingLISTs(false)
|
Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.
Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.
This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)
When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.
Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.
When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.
Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-26 01:40:30 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected RPL_LISTEND: no matching pending LIST")
|
|
|
|
}
|
2020-02-06 18:22:04 +00:00
|
|
|
case irc.RPL_NAMREPLY:
|
2020-02-07 11:36:02 +00:00
|
|
|
var name, statusStr, members string
|
|
|
|
if err := parseMessageParams(msg, nil, &statusStr, &name, &members); err != nil {
|
|
|
|
return err
|
2020-02-06 18:22:04 +00:00
|
|
|
}
|
2020-03-21 00:24:29 +00:00
|
|
|
|
|
|
|
ch, ok := uc.channels[name]
|
|
|
|
if !ok {
|
|
|
|
// NAMES on a channel we have not joined, forward to downstream
|
2020-03-26 01:39:04 +00:00
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
2020-04-16 15:19:00 +00:00
|
|
|
channel := dc.marshalEntity(uc.network, name)
|
2020-03-27 19:09:38 +00:00
|
|
|
members := splitSpace(members)
|
2020-03-21 00:24:29 +00:00
|
|
|
for i, member := range members {
|
2020-04-30 21:39:59 +00:00
|
|
|
memberships, nick := uc.parseMembershipPrefix(member)
|
|
|
|
members[i] = memberships.Format(dc) + dc.marshalEntity(uc.network, nick)
|
2020-03-21 00:24:29 +00:00
|
|
|
}
|
|
|
|
memberStr := strings.Join(members, " ")
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_NAMREPLY,
|
|
|
|
Params: []string{dc.nick, statusStr, channel, memberStr},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return nil
|
2020-02-06 18:22:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 11:36:02 +00:00
|
|
|
status, err := parseChannelStatus(statusStr)
|
2020-02-06 18:22:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ch.Status = status
|
|
|
|
|
2020-03-27 19:09:38 +00:00
|
|
|
for _, s := range splitSpace(members) {
|
2020-04-30 21:39:59 +00:00
|
|
|
memberships, nick := uc.parseMembershipPrefix(s)
|
|
|
|
ch.Members[nick] = memberships
|
2020-02-06 18:22:04 +00:00
|
|
|
}
|
|
|
|
case irc.RPL_ENDOFNAMES:
|
2020-02-07 11:36:02 +00:00
|
|
|
var name string
|
|
|
|
if err := parseMessageParams(msg, nil, &name); err != nil {
|
|
|
|
return err
|
2020-02-06 20:43:22 +00:00
|
|
|
}
|
2020-03-21 00:24:29 +00:00
|
|
|
|
|
|
|
ch, ok := uc.channels[name]
|
|
|
|
if !ok {
|
|
|
|
// NAMES on a channel we have not joined, forward to downstream
|
2020-03-26 01:39:04 +00:00
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
2020-04-16 15:19:00 +00:00
|
|
|
channel := dc.marshalEntity(uc.network, name)
|
2020-03-21 00:24:29 +00:00
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFNAMES,
|
|
|
|
Params: []string{dc.nick, channel, "End of /NAMES list"},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return nil
|
2020-02-06 20:43:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 09:54:03 +00:00
|
|
|
if ch.complete {
|
|
|
|
return fmt.Errorf("received unexpected RPL_ENDOFNAMES")
|
|
|
|
}
|
2020-02-06 20:43:22 +00:00
|
|
|
ch.complete = true
|
2020-02-06 21:19:31 +00:00
|
|
|
|
2020-06-12 12:35:26 +00:00
|
|
|
if c, ok := uc.network.channels[name]; !ok || !c.Detached {
|
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
forwardChannel(dc, ch)
|
|
|
|
})
|
|
|
|
}
|
2020-03-19 23:23:19 +00:00
|
|
|
case irc.RPL_WHOREPLY:
|
|
|
|
var channel, username, host, server, nick, mode, trailing string
|
|
|
|
if err := parseMessageParams(msg, nil, &channel, &username, &host, &server, &nick, &mode, &trailing); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(trailing, " ", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return fmt.Errorf("received malformed RPL_WHOREPLY: wrong trailing parameter: %s", trailing)
|
|
|
|
}
|
|
|
|
realname := parts[1]
|
|
|
|
hops, err := strconv.Atoi(parts[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("received malformed RPL_WHOREPLY: wrong hop count: %s", parts[0])
|
|
|
|
}
|
|
|
|
hops++
|
|
|
|
|
|
|
|
trailing = strconv.Itoa(hops) + " " + realname
|
|
|
|
|
2020-03-26 01:39:04 +00:00
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
2020-03-19 23:23:19 +00:00
|
|
|
channel := channel
|
|
|
|
if channel != "*" {
|
2020-04-16 15:19:00 +00:00
|
|
|
channel = dc.marshalEntity(uc.network, channel)
|
2020-03-19 23:23:19 +00:00
|
|
|
}
|
2020-04-16 15:19:00 +00:00
|
|
|
nick := dc.marshalEntity(uc.network, nick)
|
2020-03-19 23:23:19 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_WHOREPLY,
|
|
|
|
Params: []string{dc.nick, channel, username, host, server, nick, mode, trailing},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
case irc.RPL_ENDOFWHO:
|
|
|
|
var name string
|
|
|
|
if err := parseMessageParams(msg, nil, &name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-26 01:39:04 +00:00
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
2020-03-19 23:23:19 +00:00
|
|
|
name := name
|
|
|
|
if name != "*" {
|
|
|
|
// TODO: support WHO masks
|
2020-04-16 15:19:00 +00:00
|
|
|
name = dc.marshalEntity(uc.network, name)
|
2020-03-19 23:23:19 +00:00
|
|
|
}
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFWHO,
|
2020-03-21 23:46:56 +00:00
|
|
|
Params: []string{dc.nick, name, "End of /WHO list"},
|
2020-03-20 01:15:23 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
case irc.RPL_WHOISUSER:
|
|
|
|
var nick, username, host, realname string
|
|
|
|
if err := parseMessageParams(msg, nil, &nick, &username, &host, nil, &realname); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-26 01:39:04 +00:00
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
2020-04-16 15:19:00 +00:00
|
|
|
nick := dc.marshalEntity(uc.network, nick)
|
2020-03-20 01:15:23 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_WHOISUSER,
|
|
|
|
Params: []string{dc.nick, nick, username, host, "*", realname},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
case irc.RPL_WHOISSERVER:
|
|
|
|
var nick, server, serverInfo string
|
|
|
|
if err := parseMessageParams(msg, nil, &nick, &server, &serverInfo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-26 01:39:04 +00:00
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
2020-04-16 15:19:00 +00:00
|
|
|
nick := dc.marshalEntity(uc.network, nick)
|
2020-03-20 01:15:23 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_WHOISSERVER,
|
|
|
|
Params: []string{dc.nick, nick, server, serverInfo},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
case irc.RPL_WHOISOPERATOR:
|
|
|
|
var nick string
|
|
|
|
if err := parseMessageParams(msg, nil, &nick); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-26 01:39:04 +00:00
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
2020-04-16 15:19:00 +00:00
|
|
|
nick := dc.marshalEntity(uc.network, nick)
|
2020-03-20 01:15:23 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_WHOISOPERATOR,
|
|
|
|
Params: []string{dc.nick, nick, "is an IRC operator"},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
case irc.RPL_WHOISIDLE:
|
|
|
|
var nick string
|
|
|
|
if err := parseMessageParams(msg, nil, &nick, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-26 01:39:04 +00:00
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
2020-04-16 15:19:00 +00:00
|
|
|
nick := dc.marshalEntity(uc.network, nick)
|
2020-03-20 01:15:23 +00:00
|
|
|
params := []string{dc.nick, nick}
|
|
|
|
params = append(params, msg.Params[2:]...)
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_WHOISIDLE,
|
|
|
|
Params: params,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
case irc.RPL_WHOISCHANNELS:
|
|
|
|
var nick, channelList string
|
|
|
|
if err := parseMessageParams(msg, nil, &nick, &channelList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-27 19:09:38 +00:00
|
|
|
channels := splitSpace(channelList)
|
2020-03-20 01:15:23 +00:00
|
|
|
|
2020-03-26 01:39:04 +00:00
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
2020-04-16 15:19:00 +00:00
|
|
|
nick := dc.marshalEntity(uc.network, nick)
|
2020-03-20 01:15:23 +00:00
|
|
|
channelList := make([]string, len(channels))
|
|
|
|
for i, channel := range channels {
|
2020-03-20 23:48:19 +00:00
|
|
|
prefix, channel := uc.parseMembershipPrefix(channel)
|
2020-04-16 15:19:00 +00:00
|
|
|
channel = dc.marshalEntity(uc.network, channel)
|
2020-04-30 21:39:59 +00:00
|
|
|
channelList[i] = prefix.Format(dc) + channel
|
2020-03-20 01:15:23 +00:00
|
|
|
}
|
|
|
|
channels := strings.Join(channelList, " ")
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_WHOISCHANNELS,
|
|
|
|
Params: []string{dc.nick, nick, channels},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
case irc.RPL_ENDOFWHOIS:
|
|
|
|
var nick string
|
|
|
|
if err := parseMessageParams(msg, nil, &nick); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-26 01:39:04 +00:00
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
2020-04-16 15:19:00 +00:00
|
|
|
nick := dc.marshalEntity(uc.network, nick)
|
2020-03-20 01:15:23 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFWHOIS,
|
2020-03-21 23:46:56 +00:00
|
|
|
Params: []string{dc.nick, nick, "End of /WHOIS list"},
|
2020-03-19 23:23:19 +00:00
|
|
|
})
|
|
|
|
})
|
2020-03-18 02:11:38 +00:00
|
|
|
case "INVITE":
|
2020-04-29 13:00:17 +00:00
|
|
|
var nick, channel string
|
2020-03-18 02:11:38 +00:00
|
|
|
if err := parseMessageParams(msg, &nick, &channel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
dc.SendMessage(&irc.Message{
|
2020-04-16 15:19:00 +00:00
|
|
|
Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
2020-03-18 02:11:38 +00:00
|
|
|
Command: "INVITE",
|
2020-04-16 15:19:00 +00:00
|
|
|
Params: []string{dc.marshalEntity(uc.network, nick), dc.marshalEntity(uc.network, channel)},
|
2020-03-18 02:11:38 +00:00
|
|
|
})
|
|
|
|
})
|
2020-03-26 05:03:07 +00:00
|
|
|
case irc.RPL_INVITING:
|
2020-04-29 13:00:17 +00:00
|
|
|
var nick, channel string
|
2020-05-27 21:46:27 +00:00
|
|
|
if err := parseMessageParams(msg, nil, &nick, &channel); err != nil {
|
2020-03-26 05:03:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_INVITING,
|
2020-04-16 15:19:00 +00:00
|
|
|
Params: []string{dc.nick, dc.marshalEntity(uc.network, nick), dc.marshalEntity(uc.network, channel)},
|
2020-03-26 05:03:07 +00:00
|
|
|
})
|
|
|
|
})
|
2020-04-29 12:53:48 +00:00
|
|
|
case irc.RPL_AWAY:
|
|
|
|
var nick, reason string
|
|
|
|
if err := parseMessageParams(msg, nil, &nick, &reason); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-29 13:32:22 +00:00
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
2020-04-29 12:53:48 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_AWAY,
|
|
|
|
Params: []string{dc.nick, dc.marshalEntity(uc.network, nick), reason},
|
|
|
|
})
|
|
|
|
})
|
2020-04-29 14:28:33 +00:00
|
|
|
case "AWAY":
|
|
|
|
if msg.Prefix == nil {
|
|
|
|
return fmt.Errorf("expected a prefix")
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
if !dc.caps["away-notify"] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.marshalUserPrefix(uc.network, msg.Prefix),
|
|
|
|
Command: "AWAY",
|
|
|
|
Params: msg.Params,
|
|
|
|
})
|
|
|
|
})
|
2020-05-19 15:06:52 +00:00
|
|
|
case irc.RPL_BANLIST, irc.RPL_INVITELIST, irc.RPL_EXCEPTLIST:
|
|
|
|
var channel, mask string
|
|
|
|
if err := parseMessageParams(msg, nil, &channel, &mask); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var addNick, addTime string
|
|
|
|
if len(msg.Params) >= 5 {
|
|
|
|
addNick = msg.Params[3]
|
|
|
|
addTime = msg.Params[4]
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
|
|
|
channel := dc.marshalEntity(uc.network, channel)
|
|
|
|
|
|
|
|
var params []string
|
|
|
|
if addNick != "" && addTime != "" {
|
|
|
|
addNick := dc.marshalEntity(uc.network, addNick)
|
|
|
|
params = []string{dc.nick, channel, mask, addNick, addTime}
|
|
|
|
} else {
|
|
|
|
params = []string{dc.nick, channel, mask}
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: msg.Command,
|
|
|
|
Params: params,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
case irc.RPL_ENDOFBANLIST, irc.RPL_ENDOFINVITELIST, irc.RPL_ENDOFEXCEPTLIST:
|
|
|
|
var channel, trailing string
|
|
|
|
if err := parseMessageParams(msg, nil, &channel, &trailing); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
|
|
|
upstreamChannel := dc.marshalEntity(uc.network, channel)
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: msg.Command,
|
|
|
|
Params: []string{dc.nick, upstreamChannel, trailing},
|
|
|
|
})
|
|
|
|
})
|
2020-05-19 15:33:44 +00:00
|
|
|
case irc.ERR_UNKNOWNCOMMAND, irc.RPL_TRYAGAIN:
|
|
|
|
var command, reason string
|
|
|
|
if err := parseMessageParams(msg, nil, &command, &reason); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if command == "LIST" {
|
|
|
|
ok := uc.endPendingLISTs(false)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected response for LIST: %q: no matching pending LIST", msg.Command)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 16:21:52 +00:00
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: uc.srv.prefix(),
|
|
|
|
Command: msg.Command,
|
|
|
|
Params: []string{dc.nick, command, reason},
|
2020-05-19 15:33:44 +00:00
|
|
|
})
|
2020-07-08 16:21:52 +00:00
|
|
|
})
|
2020-03-23 02:21:43 +00:00
|
|
|
case "ACK":
|
|
|
|
// Ignore
|
2020-04-01 10:16:32 +00:00
|
|
|
case irc.RPL_NOWAWAY, irc.RPL_UNAWAY:
|
|
|
|
// Ignore
|
2020-02-06 16:04:49 +00:00
|
|
|
case irc.RPL_YOURHOST, irc.RPL_CREATED:
|
2020-02-06 15:39:09 +00:00
|
|
|
// Ignore
|
|
|
|
case irc.RPL_LUSERCLIENT, irc.RPL_LUSEROP, irc.RPL_LUSERUNKNOWN, irc.RPL_LUSERCHANNELS, irc.RPL_LUSERME:
|
|
|
|
// Ignore
|
|
|
|
case irc.RPL_MOTDSTART, irc.RPL_MOTD, irc.RPL_ENDOFMOTD:
|
|
|
|
// Ignore
|
Add LIST support
This commit adds support for downstream LIST messages from multiple
concurrent downstreams to multiple concurrent upstreams, including
support for multiple pending LIST requests from the same downstream.
Because a unique RPL_LISTEND message must be sent to the requesting
downstream, and that there might be multiple upstreams, each sending
their own RPL_LISTEND, a cache of RPL_LISTEND replies of some sort is
required to match RPL_LISTEND together in order to only send one back
downstream.
This commit adds a list of "pending LIST" structs, which each contain a
map of all upstreams that yet need to send a RPL_LISTEND, and the
corresponding LIST request associated with that response. This list of
pending LISTs is sorted according to the order that the requesting
downstreams sent the LIST messages in. Each pending set also stores the
id of the requesting downstream, in order to only forward the replies to
it and no other downstream. (This is important because LIST replies can
typically amount to several thousands messages on large servers.)
When a single downstream makes multiple LIST requests, only the first
one will be immediately sent to the upstream servers. The next ones will
be buffered until the first one is completed. Distinct downstreams can
make concurrent LIST requests without any request buffering.
Each RPL_LIST message is forwarded to the downstream of the first
matching pending LIST struct.
When an upstream sends an RPL_LISTEND message, the upstream is removed
from the first matching pending LIST struct, but that message is not
immediately forwarded downstream. If there are no remaining pending LIST
requests in that struct is then empty, that means all upstreams have
sent back all their RPL_LISTEND replies (which means they also sent all
their RPL_LIST replies); so a unique RPL_LISTEND is sent to downstream
and that pending LIST set is removed from the cache.
Upstreams are removed from the pending LIST structs in two other cases:
- when they are closed (to avoid stalling because of a disconnected
upstream that will never reply to the LIST message): they are removed
from all pending LIST structs
- when they reply with an ERR_UNKNOWNCOMMAND or RPL_TRYAGAIN LIST reply,
which is typically used when a user is not allowed to LIST because they
just joined the server: they are removed from the first pending LIST
struct, as if an RPL_LISTEND message was received
2020-03-26 01:40:30 +00:00
|
|
|
case irc.RPL_LISTSTART:
|
|
|
|
// Ignore
|
2020-02-06 15:39:09 +00:00
|
|
|
case rpl_localusers, rpl_globalusers:
|
|
|
|
// Ignore
|
2020-03-13 15:18:53 +00:00
|
|
|
case irc.RPL_STATSVLINE, rpl_statsping, irc.RPL_STATSBLINE, irc.RPL_STATSDLINE:
|
2020-02-06 15:39:09 +00:00
|
|
|
// Ignore
|
2020-08-13 13:30:41 +00:00
|
|
|
case "ERROR":
|
|
|
|
var text string
|
|
|
|
if err := parseMessageParams(msg, &text); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("fatal server error: %v", text)
|
2020-08-13 13:25:34 +00:00
|
|
|
case irc.ERR_PASSWDMISMATCH, irc.ERR_ERRONEUSNICKNAME, irc.ERR_NICKNAMEINUSE, irc.ERR_NICKCOLLISION, irc.ERR_UNAVAILRESOURCE, irc.ERR_NOPERMFORHOST, irc.ERR_YOUREBANNEDCREEP:
|
2020-06-29 15:52:49 +00:00
|
|
|
if !uc.registered {
|
2020-08-19 21:35:12 +00:00
|
|
|
text := msg.Params[len(msg.Params)-1]
|
|
|
|
return registrationError(text)
|
2020-06-29 15:52:49 +00:00
|
|
|
}
|
|
|
|
fallthrough
|
2020-02-06 15:18:19 +00:00
|
|
|
default:
|
2020-03-13 14:12:44 +00:00
|
|
|
uc.logger.Printf("unhandled message: %v", msg)
|
2020-07-08 16:21:52 +00:00
|
|
|
|
|
|
|
uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) {
|
|
|
|
// best effort marshaling for unknown messages, replies and errors:
|
|
|
|
// most numerics start with the user nick, marshal it if that's the case
|
|
|
|
// otherwise, conservately keep the params without marshaling
|
|
|
|
params := msg.Params
|
|
|
|
if _, err := strconv.Atoi(msg.Command); err == nil { // numeric
|
|
|
|
if len(msg.Params) > 0 && isOurNick(uc.network, msg.Params[0]) {
|
|
|
|
params[0] = dc.nick
|
2020-05-19 15:33:44 +00:00
|
|
|
}
|
2020-07-08 16:21:52 +00:00
|
|
|
}
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: uc.srv.prefix(),
|
|
|
|
Command: msg.Command,
|
|
|
|
Params: params,
|
2020-05-19 15:33:44 +00:00
|
|
|
})
|
2020-07-08 16:21:52 +00:00
|
|
|
})
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
2020-02-06 15:39:09 +00:00
|
|
|
return nil
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
Add customizable auto-detaching, auto-reattaching, relaying.
This uses the fields added previously to the Channel struct to implement
the actual detaching/reattaching/relaying logic.
The `FilterDefault` values of the messages filters are currently
hardcoded.
The values of the message filters are not currently user-settable.
This introduces a new user event, eventChannelDetach, which stores an
upstreamConn (which might become invalid at the time of processing), and
a channel name, used for auto-detaching. Every time the channel detach
timer is refreshed (by receveing a message, etc.), a new timer is
created on the upstreamChannel, which will dispatch this event after the
duration (and discards the previous timer, if any).
2020-11-30 21:08:33 +00:00
|
|
|
func (uc *upstreamConn) handleDetachedMessage(sender string, text string, ch *Channel) {
|
|
|
|
highlight := sender != uc.nick && isHighlight(text, uc.nick)
|
|
|
|
if ch.RelayDetached == FilterMessage || ((ch.RelayDetached == FilterHighlight || ch.RelayDetached == FilterDefault) && highlight) {
|
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
|
|
|
if highlight {
|
|
|
|
sendServiceNOTICE(dc, fmt.Sprintf("highlight in %v: <%v> %v", dc.marshalEntity(uc.network, ch.Name), sender, text))
|
|
|
|
} else {
|
|
|
|
sendServiceNOTICE(dc, fmt.Sprintf("message in %v: <%v> %v", dc.marshalEntity(uc.network, ch.Name), sender, text))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if ch.ReattachOn == FilterMessage || (ch.ReattachOn == FilterHighlight && highlight) {
|
|
|
|
uc.network.attach(ch)
|
|
|
|
if err := uc.srv.db.StoreChannel(uc.network.ID, ch); err != nil {
|
|
|
|
uc.logger.Printf("failed to update channel %q: %v", ch.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 13:27:41 +00:00
|
|
|
func (uc *upstreamConn) handleSupportedCaps(capsStr string) {
|
|
|
|
caps := strings.Fields(capsStr)
|
|
|
|
for _, s := range caps {
|
|
|
|
kv := strings.SplitN(s, "=", 2)
|
|
|
|
k := strings.ToLower(kv[0])
|
|
|
|
var v string
|
|
|
|
if len(kv) == 2 {
|
|
|
|
v = kv[1]
|
|
|
|
}
|
|
|
|
uc.supportedCaps[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uc *upstreamConn) requestCaps() {
|
|
|
|
var requestCaps []string
|
2020-04-30 14:10:39 +00:00
|
|
|
for c := range permanentUpstreamCaps {
|
2020-04-30 13:27:41 +00:00
|
|
|
if _, ok := uc.supportedCaps[c]; ok && !uc.caps[c] {
|
|
|
|
requestCaps = append(requestCaps, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if uc.requestSASL() && !uc.caps["sasl"] {
|
|
|
|
requestCaps = append(requestCaps, "sasl")
|
|
|
|
}
|
|
|
|
|
2020-04-30 14:10:39 +00:00
|
|
|
if len(requestCaps) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.SendMessage(&irc.Message{
|
|
|
|
Command: "CAP",
|
|
|
|
Params: []string{"REQ", strings.Join(requestCaps, " ")},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uc *upstreamConn) requestSASL() bool {
|
|
|
|
if uc.network.SASL.Mechanism == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := uc.supportedCaps["sasl"]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if v != "" {
|
|
|
|
mechanisms := strings.Split(v, ",")
|
|
|
|
found := false
|
|
|
|
for _, mech := range mechanisms {
|
|
|
|
if strings.EqualFold(mech, uc.network.SASL.Mechanism) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (uc *upstreamConn) handleCapAck(name string, ok bool) error {
|
|
|
|
uc.caps[name] = ok
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "sasl":
|
|
|
|
if !ok {
|
|
|
|
uc.logger.Printf("server refused to acknowledge the SASL capability")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := &uc.network.SASL
|
|
|
|
switch auth.Mechanism {
|
|
|
|
case "PLAIN":
|
|
|
|
uc.logger.Printf("starting SASL PLAIN authentication with username %q", auth.Plain.Username)
|
|
|
|
uc.saslClient = sasl.NewPlainClient("", auth.Plain.Username, auth.Plain.Password)
|
2020-05-29 11:10:54 +00:00
|
|
|
case "EXTERNAL":
|
|
|
|
uc.logger.Printf("starting SASL EXTERNAL authentication")
|
|
|
|
uc.saslClient = sasl.NewExternalClient("")
|
2020-04-30 14:10:39 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported SASL mechanism %q", name)
|
|
|
|
}
|
|
|
|
|
2020-04-30 13:27:41 +00:00
|
|
|
uc.SendMessage(&irc.Message{
|
2020-04-30 14:10:39 +00:00
|
|
|
Command: "AUTHENTICATE",
|
|
|
|
Params: []string{auth.Mechanism},
|
2020-04-30 13:27:41 +00:00
|
|
|
})
|
2020-04-30 14:10:39 +00:00
|
|
|
default:
|
|
|
|
if permanentUpstreamCaps[name] {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
uc.logger.Printf("received CAP ACK/NAK for a cap we don't support: %v", name)
|
2020-04-30 13:27:41 +00:00
|
|
|
}
|
2020-04-30 14:10:39 +00:00
|
|
|
return nil
|
2020-04-30 13:27:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:09:38 +00:00
|
|
|
func splitSpace(s string) []string {
|
|
|
|
return strings.FieldsFunc(s, func(r rune) bool {
|
|
|
|
return r == ' '
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:36:42 +00:00
|
|
|
func (uc *upstreamConn) register() {
|
2020-03-04 17:22:58 +00:00
|
|
|
uc.nick = uc.network.Nick
|
|
|
|
uc.username = uc.network.Username
|
|
|
|
if uc.username == "" {
|
|
|
|
uc.username = uc.nick
|
|
|
|
}
|
|
|
|
uc.realname = uc.network.Realname
|
|
|
|
if uc.realname == "" {
|
|
|
|
uc.realname = uc.nick
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:26:43 +00:00
|
|
|
uc.SendMessage(&irc.Message{
|
|
|
|
Command: "CAP",
|
|
|
|
Params: []string{"LS", "302"},
|
|
|
|
})
|
|
|
|
|
2020-03-13 11:06:02 +00:00
|
|
|
if uc.network.Pass != "" {
|
|
|
|
uc.SendMessage(&irc.Message{
|
|
|
|
Command: "PASS",
|
|
|
|
Params: []string{uc.network.Pass},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-17 15:17:31 +00:00
|
|
|
uc.SendMessage(&irc.Message{
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: "NICK",
|
2020-02-19 17:25:19 +00:00
|
|
|
Params: []string{uc.nick},
|
2020-02-17 15:17:31 +00:00
|
|
|
})
|
|
|
|
uc.SendMessage(&irc.Message{
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: "USER",
|
2020-03-04 17:22:58 +00:00
|
|
|
Params: []string{uc.username, "0", "*", uc.realname},
|
2020-02-17 15:17:31 +00:00
|
|
|
})
|
2020-02-07 11:37:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 10:14:36 +00:00
|
|
|
func (uc *upstreamConn) runUntilRegistered() error {
|
|
|
|
for !uc.registered {
|
2020-04-03 15:01:25 +00:00
|
|
|
msg, err := uc.ReadMessage()
|
2020-04-01 10:14:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := uc.handleMessage(msg); err != nil {
|
2020-08-19 21:35:12 +00:00
|
|
|
if _, ok := err.(registrationError); ok {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
msg.Tags = nil // prevent message tags from cluttering logs
|
|
|
|
return fmt.Errorf("failed to handle message %q: %v", msg, err)
|
|
|
|
}
|
2020-04-01 10:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 23:40:50 +00:00
|
|
|
for _, command := range uc.network.ConnectCommands {
|
|
|
|
m, err := irc.ParseMessage(command)
|
|
|
|
if err != nil {
|
|
|
|
uc.logger.Printf("failed to parse connect command %q: %v", command, err)
|
|
|
|
} else {
|
|
|
|
uc.SendMessage(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 10:14:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:33:19 +00:00
|
|
|
func (uc *upstreamConn) readMessages(ch chan<- event) error {
|
2020-02-06 15:18:19 +00:00
|
|
|
for {
|
2020-04-03 14:34:11 +00:00
|
|
|
msg, err := uc.ReadMessage()
|
2020-02-06 15:18:19 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("failed to read IRC command: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:33:19 +00:00
|
|
|
ch <- eventUpstreamMessage{msg, uc}
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 11:42:24 +00:00
|
|
|
return nil
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
2020-02-17 15:17:31 +00:00
|
|
|
|
2020-05-21 05:04:34 +00:00
|
|
|
func (uc *upstreamConn) SendMessage(msg *irc.Message) {
|
|
|
|
if !uc.caps["message-tags"] {
|
|
|
|
msg = msg.Copy()
|
|
|
|
msg.Tags = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.conn.SendMessage(msg)
|
|
|
|
}
|
|
|
|
|
2020-03-26 03:30:11 +00:00
|
|
|
func (uc *upstreamConn) SendMessageLabeled(downstreamID uint64, msg *irc.Message) {
|
2020-04-29 17:45:37 +00:00
|
|
|
if uc.caps["labeled-response"] {
|
2020-03-23 02:21:43 +00:00
|
|
|
if msg.Tags == nil {
|
|
|
|
msg.Tags = make(map[string]irc.TagValue)
|
|
|
|
}
|
2020-03-26 03:30:11 +00:00
|
|
|
msg.Tags["label"] = irc.TagValue(fmt.Sprintf("sd-%d-%d", downstreamID, uc.nextLabelID))
|
2020-03-26 01:39:04 +00:00
|
|
|
uc.nextLabelID++
|
2020-03-23 02:21:43 +00:00
|
|
|
}
|
|
|
|
uc.SendMessage(msg)
|
|
|
|
}
|
2020-03-25 22:51:28 +00:00
|
|
|
|
2020-08-28 15:21:08 +00:00
|
|
|
// appendLog appends a message to the log file.
|
|
|
|
//
|
|
|
|
// The internal message ID is returned. If the message isn't recorded in the
|
|
|
|
// log file, an empty string is returned.
|
|
|
|
func (uc *upstreamConn) appendLog(entity string, msg *irc.Message) (msgID string) {
|
2020-10-25 10:13:51 +00:00
|
|
|
if uc.user.msgStore == nil {
|
2020-08-28 15:21:08 +00:00
|
|
|
return ""
|
2020-03-25 22:51:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 13:27:41 +00:00
|
|
|
detached := false
|
|
|
|
if ch, ok := uc.network.channels[entity]; ok {
|
|
|
|
detached = ch.Detached
|
|
|
|
}
|
|
|
|
|
2020-04-10 17:22:47 +00:00
|
|
|
history, ok := uc.network.history[entity]
|
|
|
|
if !ok {
|
2020-10-25 10:13:51 +00:00
|
|
|
lastID, err := uc.user.msgStore.LastMsgID(uc.network, entity, time.Now())
|
2020-08-19 11:24:05 +00:00
|
|
|
if err != nil {
|
|
|
|
uc.logger.Printf("failed to log message: failed to get last message ID: %v", err)
|
2020-08-28 15:21:08 +00:00
|
|
|
return ""
|
2020-08-19 11:24:05 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 17:22:47 +00:00
|
|
|
history = &networkHistory{
|
2020-08-19 11:24:05 +00:00
|
|
|
clients: make(map[string]string),
|
2020-04-10 17:22:47 +00:00
|
|
|
}
|
|
|
|
uc.network.history[entity] = history
|
|
|
|
|
|
|
|
for clientName, _ := range uc.network.offlineClients {
|
2020-08-19 11:24:05 +00:00
|
|
|
history.clients[clientName] = lastID
|
2020-04-10 17:22:47 +00:00
|
|
|
}
|
2020-04-28 13:27:41 +00:00
|
|
|
|
|
|
|
if detached {
|
|
|
|
// If the channel is detached, online clients act as offline
|
|
|
|
// clients too
|
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
2020-08-19 11:24:05 +00:00
|
|
|
history.clients[dc.clientName] = lastID
|
2020-04-28 13:27:41 +00:00
|
|
|
})
|
|
|
|
}
|
2020-04-10 17:22:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 10:13:51 +00:00
|
|
|
msgID, err := uc.user.msgStore.Append(uc.network, entity, msg)
|
2020-08-19 11:24:05 +00:00
|
|
|
if err != nil {
|
|
|
|
uc.logger.Printf("failed to log message: %v", err)
|
2020-08-28 15:21:08 +00:00
|
|
|
return ""
|
2020-08-19 11:24:05 +00:00
|
|
|
}
|
2020-08-11 15:12:06 +00:00
|
|
|
|
2020-08-28 15:21:08 +00:00
|
|
|
return msgID
|
2020-04-10 17:22:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 11:24:05 +00:00
|
|
|
// produce appends a message to the logs and forwards it to connected downstream
|
|
|
|
// connections.
|
2020-04-07 17:45:29 +00:00
|
|
|
//
|
|
|
|
// If origin is not nil and origin doesn't support echo-message, the message is
|
|
|
|
// forwarded to all connections except origin.
|
2020-04-06 19:42:55 +00:00
|
|
|
func (uc *upstreamConn) produce(target string, msg *irc.Message, origin *downstreamConn) {
|
2020-08-28 15:21:08 +00:00
|
|
|
var msgID string
|
2020-04-06 19:42:55 +00:00
|
|
|
if target != "" {
|
2020-08-28 15:21:08 +00:00
|
|
|
msgID = uc.appendLog(target, msg)
|
2020-04-06 19:42:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 13:27:41 +00:00
|
|
|
// Don't forward messages if it's a detached channel
|
|
|
|
if ch, ok := uc.network.channels[target]; ok && ch.Detached {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:51:42 +00:00
|
|
|
uc.forEachDownstream(func(dc *downstreamConn) {
|
2020-04-06 19:34:45 +00:00
|
|
|
if dc != origin || dc.caps["echo-message"] {
|
2020-08-28 15:21:08 +00:00
|
|
|
dc.sendMessageWithID(dc.marshalMessage(msg, uc.network), msgID)
|
|
|
|
} else {
|
|
|
|
dc.advanceMessageWithID(msg, msgID)
|
2020-04-06 19:34:45 +00:00
|
|
|
}
|
2020-04-06 15:51:42 +00:00
|
|
|
})
|
2020-04-06 15:03:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 10:16:32 +00:00
|
|
|
func (uc *upstreamConn) updateAway() {
|
|
|
|
away := true
|
|
|
|
uc.forEachDownstream(func(*downstreamConn) {
|
|
|
|
away = false
|
|
|
|
})
|
|
|
|
if away == uc.away {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if away {
|
|
|
|
uc.SendMessage(&irc.Message{
|
|
|
|
Command: "AWAY",
|
|
|
|
Params: []string{"Auto away"},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
uc.SendMessage(&irc.Message{
|
|
|
|
Command: "AWAY",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
uc.away = away
|
|
|
|
}
|
Add customizable auto-detaching, auto-reattaching, relaying.
This uses the fields added previously to the Channel struct to implement
the actual detaching/reattaching/relaying logic.
The `FilterDefault` values of the messages filters are currently
hardcoded.
The values of the message filters are not currently user-settable.
This introduces a new user event, eventChannelDetach, which stores an
upstreamConn (which might become invalid at the time of processing), and
a channel name, used for auto-detaching. Every time the channel detach
timer is refreshed (by receveing a message, etc.), a new timer is
created on the upstreamChannel, which will dispatch this event after the
duration (and discards the previous timer, if any).
2020-11-30 21:08:33 +00:00
|
|
|
|
|
|
|
func (uc *upstreamConn) updateChannelAutoDetach(name string) {
|
|
|
|
if uch, ok := uc.channels[name]; ok {
|
|
|
|
if ch, ok := uc.network.channels[name]; ok && !ch.Detached {
|
|
|
|
uch.updateAutoDetach(ch.DetachAfter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|