Skip to content

Switch from C and Python to Lua plugins - #2248

Merged
jubalh merged 29 commits into
masterfrom
luaplugins
Oct 29, 2026
Merged

jubalh merged 29 commits into
masterfrom
luaplugins

Conversation

@jubalh

@jubalh jubalh commented Oct 29, 2026

Copy link
Copy Markdown
Member

Replace the legacy C and Python plugin systems with Lua.

CPython was too heavy: Embedded CPython added ~30–50 MB of memory overhead right on startup and slowed down launch times.

Interfacing with the Python C-API and GIL inside GLib's main loop was overly complex, and the code was basically unmaintained.

C plugins were risky: Plugins loaded via dlopen/libprofanity had no safety net—a single null dereference or memory leak in a third party plugin brought down the entire Profanity process. It also required maintaining libprofanity.so and had portability issues (Cygwin, BSDs). Almost nobody wrote C plugins anyway.

Now we have:
Memory savings: Lua states take ~200 KB instead of 30–50 MB.

All Lua hooks run protected via lua_pcall. If a plugin has a bug, Profanity catches it, shows the error in the console, and continues running.

Namespaces are better structured (prof.chat., prof.muc., prof.win.*, ...)

We also introduced priority pipelines for plugins.

Packagers will have it easier since we dont't need python3-embed anymore.

Global packaging path: Profanity now checks $XDG_DATA_DIRS/profanity/plugins/, meaning distros can finally package official plugins cleanly as system packages without users having to manually copy files into ~/.local/share/.

The meson switch is: -Dplugins=enabled. Packagers can set the lua version or we will try to autodetect via lua-version.

We cleaned up the plugins commands, which were confusing earlier.
Now we can cleanly load, install and list commands. Separate and clear defined commands for each task.

Remove support for C plugins (-Dc-plugins) and the shared library
libprofanity.

C plugins via dlopen/libprofanity had some drawbacks:
* Crashes or memory leaks in C plugins crashed profanity.
* Required building and maintaining lib (libprofanity)
* Lack of portability (not supported on Cygwin, quirks across BSDs).

And to my knowledge almost nobody wrote plugins in C.
So this was not worth the complexity and effort to keep it.

BREAKING CHANGE: C plugin support and the libprofanity shared library
have been removed.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Remove support for Python plugins (-Dpython-plugins) and embedded CPython.

Embedding CPython had multiple drawbacks:
* High memory overhead (~30-50 MB) and slower startup time.
* Packaging inconsistencies in distros with python3-embed.
* GIL and threading complexities interfacing with the GLib main loop.

Python plugins were used by users. But I never touched the python code.
It was basically unmaintained.
Interfacing with Python is also too complex without adding huge
benefits.

Profanity will be much leaner without it.
And we will add Lua plugin support so our users can script profanity.

BREAKING CHANGE: Python plugin support has been removed.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
@jubalh jubalh added this to the next milestone Oct 29, 2026
@jubalh jubalh self-assigned this Oct 29, 2026
jubalh added 22 commits October 29, 2026 20:16
Add lightweight, embedded Lua plugin engine.

Support all Profanity event hooks (chat, MUC, presence, stanzas,
commands, timed functions, custom windows, and settings).

Clean build isolation: plugin sources, unittests, and stubs are
properly conditional on -Dplugins=disabled.

Distros can easily use -Dlua-version to specify an exact pkg-config
package (e.g. lua5.4, lua-5.4, luajit).

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
And get rid of Python.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Wrap all plugin dispatch loops with a bitwise mask check to avoid the
costly Lua _G hash lookups on every event iteration.

Add hook_mask (uint64_t) to ProfPlugin and define ProfPluginHook enum.
Check hook presence exactly once during lua_plugin_create.
Removes unnecessary stack operations for disabled plugin hooks.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Dynamically append Profanity's plugins directory to Lua's package.path
and package.cpath.

Allows authors to structure complex plugins into multiple files and use
the standard require('myplugin.module') pattern.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Previously, errors occurring inside asynchronous event hooks (like
prof_pre_chat_message_display) were only written to profanity.log.

Hook and load errors are now simultaneously logged to the file and
printed as an alert directly to the console window.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
…stry refs

Remove the generic void* callback_exec and callback_destroy pointers
from PluginCommand, PluginTimedFunction, and PluginWindowCallback.

Store lua_State* L and int lua_ref directly on the structs.

Move Lua execution logic out of the middleman layer and invoke lua_pcall
directly via the callbacks.c command loop.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Remove the flat, 57-function prof.* C-style registration.

Introduce structured namespaces matching domains:
* prof.log.* (debug, info, warning, error)
* prof.settings.* (get, set, list_*)
* prof.chat.* (show, set_titlebar_enctext, etc.)
* prof.muc.* (show, join, leave, etc.)
* prof.win.* (create, exists, focus, show, close)
* prof.cons.* (show, alert, bad_cmd_usage)
* prof.disco.* (add_feature)
* prof.completer.* (add, remove, filepath_add)

