summaryrefslogtreecommitdiffstats
path: root/pycore.c
blob: 63cd2c69bda5fc828cb846e6aaf375343f8c037e (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <Python.h>
#include <string.h>
#include <signal.h>
#include <assert.h>
#include "pyirssi.h"
#include "pycore.h"
#include "pyloader.h"
#include "pymodule.h"
#include "factory.h"


/*XXX: copy parse into utils */
static void cmd_exec(const char *data)
{
    PyObject *co;
    PyObject *ret;
    PyObject *d;
    PyObject *m;
    char *cmd;

    if (!*data)
        cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);

    cmd = g_strconcat(data, "\n", NULL);
    
    m = PyImport_AddModule("__main__");
    if (!m)
        goto error;

    d = PyModule_GetDict(m);
    if (!d)
        goto error;

    co = Py_CompileString(cmd, "<stdin>", Py_single_input);
    if (!co)
        goto error;

    ret = PyEval_EvalCode((PyCodeObject *)co, d, d);
    Py_DECREF(co);
    Py_XDECREF(ret);

error:
    g_free(cmd);
    if (PyErr_Occurred())
        PyErr_Print();
}

static void cmd_load(const char *data)
{
    char **argv;

    argv = g_strsplit(data, " ", -1);
    if (*argv == NULL || **argv == '\0')
    {
        g_strfreev(argv);
        cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
    }

    pyloader_load_script_argv(argv);
    g_strfreev(argv);
}

static void cmd_unload(const char *data)
{
    void *free_arg;
    char *script;

    if (!cmd_get_params(data, &free_arg, 1, &script))
        return;

    if (*script == '\0')
        cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);

    pyloader_unload_script(script); 
    
    cmd_params_free(free_arg);
}

static void cmd_list()
{
    char buf[128];
    GSList *list;

    list = pyloader_list();

    g_snprintf(buf, sizeof(buf), "%-15s %s", "Name", "File");

    if (list != NULL)
    {
        GSList *node;

        printtext_string(NULL, NULL, MSGLEVEL_CLIENTCRAP, buf);
        for (node = list; node != NULL; node = node->next)
        {
            PY_LIST_REC *item = node->data;
            g_snprintf(buf, sizeof(buf), "%-15s %s", item->name, item->file); 

            printtext_string(NULL, NULL, MSGLEVEL_CLIENTCRAP, buf);
        }
    }
    else
        printtext_string(NULL, NULL, MSGLEVEL_CLIENTERROR, "No python scripts are loaded");

    pyloader_list_destroy(&list);
}

#if 0
/* why doesn't this get called? */
static void intr_catch(int sig)
{
    printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "got sig %d", sig);
    PyErr_SetInterrupt();
}
#endif 

void irssi_python_init(void)
{
    Py_InitializeEx(0);

    if (!pyloader_init() || !pymodule_init() || !factory_init()) 
    {
        printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Failed to load Python");
        return;
    }

    /*PyImport_ImportModule("irssi_startup");*/
    /* Install the custom output handlers, import hook and reload function */
    /* XXX: handle import error */
    PyRun_SimpleString(
            "import irssi_startup\n"
    );

    //assert(signal(SIGINT, intr_catch) != SIG_ERR); 
    
    command_bind("pyload", NULL, (SIGNAL_FUNC) cmd_load);
    command_bind("pyunload", NULL, (SIGNAL_FUNC) cmd_unload);
    command_bind("pylist", NULL, (SIGNAL_FUNC) cmd_list);
    command_bind("pyexec", NULL, (SIGNAL_FUNC) cmd_exec);
    module_register(MODULE_NAME, "core");
}

void irssi_python_deinit(void)
{
    command_unbind("pyload", (SIGNAL_FUNC) cmd_load);
    command_unbind("pyunload", (SIGNAL_FUNC) cmd_unload);
    command_unbind("pylist", (SIGNAL_FUNC) cmd_list);
    command_unbind("pyexec", (SIGNAL_FUNC) cmd_exec);

    pymodule_deinit();
    pyloader_deinit();
    Py_Finalize();
}