if (word(2 $loadinfo()) != [pf]) { load -pf $word(1 $loadinfo()); return; };

package capctl;

## Capability negotiation script for EPIC6.
## Written by zlonix@efnet, public domain.
##
## Version: 1.0 (September, 2022)
##  - Initial roll-out
## Version: 2.0 (2025)
##  - Support for /on cap
##
## This script allows you to specify which CAPs you'd like to have turned on
## when they are available, using server name wildcards.  The client handles
## the syntax part of CAP -- all we do is tell it what we want turned on.
##
## Be careful about which capabilities you enable, because it will change the
## way server send you messages. Capabilities which use message-tags are safe to
## turn on, you can get them with built-in function $tags(), which you can check
## in your hooks, it works the same way as $userhost() does.
##
## Usage:
##	capctl req *.libera.chat account-tag draft/example-1 draft/example-2
##
## This will request three capabilities upon connect to a server matching
## *.libera.chat.

alias capctl (verb, server, caps) {
	if (@verb && @server && @caps) {
		@ :server_enc = encode($server);
		@ push(cap.servers $server);
		@ cap[$server_enc][$verb] = caps;
	};
};

on ^cap "% LS *" {
	if ((:idx = rmatch($0 $cap.servers)) == 0) {
		echo rmatch $0 $cap.servers returned 0;
		if ((idx = rmatch($servername() $cap.servers)) == 0) {
			return;
		};
	};

	@idx--;
	@ :server_enc = encode($word($idx $cap.servers));
	if ([$2] in cap[$server_enc][req]) {
		quote CAP REQ :$2;
	};
};

on ^cap "% ACK *" {
	xecho -b CAP $2 has been turned on for this server!;
};

on ^cap "% NACK *" {
	xecho -b CAP $2 was rejected by this server!;
};

