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

#
# Autoget!  Because people keep asking me for this.
# Ok.  Here's the plan.
# /autoget <pattern> [<pattern> ....] will auto-get any file that matches
#	any of the patterns.
# /autoget -<pattern>  will stop autogetting any file that matches this
#	pattern.
# You can mix and match <pattern>s and -<pattern>s.
#
# New:  Patterns will now be matched against nick!user@host,IP,file.
#       This shouldn't really be incompatible unless you autoget
#       files begining with specific names.
# New:  /autoreget acts much the same as /autoget, but the patterns apply
#       to requests for which the file already exists.
#       /autoclobber takes two arguments specifying the reget modes.
#       These can be one of GET, RESUME, RENAME.  For RENAME,
#       a random string will be appended to the filename.
# Note: All commas in the filename are replaced with dots for the purpose
#       of matching to make it harder for people to accidentally or
#       purposely bypass well written masks by sending you specific
#       filenames.  Nevertheless, this isn't really a safe authentication
#       method.
#
# Note: If the sender (an fserv perhaps) sends elaborate messages before
#       the transfer begins and NO_CTCP_FLOOD is on (it is on by
#       default), the ctcp flood protection mechanism can be triggered
#       and the transfer can fail to start.  It may be better for you
#       to turn this feature off and make other flood protection
#       arrangements.
#

package autoget;

# Take possession of these variables while not clobbering them.
fe (patterns.get * patterns.reget * reget.part resume reget.full close) var val {
	@ autoget[$var] = autoget.$var ? autoget.$var : [$val];
};

on #-dcc_request 414 "% SEND *" {
	@ :path = [$getset(dcc_store_path)/$urldecode($2)];
	if ([$6] <= fsize("$path")) {
		@ :patterns = autoget.patterns.reget;
		@ :action = autoget.reget.full;
	} elsif (0 < fsize("$path")) {
		@ :patterns = autoget.patterns.reget;
		@ :action = autoget.reget.part;
	} else {
		@ :patterns = autoget.patterns.get;
		@ :action = [get];
	};
	if (rmatch($0!$userhost(),$3,$tr(/,/./$2) $autoget.patterns.reject)) {
		# Nothing.
	} elsif (rmatch($0!$userhost(),$3,$tr(/,/./$2) $patterns)) {
		if (action == []) {
			return;
		} elsif (action == [rename]) {
			@ :extra = 0;
			do {
				@ extra += rand(10);
			} while (0 < fexist("$path~$extra"));
			@ rename("$path" "$path~$extra");
			@ action = [get];
		} elsif (action == [close]) {
			@ action = [close get];
		} elsif (!rmatch("$action" "get" "resume")) {
			echo $action?;
			return;
		};
		defer dcc $action $0 $2;
	};
};

stack push alias alias.x;
alias alias.x {
	fe (get reget reject) cmd {
		//alias $sar(g/XXX/$cmd/$*);
	};
};

alias.x autoXXX {
	if (strlen($*) == 0) {
		echo AutoXXX n!u@h,IP,files matching [$autoget.XXXpatterns];
		return;
	};
	fe ($*) x {
		if (left(1 $x) == [-]) {
			@ x = rest(1 $x);
			@ autoget.XXXpatterns = remw($x $autoget.XXXpatterns);
		} else {
			push autoget.XXXpatterns $x;
		};
	};
};
stack pop alias alias.x;

alias autoclobber (args) {
	@ :types = [part full];
	if (#args) {
		fe ($args) arg {
			if (!types) {
				break;
			} elsif (!arg) {
				@ shift(types);
			} elsif (rmatch("$arg" get rename resume close)) {
				@ autoget[reget][$shift(types)] = arg;
			} else {
				echo $arg isn't one of: GET RENAME RESUME CLOSE;
			};
		};
	} else {
		echo Usage: /autoclobber [partial-mode] [full-mode];
		fe ($types) type {
			echo Autoclobber $type mode is: $autoget[reget][$type];
		};
	};
};

on ^unload "autoget" {
	^assign -autoget.patterns.get;
	^assign -autoget.patterns.reget;
	^assign -autoget.patterns.reject;
	^assign -autoget.reget.part;
	^assign -autoget.reget.full;
	^alias -autoget;
	^alias -autoreget;
	^alias -autoreject;
	^alias -autoclobber;
	^on #dcc_request 414 -"% SEND *";
};

if ( getset(mirc_broken_dcc_resume) == [off] ) {
	echo Warning: Turning mIRC DCC resume on;
	set mirc_broken_dcc_resume on;
};

if ( getset(no_ctcp_flood) != [off] ) {
	echo Warning: You may need to enter "/set no_ctcp_flood off" to autoget files from certain hosts.;
};

