-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.c
56 lines (46 loc) · 1.92 KB
/
plugin.c
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
#include <dlfcn.h>
#include <stdint.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <zlc_config/config.h>
#include "shared.h"
struct plugin_manager {
int server;
void *handle;
void (*plugin_init) (int *server, struct zl_config *cfg);
void (*plugin_receive_new_join) (enum SERVERS server, const char *nick, const char *room);
void (*plugin_receive_new_message) (enum SERVERS server, const char *room, const char *nick, const char *message);
const char *(*plugin_get_name) ();
};
static struct plugin_manager *pm;
static int size_pm;
extern struct zl_config *main_cfg;
void manager_plugin_init (const char **plugins, int size) {
size_pm = size;
pm = calloc (size_pm, sizeof (struct plugin_manager));
for (int i = 0; i < size_pm; i++) {
pm[i].handle = dlopen (plugins[i], RTLD_LAZY);
pm[i].plugin_init = (void (*) (int *, struct zl_config *)) dlsym (pm[i].handle, "plugin_init");
pm[i].plugin_receive_new_join = (void (*) (enum SERVERS, const char *, const char *)) dlsym (pm[i].handle, "plugin_receive_new_join");
pm[i].plugin_receive_new_message = (void (*) (enum SERVERS, const char *, const char *, const char *)) dlsym (pm[i].handle, "plugin_receive_new_message");
pm[i].plugin_get_name = (const char *(*) (void)) dlsym (pm[i].handle, "plugin_get_name");
int server = N_SERVERS;
pm[i].plugin_init (&server, main_cfg);
pm[i].server = server;
}
}
void manager_plugin_receive_new_join (enum SERVERS server, const char *nick, const char *room) {
for (int i = 0; i < size_pm; i++) {
if (pm[i].server == server || pm[i].server == N_SERVERS) {
pm[i].plugin_receive_new_join (server, nick, room);
}
}
}
void manager_plugin_receive_new_message (enum SERVERS server, const char *room, const char *nick, const char *message) {
for (int i = 0; i < size_pm; i++) {
if (pm[i].server == server || pm[i].server == N_SERVERS) {
pm[i].plugin_receive_new_message (server, room, nick, message);
}
}
}