# C API: Content filters

Which items a game allows, by section, slot, source, game, tag or id, with presets and JSON rules: [Content filters](../../guides/filters.md).

## sona_filter_create

```c
sona_result sona_filter_create(sona_ctx *ctx, const sona_catalogue *cat, sona_filter **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | `NULL` allowed (below) |
| `cat` | `const sona_catalogue *` | The catalogue | required |
| `out` | `sona_filter **` | Receives the new filter | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND`, `SONA_E_LIMIT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

`ctx` may be NULL (malloc; errors only as codes); with one, its allocator and `sona_last_error`. cat may be NULL: then only id, slot, section (from the id) and source rules can match. The filter may outlive both.

## sona_filter_free

```c
void sona_filter_free(sona_filter *f);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_filter *` | The filter | required |

**Returns** Nothing.

Frees the filter.

## sona_filter_reset

```c
sona_result sona_filter_reset(sona_filter *f);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_filter *` | The filter | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

Every rule goes; tags stay.

## sona_filter_sections

```c
sona_result sona_filter_sections(sona_filter *f, uint32_t mode, uint32_t sections);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_filter *` | The filter | required |
| `mode` | `uint32_t` | `SONA_CONTENT_ALLOW` or `SONA_CONTENT_DENY` | - |
| `sections` | `uint32_t` | `SONA_SECTION_*` bits | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

Adds a rule on sections: bits (1u << `sona_section`). `mode` is `SONA_CONTENT_ALLOW` or `_DENY`; each call adds to the rules.

## sona_filter_slots

```c
sona_result sona_filter_slots(sona_filter *f, uint32_t mode, uint32_t slots);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_filter *` | The filter | required |
| `mode` | `uint32_t` | `SONA_CONTENT_ALLOW` or `SONA_CONTENT_DENY` | - |
| `slots` | `uint32_t` | Slot mask bits | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

Adds a rule on slots (AssetMask bits): an item matches when it covers any of them. `mode` is `SONA_CONTENT_ALLOW` or `_DENY`.

## sona_filter_sources

```c
sona_result sona_filter_sources(sona_filter *f, uint32_t mode, uint32_t sources);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_filter *` | The filter | required |
| `mode` | `uint32_t` | `SONA_CONTENT_ALLOW` or `SONA_CONTENT_DENY` | - |
| `sources` | `uint32_t` | `SONA_ITEM_*` source bits | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Content filters](../../guides/filters.md)):

```c
sona_filter_game(f, SONA_CONTENT_ALLOW, "Racer X");
sona_filter_sources(f, SONA_CONTENT_ALLOW, SONA_CONTENT_STOCK);
```

Adds a rule on where items come from (`SONA_CONTENT_STOCK` and the other source bits). `mode` is `SONA_CONTENT_ALLOW` or `_DENY`.

## sona_filter_game

```c
sona_result sona_filter_game(sona_filter *f, uint32_t mode, const char *game);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_filter *` | The filter | required |
| `mode` | `uint32_t` | `SONA_CONTENT_ALLOW` or `SONA_CONTENT_DENY` | - |
| `game` | `const char *` | A game's name, as the catalogue spells it | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Content filters](../../guides/filters.md)):

```c
sona_filter_game(f, SONA_CONTENT_ALLOW, "Racer X");
sona_filter_sources(f, SONA_CONTENT_ALLOW, SONA_CONTENT_STOCK);
```

A game as `sona_catalogue_game` names it (ASCII case folded). `SONA_E_NOT_FOUND` when no item has that game.

## sona_filter_tag

```c
sona_result sona_filter_tag(sona_filter *f, uint32_t mode, const char *tag);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_filter *` | The filter | required |
| `mode` | `uint32_t` | `SONA_CONTENT_ALLOW` or `SONA_CONTENT_DENY` | - |
| `tag` | `const char *` | A tag's name | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

A tag by name ("weapon", "logo", or one the host defined). `SONA_E_NOT_FOUND` for an unknown name.

## sona_filter_item

```c
sona_result sona_filter_item(sona_filter *f, uint32_t mode, const sona_guid *id);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_filter *` | The filter | required |
| `mode` | `uint32_t` | `SONA_CONTENT_ALLOW` or `SONA_CONTENT_DENY` | - |
| `id` | `const sona_guid *` | An item id | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

Adds a rule on one item by id. `mode` is `SONA_CONTENT_ALLOW` or `_DENY`.

## sona_filter_preset

```c
sona_result sona_filter_preset(sona_filter *f, uint32_t preset);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_filter *` | The filter | required |
| `preset` | `uint32_t` | Which preset, one of the header's constants for it | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

Adds a preset's rules (`SONA_FILTER_PRESET_*`, listed in [Content filters](../../guides/filters.md)).

## sona_filter_load

```c
sona_result sona_filter_load(sona_filter *f, const char *json, size_t len, char *why, size_t why_cap);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_filter *` | The filter | required |
| `json` | `const char *` | Filter rules as JSON text | required |
| `len` | `size_t` | How many bytes there are | - |
| `why` | `char *` | Receives what was refused, NUL-terminated; may be NULL | `NULL`: not written |
| `why_cap` | `size_t` | How many bytes `why` holds | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_LIMIT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

Rules from JSON ([Content filters](../../guides/filters.md)): {"preset": [...], "allow": {...}, "deny": {...}, "tags": {...}, "untag": {...}}. All or nothing: on an error the filter is unchanged and why (may be NULL) says what was wrong.

## sona_filter_tag_count

```c
uint32_t sona_filter_tag_count(const sona_filter *f);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_filter *` | The filter | required |

**Returns** `uint32_t`.

How many tags the filter knows, built-in and host-defined.

## sona_filter_tag_name

```c
const char * sona_filter_tag_name(const sona_filter *f, uint32_t tag);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_filter *` | The filter | required |
| `tag` | `uint32_t` | A tag's bit | - |

**Returns** `const char *`.

NULL past the last.

## sona_filter_tag_find

```c
int32_t sona_filter_tag_find(const sona_filter *f, const char *name);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_filter *` | The filter | required |
| `name` | `const char *` | The name to look up | required |

**Returns** `int32_t`.

-1 when unknown.

## sona_filter_tag_define

```c
sona_result sona_filter_tag_define(sona_filter *f, const char *name, uint32_t *out_tag);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_filter *` | The filter | required |
| `name` | `const char *` | The new tag's name | required |
| `out_tag` | `uint32_t *` | Receives the new tag's bit | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_LIMIT` ([what each means](../../troubleshooting.md#error-codes)).

A new tag (a name of 1 to 31 bytes of a-z, 0-9 and '-'), or the index of the one of that name. `SONA_E_LIMIT` past 64 tags.

## sona_filter_tag_set

```c
sona_result sona_filter_tag_set(sona_filter *f, const sona_guid *id, uint32_t tag, int on);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_filter *` | The filter | required |
| `id` | `const sona_guid *` | An item id | required |
| `tag` | `uint32_t` | A tag's bit | - |
| `on` | `int` | 1 on, 0 off | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

Puts a tag on an item (`on` 1) or takes it off (0), for ids in the catalogue or not.

## sona_filter_item_tags

```c
uint64_t sona_filter_item_tags(const sona_filter *f, const sona_guid *id);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_filter *` | The filter | required |
| `id` | `const sona_guid *` | An item id | required |

**Returns** `uint64_t`.

The bits of an item's tags (bit n is tag n).

## sona_filter_tag_items

```c
uint32_t sona_filter_tag_items(const sona_filter *f, uint32_t tag);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_filter *` | The filter | required |
| `tag` | `uint32_t` | A tag's bit | - |

**Returns** `uint32_t`.

How many catalogue items carry a tag.

## sona_filter_allows

```c
int sona_filter_allows(const sona_filter *f, const sona_guid *id);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_filter *` | The filter | required |
| `id` | `const sona_guid *` | An item id | required |

**Returns** `int`.

1 when the item is allowed; a NULL filter allows everything.

## sona_filter_check

```c
sona_result sona_filter_check(const sona_filter *f, const sona_guid *id, sona_filter_verdict *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_filter *` | The filter | required |
| `id` | `const sona_guid *` | An item id | required |
| `out` | `sona_filter_verdict *` | Receives the [`sona_filter_verdict`](../fields.md#sona_filter_verdict) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

Whether the item is allowed, and if not, which rule denied it (`SONA_CONTENT_BY_*`) and the item's tags.

## sona_filter_search

```c
sona_result sona_filter_search(const sona_filter *f, const sona_catalogue *cat, const sona_catalogue_query *q, uint32_t *out_indexes, uint32_t max, uint32_t *total);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_filter *` | The filter | required |
| `cat` | `const sona_catalogue *` | The catalogue | required |
| `q` | `const sona_catalogue_query *` | A [`sona_catalogue_query`](../fields.md#sona_catalogue_query) | required |
| `out_indexes` | `uint32_t *` | Receives catalogue indexes, up to `max` | required |
| `max` | `uint32_t` | How many entries `out` holds | - |
| `total` | `uint32_t *` | Receives how many there are in all; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

`sona_catalogue_search` over cat, allowed items only (offset and total count allowed ones). Any catalogue works; items are matched by id.

## sona_filter_allowed_count

```c
uint32_t sona_filter_allowed_count(const sona_filter *f);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_filter *` | The filter | required |

**Returns** `uint32_t`.

Catalogue items allowed.

## sona_filter_manifest

```c
sona_result sona_filter_manifest(const sona_filter *f, sona_manifest *m, sona_filter_report *report);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_filter *` | The filter | required |
| `m` | `sona_manifest *` | Receives the [`sona_manifest`](../fields.md#sona_manifest) | required |
| `report` | `sona_filter_report *` | Receives what the call did; may be NULL | `NULL` allowed (below) |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

Takes every denied item off a manifest: worn items go back to the body's defaults, props are hidden. `report` (may be NULL) lists what changed.

## sona_filter_manifest_bytes

```c
sona_result sona_filter_manifest_bytes(const sona_filter *f, uint8_t manifest[SONA_MANIFEST_BYTES], sona_filter_report *report);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_filter *` | The filter | required |
| `manifest` | `uint8_t [SONA_MANIFEST_BYTES]` | The manifest's 1,000 bytes, changed in place | required |
| `report` | `sona_filter_report *` | Receives what the call did; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MANIFEST` ([what each means](../../troubleshooting.md#error-codes)).

The same on the 1000 bytes, in place; unchanged bytes when nothing is denied. `SONA_E_MANIFEST` for bad bytes.

## sona_ctx_set_filter

```c
sona_result sona_ctx_set_filter(sona_ctx *ctx, const sona_filter *f);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `f` | `const sona_filter *` | The filter | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

The ctx's filter: a copy of f (NULL takes it away), applied to an avatar when its manifest is read, whoever's it is (`sona_avatar_add`, headless renders); an avatar read before the change keeps what it wore until it is removed and added again. A preview of a denied item fails with `SONA_E_NOT_FOUND`. Pickers read it back with `sona_ctx_filter`: NULL when none; owned by the ctx and gone at the next `sona_ctx_set_filter` or `sona_close`.

## sona_ctx_filter

```c
const sona_filter * sona_ctx_filter(const sona_ctx *ctx);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `const sona_ctx *` | The context | required |

**Returns** `const sona_filter *`.

**Example**:

```c
const sona_filter * x_got = sona_ctx_filter(ctx);
printf("sona_ctx_filter: %s\n", x_got ? "a value" : "none");
```

The ctx's filter as `sona_ctx_set_filter` left it: NULL when none; owned by the ctx and gone at the next `sona_ctx_set_filter` or `sona_close`.

## sona_avatar_filter_report

```c
sona_result sona_avatar_filter_report(const sona_avatar *a, sona_filter_report *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `out` | `sona_filter_report *` | Receives the [`sona_filter_report`](../fields.md#sona_filter_report) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_READY`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_filter_report x_filter_report;
memset(&x_filter_report, 0, sizeof x_filter_report);
x_filter_report.size = sizeof x_filter_report;
sona_result result = sona_avatar_filter_report(avatar, &x_filter_report);
if (result != SONA_OK) printf("sona_avatar_filter_report: %s\n", sona_result_name(result));
```

What the ctx's filter changed on this avatar. `SONA_E_NOT_READY` before its manifest is read, `SONA_E_NOT_FOUND` when no filter was set then.
