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 | |
---|
28 | void init_plugin() { |
---|
29 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
30 | GDir *dir; |
---|
31 | GError *error = NULL; |
---|
32 | |
---|
33 | printf("Initialising Python... "); |
---|
34 | Py_Initialize(); |
---|
35 | PyRun_SimpleString("print 'Python initialised!'\n"); |
---|
36 | |
---|
37 | /* Get a reference to the main module. */ |
---|
38 | PyObject* main_module = PyImport_AddModule("__main__"); |
---|
39 | |
---|
40 | /* Get the main module's dictionary */ |
---|
41 | PyObject* main_dict = PyModule_GetDict(main_module); |
---|
42 | |
---|
43 | dir = g_dir_open(global.conf->plugindir, 0, &error); |
---|
44 | if (dir) { |
---|
45 | const gchar *entry; |
---|
46 | char * path; |
---|
47 | while ((entry = g_dir_read_name(dir))) { |
---|
48 | path = g_build_filename(global.conf->plugindir, entry, NULL); |
---|
49 | if (!path) { |
---|
50 | log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry); |
---|
51 | continue; |
---|
52 | } |
---|
53 | |
---|
54 | if (g_str_has_suffix(path, ".py")) { |
---|
55 | printf("Loading python file %s\n", path); |
---|
56 | FILE* pyfile = fopen(path, "r"); |
---|
57 | /* Copy main dict to make sure that separate plugins |
---|
58 | run in separate environments */ |
---|
59 | PyObject* main_dict_copy = PyDict_Copy(main_dict); |
---|
60 | PyRun_File(pyfile, path, Py_file_input, main_dict_copy, main_dict_copy); |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | g_free(path); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | ret->name = "bpython"; |
---|
70 | |
---|
71 | register_protocol(ret); |
---|
72 | |
---|
73 | // Py_Finalize(); // TODO - there's no plugin_unload for us to do this in. |
---|
74 | } |
---|
75 | |
---|
76 | int main(int argc, char ** argv) { |
---|
77 | printf("This is a bitlbee plugin, you should place it in bitlbee's plugins directory."); |
---|
78 | return 0; |
---|
79 | } |
---|