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
|
|
|
|
2020-03-16 15:16:27 +00:00
|
|
|
"github.com/emersion/go-sasl"
|
2020-03-11 18:09:32 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2020-02-06 15:18:19 +00:00
|
|
|
"gopkg.in/irc.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
// authError is an authentication error.
|
|
|
|
type authError struct {
|
|
|
|
// Internal error cause. This will not be revealed to the user.
|
|
|
|
err error
|
|
|
|
// Error cause which can safely be sent to the user without compromising
|
|
|
|
// security.
|
|
|
|
reason string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err *authError) Error() string {
|
|
|
|
return err.err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err *authError) Unwrap() error {
|
|
|
|
return err.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// authErrorReason returns the user-friendly reason of an authentication
|
|
|
|
// failure.
|
|
|
|
func authErrorReason(err error) string {
|
|
|
|
if authErr, ok := err.(*authError); ok {
|
|
|
|
return authErr.reason
|
|
|
|
} else {
|
|
|
|
return "Authentication failed"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newInvalidUsernameOrPasswordError(err error) error {
|
|
|
|
return &authError{
|
|
|
|
err: err,
|
|
|
|
reason: "Invalid username or password",
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 15:18:19 +00:00
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-29 13:51:13 +00:00
|
|
|
func fillNetworkAddrAttrs(attrs irc.Tags, network *Network) {
|
|
|
|
u, err := network.URL()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hasHostPort := true
|
|
|
|
switch u.Scheme {
|
|
|
|
case "ircs":
|
|
|
|
attrs["tls"] = irc.TagValue("1")
|
|
|
|
case "irc+insecure":
|
|
|
|
attrs["tls"] = irc.TagValue("0")
|
|
|
|
default: // e.g. unix://
|
|
|
|
hasHostPort = false
|
|
|
|
}
|
|
|
|
if host, port, err := net.SplitHostPort(u.Host); err == nil && hasHostPort {
|
|
|
|
attrs["host"] = irc.TagValue(host)
|
|
|
|
attrs["port"] = irc.TagValue(port)
|
|
|
|
} else if hasHostPort {
|
|
|
|
attrs["host"] = irc.TagValue(u.Host)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
"name": irc.TagValue(network.GetName()),
|
|
|
|
"state": irc.TagValue(state),
|
2021-11-02 22:33:17 +00:00
|
|
|
"nickname": irc.TagValue(GetNick(&network.user.User, &network.Network)),
|
2021-03-10 08:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if network.Username != "" {
|
|
|
|
attrs["username"] = irc.TagValue(network.Username)
|
|
|
|
}
|
2021-06-25 18:33:13 +00:00
|
|
|
if realname := GetRealname(&network.user.User, &network.Network); realname != "" {
|
|
|
|
attrs["realname"] = irc.TagValue(realname)
|
2021-03-10 08:27:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 13:51:13 +00:00
|
|
|
fillNetworkAddrAttrs(attrs, &network.Network)
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func networkAddrFromAttrs(attrs irc.Tags) string {
|
|
|
|
host, ok := attrs.GetTag("host")
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := host
|
|
|
|
if port, ok := attrs.GetTag("port"); ok {
|
|
|
|
addr += ":" + port
|
|
|
|
}
|
|
|
|
|
|
|
|
if tlsStr, ok := attrs.GetTag("tls"); ok && tlsStr == "0" {
|
|
|
|
addr = "irc+insecure://" + tlsStr
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateNetworkAttrs(record *Network, attrs irc.Tags, subcommand string) error {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-25 09:49:22 +00:00
|
|
|
// ' ' and ':' break the IRC message wire format, '@' and '!' break prefixes,
|
2021-11-18 08:19:27 +00:00
|
|
|
// '*' and '?' break masks, '$' breaks server masks in PRIVMSG/NOTICE,
|
2021-12-06 18:23:28 +00:00
|
|
|
// "*" is the reserved nickname for registration, ',' breaks lists
|
|
|
|
const illegalNickChars = " :@!*?$,"
|
2020-08-20 08:00:58 +00:00
|
|
|
|
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": "",
|
|
|
|
"message-tags": "",
|
|
|
|
"server-time": "",
|
2021-05-25 18:24:45 +00:00
|
|
|
"setname": "",
|
2021-03-10 08:27:59 +00:00
|
|
|
|
|
|
|
"soju.im/bouncer-networks": "",
|
|
|
|
"soju.im/bouncer-networks-notify": "",
|
2020-04-29 17:07:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 21:39:59 +00:00
|
|
|
// needAllDownstreamCaps is the list of downstream capabilities that
|
|
|
|
// require support from all upstreams to be enabled
|
|
|
|
var needAllDownstreamCaps = map[string]string{
|
2021-10-17 19:49:37 +00:00
|
|
|
"account-notify": "",
|
|
|
|
"account-tag": "",
|
|
|
|
"away-notify": "",
|
|
|
|
"extended-join": "",
|
|
|
|
"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,
|
|
|
|
"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,
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
2021-11-21 15:10:54 +00:00
|
|
|
type downstreamSASL struct {
|
|
|
|
server sasl.Server
|
|
|
|
plainUsername, plainPassword string
|
2021-12-09 22:21:08 +00:00
|
|
|
pendingResp bytes.Buffer
|
2021-11-21 15:10:54 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-11-17 10:22:26 +00:00
|
|
|
registered bool
|
|
|
|
user *user
|
|
|
|
nick string
|
|
|
|
nickCM string
|
|
|
|
rawUsername string
|
|
|
|
networkName string
|
|
|
|
clientName string
|
|
|
|
realname string
|
|
|
|
hostname string
|
2021-11-19 18:21:48 +00:00
|
|
|
account string // RPL_LOGGEDIN/OUT state
|
2021-11-17 10:22:26 +00:00
|
|
|
password string // empty after authentication
|
|
|
|
network *network // can be nil
|
|
|
|
isMultiUpstream bool
|
2020-03-16 13:28:45 +00:00
|
|
|
|
2021-09-19 14:47:27 +00:00
|
|
|
negotiatingCaps bool
|
2020-03-16 14:05:24 +00:00
|
|
|
capVersion int
|
2020-04-29 17:07:15 +00:00
|
|
|
supportedCaps map[string]string
|
2020-04-06 17:08:43 +00:00
|
|
|
caps map[string]bool
|
2021-11-21 15:10:54 +00:00
|
|
|
sasl *downstreamSASL
|
2020-03-16 14:05:24 +00:00
|
|
|
|
2021-06-05 10:38:52 +00:00
|
|
|
lastBatchRef uint64
|
|
|
|
|
2021-11-09 16:59:43 +00:00
|
|
|
monitored casemapMap
|
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{
|
2020-08-19 17:28:29 +00:00
|
|
|
conn: *newConn(srv, ic, &options),
|
2020-04-29 14:28:33 +00:00
|
|
|
id: id,
|
2021-11-18 08:19:27 +00:00
|
|
|
nick: "*",
|
|
|
|
nickCM: "*",
|
2020-04-29 17:07:15 +00:00
|
|
|
supportedCaps: make(map[string]string),
|
2020-04-29 14:28:33 +00:00
|
|
|
caps: make(map[string]bool),
|
2021-11-09 16:59:43 +00:00
|
|
|
monitored: newCasemapMap(0),
|
2020-02-06 20:11:35 +00:00
|
|
|
}
|
2020-04-23 20:25:43 +00:00
|
|
|
dc.hostname = remoteAddr
|
2020-03-21 23:44:55 +00:00
|
|
|
if host, _, err := net.SplitHostPort(dc.hostname); err == nil {
|
|
|
|
dc.hostname = host
|
|
|
|
}
|
2020-04-29 17:07:15 +00:00
|
|
|
for k, v := range permanentDownstreamCaps {
|
|
|
|
dc.supportedCaps[k] = v
|
|
|
|
}
|
2021-11-21 15:28:38 +00:00
|
|
|
dc.supportedCaps["sasl"] = "PLAIN"
|
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
|
|
|
|
if srv.Config().LogPath != "" {
|
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
|
|
|
dc.supportedCaps["draft/chathistory"] = ""
|
|
|
|
}
|
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,
|
2020-03-28 16:28:28 +00:00
|
|
|
User: dc.user.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)
|
2021-11-17 10:22:26 +00:00
|
|
|
} else if dc.isMultiUpstream {
|
2022-02-04 13:01:27 +00:00
|
|
|
for _, network := range dc.user.networks {
|
|
|
|
f(network)
|
|
|
|
}
|
2020-03-12 18:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:44:13 +00:00
|
|
|
func (dc *downstreamConn) forEachUpstream(f func(*upstreamConn)) {
|
2021-11-17 10:22:26 +00:00
|
|
|
if dc.network == nil && !dc.isMultiUpstream {
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-12 17:33:03 +00:00
|
|
|
// upstream returns the upstream connection, if any. If there are zero or if
|
|
|
|
// there are multiple upstream connections, it returns nil.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 {
|
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
|
|
|
return net.casemap(nick) == net.conn.nickCM
|
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.
|
2021-11-02 22:33:17 +00:00
|
|
|
return net.casemap(nick) == net.casemap(GetNick(&net.user.User, &net.Network))
|
2020-04-16 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 20:30:54 +00:00
|
|
|
// marshalEntity converts an upstream entity name (ie. channel or nick) into a
|
|
|
|
// downstream entity name.
|
|
|
|
//
|
|
|
|
// This involves adding a "/<network>" suffix if the entity isn't the current
|
|
|
|
// user.
|
2020-04-16 15:19:00 +00:00
|
|
|
func (dc *downstreamConn) marshalEntity(net *network, name string) string {
|
2020-05-01 17:03:34 +00:00
|
|
|
if isOurNick(net, name) {
|
|
|
|
return dc.nick
|
|
|
|
}
|
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
|
|
|
name = partialCasemap(net.casemap, name)
|
2020-04-16 14:33:56 +00:00
|
|
|
if dc.network != nil {
|
2020-04-16 15:19:00 +00:00
|
|
|
if dc.network != net {
|
2020-04-16 14:54:13 +00:00
|
|
|
panic("soju: tried to marshal an entity for another network")
|
|
|
|
}
|
2020-04-16 14:33:56 +00:00
|
|
|
return name
|
|
|
|
}
|
2020-04-16 15:19:00 +00:00
|
|
|
return name + "/" + net.GetName()
|
2020-03-18 02:14:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:19:00 +00:00
|
|
|
func (dc *downstreamConn) marshalUserPrefix(net *network, prefix *irc.Prefix) *irc.Prefix {
|
|
|
|
if isOurNick(net, prefix.Name) {
|
2020-04-16 14:33:56 +00:00
|
|
|
return dc.prefix()
|
|
|
|
}
|
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
|
|
|
prefix.Name = partialCasemap(net.casemap, prefix.Name)
|
2020-03-20 09:42:17 +00:00
|
|
|
if dc.network != nil {
|
2020-04-16 15:19:00 +00:00
|
|
|
if dc.network != net {
|
2020-04-16 14:54:13 +00:00
|
|
|
panic("soju: tried to marshal a user prefix for another network")
|
|
|
|
}
|
2020-04-16 14:33:56 +00:00
|
|
|
return prefix
|
|
|
|
}
|
|
|
|
return &irc.Prefix{
|
2020-04-16 15:19:00 +00:00
|
|
|
Name: prefix.Name + "/" + net.GetName(),
|
2020-04-16 14:33:56 +00:00
|
|
|
User: prefix.User,
|
|
|
|
Host: prefix.Host,
|
2020-03-18 02:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 09:14:47 +00:00
|
|
|
// unmarshalEntityNetwork converts a downstream entity name (ie. channel or
|
|
|
|
// nick) into an upstream entity name.
|
2020-04-07 20:30:54 +00:00
|
|
|
//
|
|
|
|
// This involves removing the "/<network>" suffix.
|
2021-09-13 09:14:47 +00:00
|
|
|
func (dc *downstreamConn) unmarshalEntityNetwork(name string) (*network, string, error) {
|
2021-03-16 08:13:46 +00:00
|
|
|
if dc.network != nil {
|
2021-09-13 09:14:47 +00:00
|
|
|
return dc.network, name, nil
|
2021-03-16 08:13:46 +00:00
|
|
|
}
|
2021-11-29 15:41:52 +00:00
|
|
|
if !dc.isMultiUpstream {
|
|
|
|
return nil, "", ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NOSUCHCHANNEL,
|
|
|
|
Params: []string{dc.nick, name, "Cannot interact with channels and users on the bouncer connection. Did you mean to use a specific network?"},
|
|
|
|
}}
|
|
|
|
}
|
2020-03-12 17:33:03 +00:00
|
|
|
|
2021-09-13 09:14:47 +00:00
|
|
|
var net *network
|
2020-03-18 02:14:36 +00:00
|
|
|
if i := strings.LastIndexByte(name, '/'); i >= 0 {
|
2020-03-19 23:23:19 +00:00
|
|
|
network := name[i+1:]
|
2020-03-18 02:14:36 +00:00
|
|
|
name = name[:i]
|
|
|
|
|
2021-09-13 09:14:47 +00:00
|
|
|
for _, n := range dc.user.networks {
|
|
|
|
if network == n.GetName() {
|
|
|
|
net = n
|
|
|
|
break
|
2020-03-18 02:14:36 +00:00
|
|
|
}
|
2021-09-13 09:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if net == nil {
|
|
|
|
return nil, "", ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NOSUCHCHANNEL,
|
2021-11-29 15:41:52 +00:00
|
|
|
Params: []string{dc.nick, name, "Missing network suffix in name"},
|
2021-09-13 09:14:47 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
return net, name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshalEntity is the same as unmarshalEntityNetwork, but returns the
|
|
|
|
// upstream connection and fails if the upstream is disconnected.
|
|
|
|
func (dc *downstreamConn) unmarshalEntity(name string) (*upstreamConn, string, error) {
|
|
|
|
net, name, err := dc.unmarshalEntityNetwork(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
2020-03-18 02:14:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 09:14:47 +00:00
|
|
|
if net.conn == nil {
|
2020-03-04 14:44:13 +00:00
|
|
|
return nil, "", ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NOSUCHCHANNEL,
|
2021-11-29 15:41:52 +00:00
|
|
|
Params: []string{dc.nick, name, "Disconnected from upstream network"},
|
2020-03-04 14:44:13 +00:00
|
|
|
}}
|
2020-02-19 17:25:19 +00:00
|
|
|
}
|
2021-09-13 09:14:47 +00:00
|
|
|
|
|
|
|
return net.conn, name, nil
|
2020-02-19 17:25:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 20:54:45 +00:00
|
|
|
func (dc *downstreamConn) unmarshalText(uc *upstreamConn, text string) string {
|
|
|
|
if dc.upstream() != nil {
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
// TODO: smarter parsing that ignores URLs
|
|
|
|
return strings.ReplaceAll(text, "/"+uc.network.GetName(), "")
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-04-06 16:23:39 +00:00
|
|
|
if !dc.caps["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":
|
2020-04-06 16:23:39 +00:00
|
|
|
supported = dc.caps["server-time"]
|
2021-06-14 19:44:38 +00:00
|
|
|
case "account":
|
|
|
|
supported = dc.caps["account"]
|
2020-04-03 18:48:23 +00:00
|
|
|
}
|
|
|
|
if !supported {
|
|
|
|
delete(msg.Tags, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-05 10:38:52 +00:00
|
|
|
if !dc.caps["batch"] && msg.Tags["batch"] != "" {
|
|
|
|
msg = msg.Copy()
|
|
|
|
delete(msg.Tags, "batch")
|
|
|
|
}
|
2020-09-08 17:49:06 +00:00
|
|
|
if msg.Command == "JOIN" && !dc.caps["extended-join"] {
|
|
|
|
msg.Params = msg.Params[:1]
|
|
|
|
}
|
2021-05-25 18:24:45 +00:00
|
|
|
if msg.Command == "SETNAME" && !dc.caps["setname"] {
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 19:53:18 +00:00
|
|
|
if msg.Command == "AWAY" && !dc.caps["away-notify"] {
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 19:49:37 +00:00
|
|
|
if msg.Command == "ACCOUNT" && !dc.caps["account-notify"] {
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-06-05 10:38:52 +00:00
|
|
|
func (dc *downstreamConn) SendBatch(typ string, params []string, tags irc.Tags, f func(batchRef irc.TagValue)) {
|
|
|
|
dc.lastBatchRef++
|
|
|
|
ref := fmt.Sprintf("%v", dc.lastBatchRef)
|
|
|
|
|
|
|
|
if dc.caps["batch"] {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Tags: tags,
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: "BATCH",
|
|
|
|
Params: append([]string{"+" + ref, typ}, params...),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
f(irc.TagValue(ref))
|
|
|
|
|
|
|
|
if dc.caps["batch"] {
|
|
|
|
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)
|
|
|
|
|
2021-10-08 22:13:16 +00:00
|
|
|
if id == "" || !dc.messageSupportsBacklog(msg) {
|
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) {
|
2021-10-08 22:13:16 +00:00
|
|
|
if id == "" || !dc.messageSupportsBacklog(msg) {
|
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) {
|
2021-03-31 09:59:13 +00:00
|
|
|
netID, entity, err := 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)
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:45:29 +00:00
|
|
|
// marshalMessage re-formats a message coming from an upstream connection so
|
|
|
|
// that it's suitable for being sent on this downstream connection. Only
|
2021-10-08 22:13:16 +00:00
|
|
|
// messages that may appear in logs are supported, except MODE messages which
|
|
|
|
// may only appear in single-upstream mode.
|
2020-04-16 15:23:35 +00:00
|
|
|
func (dc *downstreamConn) marshalMessage(msg *irc.Message, net *network) *irc.Message {
|
2021-11-15 16:38:38 +00:00
|
|
|
msg = msg.Copy()
|
|
|
|
msg.Prefix = dc.marshalUserPrefix(net, msg.Prefix)
|
|
|
|
|
2021-10-08 22:13:16 +00:00
|
|
|
if dc.network != nil {
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:51:42 +00:00
|
|
|
switch msg.Command {
|
2020-05-21 05:04:34 +00:00
|
|
|
case "PRIVMSG", "NOTICE", "TAGMSG":
|
2020-04-16 15:23:35 +00:00
|
|
|
msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
2020-04-07 17:45:29 +00:00
|
|
|
case "NICK":
|
|
|
|
// Nick change for another user
|
2020-04-16 15:23:35 +00:00
|
|
|
msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
2020-04-07 17:45:29 +00:00
|
|
|
case "JOIN", "PART":
|
2020-04-16 15:23:35 +00:00
|
|
|
msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
2020-04-07 17:45:29 +00:00
|
|
|
case "KICK":
|
2020-04-16 15:23:35 +00:00
|
|
|
msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
|
|
|
msg.Params[1] = dc.marshalEntity(net, msg.Params[1])
|
2020-04-07 17:45:29 +00:00
|
|
|
case "TOPIC":
|
2020-04-16 15:23:35 +00:00
|
|
|
msg.Params[0] = dc.marshalEntity(net, msg.Params[0])
|
2021-05-25 18:24:45 +00:00
|
|
|
case "QUIT", "SETNAME":
|
2020-04-16 15:25:39 +00:00
|
|
|
// This space is intentionally left blank
|
2020-04-06 15:51:42 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected %q message", msg.Command))
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:45:29 +00:00
|
|
|
return msg
|
2020-04-06 15:51:42 +00:00
|
|
|
}
|
|
|
|
|
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":
|
2020-03-18 11:23:08 +00:00
|
|
|
var nick string
|
|
|
|
if err := parseMessageParams(msg, &nick); err != nil {
|
2020-02-07 11:36:02 +00:00
|
|
|
return err
|
2020-02-06 15:18:19 +00:00
|
|
|
}
|
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,
|
|
|
|
Params: []string{dc.nick, nick, "contains illegal characters"},
|
|
|
|
}}
|
|
|
|
}
|
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
|
|
|
nickCM := casemapASCII(nick)
|
|
|
|
if nickCM == serviceNickCM {
|
2020-03-18 11:23:08 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NICKNAMEINUSE,
|
|
|
|
Params: []string{dc.nick, nick, "Nickname reserved for bouncer service"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
dc.nick = nick
|
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
|
|
|
dc.nickCM = nickCM
|
2020-02-06 15:18:19 +00:00
|
|
|
case "USER":
|
2020-03-18 11:23:08 +00:00
|
|
|
if err := parseMessageParams(msg, &dc.rawUsername, nil, nil, &dc.realname); 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":
|
|
|
|
if err := parseMessageParams(msg, &dc.password); err != nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-21 15:10:54 +00:00
|
|
|
if err := dc.authenticate(ctx, credentials.plainUsername, credentials.plainPassword); err != nil {
|
2021-11-29 12:14:16 +00:00
|
|
|
dc.logger.Printf("SASL authentication error for user %q: %v", credentials.plainUsername, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
var match *network
|
2022-02-04 13:01:27 +00:00
|
|
|
for _, net := range dc.user.networks {
|
2021-01-22 19:55:53 +00:00
|
|
|
if net.ID == id {
|
|
|
|
match = net
|
2022-02-04 13:01:27 +00:00
|
|
|
break
|
2021-01-22 19:55:53 +00:00
|
|
|
}
|
2022-02-04 13:01:27 +00:00
|
|
|
}
|
2021-01-22 19:55:53 +00:00
|
|
|
if match == nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"BOUNCER", "INVALID_NETID", idStr, "Unknown network ID"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.networkName = match.GetName()
|
|
|
|
}
|
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)
|
|
|
|
}
|
2021-11-18 08:19:27 +00:00
|
|
|
if dc.rawUsername != "" && dc.nick != "*" && !dc.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 {
|
|
|
|
dc.supportedCaps[k] = v
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 14:05:24 +00:00
|
|
|
|
2020-04-29 17:07:15 +00:00
|
|
|
caps := make([]string, 0, len(dc.supportedCaps))
|
|
|
|
for k, v := range dc.supportedCaps {
|
|
|
|
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
|
|
|
|
dc.caps["cap-notify"] = true
|
|
|
|
}
|
|
|
|
|
2020-03-16 14:05:24 +00:00
|
|
|
if !dc.registered {
|
2021-09-19 14:47:27 +00:00
|
|
|
dc.negotiatingCaps = true
|
2020-03-16 14:05:24 +00:00
|
|
|
}
|
|
|
|
case "LIST":
|
|
|
|
var caps []string
|
2021-05-20 13:47:19 +00:00
|
|
|
for name, enabled := range dc.caps {
|
|
|
|
if 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{
|
|
|
|
Command: 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
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2020-04-29 17:07:15 +00:00
|
|
|
// TODO: atomically ack/nak the whole capability set
|
2020-03-16 14:05:24 +00:00
|
|
|
caps := strings.Fields(args[0])
|
|
|
|
ack := true
|
|
|
|
for _, name := range caps {
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
enable := !strings.HasPrefix(name, "-")
|
|
|
|
if !enable {
|
|
|
|
name = strings.TrimPrefix(name, "-")
|
|
|
|
}
|
|
|
|
|
2020-04-29 17:07:15 +00:00
|
|
|
if enable == dc.caps[name] {
|
2020-03-16 14:05:24 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-29 17:07:15 +00:00
|
|
|
_, ok := dc.supportedCaps[name]
|
|
|
|
if !ok {
|
|
|
|
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
|
|
|
|
|
|
|
dc.caps[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 {
|
|
|
|
dc.negotiatingCaps = true
|
|
|
|
}
|
2020-03-16 14:05:24 +00:00
|
|
|
case "END":
|
2021-09-19 14:47:27 +00:00
|
|
|
dc.negotiatingCaps = false
|
2020-03-16 14:05:24 +00:00
|
|
|
default:
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: 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
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if !dc.caps["sasl"] {
|
|
|
|
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 {
|
|
|
|
dc.sasl.plainUsername = username
|
|
|
|
dc.sasl.plainPassword = password
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
default:
|
|
|
|
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, fmt.Sprintf("Unsupported SASL mechanism %q", mech)},
|
2021-11-21 15:10:54 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.sasl = &downstreamSASL{server: server}
|
|
|
|
} 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)
|
|
|
|
|
2021-12-10 09:44:40 +00:00
|
|
|
if len(chunk) == 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) {
|
|
|
|
prevValue, hasPrev := dc.supportedCaps[name]
|
|
|
|
changed := !hasPrev || prevValue != value
|
|
|
|
dc.supportedCaps[name] = value
|
|
|
|
|
|
|
|
if !dc.caps["cap-notify"] || !changed {
|
|
|
|
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) {
|
|
|
|
_, hasPrev := dc.supportedCaps[name]
|
|
|
|
delete(dc.supportedCaps, name)
|
|
|
|
delete(dc.caps, name)
|
|
|
|
|
|
|
|
if !dc.caps["cap-notify"] || !hasPrev {
|
|
|
|
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 {
|
|
|
|
supportedCaps[cap] = supported && uc.caps[cap]
|
|
|
|
}
|
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")
|
|
|
|
}
|
|
|
|
|
2021-11-30 10:54:11 +00:00
|
|
|
if uc := dc.upstream(); uc != nil && uc.caps["draft/account-registration"] {
|
|
|
|
// Strip "before-connect", because we require downstreams to be fully
|
|
|
|
// connected before attempting account registration.
|
|
|
|
values := strings.Split(uc.supportedCaps["draft/account-registration"], ",")
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:38:04 +00:00
|
|
|
if _, ok := dc.user.msgStore.(chatHistoryMessageStore); 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() {
|
|
|
|
if uc := dc.upstream(); uc != nil && uc.nick != dc.nick {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
|
|
|
Command: "NICK",
|
|
|
|
Params: []string{uc.nick},
|
|
|
|
})
|
|
|
|
dc.nick = uc.nick
|
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
|
|
|
dc.nickCM = casemapASCII(dc.nick)
|
2020-04-30 22:37:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 18:24:45 +00:00
|
|
|
func (dc *downstreamConn) updateRealname() {
|
|
|
|
if uc := dc.upstream(); uc != nil && uc.realname != dc.realname && dc.caps["setname"] {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
|
|
|
Command: "SETNAME",
|
|
|
|
Params: []string{uc.realname},
|
|
|
|
})
|
|
|
|
dc.realname = uc.realname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if dc.account == account || !dc.caps["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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-11-17 11:29:23 +00:00
|
|
|
func (dc *downstreamConn) authenticate(ctx context.Context, username, password string) error {
|
2020-03-28 16:25:48 +00:00
|
|
|
username, clientName, networkName := unmarshalUsername(username)
|
2020-03-27 18:17:58 +00:00
|
|
|
|
2021-11-17 11:29:23 +00:00
|
|
|
u, err := dc.srv.db.GetUser(ctx, username)
|
2020-03-27 21:38:38 +00:00
|
|
|
if err != nil {
|
2021-11-29 12:14:16 +00:00
|
|
|
return newInvalidUsernameOrPasswordError(fmt.Errorf("user not found: %w", err))
|
2020-03-27 18:17:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 10:52:22 +00:00
|
|
|
// Password auth disabled
|
|
|
|
if u.Password == "" {
|
2021-11-29 12:14:16 +00:00
|
|
|
return newInvalidUsernameOrPasswordError(fmt.Errorf("password auth disabled"))
|
2020-06-06 10:52:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 21:38:38 +00:00
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
2020-03-27 18:17:58 +00:00
|
|
|
if err != nil {
|
2021-11-29 12:14:16 +00:00
|
|
|
return newInvalidUsernameOrPasswordError(fmt.Errorf("wrong password"))
|
2020-03-27 18:17:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 21:38:38 +00:00
|
|
|
dc.user = dc.srv.getUser(username)
|
|
|
|
if dc.user == nil {
|
2021-11-29 12:14:16 +00:00
|
|
|
return fmt.Errorf("user not active")
|
2020-03-27 21:38:38 +00:00
|
|
|
}
|
2020-03-28 16:25:48 +00:00
|
|
|
dc.clientName = clientName
|
2020-03-27 18:17:58 +00:00
|
|
|
dc.networkName = networkName
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("tried to register twice")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-27 18:17:58 +00:00
|
|
|
password := dc.password
|
|
|
|
dc.password = ""
|
|
|
|
if dc.user == nil {
|
2021-12-07 08:40:02 +00:00
|
|
|
if password == "" {
|
|
|
|
if dc.caps["sasl"] {
|
|
|
|
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"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 11:29:23 +00:00
|
|
|
if err := dc.authenticate(ctx, dc.rawUsername, password); err != nil {
|
2021-11-29 12:14:16 +00:00
|
|
|
dc.logger.Printf("PASS authentication error for user %q: %v", dc.rawUsername, err)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 16:25:48 +00:00
|
|
|
if dc.clientName == "" && dc.networkName == "" {
|
|
|
|
_, dc.clientName, dc.networkName = unmarshalUsername(dc.rawUsername)
|
2020-03-27 18:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dc.registered = true
|
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 {
|
2020-03-27 18:17:58 +00:00
|
|
|
if dc.networkName == "" {
|
2020-03-16 15:16:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-27 18:17:58 +00:00
|
|
|
network := dc.user.getNetwork(dc.networkName)
|
2020-03-16 15:16:27 +00:00
|
|
|
if network == nil {
|
2020-03-27 18:17:58 +00:00
|
|
|
addr := dc.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,
|
2021-12-07 08:42:32 +00:00
|
|
|
Params: []string{dc.nick, fmt.Sprintf("Failed to connect to %q", dc.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.
|
|
|
|
nick, _, _ := unmarshalUsername(dc.nick)
|
|
|
|
|
2020-03-27 18:17:58 +00:00
|
|
|
dc.logger.Printf("auto-saving network %q", dc.networkName)
|
2020-03-16 15:16:27 +00:00
|
|
|
var err error
|
2021-11-17 11:33:30 +00:00
|
|
|
network, err = dc.user.createNetwork(ctx, &Network{
|
2021-05-26 08:49:52 +00:00
|
|
|
Addr: dc.networkName,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-17 10:41:11 +00:00
|
|
|
if dc.network == nil && !dc.caps["soju.im/bouncer-networks"] && dc.srv.Config().MultiUpstream {
|
2021-11-17 10:22:26 +00:00
|
|
|
dc.isMultiUpstream = true
|
|
|
|
}
|
|
|
|
|
2021-11-17 14:27:34 +00:00
|
|
|
dc.updateSupportedCaps()
|
|
|
|
|
2021-01-22 10:55:06 +00:00
|
|
|
isupport := []string{
|
2021-11-03 17:29:21 +00:00
|
|
|
fmt.Sprintf("CHATHISTORY=%v", chatHistoryLimit),
|
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
|
|
|
"CASEMAPPING=ascii",
|
2021-01-22 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 19:55:53 +00:00
|
|
|
if dc.network != nil {
|
|
|
|
isupport = append(isupport, fmt.Sprintf("BOUNCER_NETID=%v", dc.network.ID))
|
|
|
|
}
|
2021-11-15 23:38:04 +00:00
|
|
|
if title := dc.srv.Config().Title; dc.network == nil && title != "" {
|
|
|
|
isupport = append(isupport, "NETWORK="+encodeISUPPORT(title))
|
2021-11-02 21:38:07 +00:00
|
|
|
}
|
2021-11-17 10:22:26 +00:00
|
|
|
if dc.network == nil && !dc.isMultiUpstream {
|
2021-11-02 17:15:45 +00:00
|
|
|
isupport = append(isupport, "WHOX")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
})
|
2021-03-15 22:41:37 +00:00
|
|
|
for _, msg := range generateIsupport(dc.srv.prefix(), dc.nick, isupport) {
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
2021-11-17 10:22:26 +00:00
|
|
|
if dc.network == nil && !dc.isMultiUpstream && 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
|
|
|
|
2021-11-17 14:27:34 +00:00
|
|
|
dc.updateNick()
|
|
|
|
dc.updateRealname()
|
2021-11-19 18:21:48 +00:00
|
|
|
dc.updateAccount()
|
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 {
|
2021-10-13 08:58:34 +00:00
|
|
|
for _, msg := range generateMOTD(dc.srv.prefix(), dc.nick, motd) {
|
|
|
|
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
|
|
|
|
2021-03-10 08:27:59 +00:00
|
|
|
if dc.caps["soju.im/bouncer-networks-notify"] {
|
2021-06-05 10:38:52 +00:00
|
|
|
dc.SendBatch("soju.im/bouncer-networks", nil, nil, func(batchRef irc.TagValue) {
|
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) {
|
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
|
|
|
for _, entry := range uc.channels.innerMap {
|
|
|
|
ch := entry.value.(*upstreamChannel)
|
2020-04-28 13:27:41 +00:00
|
|
|
if !ch.complete {
|
|
|
|
continue
|
|
|
|
}
|
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
|
|
|
record := uc.network.channels.Value(ch.Name)
|
|
|
|
if record != nil && record.Detached {
|
2020-04-28 13:27:41 +00:00
|
|
|
continue
|
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",
|
|
|
|
Params: []string{dc.marshalEntity(ch.conn.network, ch.Name)},
|
|
|
|
})
|
|
|
|
|
|
|
|
forwardChannel(dc, ch)
|
2020-02-06 21:29:24 +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) {
|
2021-04-13 15:50:03 +00:00
|
|
|
if dc.caps["draft/chathistory"] || dc.user.msgStore == nil {
|
|
|
|
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
|
|
|
|
dc.user.forEachDownstream(func(c *downstreamConn) {
|
|
|
|
if c != dc && c.clientName == dc.clientName && c.network == dc.network {
|
|
|
|
firstClient = false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
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) {
|
2020-10-25 10:13:51 +00:00
|
|
|
if dc.caps["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
|
|
|
|
|
|
|
ch := net.channels.Value(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)
|
2021-11-03 17:29:21 +00:00
|
|
|
history, err := dc.user.msgStore.LoadLatestID(ctx, &net.Network, targetCM, msgID, backlogLimit)
|
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
|
|
|
|
2021-06-05 10:38:52 +00:00
|
|
|
dc.SendBatch("chathistory", []string{dc.marshalEntity(net, target)}, nil, func(batchRef irc.TagValue) {
|
|
|
|
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
|
2021-06-05 10:38:52 +00:00
|
|
|
dc.SendMessage(dc.marshalMessage(msg, net))
|
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) {
|
|
|
|
sendServiceNOTICE(dc, fmt.Sprintf("highlight in %v: <%v> %v", dc.marshalEntity(net, target), sender, text))
|
|
|
|
} else {
|
|
|
|
sendServiceNOTICE(dc, fmt.Sprintf("message in %v: <%v> %v", dc.marshalEntity(net, target), sender, text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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":
|
2020-11-23 16:09:31 +00:00
|
|
|
var rawNick string
|
|
|
|
if err := parseMessageParams(msg, &rawNick); err != nil {
|
2020-03-12 18:17:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:09:31 +00:00
|
|
|
nick := rawNick
|
2020-05-08 18:24:59 +00:00
|
|
|
var upstream *upstreamConn
|
|
|
|
if dc.upstream() == nil {
|
|
|
|
uc, unmarshaledNick, err := dc.unmarshalEntity(nick)
|
|
|
|
if err == nil { // NICK nick/network: NICK only on a specific upstream
|
|
|
|
upstream = uc
|
|
|
|
nick = unmarshaledNick
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2020-11-24 13:22:39 +00:00
|
|
|
Params: []string{dc.nick, rawNick, "contains illegal characters"},
|
2020-08-20 08:00:58 +00:00
|
|
|
}}
|
|
|
|
}
|
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 casemapASCII(nick) == serviceNickCM {
|
2020-11-23 16:09:31 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NICKNAMEINUSE,
|
|
|
|
Params: []string{dc.nick, rawNick, "Nickname reserved for bouncer service"},
|
|
|
|
}}
|
|
|
|
}
|
2020-08-20 08:00:58 +00:00
|
|
|
|
2020-03-12 18:17:06 +00:00
|
|
|
var err error
|
|
|
|
dc.forEachNetwork(func(n *network) {
|
2020-05-08 18:24:59 +00:00
|
|
|
if err != nil || (upstream != nil && upstream.network != n) {
|
2020-03-12 18:17:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
n.Nick = nick
|
2021-11-08 17:11:24 +00:00
|
|
|
err = dc.srv.db.StoreNetwork(ctx, dc.user.ID, &n.Network)
|
2020-03-12 18:17:06 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:44:13 +00:00
|
|
|
dc.forEachUpstream(func(uc *upstreamConn) {
|
2020-05-08 18:24:59 +00:00
|
|
|
if upstream != nil && upstream != uc {
|
|
|
|
return
|
|
|
|
}
|
2021-12-08 17:03:40 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
2020-05-08 18:24:59 +00:00
|
|
|
Command: "NICK",
|
|
|
|
Params: []string{nick},
|
|
|
|
})
|
2020-02-07 11:19:42 +00:00
|
|
|
})
|
2020-04-30 22:37:42 +00:00
|
|
|
|
2021-04-30 08:44:35 +00:00
|
|
|
if dc.upstream() == nil && upstream == nil && dc.nick != nick {
|
2020-04-30 22:37:42 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
|
|
|
Command: "NICK",
|
|
|
|
Params: []string{nick},
|
|
|
|
})
|
|
|
|
dc.nick = nick
|
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
|
|
|
dc.nickCM = casemapASCII(dc.nick)
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:33:13 +00:00
|
|
|
// If the client just resets to the default, just wipe the per-network
|
|
|
|
// preference
|
|
|
|
storeRealname := realname
|
|
|
|
if realname == dc.user.Realname {
|
|
|
|
storeRealname = ""
|
|
|
|
}
|
|
|
|
|
2021-05-25 18:24:45 +00:00
|
|
|
var storeErr error
|
|
|
|
var needUpdate []Network
|
|
|
|
dc.forEachNetwork(func(n *network) {
|
|
|
|
// We only need to call updateNetwork for upstreams that don't
|
|
|
|
// support setname
|
|
|
|
if uc := n.conn; uc != nil && uc.caps["setname"] {
|
2021-12-08 17:03:40 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
2021-05-25 18:24:45 +00:00
|
|
|
Command: "SETNAME",
|
|
|
|
Params: []string{realname},
|
|
|
|
})
|
|
|
|
|
2021-06-25 18:33:13 +00:00
|
|
|
n.Realname = storeRealname
|
2021-11-08 17:11:24 +00:00
|
|
|
if err := dc.srv.db.StoreNetwork(ctx, dc.user.ID, &n.Network); err != nil {
|
2021-05-25 18:24:45 +00:00
|
|
|
dc.logger.Printf("failed to store network realname: %v", err)
|
|
|
|
storeErr = err
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
record := n.Network // copy network record because we'll mutate it
|
2021-06-25 18:33:13 +00:00
|
|
|
record.Realname = storeRealname
|
2021-05-25 18:24:45 +00:00
|
|
|
needUpdate = append(needUpdate, record)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Walk the network list as a second step, because updateNetwork
|
|
|
|
// mutates the original list
|
|
|
|
for _, record := range needUpdate {
|
2021-11-08 18:36:10 +00:00
|
|
|
if _, err := dc.user.updateNetwork(ctx, &record); err != nil {
|
2021-05-25 18:24:45 +00:00
|
|
|
dc.logger.Printf("failed to update network realname: %v", err)
|
|
|
|
storeErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if storeErr != nil {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: "FAIL",
|
|
|
|
Params: []string{"SETNAME", "CANNOT_CHANGE_REALNAME", "Failed to update realname"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2021-10-18 07:20:11 +00:00
|
|
|
if dc.upstream() == nil {
|
2021-05-25 18:24:45 +00:00
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.prefix(),
|
|
|
|
Command: "SETNAME",
|
|
|
|
Params: []string{realname},
|
|
|
|
})
|
|
|
|
}
|
2020-03-25 10:52:24 +00:00
|
|
|
case "JOIN":
|
|
|
|
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, ",") {
|
2020-03-25 10:32:44 +00:00
|
|
|
uc, upstreamName, err := dc.unmarshalEntity(name)
|
2020-03-12 17:33:03 +00:00
|
|
|
if err != nil {
|
2020-03-25 22:56:01 +00:00
|
|
|
return err
|
2020-03-12 17:33:03 +00:00
|
|
|
}
|
2020-03-25 10:32:44 +00:00
|
|
|
|
2020-03-25 10:52:24 +00:00
|
|
|
var key string
|
|
|
|
if len(keys) > i {
|
|
|
|
key = keys[i]
|
|
|
|
}
|
|
|
|
|
2021-05-26 09:21:37 +00:00
|
|
|
if !uc.isChannel(upstreamName) {
|
|
|
|
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.
|
|
|
|
if !uc.channels.Has(upstreamName) {
|
|
|
|
params := []string{upstreamName}
|
|
|
|
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
|
|
|
|
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
|
|
|
ch := uc.network.channels.Value(upstreamName)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
uc.network.attach(ch)
|
|
|
|
} else {
|
|
|
|
ch = &Channel{
|
|
|
|
Name: upstreamName,
|
|
|
|
Key: key,
|
|
|
|
}
|
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
|
|
|
uc.network.channels.SetValue(upstreamName, 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 {
|
2020-04-05 13:04:52 +00:00
|
|
|
dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
|
2020-03-25 10:52:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case "PART":
|
|
|
|
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, ",") {
|
|
|
|
uc, upstreamName, err := dc.unmarshalEntity(name)
|
|
|
|
if err != nil {
|
2020-03-25 22:56:01 +00:00
|
|
|
return err
|
2020-03-25 10:52:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 13:27:41 +00:00
|
|
|
if strings.EqualFold(reason, "detach") {
|
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
|
|
|
ch := uc.network.channels.Value(upstreamName)
|
|
|
|
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 {
|
|
|
|
ch = &Channel{
|
|
|
|
Name: name,
|
|
|
|
Detached: true,
|
|
|
|
}
|
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
|
|
|
uc.network.channels.SetValue(upstreamName, 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 {
|
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
|
|
|
dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
|
2020-04-28 13:27:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
params := []string{upstreamName}
|
|
|
|
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
|
|
|
|
2021-11-08 18:36:10 +00:00
|
|
|
if err := uc.network.deleteChannel(ctx, upstreamName); err != nil {
|
2020-04-28 13:27:41 +00:00
|
|
|
dc.logger.Printf("failed to delete channel %q: %v", upstreamName, err)
|
|
|
|
}
|
2020-03-12 17:33:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-25 22:46:36 +00:00
|
|
|
case "KICK":
|
|
|
|
var channelStr, userStr string
|
|
|
|
if err := parseMessageParams(msg, &channelStr, &userStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
channels := strings.Split(channelStr, ",")
|
|
|
|
users := strings.Split(userStr, ",")
|
|
|
|
|
|
|
|
var reason string
|
|
|
|
if len(msg.Params) > 2 {
|
|
|
|
reason = msg.Params[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(channels) != 1 && len(channels) != len(users) {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_BADCHANMASK,
|
|
|
|
Params: []string{dc.nick, channelStr, "Bad channel mask"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, user := range users {
|
|
|
|
var channel string
|
|
|
|
if len(channels) == 1 {
|
|
|
|
channel = channels[0]
|
|
|
|
} else {
|
|
|
|
channel = channels[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ucChannel != ucUser {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_USERNOTINCHANNEL,
|
2020-08-18 11:54:44 +00:00
|
|
|
Params: []string{dc.nick, user, channel, "They are on another network"},
|
2020-03-25 22:46:36 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
uc := ucChannel
|
|
|
|
|
|
|
|
params := []string{upstreamChannel, upstreamUser}
|
|
|
|
if reason != "" {
|
|
|
|
params = append(params, reason)
|
|
|
|
}
|
2021-12-08 17:03:40 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
2020-03-25 22:46:36 +00:00
|
|
|
Command: "KICK",
|
|
|
|
Params: params,
|
|
|
|
})
|
|
|
|
}
|
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]
|
|
|
|
}
|
|
|
|
|
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 casemapASCII(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,
|
|
|
|
Params: []string{dc.nick, "Cannot change user mode in multi-upstream mode"},
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
uc, upstreamName, err := dc.unmarshalEntity(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !uc.isChannel(upstreamName) {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_USERSDONTMATCH,
|
|
|
|
Params: []string{dc.nick, "Cannot change mode for other users"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
if modeStr != "" {
|
|
|
|
params := []string{upstreamName, modeStr}
|
|
|
|
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 {
|
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
|
|
|
ch := uc.channels.Value(upstreamName)
|
|
|
|
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(),
|
|
|
|
Command: rpl_creationtime,
|
|
|
|
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":
|
|
|
|
var channel string
|
|
|
|
if err := parseMessageParams(msg, &channel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
uc, upstreamName, err := dc.unmarshalEntity(channel)
|
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",
|
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
|
|
|
Params: []string{upstreamName, topic},
|
2020-03-25 23:19:45 +00:00
|
|
|
})
|
|
|
|
} else { // getting topic
|
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
|
|
|
ch := uc.channels.Value(upstreamName)
|
|
|
|
if ch == nil {
|
2020-03-25 23:19:45 +00:00
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_NOSUCHCHANNEL,
|
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
|
|
|
Params: []string{dc.nick, upstreamName, "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":
|
2021-11-09 20:32:26 +00:00
|
|
|
network := dc.network
|
|
|
|
if network == nil && len(msg.Params) > 0 {
|
|
|
|
var err error
|
|
|
|
network, msg.Params[0], err = dc.unmarshalEntityNetwork(msg.Params[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
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
|
|
|
}
|
|
|
|
}
|
2021-11-09 20:32:26 +00:00
|
|
|
if network == nil {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_LISTEND,
|
|
|
|
Params: []string{dc.nick, "LIST without a network suffix is not supported in multi-upstream mode"},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
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
|
|
|
|
2021-11-09 20:32:26 +00:00
|
|
|
uc := network.conn
|
|
|
|
if uc == nil {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_LISTEND,
|
|
|
|
Params: []string{dc.nick, "Disconnected from upstream server"},
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-09 21:09:17 +00:00
|
|
|
uc.enqueueCommand(dc, msg)
|
2020-03-21 00:24:29 +00:00
|
|
|
case "NAMES":
|
|
|
|
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], ",")
|
|
|
|
for _, channel := range channels {
|
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
|
|
|
uc, upstreamName, err := dc.unmarshalEntity(channel)
|
2020-03-21 00:24:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
ch := uc.channels.Value(upstreamName)
|
|
|
|
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",
|
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
|
|
|
Params: []string{upstreamName},
|
2020-03-21 00:24:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-11-02 17:15:45 +00:00
|
|
|
// For WHOX docs, see:
|
|
|
|
// - http://faerion.sourceforge.net/doc/irc/whox.var
|
|
|
|
// - https://github.com/quakenet/snircd/blob/master/doc/readme.who
|
|
|
|
// Note, many features aren't widely implemented, such as flags and mask2
|
2020-03-19 23:23:19 +00:00
|
|
|
case "WHO":
|
|
|
|
if len(msg.Params) == 0 {
|
|
|
|
// TODO: support WHO without parameters
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
|
|
|
optionsParts := strings.SplitN(options, "%", 2)
|
|
|
|
// TODO: add support for WHOX flags in optionsParts[0]
|
|
|
|
var fields, whoxToken string
|
|
|
|
if len(optionsParts) == 2 {
|
|
|
|
optionsParts := strings.SplitN(optionsParts[1], ",", 2)
|
|
|
|
fields = strings.ToLower(optionsParts[0])
|
|
|
|
if len(optionsParts) == 2 && strings.Contains(fields, "t") {
|
|
|
|
whoxToken = optionsParts[1]
|
|
|
|
}
|
|
|
|
}
|
2020-03-19 23:23:19 +00:00
|
|
|
|
2021-11-02 17:15:45 +00:00
|
|
|
// TODO: support mixed bouncer/upstream WHO queries
|
|
|
|
maskCM := casemapASCII(mask)
|
|
|
|
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
|
|
|
}
|
2021-11-02 17:15:45 +00:00
|
|
|
info := whoxInfo{
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
dc.SendMessage(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 {
|
|
|
|
info := whoxInfo{
|
|
|
|
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,
|
|
|
|
Flags: "H*",
|
2021-11-02 17:32:39 +00:00
|
|
|
Account: serviceNick,
|
2021-11-02 17:15:45 +00:00
|
|
|
Realname: serviceRealname,
|
|
|
|
}
|
|
|
|
dc.SendMessage(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
|
|
|
|
}
|
2020-03-21 23:46:56 +00:00
|
|
|
|
2021-11-02 17:15:45 +00:00
|
|
|
// TODO: properly support WHO masks
|
|
|
|
uc, upstreamMask, err := dc.unmarshalEntity(mask)
|
2020-03-19 23:23:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-02 17:15:45 +00:00
|
|
|
params := []string{upstreamMask}
|
|
|
|
if options != "" {
|
|
|
|
params = append(params, options)
|
2020-03-19 23:23:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 21:09:17 +00:00
|
|
|
uc.enqueueCommand(dc, &irc.Message{
|
2020-03-19 23:23:19 +00:00
|
|
|
Command: "WHO",
|
|
|
|
Params: params,
|
|
|
|
})
|
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"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
var target, mask string
|
|
|
|
if len(msg.Params) == 1 {
|
|
|
|
target = ""
|
|
|
|
mask = msg.Params[0]
|
|
|
|
} else {
|
|
|
|
target = msg.Params[0]
|
|
|
|
mask = msg.Params[1]
|
|
|
|
}
|
|
|
|
// TODO: support multiple WHOIS users
|
|
|
|
if i := strings.IndexByte(mask, ','); i >= 0 {
|
|
|
|
mask = mask[:i]
|
|
|
|
}
|
|
|
|
|
2021-05-20 09:13:14 +00:00
|
|
|
if dc.network == nil && casemapASCII(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(),
|
|
|
|
Command: rpl_whoisaccount,
|
|
|
|
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
|
|
|
|
}
|
2021-10-06 09:50:12 +00:00
|
|
|
if casemapASCII(mask) == serviceNickCM {
|
|
|
|
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(),
|
|
|
|
Command: rpl_whoisaccount,
|
|
|
|
Params: []string{dc.nick, serviceNick, serviceNick, "is logged in as"},
|
|
|
|
})
|
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
|
|
|
|
2020-03-20 01:15:23 +00:00
|
|
|
// TODO: support WHOIS masks
|
|
|
|
uc, upstreamNick, err := dc.unmarshalEntity(mask)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var params []string
|
|
|
|
if target != "" {
|
2020-05-11 21:48:07 +00:00
|
|
|
if target == mask { // WHOIS nick nick
|
|
|
|
params = []string{upstreamNick, upstreamNick}
|
|
|
|
} else {
|
|
|
|
params = []string{target, upstreamNick}
|
|
|
|
}
|
2020-03-20 01:15:23 +00:00
|
|
|
} else {
|
|
|
|
params = []string{upstreamNick}
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:03:40 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
2020-03-20 01:15:23 +00:00
|
|
|
Command: "WHOIS",
|
|
|
|
Params: params,
|
|
|
|
})
|
2021-06-23 16:17:30 +00:00
|
|
|
case "PRIVMSG", "NOTICE":
|
2020-02-17 14:56:18 +00:00
|
|
|
var targetsStr, text string
|
|
|
|
if err := parseMessageParams(msg, &targetsStr, &text); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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, ",") {
|
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()
|
|
|
|
broadcastTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
|
|
|
broadcastMsg := &irc.Message{
|
|
|
|
Tags: broadcastTags,
|
|
|
|
Prefix: servicePrefix,
|
|
|
|
Command: msg.Command,
|
|
|
|
Params: []string{name, text},
|
|
|
|
}
|
|
|
|
dc.srv.forEachUser(func(u *user) {
|
|
|
|
u.events <- eventBroadcast{broadcastMsg}
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-24 10:45:16 +00:00
|
|
|
if dc.network == nil && casemapASCII(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,
|
|
|
|
Params: []string{name, text},
|
|
|
|
})
|
2021-05-24 10:45:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-23 16:17:30 +00:00
|
|
|
if msg.Command == "PRIVMSG" && casemapASCII(name) == serviceNickCM {
|
2020-11-23 16:14:42 +00:00
|
|
|
if dc.caps["echo-message"] {
|
|
|
|
echoTags := tags.Copy()
|
|
|
|
echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Tags: echoTags,
|
|
|
|
Prefix: dc.prefix(),
|
2021-06-23 16:17:30 +00:00
|
|
|
Command: msg.Command,
|
2020-11-23 16:14:42 +00:00
|
|
|
Params: []string{name, text},
|
|
|
|
})
|
|
|
|
}
|
2021-11-08 18:40:30 +00:00
|
|
|
handleServicePRIVMSG(ctx, dc, text)
|
2020-03-18 11:23:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-19 23:23:19 +00:00
|
|
|
uc, upstreamName, err := dc.unmarshalEntity(name)
|
2020-02-17 14:56:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-23 16:17:30 +00:00
|
|
|
if msg.Command == "PRIVMSG" && uc.network.casemap(upstreamName) == "nickserv" {
|
2021-11-08 17:11:24 +00:00
|
|
|
dc.handleNickServPRIVMSG(ctx, uc, text)
|
2020-03-13 14:12:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 20:54:45 +00:00
|
|
|
unmarshaledText := text
|
|
|
|
if uc.isChannel(upstreamName) {
|
|
|
|
unmarshaledText = dc.unmarshalText(uc, text)
|
|
|
|
}
|
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,
|
2021-06-23 16:17:30 +00:00
|
|
|
Command: msg.Command,
|
2020-04-21 20:54:45 +00:00
|
|
|
Params: []string{upstreamName, unmarshaledText},
|
2020-02-17 15:17:31 +00:00
|
|
|
})
|
2020-03-16 13:28:45 +00:00
|
|
|
|
2020-05-21 05:04:34 +00:00
|
|
|
echoTags := tags.Copy()
|
|
|
|
echoTags["time"] = irc.TagValue(time.Now().UTC().Format(serverTimeLayout))
|
2021-06-14 19:44:38 +00:00
|
|
|
if uc.account != "" {
|
|
|
|
echoTags["account"] = irc.TagValue(uc.account)
|
|
|
|
}
|
2020-03-17 15:15:54 +00:00
|
|
|
echoMsg := &irc.Message{
|
2021-11-15 22:53:25 +00:00
|
|
|
Tags: echoTags,
|
|
|
|
Prefix: &irc.Prefix{Name: uc.nick},
|
2021-06-23 16:17:30 +00:00
|
|
|
Command: msg.Command,
|
2020-03-17 15:15:54 +00:00
|
|
|
Params: []string{upstreamName, text},
|
|
|
|
}
|
2020-04-06 19:42:55 +00:00
|
|
|
uc.produce(upstreamName, echoMsg, dc)
|
Add customizable auto-detaching, auto-reattaching, relaying.
This uses the fields added previously to the Channel struct to implement
the actual detaching/reattaching/relaying logic.
The `FilterDefault` values of the messages filters are currently
hardcoded.
The values of the message filters are not currently user-settable.
This introduces a new user event, eventChannelDetach, which stores an
upstreamConn (which might become invalid at the time of processing), and
a channel name, used for auto-detaching. Every time the channel detach
timer is refreshed (by receveing a message, etc.), a new timer is
created on the upstreamChannel, which will dispatch this event after the
duration (and discards the previous timer, if any).
2020-11-30 21:08:33 +00:00
|
|
|
|
|
|
|
uc.updateChannelAutoDetach(upstreamName)
|
2020-03-26 05:20:28 +00:00
|
|
|
}
|
2020-05-21 05:04:34 +00:00
|
|
|
case "TAGMSG":
|
|
|
|
var targetsStr string
|
|
|
|
if err := parseMessageParams(msg, &targetsStr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tags := copyClientTags(msg.Tags)
|
|
|
|
|
|
|
|
for _, name := range strings.Split(targetsStr, ",") {
|
2021-10-09 10:46:01 +00:00
|
|
|
if dc.network == nil && casemapASCII(name) == dc.nickCM {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Tags: msg.Tags.Copy(),
|
|
|
|
Prefix: dc.prefix(),
|
|
|
|
Command: "TAGMSG",
|
|
|
|
Params: []string{name},
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-09 10:46:00 +00:00
|
|
|
if casemapASCII(name) == serviceNickCM {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-05-21 05:04:34 +00:00
|
|
|
uc, upstreamName, err := dc.unmarshalEntity(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-20 10:19:51 +00:00
|
|
|
if _, ok := uc.caps["message-tags"]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
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,
|
|
|
|
Command: "TAGMSG",
|
|
|
|
Params: []string{upstreamName},
|
|
|
|
})
|
Add customizable auto-detaching, auto-reattaching, relaying.
This uses the fields added previously to the Channel struct to implement
the actual detaching/reattaching/relaying logic.
The `FilterDefault` values of the messages filters are currently
hardcoded.
The values of the message filters are not currently user-settable.
This introduces a new user event, eventChannelDetach, which stores an
upstreamConn (which might become invalid at the time of processing), and
a channel name, used for auto-detaching. Every time the channel detach
timer is refreshed (by receveing a message, etc.), a new timer is
created on the upstreamChannel, which will dispatch this event after the
duration (and discards the previous timer, if any).
2020-11-30 21:08:33 +00:00
|
|
|
|
|
|
|
uc.updateChannelAutoDetach(upstreamName)
|
2020-05-21 05:04:34 +00:00
|
|
|
}
|
2020-03-26 05:03:07 +00:00
|
|
|
case "INVITE":
|
|
|
|
var user, channel string
|
|
|
|
if err := parseMessageParams(msg, &user, &channel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ucChannel, upstreamChannel, err := dc.unmarshalEntity(channel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ucUser, upstreamUser, err := dc.unmarshalEntity(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ucChannel != ucUser {
|
|
|
|
return ircError{&irc.Message{
|
|
|
|
Command: irc.ERR_USERNOTINCHANNEL,
|
2020-08-20 06:48:16 +00:00
|
|
|
Params: []string{dc.nick, user, channel, "They are on another network"},
|
2020-03-26 05:03:07 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
uc := ucChannel
|
|
|
|
|
2021-12-08 17:03:40 +00:00
|
|
|
uc.SendMessageLabeled(ctx, dc.id, &irc.Message{
|
2020-03-26 05:03:07 +00:00
|
|
|
Command: "INVITE",
|
|
|
|
Params: []string{upstreamUser, upstreamChannel},
|
|
|
|
})
|
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()
|
|
|
|
if uc == nil || !uc.caps["sasl"] {
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
uc.logger.Printf("starting post-registration SASL PLAIN authentication with username %q", credentials.plainUsername)
|
|
|
|
uc.saslClient = sasl.NewPlainClient("", credentials.plainUsername, credentials.plainPassword)
|
|
|
|
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()
|
|
|
|
if uc == nil || !uc.caps["draft/account-registration"] {
|
|
|
|
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)
|
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
|
|
|
|
if len(dc.monitored.innerMap) >= 1000 {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.ERR_MONLISTFULL,
|
|
|
|
Params: []string{dc.nick, "1000", target, "Bouncer monitor list is full"},
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.monitored.SetValue(target, nil)
|
|
|
|
|
|
|
|
if uc.monitored.Has(target) {
|
|
|
|
cmd := irc.RPL_MONOFFLINE
|
|
|
|
if online := uc.monitored.Value(target); online {
|
|
|
|
cmd = irc.RPL_MONONLINE
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: cmd,
|
|
|
|
Params: []string{dc.nick, target},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dc.monitored.Delete(target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uc.updateMonitor()
|
|
|
|
case "C": // clear
|
|
|
|
dc.monitored = newCasemapMap(0)
|
|
|
|
uc.updateMonitor()
|
|
|
|
case "L": // list
|
|
|
|
// TODO: be less lazy and pack the list
|
|
|
|
for _, entry := range dc.monitored.innerMap {
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: irc.RPL_MONLIST,
|
|
|
|
Params: []string{dc.nick, entry.originalKey},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
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
|
|
|
|
for _, entry := range dc.monitored.innerMap {
|
|
|
|
target := entry.originalKey
|
|
|
|
|
|
|
|
cmd := irc.RPL_MONOFFLINE
|
|
|
|
if online := uc.monitored.Value(target); online {
|
|
|
|
cmd = irc.RPL_MONONLINE
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.SendMessage(&irc.Message{
|
|
|
|
Prefix: dc.srv.prefix(),
|
|
|
|
Command: cmd,
|
|
|
|
Params: []string{dc.nick, 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
|
|
|
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.
|
|
|
|
dc.SendBatch("draft/chathistory-targets", nil, nil, func(batchRef irc.TagValue) {})
|
|
|
|
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
|
|
|
|
if casemapASCII(target) == serviceNickCM {
|
|
|
|
dc.SendBatch("chathistory", []string{target}, nil, func(batchRef irc.TagValue) {})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 16:17:35 +00:00
|
|
|
store, ok := dc.user.msgStore.(chatHistoryMessageStore)
|
|
|
|
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,
|
2021-03-04 15:04:30 +00:00
|
|
|
Params: []string{dc.nick, "CHATHISTORY", "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:25:38 +00:00
|
|
|
network, entity, err := dc.unmarshalEntityNetwork(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
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-13 09:25:38 +00:00
|
|
|
entity = network.casemap(entity)
|
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] == "*" {
|
2021-11-19 10:55:22 +00:00
|
|
|
bounds[0] = time.Now()
|
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
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2021-10-08 22:13:16 +00:00
|
|
|
eventPlayback := dc.caps["draft/event-playback"]
|
|
|
|
|
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 {
|
2021-11-18 19:41:27 +00:00
|
|
|
case "BEFORE", "LATEST":
|
2021-11-03 17:18:04 +00:00
|
|
|
history, err = store.LoadBeforeTime(ctx, &network.Network, entity, bounds[0], time.Time{}, limit, eventPlayback)
|
2020-07-15 15:47:57 +00:00
|
|
|
case "AFTER":
|
2021-11-03 17:18:04 +00:00
|
|
|
history, err = store.LoadAfterTime(ctx, &network.Network, entity, bounds[0], time.Now(), limit, eventPlayback)
|
2021-03-27 12:08:31 +00:00
|
|
|
case "BETWEEN":
|
|
|
|
if bounds[0].Before(bounds[1]) {
|
2021-11-03 17:18:04 +00:00
|
|
|
history, err = store.LoadAfterTime(ctx, &network.Network, entity, bounds[0], bounds[1], limit, eventPlayback)
|
2021-03-27 12:08:31 +00:00
|
|
|
} else {
|
2021-11-03 17:18:04 +00:00
|
|
|
history, err = store.LoadBeforeTime(ctx, &network.Network, entity, bounds[0], bounds[1], limit, eventPlayback)
|
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"},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2021-06-05 10:38:52 +00:00
|
|
|
dc.SendBatch("draft/chathistory-targets", nil, nil, func(batchRef irc.TagValue) {
|
|
|
|
for _, target := range targets {
|
2021-09-13 09:25:38 +00:00
|
|
|
if ch := network.channels.Value(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",
|
|
|
|
Params: []string{"TARGETS", target.Name, target.LatestMessage.UTC().Format(serverTimeLayout)},
|
|
|
|
})
|
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)
|
|
|
|
}
|
|
|
|
|
2021-06-05 10:38:52 +00:00
|
|
|
dc.SendBatch("chathistory", []string{target}, nil, func(batchRef irc.TagValue) {
|
|
|
|
for _, msg := range history {
|
|
|
|
msg.Tags["batch"] = batchRef
|
2021-09-13 09:25:38 +00:00
|
|
|
dc.SendMessage(dc.marshalMessage(msg, network))
|
2021-06-05 10:38:52 +00:00
|
|
|
}
|
2020-08-11 13:58:50 +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":
|
2021-06-05 10:38:52 +00:00
|
|
|
dc.SendBatch("soju.im/bouncer-networks", nil, nil, func(batchRef irc.TagValue) {
|
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)
|
|
|
|
|
2021-10-29 13:51:13 +00:00
|
|
|
record := &Network{Nick: dc.nick, Enabled: true}
|
|
|
|
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"},
|
|
|
|
}}
|
|
|
|
}
|
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
|
|
|
|
uc := dc.upstream()
|
|
|
|
if uc == nil {
|
|
|
|
return newUnknownCommandError(msg.Command)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|