# Writing a plugin

The server can load plugins that are not its own: a Java class of yours that it calls at the same moments as its own plugins, at a login, before an upload, when a folder is listed. It is the way to bend the server where no setting, event or job reaches.

This page describes the interface as the server's code has it. It is for Java developers.

## Before you write one

Most wishes that once needed a plugin do not any more.

| To | Use |
|---|---|
| act when a file arrives, leaves, or is deleted | an [event](Users-Events.md) that starts a [job](Jobs.md) |
| run your own program or script on files | an [Execute task](Task-Execute.md) |
| run your own Java code as a step of a job | a [Java task](Task-Java.md) or a [Custom task](Task-Custom.md) |
| check logins against your own system | [WebApplication](Plugin-WebApplication.md): your system answers over HTTP, in any language |
| drive the server from another program: make users, start jobs, read what happened | [the API](API-Getting-Started.md), with [events and webhooks](API-Events-and-Webhooks.md). The example class of the earlier documentation, which logged in over HTTP and uploaded, downloaded and listed, is replaced by this. |

A plugin is right when the server must ask your code a question in the middle of its own work and act on the answer at once: "may this user run this command on this path?"

A plugin runs inside the server, with all its rights. A mistake in it can stop logins for everybody.

## The shape of a plugin

A plugin named `MyPlugin` is a file `MyPlugin.jar` that holds a class `MyPlugin.Start`: the package is the plugin's name, the class is `Start`.

The server makes one object of the class for each configuration of the plugin, with the constructor that takes nothing, and calls four methods on it:

| Method | Called |
|---|---|
| `public Properties getDefaults()` | to learn the plugin's settings and their starting values |
| `public void setSettings(Properties p)` | when the server starts and whenever the settings are saved, with the values of this configuration |
| `public Properties getSettings()` | when the server wants the present values back |
| `public Properties run(Properties info)` | at every moment the server asks its plugins: see below |

The settings should hold at least `enabled`, `debug` and `version`. The server adds `pluginName` and, for a configuration other than the first, its name as `subItem`.

```java
package MyPlugin;

import java.util.Properties;

public class Start
{
	Properties settings = new Properties();

	public Properties getDefaults()
	{
		Properties p = new Properties();
		p.put("enabled", "false");
		p.put("debug", "false");
		p.put("version", "1.0");
		p.put("blocked_ending", ".tmp");
		return p;
	}

	public void setSettings(Properties p)
	{
		settings = p;
	}

	public Properties getSettings()
	{
		return settings;
	}

	public Properties run(Properties info)
	{
		if (!settings.getProperty("enabled", "false").equalsIgnoreCase("true")) return info;
		if (info.getProperty("action", "").equals("before_upload"))
		{
			// look at what the server handed over, and answer by changing info
		}
		return info;
	}
}
```

## The moments

`run` is called for every plugin and every configuration, whatever the plugin is for. `info.getProperty("action")` says which moment it is. A plugin returns at once for the moments it does not care about, and when it is not enabled: the server calls it either way.

| `action` | The moment |
|---|---|
| `beforeLogin` | a connection has given a name; nothing is checked yet |
| `login` | a name and password could not be settled by the User Manager: a plugin may accept the login and supply the user |
| `afterLogin` | a login was accepted, by whomever. A plugin may change the user, or still refuse. |
| `changePass` | a user changes their password |
| `beforeCommand`, `afterCommand` | around a command of the user |
| `access` | the server checks whether the user may do something to a path |
| `check_path`, `transfer_path` | a path is being worked out, for a check or a transfer |
| `list` | a folder listing was made and can be changed before it is sent |
| `before_upload`, `after_upload`, `before_download`, `after_download` | around a transfer |
| `quotaUpdate`, `getUsedQuota` | the space a user has used is counted |
| `event` | an [event](Users-Events-Editor.md) of a user runs this plugin, with the files of the event |

Plugins are called one after the other, in the middle of the user's request. What takes long makes that user wait: hand long work to a thread of your own.

## What `info` holds

Always:

| Key | Is |
|---|---|
| `action` | the moment |
| `server_item` | the listener the user came in on |
| `ServerGroup` | its connection group |
| `user` | the user's settings, once there is a user |
| `user_info` | facts about the session: `user_name`, `user_ip`, the protocol and more |
| `ServerSession` | the session object of the server |
| `server_settings` | the server's settings |
| `plugin_pref` | the settings of the configuration that is being called |

And for a moment, what belongs to it. At `access`, for example: `command` (such as `STOR`, `RNTO`, `DELE`), `the_dir` (the path), `item` (the file or folder, if it exists) and `additionalAccess`.

A plugin answers by changing `info`:

| Moment | To | Do |
|---|---|---|
| `login` | accept the login | put `action` = `success` and supply the user |
| `afterLogin` | refuse after all | put `allowLogin` = `false` |
| `access` | change what is checked | change `command`, `the_dir` or `additionalAccess` |
| `list` | hide or add items | change the listing |

The exact keys of each moment are not written down anywhere but in the server. Turn on debug logging, write `info` to the log from a test plugin at the moments you need, and build on what you see. The server's own plugins are the model: [ContentBlocker](Plugin-ContentBlocker.md) acts at `before_upload` and answers with the reply code 550; [HomeDirectory](Plugin-HomeDirectory.md) acts at `afterLogin`.

## Settings

The settings of a plugin are kept with the server's settings, under `plugins`, as a list of key and value for each configuration. What `getDefaults` names is filled in the first time.

Preferences has screens for the server's own plugins. For a plugin of yours there is no screen: its values are changed in the server's settings file, `prefs.XML`, which the server reads again when it changes. Keep the settings few, and give them good starting values.

## Getting it loaded

The server's own program files are signed, and the server checks them at every start. A plugin of yours is not one of them. So:

1. Put `MyPlugin.jar` into the folder `plugins` of the server's folder. Libraries it needs go into `plugins/lib`.
2. Allow unsigned plugins: the switch is with the update settings in Preferences. It is off on a new server.
3. Restart the server. Plugin files are read at the start.

The [About](Admin-About.md) section of Server Admin lists files that are not the server's own, under one of two lines: "Not signed, so not loaded (Preferences, Updates can allow unsigned plugins):" or "Not signed, and loaded because Preferences, Updates allows unsigned plugins:".

The server's own classes are loaded first. A plugin cannot replace one of them by carrying a class of the same name.

For developing, the server can be started with the argument `-UNSIGNED`, which makes it load what lies in its folder without checking anything. That is for a developer's machine, not for a server in use.

## Things to know

- **A plugin that throws is skipped for that call.** The log says "Plugin MyPlugin failed", and the other plugins go on. A plugin that hangs holds the user's request.
- **The interface is the server's inside.** The objects in `info` are the server's own classes. They can change between versions; test your plugin against every update before you install it.
- **In [FIPS mode](FIPS-Mode.md)** a plugin that is not the server's own gets from Java's cryptography only what the validated module allows. A plugin that asks for MD5 or DES fails there.
- **In a cluster**, put the file on every server. The settings travel with the server's settings; the file does not.
- **A plugin sees passwords** at `login`. Treat what it logs accordingly.
- **Disk usage.** The moments `quotaUpdate` and `getUsedQuota` are there for plugins that count used space faster than by walking the folders, as a plugin of earlier years did by keeping a small file per folder.

## Not tried for this page

No plugin was written and loaded for this page. The interface is read from the server's code: how a plugin's class is found and made, the four methods, the moments at which plugins are called, and what is handed over. The example above was not compiled.
