summaryrefslogtreecommitdiff
path: root/meta2/settings.cpp
blob: 06c9b9f817f55d922bac7a97ac91c70512b0a5b6 (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
#include "common.h"
#include "settings.h"
#include "core_functions.h"

PLUGINLINK *core_link = 0;
typedef int (*ServiceFunc)(const char *,WPARAM,LPARAM);
ServiceFunc coreCallService, coreCallServiceSync;

int ServiceFuncRedirect(const char *service,WPARAM wParam, LPARAM lParam, ServiceFunc coreServiceFunc) {
	if(wParam ==0 
		|| strncmp(service, "DB/Contact/", 11) != 0
		|| (strcmp(MS_DB_CONTACT_GETSETTING, service) != 0 
			&& strcmp(MS_DB_CONTACT_GETSETTING_STR, service) != 0
			&& strcmp(MS_DB_CONTACT_GETSETTINGSTATIC, service) != 0
			&& strcmp(MS_DB_CONTACT_WRITESETTING, service) != 0
			&& strcmp(MS_DB_CONTACT_DELETESETTING, service) != 0
			&& strcmp(MS_DB_CONTACT_ENUMSETTINGS, service) != 0))
	{
		return coreServiceFunc(service, wParam, lParam);
	}

	HANDLE hContact = (HANDLE)wParam;
	if(strcmp(MS_DB_CONTACT_ENUMSETTINGS, service) == 0) {
		DBCONTACTENUMSETTINGS *ces = (DBCONTACTENUMSETTINGS *)lParam;
		if(IsMetacontact(hContact) && strcmp(ces->szModule, MODULE) != 0) {
			HANDLE hSub = Meta_GetMostOnline(hContact);
			return coreServiceFunc(service, (WPARAM)hSub, lParam);
		} else
			return coreServiceFunc(service, wParam, lParam);
	}
	
	const char *szSetting = 0, *szModule = 0;
	bool read; // true for get setting, false for write setting
	if(strcmp(MS_DB_CONTACT_WRITESETTING, service) == 0 || strcmp(MS_DB_CONTACT_DELETESETTING, service)) {
		read = false;
		DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING *)lParam;
		szSetting = cws->szSetting;
		szModule = cws->szModule;
	} else { // get setting
		read = true;
		DBCONTACTGETSETTING *cgs = (DBCONTACTGETSETTING *)lParam;
		szSetting = cgs->szSetting;
		szModule = cgs->szModule;
	}

	if(szModule == 0 || szSetting == 0
		|| strcmp(szModule, "Protocol") == 0
		|| strcmp(szModule, "_Filter") == 0
		|| (strcmp(szModule, MODULE) == 0
			&& (strcmp(szSetting, "MetaID") == 0 
				|| strcmp(szSetting, "IsSubcontact") == 0 
				|| strcmp(szSetting, "Default") == 0 
				|| strcmp(szSetting, "Handle") == 0 
				|| strcmp(szSetting, "WindowOpen") == 0 
				|| strcmp(szSetting, "ParentMetaID") == 0 
				|| strcmp(szSetting, "Status") == 0))
		|| !IsMetacontact(hContact))
	{
		return coreServiceFunc(service, wParam, lParam);
	}

	HANDLE hMeta = hContact;
	int ret;
	if((ret = coreServiceFunc(service, (WPARAM)hMeta, lParam)) != 0) {
		// if the setting does not exist in the metacontact, get it from the most online subcontact
		HANDLE hSub = Meta_GetMostOnline(hMeta);
		if(!hSub) return ret; // no most online - fail
		if((ret = coreServiceFunc(service, (WPARAM)hSub, lParam)) != 0) {
			// if it does not exist in the subcontact and we're using the meta proto module, try changing the module to the subcontact proto module
			if(strcmp(szModule, MODULE) == 0) {
				char *subProto = ContactProto(hSub);
				if(subProto) {
					if(read) {
						DBCONTACTGETSETTING *cgs = (DBCONTACTGETSETTING *)lParam;
						cgs->szModule = MODULE;
					} else {
						DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING *)lParam;
						cws->szModule = MODULE;
					}
					return coreServiceFunc(service, (WPARAM)hSub, lParam);
				} else
					return ret; // no subcontact proto - fail
			} else
				return ret; // not reading from meta proto module - fail
		} else 
			return 0; // got from subcontact
	} else
		return 0; // got from metacontact
}

int CallServiceRedirect(const char *service,WPARAM wParam, LPARAM lParam) {
	return ServiceFuncRedirect(service, wParam, lParam, coreCallService);
}

int CallServiceSyncRedirect(const char *service,WPARAM wParam, LPARAM lParam) {
	return ServiceFuncRedirect(service, wParam, lParam, coreCallServiceSync);
}

void InitSettings(PLUGINLINK *link) {
	core_link = link;
	coreCallService = core_link->CallService;
	coreCallServiceSync = core_link->CallServiceSync;
	core_link->CallService = CallServiceRedirect;
	core_link->CallServiceSync = CallServiceSyncRedirect;
}

void DeinitSettings() {
	core_link->CallService = coreCallService;
	core_link->CallServiceSync = coreCallServiceSync;
}