Synopsis

$CLICRAFT_CONFIG/action.d/action.sh, /usr/local/libexec/clicraft/action.d/action.sh

Description

clicraft(1) provides a way to extend functionality through action scripts, which are bash scripts that get called by clicraft in a modified environment. To create an action script, simply set your EDITOR variable and use clicraft’s edit action. For instance:

EDITOR=nano clicraft edit foo

An action script created in this way will get executed when running clicraft foo [args].

Alternately, you can accomplish the same thing by writing a script with filename foo.sh and placing it in $CLICRAFT_CONFIG/action.d. See clicraft.conf(5) for a description of $CLICRAFT_CONFIG.

There are also action scripts in $EXECDIR/action.d, but these are system action scripts and may be overwritten when updating clicraft. They can be overridden by creating new action files in the manner described above.

Action scripts get sourced from within clicraft using something similar to . action.sh. They therefore have access to all of clicraft’s internal variables and functions, described below.

Variables

$1, $2, $3, … $9

Clicraft’s positional parameters are available, but are shifted once to the left. Thus, when an action script foo.sh is invoked with clicraft foo bar baz, $1 is "bar" and $2 is "baz".

ACTION

The name of this action. For an action script foo.sh invoked with clicraft foo bar baz, $ACTION is "foo".

MULTIPLEXER, SERVER_DIR, SERVER_NAME, SERVER_JAR, SERVER_URL, SERVER_LOG
SERVER_VERSION, SERVER_TYPE, START_COMMAND, STOP_CMD, DOWNLOAD_COMMAND
TIMEOUT, START_TIMEOUT, STOP_TIMEOUT, CMD_TIMEOUT, CMD_LSTRIP, REDB

Configuration options are stored in variables by the same name as the option. For a description of these options and their default values, see clicraft.conf(5).

In addition to these variables, any extra user-defined options in clicraft.conf(5) will be set in the environment. Unlike these variables, however, they are not guaranteed to be set and will not have a default value, so care must be taken to ensure that no undesirable behavior occurs if they are not set in clicraft.conf.

RE_TIMESTAMP, RE_IPADDR, RE_START, …

Clicraft exposes variables for each key-value pair in the regex database. The name of each variable is the uppercase version of the key prefixed with RE_ and with special characters replaced with underscores. The value of the variable is set to the value of the key in the database, with any leading occurences of "^^" replaced with the value of the timestamp database entry.

BINDIR=/usr/local/bin

The directory where the clicraft script is stored. If you need to refer to the clicraft script directly, you should use $BINDIR/clicraft instead of $0, since the latter will not work if clicraft was called using a relative path (e.g. ./bin/clicraft).

In addition, the action() function should be preferred over calling clicraft directly (e.g. action foo over $BINDIR/clicraft foo).

CLICRAFT_CONFIG

The directory where clicraft stores its configuration files. This is where clicraft.conf(5) is located. User action scripts are located in this directory under $CLICRAFT_CONFIG/action.d. See clicraft.conf(5) for a description of how the value of $CLICRAFT_CONFIG is determined.

CONFDIR=/usr/local/etc/clicraft

The default value for $CLICRAFT_CONFIG.

EXECDIR=/usr/local/libexec/clicraft

The directory for scripts used internally by clicraft. System action scripts are located in this directory under $EXECDIR/action.d.

Warning In most circumstances, files under this directory should not be removed or modified, and no new files should be created.
VERSION

Clicraft’s version number.

TRAPSTACK

An array of commands to be run if clicraft exits abnormally. This variable should not be directly manipulated.

Functions

msg <message> [argument] …

Print a formatted informational message to stdout. message is a format string composed of plain characters, character escape sequences, and format specifications for additional arguments, as described for printf under the SHELL BUILTIN COMMANDS section of bash(1).

warn <message> [argument] …

Print a formatted warning message to stderr. message is a format string composed of plain characters, character escape sequences, and format specifications for additional arguments, as described for printf under the SHELL BUILTIN COMMANDS section of bash(1).

err <message> [argument] …

Print a formatted error message to stderr. message is a format string composed of plain characters, character escape sequences, and format specifications for additional arguments, as described for printf under the SHELL BUILTIN COMMANDS section of bash(1).

actionfile <action>

Print the location of the action script for action. If a system action script and user action script share the same name, print the value of the user action script. If no action script can be found, print an error to stderr and return nonzero.

Use this to find action scripts in a system-agnostic way. The full path to an action script depends on the settings of --prefix, --sysconfdir, and/or --libexecdir at install time, and so varies from system to system. Because of this, it is preferred to use this function (or its local_ or system_ variants) in action scripts you plan to publish.

local_actionfile <action>

Like action, but will only print the location of a user action.

sys_actionfile <action>

Like action, but will only print the location of a system action.

action <action> [args]

Execute the action script for action with arguments args. If a system action and user action share the same name, run the user action script. If an action script cannot be found for action, print an error to stderr and return nonzero.

Using this function is similar to running . action.sh [args].

local_action <action> [args]

Like action, but will only execute a user action.

sys_action <action> [args]

Like action, but will only execute a system action.

actions

Print a list of available actions to stdout.

status

Return zero if the server is running, or nonzero otherwise.

cmd <command>

Sends command to the server.

usage <action>

Print the usage message for action to stdout.

redb_lookup <key> [prefix]