Replace duplicate typed getters/setters with a single, polymorphic
prof.settings.get() and prof.settings.set().

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Instead of running plugins in whatever order the hash table gives us,
plugins can now set a priority so they run in a sensible order.

Plugin authors can define `prof_priority` in their script:
* prof.PRIORITY_EARLY (1): For stuff that needs to run first, like
  message filtering, encryption, or dropping stanzas.
* prof.PRIORITY_NORMAL (100): Default for regular plugins and commands.
* prof.PRIORITY_LATE (200): For things that should run last, like
  logging or notifications that want to see the final message.

If a plugin doesn't set prof_priority, it just defaults to 100.

How the auto-sorting works:
Plugins are inserted into a sorted list (`plugins_ordered`):
* Lower numbers run first.
* When two plugins have the same priority, we sort them alphabetically
  by plugin name so the execution order is always deterministic instead
  of depending on directory reading order.
* As a nice side effect, iterating the pre-sorted list directly saves
  us from allocating a values list on every single hook call.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Replace nested plugin_name -> (command_name -> command) hash tables with
a single flat commands hash table (p_commands) for direct O(1) lookup.

Maintain per-plugin registered command names (p_plugin_commands) to
handle O(K) unregistration on plugin unload.

Eliminate dynamic GList heap allocation and linear loops over plugins
during command execution, help queries, and command listing.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Test /plugins load, command registration, custom command invocation, and
/plugins unload.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
…eaking

Add plugins_priority_ordering to verify automatic NORMAL priority
assignment and priority-ordered hook execution (PRIORITY_EARLY executing
before NORMAL).

Add plugins_priority_alphabetical_tie_break to verify that plugins with
identical priorities break ties alphabetically regardless of load order.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Make sure outgoing message filters chain properly in priority order so that
one plugin gets the modified text from the previous one.

In this test:
* An EARLY plugin replaces "apple" with "banana"
* A NORMAL plugin replaces "banana" with "cherry"

If a user sends "I like apple", EARLY turns it into "I like banana",
and then NORMAL turns that into "I like cherry".

We verify that stabber sees "I like cherry" in the stanza over the wire
and that our own chat window displays "me: I like cherry".

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
…ession

Test raw stanza filtering.

Verifies prof_on_message_stanza_receive can selectively suppress
incoming stanzas and allow non-matching stanzas through to normal
message handling.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Verifies custom disco features advertised by Lua plugins are returned in
XEP-0030 disco#info responses over the wire.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
…nteractive input

Test prof.win.* functions.

Verify window creation with prof.win.create, focusing with
prof.win.focus, interactive input routing via on_win_input(tag, line),
and window closing.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
…loads

Test prof.settings.set and prof.settings.get.

Verify keyfile settings written by a plugin survive plugin unloads and
are successfully retrieved upon reloading.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Test prof.completer.add.

Verifies TAB expansion completes plugin subcommands in the ncurses PTY.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Test prof.register_timed.

Verify periodic timer fires in the main event loop and unregisters
safely when the owning plugin is unloaded.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
…munity

Test lua_pcall error handling.

Verify unhandled errors in plugin callbacks are logged to the console
without terminating the client process or corrupting subsequent command
execution.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Test startup plugin loading.

Verify that plugins saved in profrc are automatically loaded on boot by
plugins_init(), inserted into plugins_ordered, have prof_init invoked,
and have their commands registered immediately without manual loading.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
/plugins update is redundant with /plugins install, which already overwrites
existing files upon reinstallation.

/plugins reload is just noise.
This command will be rarely used and people can just unload+load in
those cases.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Installing or downloading a plugin now places the .lua file into the
user plugins directory without automatically loading it.
This should be a separate step.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
`/plugins load` now loads plugins into the active session only.
`/plugins unload` unloads plugins from memory.
`/plugins enable` loads it and persists it to profrc so it will be
automatically loaded upon restart.
`/plugins disable` removes the plugin from profrc and unloads it.
`/plugins` lists loaded plugins as (enabled) or (session only).

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Signed-off-by: Michael Vetter <jubalh@iodoru.org>
We might ship official plugins later.
Distributions will install them to a global path.

So lets look for them in global XDG data directories like
/usr/share/profanity/plugins/ and /usr/local/share/profanity/plugins.

The local directory ~/.local/share/profanity/plugins/ takes precedence
over system paths.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
Signed-off-by: Michael Vetter <jubalh@iodoru.org>
For now. Until we have some Lua plugins.

Signed-off-by: Michael Vetter <jubalh@iodoru.org>
@jubalh
jubalh merged commit a8ca2d2 into master Oct 29, 2026
12 checks passed
@jubalh
jubalh deleted the luaplugins branch October 29, 2026 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant