2020-03-13 17:13:03 +00:00
|
|
|
package soju
|
2020-02-06 15:18:19 +00:00
|
|
|
|
|
|
|
import (
|
2021-12-09 22:21:08 +00:00
|
|
|
"bytes"
|
2021-10-18 17:15:15 +00:00
|
|
|
"context"
|
2020-03-12 20:28:09 +00:00
|
|
|
"crypto/tls"
|
2020-03-16 15:16:27 +00:00
|
|
|
"encoding/base64"
|
2021-10-29 14:03:04 +00:00
|
|
|
"errors"
|
2020-02-06 15:18:19 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2020-03-16 14:05:24 +00:00
|
|
|
"strconv"
|
2020-02-07 10:46:44 +00:00
|
|
|
"strings"
|
2020-03-12 20:28:09 +00:00
|
|
|
"time"
|
2020-02-06 15:18:19 +00:00
|
|
|
|
2021-11-27 10:48:10 +00:00
|
|
|
"github.com/SherClockHolmes/webpush-go"
|
2020-03-16 15:16:27 +00:00
|
|
|
"github.com/emersion/go-sasl"
|
2022-11-14 11:06:58 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2022-05-09 10:34:43 +00:00
|
|
|
|
2022-10-14 08:44:32 +00:00
|
|
|
"git.sr.ht/~emersion/soju/auth"
|
2022-05-09 10:34:43 +00:00
|
|
|
"git.sr.ht/~emersion/soju/database"
|
2022-05-09 14:25:57 +00:00
|
|
|
"git.sr.ht/~emersion/soju/msgstore"
|
2022-05-09 14:15:00 +00:00
|
|
|
"git.sr.ht/~emersion/soju/xirc"
|
2020-02-06 15:18:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ircError struct {
|
|
|
|
Message *irc.Message
|
|
|
|
}
|
|
|
|
|
2020-03-11 18:09:32 +00:00
|
|
|
func (err ircError) Error() string {
|
|
|
|
return err.Message.String()
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:18:19 +00:00
|
|
|
func newUnknownCommandError(cmd string) ircError {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_UNKNOWNCOMMAND,
|
|
|
|
Params: []string{
|
|
|
|
"*",
|
|
|
|
cmd,
|
|
|
|
"Unknown command",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
func newUnknownIRCError(cmd, text string) ircError {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: xirc.ERR_UNKNOWNERROR,
|
|
|
|
Params: []string{"*", cmd, text},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:18:19 +00:00
|
|
|
func newNeedMoreParamsError(cmd string) ircError {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NEEDMOREPARAMS,
|
|
|
|
Params: []string{
|
|
|
|
"*",
|
|
|
|
cmd,
|
|
|
|
"Not enough parameters",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
func newChatHistoryError(subcommand string, target string) ircError {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"CHATHISTORY", "MESSAGE_ERROR", subcommand, target, "Messages could not be retrieved"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2021-11-29 12:14:16 +00:00
|
|
|
// authErrorReason returns the user-friendly reason of an authentication
|
|
|
|
// failure.
|
|
|
|
func authErrorReason(err error) string {
|
2023-02-23 21:32:24 +00:00
|
|
|
if authErr, ok := err.(*auth.Error); ok {
|
|
|
|
return authErr.ExternalMsg
|
2021-11-29 12:14:16 +00:00
|
|
|
} else {
|
|
|
|
return "Authentication failed"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 08:27:59 +00:00
|
|
|
func parseBouncerNetID(subcommand, s string) (int64, error) {
|
2021-01-22 19:55:53 +00:00
|
|
|
id, err := strconv.ParseInt(s, 10, 64)
|
2022-03-21 14:00:30 +00:00
|
|
|
if err != nil || id <= 0 {
|
2021-01-22 19:55:53 +00:00
|
|
|
return 0, ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
2021-03-10 08:27:59 +00:00
|
|
|
Params: []string{"BOUNCER", "INVALID_NETID", subcommand, s, "Invalid network ID"},
|
2021-01-22 19:55:53 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2022-05-09 10:34:43 +00:00
|
|
|
func fillNetworkAddrAttrs(attrs irc.Tags, network *database.Network) {
|
2021-10-29 13:51:13 +00:00
|
|
|
u, err := network.URL()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hasHostPort := true
|
|
|
|
switch u.Scheme {
|
|
|
|
case "ircs":
|
2022-11-14 11:06:58 +00:00
|
|
|
attrs["tls"] = "1"
|
2021-10-29 13:51:13 +00:00
|
|
|
case "irc+insecure":
|
2022-11-14 11:06:58 +00:00
|
|
|
attrs["tls"] = "0"
|
2021-10-29 13:51:13 +00:00
|
|
|
default: // e.g. unix://
|
|
|
|
hasHostPort = false
|
|
|
|
}
|
|
|
|
if host, port, err := net.SplitHostPort(u.Host); err == nil && hasHostPort {
|
2022-11-14 11:06:58 +00:00
|
|
|
attrs["host"] = host
|
|
|
|
attrs["port"] = port
|
2021-10-29 13:51:13 +00:00
|
|
|
} else if hasHostPort {
|
2022-11-14 11:06:58 +00:00
|
|
|
attrs["host"] = u.Host
|
2021-10-29 13:51:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 08:27:59 +00:00
|
|
|
func getNetworkAttrs(network *network) irc.Tags {
|
|
|
|
state := "disconnected"
|
|
|
|
if uc := network.conn; uc != nil {
|
|
|
|
state = "connected"
|
|
|
|
}
|
|
|
|
|
|
|
|
attrs := irc.Tags{
|
2022-11-14 11:06:58 +00:00
|
|
|
"name": network.GetName(),
|
|
|
|
"state": state,
|
|
|
|
"nickname": database.GetNick(&network.user.User, &network.Network),
|
2021-03-10 08:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if network.Username != "" {
|
2022-11-14 11:06:58 +00:00
|
|
|
attrs["username"] = network.Username
|
2021-03-10 08:27:59 +00:00
|
|
|
}
|
2022-05-09 10:34:43 +00:00
|
|
|
if realname := database.GetRealname(&network.user.User, &network.Network); realname != "" {
|
2022-11-14 11:06:58 +00:00
|
|
|
attrs["realname"] = realname
|
2021-03-10 08:27:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 20:33:38 +00:00
|
|
|
if network.lastError != nil {
|
2022-11-14 11:06:58 +00:00
|
|
|
attrs["error"] = network.lastError.Error()
|
2022-04-08 20:33:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 13:51:13 +00:00
|
|
|
fillNetworkAddrAttrs(attrs, &network.Network)
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkAddrFromAttrs(attrs irc.Tags) string {
|
2022-04-08 20:33:37 +00:00
|
|
|
host := string(attrs["host"])
|
|
|
|
if host == "" {
|
2021-10-29 13:51:13 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := host
|
2022-04-08 20:33:37 +00:00
|
|
|
if port := string(attrs["port"]); port != "" {
|
2021-10-29 13:51:13 +00:00
|
|
|
addr += ":" + port
|
|
|
|
}
|
|
|
|
|
2022-04-08 20:33:37 +00:00
|
|
|
if tlsStr := string(attrs["tls"]); tlsStr == "0" {
|
2022-05-03 08:43:02 +00:00
|
|
|
addr = "irc+insecure://" + addr
|
2021-10-29 13:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2022-05-09 10:34:43 +00:00
|
|
|
func updateNetworkAttrs(record *database.Network, attrs irc.Tags, subcommand string) error {
|
2021-10-29 13:51:13 +00:00
|
|
|
addrAttrs := irc.Tags{}
|
|
|
|
fillNetworkAddrAttrs(addrAttrs, record)
|
|
|
|
|
|
|
|
updateAddr := false
|
|
|
|
for k, v := range attrs {
|
|
|
|
s := string(v)
|
|
|
|
switch k {
|
|
|
|
case "host", "port", "tls":
|
|
|
|
updateAddr = true
|
|
|
|
addrAttrs[k] = v
|
|
|
|
case "name":
|
|
|
|
record.Name = s
|
|
|
|
case "nickname":
|
|
|
|
record.Nick = s
|
|
|
|
case "username":
|
|
|
|
record.Username = s
|
|
|
|
case "realname":
|
|
|
|
record.Realname = s
|
|
|
|
case "pass":
|
|
|
|
record.Pass = s
|
2021-03-10 08:27:59 +00:00
|
|
|
default:
|
2021-10-29 13:51:13 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"BOUNCER", "UNKNOWN_ATTRIBUTE", subcommand, k, "Unknown attribute"},
|
|
|
|
}}
|
2021-03-10 08:27:59 +00:00
|
|
|
}
|
2021-10-29 13:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if updateAddr {
|
|
|
|
record.Addr = networkAddrFromAttrs(addrAttrs)
|
|
|
|
if record.Addr == "" {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"BOUNCER", "NEED_ATTRIBUTE", subcommand, "host", "Missing required host attribute"},
|
|
|
|
}}
|
2021-03-10 08:27:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 13:51:13 +00:00
|
|
|
return nil
|
2021-03-10 08:27:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 09:53:33 +00:00
|
|
|
// illegalNickChars is the list of characters forbidden in a nickname.
|
|
|
|
//
|
2022-08-05 16:37:32 +00:00
|
|
|
// - ' ' and ':' break the IRC message wire format
|
|
|
|
// - '@' and '!' break prefixes
|
|
|
|
// - '*' breaks masks and is the reserved nickname for registration
|
|
|
|
// - '?' breaks masks
|
|
|
|
// - '$' breaks server masks in PRIVMSG/NOTICE
|
|
|
|
// - ',' breaks lists
|
|
|
|
// - '.' is reserved for server names
|
2022-12-08 14:25:39 +00:00
|
|
|
//
|
|
|
|
// See https://modern.ircdocs.horse/#clients
|
2022-03-03 09:54:21 +00:00
|
|
|
const illegalNickChars = " :@!*?$,."
|
2020-08-20 08:00:58 +00:00
|
|
|
|
2022-12-08 14:25:39 +00:00
|
|
|
// illegalChanChars is the list of characters forbidden in a channel name.
|
|
|
|
//
|
|
|
|
// See https://modern.ircdocs.horse/#channels
|
|
|
|
const illegalChanChars = " ,\x07"
|
|
|
|
|
2020-04-29 17:07:15 +00:00
|
|
|
// permanentDownstreamCaps is the list of always-supported downstream
|
|
|
|
// capabilities.
|
|
|
|
var permanentDownstreamCaps = map[string]string{
|
2021-03-10 08:27:59 +00:00
|
|
|
"batch": "",
|
|
|
|
"cap-notify": "",
|
|
|
|
"echo-message": "",
|
|
|
|
"invite-notify": "",
|
|
|
|
"server-time": "",
|
2021-05-25 18:24:45 +00:00
|
|
|
"setname": "",
|
2021-03-10 08:27:59 +00:00
|
|
|
|
2022-07-01 13:58:11 +00:00
|
|
|
"draft/read-marker": "",
|
2022-06-27 13:51:56 +00:00
|
|
|
|
2021-03-10 08:27:59 +00:00
|
|
|
"soju.im/bouncer-networks": "",
|
|
|
|
"soju.im/bouncer-networks-notify": "",
|
2022-03-14 13:49:01 +00:00
|
|
|
"soju.im/no-implicit-names": "",
|
2021-01-29 15:57:38 +00:00
|
|
|
"soju.im/read": "",
|
2022-06-04 08:52:28 +00:00
|
|
|
"soju.im/account-required": "",
|
2021-11-27 10:48:10 +00:00
|
|
|
"soju.im/webpush": "",
|
2020-04-29 17:07:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 21:39:59 +00:00
|
|
|
// needAllDownstreamCaps is the list of downstream capabilities that
|
2022-03-21 15:30:58 +00:00
|
|
|
// require support from all upstreams to be enabled.
|
2020-04-30 21:39:59 +00:00
|
|
|
var needAllDownstreamCaps = map[string]string{
|
2022-11-06 19:39:04 +00:00
|
|
|
"account-notify": "",
|
|
|
|
"account-tag": "",
|
|
|
|
"away-notify": "",
|
|
|
|
"chghost": "",
|
|
|
|
"extended-join": "",
|
|
|
|
"extended-monitor": "",
|
|
|
|
"message-tags": "",
|
|
|
|
"multi-prefix": "",
|
2021-11-15 13:38:19 +00:00
|
|
|
|
|
|
|
"draft/extended-monitor": "",
|
2020-04-30 21:39:59 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 22:41:37 +00:00
|
|
|
// passthroughIsupport is the set of ISUPPORT tokens that are directly passed
|
|
|
|
// through from the upstream server to downstream clients.
|
|
|
|
//
|
|
|
|
// This is only effective in single-upstream mode.
|
|
|
|
var passthroughIsupport = map[string]bool{
|
2021-07-09 20:48:58 +00:00
|
|
|
"AWAYLEN": true,
|
|
|
|
"BOT": true,
|
2023-03-01 11:55:49 +00:00
|
|
|
"CASEMAPPING": true,
|
2021-07-09 20:48:58 +00:00
|
|
|
"CHANLIMIT": true,
|
|
|
|
"CHANMODES": true,
|
|
|
|
"CHANNELLEN": true,
|
|
|
|
"CHANTYPES": true,
|
|
|
|
"CLIENTTAGDENY": true,
|
2021-11-09 21:12:46 +00:00
|
|
|
"ELIST": true,
|
2021-07-09 20:48:58 +00:00
|
|
|
"EXCEPTS": true,
|
|
|
|
"EXTBAN": true,
|
|
|
|
"HOSTLEN": true,
|
|
|
|
"INVEX": true,
|
|
|
|
"KICKLEN": true,
|
2022-08-28 16:53:41 +00:00
|
|
|
"LINELEN": true,
|
2021-07-09 20:48:58 +00:00
|
|
|
"MAXLIST": true,
|
|
|
|
"MAXTARGETS": true,
|
|
|
|
"MODES": true,
|
2021-11-09 16:59:43 +00:00
|
|
|
"MONITOR": true,
|
2021-07-09 20:48:58 +00:00
|
|
|
"NAMELEN": true,
|
|
|
|
"NETWORK": true,
|
|
|
|
"NICKLEN": true,
|
|
|
|
"PREFIX": true,
|
|
|
|
"SAFELIST": true,
|
|
|
|
"TARGMAX": true,
|
|
|
|
"TOPICLEN": true,
|
|
|
|
"USERLEN": true,
|
|
|
|
"UTF8ONLY": true,
|
2021-11-02 17:15:45 +00:00
|
|
|
"WHOX": true,
|
2021-03-15 22:41:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 08:44:32 +00:00
|
|
|
type saslPlain struct {
|
|
|
|
Username, Password string
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:10:54 +00:00
|
|
|
type downstreamSASL struct {
|
2022-10-14 08:44:32 +00:00
|
|
|
server sasl.Server
|
|
|
|
mechanism string
|
|
|
|
plain *saslPlain
|
|
|
|
oauthBearer *sasl.OAuthBearerOptions
|
|
|
|
pendingResp bytes.Buffer
|
2021-11-21 15:10:54 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:37:45 +00:00
|
|
|
type downstreamRegistration struct {
|
|
|
|
nick string
|
|
|
|
username string
|
2022-03-21 14:02:54 +00:00
|
|
|
password string // from PASS
|
|
|
|
|
|
|
|
networkName string
|
|
|
|
networkID int64
|
2022-03-21 14:09:31 +00:00
|
|
|
|
|
|
|
negotiatingCaps bool
|
2022-03-21 13:37:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 08:44:32 +00:00
|
|
|
func serverSASLMechanisms(srv *Server) []string {
|
|
|
|
var l []string
|
|
|
|
if _, ok := srv.Config().Auth.(auth.PlainAuthenticator); ok {
|
|
|
|
l = append(l, "PLAIN")
|
|
|
|
}
|
|
|
|
if _, ok := srv.Config().Auth.(auth.OAuthBearerAuthenticator); ok {
|
|
|
|
l = append(l, "OAUTHBEARER")
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:18:19 +00:00
|
|
|
type downstreamConn struct {
|
2020-04-03 14:34:11 +00:00
|
|
|
conn
|
|
|
|
|
|
|
|
id uint64
|
2020-02-06 20:11:35 +00:00
|
|
|
|
2022-03-21 14:08:12 +00:00
|
|
|
// These don't change after connection registration
|
2022-08-05 16:40:42 +00:00
|
|
|
registered bool
|
|
|
|
user *user
|
|
|
|
network *network // can be nil
|
|
|
|
clientName string
|
2020-03-16 13:28:45 +00:00
|
|
|
|
2022-03-21 14:08:12 +00:00
|
|
|
nick string
|
|
|
|
nickCM string
|
|
|
|
realname string
|
2022-03-21 15:30:58 +00:00
|
|
|
username string
|
2022-03-21 14:08:12 +00:00
|
|
|
hostname string
|
|
|
|
account string // RPL_LOGGEDIN/OUT state
|
2022-07-11 17:36:12 +00:00
|
|
|
away *string
|
2022-03-21 13:37:45 +00:00
|
|
|
|
2022-03-21 14:09:31 +00:00
|
|
|
capVersion int
|
2022-05-29 16:26:28 +00:00
|
|
|
caps xirc.CapRegistry
|
2022-03-21 14:11:43 +00:00
|
|
|
sasl *downstreamSASL // nil unless SASL is underway
|
2022-03-21 14:09:31 +00:00
|
|
|
registration *downstreamRegistration // nil after RPL_WELCOME
|
2020-03-16 14:05:24 +00:00
|
|
|
|
2021-06-05 10:38:52 +00:00
|
|
|
lastBatchRef uint64
|
|
|
|
|
2023-03-01 12:52:33 +00:00
|
|
|
monitored xirc.CaseMappingMap[struct{}]
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 15:02:37 +00:00
|
|
|
func newDownstreamConn(srv *Server, ic ircConn, id uint64) *downstreamConn {
|
|
|
|
remoteAddr := ic.RemoteAddr().String()
|
2020-04-23 20:25:43 +00:00
|
|
|
logger := &prefixLogger{srv.Logger, fmt.Sprintf("downstream %q: ", remoteAddr)}
|
2020-08-19 17:28:29 +00:00
|
|
|
options := connOptions{Logger: logger}
|
2020-02-17 11:36:42 +00:00
|
|
|
dc := &downstreamConn{
|
2022-03-21 13:37:45 +00:00
|
|
|
conn: *newConn(srv, ic, &options),
|
|
|
|
id: id,
|
|
|
|
nick: "*",
|
|
|
|
nickCM: "*",
|
2022-03-21 15:37:04 +00:00
|
|
|
username: "~u",
|
2022-05-29 16:26:28 +00:00
|
|
|
caps: xirc.NewCapRegistry(),
|
2023-03-01 12:52:33 +00:00
|
|
|
monitored: xirc.NewCaseMappingMap[struct{}](xirc.CaseMappingASCII),
|
2022-03-21 13:37:45 +00:00
|
|
|
registration: new(downstreamRegistration),
|
2020-02-06 20:11:35 +00:00
|
|
|
}
|
2022-03-21 15:33:58 +00:00
|
|
|
if host, _, err := net.SplitHostPort(remoteAddr); err == nil {
|
2020-03-21 23:44:55 +00:00
|
|
|
dc.hostname = host
|
2022-03-21 15:33:58 +00:00
|
|
|
} else {
|
|
|
|
dc.hostname = remoteAddr
|
2020-03-21 23:44:55 +00:00
|
|
|
}
|
2020-04-29 17:07:15 +00:00
|
|
|
for k, v := range permanentDownstreamCaps {
|
2022-03-14 18:15:35 +00:00
|
|
|
dc.caps.Available[k] = v
|
2020-04-29 17:07:15 +00:00
|
|
|
}
|
2022-10-14 08:44:32 +00:00
|
|
|
dc.caps.Available["sasl"] = strings.Join(serverSASLMechanisms(dc.srv), ",")
|
2021-11-15 23:38:04 +00:00
|
|
|
// TODO: this is racy, we should only enable chathistory after
|
|
|
|
// authentication and then check that user.msgStore implements
|
|
|
|
// chatHistoryMessageStore
|
2022-12-10 23:01:16 +00:00
|
|
|
switch srv.Config().LogDriver {
|
|
|
|
case "fs", "db":
|
2022-03-14 18:15:35 +00:00
|
|
|
dc.caps.Available["draft/chathistory"] = ""
|
2022-02-21 18:44:56 +00:00
|
|
|
dc.caps.Available["soju.im/search"] = ""
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
}
|
2020-02-17 11:36:42 +00:00
|
|
|
return dc
|
2020-02-06 20:11:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 11:36:42 +00:00
|
|
|
func (dc *downstreamConn) prefix() *irc.Prefix {
|
2020-02-06 21:19:31 +00:00
|
|
|
return &irc.Prefix{
|
2020-02-17 11:36:42 +00:00
|
|
|
Name: dc.nick,
|
2022-03-21 15:30:58 +00:00
|
|
|
User: dc.username,
|
2020-03-21 23:44:55 +00:00
|
|
|
Host: dc.hostname,
|
2020-02-06 21:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:17:06 +00:00
|
|
|
func (dc *downstreamConn) forEachNetwork(f func(*network)) {
|
|
|
|
if dc.network != nil {
|
|
|
|
f(dc.network)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:44:13 +00:00
|
|
|
func (dc *downstreamConn) forEachUpstream(f func(*upstreamConn)) {
|
2022-08-05 16:40:42 +00:00
|
|
|
if dc.network == nil {
|
2021-01-22 19:55:53 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-04 14:44:13 +00:00
|
|
|
dc.user.forEachUpstream(func(uc *upstreamConn) {
|
2020-03-04 17:22:58 +00:00
|
|
|
if dc.network != nil && uc.network != dc.network {
|
2020-03-04 14:44:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
f(uc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
// upstream returns the upstream connection, if any. If there are zero upstream
|
|
|
|
// connections, it returns nil.
|
2020-03-12 17:33:03 +00:00
|
|
|
func (dc *downstreamConn) upstream() *upstreamConn {
|
|
|
|
if dc.network == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-30 08:25:16 +00:00
|
|
|
return dc.network.conn
|
2020-03-12 17:33:03 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
func (dc *downstreamConn) upstreamForCommand(cmd string) (*upstreamConn, error) {
|
|
|
|
if dc.network == nil {
|
|
|
|
return nil, newUnknownIRCError(cmd, "Cannot interact with channels and users on the bouncer connection. Did you mean to use a specific network?")
|
|
|
|
}
|
|
|
|
if dc.network.conn == nil {
|
|
|
|
return nil, newUnknownIRCError(cmd, "Disconnected from upstream network")
|
|
|
|
}
|
|
|
|
return dc.network.conn, nil
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:19:00 +00:00
|
|
|
func isOurNick(net *network, nick string) bool {
|
|
|
|
// TODO: this doesn't account for nick changes
|
|
|
|
if net.conn != nil {
|
2022-07-08 12:52:10 +00:00
|
|
|
return net.conn.isOurNick(nick)
|
2020-04-16 15:19:00 +00:00
|
|
|
}
|
|
|
|
// We're not currently connected to the upstream connection, so we don't
|
|
|
|
// know whether this name is our nickname. Best-effort: use the network's
|
|
|
|
// configured nickname and hope it was the one being used when we were
|
|
|
|
// connected.
|
2022-05-09 10:34:43 +00:00
|
|
|
return net.casemap(nick) == net.casemap(database.GetNick(&net.user.User, &net.Network))
|
2020-04-16 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 20:11:23 +00:00
|
|
|
func (dc *downstreamConn) ReadMessage() (*irc.Message, error) {
|
|
|
|
msg, err := dc.conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dc.srv.metrics.downstreamInMessagesTotal.Inc()
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:33:19 +00:00
|
|
|
func (dc *downstreamConn) readMessages(ch chan<- event) error {
|
2020-02-06 20:11:35 +00:00
|
|
|
for {
|
2020-04-03 14:34:11 +00:00
|
|
|
msg, err := dc.ReadMessage()
|
2021-10-29 14:03:04 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2020-02-06 20:11:35 +00:00
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("failed to read IRC command: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:33:19 +00:00
|
|
|
ch <- eventDownstreamMessage{msg, dc}
|
2020-02-06 20:11:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 11:42:24 +00:00
|
|
|
return nil
|
2020-02-06 20:11:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 16:23:39 +00:00
|
|
|
// SendMessage sends an outgoing message.
|
|
|
|
//
|
|
|
|
// This can only called from the user goroutine.
|
2020-02-17 11:36:42 +00:00
|
|
|
func (dc *downstreamConn) SendMessage(msg *irc.Message) {
|
2022-03-14 18:15:35 +00:00
|
|
|
if !dc.caps.IsEnabled("message-tags") {
|
2020-05-21 05:04:34 +00:00
|
|
|
if msg.Command == "TAGMSG" {
|
|
|
|
return
|
|
|
|
}
|
2020-04-03 18:48:23 +00:00
|
|
|
msg = msg.Copy()
|
|
|
|
for name := range msg.Tags {
|
|
|
|
supported := false
|
|
|
|
switch name {
|
|
|
|
case "time":
|
2022-03-14 18:15:35 +00:00
|
|
|
supported = dc.caps.IsEnabled("server-time")
|
2021-06-14 19:44:38 +00:00
|
|
|
case "account":
|
2022-03-14 18:15:35 +00:00
|
|
|
supported = dc.caps.IsEnabled("account")
|
2022-04-12 15:53:20 +00:00
|
|
|
case "batch":
|
|
|
|
supported = dc.caps.IsEnabled("batch")
|
2020-04-03 18:48:23 +00:00
|
|
|
}
|
|
|
|
if !supported {
|
|
|
|
delete(msg.Tags, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 18:15:35 +00:00
|
|
|
if !dc.caps.IsEnabled("batch") && msg.Tags["batch"] != "" {
|
2021-06-05 10:38:52 +00:00
|
|
|
msg = msg.Copy()
|
|
|
|
delete(msg.Tags, "batch")
|
|
|
|
}
|
2022-03-14 18:15:35 +00:00
|
|
|
if msg.Command == "JOIN" && !dc.caps.IsEnabled("extended-join") {
|
2022-03-21 16:13:55 +00:00
|
|
|
msg = msg.Copy()
|
2020-09-08 17:49:06 +00:00
|
|
|
msg.Params = msg.Params[:1]
|
|
|
|
}
|
2022-03-14 18:15:35 +00:00
|
|
|
if msg.Command == "SETNAME" && !dc.caps.IsEnabled("setname") {
|
2021-05-25 18:24:45 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-21 15:30:58 +00:00
|
|
|
if msg.Command == "CHGHOST" && !dc.caps.IsEnabled("chghost") {
|
|
|
|
return
|
|
|
|
}
|
2022-03-14 18:15:35 +00:00
|
|
|
if msg.Command == "AWAY" && !dc.caps.IsEnabled("away-notify") {
|
2021-10-17 19:53:18 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-14 18:15:35 +00:00
|
|
|
if msg.Command == "ACCOUNT" && !dc.caps.IsEnabled("account-notify") {
|
2021-10-17 19:49:37 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-27 13:51:56 +00:00
|
|
|
if msg.Command == "MARKREAD" && !dc.caps.IsEnabled("draft/read-marker") {
|
|
|
|
return
|
|
|
|
}
|
2022-03-14 18:15:35 +00:00
|
|
|
if msg.Command == "READ" && !dc.caps.IsEnabled("soju.im/read") {
|
2021-01-29 15:57:38 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-21 16:16:02 +00:00
|
|
|
if msg.Prefix != nil && msg.Prefix.Name == "*" {
|
|
|
|
// We use "*" as a sentinel value to simplify upstream message handling
|
|
|
|
msg = msg.Copy()
|
|
|
|
msg.Prefix = nil
|
|
|
|
}
|
2020-04-03 18:48:23 +00:00
|
|
|
|
2021-11-15 20:11:23 +00:00
|
|
|
dc.srv.metrics.downstreamOutMessagesTotal.Inc()
|
2021-12-08 17:03:40 +00:00
|
|
|
dc.conn.SendMessage(context.TODO(), msg)
|
2020-02-17 11:27:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 11:06:58 +00:00
|
|
|
func (dc *downstreamConn) SendBatch(typ string, params []string, tags irc.Tags, f func(batchRef string)) {
|
2021-06-05 10:38:52 +00:00
|
|
|
dc.lastBatchRef++
|
|
|
|
ref := fmt.Sprintf("%v", dc.lastBatchRef)
|
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
if dc.caps.IsEnabled("batch") {
|
2021-06-05 10:38:52 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Tags: tags,
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "BATCH",
|
|
|
|
Params: append([]string{"+" + ref, typ}, params...),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-14 11:06:58 +00:00
|
|
|
f(ref)
|
2021-06-05 10:38:52 +00:00
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
if dc.caps.IsEnabled("batch") {
|
2021-06-05 10:38:52 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "BATCH",
|
|
|
|
Params: []string{"-" + ref},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:21:08 +00:00
|
|
|
// sendMessageWithID sends an outgoing message with the specified internal ID.
|
|
|
|
func (dc *downstreamConn) sendMessageWithID(msg *irc.Message, id string) {
|
|
|
|
dc.SendMessage(msg)
|
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
if id == "" || !dc.messageSupportsBacklog(msg) || dc.caps.IsEnabled("draft/chathistory") {
|
2020-08-28 15:21:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.sendPing(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// advanceMessageWithID advances history to the specified message ID without
|
|
|
|
// sending a message. This is useful e.g. for self-messages when echo-message
|
|
|
|
// isn't enabled.
|
|
|
|
func (dc *downstreamConn) advanceMessageWithID(msg *irc.Message, id string) {
|
2022-03-14 18:15:35 +00:00
|
|
|
if id == "" || !dc.messageSupportsBacklog(msg) || dc.caps.IsEnabled("draft/chathistory") {
|
2020-08-28 15:21:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.sendPing(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ackMsgID acknowledges that a message has been received.
|
|
|
|
func (dc *downstreamConn) ackMsgID(id string) {
|
2022-05-09 14:25:57 +00:00
|
|
|
netID, entity, err := msgstore.ParseMsgID(id, nil)
|
2020-08-28 15:21:08 +00:00
|
|
|
if err != nil {
|
|
|
|
dc.logger.Printf("failed to ACK message ID %q: %v", id, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-04 15:26:30 +00:00
|
|
|
network := dc.user.getNetworkByID(netID)
|
2020-08-28 15:21:08 +00:00
|
|
|
if network == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:49:50 +00:00
|
|
|
network.delivered.StoreID(entity, dc.clientName, id)
|
2020-08-28 15:21:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *downstreamConn) sendPing(msgID string) {
|
2021-03-31 09:59:13 +00:00
|
|
|
token := "soju-msgid-" + msgID
|
2020-08-28 15:21:08 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Command: "PING",
|
|
|
|
Params: []string{token},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *downstreamConn) handlePong(token string) {
|
|
|
|
if !strings.HasPrefix(token, "soju-msgid-") {
|
|
|
|
dc.logger.Printf("received unrecognized PONG token %q", token)
|
|
|
|
return
|
|
|
|
}
|
2021-03-31 09:59:13 +00:00
|
|
|
msgID := strings.TrimPrefix(token, "soju-msgid-")
|
2020-08-28 15:21:08 +00:00
|
|
|
dc.ackMsgID(msgID)
|
|
|
|
}
|
|
|
|
|
2021-11-17 13:54:03 +00:00
|
|
|
func (dc *downstreamConn) handleMessage(ctx context.Context, msg *irc.Message) error {
|
|
|
|
ctx, cancel := dc.conn.NewContext(ctx)
|
2021-11-17 12:13:55 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, handleDownstreamMessageTimeout)
|
2021-11-17 11:38:08 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-02-06 15:18:19 +00:00
|
|
|
switch msg.Command {
|
2020-02-06 21:22:14 +00:00
|
|
|
case "QUIT":
|
2020-02-17 11:36:42 +00:00
|
|
|
return dc.Close()
|
2020-02-06 15:18:19 +00:00
|
|
|
default:
|
2020-02-17 11:36:42 +00:00
|
|
|
if dc.registered {
|
2021-11-17 11:38:08 +00:00
|
|
|
return dc.handleMessageRegistered(ctx, msg)
|
2020-02-06 15:18:19 +00:00
|
|
|
} else {
|
2021-11-17 11:38:08 +00:00
|
|
|
return dc.handleMessageUnregistered(ctx, msg)
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:38:08 +00:00
|
|
|
func (dc *downstreamConn) handleMessageUnregistered(ctx context.Context, msg *irc.Message) error {
|
2020-02-06 15:18:19 +00:00
|
|
|
switch msg.Command {
|
|
|
|
case "NICK":
|
2022-03-21 13:37:45 +00:00
|
|
|
if err := parseMessageParams(msg, &dc.registration.nick); err != nil {
|
2020-02-07 11:36:02 +00:00
|
|
|
return err
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
|
|
|
case "USER":
|
2022-03-21 13:37:45 +00:00
|
|
|
if err := parseMessageParams(msg, &dc.registration.username, nil, nil, nil); err != nil {
|
2020-02-07 11:36:02 +00:00
|
|
|
return err
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
2020-03-11 18:09:32 +00:00
|
|
|
case "PASS":
|
2022-03-21 13:37:45 +00:00
|
|
|
if err := parseMessageParams(msg, &dc.registration.password); err != nil {
|
2020-03-11 18:09:32 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-03-16 14:05:24 +00:00
|
|
|
case "CAP":
|
|
|
|
var subCmd string
|
|
|
|
if err := parseMessageParams(msg, &subCmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := dc.handleCapCommand(subCmd, msg.Params[1:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-16 15:16:27 +00:00
|
|
|
case "AUTHENTICATE":
|
2021-11-21 15:10:54 +00:00
|
|
|
credentials, err := dc.handleAuthenticateCommand(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if credentials == nil {
|
|
|
|
break
|
2020-03-16 15:16:27 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 08:44:32 +00:00
|
|
|
var username, clientName, networkName string
|
|
|
|
switch credentials.mechanism {
|
|
|
|
case "PLAIN":
|
|
|
|
username, clientName, networkName = unmarshalUsername(credentials.plain.Username)
|
|
|
|
password := credentials.plain.Password
|
|
|
|
|
|
|
|
auth, ok := dc.srv.Config().Auth.(auth.PlainAuthenticator)
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("SASL PLAIN not supported")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-02-23 21:32:24 +00:00
|
|
|
if err = auth.AuthPlain(ctx, dc.srv.db, username, password); err != nil {
|
2022-10-14 08:44:32 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case "OAUTHBEARER":
|
|
|
|
auth, ok := dc.srv.Config().Auth.(auth.OAuthBearerAuthenticator)
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("SASL OAUTHBEARER not supported")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-02-23 21:32:24 +00:00
|
|
|
username, err = auth.AuthOAuthBearer(ctx, dc.srv.db, credentials.oauthBearer.Token)
|
|
|
|
if err != nil {
|
2022-10-14 08:44:32 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if credentials.oauthBearer.Username != "" && credentials.oauthBearer.Username != username {
|
2023-02-23 21:32:24 +00:00
|
|
|
err = fmt.Errorf("username mismatch (server returned %q)", username)
|
|
|
|
break
|
2022-10-14 08:44:32 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unexpected SASL mechanism %q", credentials.mechanism))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if username == "" {
|
|
|
|
panic(fmt.Errorf("username unset after SASL authentication"))
|
|
|
|
}
|
|
|
|
err = dc.setUser(username, clientName, networkName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
dc.logger.Printf("SASL %v authentication error for nick %q: %v", credentials.mechanism, dc.nick, err)
|
2021-11-21 15:10:54 +00:00
|
|
|
dc.endSASL(&irc.Message{
|
2020-03-16 15:16:27 +00:00
|
|
|
Prefix: dc.srv.prefix(),
|
2020-03-19 13:51:45 +00:00
|
|
|
Command: irc.ERR_SASLFAIL,
|
2021-11-29 12:14:16 +00:00
|
|
|
Params: []string{dc.nick, authErrorReason(err)},
|
2020-03-16 15:16:27 +00:00
|
|
|
})
|
2021-11-21 15:10:54 +00:00
|
|
|
break
|
2020-03-16 15:16:27 +00:00
|
|
|
}
|
2021-11-21 15:10:54 +00:00
|
|
|
|
|
|
|
// Technically we should send RPL_LOGGEDIN here. However we use
|
|
|
|
// RPL_LOGGEDIN to mirror the upstream connection status. Let's
|
|
|
|
// see how many clients that breaks. See:
|
|
|
|
// https://github.com/ircv3/ircv3-specifications/pull/476
|
|
|
|
dc.endSASL(nil)
|
2021-01-22 19:55:53 +00:00
|
|
|
case "BOUNCER":
|
|
|
|
var subcommand string
|
|
|
|
if err := parseMessageParams(msg, &subcommand); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToUpper(subcommand) {
|
|
|
|
case "BIND":
|
|
|
|
var idStr string
|
|
|
|
if err := parseMessageParams(msg, nil, &idStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dc.user == nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"BOUNCER", "ACCOUNT_REQUIRED", "BIND", "Authentication needed to bind to bouncer network"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2021-03-10 08:27:59 +00:00
|
|
|
id, err := parseBouncerNetID(subcommand, idStr)
|
2021-01-22 19:55:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:02:54 +00:00
|
|
|
dc.registration.networkID = id
|
2021-01-22 19:55:53 +00:00
|
|
|
}
|
2020-02-06 15:18:19 +00:00
|
|
|
default:
|
2020-02-17 11:36:42 +00:00
|
|
|
dc.logger.Printf("unhandled message: %v", msg)
|
2020-02-06 15:18:19 +00:00
|
|
|
return newUnknownCommandError(msg.Command)
|
|
|
|
}
|
2022-03-21 14:09:31 +00:00
|
|
|
if dc.registration.nick != "" && dc.registration.username != "" && !dc.registration.negotiatingCaps {
|
2021-11-17 11:29:23 +00:00
|
|
|
return dc.register(ctx)
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-16 14:05:24 +00:00
|
|
|
func (dc *downstreamConn) handleCapCommand(cmd string, args []string) error {
|
2020-03-16 14:11:08 +00:00
|
|
|
cmd = strings.ToUpper(cmd)
|
|
|
|
|
2020-03-16 14:05:24 +00:00
|
|
|
switch cmd {
|
|
|
|
case "LS":
|
|
|
|
if len(args) > 0 {
|
|
|
|
var err error
|
|
|
|
if dc.capVersion, err = strconv.Atoi(args[0]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 14:41:18 +00:00
|
|
|
if !dc.registered && dc.capVersion >= 302 {
|
|
|
|
// Let downstream show everything it supports, and trim
|
|
|
|
// down the available capabilities when upstreams are
|
|
|
|
// known.
|
|
|
|
for k, v := range needAllDownstreamCaps {
|
2022-03-14 18:15:35 +00:00
|
|
|
dc.caps.Available[k] = v
|
2020-11-26 14:41:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-16 14:05:24 +00:00
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
caps := make([]string, 0, len(dc.caps.Available))
|
|
|
|
for k, v := range dc.caps.Available {
|
2020-04-29 17:07:15 +00:00
|
|
|
if dc.capVersion >= 302 && v != "" {
|
2020-04-29 14:28:33 +00:00
|
|
|
caps = append(caps, k+"="+v)
|
2020-04-29 17:07:15 +00:00
|
|
|
} else {
|
|
|
|
caps = append(caps, k)
|
|
|
|
}
|
2020-03-16 15:16:27 +00:00
|
|
|
}
|
2020-03-16 14:05:24 +00:00
|
|
|
|
|
|
|
// TODO: multi-line replies
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "CAP",
|
2021-11-18 08:19:27 +00:00
|
|
|
Params: []string{dc.nick, "LS", strings.Join(caps, " ")},
|
2020-03-16 14:05:24 +00:00
|
|
|
})
|
|
|
|
|
2020-04-29 17:07:15 +00:00
|
|
|
if dc.capVersion >= 302 {
|
|
|
|
// CAP version 302 implicitly enables cap-notify
|
2022-03-14 18:15:35 +00:00
|
|
|
dc.caps.SetEnabled("cap-notify", true)
|
2020-04-29 17:07:15 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 14:05:24 +00:00
|
|
|
if !dc.registered {
|
2022-03-21 14:09:31 +00:00
|
|
|
dc.registration.negotiatingCaps = true
|
2020-03-16 14:05:24 +00:00
|
|
|
}
|
|
|
|
case "LIST":
|
|
|
|
var caps []string
|
2022-03-14 18:15:35 +00:00
|
|
|
for name := range dc.caps.Enabled {
|
|
|
|
caps = append(caps, name)
|
2020-03-16 14:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: multi-line replies
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "CAP",
|
2021-11-18 08:19:27 +00:00
|
|
|
Params: []string{dc.nick, "LIST", strings.Join(caps, " ")},
|
2020-03-16 14:05:24 +00:00
|
|
|
})
|
|
|
|
case "REQ":
|
|
|
|
if len(args) == 0 {
|
|
|
|
return ircError{&irc.Message{
|
2022-05-09 15:18:51 +00:00
|
|
|
Command: xirc.ERR_INVALIDCAPCMD,
|
2021-11-18 08:19:27 +00:00
|
|
|
Params: []string{dc.nick, cmd, "Missing argument in CAP REQ command"},
|
2020-03-16 14:05:24 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
caps := strings.Fields(args[0])
|
|
|
|
ack := true
|
2022-03-14 18:30:29 +00:00
|
|
|
m := make(map[string]bool, len(caps))
|
2020-03-16 14:05:24 +00:00
|
|
|
for _, name := range caps {
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
enable := !strings.HasPrefix(name, "-")
|
|
|
|
if !enable {
|
|
|
|
name = strings.TrimPrefix(name, "-")
|
|
|
|
}
|
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
if enable == dc.caps.IsEnabled(name) {
|
2020-03-16 14:05:24 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
if !dc.caps.IsAvailable(name) {
|
2020-04-29 17:07:15 +00:00
|
|
|
ack = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "cap-notify" && dc.capVersion >= 302 && !enable {
|
|
|
|
// cap-notify cannot be disabled with CAP version 302
|
2020-03-16 14:05:24 +00:00
|
|
|
ack = false
|
2020-04-29 17:07:15 +00:00
|
|
|
break
|
2020-03-16 14:05:24 +00:00
|
|
|
}
|
2020-04-29 17:07:15 +00:00
|
|
|
|
2022-06-04 08:52:28 +00:00
|
|
|
if name == "soju.im/account-required" {
|
|
|
|
// account-required is an informational cap
|
|
|
|
ack = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-03-14 18:30:29 +00:00
|
|
|
m[name] = enable
|
|
|
|
}
|
|
|
|
|
|
|
|
// Atomically ack the whole capability set
|
|
|
|
if ack {
|
|
|
|
for name, enable := range m {
|
|
|
|
dc.caps.SetEnabled(name, enable)
|
|
|
|
}
|
2020-03-16 14:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reply := "NAK"
|
|
|
|
if ack {
|
|
|
|
reply = "ACK"
|
|
|
|
}
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "CAP",
|
2021-11-18 08:19:27 +00:00
|
|
|
Params: []string{dc.nick, reply, args[0]},
|
2020-03-16 14:05:24 +00:00
|
|
|
})
|
2021-09-19 14:47:27 +00:00
|
|
|
|
|
|
|
if !dc.registered {
|
2022-03-21 14:09:31 +00:00
|
|
|
dc.registration.negotiatingCaps = true
|
2021-09-19 14:47:27 +00:00
|
|
|
}
|
2020-03-16 14:05:24 +00:00
|
|
|
case "END":
|
2022-03-21 14:09:31 +00:00
|
|
|
if !dc.registered {
|
|
|
|
dc.registration.negotiatingCaps = false
|
|
|
|
}
|
2020-03-16 14:05:24 +00:00
|
|
|
default:
|
|
|
|
return ircError{&irc.Message{
|
2022-05-09 15:18:51 +00:00
|
|
|
Command: xirc.ERR_INVALIDCAPCMD,
|
2021-11-18 08:19:27 +00:00
|
|
|
Params: []string{dc.nick, cmd, "Unknown CAP command"},
|
2020-03-16 14:05:24 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:10:54 +00:00
|
|
|
func (dc *downstreamConn) handleAuthenticateCommand(msg *irc.Message) (result *downstreamSASL, err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
dc.sasl = nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
if !dc.caps.IsEnabled("sasl") {
|
2021-11-21 15:10:54 +00:00
|
|
|
return nil, ircError{&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_SASLFAIL,
|
2021-12-07 08:42:32 +00:00
|
|
|
Params: []string{dc.nick, "AUTHENTICATE requires the \"sasl\" capability to be enabled"},
|
2021-11-21 15:10:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
if len(msg.Params) == 0 {
|
|
|
|
return nil, ircError{&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_SASLFAIL,
|
2021-12-07 08:42:32 +00:00
|
|
|
Params: []string{dc.nick, "Missing AUTHENTICATE argument"},
|
2021-11-21 15:10:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
if msg.Params[0] == "*" {
|
|
|
|
return nil, ircError{&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_SASLABORTED,
|
2021-12-07 08:42:32 +00:00
|
|
|
Params: []string{dc.nick, "SASL authentication aborted"},
|
2021-11-21 15:10:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp []byte
|
|
|
|
if dc.sasl == nil {
|
|
|
|
mech := strings.ToUpper(msg.Params[0])
|
|
|
|
var server sasl.Server
|
|
|
|
switch mech {
|
|
|
|
case "PLAIN":
|
|
|
|
server = sasl.NewPlainServer(sasl.PlainAuthenticator(func(identity, username, password string) error {
|
2022-10-14 08:44:32 +00:00
|
|
|
dc.sasl.plain = &saslPlain{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
case "OAUTHBEARER":
|
|
|
|
server = sasl.NewOAuthBearerServer(sasl.OAuthBearerAuthenticator(func(options sasl.OAuthBearerOptions) *sasl.OAuthBearerError {
|
|
|
|
dc.sasl.oauthBearer = &options
|
2021-11-21 15:10:54 +00:00
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
default:
|
|
|
|
return nil, ircError{&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_SASLFAIL,
|
2022-09-12 19:42:03 +00:00
|
|
|
Params: []string{dc.nick, "Unsupported SASL mechanism"},
|
2021-11-21 15:10:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-10-14 08:44:32 +00:00
|
|
|
dc.sasl = &downstreamSASL{server: server, mechanism: mech}
|
2021-11-21 15:10:54 +00:00
|
|
|
} else {
|
2021-12-09 22:21:08 +00:00
|
|
|
chunk := msg.Params[0]
|
|
|
|
if chunk == "+" {
|
|
|
|
chunk = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if dc.sasl.pendingResp.Len()+len(chunk) > 10*1024 {
|
|
|
|
return nil, ircError{&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_SASLFAIL,
|
|
|
|
Params: []string{dc.nick, "Response too long"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.sasl.pendingResp.WriteString(chunk)
|
|
|
|
|
2022-05-30 07:41:47 +00:00
|
|
|
if len(chunk) == xirc.MaxSASLLength {
|
2021-12-09 22:21:08 +00:00
|
|
|
return nil, nil // Multi-line response, wait for the next command
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err = base64.StdEncoding.DecodeString(dc.sasl.pendingResp.String())
|
|
|
|
if err != nil {
|
2021-11-21 15:10:54 +00:00
|
|
|
return nil, ircError{&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_SASLFAIL,
|
2021-12-07 08:42:32 +00:00
|
|
|
Params: []string{dc.nick, "Invalid base64-encoded response"},
|
2021-11-21 15:10:54 +00:00
|
|
|
}}
|
|
|
|
}
|
2021-12-09 22:21:08 +00:00
|
|
|
|
|
|
|
dc.sasl.pendingResp.Reset()
|
2021-11-21 15:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
challenge, done, err := dc.sasl.server.Next(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if done {
|
|
|
|
return dc.sasl, nil
|
|
|
|
} else {
|
|
|
|
challengeStr := "+"
|
|
|
|
if len(challenge) > 0 {
|
|
|
|
challengeStr = base64.StdEncoding.EncodeToString(challenge)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: multi-line messages
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "AUTHENTICATE",
|
|
|
|
Params: []string{challengeStr},
|
|
|
|
})
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *downstreamConn) endSASL(msg *irc.Message) {
|
|
|
|
if dc.sasl == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.sasl = nil
|
|
|
|
|
|
|
|
if msg != nil {
|
|
|
|
dc.SendMessage(msg)
|
|
|
|
} else {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_SASLSUCCESS,
|
|
|
|
Params: []string{dc.nick, "SASL authentication successful"},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 17:07:15 +00:00
|
|
|
func (dc *downstreamConn) setSupportedCap(name, value string) {
|
2022-03-14 18:15:35 +00:00
|
|
|
prevValue, hasPrev := dc.caps.Available[name]
|
2020-04-29 17:07:15 +00:00
|
|
|
changed := !hasPrev || prevValue != value
|
2022-03-14 18:15:35 +00:00
|
|
|
dc.caps.Available[name] = value
|
2020-04-29 17:07:15 +00:00
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
if !dc.caps.IsEnabled("cap-notify") || !changed {
|
2020-04-29 17:07:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cap := name
|
|
|
|
if value != "" && dc.capVersion >= 302 {
|
|
|
|
cap = name + "=" + value
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "CAP",
|
2021-11-18 08:19:27 +00:00
|
|
|
Params: []string{dc.nick, "NEW", cap},
|
2020-04-29 17:07:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *downstreamConn) unsetSupportedCap(name string) {
|
2022-03-14 18:15:35 +00:00
|
|
|
hasPrev := dc.caps.IsAvailable(name)
|
|
|
|
dc.caps.Del(name)
|
2020-04-29 17:07:15 +00:00
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
if !dc.caps.IsEnabled("cap-notify") || !hasPrev {
|
2020-04-29 17:07:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "CAP",
|
2021-11-18 08:19:27 +00:00
|
|
|
Params: []string{dc.nick, "DEL", name},
|
2020-04-29 17:07:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:28:33 +00:00
|
|
|
func (dc *downstreamConn) updateSupportedCaps() {
|
2020-04-30 21:39:59 +00:00
|
|
|
supportedCaps := make(map[string]bool)
|
|
|
|
for cap := range needAllDownstreamCaps {
|
|
|
|
supportedCaps[cap] = true
|
|
|
|
}
|
2020-04-29 14:28:33 +00:00
|
|
|
dc.forEachUpstream(func(uc *upstreamConn) {
|
2020-04-30 21:39:59 +00:00
|
|
|
for cap, supported := range supportedCaps {
|
2022-03-14 18:24:39 +00:00
|
|
|
supportedCaps[cap] = supported && uc.caps.IsEnabled(cap)
|
2020-04-30 21:39:59 +00:00
|
|
|
}
|
2020-04-29 14:28:33 +00:00
|
|
|
})
|
|
|
|
|
2020-04-30 21:39:59 +00:00
|
|
|
for cap, supported := range supportedCaps {
|
|
|
|
if supported {
|
|
|
|
dc.setSupportedCap(cap, needAllDownstreamCaps[cap])
|
|
|
|
} else {
|
|
|
|
dc.unsetSupportedCap(cap)
|
|
|
|
}
|
2020-04-29 14:28:33 +00:00
|
|
|
}
|
2021-10-08 22:13:16 +00:00
|
|
|
|
2021-11-21 15:28:38 +00:00
|
|
|
if uc := dc.upstream(); uc != nil && uc.supportsSASL("PLAIN") {
|
|
|
|
dc.setSupportedCap("sasl", "PLAIN")
|
|
|
|
} else if dc.network != nil {
|
|
|
|
dc.unsetSupportedCap("sasl")
|
|
|
|
}
|
|
|
|
|
2022-03-14 18:24:39 +00:00
|
|
|
if uc := dc.upstream(); uc != nil && uc.caps.IsEnabled("draft/account-registration") {
|
2021-11-30 10:54:11 +00:00
|
|
|
// Strip "before-connect", because we require downstreams to be fully
|
|
|
|
// connected before attempting account registration.
|
2022-03-14 18:24:39 +00:00
|
|
|
values := strings.Split(uc.caps.Available["draft/account-registration"], ",")
|
2021-11-30 10:54:11 +00:00
|
|
|
for i, v := range values {
|
|
|
|
if v == "before-connect" {
|
|
|
|
values = append(values[:i], values[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dc.setSupportedCap("draft/account-registration", strings.Join(values, ","))
|
|
|
|
} else {
|
|
|
|
dc.unsetSupportedCap("draft/account-registration")
|
|
|
|
}
|
|
|
|
|
2022-05-09 14:25:57 +00:00
|
|
|
if _, ok := dc.user.msgStore.(msgstore.ChatHistoryStore); ok && dc.network != nil {
|
2021-10-08 22:13:16 +00:00
|
|
|
dc.setSupportedCap("draft/event-playback", "")
|
|
|
|
} else {
|
|
|
|
dc.unsetSupportedCap("draft/event-playback")
|
|
|
|
}
|
2020-04-29 14:28:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 22:37:42 +00:00
|
|
|
func (dc *downstreamConn) updateNick() {
|
2022-04-01 12:55:36 +00:00
|
|
|
var nick string
|
|
|
|
if uc := dc.upstream(); uc != nil {
|
|
|
|
nick = uc.nick
|
|
|
|
} else if dc.network != nil {
|
2022-05-09 10:34:43 +00:00
|
|
|
nick = database.GetNick(&dc.user.User, &dc.network.Network)
|
2022-04-01 12:55:36 +00:00
|
|
|
} else {
|
2022-05-09 10:34:43 +00:00
|
|
|
nick = database.GetNick(&dc.user.User, nil)
|
2020-04-30 22:37:42 +00:00
|
|
|
}
|
2022-04-01 12:55:36 +00:00
|
|
|
|
|
|
|
if nick == dc.nick {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
|
|
|
Command: "NICK",
|
|
|
|
Params: []string{nick},
|
|
|
|
})
|
|
|
|
dc.nick = nick
|
2023-03-01 12:30:47 +00:00
|
|
|
dc.nickCM = xirc.CaseMappingASCII(dc.nick)
|
2020-04-30 22:37:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 15:09:45 +00:00
|
|
|
func (dc *downstreamConn) updateHost() {
|
2022-03-21 15:30:58 +00:00
|
|
|
uc := dc.upstream()
|
|
|
|
if uc == nil || uc.hostname == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if uc.hostname == dc.hostname && uc.username == dc.username {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if dc.caps.IsEnabled("chghost") {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
|
|
|
Command: "CHGHOST",
|
|
|
|
Params: []string{uc.username, uc.hostname},
|
|
|
|
})
|
|
|
|
} else if uc.hostname != dc.hostname {
|
2022-03-21 15:09:45 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
2022-05-09 15:18:51 +00:00
|
|
|
Command: xirc.RPL_VISIBLEHOST,
|
2022-03-21 15:09:45 +00:00
|
|
|
Params: []string{dc.nick, uc.hostname, "is now your visible host"},
|
|
|
|
})
|
|
|
|
}
|
2022-03-21 15:30:58 +00:00
|
|
|
|
|
|
|
dc.hostname = uc.hostname
|
|
|
|
dc.username = uc.username
|
2022-03-21 15:09:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 18:24:45 +00:00
|
|
|
func (dc *downstreamConn) updateRealname() {
|
2022-03-30 12:15:39 +00:00
|
|
|
if !dc.caps.IsEnabled("setname") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var realname string
|
|
|
|
if uc := dc.upstream(); uc != nil {
|
|
|
|
realname = uc.realname
|
|
|
|
} else if dc.network != nil {
|
2022-05-09 10:34:43 +00:00
|
|
|
realname = database.GetRealname(&dc.user.User, &dc.network.Network)
|
2022-03-30 12:15:39 +00:00
|
|
|
} else {
|
2022-05-09 10:34:43 +00:00
|
|
|
realname = database.GetRealname(&dc.user.User, nil)
|
2022-03-30 12:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if realname != dc.realname {
|
2021-05-25 18:24:45 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
|
|
|
Command: "SETNAME",
|
2022-03-30 12:15:39 +00:00
|
|
|
Params: []string{realname},
|
2021-05-25 18:24:45 +00:00
|
|
|
})
|
2022-03-30 12:15:39 +00:00
|
|
|
dc.realname = realname
|
2021-05-25 18:24:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 18:21:48 +00:00
|
|
|
func (dc *downstreamConn) updateAccount() {
|
2021-11-21 10:00:57 +00:00
|
|
|
var account string
|
|
|
|
if dc.network == nil {
|
|
|
|
account = dc.user.Username
|
|
|
|
} else if uc := dc.upstream(); uc != nil {
|
|
|
|
account = uc.account
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
if dc.account == account || !dc.caps.IsEnabled("sasl") {
|
2021-11-19 18:21:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-21 10:00:57 +00:00
|
|
|
if account != "" {
|
2021-11-19 18:21:48 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_LOGGEDIN,
|
2021-11-21 10:00:57 +00:00
|
|
|
Params: []string{dc.nick, dc.prefix().String(), account, "You are logged in as " + account},
|
2021-11-19 18:21:48 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_LOGGEDOUT,
|
|
|
|
Params: []string{dc.nick, dc.prefix().String(), "You are logged out"},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-21 10:00:57 +00:00
|
|
|
dc.account = account
|
2021-11-19 18:21:48 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 11:55:10 +00:00
|
|
|
func (dc *downstreamConn) updateCasemapping() {
|
2023-03-01 12:30:47 +00:00
|
|
|
cm := xirc.CaseMappingASCII
|
2023-03-01 11:55:10 +00:00
|
|
|
if dc.network != nil {
|
|
|
|
cm = dc.network.casemap
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.nickCM = cm(dc.nick)
|
2023-03-01 12:33:58 +00:00
|
|
|
dc.monitored.SetCaseMapping(cm)
|
2023-03-01 11:55:10 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 11:10:40 +00:00
|
|
|
func sanityCheckServer(ctx context.Context, addr string) error {
|
2021-11-17 11:12:40 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
2021-11-17 11:10:40 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
conn, err := new(tls.Dialer).DialContext(ctx, "tcp", addr)
|
2020-03-12 20:28:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-17 11:10:40 +00:00
|
|
|
|
2020-03-12 20:28:09 +00:00
|
|
|
return conn.Close()
|
|
|
|
}
|
|
|
|
|
2020-03-28 16:25:48 +00:00
|
|
|
func unmarshalUsername(rawUsername string) (username, client, network string) {
|
2020-03-16 15:16:27 +00:00
|
|
|
username = rawUsername
|
2020-03-28 16:25:48 +00:00
|
|
|
|
|
|
|
i := strings.IndexAny(username, "/@")
|
|
|
|
j := strings.LastIndexAny(username, "/@")
|
|
|
|
if i >= 0 {
|
|
|
|
username = rawUsername[:i]
|
|
|
|
}
|
|
|
|
if j >= 0 {
|
2020-03-31 17:02:02 +00:00
|
|
|
if rawUsername[j] == '@' {
|
|
|
|
client = rawUsername[j+1:]
|
|
|
|
} else {
|
|
|
|
network = rawUsername[j+1:]
|
|
|
|
}
|
2020-03-04 14:44:13 +00:00
|
|
|
}
|
2020-03-28 16:25:48 +00:00
|
|
|
if i >= 0 && j >= 0 && i < j {
|
2020-03-31 17:02:02 +00:00
|
|
|
if rawUsername[i] == '@' {
|
|
|
|
client = rawUsername[i+1 : j]
|
|
|
|
} else {
|
|
|
|
network = rawUsername[i+1 : j]
|
|
|
|
}
|
2020-03-04 14:44:13 +00:00
|
|
|
}
|
2020-03-28 16:25:48 +00:00
|
|
|
|
|
|
|
return username, client, network
|
2020-03-16 15:16:27 +00:00
|
|
|
}
|
2020-03-04 14:44:13 +00:00
|
|
|
|
2022-10-14 08:44:32 +00:00
|
|
|
func (dc *downstreamConn) setUser(username, clientName, networkName string) error {
|
2020-03-27 21:38:38 +00:00
|
|
|
dc.user = dc.srv.getUser(username)
|
2023-01-26 19:28:59 +00:00
|
|
|
if dc.user == nil && dc.user.srv.Config().EnableUsersOnAuth {
|
|
|
|
ctx := context.TODO()
|
|
|
|
if _, err := dc.user.srv.db.GetUser(ctx, username); err != nil {
|
|
|
|
// Can't find the user in the DB -- try to create it
|
|
|
|
record := database.User{
|
|
|
|
Username: username,
|
|
|
|
Enabled: true,
|
|
|
|
}
|
|
|
|
dc.user, err = dc.user.srv.createUser(ctx, &record)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to automatically create user %q after successful authentication: %v", username, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 21:38:38 +00:00
|
|
|
if dc.user == nil {
|
2022-03-23 12:11:37 +00:00
|
|
|
return fmt.Errorf("user exists in the DB but hasn't been loaded by the bouncer -- a restart may help")
|
2020-03-27 21:38:38 +00:00
|
|
|
}
|
2020-03-28 16:25:48 +00:00
|
|
|
dc.clientName = clientName
|
2022-03-21 14:02:54 +00:00
|
|
|
dc.registration.networkName = networkName
|
2020-03-27 18:17:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-14 08:44:32 +00:00
|
|
|
func (dc *downstreamConn) authenticate(ctx context.Context, username, password string) error {
|
|
|
|
username, clientName, networkName := unmarshalUsername(username)
|
|
|
|
|
|
|
|
plainAuth, ok := dc.srv.Config().Auth.(auth.PlainAuthenticator)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("PLAIN authentication unsupported")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := plainAuth.AuthPlain(ctx, dc.srv.db, username, password); err != nil {
|
2023-02-23 21:32:24 +00:00
|
|
|
return err
|
2022-10-14 08:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dc.setUser(username, clientName, networkName)
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:29:23 +00:00
|
|
|
func (dc *downstreamConn) register(ctx context.Context) error {
|
2020-03-27 18:17:58 +00:00
|
|
|
if dc.registered {
|
2022-03-03 07:33:10 +00:00
|
|
|
panic("tried to register twice")
|
2020-03-27 18:17:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 15:10:54 +00:00
|
|
|
if dc.sasl != nil {
|
|
|
|
dc.endSASL(&irc.Message{
|
2021-11-19 17:55:07 +00:00
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_SASLABORTED,
|
2021-12-07 08:42:32 +00:00
|
|
|
Params: []string{dc.nick, "SASL authentication aborted"},
|
2021-11-19 17:55:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:37:45 +00:00
|
|
|
password := dc.registration.password
|
|
|
|
dc.registration.password = ""
|
2020-03-27 18:17:58 +00:00
|
|
|
if dc.user == nil {
|
2021-12-07 08:40:02 +00:00
|
|
|
if password == "" {
|
2022-03-14 18:15:35 +00:00
|
|
|
if dc.caps.IsEnabled("sasl") {
|
2021-12-07 08:40:02 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"*", "ACCOUNT_REQUIRED", "Authentication required"},
|
|
|
|
}}
|
|
|
|
} else {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_PASSWDMISMATCH,
|
|
|
|
Params: []string{dc.nick, "Authentication required"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:37:45 +00:00
|
|
|
if err := dc.authenticate(ctx, dc.registration.username, password); err != nil {
|
|
|
|
dc.logger.Printf("PASS authentication error for user %q: %v", dc.registration.username, err)
|
2021-11-29 12:14:16 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_PASSWDMISMATCH,
|
2021-12-07 08:40:02 +00:00
|
|
|
Params: []string{dc.nick, authErrorReason(err)},
|
2021-11-29 12:14:16 +00:00
|
|
|
}}
|
2020-03-27 18:17:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:37:45 +00:00
|
|
|
_, fallbackClientName, fallbackNetworkName := unmarshalUsername(dc.registration.username)
|
2022-03-03 07:35:34 +00:00
|
|
|
if dc.clientName == "" {
|
|
|
|
dc.clientName = fallbackClientName
|
2022-03-03 08:08:51 +00:00
|
|
|
} else if fallbackClientName != "" && dc.clientName != fallbackClientName {
|
2022-03-03 07:50:37 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_ERRONEUSNICKNAME,
|
|
|
|
Params: []string{dc.nick, "Client name mismatch in usernames"},
|
|
|
|
}}
|
2022-03-03 07:35:34 +00:00
|
|
|
}
|
2022-03-21 14:02:54 +00:00
|
|
|
|
|
|
|
if dc.registration.networkName == "" {
|
|
|
|
dc.registration.networkName = fallbackNetworkName
|
|
|
|
} else if fallbackNetworkName != "" && dc.registration.networkName != fallbackNetworkName {
|
2022-03-03 07:50:37 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_ERRONEUSNICKNAME,
|
|
|
|
Params: []string{dc.nick, "Network name mismatch in usernames"},
|
|
|
|
}}
|
2020-03-27 18:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dc.registered = true
|
2022-03-21 15:30:58 +00:00
|
|
|
dc.username = dc.user.Username
|
2020-03-28 16:28:28 +00:00
|
|
|
dc.logger.Printf("registration complete for user %q", dc.user.Username)
|
2020-03-27 18:17:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:33:30 +00:00
|
|
|
func (dc *downstreamConn) loadNetwork(ctx context.Context) error {
|
2022-03-21 14:02:54 +00:00
|
|
|
if id := dc.registration.networkID; id != 0 {
|
|
|
|
network := dc.user.getNetworkByID(id)
|
|
|
|
if network == nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"BOUNCER", "INVALID_NETID", fmt.Sprintf("%v", id), "Unknown network ID"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
dc.network = network
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if dc.registration.networkName == "*" {
|
2022-08-05 16:33:41 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_PASSWDMISMATCH,
|
|
|
|
Params: []string{dc.nick, fmt.Sprintf("Multi-upstream mode is no longer supported")},
|
|
|
|
}}
|
2022-03-21 13:45:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:02:54 +00:00
|
|
|
if dc.registration.networkName == "" {
|
2020-03-16 15:16:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:02:54 +00:00
|
|
|
network := dc.user.getNetwork(dc.registration.networkName)
|
2020-03-16 15:16:27 +00:00
|
|
|
if network == nil {
|
2022-03-21 14:02:54 +00:00
|
|
|
addr := dc.registration.networkName
|
2020-03-16 15:16:27 +00:00
|
|
|
if !strings.ContainsRune(addr, ':') {
|
|
|
|
addr = addr + ":6697"
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.logger.Printf("trying to connect to new network %q", addr)
|
2021-11-17 11:33:30 +00:00
|
|
|
if err := sanityCheckServer(ctx, addr); err != nil {
|
2020-03-16 15:16:27 +00:00
|
|
|
dc.logger.Printf("failed to connect to %q: %v", addr, err)
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_PASSWDMISMATCH,
|
2022-03-21 14:02:54 +00:00
|
|
|
Params: []string{dc.nick, fmt.Sprintf("Failed to connect to %q", dc.registration.networkName)},
|
2020-03-16 15:16:27 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2020-07-06 16:13:40 +00:00
|
|
|
// Some clients only allow specifying the nickname (and use the
|
|
|
|
// nickname as a username too). Strip the network name from the
|
|
|
|
// nickname when auto-saving networks.
|
2022-03-21 13:37:45 +00:00
|
|
|
nick, _, _ := unmarshalUsername(dc.registration.nick)
|
|
|
|
if nick == "" || strings.ContainsAny(nick, illegalNickChars) {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_ERRONEUSNICKNAME,
|
|
|
|
Params: []string{dc.nick, dc.registration.nick, "Nickname contains illegal characters"},
|
|
|
|
}}
|
|
|
|
}
|
2023-03-01 12:30:47 +00:00
|
|
|
if xirc.CaseMappingASCII(nick) == serviceNickCM {
|
2022-03-21 13:37:45 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NICKNAMEINUSE,
|
|
|
|
Params: []string{dc.nick, dc.registration.nick, "Nickname reserved for bouncer service"},
|
|
|
|
}}
|
|
|
|
}
|
2020-07-06 16:13:40 +00:00
|
|
|
|
2022-03-21 14:02:54 +00:00
|
|
|
dc.logger.Printf("auto-saving network %q", dc.registration.networkName)
|
2020-03-16 15:16:27 +00:00
|
|
|
var err error
|
2022-05-09 10:34:43 +00:00
|
|
|
network, err = dc.user.createNetwork(ctx, &database.Network{
|
2022-03-21 14:02:54 +00:00
|
|
|
Addr: dc.registration.networkName,
|
2021-05-26 08:49:52 +00:00
|
|
|
Nick: nick,
|
|
|
|
Enabled: true,
|
2020-03-18 23:57:14 +00:00
|
|
|
})
|
2020-03-16 15:16:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.network = network
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:33:30 +00:00
|
|
|
func (dc *downstreamConn) welcome(ctx context.Context) error {
|
2020-03-27 18:17:58 +00:00
|
|
|
if dc.user == nil || !dc.registered {
|
|
|
|
panic("tried to welcome an unregistered connection")
|
2020-02-07 10:36:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 17:56:00 +00:00
|
|
|
remoteAddr := dc.conn.RemoteAddr().String()
|
|
|
|
dc.logger = &prefixLogger{dc.srv.Logger, fmt.Sprintf("user %q: downstream %q: ", dc.user.Username, remoteAddr)}
|
|
|
|
|
2020-03-27 18:17:58 +00:00
|
|
|
// TODO: doing this might take some time. We should do it in dc.register
|
|
|
|
// instead, but we'll potentially be adding a new network and this must be
|
|
|
|
// done in the user goroutine.
|
2021-11-17 11:33:30 +00:00
|
|
|
if err := dc.loadNetwork(ctx); err != nil {
|
2020-03-27 18:17:58 +00:00
|
|
|
return err
|
2020-03-04 14:44:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:02:54 +00:00
|
|
|
dc.registration = nil
|
|
|
|
|
2021-11-17 14:27:34 +00:00
|
|
|
dc.updateSupportedCaps()
|
|
|
|
|
2022-03-21 13:37:45 +00:00
|
|
|
if uc := dc.upstream(); uc != nil {
|
|
|
|
dc.nick = uc.nick
|
|
|
|
} else if dc.network != nil {
|
2022-05-09 10:34:43 +00:00
|
|
|
dc.nick = database.GetNick(&dc.user.User, &dc.network.Network)
|
2022-03-21 13:37:45 +00:00
|
|
|
} else {
|
|
|
|
dc.nick = dc.user.Username
|
|
|
|
}
|
2023-03-01 12:30:47 +00:00
|
|
|
dc.nickCM = xirc.CaseMappingASCII(dc.nick)
|
2022-03-21 13:37:45 +00:00
|
|
|
|
2023-03-01 11:55:49 +00:00
|
|
|
var isupport []string
|
2021-01-22 19:55:53 +00:00
|
|
|
if dc.network != nil {
|
|
|
|
isupport = append(isupport, fmt.Sprintf("BOUNCER_NETID=%v", dc.network.ID))
|
2021-10-06 10:02:49 +00:00
|
|
|
} else {
|
2023-03-01 11:55:49 +00:00
|
|
|
isupport = append(isupport, "BOT=B", "CASEMAPPING=ascii")
|
2021-01-22 19:55:53 +00:00
|
|
|
}
|
2021-11-15 23:38:04 +00:00
|
|
|
if title := dc.srv.Config().Title; dc.network == nil && title != "" {
|
2022-05-29 16:24:10 +00:00
|
|
|
isupport = append(isupport, "NETWORK="+title)
|
2021-11-02 21:38:07 +00:00
|
|
|
}
|
2022-08-05 16:40:42 +00:00
|
|
|
if dc.network == nil {
|
2021-11-02 17:15:45 +00:00
|
|
|
isupport = append(isupport, "WHOX")
|
2023-01-16 15:46:46 +00:00
|
|
|
isupport = append(isupport, "CHANTYPES=") // channels are not supported
|
2021-11-02 17:15:45 +00:00
|
|
|
}
|
2023-02-02 18:36:18 +00:00
|
|
|
if _, ok := dc.user.msgStore.(msgstore.ChatHistoryStore); ok && dc.network != nil {
|
|
|
|
isupport = append(isupport, fmt.Sprintf("CHATHISTORY=%v", chatHistoryLimit))
|
2023-02-02 18:38:32 +00:00
|
|
|
isupport = append(isupport, "MSGREFTYPES=timestamp")
|
2023-02-02 18:36:18 +00:00
|
|
|
}
|
2021-11-27 10:48:10 +00:00
|
|
|
if dc.caps.IsEnabled("soju.im/webpush") {
|
|
|
|
isupport = append(isupport, "VAPID="+dc.srv.webPush.VAPIDKeys.Public)
|
|
|
|
}
|
2021-11-02 17:15:45 +00:00
|
|
|
|
2021-03-15 22:41:37 +00:00
|
|
|
if uc := dc.upstream(); uc != nil {
|
|
|
|
for k := range passthroughIsupport {
|
|
|
|
v, ok := uc.isupport[k]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if v != nil {
|
|
|
|
isupport = append(isupport, fmt.Sprintf("%v=%v", k, *v))
|
|
|
|
} else {
|
|
|
|
isupport = append(isupport, k)
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 11:00:02 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 11:36:42 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: irc.RPL_WELCOME,
|
2020-03-13 17:13:03 +00:00
|
|
|
Params: []string{dc.nick, "Welcome to soju, " + dc.nick},
|
2020-02-17 11:27:48 +00:00
|
|
|
})
|
2020-02-17 11:36:42 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: irc.RPL_YOURHOST,
|
2021-11-15 23:38:04 +00:00
|
|
|
Params: []string{dc.nick, "Your host is " + dc.srv.Config().Hostname},
|
2020-02-17 11:27:48 +00:00
|
|
|
})
|
2020-02-17 11:36:42 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
2020-02-06 15:18:19 +00:00
|
|
|
Command: irc.RPL_MYINFO,
|
2021-11-15 23:38:04 +00:00
|
|
|
Params: []string{dc.nick, dc.srv.Config().Hostname, "soju", "aiwroO", "OovaimnqpsrtklbeI"},
|
2020-02-17 11:27:48 +00:00
|
|
|
})
|
2022-05-29 15:57:21 +00:00
|
|
|
for _, msg := range xirc.GenerateIsupport(dc.srv.prefix(), dc.nick, isupport) {
|
2021-03-15 22:41:37 +00:00
|
|
|
dc.SendMessage(msg)
|
|
|
|
}
|
2021-06-09 19:58:27 +00:00
|
|
|
if uc := dc.upstream(); uc != nil {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_UMODEIS,
|
2021-11-03 21:02:19 +00:00
|
|
|
Params: []string{dc.nick, "+" + string(uc.modes)},
|
2021-06-09 19:58:27 +00:00
|
|
|
})
|
|
|
|
}
|
2022-08-05 16:40:42 +00:00
|
|
|
if dc.network == nil && dc.user.Admin {
|
2021-11-03 20:41:29 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_UMODEIS,
|
|
|
|
Params: []string{dc.nick, "+o"},
|
|
|
|
})
|
|
|
|
}
|
2021-10-13 08:58:34 +00:00
|
|
|
|
2022-03-21 15:09:45 +00:00
|
|
|
dc.updateHost()
|
2021-11-17 14:27:34 +00:00
|
|
|
dc.updateRealname()
|
2021-11-19 18:21:48 +00:00
|
|
|
dc.updateAccount()
|
2023-03-01 11:55:10 +00:00
|
|
|
dc.updateCasemapping()
|
2021-11-17 14:27:34 +00:00
|
|
|
|
2021-11-15 23:38:04 +00:00
|
|
|
if motd := dc.user.srv.Config().MOTD; motd != "" && dc.network == nil {
|
2022-05-29 15:57:21 +00:00
|
|
|
for _, msg := range xirc.GenerateMOTD(dc.srv.prefix(), dc.nick, motd) {
|
2021-10-13 08:58:34 +00:00
|
|
|
dc.SendMessage(msg)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
motdHint := "No MOTD"
|
|
|
|
if dc.network != nil {
|
|
|
|
motdHint = "Use /motd to read the message of the day"
|
|
|
|
}
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_NOMOTD,
|
|
|
|
Params: []string{dc.nick, motdHint},
|
|
|
|
})
|
|
|
|
}
|
2020-02-06 15:18:19 +00:00
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") {
|
2022-11-14 11:06:58 +00:00
|
|
|
dc.SendBatch("soju.im/bouncer-networks", nil, nil, func(batchRef string) {
|
2022-02-04 13:01:27 +00:00
|
|
|
for _, network := range dc.user.networks {
|
2021-06-05 10:38:52 +00:00
|
|
|
idStr := fmt.Sprintf("%v", network.ID)
|
|
|
|
attrs := getNetworkAttrs(network)
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Tags: irc.Tags{"batch": batchRef},
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "BOUNCER",
|
|
|
|
Params: []string{"NETWORK", idStr, attrs.String()},
|
|
|
|
})
|
2022-02-04 13:01:27 +00:00
|
|
|
}
|
2021-03-10 08:27:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:44:13 +00:00
|
|
|
dc.forEachUpstream(func(uc *upstreamConn) {
|
2023-03-01 12:15:38 +00:00
|
|
|
uc.channels.ForEach(func(_ string, ch *upstreamChannel) {
|
2020-04-28 13:27:41 +00:00
|
|
|
if !ch.complete {
|
2022-06-06 07:58:39 +00:00
|
|
|
return
|
2020-04-28 13:27:41 +00:00
|
|
|
}
|
2022-06-06 07:58:39 +00:00
|
|
|
record := uc.network.channels.Get(ch.Name)
|
Implement casemapping
TL;DR: supports for casemapping, now logs are saved in
casemapped/canonical/tolower form
(eg. in the #channel directory instead of #Channel... or something)
== What is casemapping? ==
see <https://modern.ircdocs.horse/#casemapping-parameter>
== Casemapping and multi-upstream ==
Since each upstream does not necessarily use the same casemapping, and
since casemappings cannot coexist [0],
1. soju must also update the database accordingly to upstreams'
casemapping, otherwise it will end up inconsistent,
2. soju must "normalize" entity names and expose only one casemapping
that is a subset of all supported casemappings (here, ascii).
[0] On some upstreams, "emersion[m]" and "emersion{m}" refer to the same
user (upstreams that advertise rfc1459 for example), while on others
(upstreams that advertise ascii) they don't.
Once upstream's casemapping is known (default to rfc1459), entity names
in map keys are made into casemapped form, for upstreamConn,
upstreamChannel and network.
downstreamConn advertises "CASEMAPPING=ascii", and always casemap map
keys with ascii.
Some functions require the caller to casemap their argument (to avoid
needless calls to casemapping functions).
== Message forwarding and casemapping ==
downstream message handling (joins and parts basically):
When relaying entity names from downstreams to upstreams, soju uses the
upstream casemapping, in order to not get in the way of the user. This
does not brings any issue, as long as soju replies with the ascii
casemapping in mind (solves point 1.).
marshalEntity/marshalUserPrefix:
When relaying entity names from upstreams with non-ascii casemappings,
soju *partially* casemap them: it only change the case of characters
which are not ascii letters. ASCII case is thus kept intact, while
special symbols like []{} are the same every time soju sends them to
downstreams (solves point 2.).
== Casemapping changes ==
Casemapping changes are not fully supported by this patch and will
result in loss of history. This is a limitation of the protocol and
should be solved by the RENAME spec.
2021-03-16 09:00:34 +00:00
|
|
|
if record != nil && record.Detached {
|
2022-06-06 07:58:39 +00:00
|
|
|
return
|
2020-02-06 21:29:24 +00:00
|
|
|
}
|
2020-04-28 13:27:41 +00:00
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
|
|
|
Command: "JOIN",
|
2022-08-05 17:14:03 +00:00
|
|
|
Params: []string{ch.Name},
|
2020-04-28 13:27:41 +00:00
|
|
|
})
|
|
|
|
|
2021-01-29 15:57:38 +00:00
|
|
|
forwardChannel(ctx, dc, ch)
|
2022-06-06 07:58:39 +00:00
|
|
|
})
|
2020-03-25 09:53:08 +00:00
|
|
|
})
|
2020-02-07 15:43:14 +00:00
|
|
|
|
2020-03-25 09:53:08 +00:00
|
|
|
dc.forEachNetwork(func(net *network) {
|
2022-03-14 18:15:35 +00:00
|
|
|
if dc.caps.IsEnabled("draft/chathistory") || dc.user.msgStore == nil {
|
2021-04-13 15:50:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-10 17:22:47 +00:00
|
|
|
// Only send history if we're the first connected client with that name
|
|
|
|
// for the network
|
2021-03-29 14:55:57 +00:00
|
|
|
firstClient := true
|
2022-04-15 08:32:28 +00:00
|
|
|
for _, c := range dc.user.downstreamConns {
|
2021-03-29 14:55:57 +00:00
|
|
|
if c != dc && c.clientName == dc.clientName && c.network == dc.network {
|
|
|
|
firstClient = false
|
|
|
|
}
|
2022-04-15 08:32:28 +00:00
|
|
|
}
|
2021-03-29 14:55:57 +00:00
|
|
|
if firstClient {
|
2021-03-29 15:49:50 +00:00
|
|
|
net.delivered.ForEachTarget(func(target string) {
|
2021-04-13 15:49:37 +00:00
|
|
|
lastDelivered := net.delivered.LoadID(target, dc.clientName)
|
|
|
|
if lastDelivered == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:33:30 +00:00
|
|
|
dc.sendTargetBacklog(ctx, net, target, lastDelivered)
|
2021-04-13 15:49:37 +00:00
|
|
|
|
|
|
|
// Fast-forward history to last message
|
|
|
|
targetCM := net.casemap(target)
|
2021-11-03 15:37:01 +00:00
|
|
|
lastID, err := dc.user.msgStore.LastMsgID(&net.Network, targetCM, time.Now())
|
2021-04-13 15:49:37 +00:00
|
|
|
if err != nil {
|
|
|
|
dc.logger.Printf("failed to get last message ID: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
net.delivered.StoreID(target, dc.clientName, lastID)
|
2021-03-29 15:49:50 +00:00
|
|
|
})
|
2020-04-10 17:22:47 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-08 22:13:16 +00:00
|
|
|
// messageSupportsBacklog checks whether the provided message can be sent as
|
2020-08-28 15:21:08 +00:00
|
|
|
// part of an history batch.
|
2021-10-08 22:13:16 +00:00
|
|
|
func (dc *downstreamConn) messageSupportsBacklog(msg *irc.Message) bool {
|
2020-08-28 15:21:08 +00:00
|
|
|
// Don't replay all messages, because that would mess up client
|
|
|
|
// state. For instance we just sent the list of users, sending
|
|
|
|
// PART messages for one of these users would be incorrect.
|
|
|
|
switch msg.Command {
|
|
|
|
case "PRIVMSG", "NOTICE":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:33:30 +00:00
|
|
|
func (dc *downstreamConn) sendTargetBacklog(ctx context.Context, net *network, target, msgID string) {
|
2022-03-14 18:15:35 +00:00
|
|
|
if dc.caps.IsEnabled("draft/chathistory") || dc.user.msgStore == nil {
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-13 17:11:05 +00:00
|
|
|
|
2022-06-06 07:58:39 +00:00
|
|
|
ch := net.channels.Get(target)
|
2021-03-29 15:49:50 +00:00
|
|
|
|
2021-11-17 11:33:30 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, backlogTimeout)
|
2021-11-03 17:18:04 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-03-29 15:07:39 +00:00
|
|
|
targetCM := net.casemap(target)
|
2022-05-09 14:25:57 +00:00
|
|
|
loadOptions := msgstore.LoadMessageOptions{
|
2022-05-09 13:36:39 +00:00
|
|
|
Network: &net.Network,
|
|
|
|
Entity: targetCM,
|
|
|
|
Limit: backlogLimit,
|
|
|
|
}
|
|
|
|
history, err := dc.user.msgStore.LoadLatestID(ctx, msgID, &loadOptions)
|
2021-02-10 12:48:41 +00:00
|
|
|
if err != nil {
|
2021-04-13 15:49:37 +00:00
|
|
|
dc.logger.Printf("failed to send backlog for %q: %v", target, err)
|
2021-02-10 12:48:41 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-25 10:28:25 +00:00
|
|
|
|
2022-11-14 11:06:58 +00:00
|
|
|
dc.SendBatch("chathistory", []string{target}, nil, func(batchRef string) {
|
2021-06-05 10:38:52 +00:00
|
|
|
for _, msg := range history {
|
|
|
|
if ch != nil && ch.Detached {
|
|
|
|
if net.detachedMessageNeedsRelay(ch, msg) {
|
|
|
|
dc.relayDetachedMessage(net, msg)
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-18 07:20:11 +00:00
|
|
|
msg.Tags["batch"] = batchRef
|
2022-08-05 16:57:38 +00:00
|
|
|
dc.SendMessage(msg)
|
2021-04-13 17:11:05 +00:00
|
|
|
}
|
2020-03-25 10:28:25 +00:00
|
|
|
}
|
2021-06-05 10:38:52 +00:00
|
|
|
})
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 17:11:05 +00:00
|
|
|
func (dc *downstreamConn) relayDetachedMessage(net *network, msg *irc.Message) {
|
|
|
|
if msg.Command != "PRIVMSG" && msg.Command != "NOTICE" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sender := msg.Prefix.Name
|
|
|
|
target, text := msg.Params[0], msg.Params[1]
|
|
|
|
if net.isHighlight(msg) {
|
2022-08-05 17:14:03 +00:00
|
|
|
sendServiceNOTICE(dc, fmt.Sprintf("highlight in %v: <%v> %v", target, sender, text))
|
2021-04-13 17:11:05 +00:00
|
|
|
} else {
|
2022-08-05 17:14:03 +00:00
|
|
|
sendServiceNOTICE(dc, fmt.Sprintf("message in %v: <%v> %v", target, sender, text))
|
2021-04-13 17:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 11:44:59 +00:00
|
|
|
func (dc *downstreamConn) runUntilRegistered() error {
|
2021-11-17 13:54:03 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), downstreamRegisterTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Close the connection with an error if the deadline is exceeded
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
if err := ctx.Err(); err == context.DeadlineExceeded {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "ERROR",
|
|
|
|
Params: []string{"Connection registration timed out"},
|
|
|
|
})
|
|
|
|
dc.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-03-16 11:44:59 +00:00
|
|
|
for !dc.registered {
|
2020-04-03 15:01:25 +00:00
|
|
|
msg, err := dc.ReadMessage()
|
2020-03-16 13:30:49 +00:00
|
|
|
if err != nil {
|
2021-10-29 14:03:04 +00:00
|
|
|
return fmt.Errorf("failed to read IRC command: %w", err)
|
2020-03-16 11:44:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 13:54:03 +00:00
|
|
|
err = dc.handleMessage(ctx, msg)
|
2020-03-16 11:44:59 +00:00
|
|
|
if ircErr, ok := err.(ircError); ok {
|
|
|
|
ircErr.Message.Prefix = dc.srv.prefix()
|
|
|
|
dc.SendMessage(ircErr.Message)
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("failed to handle IRC command %q: %v", msg, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:38:08 +00:00
|
|
|
func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.Message) error {
|
2020-02-06 15:18:19 +00:00
|
|
|
switch msg.Command {
|
2020-03-16 14:11:08 +00:00
|
|
|
case "CAP":
|
|
|
|
var subCmd string
|
|
|
|
if err := parseMessageParams(msg, &subCmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := dc.handleCapCommand(subCmd, msg.Params[1:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-16 13:32:38 +00:00
|
|
|
case "PING":
|
2020-08-26 13:18:57 +00:00
|
|
|
var source, destination string
|
|
|
|
if err := parseMessageParams(msg, &source); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(msg.Params) > 1 {
|
|
|
|
destination = msg.Params[1]
|
|
|
|
}
|
2021-11-15 23:38:04 +00:00
|
|
|
hostname := dc.srv.Config().Hostname
|
|
|
|
if destination != "" && destination != hostname {
|
2020-08-26 13:18:57 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NOSUCHSERVER,
|
2020-08-26 13:28:10 +00:00
|
|
|
Params: []string{dc.nick, destination, "No such server"},
|
2020-08-26 13:18:57 +00:00
|
|
|
}}
|
|
|
|
}
|
2020-03-16 13:32:38 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "PONG",
|
2021-11-15 23:38:04 +00:00
|
|
|
Params: []string{hostname, source},
|
2020-03-16 13:32:38 +00:00
|
|
|
})
|
|
|
|
return nil
|
2020-08-28 15:21:08 +00:00
|
|
|
case "PONG":
|
|
|
|
if len(msg.Params) == 0 {
|
|
|
|
return newNeedMoreParamsError(msg.Command)
|
|
|
|
}
|
|
|
|
token := msg.Params[len(msg.Params)-1]
|
|
|
|
dc.handlePong(token)
|
2020-02-07 11:19:42 +00:00
|
|
|
case "USER":
|
2020-02-06 15:18:19 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_ALREADYREGISTERED,
|
2020-02-17 11:36:42 +00:00
|
|
|
Params: []string{dc.nick, "You may not reregister"},
|
2020-02-06 15:18:19 +00:00
|
|
|
}}
|
2020-02-07 11:19:42 +00:00
|
|
|
case "NICK":
|
2022-04-01 12:55:36 +00:00
|
|
|
var nick string
|
|
|
|
if err := parseMessageParams(msg, &nick); err != nil {
|
2020-03-12 18:17:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-18 08:44:33 +00:00
|
|
|
if nick == "" || strings.ContainsAny(nick, illegalNickChars) {
|
2020-08-20 08:00:58 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_ERRONEUSNICKNAME,
|
2022-04-01 12:55:36 +00:00
|
|
|
Params: []string{dc.nick, nick, "Nickname contains illegal characters"},
|
2020-08-20 08:00:58 +00:00
|
|
|
}}
|
|
|
|
}
|
2023-03-01 12:30:47 +00:00
|
|
|
if xirc.CaseMappingASCII(nick) == serviceNickCM {
|
2020-11-23 16:09:31 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NICKNAMEINUSE,
|
2022-04-01 12:55:36 +00:00
|
|
|
Params: []string{dc.nick, nick, "Nickname reserved for bouncer service"},
|
2020-11-23 16:09:31 +00:00
|
|
|
}}
|
|
|
|
}
|
2020-08-20 08:00:58 +00:00
|
|
|
|
2022-07-08 16:01:05 +00:00
|
|
|
var err error
|
|
|
|
if dc.network != nil {
|
|
|
|
record := dc.network.Network
|
|
|
|
record.Nick = nick
|
|
|
|
err = dc.srv.db.StoreNetwork(ctx, dc.user.ID, &record)
|
|
|
|
} else {
|
2023-03-01 13:16:33 +00:00
|
|
|
err = dc.user.updateUser(ctx, func(record *database.User) error {
|
|
|
|
record.Nick = nick
|
|
|
|
return nil
|
|
|
|
})
|
2022-07-08 16:01:05 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
dc.logger.Printf("failed to update nick: %v", err)
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: xirc.ERR_UNKNOWNERROR,
|
|
|
|
Params: []string{dc.nick, "NICK", "Failed to update nick"},
|
|
|
|
}}
|
2020-03-12 18:17:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 16:01:05 +00:00
|
|
|
if dc.network != nil {
|
2022-08-22 19:59:52 +00:00
|
|
|
dc.network.Network.Nick = nick
|
2022-07-08 16:01:05 +00:00
|
|
|
if uc := dc.upstream(); uc != nil {
|
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
|
|
|
Command: "NICK",
|
|
|
|
Params: []string{nick},
|
|
|
|
})
|
|
|
|
} else {
|
2022-07-08 16:14:10 +00:00
|
|
|
dc.updateNick()
|
2022-07-08 16:01:05 +00:00
|
|
|
}
|
2022-04-01 12:55:36 +00:00
|
|
|
} else {
|
2022-07-08 16:01:05 +00:00
|
|
|
for _, c := range dc.user.downstreamConns {
|
|
|
|
if c.network == nil {
|
|
|
|
c.updateNick()
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 22:37:42 +00:00
|
|
|
}
|
2021-05-25 18:24:45 +00:00
|
|
|
case "SETNAME":
|
|
|
|
var realname string
|
|
|
|
if err := parseMessageParams(msg, &realname); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-30 15:41:40 +00:00
|
|
|
if dc.realname == realname {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
|
|
|
Command: "SETNAME",
|
|
|
|
Params: []string{realname},
|
|
|
|
})
|
|
|
|
return nil
|
2021-06-25 18:33:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 15:41:40 +00:00
|
|
|
var err error
|
|
|
|
if dc.network != nil {
|
|
|
|
// If the client just resets to the default, just wipe the per-network
|
|
|
|
// preference
|
|
|
|
record := dc.network.Network
|
|
|
|
record.Realname = realname
|
|
|
|
if realname == dc.user.Realname {
|
|
|
|
record.Realname = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if uc := dc.upstream(); uc != nil && uc.caps.IsEnabled("setname") {
|
|
|
|
// Upstream will reply with a SETNAME message on success
|
2022-07-08 16:17:24 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
2021-05-25 18:24:45 +00:00
|
|
|
Command: "SETNAME",
|
|
|
|
Params: []string{realname},
|
|
|
|
})
|
|
|
|
|
2022-03-30 15:41:40 +00:00
|
|
|
err = dc.srv.db.StoreNetwork(ctx, dc.user.ID, &record)
|
|
|
|
} else {
|
|
|
|
// This will disconnect then re-connect the upstream connection
|
|
|
|
_, err = dc.user.updateNetwork(ctx, &record)
|
2021-05-25 18:24:45 +00:00
|
|
|
}
|
2022-03-30 15:41:40 +00:00
|
|
|
} else {
|
2023-03-01 13:16:33 +00:00
|
|
|
err = dc.user.updateUser(ctx, func(record *database.User) error {
|
|
|
|
record.Realname = realname
|
|
|
|
return nil
|
|
|
|
})
|
2021-05-25 18:24:45 +00:00
|
|
|
}
|
2022-03-30 15:41:40 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
dc.logger.Printf("failed to update realname: %v", err)
|
2021-05-25 18:24:45 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"SETNAME", "CANNOT_CHANGE_REALNAME", "Failed to update realname"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-03-30 15:41:40 +00:00
|
|
|
if dc.network == nil {
|
|
|
|
for _, c := range dc.user.downstreamConns {
|
|
|
|
if c.network == nil {
|
|
|
|
c.updateRealname()
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 18:24:45 +00:00
|
|
|
}
|
2020-03-25 10:52:24 +00:00
|
|
|
case "JOIN":
|
2022-08-08 09:30:10 +00:00
|
|
|
uc, err := dc.upstreamForCommand(msg.Command)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:52:24 +00:00
|
|
|
var namesStr string
|
|
|
|
if err := parseMessageParams(msg, &namesStr); err != nil {
|
2020-02-07 12:36:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:52:24 +00:00
|
|
|
var keys []string
|
|
|
|
if len(msg.Params) > 1 {
|
|
|
|
keys = strings.Split(msg.Params[1], ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, name := range strings.Split(namesStr, ",") {
|
|
|
|
var key string
|
|
|
|
if len(keys) > i {
|
|
|
|
key = keys[i]
|
|
|
|
}
|
|
|
|
|
2022-12-08 14:25:39 +00:00
|
|
|
if name == "" || strings.ContainsAny(name, illegalChanChars) {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
2022-12-08 14:27:02 +00:00
|
|
|
Command: irc.ERR_BADCHANMASK,
|
2022-12-08 14:25:39 +00:00
|
|
|
Params: []string{name, "Invalid channel name"},
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
2022-08-08 09:30:10 +00:00
|
|
|
if !uc.isChannel(name) {
|
2021-05-26 09:21:37 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_NOSUCHCHANNEL,
|
|
|
|
Params: []string{name, "Not a channel name"},
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:23:46 +00:00
|
|
|
// Most servers ignore duplicate JOIN messages. We ignore them here
|
|
|
|
// because some clients automatically send JOIN messages in bulk
|
|
|
|
// when reconnecting to the bouncer. We don't want to flood the
|
|
|
|
// upstream connection with these.
|
2022-08-08 09:30:10 +00:00
|
|
|
if !uc.channels.Has(name) {
|
|
|
|
params := []string{name}
|
2021-12-08 17:23:46 +00:00
|
|
|
if key != "" {
|
|
|
|
params = append(params, key)
|
|
|
|
}
|
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
|
|
|
Command: "JOIN",
|
|
|
|
Params: params,
|
|
|
|
})
|
2020-03-25 10:52:24 +00:00
|
|
|
}
|
2020-03-25 10:32:44 +00:00
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
ch := uc.network.channels.Get(name)
|
Implement casemapping
TL;DR: supports for casemapping, now logs are saved in
casemapped/canonical/tolower form
(eg. in the #channel directory instead of #Channel... or something)
== What is casemapping? ==
see <https://modern.ircdocs.horse/#casemapping-parameter>
== Casemapping and multi-upstream ==
Since each upstream does not necessarily use the same casemapping, and
since casemappings cannot coexist [0],
1. soju must also update the database accordingly to upstreams'
casemapping, otherwise it will end up inconsistent,
2. soju must "normalize" entity names and expose only one casemapping
that is a subset of all supported casemappings (here, ascii).
[0] On some upstreams, "emersion[m]" and "emersion{m}" refer to the same
user (upstreams that advertise rfc1459 for example), while on others
(upstreams that advertise ascii) they don't.
Once upstream's casemapping is known (default to rfc1459), entity names
in map keys are made into casemapped form, for upstreamConn,
upstreamChannel and network.
downstreamConn advertises "CASEMAPPING=ascii", and always casemap map
keys with ascii.
Some functions require the caller to casemap their argument (to avoid
needless calls to casemapping functions).
== Message forwarding and casemapping ==
downstream message handling (joins and parts basically):
When relaying entity names from downstreams to upstreams, soju uses the
upstream casemapping, in order to not get in the way of the user. This
does not brings any issue, as long as soju replies with the ascii
casemapping in mind (solves point 1.).
marshalEntity/marshalUserPrefix:
When relaying entity names from upstreams with non-ascii casemappings,
soju *partially* casemap them: it only change the case of characters
which are not ascii letters. ASCII case is thus kept intact, while
special symbols like []{} are the same every time soju sends them to
downstreams (solves point 2.).
== Casemapping changes ==
Casemapping changes are not fully supported by this patch and will
result in loss of history. This is a limitation of the protocol and
should be solved by the RENAME spec.
2021-03-16 09:00:34 +00:00
|
|
|
if ch != nil {
|
2020-05-01 15:39:53 +00:00
|
|
|
// Don't clear the channel key if there's one set
|
|
|
|
// TODO: add a way to unset the channel key
|
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 key != "" {
|
|
|
|
ch.Key = key
|
|
|
|
}
|
2021-01-29 15:57:38 +00:00
|
|
|
uc.network.attach(ctx, 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
|
|
|
} else {
|
2022-05-09 10:34:43 +00:00
|
|
|
ch = &database.Channel{
|
2022-08-08 09:30:10 +00:00
|
|
|
Name: name,
|
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
|
|
|
Key: key,
|
|
|
|
}
|
2023-03-01 12:15:38 +00:00
|
|
|
uc.network.channels.Set(ch.Name, ch)
|
2020-05-01 15:39:53 +00:00
|
|
|
}
|
2021-11-08 17:11:24 +00:00
|
|
|
if err := dc.srv.db.StoreChannel(ctx, uc.network.ID, ch); err != nil {
|
2022-08-08 09:30:10 +00:00
|
|
|
dc.logger.Printf("failed to create or update channel %q: %v", name, err)
|
2020-03-25 10:52:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case "PART":
|
2022-08-08 09:30:10 +00:00
|
|
|
uc, err := dc.upstreamForCommand(msg.Command)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:52:24 +00:00
|
|
|
var namesStr string
|
|
|
|
if err := parseMessageParams(msg, &namesStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var reason string
|
|
|
|
if len(msg.Params) > 1 {
|
|
|
|
reason = msg.Params[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range strings.Split(namesStr, ",") {
|
2020-04-28 13:27:41 +00:00
|
|
|
if strings.EqualFold(reason, "detach") {
|
2022-08-08 09:30:10 +00:00
|
|
|
ch := uc.network.channels.Get(name)
|
Implement casemapping
TL;DR: supports for casemapping, now logs are saved in
casemapped/canonical/tolower form
(eg. in the #channel directory instead of #Channel... or something)
== What is casemapping? ==
see <https://modern.ircdocs.horse/#casemapping-parameter>
== Casemapping and multi-upstream ==
Since each upstream does not necessarily use the same casemapping, and
since casemappings cannot coexist [0],
1. soju must also update the database accordingly to upstreams'
casemapping, otherwise it will end up inconsistent,
2. soju must "normalize" entity names and expose only one casemapping
that is a subset of all supported casemappings (here, ascii).
[0] On some upstreams, "emersion[m]" and "emersion{m}" refer to the same
user (upstreams that advertise rfc1459 for example), while on others
(upstreams that advertise ascii) they don't.
Once upstream's casemapping is known (default to rfc1459), entity names
in map keys are made into casemapped form, for upstreamConn,
upstreamChannel and network.
downstreamConn advertises "CASEMAPPING=ascii", and always casemap map
keys with ascii.
Some functions require the caller to casemap their argument (to avoid
needless calls to casemapping functions).
== Message forwarding and casemapping ==
downstream message handling (joins and parts basically):
When relaying entity names from downstreams to upstreams, soju uses the
upstream casemapping, in order to not get in the way of the user. This
does not brings any issue, as long as soju replies with the ascii
casemapping in mind (solves point 1.).
marshalEntity/marshalUserPrefix:
When relaying entity names from upstreams with non-ascii casemappings,
soju *partially* casemap them: it only change the case of characters
which are not ascii letters. ASCII case is thus kept intact, while
special symbols like []{} are the same every time soju sends them to
downstreams (solves point 2.).
== Casemapping changes ==
Casemapping changes are not fully supported by this patch and will
result in loss of history. This is a limitation of the protocol and
should be solved by the RENAME spec.
2021-03-16 09:00:34 +00:00
|
|
|
if ch != nil {
|
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.network.detach(ch)
|
|
|
|
} else {
|
2022-05-09 10:34:43 +00:00
|
|
|
ch = &database.Channel{
|
2022-08-08 09:30:10 +00:00
|
|
|
Name: name,
|
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
|
|
|
Detached: true,
|
|
|
|
}
|
2023-03-01 12:15:38 +00:00
|
|
|
uc.network.channels.Set(ch.Name, 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
|
|
|
}
|
2021-11-08 17:11:24 +00:00
|
|
|
if err := dc.srv.db.StoreChannel(ctx, uc.network.ID, ch); err != nil {
|
2022-08-08 09:30:10 +00:00
|
|
|
dc.logger.Printf("failed to create or update channel %q: %v", name, err)
|
2020-04-28 13:27:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-08 09:30:10 +00:00
|
|
|
params := []string{name}
|
2020-04-28 13:27:41 +00:00
|
|
|
if reason != "" {
|
|
|
|
params = append(params, reason)
|
|
|
|
}
|
2021-12-08 17:03:40 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
2020-04-28 13:27:41 +00:00
|
|
|
Command: "PART",
|
|
|
|
Params: params,
|
|
|
|
})
|
2020-03-25 10:52:24 +00:00
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
if err := uc.network.deleteChannel(ctx, name); err != nil {
|
|
|
|
dc.logger.Printf("failed to delete channel %q: %v", name, err)
|
2020-04-28 13:27:41 +00:00
|
|
|
}
|
2022-07-08 14:55:29 +00:00
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
uc.network.pushTargets.Del(name)
|
2020-03-12 17:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-25 22:46:36 +00:00
|
|
|
case "KICK":
|
2022-08-08 09:30:10 +00:00
|
|
|
uc, err := dc.upstreamForCommand(msg.Command)
|
|
|
|
if err != nil {
|
2020-03-25 22:46:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, msg)
|
2020-02-07 12:08:27 +00:00
|
|
|
case "MODE":
|
|
|
|
var name string
|
|
|
|
if err := parseMessageParams(msg, &name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var modeStr string
|
|
|
|
if len(msg.Params) > 1 {
|
|
|
|
modeStr = msg.Params[1]
|
|
|
|
}
|
|
|
|
|
2023-03-01 12:30:47 +00:00
|
|
|
if xirc.CaseMappingASCII(name) == dc.nickCM {
|
2020-02-07 12:08:27 +00:00
|
|
|
if modeStr != "" {
|
2021-06-10 09:24:10 +00:00
|
|
|
if uc := dc.upstream(); uc != nil {
|
2021-12-08 17:03:40 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
2020-02-19 17:25:19 +00:00
|
|
|
Command: "MODE",
|
|
|
|
Params: []string{uc.nick, modeStr},
|
|
|
|
})
|
2021-06-10 09:24:10 +00:00
|
|
|
} else {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_UMODEUNKNOWNFLAG,
|
2022-08-08 09:30:10 +00:00
|
|
|
Params: []string{dc.nick, "Cannot change user mode on bouncer connection"},
|
2021-06-10 09:24:10 +00:00
|
|
|
})
|
|
|
|
}
|
2020-02-07 12:08:27 +00:00
|
|
|
} else {
|
2021-06-09 19:58:27 +00:00
|
|
|
var userMode string
|
|
|
|
if uc := dc.upstream(); uc != nil {
|
|
|
|
userMode = string(uc.modes)
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:36:42 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
2020-02-07 12:08:27 +00:00
|
|
|
Command: irc.RPL_UMODEIS,
|
2021-11-03 21:02:19 +00:00
|
|
|
Params: []string{dc.nick, "+" + userMode},
|
2020-02-17 11:27:48 +00:00
|
|
|
})
|
2020-02-07 12:08:27 +00:00
|
|
|
}
|
2020-03-20 23:48:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
uc, err := dc.upstreamForCommand(msg.Command)
|
2020-03-20 23:48:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
if !uc.isChannel(name) {
|
2020-03-20 23:48:19 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_USERSDONTMATCH,
|
|
|
|
Params: []string{dc.nick, "Cannot change mode for other users"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
if modeStr != "" {
|
2022-08-08 09:30:10 +00:00
|
|
|
params := []string{name, modeStr}
|
2020-03-20 23:48:19 +00:00
|
|
|
params = append(params, msg.Params[2:]...)
|
2021-12-08 17:03:40 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
2020-03-20 23:48:19 +00:00
|
|
|
Command: "MODE",
|
|
|
|
Params: params,
|
|
|
|
})
|
|
|
|
} else {
|
2022-08-08 09:30:10 +00:00
|
|
|
ch := uc.channels.Get(name)
|
Implement casemapping
TL;DR: supports for casemapping, now logs are saved in
casemapped/canonical/tolower form
(eg. in the #channel directory instead of #Channel... or something)
== What is casemapping? ==
see <https://modern.ircdocs.horse/#casemapping-parameter>
== Casemapping and multi-upstream ==
Since each upstream does not necessarily use the same casemapping, and
since casemappings cannot coexist [0],
1. soju must also update the database accordingly to upstreams'
casemapping, otherwise it will end up inconsistent,
2. soju must "normalize" entity names and expose only one casemapping
that is a subset of all supported casemappings (here, ascii).
[0] On some upstreams, "emersion[m]" and "emersion{m}" refer to the same
user (upstreams that advertise rfc1459 for example), while on others
(upstreams that advertise ascii) they don't.
Once upstream's casemapping is known (default to rfc1459), entity names
in map keys are made into casemapped form, for upstreamConn,
upstreamChannel and network.
downstreamConn advertises "CASEMAPPING=ascii", and always casemap map
keys with ascii.
Some functions require the caller to casemap their argument (to avoid
needless calls to casemapping functions).
== Message forwarding and casemapping ==
downstream message handling (joins and parts basically):
When relaying entity names from downstreams to upstreams, soju uses the
upstream casemapping, in order to not get in the way of the user. This
does not brings any issue, as long as soju replies with the ascii
casemapping in mind (solves point 1.).
marshalEntity/marshalUserPrefix:
When relaying entity names from upstreams with non-ascii casemappings,
soju *partially* casemap them: it only change the case of characters
which are not ascii letters. ASCII case is thus kept intact, while
special symbols like []{} are the same every time soju sends them to
downstreams (solves point 2.).
== Casemapping changes ==
Casemapping changes are not fully supported by this patch and will
result in loss of history. This is a limitation of the protocol and
should be solved by the RENAME spec.
2021-03-16 09:00:34 +00:00
|
|
|
if ch == nil {
|
2020-03-20 23:48:19 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NOSUCHCHANNEL,
|
|
|
|
Params: []string{dc.nick, name, "No such channel"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ch.modes == nil {
|
|
|
|
// we haven't received the initial RPL_CHANNELMODEIS yet
|
|
|
|
// ignore the request, we will broadcast the modes later when we receive RPL_CHANNELMODEIS
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
modeStr, modeParams := ch.modes.Format()
|
|
|
|
params := []string{dc.nick, name, modeStr}
|
|
|
|
params = append(params, modeParams...)
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_CHANNELMODEIS,
|
|
|
|
Params: params,
|
|
|
|
})
|
2020-03-26 04:51:47 +00:00
|
|
|
if ch.creationTime != "" {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
2022-05-09 15:18:51 +00:00
|
|
|
Command: xirc.RPL_CREATIONTIME,
|
2020-03-26 04:51:47 +00:00
|
|
|
Params: []string{dc.nick, name, ch.creationTime},
|
|
|
|
})
|
|
|
|
}
|
2020-02-07 12:08:27 +00:00
|
|
|
}
|
2020-03-25 23:19:45 +00:00
|
|
|
case "TOPIC":
|
2022-08-08 09:30:10 +00:00
|
|
|
var name string
|
|
|
|
if err := parseMessageParams(msg, &name); err != nil {
|
2020-03-25 23:19:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
uc, err := dc.upstreamForCommand(msg.Command)
|
2020-03-25 23:19:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(msg.Params) > 1 { // setting topic
|
|
|
|
topic := msg.Params[1]
|
2021-12-08 17:03:40 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
2020-03-25 23:19:45 +00:00
|
|
|
Command: "TOPIC",
|
2022-08-08 09:30:10 +00:00
|
|
|
Params: []string{name, topic},
|
2020-03-25 23:19:45 +00:00
|
|
|
})
|
|
|
|
} else { // getting topic
|
2022-08-08 09:30:10 +00:00
|
|
|
ch := uc.channels.Get(name)
|
Implement casemapping
TL;DR: supports for casemapping, now logs are saved in
casemapped/canonical/tolower form
(eg. in the #channel directory instead of #Channel... or something)
== What is casemapping? ==
see <https://modern.ircdocs.horse/#casemapping-parameter>
== Casemapping and multi-upstream ==
Since each upstream does not necessarily use the same casemapping, and
since casemappings cannot coexist [0],
1. soju must also update the database accordingly to upstreams'
casemapping, otherwise it will end up inconsistent,
2. soju must "normalize" entity names and expose only one casemapping
that is a subset of all supported casemappings (here, ascii).
[0] On some upstreams, "emersion[m]" and "emersion{m}" refer to the same
user (upstreams that advertise rfc1459 for example), while on others
(upstreams that advertise ascii) they don't.
Once upstream's casemapping is known (default to rfc1459), entity names
in map keys are made into casemapped form, for upstreamConn,
upstreamChannel and network.
downstreamConn advertises "CASEMAPPING=ascii", and always casemap map
keys with ascii.
Some functions require the caller to casemap their argument (to avoid
needless calls to casemapping functions).
== Message forwarding and casemapping ==
downstream message handling (joins and parts basically):
When relaying entity names from downstreams to upstreams, soju uses the
upstream casemapping, in order to not get in the way of the user. This
does not brings any issue, as long as soju replies with the ascii
casemapping in mind (solves point 1.).
marshalEntity/marshalUserPrefix:
When relaying entity names from upstreams with non-ascii casemappings,
soju *partially* casemap them: it only change the case of characters
which are not ascii letters. ASCII case is thus kept intact, while
special symbols like []{} are the same every time soju sends them to
downstreams (solves point 2.).
== Casemapping changes ==
Casemapping changes are not fully supported by this patch and will
result in loss of history. This is a limitation of the protocol and
should be solved by the RENAME spec.
2021-03-16 09:00:34 +00:00
|
|
|
if ch == nil {
|
2020-03-25 23:19:45 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NOSUCHCHANNEL,
|
2022-08-08 09:30:10 +00:00
|
|
|
Params: []string{dc.nick, name, "No such channel"},
|
2020-03-25 23:19:45 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
sendTopic(dc, ch)
|
|
|
|
}
|
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 "LIST":
|
2022-08-08 09:30:10 +00:00
|
|
|
uc, err := dc.upstreamForCommand(msg.Command)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-11-09 20:32:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 21:09:17 +00:00
|
|
|
uc.enqueueCommand(dc, msg)
|
2020-03-21 00:24:29 +00:00
|
|
|
case "NAMES":
|
2022-08-08 09:30:10 +00:00
|
|
|
uc, err := dc.upstreamForCommand(msg.Command)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-21 00:24:29 +00:00
|
|
|
if len(msg.Params) == 0 {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFNAMES,
|
|
|
|
Params: []string{dc.nick, "*", "End of /NAMES list"},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
channels := strings.Split(msg.Params[0], ",")
|
2022-08-08 09:30:10 +00:00
|
|
|
for _, name := range channels {
|
|
|
|
ch := uc.channels.Get(name)
|
Implement casemapping
TL;DR: supports for casemapping, now logs are saved in
casemapped/canonical/tolower form
(eg. in the #channel directory instead of #Channel... or something)
== What is casemapping? ==
see <https://modern.ircdocs.horse/#casemapping-parameter>
== Casemapping and multi-upstream ==
Since each upstream does not necessarily use the same casemapping, and
since casemappings cannot coexist [0],
1. soju must also update the database accordingly to upstreams'
casemapping, otherwise it will end up inconsistent,
2. soju must "normalize" entity names and expose only one casemapping
that is a subset of all supported casemappings (here, ascii).
[0] On some upstreams, "emersion[m]" and "emersion{m}" refer to the same
user (upstreams that advertise rfc1459 for example), while on others
(upstreams that advertise ascii) they don't.
Once upstream's casemapping is known (default to rfc1459), entity names
in map keys are made into casemapped form, for upstreamConn,
upstreamChannel and network.
downstreamConn advertises "CASEMAPPING=ascii", and always casemap map
keys with ascii.
Some functions require the caller to casemap their argument (to avoid
needless calls to casemapping functions).
== Message forwarding and casemapping ==
downstream message handling (joins and parts basically):
When relaying entity names from downstreams to upstreams, soju uses the
upstream casemapping, in order to not get in the way of the user. This
does not brings any issue, as long as soju replies with the ascii
casemapping in mind (solves point 1.).
marshalEntity/marshalUserPrefix:
When relaying entity names from upstreams with non-ascii casemappings,
soju *partially* casemap them: it only change the case of characters
which are not ascii letters. ASCII case is thus kept intact, while
special symbols like []{} are the same every time soju sends them to
downstreams (solves point 2.).
== Casemapping changes ==
Casemapping changes are not fully supported by this patch and will
result in loss of history. This is a limitation of the protocol and
should be solved by the RENAME spec.
2021-03-16 09:00:34 +00:00
|
|
|
if ch != nil {
|
2020-03-21 00:24:29 +00:00
|
|
|
sendNames(dc, ch)
|
|
|
|
} else {
|
|
|
|
// NAMES on a channel we have not joined, ask upstream
|
2021-12-08 17:03:40 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
2020-03-21 00:24:29 +00:00
|
|
|
Command: "NAMES",
|
2022-08-08 09:30:10 +00:00
|
|
|
Params: []string{name},
|
2020-03-21 00:24:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-03-19 23:23:19 +00:00
|
|
|
case "WHO":
|
|
|
|
if len(msg.Params) == 0 {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFWHO,
|
2020-03-21 00:24:29 +00:00
|
|
|
Params: []string{dc.nick, "*", "End of /WHO list"},
|
2020-03-19 23:23:19 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-02 17:15:45 +00:00
|
|
|
// Clients will use the first mask to match RPL_ENDOFWHO
|
|
|
|
endOfWhoToken := msg.Params[0]
|
|
|
|
|
|
|
|
// TODO: add support for WHOX mask2
|
|
|
|
mask := msg.Params[0]
|
|
|
|
var options string
|
|
|
|
if len(msg.Params) > 1 {
|
|
|
|
options = msg.Params[1]
|
|
|
|
}
|
|
|
|
|
Add WHO cache
This adds a new field to upstreams, members, which is a casemapped map
of upstream users known to the soju. The upstream users known to soju
are: self, any monitored user, and any user with whom we share a
channel.
The information stored for each upstream user corresponds to the info
that can be returned by a WHO/WHOX command.
We build the upstream user information both incrementally, capturing
information contained in JOIN and AWAY messages; and with the bulk user
information contained in WHO replies we receive.
This lets us build a user cache that can then be used to return
synthetic WHO responses to later WHO requests by downstreams.
This is useful because some networks (eg Libera) heavily throttle WHO
commands, and without this cache, any downstream connecting would send 1
WHO command per channel, so possibly more than a dozen WHO commands,
which soju then forwarded to the upstream as WHO commands.
With this cache most WHO commands can be cached and avoid sending
WHO commands to the upstream.
In order to cache the "flags" field, we synthetize the field from user
info we get from incremental messages: away status (H/G) and bot status
(B). This could result in incorrect values for proprietary user fields.
Support for the server-operator status (*) is also not supported.
Of note is that it is difficult to obtain a user "connected server"
field incrementally, so clients that want to maximize their WHO cache
hit ratio can use WHOX to only request fields they need, and in
particular not include the server field flag.
Co-authored-by: delthas <delthas@dille.cc>
2022-12-01 14:47:58 +00:00
|
|
|
fields, whoxToken := xirc.ParseWHOXOptions(options)
|
2020-03-19 23:23:19 +00:00
|
|
|
|
2021-11-02 17:15:45 +00:00
|
|
|
// TODO: support mixed bouncer/upstream WHO queries
|
2023-03-01 12:30:47 +00:00
|
|
|
maskCM := xirc.CaseMappingASCII(mask)
|
2021-11-02 17:15:45 +00:00
|
|
|
if dc.network == nil && maskCM == dc.nickCM {
|
2020-03-21 23:46:56 +00:00
|
|
|
// TODO: support AWAY (H/G) in self WHO reply
|
2021-11-01 17:32:01 +00:00
|
|
|
flags := "H"
|
|
|
|
if dc.user.Admin {
|
2021-11-01 17:36:21 +00:00
|
|
|
flags += "*"
|
2021-11-01 17:32:01 +00:00
|
|
|
}
|
2022-05-29 16:33:29 +00:00
|
|
|
info := xirc.WHOXInfo{
|
2021-11-02 17:15:45 +00:00
|
|
|
Token: whoxToken,
|
|
|
|
Username: dc.user.Username,
|
|
|
|
Hostname: dc.hostname,
|
2021-11-15 23:38:04 +00:00
|
|
|
Server: dc.srv.Config().Hostname,
|
2021-11-02 17:15:45 +00:00
|
|
|
Nickname: dc.nick,
|
|
|
|
Flags: flags,
|
2021-11-02 17:32:39 +00:00
|
|
|
Account: dc.user.Username,
|
2021-11-02 17:15:45 +00:00
|
|
|
Realname: dc.realname,
|
|
|
|
}
|
2022-05-29 16:33:29 +00:00
|
|
|
dc.SendMessage(xirc.GenerateWHOXReply(dc.srv.prefix(), dc.nick, fields, &info))
|
2020-03-21 23:46:56 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFWHO,
|
2021-11-02 17:15:45 +00:00
|
|
|
Params: []string{dc.nick, endOfWhoToken, "End of /WHO list"},
|
2020-03-21 23:46:56 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-02 17:15:45 +00:00
|
|
|
if maskCM == serviceNickCM {
|
2021-10-06 10:02:49 +00:00
|
|
|
flags := "H*"
|
|
|
|
if dc.network == nil {
|
|
|
|
flags += "B"
|
|
|
|
} else if uc := dc.upstream(); uc != nil {
|
|
|
|
if v := uc.isupport["BOT"]; v != nil && len(*v) == 1 {
|
|
|
|
flags += *v
|
|
|
|
}
|
|
|
|
}
|
2022-05-29 16:33:29 +00:00
|
|
|
info := xirc.WHOXInfo{
|
2021-11-02 17:15:45 +00:00
|
|
|
Token: whoxToken,
|
|
|
|
Username: servicePrefix.User,
|
|
|
|
Hostname: servicePrefix.Host,
|
2021-11-15 23:38:04 +00:00
|
|
|
Server: dc.srv.Config().Hostname,
|
2021-11-02 17:15:45 +00:00
|
|
|
Nickname: serviceNick,
|
2021-10-06 10:02:49 +00:00
|
|
|
Flags: flags,
|
2021-11-02 17:32:39 +00:00
|
|
|
Account: serviceNick,
|
2021-11-02 17:15:45 +00:00
|
|
|
Realname: serviceRealname,
|
|
|
|
}
|
2022-05-29 16:33:29 +00:00
|
|
|
dc.SendMessage(xirc.GenerateWHOXReply(dc.srv.prefix(), dc.nick, fields, &info))
|
2020-06-29 16:09:48 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFWHO,
|
2021-11-02 17:15:45 +00:00
|
|
|
Params: []string{dc.nick, endOfWhoToken, "End of /WHO list"},
|
2020-06-29 16:09:48 +00:00
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-08 09:30:10 +00:00
|
|
|
if dc.network == nil {
|
2022-03-23 18:15:52 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFWHO,
|
|
|
|
Params: []string{dc.nick, endOfWhoToken, "End of /WHO list"},
|
|
|
|
})
|
|
|
|
return nil
|
2020-03-19 23:23:19 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
uc, err := dc.upstreamForCommand(msg.Command)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-03-19 23:23:19 +00:00
|
|
|
}
|
|
|
|
|
Add WHO cache
This adds a new field to upstreams, members, which is a casemapped map
of upstream users known to the soju. The upstream users known to soju
are: self, any monitored user, and any user with whom we share a
channel.
The information stored for each upstream user corresponds to the info
that can be returned by a WHO/WHOX command.
We build the upstream user information both incrementally, capturing
information contained in JOIN and AWAY messages; and with the bulk user
information contained in WHO replies we receive.
This lets us build a user cache that can then be used to return
synthetic WHO responses to later WHO requests by downstreams.
This is useful because some networks (eg Libera) heavily throttle WHO
commands, and without this cache, any downstream connecting would send 1
WHO command per channel, so possibly more than a dozen WHO commands,
which soju then forwarded to the upstream as WHO commands.
With this cache most WHO commands can be cached and avoid sending
WHO commands to the upstream.
In order to cache the "flags" field, we synthetize the field from user
info we get from incremental messages: away status (H/G) and bot status
(B). This could result in incorrect values for proprietary user fields.
Support for the server-operator status (*) is also not supported.
Of note is that it is difficult to obtain a user "connected server"
field incrementally, so clients that want to maximize their WHO cache
hit ratio can use WHOX to only request fields they need, and in
particular not include the server field flag.
Co-authored-by: delthas <delthas@dille.cc>
2022-12-01 14:47:58 +00:00
|
|
|
// Check if we have the reply cached
|
|
|
|
if l, ok := uc.getCachedWHO(mask, fields); ok {
|
|
|
|
for _, uu := range l {
|
|
|
|
info := xirc.WHOXInfo{
|
|
|
|
Token: whoxToken,
|
|
|
|
Username: uu.Username,
|
|
|
|
Hostname: uu.Hostname,
|
|
|
|
Server: uu.Server,
|
|
|
|
Nickname: uu.Nickname,
|
|
|
|
Flags: uu.Flags,
|
|
|
|
Account: uu.Account,
|
|
|
|
Realname: uu.Realname,
|
|
|
|
}
|
|
|
|
dc.SendMessage(xirc.GenerateWHOXReply(dc.srv.prefix(), dc.nick, fields, &info))
|
|
|
|
}
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFWHO,
|
|
|
|
Params: []string{dc.nick, endOfWhoToken, "End of /WHO list"},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
uc.enqueueCommand(dc, msg)
|
2020-03-20 01:15:23 +00:00
|
|
|
case "WHOIS":
|
|
|
|
if len(msg.Params) == 0 {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NONICKNAMEGIVEN,
|
|
|
|
Params: []string{dc.nick, "No nickname given"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
var mask string
|
2020-03-20 01:15:23 +00:00
|
|
|
if len(msg.Params) == 1 {
|
|
|
|
mask = msg.Params[0]
|
|
|
|
} else {
|
|
|
|
mask = msg.Params[1]
|
|
|
|
}
|
|
|
|
// TODO: support multiple WHOIS users
|
|
|
|
if i := strings.IndexByte(mask, ','); i >= 0 {
|
|
|
|
mask = mask[:i]
|
|
|
|
}
|
|
|
|
|
2023-03-01 12:30:47 +00:00
|
|
|
if dc.network == nil && xirc.CaseMappingASCII(mask) == dc.nickCM {
|
2020-03-21 23:46:56 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_WHOISUSER,
|
2020-03-28 16:28:28 +00:00
|
|
|
Params: []string{dc.nick, dc.nick, dc.user.Username, dc.hostname, "*", dc.realname},
|
2020-03-21 23:46:56 +00:00
|
|
|
})
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_WHOISSERVER,
|
2021-11-15 23:38:04 +00:00
|
|
|
Params: []string{dc.nick, dc.nick, dc.srv.Config().Hostname, "soju"},
|
2020-03-21 23:46:56 +00:00
|
|
|
})
|
2021-11-01 17:32:01 +00:00
|
|
|
if dc.user.Admin {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_WHOISOPERATOR,
|
|
|
|
Params: []string{dc.nick, dc.nick, "is a bouncer administrator"},
|
|
|
|
})
|
|
|
|
}
|
2021-11-02 17:32:39 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
2022-05-09 15:18:51 +00:00
|
|
|
Command: xirc.RPL_WHOISACCOUNT,
|
2021-11-02 17:32:39 +00:00
|
|
|
Params: []string{dc.nick, dc.nick, dc.user.Username, "is logged in as"},
|
|
|
|
})
|
2020-03-21 23:46:56 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFWHOIS,
|
|
|
|
Params: []string{dc.nick, dc.nick, "End of /WHOIS list"},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-01 12:30:47 +00:00
|
|
|
if xirc.CaseMappingASCII(mask) == serviceNickCM {
|
2021-10-06 09:50:12 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_WHOISUSER,
|
|
|
|
Params: []string{dc.nick, serviceNick, servicePrefix.User, servicePrefix.Host, "*", serviceRealname},
|
|
|
|
})
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_WHOISSERVER,
|
2021-11-15 23:38:04 +00:00
|
|
|
Params: []string{dc.nick, serviceNick, dc.srv.Config().Hostname, "soju"},
|
2021-10-06 09:50:12 +00:00
|
|
|
})
|
2021-11-01 17:28:19 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_WHOISOPERATOR,
|
|
|
|
Params: []string{dc.nick, serviceNick, "is the bouncer service"},
|
|
|
|
})
|
2021-11-02 17:32:39 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
2022-05-09 15:18:51 +00:00
|
|
|
Command: xirc.RPL_WHOISACCOUNT,
|
2021-11-02 17:32:39 +00:00
|
|
|
Params: []string{dc.nick, serviceNick, serviceNick, "is logged in as"},
|
|
|
|
})
|
2021-10-06 10:02:49 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
2022-05-09 15:18:51 +00:00
|
|
|
Command: xirc.RPL_WHOISBOT,
|
2021-10-06 10:02:49 +00:00
|
|
|
Params: []string{dc.nick, serviceNick, "is a bot"},
|
|
|
|
})
|
2021-10-06 09:50:12 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFWHOIS,
|
|
|
|
Params: []string{dc.nick, serviceNick, "End of /WHOIS list"},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-21 23:46:56 +00:00
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
uc, err := dc.upstreamForCommand(msg.Command)
|
2020-03-20 01:15:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
uc.enqueueCommand(dc, msg)
|
2022-04-05 08:06:31 +00:00
|
|
|
case "PRIVMSG", "NOTICE", "TAGMSG":
|
2020-02-17 14:56:18 +00:00
|
|
|
var targetsStr, text string
|
2022-04-05 08:06:31 +00:00
|
|
|
if msg.Command != "TAGMSG" {
|
|
|
|
if err := parseMessageParams(msg, &targetsStr, &text); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := parseMessageParams(msg, &targetsStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-17 14:56:18 +00:00
|
|
|
}
|
2022-04-05 08:06:31 +00:00
|
|
|
|
2020-05-21 05:04:34 +00:00
|
|
|
tags := copyClientTags(msg.Tags)
|
2020-02-17 14:56:18 +00:00
|
|
|
|
|
|
|
for _, name := range strings.Split(targetsStr, ",") {
|
2022-04-05 08:06:31 +00:00
|
|
|
params := []string{name}
|
|
|
|
if msg.Command != "TAGMSG" {
|
|
|
|
params = append(params, text)
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:38:04 +00:00
|
|
|
if name == "$"+dc.srv.Config().Hostname || (name == "$*" && dc.network == nil) {
|
2021-06-23 17:21:18 +00:00
|
|
|
// "$" means a server mask follows. If it's the bouncer's
|
|
|
|
// hostname, broadcast the message to all bouncer users.
|
|
|
|
if !dc.user.Admin {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_BADMASK,
|
|
|
|
Params: []string{dc.nick, name, "Permission denied to broadcast message to all bouncer users"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.logger.Printf("broadcasting bouncer-wide %v: %v", msg.Command, text)
|
|
|
|
|
|
|
|
broadcastTags := tags.Copy()
|
2022-10-18 12:57:33 +00:00
|
|
|
broadcastTags["time"] = dc.user.FormatServerTime(time.Now())
|
2021-06-23 17:21:18 +00:00
|
|
|
broadcastMsg := &irc.Message{
|
|
|
|
Tags: broadcastTags,
|
|
|
|
Prefix: servicePrefix,
|
|
|
|
Command: msg.Command,
|
2022-04-05 08:06:31 +00:00
|
|
|
Params: params,
|
2021-06-23 17:21:18 +00:00
|
|
|
}
|
|
|
|
dc.srv.forEachUser(func(u *user) {
|
|
|
|
u.events <- eventBroadcast{broadcastMsg}
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-01 12:30:47 +00:00
|
|
|
if dc.network == nil && xirc.CaseMappingASCII(name) == dc.nickCM {
|
2021-10-09 10:46:02 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Tags: msg.Tags.Copy(),
|
|
|
|
Prefix: dc.prefix(),
|
|
|
|
Command: msg.Command,
|
2022-04-05 08:06:31 +00:00
|
|
|
Params: params,
|
2021-10-09 10:46:02 +00:00
|
|
|
})
|
2021-05-24 10:45:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-01 12:30:47 +00:00
|
|
|
if xirc.CaseMappingASCII(name) == serviceNickCM {
|
2022-03-14 18:15:35 +00:00
|
|
|
if dc.caps.IsEnabled("echo-message") {
|
2020-11-23 16:14:42 +00:00
|
|
|
echoTags := tags.Copy()
|
2022-10-18 12:57:33 +00:00
|
|
|
echoTags["time"] = dc.user.FormatServerTime(time.Now())
|
2020-11-23 16:14:42 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Tags: echoTags,
|
|
|
|
Prefix: dc.prefix(),
|
2021-06-23 16:17:30 +00:00
|
|
|
Command: msg.Command,
|
2022-04-05 08:06:31 +00:00
|
|
|
Params: params,
|
2020-11-23 16:14:42 +00:00
|
|
|
})
|
|
|
|
}
|
2022-04-05 08:06:31 +00:00
|
|
|
if msg.Command == "PRIVMSG" {
|
2023-01-20 14:46:42 +00:00
|
|
|
if err := handleServicePRIVMSG(&serviceContext{
|
2023-01-17 12:26:05 +00:00
|
|
|
Context: ctx,
|
|
|
|
nick: dc.nick,
|
|
|
|
network: dc.network,
|
|
|
|
user: dc.user,
|
2023-01-19 17:33:22 +00:00
|
|
|
srv: dc.user.srv,
|
2023-01-19 17:13:59 +00:00
|
|
|
admin: dc.user.Admin,
|
2023-01-17 12:26:05 +00:00
|
|
|
print: func(text string) {
|
|
|
|
sendServicePRIVMSG(dc, text)
|
|
|
|
},
|
2023-01-20 14:46:42 +00:00
|
|
|
}, text); err != nil {
|
|
|
|
sendServicePRIVMSG(dc, fmt.Sprintf("error: %v", err))
|
|
|
|
}
|
2022-04-05 08:06:31 +00:00
|
|
|
}
|
2020-03-18 11:23:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
uc, err := dc.upstreamForCommand(msg.Command)
|
2020-02-17 14:56:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
if msg.Command == "PRIVMSG" && uc.network.casemap(name) == "nickserv" {
|
2021-11-08 17:11:24 +00:00
|
|
|
dc.handleNickServPRIVMSG(ctx, uc, text)
|
2020-03-13 14:12:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
upstreamParams := []string{name}
|
2022-04-05 08:06:31 +00:00
|
|
|
if msg.Command != "TAGMSG" {
|
2022-08-05 16:43:46 +00:00
|
|
|
upstreamParams = append(upstreamParams, text)
|
2020-11-20 10:19:51 +00:00
|
|
|
}
|
2020-05-21 05:04:34 +00:00
|
|
|
|
2021-12-08 17:03:40 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
2020-05-21 05:04:34 +00:00
|
|
|
Tags: tags,
|
2022-04-05 08:06:31 +00:00
|
|
|
Command: msg.Command,
|
|
|
|
Params: upstreamParams,
|
2020-05-21 05:04:34 +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
|
|
|
|
2022-04-10 16:05:12 +00:00
|
|
|
// If the upstream supports echo message, we'll produce the message
|
|
|
|
// when it is echoed from the upstream.
|
|
|
|
// Otherwise, produce/log it here because it's the last time we'll see it.
|
|
|
|
if !uc.caps.IsEnabled("echo-message") {
|
2022-08-08 09:30:10 +00:00
|
|
|
echoParams := []string{name}
|
2022-04-10 16:05:12 +00:00
|
|
|
if msg.Command != "TAGMSG" {
|
|
|
|
echoParams = append(echoParams, text)
|
|
|
|
}
|
2022-04-05 08:06:31 +00:00
|
|
|
|
2022-04-10 16:05:12 +00:00
|
|
|
echoTags := tags.Copy()
|
2022-10-18 12:57:33 +00:00
|
|
|
echoTags["time"] = dc.user.FormatServerTime(time.Now())
|
2022-04-10 16:05:12 +00:00
|
|
|
if uc.account != "" {
|
2022-11-14 11:06:58 +00:00
|
|
|
echoTags["account"] = uc.account
|
2022-04-10 16:05:12 +00:00
|
|
|
}
|
|
|
|
echoMsg := &irc.Message{
|
|
|
|
Tags: echoTags,
|
|
|
|
Prefix: &irc.Prefix{
|
|
|
|
Name: uc.nick,
|
|
|
|
User: uc.username,
|
|
|
|
Host: uc.hostname,
|
|
|
|
},
|
|
|
|
Command: msg.Command,
|
|
|
|
Params: echoParams,
|
|
|
|
}
|
2022-08-08 09:30:10 +00:00
|
|
|
uc.produce(name, echoMsg, dc.id)
|
2022-02-09 15:50:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
uc.updateChannelAutoDetach(name)
|
2020-05-21 05:04:34 +00:00
|
|
|
}
|
2020-03-26 05:03:07 +00:00
|
|
|
case "INVITE":
|
2022-08-08 09:30:10 +00:00
|
|
|
uc, err := dc.upstreamForCommand(msg.Command)
|
2020-03-26 05:03:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, msg)
|
2021-11-21 15:10:54 +00:00
|
|
|
case "AUTHENTICATE":
|
|
|
|
// Post-connection-registration AUTHENTICATE is unsupported in
|
|
|
|
// multi-upstream mode, or if the upstream doesn't support SASL
|
|
|
|
uc := dc.upstream()
|
2022-03-14 18:24:39 +00:00
|
|
|
if uc == nil || !uc.caps.IsEnabled("sasl") {
|
2021-11-21 15:10:54 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_SASLFAIL,
|
|
|
|
Params: []string{dc.nick, "Upstream network authentication not supported"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
credentials, err := dc.handleAuthenticateCommand(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if credentials != nil {
|
2022-10-14 08:44:32 +00:00
|
|
|
if credentials.mechanism != "PLAIN" {
|
|
|
|
dc.endSASL(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_SASLFAIL,
|
|
|
|
Params: []string{dc.nick, "Unsupported SASL authentication mechanism"},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:10:54 +00:00
|
|
|
if uc.saslClient != nil {
|
|
|
|
dc.endSASL(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_SASLFAIL,
|
|
|
|
Params: []string{dc.nick, "Another authentication attempt is already in progress"},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-14 08:44:32 +00:00
|
|
|
uc.logger.Printf("starting post-registration SASL PLAIN authentication with username %q", credentials.plain.Username)
|
|
|
|
uc.saslClient = sasl.NewPlainClient("", credentials.plain.Username, credentials.plain.Password)
|
2021-11-21 15:10:54 +00:00
|
|
|
uc.enqueueCommand(dc, &irc.Message{
|
|
|
|
Command: "AUTHENTICATE",
|
|
|
|
Params: []string{"PLAIN"},
|
|
|
|
})
|
|
|
|
}
|
2021-11-30 10:54:11 +00:00
|
|
|
case "REGISTER", "VERIFY":
|
|
|
|
// Check number of params here, since we'll use that to save the
|
|
|
|
// credentials on command success
|
|
|
|
if (msg.Command == "REGISTER" && len(msg.Params) < 3) || (msg.Command == "VERIFY" && len(msg.Params) < 2) {
|
|
|
|
return newNeedMoreParamsError(msg.Command)
|
|
|
|
}
|
|
|
|
|
|
|
|
uc := dc.upstream()
|
2022-03-14 18:24:39 +00:00
|
|
|
if uc == nil || !uc.caps.IsEnabled("draft/account-registration") {
|
2021-11-30 10:54:11 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{msg.Command, "TEMPORARILY_UNAVAILABLE", "*", "Upstream network account registration not supported"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.logger.Printf("starting %v with account name %v", msg.Command, msg.Params[0])
|
|
|
|
uc.enqueueCommand(dc, msg)
|
2022-07-11 17:36:12 +00:00
|
|
|
case "AWAY":
|
|
|
|
if len(msg.Params) > 0 {
|
|
|
|
dc.away = &msg.Params[0]
|
|
|
|
} else {
|
|
|
|
dc.away = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := irc.RPL_NOWAWAY
|
|
|
|
desc := "You have been marked as being away"
|
|
|
|
if dc.away == nil {
|
|
|
|
cmd = irc.RPL_UNAWAY
|
|
|
|
desc = "You are no longer marked as being away"
|
|
|
|
}
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Command: cmd,
|
|
|
|
Params: []string{dc.nick, desc},
|
|
|
|
})
|
|
|
|
|
|
|
|
uc := dc.upstream()
|
|
|
|
if uc != nil {
|
|
|
|
uc.updateAway()
|
|
|
|
}
|
2022-07-14 13:51:26 +00:00
|
|
|
case "INFO":
|
|
|
|
if dc.network == nil {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Command: irc.RPL_INFO,
|
|
|
|
Params: []string{dc.nick, "soju <https://soju.im>"},
|
|
|
|
})
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Command: irc.RPL_ENDOFINFO,
|
|
|
|
Params: []string{dc.nick, "End of INFO"},
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if uc := dc.upstream(); uc == nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_UNKNOWNCOMMAND,
|
|
|
|
Params: []string{dc.nick, msg.Command, "Disconnected from upstream network"},
|
|
|
|
}}
|
|
|
|
} else {
|
|
|
|
uc.SendMessageLabeled(ctx, dc.id, msg)
|
|
|
|
}
|
2021-11-09 16:59:43 +00:00
|
|
|
case "MONITOR":
|
|
|
|
// MONITOR is unsupported in multi-upstream mode
|
|
|
|
uc := dc.upstream()
|
|
|
|
if uc == nil {
|
|
|
|
return newUnknownCommandError(msg.Command)
|
|
|
|
}
|
2021-12-04 18:29:39 +00:00
|
|
|
if _, ok := uc.isupport["MONITOR"]; !ok {
|
|
|
|
return newUnknownCommandError(msg.Command)
|
|
|
|
}
|
2021-11-09 16:59:43 +00:00
|
|
|
|
|
|
|
var subcommand string
|
|
|
|
if err := parseMessageParams(msg, &subcommand); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToUpper(subcommand) {
|
|
|
|
case "+", "-":
|
|
|
|
var targets string
|
|
|
|
if err := parseMessageParams(msg, nil, &targets); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, target := range strings.Split(targets, ",") {
|
|
|
|
if subcommand == "+" {
|
|
|
|
// Hard limit, just to avoid having downstreams fill our map
|
2022-06-06 07:58:39 +00:00
|
|
|
if dc.monitored.Len() >= 1000 {
|
2021-11-09 16:59:43 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_MONLISTFULL,
|
|
|
|
Params: []string{dc.nick, "1000", target, "Bouncer monitor list is full"},
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-01 12:15:38 +00:00
|
|
|
dc.monitored.Set(target, struct{}{})
|
2021-11-09 16:59:43 +00:00
|
|
|
|
2022-03-08 20:29:04 +00:00
|
|
|
if uc.network.casemap(target) == serviceNickCM {
|
|
|
|
// BouncerServ is never tired
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_MONONLINE,
|
|
|
|
Params: []string{dc.nick, target},
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-09 16:59:43 +00:00
|
|
|
if uc.monitored.Has(target) {
|
|
|
|
cmd := irc.RPL_MONOFFLINE
|
2022-06-06 07:58:39 +00:00
|
|
|
if online := uc.monitored.Get(target); online {
|
2021-11-09 16:59:43 +00:00
|
|
|
cmd = irc.RPL_MONONLINE
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: cmd,
|
|
|
|
Params: []string{dc.nick, target},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-06 07:58:39 +00:00
|
|
|
dc.monitored.Del(target)
|
2021-11-09 16:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
uc.updateMonitor()
|
|
|
|
case "C": // clear
|
2023-03-01 12:52:33 +00:00
|
|
|
dc.monitored = xirc.NewCaseMappingMap[struct{}](uc.network.casemap)
|
2021-11-09 16:59:43 +00:00
|
|
|
uc.updateMonitor()
|
|
|
|
case "L": // list
|
|
|
|
// TODO: be less lazy and pack the list
|
2023-03-01 12:52:33 +00:00
|
|
|
dc.monitored.ForEach(func(name string, _ struct{}) {
|
2021-11-09 16:59:43 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_MONLIST,
|
2023-03-01 12:52:33 +00:00
|
|
|
Params: []string{dc.nick, name},
|
2021-11-09 16:59:43 +00:00
|
|
|
})
|
2023-03-01 12:52:33 +00:00
|
|
|
})
|
2021-11-09 16:59:43 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_ENDOFMONLIST,
|
|
|
|
Params: []string{dc.nick, "End of MONITOR list"},
|
|
|
|
})
|
|
|
|
case "S": // status
|
|
|
|
// TODO: be less lazy and pack the lists
|
2023-03-01 12:52:33 +00:00
|
|
|
dc.monitored.ForEach(func(target string, _ struct{}) {
|
2021-11-09 16:59:43 +00:00
|
|
|
cmd := irc.RPL_MONOFFLINE
|
2022-06-06 07:58:39 +00:00
|
|
|
if online := uc.monitored.Get(target); online {
|
2021-11-09 16:59:43 +00:00
|
|
|
cmd = irc.RPL_MONONLINE
|
|
|
|
}
|
|
|
|
|
2022-03-08 20:29:04 +00:00
|
|
|
if uc.network.casemap(target) == serviceNickCM {
|
|
|
|
cmd = irc.RPL_MONONLINE
|
|
|
|
}
|
|
|
|
|
2021-11-09 16:59:43 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: cmd,
|
|
|
|
Params: []string{dc.nick, target},
|
|
|
|
})
|
2023-03-01 12:52:33 +00:00
|
|
|
})
|
2021-11-09 16:59:43 +00:00
|
|
|
}
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
case "CHATHISTORY":
|
|
|
|
var subcommand string
|
|
|
|
if err := parseMessageParams(msg, &subcommand); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-27 12:08:31 +00:00
|
|
|
var target, limitStr string
|
|
|
|
var boundsStr [2]string
|
|
|
|
switch subcommand {
|
2021-11-18 19:41:27 +00:00
|
|
|
case "AFTER", "BEFORE", "LATEST":
|
2021-03-27 12:08:31 +00:00
|
|
|
if err := parseMessageParams(msg, nil, &target, &boundsStr[0], &limitStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "BETWEEN":
|
|
|
|
if err := parseMessageParams(msg, nil, &target, &boundsStr[0], &boundsStr[1], &limitStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-18 14:50:19 +00:00
|
|
|
case "TARGETS":
|
2021-11-15 18:20:17 +00:00
|
|
|
if dc.network == nil {
|
|
|
|
// Either an unbound bouncer network, in which case we should return no targets,
|
|
|
|
// or a multi-upstream downstream, but we don't support CHATHISTORY TARGETS for those yet.
|
2022-11-14 11:06:58 +00:00
|
|
|
dc.SendBatch("draft/chathistory-targets", nil, nil, func(batchRef string) {})
|
2021-11-15 18:20:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-18 14:50:19 +00:00
|
|
|
if err := parseMessageParams(msg, nil, &boundsStr[0], &boundsStr[1], &limitStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-27 12:08:31 +00:00
|
|
|
default:
|
2021-11-18 19:41:27 +00:00
|
|
|
// TODO: support AROUND
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
2021-03-27 12:08:31 +00:00
|
|
|
Params: []string{"CHATHISTORY", "INVALID_PARAMS", subcommand, "Unknown command"},
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2021-09-13 09:36:25 +00:00
|
|
|
// We don't save history for our service
|
2023-03-01 12:30:47 +00:00
|
|
|
if xirc.CaseMappingASCII(target) == serviceNickCM {
|
2022-11-14 11:06:58 +00:00
|
|
|
dc.SendBatch("chathistory", []string{target}, nil, func(batchRef string) {})
|
2021-09-13 09:36:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-09 14:25:57 +00:00
|
|
|
store, ok := dc.user.msgStore.(msgstore.ChatHistoryStore)
|
2021-01-04 16:17:35 +00:00
|
|
|
if !ok {
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_UNKNOWNCOMMAND,
|
2022-08-08 09:30:10 +00:00
|
|
|
Params: []string{dc.nick, "CHATHISTORY", "Chat history disabled"},
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
network := dc.network
|
|
|
|
if network == nil {
|
|
|
|
return newChatHistoryError(subcommand, "Cannot fetch chat history on bouncer connection")
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
}
|
2022-08-08 09:30:10 +00:00
|
|
|
|
|
|
|
target = network.casemap(target)
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
|
|
|
|
// TODO: support msgid criteria
|
2021-03-27 12:08:31 +00:00
|
|
|
var bounds [2]time.Time
|
|
|
|
bounds[0] = parseChatHistoryBound(boundsStr[0])
|
2021-11-18 19:41:27 +00:00
|
|
|
if subcommand == "LATEST" && boundsStr[0] == "*" {
|
2022-09-16 16:55:31 +00:00
|
|
|
bounds[0] = time.Time{}
|
2021-11-18 19:41:27 +00:00
|
|
|
} else if bounds[0].IsZero() {
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
2021-03-27 12:08:31 +00:00
|
|
|
Params: []string{"CHATHISTORY", "INVALID_PARAMS", subcommand, boundsStr[0], "Invalid first bound"},
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2021-03-27 12:08:31 +00:00
|
|
|
if boundsStr[1] != "" {
|
|
|
|
bounds[1] = parseChatHistoryBound(boundsStr[1])
|
|
|
|
if bounds[1].IsZero() {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"CHATHISTORY", "INVALID_PARAMS", subcommand, boundsStr[1], "Invalid second bound"},
|
|
|
|
}}
|
|
|
|
}
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
limit, err := strconv.Atoi(limitStr)
|
2021-11-03 17:29:21 +00:00
|
|
|
if err != nil || limit < 0 || limit > chatHistoryLimit {
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
2021-03-04 15:04:30 +00:00
|
|
|
Params: []string{"CHATHISTORY", "INVALID_PARAMS", subcommand, limitStr, "Invalid limit"},
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-03-14 18:15:35 +00:00
|
|
|
eventPlayback := dc.caps.IsEnabled("draft/event-playback")
|
2021-10-08 22:13:16 +00:00
|
|
|
|
2022-05-09 14:25:57 +00:00
|
|
|
options := msgstore.LoadMessageOptions{
|
2022-05-09 13:36:39 +00:00
|
|
|
Network: &network.Network,
|
2022-08-08 09:30:10 +00:00
|
|
|
Entity: target,
|
2022-05-09 13:36:39 +00:00
|
|
|
Limit: limit,
|
|
|
|
Events: eventPlayback,
|
|
|
|
}
|
|
|
|
|
2020-08-11 13:58:50 +00:00
|
|
|
var history []*irc.Message
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
switch subcommand {
|
2022-08-08 18:27:49 +00:00
|
|
|
case "BEFORE":
|
2022-05-09 13:36:39 +00:00
|
|
|
history, err = store.LoadBeforeTime(ctx, bounds[0], time.Time{}, &options)
|
2022-08-08 18:27:49 +00:00
|
|
|
case "LATEST":
|
|
|
|
history, err = store.LoadBeforeTime(ctx, time.Now(), bounds[0], &options)
|
2020-07-15 15:47:57 +00:00
|
|
|
case "AFTER":
|
2022-05-09 13:36:39 +00:00
|
|
|
history, err = store.LoadAfterTime(ctx, bounds[0], time.Now(), &options)
|
2021-03-27 12:08:31 +00:00
|
|
|
case "BETWEEN":
|
|
|
|
if bounds[0].Before(bounds[1]) {
|
2022-05-09 13:36:39 +00:00
|
|
|
history, err = store.LoadAfterTime(ctx, bounds[0], bounds[1], &options)
|
2021-03-27 12:08:31 +00:00
|
|
|
} else {
|
2022-05-09 13:36:39 +00:00
|
|
|
history, err = store.LoadBeforeTime(ctx, bounds[0], bounds[1], &options)
|
2021-03-27 12:08:31 +00:00
|
|
|
}
|
2021-05-18 14:50:19 +00:00
|
|
|
case "TARGETS":
|
|
|
|
// TODO: support TARGETS in multi-upstream mode
|
2021-11-03 17:18:04 +00:00
|
|
|
targets, err := store.ListTargets(ctx, &network.Network, bounds[0], bounds[1], limit, eventPlayback)
|
2021-05-18 14:50:19 +00:00
|
|
|
if err != nil {
|
2021-10-12 15:32:32 +00:00
|
|
|
dc.logger.Printf("failed fetching targets for chathistory: %v", err)
|
2021-05-18 14:50:19 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"CHATHISTORY", "MESSAGE_ERROR", subcommand, "Failed to retrieve targets"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-11-14 11:06:58 +00:00
|
|
|
dc.SendBatch("draft/chathistory-targets", nil, nil, func(batchRef string) {
|
2021-06-05 10:38:52 +00:00
|
|
|
for _, target := range targets {
|
2022-06-06 07:58:39 +00:00
|
|
|
if ch := network.channels.Get(target.Name); ch != nil && ch.Detached {
|
2021-06-05 10:38:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-05-18 14:50:19 +00:00
|
|
|
|
2021-06-05 10:38:52 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Tags: irc.Tags{"batch": batchRef},
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "CHATHISTORY",
|
2022-05-09 14:15:00 +00:00
|
|
|
Params: []string{"TARGETS", target.Name, xirc.FormatServerTime(target.LatestMessage)},
|
2021-06-05 10:38:52 +00:00
|
|
|
})
|
2021-06-04 09:27:59 +00:00
|
|
|
}
|
2021-05-18 14:50:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
Add support for downstream CHATHISTORY
This adds support for the WIP (at the time of this commit)
draft/chathistory extension, based on the draft at [1] and the
additional comments at [2].
This gets the history by parsing the chat logs, and is therefore only
enabled when the logs are enabled and the log path is configured.
Getting the history only from the logs adds some restrictions:
- we cannot get history by msgid (those are not logged)
- we cannot get the users masks (maybe they could be inferred from the
JOIN etc, but it is not worth the effort and would not work every
time)
The regular soju network history is not sent to clients that support
draft/chathistory, so that they can fetch what they need by manually
calling CHATHISTORY.
The only supported command is BEFORE for now, because that is the only
required command for an app that offers an "infinite history scrollback"
feature.
Regarding implementation, rather than reading the file from the end in
reverse, we simply start from the beginning of each log file, store each
PRIVMSG into a ring, then add the last lines of that ring into the
history we'll return later. The message parsing implementation must be
kept somewhat fast because an app could potentially request thousands of
messages in several files. Here we are using simple sscanf and indexOf
rather than regexps.
In case some log files do not contain any message (for example because
the user had not joined a channel at that time), we try up to a 100 days
of empty log files before giving up.
[1]: https://github.com/prawnsalad/ircv3-specifications/pull/3/files
[2]: https://github.com/ircv3/ircv3-specifications/pull/393/files#r350210018
2020-05-21 22:59:57 +00:00
|
|
|
}
|
2020-08-11 13:58:50 +00:00
|
|
|
if err != nil {
|
2021-05-11 10:42:12 +00:00
|
|
|
dc.logger.Printf("failed fetching %q messages for chathistory: %v", target, err)
|
2020-08-11 13:58:50 +00:00
|
|
|
return newChatHistoryError(subcommand, target)
|
|
|
|
}
|
|
|
|
|
2022-11-14 11:06:58 +00:00
|
|
|
dc.SendBatch("chathistory", []string{target}, nil, func(batchRef string) {
|
2021-06-05 10:38:52 +00:00
|
|
|
for _, msg := range history {
|
|
|
|
msg.Tags["batch"] = batchRef
|
2022-08-05 16:57:38 +00:00
|
|
|
dc.SendMessage(msg)
|
2021-06-05 10:38:52 +00:00
|
|
|
}
|
2020-08-11 13:58:50 +00:00
|
|
|
})
|
2022-06-27 13:51:56 +00:00
|
|
|
case "READ", "MARKREAD":
|
2021-01-29 15:57:38 +00:00
|
|
|
var target, criteria string
|
|
|
|
if err := parseMessageParams(msg, &target); err != nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
2022-06-27 13:51:56 +00:00
|
|
|
Params: []string{msg.Command, "NEED_MORE_PARAMS", "Missing parameters"},
|
2021-01-29 15:57:38 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
if len(msg.Params) > 1 {
|
|
|
|
criteria = msg.Params[1]
|
|
|
|
}
|
|
|
|
|
2022-02-12 18:07:30 +00:00
|
|
|
// We don't save read receipts for our service
|
2023-03-01 12:30:47 +00:00
|
|
|
if xirc.CaseMappingASCII(target) == serviceNickCM {
|
2022-02-12 18:07:30 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
2022-06-27 13:51:56 +00:00
|
|
|
Command: msg.Command,
|
2022-02-12 18:07:30 +00:00
|
|
|
Params: []string{target, "*"},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
network := dc.network
|
|
|
|
if network == nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{msg.Command, "INTERNAL_ERROR", target, "Cannot set read markers on bouncer connection"},
|
|
|
|
}}
|
2021-01-29 15:57:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
targetCM := network.casemap(target)
|
|
|
|
|
|
|
|
r, err := dc.srv.db.GetReadReceipt(ctx, network.ID, targetCM)
|
2021-01-29 15:57:38 +00:00
|
|
|
if err != nil {
|
2022-08-08 09:30:10 +00:00
|
|
|
dc.logger.Printf("failed to get the read receipt for %q: %v", target, err)
|
2021-01-29 15:57:38 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
2022-06-27 13:51:56 +00:00
|
|
|
Params: []string{msg.Command, "INTERNAL_ERROR", target, "Internal error"},
|
2021-01-29 15:57:38 +00:00
|
|
|
}}
|
|
|
|
} else if r == nil {
|
2022-05-09 10:34:43 +00:00
|
|
|
r = &database.ReadReceipt{
|
2022-08-08 09:30:10 +00:00
|
|
|
Target: targetCM,
|
2021-01-29 15:57:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
broadcast := false
|
|
|
|
if len(criteria) > 0 {
|
|
|
|
// TODO: support msgid criteria
|
|
|
|
criteriaParts := strings.SplitN(criteria, "=", 2)
|
|
|
|
if len(criteriaParts) != 2 || criteriaParts[0] != "timestamp" {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
2022-06-27 13:51:56 +00:00
|
|
|
Params: []string{msg.Command, "INVALID_PARAMS", criteria, "Unknown criteria"},
|
2021-01-29 15:57:38 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-05-09 14:15:00 +00:00
|
|
|
timestamp, err := time.Parse(xirc.ServerTimeLayout, criteriaParts[1])
|
2021-01-29 15:57:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
2022-06-27 13:51:56 +00:00
|
|
|
Params: []string{msg.Command, "INVALID_PARAMS", criteria, "Invalid criteria"},
|
2021-01-29 15:57:38 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
if timestamp.After(now) {
|
|
|
|
timestamp = now
|
|
|
|
}
|
|
|
|
if r.Timestamp.Before(timestamp) {
|
|
|
|
r.Timestamp = timestamp
|
2022-03-08 21:32:45 +00:00
|
|
|
if err := dc.srv.db.StoreReadReceipt(ctx, network.ID, r); err != nil {
|
2022-08-08 09:30:10 +00:00
|
|
|
dc.logger.Printf("failed to store receipt for %q: %v", target, err)
|
2021-01-29 15:57:38 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
2022-06-27 13:51:56 +00:00
|
|
|
Params: []string{msg.Command, "INTERNAL_ERROR", target, "Internal error"},
|
2021-01-29 15:57:38 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
broadcast = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timestampStr := "*"
|
|
|
|
if !r.Timestamp.IsZero() {
|
2022-05-09 14:15:00 +00:00
|
|
|
timestampStr = fmt.Sprintf("timestamp=%s", xirc.FormatServerTime(r.Timestamp))
|
2021-01-29 15:57:38 +00:00
|
|
|
}
|
2022-03-08 21:32:45 +00:00
|
|
|
network.forEachDownstream(func(d *downstreamConn) {
|
2021-01-29 15:57:38 +00:00
|
|
|
if broadcast || dc.id == d.id {
|
2022-07-08 13:17:30 +00:00
|
|
|
cmd := "MARKREAD"
|
2022-07-12 15:03:23 +00:00
|
|
|
if !d.caps.IsEnabled("draft/read-marker") {
|
2022-07-08 13:17:30 +00:00
|
|
|
cmd = "READ"
|
|
|
|
}
|
2021-01-29 15:57:38 +00:00
|
|
|
d.SendMessage(&irc.Message{
|
|
|
|
Prefix: d.prefix(),
|
2022-07-08 13:17:30 +00:00
|
|
|
Command: cmd,
|
2022-08-08 09:30:10 +00:00
|
|
|
Params: []string{target, timestampStr},
|
2021-01-29 15:57:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2022-07-08 14:55:29 +00:00
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
if broadcast && network.pushTargets.Has(target) {
|
2022-07-08 14:55:29 +00:00
|
|
|
// TODO: only broadcast if draft/read-marker has been negotiated
|
2022-10-24 12:56:35 +00:00
|
|
|
if !r.Timestamp.Before(network.pushTargets.Get(target)) {
|
|
|
|
network.pushTargets.Del(target)
|
|
|
|
}
|
2022-08-17 14:09:12 +00:00
|
|
|
go network.broadcastWebPush(&irc.Message{
|
2022-07-08 14:55:29 +00:00
|
|
|
Command: "MARKREAD",
|
2022-08-08 09:30:10 +00:00
|
|
|
Params: []string{target, timestampStr},
|
2022-07-08 14:55:29 +00:00
|
|
|
})
|
|
|
|
}
|
2022-02-21 18:44:56 +00:00
|
|
|
case "SEARCH":
|
2022-05-09 14:25:57 +00:00
|
|
|
store, ok := dc.user.msgStore.(msgstore.SearchStore)
|
2022-02-21 18:44:56 +00:00
|
|
|
if !ok {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_UNKNOWNCOMMAND,
|
|
|
|
Params: []string{dc.nick, "SEARCH", "Unknown command"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
var attrsStr string
|
|
|
|
if err := parseMessageParams(msg, &attrsStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
attrs := irc.ParseTags(attrsStr)
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
var network *network
|
2022-02-21 18:44:56 +00:00
|
|
|
const searchMaxLimit = 100
|
2022-05-09 14:25:57 +00:00
|
|
|
opts := msgstore.SearchMessageOptions{
|
2022-05-09 13:44:41 +00:00
|
|
|
Limit: searchMaxLimit,
|
2022-02-21 18:44:56 +00:00
|
|
|
}
|
|
|
|
for name, v := range attrs {
|
|
|
|
value := string(v)
|
|
|
|
switch name {
|
|
|
|
case "before", "after":
|
2022-05-09 14:15:00 +00:00
|
|
|
timestamp, err := time.Parse(xirc.ServerTimeLayout, value)
|
2022-02-21 18:44:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"SEARCH", "INVALID_PARAMS", name, "Invalid criteria"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
switch name {
|
|
|
|
case "after":
|
2022-05-09 13:44:41 +00:00
|
|
|
opts.Start = timestamp
|
2022-02-21 18:44:56 +00:00
|
|
|
case "before":
|
2022-05-09 13:44:41 +00:00
|
|
|
opts.End = timestamp
|
2022-02-21 18:44:56 +00:00
|
|
|
}
|
|
|
|
case "from":
|
2022-05-09 13:44:41 +00:00
|
|
|
opts.From = value
|
2022-02-21 18:44:56 +00:00
|
|
|
case "in":
|
2022-08-08 09:30:10 +00:00
|
|
|
network = dc.network
|
|
|
|
if network == nil {
|
2022-02-21 18:44:56 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
2022-08-08 09:30:10 +00:00
|
|
|
Params: []string{"SEARCH", "INVALID_PARAMS", name, "Cannot search on bouncer connection"},
|
2022-02-21 18:44:56 +00:00
|
|
|
}}
|
|
|
|
}
|
2022-08-08 09:30:10 +00:00
|
|
|
opts.In = network.casemap(value)
|
2022-02-21 18:44:56 +00:00
|
|
|
case "text":
|
2022-05-09 13:44:41 +00:00
|
|
|
opts.Text = value
|
2022-02-21 18:44:56 +00:00
|
|
|
case "limit":
|
|
|
|
limit, err := strconv.Atoi(value)
|
|
|
|
if err != nil || limit <= 0 {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"SEARCH", "INVALID_PARAMS", name, "Invalid limit"},
|
|
|
|
}}
|
|
|
|
}
|
2022-05-09 13:44:41 +00:00
|
|
|
opts.Limit = limit
|
2022-02-21 18:44:56 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-08 09:30:10 +00:00
|
|
|
if network == nil {
|
2022-02-21 18:44:56 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"SEARCH", "INVALID_PARAMS", "in", "The in parameter is mandatory"},
|
|
|
|
}}
|
|
|
|
}
|
2022-05-09 13:44:41 +00:00
|
|
|
if opts.Limit > searchMaxLimit {
|
|
|
|
opts.Limit = searchMaxLimit
|
2022-02-21 18:44:56 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:30:10 +00:00
|
|
|
messages, err := store.Search(ctx, &network.Network, &opts)
|
2022-02-21 18:44:56 +00:00
|
|
|
if err != nil {
|
|
|
|
dc.logger.Printf("failed fetching messages for search: %v", err)
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"SEARCH", "INTERNAL_ERROR", "Messages could not be retrieved"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-11-14 11:06:58 +00:00
|
|
|
dc.SendBatch("soju.im/search", nil, nil, func(batchRef string) {
|
2022-02-21 18:44:56 +00:00
|
|
|
for _, msg := range messages {
|
|
|
|
msg.Tags["batch"] = batchRef
|
2022-08-05 16:57:38 +00:00
|
|
|
dc.SendMessage(msg)
|
2022-02-21 18:44:56 +00:00
|
|
|
}
|
|
|
|
})
|
2021-01-22 19:55:53 +00:00
|
|
|
case "BOUNCER":
|
|
|
|
var subcommand string
|
|
|
|
if err := parseMessageParams(msg, &subcommand); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToUpper(subcommand) {
|
2021-10-16 09:41:37 +00:00
|
|
|
case "BIND":
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"BOUNCER", "REGISTRATION_IS_COMPLETED", "BIND", "Cannot bind to a network after registration"},
|
|
|
|
}}
|
2021-01-22 19:55:53 +00:00
|
|
|
case "LISTNETWORKS":
|
2022-11-14 11:06:58 +00:00
|
|
|
dc.SendBatch("soju.im/bouncer-networks", nil, nil, func(batchRef string) {
|
2022-02-04 13:01:27 +00:00
|
|
|
for _, network := range dc.user.networks {
|
2021-06-05 10:38:52 +00:00
|
|
|
idStr := fmt.Sprintf("%v", network.ID)
|
|
|
|
attrs := getNetworkAttrs(network)
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Tags: irc.Tags{"batch": batchRef},
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "BOUNCER",
|
|
|
|
Params: []string{"NETWORK", idStr, attrs.String()},
|
|
|
|
})
|
2022-02-04 13:01:27 +00:00
|
|
|
}
|
2021-01-22 19:55:53 +00:00
|
|
|
})
|
|
|
|
case "ADDNETWORK":
|
|
|
|
var attrsStr string
|
|
|
|
if err := parseMessageParams(msg, nil, &attrsStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
attrs := irc.ParseTags(attrsStr)
|
|
|
|
|
2022-05-09 10:34:43 +00:00
|
|
|
record := &database.Network{Nick: dc.nick, Enabled: true}
|
2021-10-29 13:51:13 +00:00
|
|
|
if err := updateNetworkAttrs(record, attrs, subcommand); err != nil {
|
|
|
|
return err
|
2021-01-22 19:55:53 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 22:33:17 +00:00
|
|
|
if record.Nick == dc.user.Username {
|
|
|
|
record.Nick = ""
|
|
|
|
}
|
2021-10-29 13:51:13 +00:00
|
|
|
if record.Realname == dc.user.Realname {
|
|
|
|
record.Realname = ""
|
2021-06-25 18:33:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 18:36:10 +00:00
|
|
|
network, err := dc.user.createNetwork(ctx, record)
|
2021-01-22 19:55:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"BOUNCER", "UNKNOWN_ERROR", subcommand, fmt.Sprintf("Failed to create network: %v", err)},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "BOUNCER",
|
|
|
|
Params: []string{"ADDNETWORK", fmt.Sprintf("%v", network.ID)},
|
|
|
|
})
|
|
|
|
case "CHANGENETWORK":
|
|
|
|
var idStr, attrsStr string
|
|
|
|
if err := parseMessageParams(msg, nil, &idStr, &attrsStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-10 08:27:59 +00:00
|
|
|
id, err := parseBouncerNetID(subcommand, idStr)
|
2021-01-22 19:55:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
attrs := irc.ParseTags(attrsStr)
|
|
|
|
|
|
|
|
net := dc.user.getNetworkByID(id)
|
|
|
|
if net == nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
2021-03-10 08:27:59 +00:00
|
|
|
Params: []string{"BOUNCER", "INVALID_NETID", subcommand, idStr, "Invalid network ID"},
|
2021-01-22 19:55:53 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
record := net.Network // copy network record because we'll mutate it
|
2021-10-29 13:51:13 +00:00
|
|
|
if err := updateNetworkAttrs(&record, attrs, subcommand); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-02 22:33:17 +00:00
|
|
|
if record.Nick == dc.user.Username {
|
|
|
|
record.Nick = ""
|
|
|
|
}
|
2021-10-29 13:51:13 +00:00
|
|
|
if record.Realname == dc.user.Realname {
|
|
|
|
record.Realname = ""
|
2021-01-22 19:55:53 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 18:36:10 +00:00
|
|
|
_, err = dc.user.updateNetwork(ctx, &record)
|
2021-01-22 19:55:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"BOUNCER", "UNKNOWN_ERROR", subcommand, fmt.Sprintf("Failed to update network: %v", err)},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "BOUNCER",
|
|
|
|
Params: []string{"CHANGENETWORK", idStr},
|
|
|
|
})
|
|
|
|
case "DELNETWORK":
|
|
|
|
var idStr string
|
|
|
|
if err := parseMessageParams(msg, nil, &idStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-10 08:27:59 +00:00
|
|
|
id, err := parseBouncerNetID(subcommand, idStr)
|
2021-01-22 19:55:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
net := dc.user.getNetworkByID(id)
|
|
|
|
if net == nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
2021-03-10 08:27:59 +00:00
|
|
|
Params: []string{"BOUNCER", "INVALID_NETID", subcommand, idStr, "Invalid network ID"},
|
2021-01-22 19:55:53 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:36:10 +00:00
|
|
|
if err := dc.user.deleteNetwork(ctx, net.ID); err != nil {
|
2021-01-22 19:55:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "BOUNCER",
|
|
|
|
Params: []string{"DELNETWORK", idStr},
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"BOUNCER", "UNKNOWN_COMMAND", subcommand, "Unknown subcommand"},
|
|
|
|
}}
|
|
|
|
}
|
2021-11-27 10:48:10 +00:00
|
|
|
case "WEBPUSH":
|
|
|
|
if !dc.caps.IsEnabled("soju.im/webpush") {
|
|
|
|
return newUnknownCommandError(msg.Command)
|
|
|
|
}
|
|
|
|
|
|
|
|
var subcommand string
|
|
|
|
if err := parseMessageParams(msg, &subcommand); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch subcommand {
|
|
|
|
case "REGISTER":
|
|
|
|
var endpoint, keysStr string
|
|
|
|
if err := parseMessageParams(msg, nil, &endpoint, &keysStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawKeys := irc.ParseTags(keysStr)
|
|
|
|
authKey, hasAuthKey := rawKeys["auth"]
|
|
|
|
p256dhKey, hasP256dh := rawKeys["p256dh"]
|
|
|
|
if !hasAuthKey || !hasP256dh {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"WEBPUSH", "INVALID_PARAMS", subcommand, "Missing auth or p256dh key"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
newSub := database.WebPushSubscription{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
}
|
|
|
|
newSub.Keys.VAPID = dc.srv.webPush.VAPIDKeys.Public
|
|
|
|
newSub.Keys.Auth = string(authKey)
|
|
|
|
newSub.Keys.P256DH = string(p256dhKey)
|
|
|
|
|
2022-08-17 15:04:11 +00:00
|
|
|
subs, err := dc.listWebPushSubscriptions(ctx)
|
2021-11-27 10:48:10 +00:00
|
|
|
if err != nil {
|
|
|
|
dc.logger.Printf("failed to fetch Web push subscription: %v", err)
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"WEBPUSH", "INTERNAL_ERROR", subcommand, "Internal error"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:08:23 +00:00
|
|
|
if len(subs) > 25 {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"WEBPUSH", "INTERNAL_ERROR", subcommand, "Too many subscriptions"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2023-02-18 12:23:02 +00:00
|
|
|
updateSub := true
|
2022-08-17 15:04:11 +00:00
|
|
|
oldSub := findWebPushSubscription(subs, endpoint)
|
2021-11-27 10:48:10 +00:00
|
|
|
if oldSub != nil {
|
|
|
|
// Update the old subscription instead of creating a new one
|
|
|
|
newSub.ID = oldSub.ID
|
2023-02-18 12:23:02 +00:00
|
|
|
// Rate-limit subscription checks
|
|
|
|
updateSub = time.Since(oldSub.UpdatedAt) > webpushCheckSubscriptionDelay || oldSub.Keys != newSub.Keys
|
2021-11-27 10:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var networkID int64
|
|
|
|
if dc.network != nil {
|
|
|
|
networkID = dc.network.ID
|
|
|
|
}
|
|
|
|
|
2022-09-30 10:20:07 +00:00
|
|
|
// Send a test Web Push message, to make sure the endpoint is valid
|
2023-02-18 12:23:02 +00:00
|
|
|
if updateSub {
|
|
|
|
err = dc.srv.sendWebPush(ctx, &webpush.Subscription{
|
|
|
|
Endpoint: newSub.Endpoint,
|
|
|
|
Keys: webpush.Keys{
|
|
|
|
Auth: newSub.Keys.Auth,
|
|
|
|
P256dh: newSub.Keys.P256DH,
|
|
|
|
},
|
|
|
|
}, newSub.Keys.VAPID, &irc.Message{
|
|
|
|
Command: "NOTE",
|
|
|
|
Params: []string{"WEBPUSH", "REGISTERED", "Push notifications enabled"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
dc.logger.Printf("failed to send Web push notification to endpoint %q: %v", newSub.Endpoint, err)
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"WEBPUSH", "INVALID_PARAMS", subcommand, "Invalid endpoint"},
|
|
|
|
}}
|
|
|
|
}
|
2022-09-30 10:20:07 +00:00
|
|
|
|
2023-02-18 12:23:02 +00:00
|
|
|
// TODO: limit max number of subscriptions, prune old ones
|
2022-09-30 10:20:07 +00:00
|
|
|
|
2023-02-18 12:23:02 +00:00
|
|
|
if err := dc.user.srv.db.StoreWebPushSubscription(ctx, dc.user.ID, networkID, &newSub); err != nil {
|
|
|
|
dc.logger.Printf("failed to store Web push subscription: %v", err)
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"WEBPUSH", "INTERNAL_ERROR", subcommand, "Internal error"},
|
|
|
|
}}
|
|
|
|
}
|
2021-11-27 10:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "WEBPUSH",
|
|
|
|
Params: []string{"REGISTER", endpoint},
|
|
|
|
})
|
|
|
|
case "UNREGISTER":
|
|
|
|
var endpoint string
|
|
|
|
if err := parseMessageParams(msg, nil, &endpoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:04:11 +00:00
|
|
|
subs, err := dc.listWebPushSubscriptions(ctx)
|
2021-11-27 10:48:10 +00:00
|
|
|
if err != nil {
|
|
|
|
dc.logger.Printf("failed to fetch Web push subscription: %v", err)
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"WEBPUSH", "INTERNAL_ERROR", subcommand, "Internal error"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:04:11 +00:00
|
|
|
oldSub := findWebPushSubscription(subs, endpoint)
|
2021-11-27 10:48:10 +00:00
|
|
|
if oldSub == nil {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "WEBPUSH",
|
|
|
|
Params: []string{"UNREGISTER", endpoint},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dc.srv.db.DeleteWebPushSubscription(ctx, oldSub.ID); err != nil {
|
|
|
|
dc.logger.Printf("failed to delete Web push subscription: %v", err)
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"WEBPUSH", "INTERNAL_ERROR", subcommand, "Internal error"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "WEBPUSH",
|
|
|
|
Params: []string{"UNREGISTER", endpoint},
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"WEBPUSH", "INVALID_PARAMS", subcommand, "Unknown command"},
|
|
|
|
}}
|
|
|
|
}
|
2020-02-06 15:18:19 +00:00
|
|
|
default:
|
2020-02-17 11:36:42 +00:00
|
|
|
dc.logger.Printf("unhandled message: %v", msg)
|
2021-05-28 09:15:15 +00:00
|
|
|
|
|
|
|
// Only forward unknown commands in single-upstream mode
|
2022-04-27 17:05:01 +00:00
|
|
|
if dc.network == nil {
|
|
|
|
return newUnknownCommandError(msg.Command)
|
|
|
|
}
|
|
|
|
|
2021-05-28 09:15:15 +00:00
|
|
|
uc := dc.upstream()
|
|
|
|
if uc == nil {
|
2022-04-27 17:05:01 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_UNKNOWNCOMMAND,
|
|
|
|
Params: []string{"*", msg.Command, "Disconnected from upstream network"},
|
|
|
|
}}
|
2021-05-28 09:15:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 17:03:40 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, msg)
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
2020-02-07 11:19:42 +00:00
|
|
|
return nil
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
2020-03-13 14:12:44 +00:00
|
|
|
|
2021-11-08 17:11:24 +00:00
|
|
|
func (dc *downstreamConn) handleNickServPRIVMSG(ctx context.Context, uc *upstreamConn, text string) {
|
2020-03-13 14:12:44 +00:00
|
|
|
username, password, ok := parseNickServCredentials(text, uc.nick)
|
2021-11-21 15:10:54 +00:00
|
|
|
if ok {
|
|
|
|
uc.network.autoSaveSASLPlain(ctx, username, password)
|
2020-03-13 14:12:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:04:11 +00:00
|
|
|
func (dc *downstreamConn) listWebPushSubscriptions(ctx context.Context) ([]database.WebPushSubscription, error) {
|
2022-06-15 07:09:28 +00:00
|
|
|
var networkID int64
|
|
|
|
if dc.network != nil {
|
|
|
|
networkID = dc.network.ID
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:04:11 +00:00
|
|
|
return dc.user.srv.db.ListWebPushSubscriptions(ctx, dc.user.ID, networkID)
|
|
|
|
}
|
2021-11-27 10:48:10 +00:00
|
|
|
|
2022-08-17 15:04:11 +00:00
|
|
|
func findWebPushSubscription(subs []database.WebPushSubscription, endpoint string) *database.WebPushSubscription {
|
2021-11-27 10:48:10 +00:00
|
|
|
for i, sub := range subs {
|
|
|
|
if sub.Endpoint == endpoint {
|
2022-08-17 15:04:11 +00:00
|
|
|
return &subs[i]
|
2021-11-27 10:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-17 15:04:11 +00:00
|
|
|
return nil
|
2021-11-27 10:48:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 14:12:44 +00:00
|
|
|
func parseNickServCredentials(text, nick string) (username, password string, ok bool) {
|
|
|
|
fields := strings.Fields(text)
|
|
|
|
if len(fields) < 2 {
|
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
cmd := strings.ToUpper(fields[0])
|
|
|
|
params := fields[1:]
|
|
|
|
switch cmd {
|
|
|
|
case "REGISTER":
|
|
|
|
username = nick
|
|
|
|
password = params[0]
|
|
|
|
case "IDENTIFY":
|
|
|
|
if len(params) == 1 {
|
|
|
|
username = nick
|
2020-03-28 09:40:33 +00:00
|
|
|
password = params[0]
|
2020-03-13 14:12:44 +00:00
|
|
|
} else {
|
|
|
|
username = params[0]
|
2020-03-28 09:40:33 +00:00
|
|
|
password = params[1]
|
|
|
|
}
|
|
|
|
case "SET":
|
|
|
|
if len(params) == 2 && strings.EqualFold(params[0], "PASSWORD") {
|
|
|
|
username = nick
|
|
|
|
password = params[1]
|
2020-03-13 14:12:44 +00:00
|
|
|
}
|
2020-06-24 19:48:09 +00:00
|
|
|
default:
|
|
|
|
return "", "", false
|
2020-03-13 14:12:44 +00:00
|
|
|
}
|
|
|
|
return username, password, true
|
|
|
|
}
|
2022-05-30 07:51:36 +00:00
|
|
|
|
|
|
|
func forwardChannel(ctx context.Context, dc *downstreamConn, ch *upstreamChannel) {
|
|
|
|
if !ch.complete {
|
|
|
|
panic("Tried to forward a partial channel")
|
|
|
|
}
|
|
|
|
|
|
|
|
// RPL_NOTOPIC shouldn't be sent on JOIN
|
|
|
|
if ch.Topic != "" {
|
|
|
|
sendTopic(dc, ch)
|
|
|
|
}
|
|
|
|
|
2022-06-27 13:51:56 +00:00
|
|
|
var markReadCmd string
|
|
|
|
if dc.caps.IsEnabled("draft/read-marker") {
|
|
|
|
markReadCmd = "MARKREAD"
|
|
|
|
} else if dc.caps.IsEnabled("soju.im/read") {
|
|
|
|
markReadCmd = "READ"
|
|
|
|
}
|
|
|
|
if markReadCmd != "" {
|
2022-05-30 07:51:36 +00:00
|
|
|
channelCM := ch.conn.network.casemap(ch.Name)
|
|
|
|
r, err := dc.srv.db.GetReadReceipt(ctx, ch.conn.network.ID, channelCM)
|
|
|
|
if err != nil {
|
|
|
|
dc.logger.Printf("failed to get the read receipt for %q: %v", ch.Name, err)
|
|
|
|
} else {
|
|
|
|
timestampStr := "*"
|
|
|
|
if r != nil {
|
|
|
|
timestampStr = fmt.Sprintf("timestamp=%s", xirc.FormatServerTime(r.Timestamp))
|
|
|
|
}
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
2022-06-27 13:51:56 +00:00
|
|
|
Command: markReadCmd,
|
2022-08-05 17:14:03 +00:00
|
|
|
Params: []string{ch.Name, timestampStr},
|
2022-05-30 07:51:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !dc.caps.IsEnabled("soju.im/no-implicit-names") {
|
|
|
|
sendNames(dc, ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendTopic(dc *downstreamConn, ch *upstreamChannel) {
|
|
|
|
if ch.Topic != "" {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_TOPIC,
|
2022-08-05 17:14:03 +00:00
|
|
|
Params: []string{dc.nick, ch.Name, ch.Topic},
|
2022-05-30 07:51:36 +00:00
|
|
|
})
|
|
|
|
if ch.TopicWho != nil {
|
|
|
|
topicTime := strconv.FormatInt(ch.TopicTime.Unix(), 10)
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: xirc.RPL_TOPICWHOTIME,
|
2022-08-05 17:14:03 +00:00
|
|
|
Params: []string{dc.nick, ch.Name, ch.TopicWho.String(), topicTime},
|
2022-05-30 07:51:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_NOTOPIC,
|
2022-08-05 17:14:03 +00:00
|
|
|
Params: []string{dc.nick, ch.Name, "No topic is set"},
|
2022-05-30 07:51:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendNames(dc *downstreamConn, ch *upstreamChannel) {
|
|
|
|
var members []string
|
2022-06-06 07:58:39 +00:00
|
|
|
ch.Members.ForEach(func(nick string, memberships *xirc.MembershipSet) {
|
2022-08-05 17:14:03 +00:00
|
|
|
s := formatMemberPrefix(*memberships, dc) + nick
|
2022-05-30 07:51:36 +00:00
|
|
|
members = append(members, s)
|
2022-06-06 07:58:39 +00:00
|
|
|
})
|
2022-05-30 07:51:36 +00:00
|
|
|
|
2022-08-05 17:14:03 +00:00
|
|
|
msgs := xirc.GenerateNamesReply(dc.srv.prefix(), dc.nick, ch.Name, ch.Status, members)
|
2022-05-30 07:51:36 +00:00
|
|
|
for _, msg := range msgs {
|
|
|
|
dc.SendMessage(msg)
|
|
|
|
}
|
|
|
|
}
|