IF (WORD(2 $LOADINFO()) != [pf]) { LOAD -pf $WORD(1 $LOADINFO()); RETURN; };

package massmode;

# MASSMODE [channel] <+-><mode> <nick> [<nick> ...]
#
# Sets or clear <mode> on the given nicks in [channel], or $C if [channel]
# isn't specified.  Will set as many modes at once as the server 005 indicates
# it supports.  If 005 isn't present, it sets 3 modes at once.
ALIAS MASSMODE {
	@ :CHAN = C, REST = [$*];
	IF (ISCHANNEL($WORD(0 $REST))) {
		@ :CHAN = WORD(0 $REST);
		SHIFT REST;
	};

	@ :MODE = WORD(0 $REST);
	SHIFT REST;

	IF (!MODE || STRLEN($MODE) != 2) {
		XECHO -b Usage: ${K}MASSMODE [channel] <+-><mode> <nick> [<nick> ...];
		RETURN;
	};

	@ :WAY = LEFT(1 $MODE);
	@ :NICKS = REST;
	@ :NMODES = SERVERCTL(GET $SERVERCTL(FROM_SERVER) 005 MODES);
	IF (!NMODES) {
		@ :NMODES = 3;
	};
	
	@ :TEMP = [];
	FOR NICK IN ($NICKS) {
		IF (NUMWORDS($TEMP) == NMODES) {
			MODE $CHAN $WAY$REPEAT($NMODES $RIGHT(1 $MODE)) $TEMP;
			@ :TEMP = [];
		};

		UNSHIFT TEMP $NICK;
	};
		
	if (TEMP) {
		MODE $CHAN $WAY$REPEAT($NUMWORDS($TEMP) $RIGHT(1 $MODE)) $TEMP;
	};
};

ALIAS CHANVOICES { RETURN $STRIP(.!&*@+? $PATTERN(%+% $CHANNEL($0))) };
ALIAS NOCHANVOICES { RETURN $STRIP(.!&*@+? $FILTER(*+* $CHANNEL($0))) };

ALIAS OP { MASSMODE +o $* };
ALIAS VOICE { MASSMODE +v $* };
#ALIAS DEOP { MASSMODE -o $* };
ALIAS DEVOICE { MASSMODE -v $* };
ALIAS MOP (CHAN DEFAULT $C) { MASSMODE $CHAN +o $NOCHOPS($CHAN) };
ALIAS MDEOP (CHAN DEFAULT $C) { MASSMODE $CHAN -o $NOCHOPS($CHAN) };
ALIAS MVOICE (CHAN DEFAULT $C) { MASSMODE $CHAN +v $NOCHANVOICES($CHAN) };
ALIAS MDEVOICE (CHAN DEFAULT $C) { MASSMODE $CHAN -v $NOCHANVOICES($CHAN) };
