File: plugins.c

package info (click to toggle)
ctrlproxy 3.0.7-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 1,668 kB
  • ctags: 2,045
  • sloc: ansic: 15,358; sh: 2,805; xml: 1,078; python: 346; makefile: 302; perl: 18
file content (138 lines) | stat: -rw-r--r-- 3,403 bytes parent folder | download
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
/*
	ctrlproxy: A modular IRC proxy
	(c) 2002-2004 Jelmer Vernooij <jelmer@nl.linux.org>

	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 3 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "internals.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* globals */
struct plugin *current_plugin = NULL;
GList *plugins = NULL;

/**
 * Check whether a certain plugin has already been loaded.
 *
 * @param name Name of the plugin
 */
gboolean plugin_loaded(const char *name)
{
	GList *gl;
	for (gl = plugins; gl; gl = gl->next) {
		struct plugin *p = (struct plugin *)gl->data;
		if (p && p->ops && p->ops->name && !strcmp(p->ops->name, name)) 
			return TRUE;
	}
	return FALSE;
}

struct plugin *load_plugin(const char *modulesdir, const char *name)
{
	GModule *m = NULL;
	struct plugin_ops *ops = NULL;
	struct plugin *p = g_new0(struct plugin, 1);
	gchar *path_name = NULL;

	/* Try to load from .so file */
	if (!ops) {

		if (g_file_test(name, G_FILE_TEST_EXISTS))path_name = g_strdup(name);
		else path_name = g_module_build_path(modulesdir, name);
	
		m = g_module_open(path_name, 0);

		if (!m) {
			log_global(LOG_ERROR, "Unable to open module %s(%s), ignoring", path_name, g_module_error());
			g_free(path_name);
			g_free(p);
			return NULL;
		}

		if (!g_module_symbol(m, "plugin", (gpointer)&ops)) {
			log_global(LOG_ERROR, "%s: No valid plugin information found", 
				strchr(path_name, '/')?(strrchr(path_name, '/')+1):"error"
					   );
			g_free(path_name);
			g_free(p);
			return NULL;
		}
	}

	if (plugin_loaded(ops->name)) {
		log_global(LOG_WARNING, "%s: Plugin already loaded", ops->name);
		g_free(path_name);
		g_free(p);
		return NULL;
	}

	if (ops->version != CTRLPROXY_PLUGIN_VERSION) {
		log_global(LOG_WARNING, "%s: Plugin has incompatible version %d, expected %d", 
				   ops->name, ops->version, CTRLPROXY_PLUGIN_VERSION);
		g_free(path_name);
		g_free(p);
		return NULL;
	}

	g_free(path_name);

	p->module = m;
	p->ops = ops;

	if (!p->ops->init()) {
		log_global( LOG_ERROR, "%s: Error during initialization.", p->ops->name);
		g_free(p);
		return NULL;
	}

	plugins = g_list_append(plugins, p);

	return p;
}

gboolean init_plugins(const char *plugin_dir)
{
	gboolean ret = TRUE;

	if (!g_module_supported()) {
		log_global(LOG_WARNING, "DSO's not supported on this platform. Not loading any plugins");
	} else {
		const char *name;
		GDir *dir = g_dir_open(plugin_dir, 0, NULL);
		if (!dir) {
			log_global(LOG_WARNING, "Plugin dir does not exist, not loading plugins");
			return FALSE;
		}

		while ((name = g_dir_read_name(dir))) {
			if (strcmp(name + strlen(name) - strlen(G_MODULE_SUFFIX), G_MODULE_SUFFIX) != 0)
				continue;

			if (!load_plugin(plugin_dir, name))
				ret = FALSE;
		}

		g_dir_close(dir);
	}

	return ret;
}

GList *get_plugin_list() {
	return plugins;
}