source: nscp/include/nscapi/nscapi_core_wrapper.cpp @ acf0660

0.4.00.4.10.4.2
Last change on this file since acf0660 was acf0660, checked in by Michael Medin <michael@…>, 3 years ago

renamed to_string as it clashed with some boost classes, as well as refactoring of the cmake build scripts to work better..

  • Property mode set to 100644
File size: 25.3 KB
Line 
1/**************************************************************************
2*   Copyright (C) 2004-2007 by Michael Medin <michael@medin.name>         *
3*                                                                         *
4*   This code is part of NSClient++ - http://trac.nakednuns.org/nscp      *
5*                                                                         *
6*   This program is free software; you can redistribute it and/or modify  *
7*   it under the terms of the GNU General Public License as published by  *
8*   the Free Software Foundation; either version 2 of the License, or     *
9*   (at your option) any later version.                                   *
10*                                                                         *
11*   This program is distributed in the hope that it will be useful,       *
12*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14*   GNU General Public License for more details.                          *
15*                                                                         *
16*   You should have received a copy of the GNU General Public License     *
17*   along with this program; if not, write to the                         *
18*   Free Software Foundation, Inc.,                                       *
19*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20***************************************************************************/
21
22#include <boost/foreach.hpp>
23#include <boost/tokenizer.hpp>
24
25#include <nscapi/nscapi_core_wrapper.hpp>
26#include <settings/macros.h>
27#include <arrayBuffer.h>
28#include <strEx.h>
29
30#include "../libs/protobuf/plugin.proto.h"
31
32using namespace nscp::helpers;
33
34#define CORE_LOG_ERROR_STD(msg) CORE_LOG_ERROR(((std::wstring)msg).c_str())
35#define CORE_LOG_ERROR(msg) CORE_ANY_MSG(msg,NSCAPI::error)
36
37#define CORE_LOG_CRITICAL_STD(msg) CORE_LOG_CRITICAL(((std::wstring)msg).c_str())
38#define CORE_LOG_CRITICAL(msg) CORE_ANY_MSG(msg,NSCAPI::critical)
39
40#define CORE_LOG_MESSAGE_STD(msg) CORE_LOG_MESSAGE(((std::wstring)msg).c_str())
41#define CORE_LOG_MESSAGE(msg) CORE_ANY_MSG(msg,NSCAPI::log)
42
43#define CORE_DEBUG_MSG_STD(msg) CORE_DEBUG_MSG((std::wstring)msg)
44#define CORE_DEBUG_MSG(msg) CORE_ANY_MSG(msg,NSCAPI::debug)
45
46#define CORE_ANY_MSG(msg, type) Message(type, __FILEW__, __LINE__, msg)
47
48
49//////////////////////////////////////////////////////////////////////////
50// Callbacks into the core
51//////////////////////////////////////////////////////////////////////////
52
53/**
54 * Callback to send a message through to the core
55 *
56 * @param msgType Message type (debug, warning, etc.)
57 * @param file File where message was generated (__FILE__)
58 * @param line Line where message was generated (__LINE__)
59 * @param message Message in human readable format
60 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
61 */
62void nscapi::core_wrapper::Message(int msgType, std::wstring file, int line, std::wstring message) {
63        if (fNSAPIMessage) {
64                if ((msgType == NSCAPI::debug) && (!logDebug()))
65                        return;
66                /*
67                std::wstring::size_type pos = file.find_last_of("\\");
68                if (pos != std::wstring::npos)
69                        file = file.substr(pos);
70                        */
71                return fNSAPIMessage(msgType, file.c_str(), line, message.c_str());
72        }
73        else
74                std::wcout << _T("*** *** *** NSCore not loaded, dumping log: ") << file << _T(":") << line << _T(": ") << std::endl << message << std::endl;
75}
76/**
77 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
78 * @param command Command to inject (password should not be included.
79 * @return The result (if any) of the command.
80 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unknown inject error occurs.
81 */
82
83/**
84 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
85 * @param command Command to inject
86 * @param argLen The length of the argument buffer
87 * @param **argument The argument buffer
88 * @param *returnMessageBuffer Buffer to hold the returned message
89 * @param returnMessageBufferLen Length of returnMessageBuffer
90 * @param *returnPerfBuffer Buffer to hold the returned performance data
91 * @param returnPerfBufferLen returnPerfBuffer
92 * @return The returned status of the command
93 */
94NSCAPI::nagiosReturn nscapi::core_wrapper::InjectCommandRAW(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
95{
96        if (!fNSAPIInject)
97                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
98        return fNSAPIInject(command, request, request_len, response, response_len);
99}
100
101void nscapi::core_wrapper::DestroyBuffer(char**buffer) {
102        if (!fNSAPIDestroyBuffer)
103                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
104        return fNSAPIDestroyBuffer(buffer);
105}
106
107NSCAPI::errorReturn nscapi::core_wrapper::NotifyChannel(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::string result) {
108        if (!fNSAPINotify)
109                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
110        return fNSAPINotify(channel.c_str(), command.c_str(), code, result.c_str(), result.size());
111}
112
113/**
114* Inject a request command in the core (this will then be sent to the plug-in stack for processing)
115* @param command Command to inject (password should not be included.
116* @param argLen The length of the argument buffer
117* @param **argument The argument buffer
118* @param message The return message buffer
119* @param perf The return performance data buffer
120* @return The return of the command
121*/
122NSCAPI::nagiosReturn nscapi::core_wrapper::InjectSimpleCommand(const std::wstring command, const std::list<std::wstring> argument, std::wstring & msg, std::wstring & perf)
123{
124        if (!fNSAPIInject)
125                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
126
127        std::string response;
128        NSCAPI::nagiosReturn ret = InjectCommand(command, argument, response);
129        if (!response.empty()) {
130                PluginCommand::ResponseMessage rsp_msg;
131                rsp_msg.ParseFromString(response);
132                if (rsp_msg.payload_size() != 1) {
133                        CORE_LOG_ERROR_STD(_T("Failed to extract return message not 1 payload: ") + strEx::itos(rsp_msg.payload_size()));
134                        return NSCAPI::returnUNKNOWN;
135                }
136                msg = to_wstring(rsp_msg.payload(0).message());
137        }
138        return ret;
139}
140/**
141* Inject a request command in the core (this will then be sent to the plug-in stack for processing)
142* @param command Command to inject (password should not be included.
143* @param argLen The length of the argument buffer
144* @param **argument The argument buffer
145* @param message The return message buffer
146* @param perf The return performance data buffer
147* @return The return of the command
148*/
149NSCAPI::nagiosReturn nscapi::core_wrapper::InjectCommand(const std::wstring command, const std::list<std::wstring> argument, std::string & result)
150{
151        if (!fNSAPIInject)
152                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
153
154
155        PluginCommand::RequestMessage message;
156        PluginCommand::Header *hdr = message.mutable_header();
157        hdr->set_type(PluginCommand::Header_Type_REQUEST);
158        hdr->set_version(PluginCommand::Header_Version_VERSION_1);
159
160        PluginCommand::Request *req = message.add_payload();
161        req->set_command(to_string(command));
162        req->set_version(PluginCommand::Request_Version_VERSION_1);
163
164        BOOST_FOREACH(std::wstring s, argument)
165                req->add_arguments(to_string(s));
166
167        std::string request;
168        message.SerializeToString(&request);
169
170        char *buffer = NULL;
171        unsigned int buffer_size = 0;
172
173        NSCAPI::nagiosReturn retC = InjectCommandRAW(command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
174
175        if (buffer_size > 0 && buffer != NULL) {
176                PluginCommand::ResponseMessage rsp_msg;
177                result = std::string(buffer, buffer_size);
178        }
179
180        DestroyBuffer(&buffer);
181        switch (retC) {
182                case NSCAPI::returnIgnored:
183                        CORE_LOG_MESSAGE_STD(_T("No handler for command '") + command + _T("'."));
184                        break;
185                case NSCAPI::returnOK:
186                case NSCAPI::returnCRIT:
187                case NSCAPI::returnWARN:
188                case NSCAPI::returnUNKNOWN:
189                        break;
190                default:
191                        throw nscapi::nscapi_exception(_T("Unknown return code when injecting: ") + std::wstring(command));
192        }
193        return retC;
194}
195
196/**
197 * A wrapper around the InjetCommand that is simpler to use.
198 * Parses a string by splitting and makes the array and also manages return buffers and such.
199 * @param command The command to execute
200 * @param buffer The buffer to split
201 * @param spliwchar_t The char to use as splitter
202 * @param message The return message buffer
203 * @param perf The return performance data buffer
204 * @return The result of the command
205 */
206NSCAPI::nagiosReturn nscapi::core_wrapper::InjectSplitAndCommand(const wchar_t* command, wchar_t* buffer, wchar_t splitChar, std::wstring & message, std::wstring & perf)
207{
208        if (!fNSAPIInject)
209                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
210
211        std::wstring args = std::wstring(buffer);
212        boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(args, boost::escaped_list_separator<wchar_t>(L'\\', splitChar, L'\"'));
213        std::list<std::wstring> arglist;
214        BOOST_FOREACH(std::wstring s, tok)
215                arglist.push_back(s);
216        return InjectSimpleCommand(command, arglist, message, perf);
217}
218/**
219 * A wrapper around the InjetCommand that is simpler to use.
220 * @param command The command to execute
221 * @param buffer The buffer to split
222 * @param spliwchar_t The char to use as splitter
223 * @param message The return message buffer
224 * @param perf The return performance data buffer
225 * @return The result of the command
226 */
227NSCAPI::nagiosReturn nscapi::core_wrapper::InjectSplitAndCommand(const std::wstring command, const std::wstring buffer, wchar_t spliwchar_t, std::wstring & message, std::wstring & perf, bool escape) {
228        if (!fNSAPIInject)
229                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
230        boost::tokenizer<boost::escaped_list_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(buffer, boost::escaped_list_separator<wchar_t>(L'\\', spliwchar_t, L'\"'));
231        std::list<std::wstring> arglist;
232        BOOST_FOREACH(std::wstring s, tok)
233                arglist.push_back(s);
234        return InjectSimpleCommand(command.c_str(), arglist, message, perf);
235}
236/**
237 * Ask the core to shutdown (only works when run as a service, o/w does nothing ?
238 * @todo Check if this might cause damage if not run as a service.
239 */
240void nscapi::core_wrapper::StopService(void) {
241        if (fNSAPIStopServer)
242                fNSAPIStopServer();
243}
244/**
245 * Close the program (usefull for tray/testmode) without stopping the service (unless this is the service).
246 * @author mickem
247 */
248void nscapi::core_wrapper::Exit(void) {
249        if (fNSAPIExit)
250                fNSAPIExit();
251}
252/**
253 * Retrieve a string from the settings subsystem (INI-file)
254 * Might possibly be located in the registry in the future.
255 *
256 * @param section Section key (generally module specific, make sure this is "unique")
257 * @param key The key to retrieve
258 * @param defaultValue A default value (if no value is set in the settings file)
259 * @return the current value or defaultValue if no value is set.
260 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an error occurs.
261 */
262std::wstring nscapi::core_wrapper::getSettingsString(std::wstring section, std::wstring key, std::wstring defaultValue) {
263        if (!fNSAPIGetSettingsString)
264                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
265        unsigned int buf_len = getBufferLength();
266        wchar_t *buffer = new wchar_t[buf_len+1];
267        if (fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
268                delete [] buffer;
269                throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
270        }
271        std::wstring ret = buffer;
272        delete [] buffer;
273        return ret;
274}
275/**
276 * Get a section of settings strings
277 * @param section The section to retrieve
278 * @return The keys in the section
279 */
280std::list<std::wstring> nscapi::core_wrapper::getSettingsSection(std::wstring section) {
281        if (!fNSAPIGetSettingsSection)
282                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
283        arrayBuffer::arrayBuffer aBuffer = NULL;
284        unsigned int argLen = 0;
285        if (fNSAPIGetSettingsSection(section.c_str(), &aBuffer, &argLen) != NSCAPI::isSuccess) {
286                throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
287        }
288        std::list<std::wstring> ret = arrayBuffer::arrayBuffer2list(argLen, aBuffer);
289        if (fNSAPIReleaseSettingsSectionBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
290                throw nscapi::nscapi_exception(_T("Settings could not be destroyed."));
291        }
292        if (aBuffer != NULL)
293                throw nscapi::nscapi_exception(_T("buffer is not null?."));
294        return ret;
295}
296/**
297 * Retrieve an int from the settings subsystem (INI-file)
298 * Might possibly be located in the registry in the future.
299 *
300 * @param section Section key (generally module specific, make sure this is "unique")
301 * @param key The key to retrieve
302 * @param defaultValue A default value (if no value is set in the settings file)
303 * @return the current value or defaultValue if no value is set.
304 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
305 */
306int nscapi::core_wrapper::getSettingsInt(std::wstring section, std::wstring key, int defaultValue) {
307        if (!fNSAPIGetSettingsInt)
308                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
309        return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue);
310}
311void nscapi::core_wrapper::settings_register_key(std::wstring path, std::wstring key, NSCAPI::settings_type type, std::wstring title, std::wstring description, std::wstring defaultValue, bool advanced) {
312        if (!fNSAPISettingsRegKey)
313                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
314        fNSAPISettingsRegKey(path.c_str(), key.c_str(), type, title.c_str(), description.c_str(), defaultValue.c_str(), advanced);
315}
316void nscapi::core_wrapper::settings_register_path(std::wstring path, std::wstring title, std::wstring description, bool advanced) {
317        if (!fNSAPISettingsRegPath)
318                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
319        fNSAPISettingsRegPath(path.c_str(), title.c_str(), description.c_str(), advanced);
320}
321
322
323void nscapi::core_wrapper::settings_save() {
324        if (!fNSAPISettingsSave)
325                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
326        fNSAPISettingsSave();
327}
328
329/**
330 * Retrieve the application name (in human readable format) from the core.
331 * @return A string representing the application name.
332 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
333 */
334std::wstring nscapi::core_wrapper::getApplicationName() {
335        if (!fNSAPIGetApplicationName)
336                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
337        unsigned int buf_len = getBufferLength();
338        wchar_t *buffer = new wchar_t[buf_len+1];
339        if (fNSAPIGetApplicationName(buffer, buf_len) != NSCAPI::isSuccess) {
340                delete [] buffer;
341                throw nscapi::nscapi_exception(_T("Application name could not be retrieved"));
342        }
343        std::wstring ret = buffer;
344        delete [] buffer;
345        return ret;
346}
347/**
348 * Retrieve the directory root of the application from the core.
349 * @return A string representing the base path.
350 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
351 */
352std::wstring nscapi::core_wrapper::getBasePath() {
353        if (!fNSAPIGetBasePath)
354                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
355        unsigned int buf_len = getBufferLength();
356        wchar_t *buffer = new wchar_t[buf_len+1];
357        if (fNSAPIGetBasePath(buffer, buf_len) != NSCAPI::isSuccess) {
358                delete [] buffer;
359                throw nscapi::nscapi_exception(_T("Base path could not be retrieved"));
360        }
361        std::wstring ret = buffer;
362        delete [] buffer;
363        return ret;
364}
365
366unsigned int nscapi::core_wrapper::getBufferLength() {
367        static unsigned int len = 0;
368        if (len == 0) {
369                len = getSettingsInt(setting_keys::settings_def::PAYLOAD_LEN_PATH, setting_keys::settings_def::PAYLOAD_LEN, setting_keys::settings_def::PAYLOAD_LEN_DEFAULT);
370        }
371        return len;
372}
373
374
375bool nscapi::core_wrapper::logDebug() {
376        enum status {unknown, debug, nodebug };
377        static status d = unknown;
378        if (d == unknown) {
379                if (checkLogMessages(debug)== NSCAPI::istrue)
380                        d = debug;
381                else
382                        d = nodebug;
383        }
384        return (d == debug);
385}
386
387std::wstring nscapi::core_wrapper::Encrypt(std::wstring str, unsigned int algorithm) {
388        if (!fNSAPIEncrypt)
389                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
390        unsigned int len = 0;
391        // @todo investigate potential problems with static_cast<unsigned int>
392        fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
393        len+=2;
394        wchar_t *buf = new wchar_t[len+1];
395        NSCAPI::errorReturn ret = fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
396        if (ret == NSCAPI::isSuccess) {
397                std::wstring ret = buf;
398                delete [] buf;
399                return ret;
400        }
401        return _T("");
402}
403std::wstring nscapi::core_wrapper::Decrypt(std::wstring str, unsigned int algorithm) {
404        if (!fNSAPIDecrypt)
405                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
406        unsigned int len = 0;
407        // @todo investigate potential problems with: static_cast<unsigned int>(str.size())
408        fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
409        len+=2;
410        wchar_t *buf = new wchar_t[len+1];
411        NSCAPI::errorReturn ret = fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
412        if (ret == NSCAPI::isSuccess) {
413                std::wstring ret = buf;
414                delete [] buf;
415                return ret;
416        }
417        return _T("");
418}
419NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsString(std::wstring section, std::wstring key, std::wstring value) {
420        if (!fNSAPISetSettingsString)
421                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
422        return fNSAPISetSettingsString(section.c_str(), key.c_str(), value.c_str());
423}
424NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsInt(std::wstring section, std::wstring key, int value) {
425        if (!fNSAPISetSettingsInt)
426                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
427        return fNSAPISetSettingsInt(section.c_str(), key.c_str(), value);
428}
429NSCAPI::errorReturn nscapi::core_wrapper::WriteSettings(int type) {
430        if (!fNSAPIWriteSettings)
431                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
432        return fNSAPIWriteSettings(type);
433}
434NSCAPI::errorReturn nscapi::core_wrapper::ReadSettings(int type) {
435        if (!fNSAPIReadSettings)
436                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
437        return fNSAPIReadSettings(type);
438}
439NSCAPI::errorReturn nscapi::core_wrapper::Rehash(int flag) {
440        if (!fNSAPIRehash)
441                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
442        return fNSAPIRehash(flag);
443}
444nscapi::core_wrapper::plugin_info_list nscapi::core_wrapper::getPluginList() {
445        if (!fNSAPIGetPluginList || !fNSAPIReleasePluginList)
446                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
447        plugin_info_list ret;
448       
449       
450        int len = 0;
451        //NSCAPI::plugin_info_list **list2;
452        //NSCAPI::plugin_info_list *list[1];
453        NSCAPI::plugin_info *list[1];
454        //typedef NSCAPI::errorReturn (*lpNSAPIGetPluginList)(int *len, NSAPI_plugin_info** list);
455        //typedef NSCAPI::errorReturn (*lpNSAPIReleasePluginList)(int len, NSAPI_plugin_info** list);
456        NSCAPI::errorReturn err = fNSAPIGetPluginList(&len, list);
457        if (err != NSCAPI::isSuccess)
458                return ret;
459        for (int i=0;i<len;i++) {
460                plugin_info_type info;
461                info.description = (*list)[i].description;
462                info.name = (*list)[i].name;
463                info.dll = (*list)[i].dll;
464                ret.push_back(info);
465        }
466        fNSAPIReleasePluginList(len, list);
467        return ret;
468}
469
470std::list<std::wstring> nscapi::core_wrapper::getAllCommandNames() {
471        if (!fNSAPIGetAllCommandNames || !fNSAPIReleaseAllCommandNamessBuffer )
472                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
473        arrayBuffer::arrayBuffer aBuffer = NULL;
474        unsigned int argLen = 0;
475        if (fNSAPIGetAllCommandNames(&aBuffer, &argLen) != NSCAPI::isSuccess) {
476                throw nscapi::nscapi_exception(_T("Commands could not be retrieved."));
477        }
478        std::list<std::wstring> ret = arrayBuffer::arrayBuffer2list(argLen, aBuffer);
479        if (fNSAPIReleaseAllCommandNamessBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
480                throw nscapi::nscapi_exception(_T("Commands could not be destroyed."));
481        }
482        if (aBuffer != NULL)
483                throw nscapi::nscapi_exception(_T("buffer is not null?."));
484        return ret;
485}
486std::wstring nscapi::core_wrapper::describeCommand(std::wstring command) {
487        if (!fNSAPIDescribeCommand)
488                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
489        unsigned int buf_len = getBufferLength();
490        wchar_t *buffer = new wchar_t[buf_len+1];
491        if (fNSAPIDescribeCommand(command.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
492                delete [] buffer;
493                throw nscapi::nscapi_exception(_T("Base path could not be retrieved"));
494        }
495        std::wstring ret = buffer;
496        delete [] buffer;
497        return ret;
498}
499void nscapi::core_wrapper::registerCommand(std::wstring command, std::wstring description) {
500        if (!fNSAPIRegisterCommand)
501                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
502        fNSAPIRegisterCommand(id_, command.c_str(), description.c_str());
503}
504
505
506bool nscapi::core_wrapper::checkLogMessages(int type) {
507        if (!fNSAPICheckLogMessages)
508                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
509        return fNSAPICheckLogMessages(type) == NSCAPI::istrue;
510}
511/**
512 * Retrieve the application version as a string (in human readable format) from the core.
513 * @return A string representing the application version.
514 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
515 */
516std::wstring nscapi::core_wrapper::getApplicationVersionString() {
517        if (!fNSAPIGetApplicationVersionStr)
518                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
519        unsigned int buf_len = getBufferLength();
520        wchar_t *buffer = new wchar_t[buf_len+1];
521        if (fNSAPIGetApplicationVersionStr(buffer, buf_len) != NSCAPI::isSuccess) {
522                delete [] buffer;
523                return _T("");
524        }
525        std::wstring ret = buffer;
526        delete [] buffer;
527        return ret;
528}
529
530/**
531 * Wrapper function around the ModuleHelperInit call.
532 * This wrapper retrieves all pointers and stores them for future use.
533 * @param f A function pointer to a function that can be used to load function from the core.
534 * @return NSCAPI::success or NSCAPI::failure
535 */
536bool nscapi::core_wrapper::load_endpoints(unsigned int id, nscapi::core_api::lpNSAPILoader f) {
537        id_ = id;
538        fNSAPIGetApplicationName = (nscapi::core_api::lpNSAPIGetApplicationName)f(_T("NSAPIGetApplicationName"));
539        fNSAPIGetApplicationVersionStr = (nscapi::core_api::lpNSAPIGetApplicationVersionStr)f(_T("NSAPIGetApplicationVersionStr"));
540        fNSAPIGetSettingsInt = (nscapi::core_api::lpNSAPIGetSettingsInt)f(_T("NSAPIGetSettingsInt"));
541        fNSAPIGetSettingsString = (nscapi::core_api::lpNSAPIGetSettingsString)f(_T("NSAPIGetSettingsString"));
542        fNSAPIGetSettingsSection = (nscapi::core_api::lpNSAPIGetSettingsSection)f(_T("NSAPIGetSettingsSection"));
543        fNSAPIReleaseSettingsSectionBuffer = (nscapi::core_api::lpNSAPIReleaseSettingsSectionBuffer)f(_T("NSAPIReleaseSettingsSectionBuffer"));
544        fNSAPIMessage = (nscapi::core_api::lpNSAPIMessage)f(_T("NSAPIMessage"));
545        fNSAPIStopServer = (nscapi::core_api::lpNSAPIStopServer)f(_T("NSAPIStopServer"));
546        //fNSAPIExit = (nscapi::core_api::lpNSAPIExit)f(_T("NSAPIExit"));
547        fNSAPIInject = (nscapi::core_api::lpNSAPIInject)f(_T("NSAPIInject"));
548        fNSAPIDestroyBuffer = (nscapi::core_api::lpNSAPIDestroyBuffer)f(_T("NSAPIDestroyBuffer"));
549        fNSAPINotify = (nscapi::core_api::lpNSAPINotify)f(_T("NSAPINotify"));
550        fNSAPIGetBasePath = (nscapi::core_api::lpNSAPIGetBasePath)f(_T("NSAPIGetBasePath"));
551        fNSAPICheckLogMessages = (nscapi::core_api::lpNSAPICheckLogMessages)f(_T("NSAPICheckLogMessages"));
552        fNSAPIDecrypt = (nscapi::core_api::lpNSAPIDecrypt)f(_T("NSAPIDecrypt"));
553        fNSAPIEncrypt = (nscapi::core_api::lpNSAPIEncrypt)f(_T("NSAPIEncrypt"));
554        fNSAPISetSettingsString = (nscapi::core_api::lpNSAPISetSettingsString)f(_T("NSAPISetSettingsString"));
555        fNSAPISetSettingsInt = (nscapi::core_api::lpNSAPISetSettingsInt)f(_T("NSAPISetSettingsInt"));
556        fNSAPIWriteSettings = (nscapi::core_api::lpNSAPIWriteSettings)f(_T("NSAPIWriteSettings"));
557        fNSAPIReadSettings = (nscapi::core_api::lpNSAPIReadSettings)f(_T("NSAPIReadSettings"));
558        fNSAPIRehash = (nscapi::core_api::lpNSAPIRehash)f(_T("NSAPIRehash"));
559
560        fNSAPIDescribeCommand = (nscapi::core_api::lpNSAPIDescribeCommand)f(_T("NSAPIDescribeCommand"));
561        fNSAPIGetAllCommandNames = (nscapi::core_api::lpNSAPIGetAllCommandNames)f(_T("NSAPIGetAllCommandNames"));
562        fNSAPIReleaseAllCommandNamessBuffer = (nscapi::core_api::lpNSAPIReleaseAllCommandNamessBuffer)f(_T("NSAPIReleaseAllCommandNamessBuffer"));
563        fNSAPIRegisterCommand = (nscapi::core_api::lpNSAPIRegisterCommand)f(_T("NSAPIRegisterCommand"));
564
565        fNSAPISettingsRegKey = (nscapi::core_api::lpNSAPISettingsRegKey)f(_T("NSAPISettingsRegKey"));
566        fNSAPISettingsRegPath = (nscapi::core_api::lpNSAPISettingsRegPath)f(_T("NSAPISettingsRegPath"));
567
568        fNSAPIGetPluginList = (nscapi::core_api::lpNSAPIGetPluginList)f(_T("NSAPIGetPluginList"));
569        fNSAPIReleasePluginList = (nscapi::core_api::lpNSAPIReleasePluginList)f(_T("NSAPIReleasePluginList"));
570
571        fNSAPISettingsSave = (nscapi::core_api::lpNSAPISettingsSave)f(_T("NSAPISettingsSave"));
572       
573        return true;
574}
Note: See TracBrowser for help on using the repository browser.