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

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

removed protobuf/log.proto.h and moved alll includes to target dir instead...

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