source: protocols/python/src/bpython.c @ 93302ef

Last change on this file since 93302ef was 93302ef, checked in by Nick Murdoch <nick@…>, at 2013-03-18T21:36:08Z

Turn bpython.c into an extension module with a dummy method from the Extending Python guide.

  • Property mode set to 100644
File size: 4.5 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 PyObject * bpython_register_protocol(PyObject *self, PyObject *args)
30{
31    const char *command;
32    int sts;
33
34    if (!PyArg_ParseTuple(args, "s", &command))
35        return NULL;
36    sts = system(command);
37    return Py_BuildValue("i", sts);
38}
39
40static PyMethodDef BpythonMethods[] = {
41    {"register_protocol",  bpython_register_protocol, METH_VARARGS,
42     "Register a protocol."},
43    {NULL, NULL, 0, NULL}        /* Sentinel */
44};
45
46PyMODINIT_FUNC initbpython(void)
47{
48    PyImport_AddModule("bpython");
49    Py_InitModule("bpython", BpythonMethods);
50}
51
52
53static void load_pyfile(char * path, PyObject * main_dict) {
54    FILE * pyfile;
55    PyObject * err;
56    struct prpl *ret;
57    ret = g_new0(struct prpl, 1);
58   
59    printf("Loading python file %s\n", path);
60    pyfile = fopen(path, "r");
61   
62    /* Copy main dict to make sure that separate plugins
63       run in separate environments */
64    PyObject * main_dict_copy = PyDict_Copy(main_dict);
65
66    /* Run the python file */
67    PyRun_File(pyfile, path, Py_file_input, main_dict_copy, main_dict_copy);
68   
69    PyObject * pluginname = PyDict_GetItemString(main_dict_copy, "name");
70    if ((err = PyErr_Occurred()) || !pluginname) {
71        printf("No plugin name\n");
72        PyErr_Print();
73        g_free(ret);
74        return;
75    }
76    PyObject * pluginname_unicode = PyObject_Unicode(pluginname);
77    PyObject * pluginname_encoded = PyUnicode_AsEncodedString(pluginname_unicode, "ascii", "ignore");
78    if ((err = PyErr_Occurred())) {
79        printf("Error encoding plugin name\n");
80        PyErr_Print();
81        g_free(ret);
82        return;
83    }
84    char * pluginname_tmp; /* reference, do not modify */
85    Py_ssize_t length;
86    PyBytes_AsStringAndSize(pluginname_encoded, &pluginname_tmp, &length);
87    if ((err = PyErr_Occurred())) {
88        printf("Bad plugin name\n");
89        PyErr_Print();
90        g_free(ret);
91        return;
92    }
93    ret->name = g_malloc0(length + 1);
94    memmove(ret->name, pluginname_tmp, length);
95
96    Py_DECREF(pluginname_encoded);
97    Py_DECREF(pluginname_unicode);
98
99    register_protocol(ret);
100}
101
102void init_plugin() {
103    GDir *dir;
104    GError *error = NULL;
105
106    printf("Initialising Python... ");
107    Py_Initialize();
108    PyRun_SimpleString("print 'Python initialised!'\n");
109   
110    /* Add our static module */
111    initbpython();
112
113    /* Get a reference to the main module. */
114    PyObject* main_module = PyImport_AddModule("__main__");
115
116    /* Get the main module's dictionary */
117    PyObject* main_dict = PyModule_GetDict(main_module);
118
119    dir = g_dir_open(global.conf->plugindir, 0, &error);
120    if (dir) {
121        const gchar *entry;
122        char * path;
123        while ((entry = g_dir_read_name(dir))) {
124            path = g_build_filename(global.conf->plugindir, entry, NULL);
125            if (!path) {
126                log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry);
127                continue;
128            }
129           
130            if (g_str_has_suffix(path, ".py")) {
131                load_pyfile(path, main_dict);
132            }
133
134            g_free(path);
135        }
136    }
137
138    // Py_Finalize(); // TODO - there's no plugin_unload for us to do this in.
139}
140
141int main(int argc, char ** argv) {
142    printf("This is a bitlbee plugin, you should place it in bitlbee's plugins directory.");
143    return 0;
144}
Note: See TracBrowser for help on using the repository browser.