poezio/doc/en/plugins.txt

202 lines
6.9 KiB
Text
Raw Normal View History

2011-11-05 03:20:03 +00:00
Plugins
=======
Location
--------
The plugins have to be present in '$XDG_DATA_HOME/poezio/plugins/plugin_name.py'
(or '~/.local/share' if not defined)
Structure
---------
A plugin must always be a class named Plugin that inherits the
plugin.BasePlugin class defined into the *plugin* poezio module.
Methods
-------
Overridden methods
~~~~~~~~~~~~~~~~~~
2011-11-09 14:05:55 +00:00
The *Plugin* class has several method that you can override for your own
convenience
2011-11-05 03:20:03 +00:00
[[init]]
*init*:: +self+ +
This method is called when the plugin is loaded, this is where you call
the other methods, for example <<add-command,add_command>>, and initialize
2011-11-09 14:05:55 +00:00
everything to make your plugin actually do something. <<example-1,ex 1>>,
<<example-2,ex 2>>
2011-11-05 03:20:03 +00:00
*cleanup*:: +self+ +
Called when the plugin is unloaded (or when poezio is exited). Clean everything
that needs to be cleaned here.
Callable methods
~~~~~~~~~~~~~~~~
The BasePlugin has several methods that can be used. Here is a list of
all its methods that you could use in your plugin, describing what they
do, their arguments, and giving some example for each of them.
[[add-command]]
*add_command*:: +self+, +name+, +handler+, +help+, +completion=None+ +
This method adds a global command to poezio. For example you can add a /foo
command that the user could call when the plugin is loaded, by calling this
method with _foo_ as its _name_ argument. <<example-1,ex 1>>
2011-11-09 14:05:55 +00:00
* _name_: (string) the name of the command (for example, if it is 'plugintest',
it can add a /plugintest command)
2011-11-05 03:20:03 +00:00
* _handler_: (function) the function to be called when the command is executed.
the handler takes *args* as a parameter, which is a string of what
is entered after the command. Split *args* (with _common.shell_split_) to use
that as command arguments.
2011-11-09 14:05:55 +00:00
* _help_: (string) the help message available for that command through the
_/help_ command.
* _completion_: (function) the completion for the args of that command. It
takes an input object as its only argument. This function should call the
_auto_completion()_ method on the input object, passing a list of possible
strings for the completion and returning the value of that call directly.
Everything else is handled by that _auto_completion()_ method (checking what
strings match, how to cycle between matches, etc). If you dont want any
special completion for that command, just pass None (the default value).
*add_event_handler**: +self+, +event_name+, +handler+ +position+
2011-11-05 03:20:03 +00:00
This methods adds a callback that will be called whenever the given event
occurs. <<example-2,ex 2>>
* _event_name_: (string) The name of the event you want to ``monitor''.
This can be a sleekxmpp event, or a poezio event. See the list of
<<events-list,all available events>>.
* _handler_: The method that will be called whenever the event occurs.
2011-11-09 14:05:55 +00:00
It must accept the arguments specified for that event in the
<<events-list,events list>>.
* _position_: Optional, this argument will specify the position of the handler
in the handler list for this event. It is 0 by default, and will only be used
with the internal poezio events described below.
2011-11-05 03:20:03 +00:00
Attributes
----------
Config
~~~~~~
By default, each plugin has a PluginConfig object accessible with *self.config*.
*PluginConfig.read*:: +self+ +
2011-11-09 14:05:55 +00:00
Reads the config file from $XDG_CONFIG_HOME/poezio/plugins/plugin_name.cfg, it
is called upon initialization, so you should not need it.
2011-11-05 03:20:03 +00:00
*PluginConfig.write*:: +self+ +
Writes the config to the same file mentioned previously.
Core
~~~~
2011-11-09 14:05:55 +00:00
Each plugin has a reference to the Core object accessible with *self.core*,
that allows you to do about anything with poezio. Remember to use it only when
you need it, and to use the functions described in this documentation only,
even if much more is available. If your plugin needs to do something not
available with these methods, please do a feature request instead of doing
some dirty hacks with some other methods.
2011-11-05 03:20:03 +00:00
Core methods
^^^^^^^^^^^^
CAUTION: TODO
[[events-list]]
Events list
-----------
2011-11-09 13:51:48 +00:00
SleekXMPP events
~~~~~~~~~~~~~~~~
2011-11-09 14:05:55 +00:00
For the sleekxmpp events, please refer to the
https://github.com/fritzy/SleekXMPP/wiki/Event-Index[sleekxmpp event index].
2011-11-09 13:51:48 +00:00
Poezio events
~~~~~~~~~~~~~
*muc_say*:: +message+, +tab+ +
2011-11-09 14:05:55 +00:00
The handlers for this event are called whenever you say something in a MUC
(through the /say command or by direct input). The parameters given to the
handlers are:
2011-11-09 13:51:48 +00:00
* _message_: Message to be sent
* _tab_: Tab in which the message will be sent
*private_say*:: +message+, +tab+ +
2011-11-09 14:05:55 +00:00
The handlers for this event are called whenever you say something in a private
conversaton in a MUC (through the /say command or by direct input). The
parameters given to the handlers are:
2011-11-09 13:51:48 +00:00
* _message_: Message to be sent
* _tab_: Tab in which the message will be sent
*conversation_say*:: +message+, +tab+ +
2011-11-09 14:05:55 +00:00
The handlers for this event are called whenever you say something in direct
conversation (through the /say command or by direct input). The parameters
given to the handler are:
2011-11-09 13:51:48 +00:00
* _message_: Message to be sent
* _tab_: Tab in which the message will be sent
*muc_msg*:: +message+, +tab+ +
2011-11-09 14:05:55 +00:00
The handlers for this event are called whenever you receive a message in a MUC.
The parameters given to the handler are:
2011-11-09 13:51:48 +00:00
* _message_: Message received
* _tab_: Tab in which the message was received
*private_msg*:: +message+, +tab+ +
2011-11-09 14:05:55 +00:00
The handlers for this event are called whenever you receive a message in a
private conversation in a MUC. The parameters given to the handler are:
2011-11-09 13:51:48 +00:00
* _message_: Message received
* _tab_: Tab in which the message was received
*conversation_msg*:: +message+, +tab+ +
2011-11-09 14:05:55 +00:00
The handlers for this event are called whenever you receive a message in a
direct conversation. The parameters given to the handler are:
2011-11-09 13:51:48 +00:00
* _message_: Message received
* _tab_: Tab in which the message was received
2011-11-09 14:05:55 +00:00
CAUTION: Plugins are in an experimental state and this API might change
slightly in a near future. You have to be aware of that while making a plugin.
2011-11-05 03:20:03 +00:00
Examples
--------
[[example-1]]
.Add a simple command that sends "Hello World!" into the conversation
=====================================================================
[source,python]
---------------
class Plugin(BasePlugin):
def init(self):
self.add_command('hello', self.command_hello, "Usage: /hello\nHello: Send 'Hello World!'", None)
2011-11-09 13:51:48 +00:00
def command_hello(self, arg):
2011-11-05 03:20:03 +00:00
self.core.send_message('Hello World!')
---------------
=====================================================================
[[example-2]]
.Adds an event handler that sends ``tg'' to a groupchat when a message is received from someone named ``Partauch''
=====================================================================
[source,python]
---------------
class Plugin(BasePlugin):
def init(self):
2011-11-09 13:51:48 +00:00
self.add_event_handler('muc_msg', self.on_groupchat_message)
2011-11-05 03:20:03 +00:00
2011-11-09 13:51:48 +00:00
def on_groupchat_message(self, message, tab):
2011-11-05 03:20:03 +00:00
if message['mucnick'] == "Partauche":
2011-11-09 13:51:48 +00:00
tab.command_say('tg')
2011-11-05 03:20:03 +00:00
---------------
=====================================================================