Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for some kind of slow mode + regex filter to allow certain words to be banned? #2179

Open
scottc943 opened this issue Jul 29, 2024 · 3 comments

Comments

@scottc943
Copy link

Is there a way to do this? I don't mind to patch it in myself if needed, but I'm trying to find the main function that handles channel messages (I believe in handlers.go or services.go there's a privmsg function that only handles DMs iirc). I don't have the code pulled up where I can see it right now.

Basically I'm hosting an irc server with ergo that is getting hit with trolls and I'm wanting to lock it down and ban some very offensive words. If I could force a "slow mode" similar to discords slow mode that would be ideal as well.

@slingamn
Copy link
Member

Sorry to hear about the moderation issues. Personally, I've found that setting channels +R (only registered users can join) combined with liberal use of the +I list (exempting known-good unregistered users by nickmask) is effective against trolls. (Spamming new registrations is typically mitigated by the account registration throttle, but if it gets bad you can temporarily disable new registrations.)

A robust "slowmode" equivalent is a good idea; we don't have one now but I opened #2180 to investigate it. In the meantime you can try setting more aggressive fakelag settings, although these will penalize all traffic on the server, not just channel messages:

ergo/default.yaml

Lines 899 to 924 in 30f47a9

# fakelag: prevents clients from spamming commands too rapidly
fakelag:
# whether to enforce fakelag
enabled: true
# time unit for counting command rates
window: 1s
# clients can send this many commands without fakelag being imposed
burst-limit: 5
# once clients have exceeded their burst allowance, they can send only
# this many commands per `window`:
messages-per-window: 2
# client status resets to the default state if they go this long without
# sending any commands:
cooldown: 2s
# exempt a certain number of command invocations per session from fakelag;
# this is to speed up "resynchronization" of client state during reattach
command-budgets:
"CHATHISTORY": 16
"MARKREAD": 16
"MONITOR": 1
"WHO": 4

If you want to patch in a regex filter, I think this is the best place (compare the handling of modes.NoCTCP in this function):

ergo/irc/channel.go

Lines 1298 to 1317 in 30f47a9

func (channel *Channel) SendSplitMessage(command string, minPrefixMode modes.Mode, clientOnlyTags map[string]string, client *Client, message utils.SplitMessage, rb *ResponseBuffer) {
histType, err := msgCommandToHistType(command)
if err != nil {
return
}
if canSpeak, mode := channel.CanSpeak(client); !canSpeak {
if histType != history.Notice {
rb.Add(nil, client.server.name, ERR_CANNOTSENDTOCHAN, client.Nick(), channel.Name(), fmt.Sprintf(client.t("Cannot send to channel (+%s)"), mode))
}
return
}
isCTCP := message.IsRestrictedCTCPMessage()
if isCTCP && channel.flags.HasMode(modes.NoCTCP) {
if histType != history.Notice {
rb.Add(nil, client.server.name, ERR_CANNOTSENDTOCHAN, client.Nick(), channel.Name(), fmt.Sprintf(client.t("Cannot send to channel (+%s)"), "C"))
}
return
}

You can apply the regexes to message.Message here.

@wlinator
Copy link

For anyone that wants to implement this in the future, I have created a simple automod system with configurable automod rules.

Add this to the SendSplitMessage func in ergo/irc/channel.go:

	// Check if automod is enabled
	if channel.server.Config().Automod.Enabled {
		// Check message against automod rules
		for _, rule := range channel.server.Config().Automod.Rules {
			if rule.RegexString != "" {
				re := regexp.MustCompile(rule.RegexString)
				if re.MatchString(message.Message) {
					rb.Add(nil, client.server.name, ERR_CANNOTSENDTOCHAN, client.Nick(), channel.Name(), fmt.Sprintf(client.t("Message blocked by rule: %s"), rule.Name))
					return
				}
			}
			for _, word := range rule.BlockedWords {
				if strings.Contains(message.Message, word) {
					rb.Add(nil, client.server.name, ERR_CANNOTSENDTOCHAN, client.Nick(), channel.Name(), fmt.Sprintf(client.t("Message blocked by rule: %s"), rule.Name))
					return
				}
			}
		}
	}

Update ergo/irc/config.go:

type AutomodRule struct {
	Name         string   `yaml:"name"`
	RegexString  string   `yaml:"regex_string"`
	BlockedWords []string `yaml:"blocked_words"`
}

type AutomodConfig struct {
	Enabled bool          `yaml:"enabled"`
	Rules   []AutomodRule `yaml:"rules"`
}

type Config struct {
	...
	Automod AutomodConfig `yaml:"automod"`
}

Finally, you can configure your config file (ircd.yaml) to add as many automod rules as you like. Here's a (silly) example:

automod:
    enabled: true
    rules:
        - name: "Don't talk about food."
          regex_string: "\bham\b"
          blocked_words: ["pizza", "cheese"]

        - name: "Don't say this."
          blocked_words: ["milk"]

        - name: "This content is definitely forbidden."
          regex_string: "(?i)\\b(almonds|beans)\\b"

If an automod rule is triggered, the user will see which rule was broken.

[09:07] <wlinator> pizza
[09:07] [404] wlinator #test Message blocked by rule: Don't talk about food.

@slingamn
Copy link
Member

@wlinator I'm not sure if you were the person who asked in #ergo about this, but if you want to exempt users with an operator capability from this check, operator capabilities are just freeform strings written in the config and tested in the code. See nofakelag for example:

ergo/default.yaml

Lines 687 to 690 in 1bdc45e

capabilities:
- "kill" # disconnect user sessions
- "ban" # ban IPs, CIDRs, NUH masks, and suspend accounts (UBAN / DLINE / KLINE)
- "nofakelag" # exempted from "fakelag" restrictions on rate of message sending

flc.Enabled = flc.Enabled && !session.client.HasRoleCapabs("nofakelag")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants