Welcome to mirror list, hosted at ThFree Co, Russian Federation.

remmina_plugin_python.c « src - gitlab.com/Remmina/Remmina.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: afdad3148d4b093a11a43f236654911d09f9b7f2 (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
/*
 * Remmina - The GTK+ Remote Desktop Client
 * Copyright (C) 2014-2021 Antenore Gatta, Giovanni Panozzo
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of portions of this program with the
 *  OpenSSL library under certain conditions as described in each
 *  individual source file, and distribute linked combinations
 *  including the two.
 *  You must obey the GNU General Public License in all respects
 *  for all of the code used other than OpenSSL. *  If you modify
 *  file(s) with this exception, you may extend this exception to your
 *  version of the file(s), but you are not obligated to do so. *  If you
 *  do not wish to do so, delete this exception statement from your
 *  version. *  If you delete this exception statement from all source
 *  files in the program, then also delete it here.
 *
 */

/**
 * @file remmina_plugin_python.c
 * @brief Remmina Python plugin loader.
 * @author Mathias Winterhalter
 * @date 14.10.2020
 *
 * When Remmina discovers Python scripts in the plugin root folder the plugin manager
 * passes the path to the Python plugin loader. There it gets executed and the plugin
 * classes get mapped to "real" Remmina plugin instances.
 *
 * For the communication between Remmina and Python the python module called 'remmina'
 * is initialized and loaded into the environment of the plugin script
 * (@see remmina_plugin_python_module.c).
 *
 * @see http://www.remmina.org/wp for more information.
 */

#include <gtk/gtk.h>
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>

#include "config.h"
#include "remmina/remmina_trace_calls.h"
#include "remmina_plugin_python.h"
#include "remmina_plugin_python_module.h"
#include "remmina_plugin_python_protocol_widget.h"

/**
 * @brief Extracts the filename without extension from a path.
 *
 * @param   in  The string to extract the filename from
 * @param   out The resulting filename without extension (must point to allocated memory).
 *
 * @return  The length of the filename extracted.
 */
static int basename_no_ext(const char* in, char** out);

/**
 *
 */
void remmina_plugin_python_init(void) {
    TRACE_CALL(__FUNC__);

    remmina_plugin_python_module_init();

    Py_Initialize();

    // Tell the Python engine where to look for Python scripts.
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('" REMMINA_RUNTIME_PLUGINDIR "')");

    remmina_plugin_python_protocol_widget_init();
}

gboolean remmina_plugin_python_load(RemminaPluginService* service, const gchar* name) {
    TRACE_CALL(__FUNC__);

    gchar* filename = NULL;
    if (basename_no_ext(name, &filename) == 0) {
        g_printerr("[%s:%s]: %s can not extract filename from name!\n", __FILE__, __LINE__, name);
        return FALSE;
    }

    PyObject *plugin_name = PyUnicode_DecodeFSDefault(filename);
    free(filename);

    if (!plugin_name) {
        g_printerr("[%s:%s]: Error converting plugin file name to PyUnicode!\n", __FILE__, __LINE__);
        return FALSE;
    }

    wchar_t* program_name = NULL;
    Py_ssize_t len = PyUnicode_AsWideChar(plugin_name, program_name, 0);
    program_name = malloc(sizeof(wchar_t)*len);
    if (!program_name) {
        g_printerr("[%s:%s]: Failed allocating %d bytes!\n", __FILE__, __LINE__, (sizeof(wchar_t)*len));
        return FALSE;
    }

    PyUnicode_AsWideChar(plugin_name, program_name, len);

    // This line works around the issue that in Python the sys.argv array is empty. This causes several problems since
    // many rely on the fact that the program name is set in sys.argv[0].
    PySys_SetArgv(1, &program_name);

    if (PyImport_Import(plugin_name)) {
        return TRUE;
    }

    g_print("Failed to load python plugin file: “%s”\n", name);
    PyErr_Print();
    return FALSE;
}


static int basename_no_ext(const char* in, char** out) {
    const char* base = strrchr(in, '/');
    if (base) {
        base++;
    }

    const char* base_end = strrchr(base, '.');
    if (!base_end) {
        base_end = base + strlen(base);
    }

    int length = base_end - base;
    int memsize = sizeof(char*) * ((length) + 1);
    *out = (char*)malloc(memsize);
    memset(*out, 0, memsize);
    strncpy(*out, base, length);
    (*out)[length] = '\0';

    return length;
}