1 #ifndef _KVI_KVS_MODULEINTERFACE_H_
2 #define _KVI_KVS_MODULEINTERFACE_H_
3 //=============================================================================
4 //
5 //   File : KviKvsModuleInterface.h
6 //   Creation date : Tue 16 Dec 2003 00:27:54 by Szymon Stefanek
7 //
8 //   This file is part of the KVIrc IRC client distribution
9 //   Copyright (C) 2003-2010 Szymon Stefanek <pragma at kvirc dot net>
10 //
11 //   This program is FREE software. You can redistribute it and/or
12 //   modify it under the terms of the GNU General Public License
13 //   as published by the Free Software Foundation; either version 2
14 //   of the License, or (at your option) any later version.
15 //
16 //   This program is distributed in the HOPE that it will be USEFUL,
17 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
18 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 //   See the GNU General Public License for more details.
20 //
21 //   You should have received a copy of the GNU General Public License
22 //   along with this program. If not, write to the Free Software Foundation,
23 //   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 //=============================================================================
26 
27 #include "kvi_settings.h"
28 
29 #include "KviPointerHashTable.h"
30 
31 #include "KviKvsRunTimeCall.h"
32 #include "KviKvsParameterProcessor.h"
33 #include "KviKvsSwitchList.h"
34 #include "KviKvsScript.h"
35 #include "KviQString.h"
36 
37 #include <vector>
38 
39 class KviModule;
40 class KviKvsTreeNodeDataList;
41 
42 class KVIRC_API KviKvsModuleRunTimeCall : public KviKvsRunTimeCall
43 {
44 protected:
45 	KviModule * m_pModule;
46 
47 public:
KviKvsModuleRunTimeCall(KviModule * pModule,KviKvsRunTimeContext * pContext,KviKvsVariantList * pParams)48 	KviKvsModuleRunTimeCall(KviModule * pModule,
49 	    KviKvsRunTimeContext * pContext,
50 	    KviKvsVariantList * pParams)
51 	    : KviKvsRunTimeCall(pContext, pParams), m_pModule(pModule) {}
~KviKvsModuleRunTimeCall()52 	~KviKvsModuleRunTimeCall(){};
53 
54 public:
module()55 	KviModule * module() { return m_pModule; };
56 };
57 
58 class KVIRC_API KviKvsModuleEventCall : public KviKvsModuleRunTimeCall
59 {
60 public:
KviKvsModuleEventCall(KviModule * pModule,KviKvsRunTimeContext * pContext,KviKvsVariantList * pParams)61 	KviKvsModuleEventCall(KviModule * pModule,
62 	    KviKvsRunTimeContext * pContext,
63 	    KviKvsVariantList * pParams)
64 	    : KviKvsModuleRunTimeCall(pModule, pContext, pParams){};
~KviKvsModuleEventCall()65 	~KviKvsModuleEventCall(){};
66 };
67 
68 class KVIRC_API KviKvsModuleCommandCall : public KviKvsModuleRunTimeCall
69 {
70 protected:
71 	KviKvsSwitchList * m_pSwitchList;
72 
73 public:
KviKvsModuleCommandCall(KviModule * pModule,KviKvsRunTimeContext * pContext,KviKvsVariantList * pParams,KviKvsSwitchList * pSwitches)74 	KviKvsModuleCommandCall(KviModule * pModule,
75 	    KviKvsRunTimeContext * pContext,
76 	    KviKvsVariantList * pParams,
77 	    KviKvsSwitchList * pSwitches)
78 	    : KviKvsModuleRunTimeCall(pModule, pContext, pParams), m_pSwitchList(pSwitches){};
~KviKvsModuleCommandCall()79 	~KviKvsModuleCommandCall(){};
80 
81 public:
switches()82 	KviKvsSwitchList * switches() { return m_pSwitchList; };
switchList()83 	KviKvsSwitchList * switchList() { return m_pSwitchList; };
84 
85 	// forwarders for the switch list
hasSwitch(unsigned short u,const QString & szSwitch)86 	bool hasSwitch(unsigned short u, const QString & szSwitch) { return (m_pSwitchList->find(u, szSwitch) != 0); };
getSwitch(unsigned short u,const QString & szSwitch)87 	KviKvsVariant * getSwitch(unsigned short u, const QString & szSwitch) { return m_pSwitchList->find(u, szSwitch); };
88 };
89 
90 class KVIRC_API KviKvsModuleCallbackCommandCall : public KviKvsModuleCommandCall
91 {
92 protected:
93 	const KviKvsScript * m_pCallback;
94 	KviKvsTreeNodeDataList * m_pParameterDataList; // core subtree that rappresents the parameter list
95 public:
KviKvsModuleCallbackCommandCall(KviModule * pModule,KviKvsRunTimeContext * pContext,KviKvsVariantList * pParams,KviKvsSwitchList * pSwitches,const KviKvsScript * pCallback,KviKvsTreeNodeDataList * pDataList)96 	KviKvsModuleCallbackCommandCall(KviModule * pModule,
97 	    KviKvsRunTimeContext * pContext,
98 	    KviKvsVariantList * pParams,
99 	    KviKvsSwitchList * pSwitches,
100 	    const KviKvsScript * pCallback,
101 	    KviKvsTreeNodeDataList * pDataList)
102 	    : KviKvsModuleCommandCall(pModule, pContext, pParams, pSwitches), m_pCallback(pCallback), m_pParameterDataList(pDataList){};
~KviKvsModuleCallbackCommandCall()103 	~KviKvsModuleCallbackCommandCall(){};
104 
105 public:
106 	// Never nullptr, but may have empty code
callback()107 	const KviKvsScript * callback() { return m_pCallback; };
108 	virtual bool getParameterCode(unsigned int uParamIdx, QString & szParamBuffer);
109 };
110 
111 class KVIRC_API KviKvsModuleFunctionCall : public KviKvsModuleRunTimeCall
112 {
113 	friend class KviKvsTreeNodeModuleFunctionCall;
114 
115 protected:
116 	KviKvsVariant * m_pResult;
117 
118 public:
KviKvsModuleFunctionCall(KviModule * pModule,KviKvsRunTimeContext * pContext,KviKvsVariantList * pParams,KviKvsVariant * pResult)119 	KviKvsModuleFunctionCall(KviModule * pModule,
120 	    KviKvsRunTimeContext * pContext,
121 	    KviKvsVariantList * pParams,
122 	    KviKvsVariant * pResult)
123 	    : KviKvsModuleRunTimeCall(pModule, pContext, pParams), m_pResult(pResult){};
~KviKvsModuleFunctionCall()124 	~KviKvsModuleFunctionCall(){};
125 
126 public:
returnValue()127 	KviKvsVariant * returnValue() { return m_pResult; };
128 };
129 
130 typedef bool (*KviKvsModuleSimpleCommandExecRoutine)(KviKvsModuleCommandCall * c);
131 typedef bool (*KviKvsModuleFunctionExecRoutine)(KviKvsModuleFunctionCall * c);
132 typedef bool (*KviKvsModuleCallbackCommandExecRoutine)(KviKvsModuleCallbackCommandCall * c);
133 typedef bool (*KviKvsModuleEventHandlerRoutine)(KviKvsModuleEventCall * c);
134 
135 class KVIRC_API KviKvsModuleInterface
136 {
137 	friend class KviKvsModuleManager;
138 
139 public:
140 	KviKvsModuleInterface();
141 	~KviKvsModuleInterface();
142 
143 protected:
144 	KviPointerHashTable<QString, KviKvsModuleSimpleCommandExecRoutine> * m_pModuleSimpleCommandExecRoutineDict;
145 	KviPointerHashTable<QString, KviKvsModuleFunctionExecRoutine> * m_pModuleFunctionExecRoutineDict;
146 	KviPointerHashTable<QString, KviKvsModuleCallbackCommandExecRoutine> * m_pModuleCallbackCommandExecRoutineDict;
147 
148 public:
149 	void kvsRegisterSimpleCommand(const QString & szCommand, KviKvsModuleSimpleCommandExecRoutine r);
150 	void kvsRegisterCallbackCommand(const QString & szCommand, KviKvsModuleCallbackCommandExecRoutine r);
151 	void kvsRegisterFunction(const QString & szFunction, KviKvsModuleFunctionExecRoutine r);
152 	bool kvsRegisterAppEventHandler(unsigned int iEventIdx, KviKvsModuleEventHandlerRoutine r);
153 	bool kvsRegisterRawEventHandler(unsigned int iRawIdx, KviKvsModuleEventHandlerRoutine r);
154 
kvsUnregisterSimpleCommand(const QString & szCommand)155 	void kvsUnregisterSimpleCommand(const QString & szCommand)
156 	{
157 		m_pModuleSimpleCommandExecRoutineDict->remove(szCommand);
158 	};
kvsUnregisterCallbackCommand(const QString & szCommand)159 	void kvsUnregisterCallbackCommand(const QString & szCommand)
160 	{
161 		m_pModuleCallbackCommandExecRoutineDict->remove(szCommand);
162 	};
kvsUnregisterFunction(const QString & szFunction)163 	void kvsUnregisterFunction(const QString & szFunction)
164 	{
165 		m_pModuleFunctionExecRoutineDict->remove(szFunction);
166 	};
167 	void kvsUnregisterAppEventHandler(unsigned int iEventIdx);
168 	void kvsUnregisterRawEventHandler(unsigned int iRawIdx);
169 
kvsUnregisterAllSimpleCommands()170 	void kvsUnregisterAllSimpleCommands()
171 	{
172 		m_pModuleSimpleCommandExecRoutineDict->clear();
173 	};
kvsUnregisterAllCallbackCommands()174 	void kvsUnregisterAllCallbackCommands()
175 	{
176 		m_pModuleCallbackCommandExecRoutineDict->clear();
177 	};
kvsUnregisterAllFunctions()178 	void kvsUnregisterAllFunctions()
179 	{
180 		m_pModuleFunctionExecRoutineDict->clear();
181 	};
182 	void kvsUnregisterAllAppEventHandlers();
183 	void kvsUnregisterAllRawEventHandlers();
184 	void kvsUnregisterAllEventHandlers();
185 
kvsFindSimpleCommand(const QString & szCommand)186 	KviKvsModuleSimpleCommandExecRoutine * kvsFindSimpleCommand(const QString & szCommand)
187 	{
188 		return m_pModuleSimpleCommandExecRoutineDict->find(szCommand);
189 	};
kvsFindCallbackCommand(const QString & szCommand)190 	KviKvsModuleCallbackCommandExecRoutine * kvsFindCallbackCommand(const QString & szCommand)
191 	{
192 		return m_pModuleCallbackCommandExecRoutineDict->find(szCommand);
193 	};
kvsFindFunction(const QString & szFunction)194 	KviKvsModuleFunctionExecRoutine * kvsFindFunction(const QString & szFunction)
195 	{
196 		return m_pModuleFunctionExecRoutineDict->find(szFunction);
197 	};
198 
199 	void completeCommand(const QString & cmd, std::vector<QString> & matches);
200 	void completeFunction(const QString & cmd, std::vector<QString> & matches);
201 	void getAllFunctionsCommandsModule(QStringList * list, QString & szModuleName);
202 
203 protected:
204 	void registerDefaultCommands();
205 };
206 
207 #define KVSM_REGISTER_SIMPLE_COMMAND(_pModule, _szCmd, _procname) \
208 	_pModule->kvsRegisterSimpleCommand(_szCmd, _procname);
209 
210 #define KVSM_UNREGISTER_SIMPLE_COMMAND(_pModule, _szCmd) \
211 	_pModule->kvsUnregisterSimpleCommand(_szCmd);
212 
213 #define KVSM_REGISTER_CALLBACK_COMMAND(_pModule, _szCmd, _procname) \
214 	_pModule->kvsRegisterCallbackCommand(_szCmd, _procname);
215 
216 #define KVSM_UNREGISTER_CALLBACK_COMMAND(_pModule, _szCmd) \
217 	_pModule->kvsUnregisterCallbackCommand(_szCmd);
218 
219 #define KVSM_REGISTER_FUNCTION(_pModule, _szFnc, _procname) \
220 	_pModule->kvsRegisterFunction(_szFnc, _procname);
221 
222 #define KVSM_UNREGISTER_FUNCTION(_pModule, _szFnc) \
223 	_pModule->kvsUnregisterFunction(_szFnc);
224 
225 #define KVSM_UNREGISTER_ALL_SIMPLE_COMMANDS(_pModule) \
226 	_pModule->kvsUnregisterAllSimpleCommands();
227 
228 #define KVSM_UNREGISTER_ALL_CALLBACK_COMMANDS(_pModule) \
229 	_pModule->kvsUnregisterAllCallbackCommands();
230 
231 #define KVSM_UNREGISTER_ALL_FUNCTIONS(_pModule) \
232 	_pModule->kvsUnregisterAllFunctions();
233 
234 #define KVSM_PARAMETER(a, b, c, d) KVS_PARAMETER(a, b, c, d)
235 #define KVSM_PARAMETER_IGNORED(a) KVS_PARAMETER_IGNORED(a)
236 
237 #define KVSM_PARAMETERS_BEGIN(pCall) \
238 	KVS_PARAMETERS_BEGIN(parameter_format_list)
239 
240 #define KVSM_PARAMETERS_END(pCall)                                                                   \
241 	KVS_PARAMETERS_END                                                                               \
242 	if(!KviKvsParameterProcessor::process(pCall->params(), pCall->context(), parameter_format_list)) \
243 		return false;
244 
245 #define KVSM_REQUIRE_CONNECTION(pCall)            \
246 	if(!pCall->window()->context())               \
247 		return c->context()->errorNoIrcContext(); \
248 	if(!pCall->window()->connection())            \
249 		return c->context()->warningNoIrcConnection();
250 
251 #endif //!_KVI_KVS_MODULEINTERFACE_H_
252