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

0.4.00.4.10.4.2
Last change on this file since f7a074d was f7a074d, checked in by Michael Medin <michael@…>, 20 months ago
  • Had some "vacation" so no updates for a while
  • A lot of fixes to the NSCA parts (now 100% compatible with old settings file)
  • Added option to read sections (to plugin API)
  • Fixed issues in settings wrapper so child paths are also mapped (not just keys)
  • Many fixes related to NRPE/NSCA/*
  • "RC quality" expect RC within the week (only need to fix default config file somehow)
  • Upgraded all unit test to use the simplified API
  • Fixed a some unicode issues in PythonScript module
  • Improved threading a bit
  • Fixed CheckSystem (service check)
  • Fixed duplicate keys (when replacing) in old settings client
  • Property mode set to 100644
File size: 30.6 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 <strEx.h>
26#include <arrayBuffer.h>
27
28#include <nscapi/nscapi_core_wrapper.hpp>
29#include <nscapi/nscapi_plugin_wrapper.hpp>
30#include <nscapi/functions.hpp>
31#include <settings/macros.h>
32
33#include <protobuf/plugin.pb.h>
34
35using namespace nscp::helpers;
36
37#define CORE_LOG_ERROR_STD(msg) CORE_LOG_ERROR(((std::wstring)msg).c_str())
38#define CORE_LOG_ERROR(msg) CORE_ANY_MSG(msg,NSCAPI::error)
39
40#define CORE_LOG_CRITICAL_STD(msg) CORE_LOG_CRITICAL(((std::wstring)msg).c_str())
41#define CORE_LOG_CRITICAL(msg) CORE_ANY_MSG(msg,NSCAPI::critical)
42
43#define CORE_LOG_MESSAGE_STD(msg) CORE_LOG_MESSAGE(((std::wstring)msg).c_str())
44#define CORE_LOG_MESSAGE(msg) CORE_ANY_MSG(msg,NSCAPI::log)
45
46#define CORE_DEBUG_MSG_STD(msg) CORE_DEBUG_MSG((std::wstring)msg)
47#define CORE_DEBUG_MSG(msg) CORE_ANY_MSG(msg,NSCAPI::debug)
48
49#define CORE_ANY_MSG(msg, type) Message(type, __FILE__, __LINE__, msg)
50
51
52//////////////////////////////////////////////////////////////////////////
53// Callbacks into the core
54//////////////////////////////////////////////////////////////////////////
55
56/**
57 * Callback to send a message through to the core
58 *
59 * @param msgType Message type (debug, warning, etc.)
60 * @param file File where message was generated (__FILE__)
61 * @param line Line where message was generated (__LINE__)
62 * @param message Message in human readable format
63 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
64 */
65void nscapi::core_wrapper::Message(int msgType, std::string file, int line, std::wstring logMessage) {
66        if (fNSAPIMessage) {
67                if ((msgType == NSCAPI::debug) && (!logDebug()))
68                        return;
69                std::string str;
70                try {
71                        Plugin::LogEntry message;
72                        Plugin::LogEntry::Entry *msg = message.add_entry();
73                        msg->set_level(nscapi::functions::log_to_gpb(msgType));
74                        msg->set_file(file);
75                        msg->set_line(line);
76                        msg->set_message(to_string(logMessage));
77                        if (!message.SerializeToString(&str)) {
78                                std::cout << "Failed to generate message";
79                        }
80                        return fNSAPIMessage(str.c_str(), str.size());
81                } catch (...) {
82                        std::wcout << _T("Failed to generate message: ");
83                }
84//              return fNSAPIMessage(to_string(logMessage).c_str(), logMessage.size());
85        }
86        else
87                std::wcout << _T("*** *** *** NSCore not loaded, dumping log: ") << to_wstring(file) << _T(":") << line << _T(": ") << std::endl << logMessage << std::endl;
88}
89/**
90 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
91 * @param command Command to inject (password should not be included.
92 * @return The result (if any) of the command.
93 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unknown inject error occurs.
94 */
95
96/**
97 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
98 * @param command Command to inject
99 * @param argLen The length of the argument buffer
100 * @param **argument The argument buffer
101 * @param *returnMessageBuffer Buffer to hold the returned message
102 * @param returnMessageBufferLen Length of returnMessageBuffer
103 * @param *returnPerfBuffer Buffer to hold the returned performance data
104 * @param returnPerfBufferLen returnPerfBuffer
105 * @return The returned status of the command
106 */
107NSCAPI::nagiosReturn nscapi::core_wrapper::query(const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
108{
109        if (!fNSAPIInject)
110                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
111        return fNSAPIInject(command, request, request_len, response, response_len);
112}
113
114
115void nscapi::core_wrapper::DestroyBuffer(char**buffer) {
116        if (!fNSAPIDestroyBuffer)
117                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
118        return fNSAPIDestroyBuffer(buffer);
119}
120
121
122NSCAPI::errorReturn nscapi::core_wrapper::submit_message(std::wstring channel, std::string request, std::string &response) {
123
124        if (!fNSAPINotify)
125                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
126        char *buffer = NULL;
127        unsigned int buffer_size = 0;
128        NSCAPI::nagiosReturn ret = submit_message(channel.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
129
130        if (buffer_size > 0 && buffer != NULL) {
131                response = std::string(buffer, buffer_size);
132        }
133
134        DestroyBuffer(&buffer);
135        return ret;
136}
137
138NSCAPI::errorReturn nscapi::core_wrapper::reload(std::wstring module) {
139
140        if (!fNSAPIReload)
141                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
142        return fNSAPIReload(module.c_str());
143}
144
145bool nscapi::core_wrapper::submit_simple_message(std::wstring channel, std::wstring command, NSCAPI::nagiosReturn code, std::wstring & message, std::wstring & perf, std::wstring & response) {
146        std::string request, buffer;
147        nscapi::functions::create_simple_submit_request(channel, command, code, message, perf, request);
148        NSCAPI::nagiosReturn ret = submit_message(channel, request, buffer);
149        if (ret == NSCAPI::returnIgnored) {
150                response = _T("No handler for this message");
151                return false;
152        }
153        if (buffer.size() == 0) {
154                response = _T("Missing response from submission");
155                return false;
156        }
157        nscapi::functions::parse_simple_submit_response(buffer, response);
158        return ret == NSCAPI::isSuccess;
159}
160
161NSCAPI::nagiosReturn nscapi::core_wrapper::submit_message(const wchar_t* channel, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
162{
163        if (!fNSAPINotify)
164                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
165        return fNSAPINotify(channel, request, request_len, response, response_len);
166}
167
168/**
169* Inject a request command in the core (this will then be sent to the plug-in stack for processing)
170* @param command Command to inject (password should not be included.
171* @param argLen The length of the argument buffer
172* @param **argument The argument buffer
173* @param message The return message buffer
174* @param perf The return performance data buffer
175* @return The return of the command
176*/
177NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query(const std::wstring command, const std::list<std::wstring> & argument, std::wstring & msg, std::wstring & perf)
178{
179        if (!fNSAPIInject)
180                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
181        std::string response;
182        NSCAPI::nagiosReturn ret = simple_query(command, argument, response);
183        if (!response.empty()) {
184                try {
185                        nscapi::functions::parse_simple_query_response(response, msg, perf);
186                } catch (std::exception &e) {
187                        CORE_LOG_ERROR_STD(_T("Failed to extract return message: ") + utf8::cvt<std::wstring>(e.what()));
188                        return NSCAPI::returnUNKNOWN;
189                }
190        }
191        return ret;
192}
193/**
194* Inject a request command in the core (this will then be sent to the plug-in stack for processing)
195* @param command Command to inject (password should not be included.
196* @param argLen The length of the argument buffer
197* @param **argument The argument buffer
198* @param message The return message buffer
199* @param perf The return performance data buffer
200* @return The return of the command
201*/
202NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query(const std::wstring command, const std::list<std::wstring> & arguments, std::string & result)
203{
204        if (!fNSAPIInject)
205                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
206
207        std::string request;
208        try {
209                nscapi::functions::create_simple_query_request(command, arguments, request);
210        } catch (std::exception &e) {
211                CORE_LOG_ERROR_STD(_T("Failed to extract return message: ") + utf8::cvt<std::wstring>(e.what()));
212                return NSCAPI::returnUNKNOWN;
213        }
214        return query(command.c_str(), request, result);
215}
216
217NSCAPI::nagiosReturn nscapi::core_wrapper::query(const std::wstring & command, const std::string & request, std::string & result)
218{
219        if (!fNSAPIInject)
220                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
221        char *buffer = NULL;
222        unsigned int buffer_size = 0;
223        NSCAPI::nagiosReturn retC = query(command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
224
225        if (buffer_size > 0 && buffer != NULL) {
226                //PluginCommand::ResponseMessage rsp_msg;
227                result = std::string(buffer, buffer_size);
228        }
229
230        DestroyBuffer(&buffer);
231        switch (retC) {
232                case NSCAPI::returnIgnored:
233                        CORE_LOG_MESSAGE_STD(_T("No handler for command '") + command + _T("'."));
234                        break;
235                case NSCAPI::returnOK:
236                case NSCAPI::returnCRIT:
237                case NSCAPI::returnWARN:
238                case NSCAPI::returnUNKNOWN:
239                        break;
240                default:
241                        throw nscapi::nscapi_exception(_T("Unknown return code when injecting: ") + std::wstring(command));
242        }
243        return retC;
244}
245
246
247NSCAPI::nagiosReturn nscapi::core_wrapper::simple_query_from_nrpe(const std::wstring command, const std::wstring & buffer, std::wstring & message, std::wstring & perf) {
248        if (!fNSAPIInject)
249                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
250        boost::tokenizer<boost::char_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tok(buffer, boost::char_separator<wchar_t>(_T("!")));
251        std::list<std::wstring> arglist;
252        BOOST_FOREACH(std::wstring s, tok)
253                arglist.push_back(s);
254        return simple_query(command, arglist, message, perf);
255}
256
257NSCAPI::nagiosReturn nscapi::core_wrapper::exec_command(const std::wstring target, const std::wstring command, std::string request, std::string & result) {
258        char *buffer = NULL;
259        unsigned int buffer_size = 0;
260        NSCAPI::nagiosReturn retC = exec_command(target.c_str(), command.c_str(), request.c_str(), request.size(), &buffer, &buffer_size);
261
262        if (buffer_size > 0 && buffer != NULL) {
263                result = std::string(buffer, buffer_size);
264        }
265
266        DestroyBuffer(&buffer);
267        switch (retC) {
268                case NSCAPI::returnIgnored:
269                        CORE_LOG_MESSAGE_STD(_T("No handler for command '") + command + _T("'."));
270                        break;
271                case NSCAPI::returnOK:
272                case NSCAPI::returnCRIT:
273                case NSCAPI::returnWARN:
274                case NSCAPI::returnUNKNOWN:
275                        break;
276                default:
277                        throw nscapi::nscapi_exception(_T("Unknown return code when injecting: ") + std::wstring(command));
278        }
279        return retC;
280}
281NSCAPI::nagiosReturn nscapi::core_wrapper::exec_command(const wchar_t* target, const wchar_t* command, const char *request, const unsigned int request_len, char **response, unsigned int *response_len)
282{
283        if (!fNSAPIExecCommand)
284                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
285        return fNSAPIExecCommand(target, command, request, request_len, response, response_len);
286}
287
288NSCAPI::nagiosReturn nscapi::core_wrapper::exec_simple_command(const std::wstring target, const std::wstring command, const std::list<std::wstring> &argument, std::list<std::wstring> & result) {
289        std::string request, response;
290        nscapi::functions::create_simple_exec_request(command, argument, request);
291        NSCAPI::nagiosReturn ret = exec_command(target, command, request, response);
292        nscapi::functions::parse_simple_exec_result(response, result);
293        return ret;
294}
295
296
297
298/**
299 * Ask the core to shutdown (only works when run as a service, o/w does nothing ?
300 * @todo Check if this might cause damage if not run as a service.
301 */
302void nscapi::core_wrapper::StopService(void) {
303        if (fNSAPIStopServer)
304                fNSAPIStopServer();
305}
306/**
307 * Close the program (usefull for tray/testmode) without stopping the service (unless this is the service).
308 * @author mickem
309 */
310void nscapi::core_wrapper::Exit(void) {
311        if (fNSAPIExit)
312                fNSAPIExit();
313}
314/**
315 * Retrieve a string from the settings subsystem (INI-file)
316 * Might possibly be located in the registry in the future.
317 *
318 * @param section Section key (generally module specific, make sure this is "unique")
319 * @param key The key to retrieve
320 * @param defaultValue A default value (if no value is set in the settings file)
321 * @return the current value or defaultValue if no value is set.
322 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an error occurs.
323 */
324std::wstring nscapi::core_wrapper::getSettingsString(std::wstring section, std::wstring key, std::wstring defaultValue) {
325        if (!fNSAPIGetSettingsString)
326                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
327        unsigned int buf_len = getBufferLength();
328        wchar_t *buffer = new wchar_t[buf_len+1];
329        if (fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
330                delete [] buffer;
331                throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
332        }
333        std::wstring ret = buffer;
334        delete [] buffer;
335        return ret;
336}
337
338std::wstring nscapi::core_wrapper::expand_path(std::wstring value) {
339        if (!fNSAPIExpandPath)
340                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
341        unsigned int buf_len = getBufferLength();
342        wchar_t *buffer = new wchar_t[buf_len+1];
343        if (fNSAPIExpandPath(value.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
344                delete [] buffer;
345                throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
346        }
347        std::wstring ret = buffer;
348        delete [] buffer;
349        return ret;
350}
351/**
352 * Get a section of settings strings
353 * @param section The section to retrieve
354 * @return The keys in the section
355 */
356std::list<std::wstring> nscapi::core_wrapper::getSettingsSection(std::wstring section) {
357        if (!fNSAPIGetSettingsSection)
358                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
359        array_buffer::arrayBuffer aBuffer = NULL;
360        unsigned int argLen = 0;
361        if (fNSAPIGetSettingsSection(section.c_str(), &aBuffer, &argLen) != NSCAPI::isSuccess) {
362                throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
363        }
364        std::list<std::wstring> ret = array_buffer::arrayBuffer2list(argLen, aBuffer);
365        if (fNSAPIReleaseSettingsSectionBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
366                throw nscapi::nscapi_exception(_T("Settings could not be destroyed."));
367        }
368        if (aBuffer != NULL)
369                throw nscapi::nscapi_exception(_T("buffer is not null?."));
370        return ret;
371}
372std::list<std::wstring> nscapi::core_wrapper::getSettingsSections(std::wstring section) {
373        if (!fNSAPIGetSettingsSections)
374                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
375        array_buffer::arrayBuffer aBuffer = NULL;
376        unsigned int argLen = 0;
377        if (fNSAPIGetSettingsSections(section.c_str(), &aBuffer, &argLen) != NSCAPI::isSuccess) {
378                throw nscapi::nscapi_exception(_T("Settings could not be retrieved."));
379        }
380        std::list<std::wstring> ret = array_buffer::arrayBuffer2list(argLen, aBuffer);
381        if (fNSAPIReleaseSettingsSectionBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
382                throw nscapi::nscapi_exception(_T("Settings could not be destroyed."));
383        }
384        if (aBuffer != NULL)
385                throw nscapi::nscapi_exception(_T("buffer is not null?."));
386        return ret;
387}
388/**
389 * Retrieve an int from the settings subsystem (INI-file)
390 * Might possibly be located in the registry in the future.
391 *
392 * @param section Section key (generally module specific, make sure this is "unique")
393 * @param key The key to retrieve
394 * @param defaultValue A default value (if no value is set in the settings file)
395 * @return the current value or defaultValue if no value is set.
396 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
397 */
398int nscapi::core_wrapper::getSettingsInt(std::wstring section, std::wstring key, int defaultValue) {
399        if (!fNSAPIGetSettingsInt)
400                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
401        return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue);
402}
403bool nscapi::core_wrapper::getSettingsBool(std::wstring section, std::wstring key, bool defaultValue) {
404        if (!fNSAPIGetSettingsBool)
405                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
406        return fNSAPIGetSettingsBool(section.c_str(), key.c_str(), defaultValue?1:0) == 1;
407}
408void 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) {
409        if (!fNSAPISettingsRegKey)
410                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
411        fNSAPISettingsRegKey(path.c_str(), key.c_str(), type, title.c_str(), description.c_str(), defaultValue.c_str(), advanced);
412}
413void nscapi::core_wrapper::settings_register_path(std::wstring path, std::wstring title, std::wstring description, bool advanced) {
414        if (!fNSAPISettingsRegPath)
415                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
416        fNSAPISettingsRegPath(path.c_str(), title.c_str(), description.c_str(), advanced);
417}
418
419
420void nscapi::core_wrapper::settings_save() {
421        if (!fNSAPISettingsSave)
422                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
423        fNSAPISettingsSave();
424}
425
426/**
427 * Retrieve the application name (in human readable format) from the core.
428 * @return A string representing the application name.
429 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
430 */
431std::wstring nscapi::core_wrapper::getApplicationName() {
432        if (!fNSAPIGetApplicationName)
433                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
434        unsigned int buf_len = getBufferLength();
435        wchar_t *buffer = new wchar_t[buf_len+1];
436        if (fNSAPIGetApplicationName(buffer, buf_len) != NSCAPI::isSuccess) {
437                delete [] buffer;
438                throw nscapi::nscapi_exception(_T("Application name could not be retrieved"));
439        }
440        std::wstring ret = buffer;
441        delete [] buffer;
442        return ret;
443}
444/**
445 * Retrieve the directory root of the application from the core.
446 * @return A string representing the base path.
447 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
448 */
449std::wstring nscapi::core_wrapper::getBasePath() {
450        if (!fNSAPIGetBasePath)
451                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
452        unsigned int buf_len = getBufferLength();
453        wchar_t *buffer = new wchar_t[buf_len+1];
454        if (fNSAPIGetBasePath(buffer, buf_len) != NSCAPI::isSuccess) {
455                delete [] buffer;
456                throw nscapi::nscapi_exception(_T("Base path could not be retrieved"));
457        }
458        std::wstring ret = buffer;
459        delete [] buffer;
460        return ret;
461}
462
463unsigned int nscapi::core_wrapper::getBufferLength() {
464        static unsigned int len = 0;
465        if (len == 0) {
466                len = getSettingsInt(setting_keys::settings_def::PAYLOAD_LEN_PATH, setting_keys::settings_def::PAYLOAD_LEN, setting_keys::settings_def::PAYLOAD_LEN_DEFAULT);
467        }
468        return len;
469}
470
471
472bool nscapi::core_wrapper::logDebug() {
473        enum status {unknown, debug, nodebug };
474        static status d = unknown;
475        if (d == unknown) {
476                if (checkLogMessages(debug)== NSCAPI::istrue)
477                        d = debug;
478                else
479                        d = nodebug;
480        }
481        return (d == debug);
482}
483
484std::wstring nscapi::core_wrapper::Encrypt(std::wstring str, unsigned int algorithm) {
485        if (!fNSAPIEncrypt)
486                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
487        unsigned int len = 0;
488        // @todo investigate potential problems with static_cast<unsigned int>
489        fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
490        len+=2;
491        wchar_t *buf = new wchar_t[len+1];
492        NSCAPI::errorReturn ret = fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
493        if (ret == NSCAPI::isSuccess) {
494                std::wstring ret = buf;
495                delete [] buf;
496                return ret;
497        }
498        return _T("");
499}
500std::wstring nscapi::core_wrapper::Decrypt(std::wstring str, unsigned int algorithm) {
501        if (!fNSAPIDecrypt)
502                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
503        unsigned int len = 0;
504        // @todo investigate potential problems with: static_cast<unsigned int>(str.size())
505        fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
506        len+=2;
507        wchar_t *buf = new wchar_t[len+1];
508        NSCAPI::errorReturn ret = fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
509        if (ret == NSCAPI::isSuccess) {
510                std::wstring ret = buf;
511                delete [] buf;
512                return ret;
513        }
514        return _T("");
515}
516NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsString(std::wstring section, std::wstring key, std::wstring value) {
517        if (!fNSAPISetSettingsString)
518                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
519        return fNSAPISetSettingsString(section.c_str(), key.c_str(), value.c_str());
520}
521NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsInt(std::wstring section, std::wstring key, int value) {
522        if (!fNSAPISetSettingsInt)
523                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
524        return fNSAPISetSettingsInt(section.c_str(), key.c_str(), value);
525}
526NSCAPI::errorReturn nscapi::core_wrapper::WriteSettings(int type) {
527        if (!fNSAPIWriteSettings)
528                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
529        return fNSAPIWriteSettings(type);
530}
531NSCAPI::errorReturn nscapi::core_wrapper::ReadSettings(int type) {
532        if (!fNSAPIReadSettings)
533                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
534        return fNSAPIReadSettings(type);
535}
536NSCAPI::errorReturn nscapi::core_wrapper::Rehash(int flag) {
537        if (!fNSAPIRehash)
538                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
539        return fNSAPIRehash(flag);
540}
541nscapi::core_wrapper::plugin_info_list nscapi::core_wrapper::getPluginList() {
542        if (!fNSAPIGetPluginList || !fNSAPIReleasePluginList)
543                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
544        plugin_info_list ret;
545       
546       
547        int len = 0;
548        //NSCAPI::plugin_info_list **list2;
549        //NSCAPI::plugin_info_list *list[1];
550        NSCAPI::plugin_info *list[1];
551        //typedef NSCAPI::errorReturn (*lpNSAPIGetPluginList)(int *len, NSAPI_plugin_info** list);
552        //typedef NSCAPI::errorReturn (*lpNSAPIReleasePluginList)(int len, NSAPI_plugin_info** list);
553        NSCAPI::errorReturn err = fNSAPIGetPluginList(&len, list);
554        if (err != NSCAPI::isSuccess)
555                return ret;
556        for (int i=0;i<len;i++) {
557                plugin_info_type info;
558                info.description = (*list)[i].description;
559                info.name = (*list)[i].name;
560                info.dll = (*list)[i].dll;
561                ret.push_back(info);
562        }
563        fNSAPIReleasePluginList(len, list);
564        return ret;
565}
566
567std::list<std::wstring> nscapi::core_wrapper::getAllCommandNames() {
568        if (!fNSAPIGetAllCommandNames || !fNSAPIReleaseAllCommandNamessBuffer )
569                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
570        array_buffer::arrayBuffer aBuffer = NULL;
571        unsigned int argLen = 0;
572        if (fNSAPIGetAllCommandNames(&aBuffer, &argLen) != NSCAPI::isSuccess) {
573                throw nscapi::nscapi_exception(_T("Commands could not be retrieved."));
574        }
575        std::list<std::wstring> ret = array_buffer::arrayBuffer2list(argLen, aBuffer);
576        if (fNSAPIReleaseAllCommandNamessBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
577                throw nscapi::nscapi_exception(_T("Commands could not be destroyed."));
578        }
579        if (aBuffer != NULL)
580                throw nscapi::nscapi_exception(_T("buffer is not null?."));
581        return ret;
582}
583std::wstring nscapi::core_wrapper::describeCommand(std::wstring command) {
584        if (!fNSAPIDescribeCommand)
585                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
586        unsigned int buf_len = getBufferLength();
587        wchar_t *buffer = new wchar_t[buf_len+1];
588        if (fNSAPIDescribeCommand(command.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
589                delete [] buffer;
590                throw nscapi::nscapi_exception(_T("Base path could not be retrieved"));
591        }
592        std::wstring ret = buffer;
593        delete [] buffer;
594        return ret;
595}
596void nscapi::core_wrapper::registerCommand(unsigned int id, std::wstring command, std::wstring description) {
597        if (!fNSAPIRegisterCommand)
598                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
599        if (fNSAPIRegisterCommand(id, command.c_str(), description.c_str()) != NSCAPI::isSuccess) {
600                CORE_LOG_ERROR_STD(_T("Failed to register command: ") + command + _T(" in plugin: ") + to_wstring(id));
601        }
602}
603
604void nscapi::core_wrapper::registerSubmissionListener(unsigned int id, std::wstring channel) {
605        if (!fNSAPIRegisterSubmissionListener)
606                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
607        if (fNSAPIRegisterSubmissionListener(id, channel.c_str()) != NSCAPI::isSuccess) {
608                CORE_LOG_ERROR_STD(_T("Failed to register channel: ") + channel + _T(" in plugin: ") + to_wstring(id));
609        }
610}
611void nscapi::core_wrapper::registerRoutingListener(unsigned int id, std::wstring channel) {
612        if (!fNSAPIRegisterRoutingListener)
613                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
614        if (fNSAPIRegisterRoutingListener(id, channel.c_str()) != NSCAPI::isSuccess) {
615                CORE_LOG_ERROR_STD(_T("Failed to register channel: ") + channel + _T(" in plugin: ") + to_wstring(id));
616        }
617}
618
619
620bool nscapi::core_wrapper::checkLogMessages(int type) {
621        if (!fNSAPICheckLogMessages)
622                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
623        return fNSAPICheckLogMessages(type) == NSCAPI::istrue;
624}
625/**
626 * Retrieve the application version as a string (in human readable format) from the core.
627 * @return A string representing the application version.
628 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
629 */
630std::wstring nscapi::core_wrapper::getApplicationVersionString() {
631        if (!fNSAPIGetApplicationVersionStr)
632                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
633        unsigned int buf_len = getBufferLength();
634        wchar_t *buffer = new wchar_t[buf_len+1];
635        if (fNSAPIGetApplicationVersionStr(buffer, buf_len) != NSCAPI::isSuccess) {
636                delete [] buffer;
637                return _T("");
638        }
639        std::wstring ret = buffer;
640        delete [] buffer;
641        return ret;
642}
643
644/**
645 * Wrapper function around the ModuleHelperInit call.
646 * This wrapper retrieves all pointers and stores them for future use.
647 * @param f A function pointer to a function that can be used to load function from the core.
648 * @return NSCAPI::success or NSCAPI::failure
649 */
650bool nscapi::core_wrapper::load_endpoints(nscapi::core_api::lpNSAPILoader f) {
651        fNSAPIGetApplicationName = (nscapi::core_api::lpNSAPIGetApplicationName)f(_T("NSAPIGetApplicationName"));
652        fNSAPIGetApplicationVersionStr = (nscapi::core_api::lpNSAPIGetApplicationVersionStr)f(_T("NSAPIGetApplicationVersionStr"));
653        fNSAPIGetSettingsInt = (nscapi::core_api::lpNSAPIGetSettingsInt)f(_T("NSAPIGetSettingsInt"));
654        fNSAPIGetSettingsBool = (nscapi::core_api::lpNSAPIGetSettingsBool)f(_T("NSAPIGetSettingsBool"));
655        fNSAPIGetSettingsString = (nscapi::core_api::lpNSAPIGetSettingsString)f(_T("NSAPIGetSettingsString"));
656        fNSAPIGetSettingsSection = (nscapi::core_api::lpNSAPIGetSettingsSection)f(_T("NSAPIGetSettingsSection"));
657        fNSAPIGetSettingsSections = (nscapi::core_api::lpNSAPIGetSettingsSections)f(_T("NSAPIGetSettingsSections"));
658        fNSAPIReleaseSettingsSectionBuffer = (nscapi::core_api::lpNSAPIReleaseSettingsSectionBuffer)f(_T("NSAPIReleaseSettingsSectionBuffer"));
659        fNSAPIMessage = (nscapi::core_api::lpNSAPIMessage)f(_T("NSAPIMessage"));
660        fNSAPIStopServer = (nscapi::core_api::lpNSAPIStopServer)f(_T("NSAPIStopServer"));
661        //fNSAPIExit = (nscapi::core_api::lpNSAPIExit)f(_T("NSAPIExit"));
662        fNSAPIInject = (nscapi::core_api::lpNSAPIInject)f(_T("NSAPIInject"));
663        fNSAPIExecCommand = (nscapi::core_api::lpNSAPIExecCommand)f(_T("NSAPIExecCommand"));
664        fNSAPIDestroyBuffer = (nscapi::core_api::lpNSAPIDestroyBuffer)f(_T("NSAPIDestroyBuffer"));
665        fNSAPINotify = (nscapi::core_api::lpNSAPINotify)f(_T("NSAPINotify"));
666        fNSAPIGetBasePath = (nscapi::core_api::lpNSAPIGetBasePath)f(_T("NSAPIGetBasePath"));
667        fNSAPICheckLogMessages = (nscapi::core_api::lpNSAPICheckLogMessages)f(_T("NSAPICheckLogMessages"));
668        fNSAPIDecrypt = (nscapi::core_api::lpNSAPIDecrypt)f(_T("NSAPIDecrypt"));
669        fNSAPIEncrypt = (nscapi::core_api::lpNSAPIEncrypt)f(_T("NSAPIEncrypt"));
670        fNSAPISetSettingsString = (nscapi::core_api::lpNSAPISetSettingsString)f(_T("NSAPISetSettingsString"));
671        fNSAPISetSettingsInt = (nscapi::core_api::lpNSAPISetSettingsInt)f(_T("NSAPISetSettingsInt"));
672        fNSAPIWriteSettings = (nscapi::core_api::lpNSAPIWriteSettings)f(_T("NSAPIWriteSettings"));
673        fNSAPIReadSettings = (nscapi::core_api::lpNSAPIReadSettings)f(_T("NSAPIReadSettings"));
674        fNSAPIRehash = (nscapi::core_api::lpNSAPIRehash)f(_T("NSAPIRehash"));
675        fNSAPIReload = (nscapi::core_api::lpNSAPIReload)f(_T("NSAPIReload"));
676
677        fNSAPIDescribeCommand = (nscapi::core_api::lpNSAPIDescribeCommand)f(_T("NSAPIDescribeCommand"));
678        fNSAPIGetAllCommandNames = (nscapi::core_api::lpNSAPIGetAllCommandNames)f(_T("NSAPIGetAllCommandNames"));
679        fNSAPIReleaseAllCommandNamessBuffer = (nscapi::core_api::lpNSAPIReleaseAllCommandNamessBuffer)f(_T("NSAPIReleaseAllCommandNamessBuffer"));
680        fNSAPIRegisterCommand = (nscapi::core_api::lpNSAPIRegisterCommand)f(_T("NSAPIRegisterCommand"));
681
682        fNSAPISettingsRegKey = (nscapi::core_api::lpNSAPISettingsRegKey)f(_T("NSAPISettingsRegKey"));
683        fNSAPISettingsRegPath = (nscapi::core_api::lpNSAPISettingsRegPath)f(_T("NSAPISettingsRegPath"));
684
685        fNSAPIGetPluginList = (nscapi::core_api::lpNSAPIGetPluginList)f(_T("NSAPIGetPluginList"));
686        fNSAPIReleasePluginList = (nscapi::core_api::lpNSAPIReleasePluginList)f(_T("NSAPIReleasePluginList"));
687
688        fNSAPISettingsSave = (nscapi::core_api::lpNSAPISettingsSave)f(_T("NSAPISettingsSave"));
689
690        fNSAPIExpandPath = (nscapi::core_api::lpNSAPIExpandPath)f(_T("NSAPIExpandPath"));
691       
692        fNSAPIRegisterSubmissionListener = (nscapi::core_api::lpNSAPIRegisterSubmissionListener)f(_T("NSAPIRegisterSubmissionListener"));
693        fNSAPIRegisterRoutingListener = (nscapi::core_api::lpNSAPIRegisterRoutingListener)f(_T("NSAPIRegisterRoutingListener"));
694
695        return true;
696}
Note: See TracBrowser for help on using the repository browser.