summaryrefslogtreecommitdiff
path: root/libpurple/plugins/mono/loader/mono.c
blob: 6810ef42a0e456fb52d2dc6ca5eb473441aef159 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Mono Plugin Loader
 *
 * -- Thanks to the perl plugin loader for all the great tips ;-)
 *
 * Eoin Coffey
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "internal.h"
#include "debug.h"
#include "plugin.h"
#include "version.h"
#include "mono-helper.h"

#define MONO_PLUGIN_ID "core-mono"

/******************************************************************************
 * Loader Stuff
 *****************************************************************************/
/* probes the given plugin to determine if its a plugin */
static gboolean probe_mono_plugin(PurplePlugin *plugin)
{
	MonoAssembly *assm;
	MonoMethod *m = NULL;
	MonoObject *plugin_info;
	gboolean found_load = FALSE, found_unload = FALSE, found_destroy = FALSE;
	gpointer iter = NULL;

	PurplePluginInfo *info;
	PurpleMonoPlugin *mplug;

	char *file = plugin->path;

	assm = mono_domain_assembly_open(ml_get_domain(), file);

	if (!assm) {
		return FALSE;
	}

	purple_debug(PURPLE_DEBUG_INFO, "mono", "Probing plugin\n");

	if (ml_is_api_dll(mono_assembly_get_image(assm))) {
		purple_debug(PURPLE_DEBUG_INFO, "mono", "Found our PurpleAPI.dll\n");
		return FALSE;
	}

	info = g_new0(PurplePluginInfo, 1);
	mplug = g_new0(PurpleMonoPlugin, 1);

	mplug->signal_data = NULL;

	mplug->assm = assm;

	mplug->klass = ml_find_plugin_class(mono_assembly_get_image(mplug->assm));
	if (!mplug->klass) {
		purple_debug(PURPLE_DEBUG_ERROR, "mono", "no plugin class in \'%s\'\n", file);
		return FALSE;
	}

	mplug->obj = mono_object_new(ml_get_domain(), mplug->klass);
	if (!mplug->obj) {
		purple_debug(PURPLE_DEBUG_ERROR, "mono", "obj not valid\n");
		return FALSE;
	}

	mono_runtime_object_init(mplug->obj);

	while ((m = mono_class_get_methods(mplug->klass, &iter))) {
		purple_debug_info("mono", "plugin method: %s\n", mono_method_get_name(m));
		if (strcmp(mono_method_get_name(m), "Load") == 0) {
			mplug->load = m;
			found_load = TRUE;
		} else if (strcmp(mono_method_get_name(m), "Unload") == 0) {
			mplug->unload = m;
			found_unload = TRUE;
		} else if (strcmp(mono_method_get_name(m), "Destroy") == 0) {
			mplug->destroy = m;
			found_destroy = TRUE;
		}
	}

	if (!(found_load && found_unload && found_destroy)) {
		purple_debug(PURPLE_DEBUG_ERROR, "mono", "did not find the required methods\n");
		return FALSE;
	}

	plugin_info = ml_get_info_prop(mplug->obj);

	/* now that the methods are filled out we can populate
	   the info struct with all the needed info */

	info->id = ml_get_prop_string(plugin_info, "Id");
	info->name = ml_get_prop_string(plugin_info, "Name");
	info->version = ml_get_prop_string(plugin_info, "Version");
	info->summary = ml_get_prop_string(plugin_info, "Summary");
	info->description = ml_get_prop_string(plugin_info, "Description");
	info->author = ml_get_prop_string(plugin_info, "Author");
	info->homepage = ml_get_prop_string(plugin_info, "Homepage");

	info->magic = PURPLE_PLUGIN_MAGIC;
	info->major_version = PURPLE_MAJOR_VERSION;
	info->minor_version = PURPLE_MINOR_VERSION;
	info->type = PURPLE_PLUGIN_STANDARD;

	/* this plugin depends on us; duh */
	info->dependencies = g_list_append(info->dependencies, MONO_PLUGIN_ID);
	mplug->plugin = plugin;

	plugin->info = info;
	info->extra_info = mplug;

	ml_add_plugin(mplug);

	return purple_plugin_register(plugin);
}

/* Loads a Mono Plugin by calling 'load' in the class */
static gboolean load_mono_plugin(PurplePlugin *plugin)
{
	PurpleMonoPlugin *mplug;

	purple_debug(PURPLE_DEBUG_INFO, "mono", "Loading plugin\n");

	mplug = (PurpleMonoPlugin*)plugin->info->extra_info;

	ml_invoke(mplug->load, mplug->obj, NULL);

	return TRUE;
}

/* Unloads a Mono Plugin by calling 'unload' in the class */
static gboolean unload_mono_plugin(PurplePlugin *plugin)
{
	PurpleMonoPlugin *mplug;

	purple_debug(PURPLE_DEBUG_INFO, "mono", "Unloading plugin\n");

	mplug = (PurpleMonoPlugin*)plugin->info->extra_info;

	purple_signals_disconnect_by_handle((gpointer)mplug->klass);
	g_list_foreach(mplug->signal_data, (GFunc)g_free, NULL);
	g_list_free(mplug->signal_data);
	mplug->signal_data = NULL;

	ml_invoke(mplug->unload, mplug->obj, NULL);

	return TRUE;
}

static void destroy_mono_plugin(PurplePlugin *plugin)
{
	PurpleMonoPlugin *mplug;

	purple_debug(PURPLE_DEBUG_INFO, "mono", "Destroying plugin\n");

	mplug = (PurpleMonoPlugin*)plugin->info->extra_info;

	ml_invoke(mplug->destroy, mplug->obj, NULL);

	if (plugin->info) {
		g_free(plugin->info->name);
		g_free(plugin->info->version);
		g_free(plugin->info->summary);
		g_free(plugin->info->description);
		g_free(plugin->info->author);
		g_free(plugin->info->homepage);
	}

	if (mplug) {
		if (mplug->assm) {
			mono_assembly_close(mplug->assm);
		}

		g_free(mplug);
		mplug = NULL;
	}
}

/******************************************************************************
 * Plugin Stuff
 *****************************************************************************/
static void plugin_destroy(PurplePlugin *plugin)
{
	ml_uninit();
}

static PurplePluginLoaderInfo loader_info =
{
	NULL,
	probe_mono_plugin,
	load_mono_plugin,
	unload_mono_plugin,
	destroy_mono_plugin,

	/* padding */
	NULL,
	NULL,
	NULL,
	NULL
};

static PurplePluginInfo info =
{
	PURPLE_PLUGIN_MAGIC,
	PURPLE_MAJOR_VERSION,
	PURPLE_MINOR_VERSION,
	PURPLE_PLUGIN_LOADER,
	NULL,
	0,
	NULL,
	PURPLE_PRIORITY_DEFAULT,
	MONO_PLUGIN_ID,
	N_("Mono Plugin Loader"),
	DISPLAY_VERSION,
	N_("Loads .NET plugins with Mono."),
	N_("Loads .NET plugins with Mono."),
	"Eoin Coffey <ecoffey@simla.colostate.edu>",
	PURPLE_WEBSITE,
	NULL,
	NULL,
	plugin_destroy,
	NULL,
	&loader_info,
	NULL,
	NULL,

	/* padding */
	NULL,
	NULL,
	NULL,
	NULL
};

static void init_plugin(PurplePlugin *plugin)
{
	ml_init();

	loader_info.exts = g_list_append(loader_info.exts, "dll");
}

PURPLE_INIT_PLUGIN(mono, init_plugin, info)