Look up key in the regex database and print the corresponding value to stdout. If the value of key has two leading start-of-line characters (^^) and prefix is given, insert prefix in place of the second ^ before printing. If a regex for key does not exist, print an error message to stderr and return nonzero.

The location of the regex database is configured by the REDB option in clicraft.conf(5).

redb_insert <key> <value>

Insert a new key and value into the regex database. If a regex for key already exists, print an error message to stderr and return nonzero.

The location of the regex database is configured by the REDB option in clicraft.conf(5).

redb_update <key> <value>

Change the value for key to value in the regex database. If a regex for key does not exist, print an error message to stderr and return nonzero.

The location of the regex database is configured by the REDB option in clicraft.conf(5).

redb_delete <key>

Delete the regex for key from the regex database. If a regex for key does not exist, print an error message to stderr and return nonzero.

The location of the regex database is configured by the REDB option in clicraft.conf(5).

save-off

If another process holds a "save" lock, do nothing and return nonzero. Otherwise, run /save-off on the server and wait for it to finish.

save-on

If another process holds a "save" lock, do nothing and return nonzero. Otherwise, run /save-on on the server and wait for it to finish.

save-all

If another process holds a "save" lock, do nothing and return nonzero. Otherwise, run /save-all on the server and wait for it to finish.

list

Print a space-delimited list of logged in players to stdout.

kick <player>

Kick player from the server. Returns nonzero if the kick was unsuccessful.

ban <target>

Ban target from the server. target can be a player or ip address.

pardon <target>

Pardon banned target on the server. target can be a player or ip address. Returns nonzero if the pardon was unsuccessful.

str2var <string> [case]

Prints a variable name corresponding to string to stdout. Special characters in string not suitable for variable names are changed to underscores (_). If case is "upper" or "lower", alphabetical characters are changed to the respective case.

str2val <string> [case]

Prints the value of the variable corresponding to string to stdout. The variable to use is determined using `str2var string [case]\`.

dimdir <dimension>

Print the directory for dimension to stdout.

If level-name=world in server.properties, `dimdir overworld\` will output SERVER_DIR/world. If SERVER_TYPE is set to "minecraft", `dimdir nether\` and `dimdir end\` will output SERVER_DIR/world/DIM-1 and SERVER_DIR/world/DIM1, respectively, or SERVER_DIR/world_nether and SERVER_DIR/world_the_end for other values of SERVER_TYPE. For other values of dimension, `dimdir foo\` will output SERVER_DIR/world_foo.

Returns nonzero if level-name can’t be found in server.properties.

This function might be better understood by examining example output from an installation where level-name=world in server.properties and SERVER_DIR is set to /srv in clicraft.conf(5):

$ dimdir overworld
/srv/world

$ SERVER_TYPE=minecraft dimdir nether
/srv/world/DIM-1

$ SERVER_TYPE=minecraft dimdir end
/srv/world/DIM1

$ SERVER_TYPE=bukkit dimdir nether
/srv/world_nether

$ SERVER_TYPE=bukkit dimdir end
/srv/world_the_end

$ dimdir foo
/srv/world_foo
serverprop <property>

Print the value of property in server.properties to stdout. If the property cannot be found, print an error message to stderr and return nonzero.

wickedwhich <prog> [progs …]

Search for executable prog in PATH, HOME/bin, and SERVER_DIR. If prog can’t be found, search for any additional progs in the same way. If none can be found, return nonzero.

exithandler

Run the commands in TRAPSTACK.

This function is used by clicraft internally, and should not be called by action scripts.

pushtrap <command>

Add a command to TRAPSTACK for clicraft to run if it exits abnormally.

Every pushtrap call should have a matching poptrap call to keep subsequent poptrap calls from failing.

poptrap <command>

Try to remove the last command added to TRAPSTACK. If it doesn’t match command, poptrap will fail.

Every pushtrap call should have a matching poptrap call to keep subsequent poptrap calls from failing.

mklock <lockname>

Try to create a lock identified by lockname. Returns nonzero if the lock is already held.

Lock files are stored in $TMPDIR/clicraft.$EUID. If $TMPDIR is unset or empty, /tmp is used instead.

mklock and rmlock call pushtrap and poptrap internally, so every mklock call should have a matching rmlock call.

rmlock <lockname>

Try to remove the lock identified by lockname. Returns nonzero if the lock isn’t held by this clicraft process.

Lock files are stored in $TMPDIR/clicraft.user. If $TMPDIR is unset or empty, /tmp is used instead.

mklock and rmlock call pushtrap and poptrap internally, so every mklock call should have a matching rmlock call.

serverlog <condition> <command>

Start printing SERVER_LOG to stdout, run command, and wait until condition is reached or TIMEOUT seconds have passed before stopping output and returning. If TIMEOUT is reached, return nonzero. See clicraft.conf(5) for a description of the TIMEOUT option.

If condition is an integer, it is interpreted as the maximum number of lines to print. Otherwise, condition will be used as an extended regular expression, and this function will return as soon as it prints a matching line.

Examples

Example action scripts can be found in $CLICRAFT_CONFIG/action.d/ with a .sh.example file extension. They can be viewed, edited and installed with clicraft edit. Their usage is documented in clicraft-examples(1), except for action.sh.example and action-override.sh.example, which are used internally by clicraft edit and are documented in clicraft(1).

See Also

Bugs

Report any and all bugs to the clicraft issue tracker with as much detail as possible.

Authors

To see a full list of contributors, use git shortlog -s in the clicraft git repository.