source: protocols/python/src/bpython.c @ e38c6d8

Last change on this file since e38c6d8 was e38c6d8, checked in by Nick Murdoch <nick@…>, at 2013-03-17T16:49:05Z

Register an (incomplete) plugin per python file found, with name based on module's 'name' variable.

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 * bpython.c - Python plugin for bitlbee
3 *
4 * Copyright (c) 2013 Nick Murdoch
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include <bitlbee.h>
26#include <Python.h>
27#include "bytesobject.h"
28
29static void load_pyfile(char * path, PyObject * main_dict) {
30    FILE * pyfile;
31    PyObject * err;
32    struct prpl *ret;
33    ret = g_new0(struct prpl, 1);
34   
35    printf("Loading python file %s\n", path);
36    pyfile = fopen(path, "r");
37    /* Copy main dict to make sure that separate plugins
38       run in separate environments */
39    PyObject * main_dict_copy = PyDict_Copy(main_dict);
40    PyRun_File(pyfile, path, Py_file_input, main_dict_copy, main_dict_copy);
41   
42    PyObject * pluginname = PyDict_GetItemString(main_dict_copy, "name");
43    if ((err = PyErr_Occurred()) || !pluginname) {
44        printf("No plugin name\n");
45        PyErr_Print();
46        g_free(ret);
47        return;
48    }
49    PyObject * pluginname_unicode = PyObject_Unicode(pluginname);
50    PyObject * pluginname_encoded = PyUnicode_AsEncodedString(pluginname_unicode, "ascii", "ignore");
51    if ((err = PyErr_Occurred())) {
52        printf("Error encoding plugin name\n");
53        PyErr_Print();
54        g_free(ret);
55        return;
56    }
57    char * pluginname_tmp; /* reference, do not modify */
58    Py_ssize_t length;
59    PyBytes_AsStringAndSize(pluginname_encoded, &pluginname_tmp, &length);
60    if ((err = PyErr_Occurred())) {
61        printf("Bad plugin name\n");
62        PyErr_Print();
63        g_free(ret);
64        return;
65    }
66    ret->name = g_malloc0(length + 1);
67    memmove(ret->name, pluginname_tmp, length);
68
69    Py_DECREF(pluginname_encoded);
70    Py_DECREF(pluginname_unicode);
71
72    register_protocol(ret);
73}
74
75void init_plugin() {
76    GDir *dir;
77    GError *error = NULL;
78
79    printf("Initialising Python... ");
80    Py_Initialize();
81    PyRun_SimpleString("print 'Python initialised!'\n");
82   
83    /* Get a reference to the main module. */
84    PyObject* main_module = PyImport_AddModule("__main__");
85
86    /* Get the main module's dictionary */
87    PyObject* main_dict = PyModule_GetDict(main_module);
88
89    dir = g_dir_open(global.conf->plugindir, 0, &error);
90    if (dir) {
91        const gchar *entry;
92        char * path;
93        while ((entry = g_dir_read_name(dir))) {
94            path = g_build_filename(global.conf->plugindir, entry, NULL);
95            if (!path) {
96                log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry);
97                continue;
98            }
99           
100            if (g_str_has_suffix(path, ".py")) {
101                load_pyfile(path, main_dict);
102            }
103
104            g_free(path);
105        }
106    }
107
108    // Py_Finalize(); // TODO - there's no plugin_unload for us to do this in.
109}
110
111int main(int argc, char ** argv) {
112    printf("This is a bitlbee plugin, you should place it in bitlbee's plugins directory.");
113    return 0;
114}
Note: See TracBrowser for help on using the repository browser.