-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin.c
More file actions
229 lines (176 loc) · 5.73 KB
/
Copy pathplugin.c
File metadata and controls
229 lines (176 loc) · 5.73 KB
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
#include "plugin.h"
#include "plugins/plugin_client.h"
#include "global.h"
#include "thread.h"
#include "dl.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <dirent.h>
#define PLUGIN_DIR "plugins"
LIST_HEAD(dl_library_holder_list_t, dl_library_holder_t);
struct dl_library_holder_t {
dl_library_t plugin;
LIST_ENTRY(dl_library_holder_t) list;
};
static struct dl_library_holder_list_t dl_library_holder_head;
LIST_HEAD(plugin_threads_list_t, plugin_threads_t);
struct plugin_threads_t {
thread_t thread;
LIST_ENTRY(plugin_threads_t) list;
};
static struct plugin_threads_list_t plugin_threads_head;
static void *start_thread(void * pdata)
{
((struct plugin_t *)pdata)->run(0);
return NULL;
}
void plugin_free()
{
struct plugin_t * iterator, * temp;
struct dl_library_holder_t * library_it, * temp_it;
debug("Freeing plugin resources.\n");
temp = NULL;
LIST_FOREACH(iterator, &plugin_slist_head, list) {
free(temp);
temp = NULL;
debug("Freeing plugin: %s\n", iterator->name);
temp = iterator;
}
free(temp);
struct plugin_threads_t * iterator_threads, * t_temp;
debug("Killing threads.\n");
t_temp = NULL;
LIST_FOREACH(iterator_threads, &plugin_threads_head, list) {
free(t_temp);
thread_kill(iterator_threads->thread);
t_temp = iterator_threads;
}
free(t_temp);
temp_it = NULL;
LIST_FOREACH(library_it, &dl_library_holder_head, list) {
if (temp_it) free(temp_it);
dl_close(library_it->plugin);
temp_it = library_it;
}
if (temp_it) free(temp_it);
}
void plugin_start_daemons(struct irc_t * irc)
{
int err;
struct plugin_t * iterator;
LIST_INIT(&plugin_threads_head);
LIST_FOREACH(iterator, &plugin_slist_head, list) {
struct plugin_t * plugin = iterator;
struct plugin_threads_t * plugin_thread;
if (!(plugin->type & PLUGIN_TYPE_DAEMON))
continue;
plugin_thread = malloc(sizeof(struct plugin_threads_t));
memset(plugin_thread, 0, sizeof(struct plugin_threads_t));
debug("Looper plugin [%s] is started\n", plugin->name);
if ((err = thread_create(&plugin_thread->thread, start_thread, plugin)) != 0) {
debug("Thread could not be started. pthread_create, errno = %d\n", err);
}
LIST_INSERT_HEAD(&plugin_threads_head, plugin_thread, list);
}
}
struct plugin_list_t * plugin_find_commands(char * name, struct plugin_list_t * temp_plugin_list)
{
struct plugin_t * iterator;
LIST_INIT(temp_plugin_list);
LIST_FOREACH(iterator, &plugin_slist_head, list) {
struct plugin_t * plugin = iterator;
if (!(plugin->type & PLUGIN_TYPE_COMMAND))
continue;
if (strcmp(plugin->name, name) == 0) {
LIST_INSERT_HEAD(temp_plugin_list, plugin, list);
debug("Found plugin %s\n", name);
}
}
return temp_plugin_list;
}
void send_message(struct irc_t * irc, char * response)
{
char buffer[4096];
snprintf(buffer, sizeof buffer, "%s\r\n", response);
socket_write(irc->sockfd, buffer, strlen(buffer));
}
static void plugin_load_file(char * file, struct irc_t * irc)
{
struct dl_library_holder_t * library_holder;
dl_library_t plugin_file;
struct plugin_t * (*init)(void);
struct plugin_t * plugin;
debug("Loading native plugin \"%s\"\n", file);
if ((plugin_file = dl_open(file)) == NULL) {
debug("The plugin file \"%s\" could not be opened.\n", file);
return;
}
if ((init = dl_sym(plugin_file, "init")) == NULL) {
debug("The plugin \"%s\" has no exported init function symbol.\n", file);
}
/* initialize and get handle to the plugin */
if ((plugin = init()) == NULL) {
debug("The plugin initialization is failed for reasons unknown."
" Hope it's last words were meaningful, if any.\n");
return;
}
/* Attach callbacks to be used by plugin. */
plugin->send_message = send_message;
plugin->irc = irc;
/* If plugin is of type grep, acquire grep keywords. */
if (plugin->type & PLUGIN_TYPE_GREP) {
char ** keywords;
if ((keywords = dl_sym(plugin_file, "keywords"))) {
plugin->keywords = keywords;
} else {
debug("The plugin \"%s\" is of type `grep', but has no "
"exported grep keywords symbols found. Discarding.\n", file);
return;
}
}
LIST_INSERT_HEAD(&plugin_slist_head, plugin, list);
library_holder = malloc (sizeof (*library_holder));
library_holder->plugin = plugin_file;
LIST_INSERT_HEAD(&dl_library_holder_head, library_holder, list);
}
void plugin_init(struct irc_t * irc)
{
DIR * dir;
struct dirent * dirent;
LIST_INIT(&plugin_slist_head);
dir = opendir(PLUGIN_DIR);
if (!dir) {
debug("No plugins directory found. No plugins will be loaded.\n");
return;
}
while ((dirent = readdir(dir)) != NULL) {
#ifdef __WIN32__
if (strstr(dirent->d_name, ".dll")) {
#else
if (strstr(dirent->d_name, ".so")) {
#endif // __WIN32__
char plugin_path[200];
snprintf(plugin_path, sizeof plugin_path, "%s/%s", PLUGIN_DIR, dirent->d_name);
plugin_load_file(plugin_path, irc);
}
}
closedir(dir);
}
#if TEST_PLUGIN
int main()
{
struct plugin_slist_t * iterator;
LIST_INIT(&dl_library_holder_head);
LIST_INIT(&plugin_threads_head);
plugin_init();
puts("Iterating the plugins...");
LIST_FOREACH(iterator, &plugin_slist_head, plugin_slist) {
struct plugin_t * plugin = iterator->plugin;
debug("%s\n", plugin->name);
}
puts("Finished.");
return 0;
}
#endif