source: nscp/include/nscapi/nscapi_core_wrapper.cpp @ 98113da

0.4.00.4.10.4.2
Last change on this file since 98113da was 98113da, checked in by Michael Medin <michael@…>, 20 months ago
  • Real-time CheckEventLog working (still only for one, and I think application log)
  • Added python tests to validfate that Real-time eventlog is working.
  • Fixed som defects here and there (now builds on Linux again)
  • Fixed so it builds in "debug mode"
  • Fixed issue in grammar which caused infiniate loop in som cases
  • Fixed so error rendering in eventlog works with "infininate number of argumnets"
  • Added support for targeting execs (in API)
  • Fixed some invalid return messages
  • Streamlined submissions wrappers to be more inline with "other wrappers"
  • Fixed a myrriad of minor python script bugs
  • Added sleep command (which sometimes causes issues so use with care)
  • Property mode set to 100644
File size: 29.7 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}
372/**
373 * Retrieve an int from the settings subsystem (INI-file)
374 * Might possibly be located in the registry in the future.
375 *
376 * @param section Section key (generally module specific, make sure this is "unique")
377 * @param key The key to retrieve
378 * @param defaultValue A default value (if no value is set in the settings file)
379 * @return the current value or defaultValue if no value is set.
380 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
381 */
382int nscapi::core_wrapper::getSettingsInt(std::wstring section, std::wstring key, int defaultValue) {
383        if (!fNSAPIGetSettingsInt)
384                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
385        return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue);
386}
387bool nscapi::core_wrapper::getSettingsBool(std::wstring section, std::wstring key, bool defaultValue) {
388        if (!fNSAPIGetSettingsBool)
389                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
390        return fNSAPIGetSettingsBool(section.c_str(), key.c_str(), defaultValue?1:0) == 1;
391}
392void 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) {
393        if (!fNSAPISettingsRegKey)
394                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
395        fNSAPISettingsRegKey(path.c_str(), key.c_str(), type, title.c_str(), description.c_str(), defaultValue.c_str(), advanced);
396}
397void nscapi::core_wrapper::settings_register_path(std::wstring path, std::wstring title, std::wstring description, bool advanced) {
398        if (!fNSAPISettingsRegPath)
399                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
400        fNSAPISettingsRegPath(path.c_str(), title.c_str(), description.c_str(), advanced);
401}
402
403
404void nscapi::core_wrapper::settings_save() {
405        if (!fNSAPISettingsSave)
406                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
407        fNSAPISettingsSave();
408}
409
410/**
411 * Retrieve the application name (in human readable format) from the core.
412 * @return A string representing the application name.
413 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
414 */
415std::wstring nscapi::core_wrapper::getApplicationName() {
416        if (!fNSAPIGetApplicationName)
417                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
418        unsigned int buf_len = getBufferLength();
419        wchar_t *buffer = new wchar_t[buf_len+1];
420        if (fNSAPIGetApplicationName(buffer, buf_len) != NSCAPI::isSuccess) {
421                delete [] buffer;
422                throw nscapi::nscapi_exception(_T("Application name could not be retrieved"));
423        }
424        std::wstring ret = buffer;
425        delete [] buffer;
426        return ret;
427}
428/**
429 * Retrieve the directory root of the application from the core.
430 * @return A string representing the base path.
431 * @throws nscapi::nscapi_exception When core pointer set is unavailable or an unexpected error occurs.
432 */
433std::wstring nscapi::core_wrapper::getBasePath() {
434        if (!fNSAPIGetBasePath)
435                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
436        unsigned int buf_len = getBufferLength();
437        wchar_t *buffer = new wchar_t[buf_len+1];
438        if (fNSAPIGetBasePath(buffer, buf_len) != NSCAPI::isSuccess) {
439                delete [] buffer;
440                throw nscapi::nscapi_exception(_T("Base path could not be retrieved"));
441        }
442        std::wstring ret = buffer;
443        delete [] buffer;
444        return ret;
445}
446
447unsigned int nscapi::core_wrapper::getBufferLength() {
448        static unsigned int len = 0;
449        if (len == 0) {
450                len = getSettingsInt(setting_keys::settings_def::PAYLOAD_LEN_PATH, setting_keys::settings_def::PAYLOAD_LEN, setting_keys::settings_def::PAYLOAD_LEN_DEFAULT);
451        }
452        return len;
453}
454
455
456bool nscapi::core_wrapper::logDebug() {
457        enum status {unknown, debug, nodebug };
458        static status d = unknown;
459        if (d == unknown) {
460                if (checkLogMessages(debug)== NSCAPI::istrue)
461                        d = debug;
462                else
463                        d = nodebug;
464        }
465        return (d == debug);
466}
467
468std::wstring nscapi::core_wrapper::Encrypt(std::wstring str, unsigned int algorithm) {
469        if (!fNSAPIEncrypt)
470                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
471        unsigned int len = 0;
472        // @todo investigate potential problems with static_cast<unsigned int>
473        fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len);
474        len+=2;
475        wchar_t *buf = new wchar_t[len+1];
476        NSCAPI::errorReturn ret = fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len);
477        if (ret == NSCAPI::isSuccess) {
478                std::wstring ret = buf;
479                delete [] buf;
480                return ret;
481        }
482        return _T("");
483}
484std::wstring nscapi::core_wrapper::Decrypt(std::wstring str, unsigned int algorithm) {
485        if (!fNSAPIDecrypt)
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>(str.size())
489        fNSAPIDecrypt(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 = fNSAPIDecrypt(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}
500NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsString(std::wstring section, std::wstring key, std::wstring value) {
501        if (!fNSAPISetSettingsString)
502                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
503        return fNSAPISetSettingsString(section.c_str(), key.c_str(), value.c_str());
504}
505NSCAPI::errorReturn nscapi::core_wrapper::SetSettingsInt(std::wstring section, std::wstring key, int value) {
506        if (!fNSAPISetSettingsInt)
507                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
508        return fNSAPISetSettingsInt(section.c_str(), key.c_str(), value);
509}
510NSCAPI::errorReturn nscapi::core_wrapper::WriteSettings(int type) {
511        if (!fNSAPIWriteSettings)
512                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
513        return fNSAPIWriteSettings(type);
514}
515NSCAPI::errorReturn nscapi::core_wrapper::ReadSettings(int type) {
516        if (!fNSAPIReadSettings)
517                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
518        return fNSAPIReadSettings(type);
519}
520NSCAPI::errorReturn nscapi::core_wrapper::Rehash(int flag) {
521        if (!fNSAPIRehash)
522                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
523        return fNSAPIRehash(flag);
524}
525nscapi::core_wrapper::plugin_info_list nscapi::core_wrapper::getPluginList() {
526        if (!fNSAPIGetPluginList || !fNSAPIReleasePluginList)
527                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
528        plugin_info_list ret;
529       
530       
531        int len = 0;
532        //NSCAPI::plugin_info_list **list2;
533        //NSCAPI::plugin_info_list *list[1];
534        NSCAPI::plugin_info *list[1];
535        //typedef NSCAPI::errorReturn (*lpNSAPIGetPluginList)(int *len, NSAPI_plugin_info** list);
536        //typedef NSCAPI::errorReturn (*lpNSAPIReleasePluginList)(int len, NSAPI_plugin_info** list);
537        NSCAPI::errorReturn err = fNSAPIGetPluginList(&len, list);
538        if (err != NSCAPI::isSuccess)
539                return ret;
540        for (int i=0;i<len;i++) {
541                plugin_info_type info;
542                info.description = (*list)[i].description;
543                info.name = (*list)[i].name;
544                info.dll = (*list)[i].dll;
545                ret.push_back(info);
546        }
547        fNSAPIReleasePluginList(len, list);
548        return ret;
549}
550
551std::list<std::wstring> nscapi::core_wrapper::getAllCommandNames() {
552        if (!fNSAPIGetAllCommandNames || !fNSAPIReleaseAllCommandNamessBuffer )
553                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
554        array_buffer::arrayBuffer aBuffer = NULL;
555        unsigned int argLen = 0;
556        if (fNSAPIGetAllCommandNames(&aBuffer, &argLen) != NSCAPI::isSuccess) {
557                throw nscapi::nscapi_exception(_T("Commands could not be retrieved."));
558        }
559        std::list<std::wstring> ret = array_buffer::arrayBuffer2list(argLen, aBuffer);
560        if (fNSAPIReleaseAllCommandNamessBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) {
561                throw nscapi::nscapi_exception(_T("Commands could not be destroyed."));
562        }
563        if (aBuffer != NULL)
564                throw nscapi::nscapi_exception(_T("buffer is not null?."));
565        return ret;
566}
567std::wstring nscapi::core_wrapper::describeCommand(std::wstring command) {
568        if (!fNSAPIDescribeCommand)
569                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
570        unsigned int buf_len = getBufferLength();
571        wchar_t *buffer = new wchar_t[buf_len+1];
572        if (fNSAPIDescribeCommand(command.c_str(), buffer, buf_len) != NSCAPI::isSuccess) {
573                delete [] buffer;
574                throw nscapi::nscapi_exception(_T("Base path could not be retrieved"));
575        }
576        std::wstring ret = buffer;
577        delete [] buffer;
578        return ret;
579}
580void nscapi::core_wrapper::registerCommand(unsigned int id, std::wstring command, std::wstring description) {
581        if (!fNSAPIRegisterCommand)
582                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
583        if (fNSAPIRegisterCommand(id, command.c_str(), description.c_str()) != NSCAPI::isSuccess) {
584                CORE_LOG_ERROR_STD(_T("Failed to register command: ") + command + _T(" in plugin: ") + to_wstring(id));
585        }
586}
587
588void nscapi::core_wrapper::registerSubmissionListener(unsigned int id, std::wstring channel) {
589        if (!fNSAPIRegisterSubmissionListener)
590                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
591        if (fNSAPIRegisterSubmissionListener(id, channel.c_str()) != NSCAPI::isSuccess) {
592                CORE_LOG_ERROR_STD(_T("Failed to register channel: ") + channel + _T(" in plugin: ") + to_wstring(id));
593        }
594}
595void nscapi::core_wrapper::registerRoutingListener(unsigned int id, std::wstring channel) {
596        if (!fNSAPIRegisterRoutingListener)
597                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
598        if (fNSAPIRegisterRoutingListener(id, channel.c_str()) != NSCAPI::isSuccess) {
599                CORE_LOG_ERROR_STD(_T("Failed to register channel: ") + channel + _T(" in plugin: ") + to_wstring(id));
600        }
601}
602
603
604bool nscapi::core_wrapper::checkLogMessages(int type) {
605        if (!fNSAPICheckLogMessages)
606                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
607        return fNSAPICheckLogMessages(type) == NSCAPI::istrue;
608}
609/**
610 * Retrieve the application version as a string (in human readable format) from the core.
611 * @return A string representing the application version.
612 * @throws nscapi::nscapi_exception When core pointer set is unavailable.
613 */
614std::wstring nscapi::core_wrapper::getApplicationVersionString() {
615        if (!fNSAPIGetApplicationVersionStr)
616                throw nscapi::nscapi_exception(_T("NSCore has not been initiated..."));
617        unsigned int buf_len = getBufferLength();
618        wchar_t *buffer = new wchar_t[buf_len+1];
619        if (fNSAPIGetApplicationVersionStr(buffer, buf_len) != NSCAPI::isSuccess) {
620                delete [] buffer;
621                return _T("");
622        }
623        std::wstring ret = buffer;
624        delete [] buffer;
625        return ret;
626}
627
628/**
629 * Wrapper function around the ModuleHelperInit call.
630 * This wrapper retrieves all pointers and stores them for future use.
631 * @param f A function pointer to a function that can be used to load function from the core.
632 * @return NSCAPI::success or NSCAPI::failure
633 */
634bool nscapi::core_wrapper::load_endpoints(nscapi::core_api::lpNSAPILoader f) {
635        fNSAPIGetApplicationName = (nscapi::core_api::lpNSAPIGetApplicationName)f(_T("NSAPIGetApplicationName"));
636        fNSAPIGetApplicationVersionStr = (nscapi::core_api::lpNSAPIGetApplicationVersionStr)f(_T("NSAPIGetApplicationVersionStr"));
637        fNSAPIGetSettingsInt = (nscapi::core_api::lpNSAPIGetSettingsInt)f(_T("NSAPIGetSettingsInt"));
638        fNSAPIGetSettingsBool = (nscapi::core_api::lpNSAPIGetSettingsBool)f(_T("NSAPIGetSettingsBool"));
639        fNSAPIGetSettingsString = (nscapi::core_api::lpNSAPIGetSettingsString)f(_T("NSAPIGetSettingsString"));
640        fNSAPIGetSettingsSection = (nscapi::core_api::lpNSAPIGetSettingsSection)f(_T("NSAPIGetSettingsSection"));
641        fNSAPIReleaseSettingsSectionBuffer = (nscapi::core_api::lpNSAPIReleaseSettingsSectionBuffer)f(_T("NSAPIReleaseSettingsSectionBuffer"));
642        fNSAPIMessage = (nscapi::core_api::lpNSAPIMessage)f(_T("NSAPIMessage"));
643        fNSAPIStopServer = (nscapi::core_api::lpNSAPIStopServer)f(_T("NSAPIStopServer"));
644        //fNSAPIExit = (nscapi::core_api::lpNSAPIExit)f(_T("NSAPIExit"));
645        fNSAPIInject = (nscapi::core_api::lpNSAPIInject)f(_T("NSAPIInject"));
646        fNSAPIExecCommand = (nscapi::core_api::lpNSAPIExecCommand)f(_T("NSAPIExecCommand"));
647        fNSAPIDestroyBuffer = (nscapi::core_api::lpNSAPIDestroyBuffer)f(_T("NSAPIDestroyBuffer"));
648        fNSAPINotify = (nscapi::core_api::lpNSAPINotify)f(_T("NSAPINotify"));
649        fNSAPIGetBasePath = (nscapi::core_api::lpNSAPIGetBasePath)f(_T("NSAPIGetBasePath"));
650        fNSAPICheckLogMessages = (nscapi::core_api::lpNSAPICheckLogMessages)f(_T("NSAPICheckLogMessages"));
651        fNSAPIDecrypt = (nscapi::core_api::lpNSAPIDecrypt)f(_T("NSAPIDecrypt"));
652        fNSAPIEncrypt = (nscapi::core_api::lpNSAPIEncrypt)f(_T("NSAPIEncrypt"));
653        fNSAPISetSettingsString = (nscapi::core_api::lpNSAPISetSettingsString)f(_T("NSAPISetSettingsString"));
654        fNSAPISetSettingsInt = (nscapi::core_api::lpNSAPISetSettingsInt)f(_T("NSAPISetSettingsInt"));
655        fNSAPIWriteSettings = (nscapi::core_api::lpNSAPIWriteSettings)f(_T("NSAPIWriteSettings"));
656        fNSAPIReadSettings = (nscapi::core_api::lpNSAPIReadSettings)f(_T("NSAPIReadSettings"));
657        fNSAPIRehash = (nscapi::core_api::lpNSAPIRehash)f(_T("NSAPIRehash"));
658        fNSAPIReload = (nscapi::core_api::lpNSAPIReload)f(_T("NSAPIReload"));
659
660        fNSAPIDescribeCommand = (nscapi::core_api::lpNSAPIDescribeCommand)f(_T("NSAPIDescribeCommand"));
661        fNSAPIGetAllCommandNames = (nscapi::core_api::lpNSAPIGetAllCommandNames)f(_T("NSAPIGetAllCommandNames"));
662        fNSAPIReleaseAllCommandNamessBuffer = (nscapi::core_api::lpNSAPIReleaseAllCommandNamessBuffer)f(_T("NSAPIReleaseAllCommandNamessBuffer"));
663        fNSAPIRegisterCommand = (nscapi::core_api::lpNSAPIRegisterCommand)f(_T("NSAPIRegisterCommand"));
664
665        fNSAPISettingsRegKey = (nscapi::core_api::lpNSAPISettingsRegKey)f(_T("NSAPISettingsRegKey"));
666        fNSAPISettingsRegPath = (nscapi::core_api::lpNSAPISettingsRegPath)f(_T("NSAPISettingsRegPath"));
667
668        fNSAPIGetPluginList = (nscapi::core_api::lpNSAPIGetPluginList)f(_T("NSAPIGetPluginList"));
669        fNSAPIReleasePluginList = (nscapi::core_api::lpNSAPIReleasePluginList)f(_T("NSAPIReleasePluginList"));
670
671        fNSAPISettingsSave = (nscapi::core_api::lpNSAPISettingsSave)f(_T("NSAPISettingsSave"));
672
673        fNSAPIExpandPath = (nscapi::core_api::lpNSAPIExpandPath)f(_T("NSAPIExpandPath"));
674       
675        fNSAPIRegisterSubmissionListener = (nscapi::core_api::lpNSAPIRegisterSubmissionListener)f(_T("NSAPIRegisterSubmissionListener"));
676        fNSAPIRegisterRoutingListener = (nscapi::core_api::lpNSAPIRegisterRoutingListener)f(_T("NSAPIRegisterRoutingListener"));
677
678        return true;
679}
Note: See TracBrowser for help on using the repository browser.