summaryrefslogtreecommitdiff
path: root/doc/en/plugins.txt
blob: 47ab7f01186b9b0a64a4d47c882e4d97c8af131d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Plugins
=======

Currently, the plugins are in a plugin branch on the git repo.

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
~~~~~~~~~~~~~~~~~~
The *Plugin* class has several method that you can override for your own convenience

[[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
everything to make your plugin actually do something. <<example-1,ex 1>>, <<example-2,ex 2>>

*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>>

* _name_: (string) the name of the command (for example, if it is 'plugintest', it can
add a /plugintest command)
* _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.
* _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 don’t want any special completion
for that command, just pass None (the default value).

*add_event_handler**: +self+, +event_name+, +handler+ +
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.
It must accept the arguments specified for that event in the <<events-list,events list>>.

Attributes
----------

Config
~~~~~~
By default, each plugin has a PluginConfig object accessible with *self.config*.

*PluginConfig.read*:: +self+ + 
Reads the config file from $XDG_CONFIG_HOME/poezio/plugins/plugin_name.cfg, it is called upon initialization, so you should not need it.

*PluginConfig.write*:: +self+ +
Writes the config to the same file mentioned previously.

Core
~~~~
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.

Core methods
^^^^^^^^^^^^
CAUTION: TODO


[[events-list]]
Events list
-----------
CAUTION: TODO

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)

    def command_hello(self, args):
        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):
    	self.add_event_handler('groupchat_message', self.on_groupchat_message)

    def on_groupchat_message(self, message):
        if message['mucnick'] == "Partauche":
            self.core.send_message('tg', to=message.getMucroom())
---------------
=====================================================================