Mosquitto plugin that lets you write your plugins in Python.
You need mosquitto version 2.0.0 or higher.
Make sure you have Python dev package installed (apt-get install python-dev or apt-get install python3-dev under Debian/Ubuntu).
You must either have mosquitto header files installed globally in
/usr/include, etc. or clone this repository at the top of the
mosquitto source directory. Then:
cd mosquitto_pyplugin
make
Pass PYTHON variable to compile with other Python interpreter
version than default (pypy3.9):
make PYTHON=python3
If all goes ok, there should be libmosquitto_pyplugin.so file in the
current directory. Copy it under path accessible for mosquitto daemon,
e.g.: /usr/local/lib/mosquitto/.
If you get errors while compiling the plugin about -lmosquitto then you have a missing link to libmosquitto.
Just check the file /usr/lib/libmosquitto.so or /usr/lib/mosquitto.so.1 exists and create a symlink:
ln -s /usr/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so
And make again the plugin. This time it should work.
Add following line to mosquitto.conf:
plugin /path/to/libmosquitto_pyplugin.so
File mosquitto_pyplugin.py must be found by the used python interpreter. You can use PYTHONPATH to adapt this accordingly.
You must also give a pointer to Python module which is going to be
loaded (make sure it's in Python path, use PYTHONPATH env variable
to the rescue):
plugin_opt_pyplugin_module some_module
Python module should do required initializations when it's imported and provide following global functions:
-
plugin_init(options): called on plugin init,optionsis a dictionary with allplugin_opt_params from mosquitto configuration (exceptplugin_opt_pyplugin_module) -
plugin_cleanup(options): called on plugin cleanup,optionsis a dictionary with allplugin_opt_params from mosquitto configuration (exceptplugin_opt_pyplugin_module) -
basic_auth(client, username, password): return mosquitto_pyplugin.MOSQ_ERR_SUCCESS if given client, username and password combination is allowed to log in or mosquitto_pyplugin.MOSQ_ERR_PLUGIN_DEFER if another module should take care -
extended_auth_start(client, auth): return mosquitto_pyplugin.MOSQ_ERR_SUCCESS if given auth.data_in succeeded the authentication. return mosquitto_pyplugin.MOSQ_ERR_AUTH_CONTINUE and pass new auth.data_out to client to continue authentication or mosquitto_pyplugin.MOSQ_ERR_PLUGIN_DEFER if another module should take care -
extended_auth_continue(client, auth): return mosquitto_pyplugin.MOSQ_ERR_SUCCESS if given auth.data_in succeeded the authentication. return mosquitto_pyplugin.MOSQ_ERR_AUTH_CONTINUE and pass new auth.data_out to client to continue authentication or mosquitto_pyplugin.MOSQ_ERR_PLUGIN_DEFER if another module should take care -
acl_check(client, topic, access, payload): return MOSQ_ERR_SUCCESS if given user is allowed to subscribe (access = mosquitto_pyplugin.MOSQ_ACL_SUBSCRIBE), read (access = mosquitto_pyplugin.MOSQ_ACL_READ) or publish (access = mosquitto_pyplugin.MOSQ_ACL_WRITE) to given topic (seemosquitto_pypluginmodule below).payloadargument holds message payload as bytes, orNoneif not applicable. Return mosquitto_pyplugin.MOSQ_ERR_PLUGIN_DEFER in case another plugin should take care -
psk_key(client, identity, hint): returnPSKstring (in hex format without heading 0x) if given identity and hint pair is allowed to connect else return""for returning MOSQ_ERR_AUTH orNonefor returning MOSQ_ERR_PLUGIN_DEFER to mosquitto. -
disconnect(client, reason): inform about disconnection ofclientwith reason codereason -
message(client, message_event): inform new message event. Message is passed as dictionary If the callback adapts the message, the new information is passed back to mosquitto and the message will be adapted accordingly. -
tick(): tick event. Will be called on every mosquitto main loop iteration -
reload(): reload event. Will be called on reload/security_init by mosquitto
Plugin module can import an auxiliary module provided by mosquitto:
import mosquitto_pyplugin
The module provides following function:
-
log(loglevel, message): logmessageinto mosquitto's log file with the givenloglevel(one of the constants below). -
strerror(mosq_errno): return brief error message -
client_address(client): get client address fromclienthandle -
client_id(client): get client id fromclienthandle -
client_certificate(client): get the client certificate from theclienthandle -
client_protocol(client): get used client protocol fromclienthandle -
client_protocol_version(client): get used client protocol version fromclienthandle -
client_username(client): get client username fromclienthandle -
set_username(client, username): change client username inclienthandle tousername -
kick_client_by_clientid(client_id, with_will): -
kick_client_by_username(client_username, with_will): -
broker_publish(topic, clientid=None, payload=None, qos=0, retain=False, properties=[]): when clientid is None [default], publish message to any client. If a clientid is given, publish the message the the specified client. -
topic_matches_sub(sub, topic): it mirrorsmosquitto_topic_matches_subfrom libmosquitto C library - the function checks whethertopicmatches givensubpattern (for example, it returnsTrueifsubis/foo/#andtopicis/foo/bar) and is mostly useful isacl_checkfunction above
The following constants for access parameter in acl_check are
provided:
MOSQ_ACL_NONEMOSQ_ACL_READMOSQ_ACL_WRITEMOSQ_ACL_SUBSCRIBEMOSQ_ACL_UNSUBSCRIBE
The following constants for loglevel parameter in Log are provided:
MOSQ_LOG_NONEMOSQ_LOG_INFOMOSQ_LOG_NOTICEMOSQ_LOG_WARNINGMOSQ_LOG_ERRMOSQ_LOG_DEBUGMOSQ_LOG_SUBSCRIBE(not recommended for use by plugins)MOSQ_LOG_UNSUBSCRIBE(not recommended for use by plugins)MOSQ_LOG_WEBSOCKETS(not recommended for use by plugins)MOSQ_LOG_INTERNAL(not recommended for use by plugins)MOSQ_LOG_ALL(not recommended for use by plugins)
The following constants for errors are provided:
MOSQ_ERR_AUTH_CONTINUEMOSQ_ERR_NO_SUBSCRIBERSMOSQ_ERR_SUB_EXISTSMOSQ_ERR_CONN_PENDINGMOSQ_ERR_SUCCESSMOSQ_ERR_NOMEMMOSQ_ERR_PROTOCOLMOSQ_ERR_INVALMOSQ_ERR_NO_CONNMOSQ_ERR_CONN_REFUSEDMOSQ_ERR_NOT_FOUNDMOSQ_ERR_CONN_LOSTMOSQ_ERR_TLSMOSQ_ERR_PAYLOAD_SIZEMOSQ_ERR_NOT_SUPPORTEDMOSQ_ERR_AUTHMOSQ_ERR_ACL_DENIEDMOSQ_ERR_UNKNOWNMOSQ_ERR_ERRNOMOSQ_ERR_EAIMOSQ_ERR_PROXYMOSQ_ERR_PLUGIN_DEFERMOSQ_ERR_MALFORMED_UTF8MOSQ_ERR_KEEPALIVEMOSQ_ERR_LOOKUPMOSQ_ERR_MALFORMED_PACKETMOSQ_ERR_DUPLICATE_PROPERTYMOSQ_ERR_TLS_HANDSHAKEMOSQ_ERR_QOS_NOT_SUPPORTEDMOSQ_ERR_OVERSIZE_PACKETMOSQ_ERR_OCSPMOSQ_ERR_TIMEOUTMOSQ_ERR_RETAIN_NOT_SUPPORTEDMOSQ_ERR_TOPIC_ALIAS_INVALIDMOSQ_ERR_ADMINISTRATIVE_ACTIONMOSQ_ERR_ALREADY_EXISTS
The following constants for property types are provided:
MQTT_PROP_TYPE_BYTEMQTT_PROP_TYPE_INT16MQTT_PROP_TYPE_INT32MQTT_PROP_TYPE_VARINTMQTT_PROP_TYPE_BINARYMQTT_PROP_TYPE_STRINGMQTT_PROP_TYPE_STRING_PAIR