summaryrefslogtreecommitdiff
path: root/TPlugin.cpp
blob: f3cbc487cb2a202e49f30c73752fda843e74afe9 (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
#include <list>
#include <string>
#include <fstream>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <cstring>
#include <dlfcn.h>

using namespace std;
#include "TPlugin.h"
#include "main.h"

#include "TConfig.h"
#include "TConfigReadOnly.h"

TPlugin::TPlugin(string pluginname)
{
	string path;
	plugininitfunc initfunc;
	
	path="./plugins/"+lcase(pluginname)+".so";
	handle=dlopen(path.c_str(),RTLD_NOW);
	if (!handle)
	{
		cout << "file '" << "./plugins/"+lcase(pluginname)+".so" << "' not found. " << dlerror() << endl;
		throw 1;
	}
	
	func=(pluginfunc)dlsym(handle, "plugin");
	if (func==NULL)
	{
		cout << "symbol 'plugin' not found."<< endl;
		dlclose(handle); handle=0;
		throw 2;
	}
		
	recv_msg=(pluginrecvfunc)dlsym(handle, "recv_message");
	if (recv_msg==NULL)
	{
		cout << "symbol 'recv_message' not found. this is not an error."<< endl;
	}
		
	initfunc=(plugininitfunc)dlsym(handle,"init");
	if (initfunc==NULL)
	{
		cout << "symbol 'init' not found." << endl;
		dlclose(handle); handle=0;
		throw 3;
	}
		
	(*initfunc) (&context_size, &default_flags_for_connections,&default_flags_for_channels,&default_flags_for_sessions);
	
	name=pluginname;
}

TPlugin::~TPlugin()
{
	if (handle) 
	{
		cout << "unloading '"<<name<<"'..." << endl;
		dlclose(handle);
	}
}

void TPlugin::execute(plugincontext* context,ircmessage msg,TPluginParent* parent, int reason)
{
	(*func) (context, msg, parent, config, reason);
}

void TPlugin::push_message(plugincontext* context, string subject, void* data, TPluginParent* parent)
{
	if (recv_msg)
		(*recv_msg) (context, subject, data, parent, config);
	else
		cout << "WARNING: tried to call a recv_msg for plugin '"<<name<<"', but it's not defined!" << endl;
}

int TPlugin::get_context_size(){return context_size;}
int TPlugin::get_default_flags_for_connections() {return default_flags_for_connections;}
int TPlugin::get_default_flags_for_channels() {return default_flags_for_channels;}
int TPlugin::get_default_flags_for_sessions() {return default_flags_for_sessions;}
string TPlugin::get_name(){return name;}