added NXSL function AgentExecuteAction
[public/netxms.git] / src / smsdrv / nxagent / main.cpp
CommitLineData
5039dede
AK
1/*
2** SMS Driver for sending SMS via NetXMS agent
f19168bd 3** Copyright (C) 2007-2016 Victor Kirhenshtein
5039dede
AK
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** File: main.cpp
5039dede
AK
20**/
21
22#include <nms_common.h>
23#include <nxsrvapi.h>
24
25#ifdef _WIN32
26#define EXPORT __declspec(dllexport)
27#else
28#define EXPORT
29#endif
30
29d7515f
VK
31/**
32 * Configuration
33 */
34static TCHAR m_hostName[256] = _T("localhost");
35static UINT16 m_port = 4700;
36static TCHAR m_secret[256] = _T("");
37static UINT32 m_timeout = 30000; // Default timeout is 30 seconds
38
39/**
40 * Initialize driver
41 * pszInitArgs format: hostname,port,timeout,secret
42 */
f19168bd 43extern "C" bool EXPORT SMSDriverInit(const TCHAR *pszInitArgs, Config *config)
5039dede
AK
44{
45 TCHAR *temp, *ptr, *eptr;
46 int field;
47
48 temp = _tcsdup(pszInitArgs);
49 for(ptr = eptr = temp, field = 0; eptr != NULL; field++, ptr = eptr + 1)
50 {
9f24efb3 51 eptr = _tcschr(ptr, ',');
5039dede
AK
52 if (eptr != NULL)
53 *eptr = 0;
54 switch(field)
55 {
56 case 0: // Host name
29d7515f 57 nx_strncpy(m_hostName, ptr, 256);
5039dede
AK
58 break;
59 case 1: // Port
29d7515f 60 m_port = (WORD)_tcstoul(ptr, NULL, 0);
5039dede
AK
61 break;
62 case 2: // Timeout
29d7515f 63 m_timeout = _tcstoul(ptr, NULL, 0) * 1000;
5039dede
AK
64 break;
65 case 3: // Secret
29d7515f 66 nx_strncpy(m_secret, ptr, 256);
5039dede
AK
67 break;
68 default:
69 break;
70 }
71 }
72 free(temp);
f19168bd 73 return true;
5039dede
AK
74}
75
29d7515f
VK
76/**
77 * Send SMS
78 */
f19168bd 79extern "C" bool EXPORT SMSDriverSend(const TCHAR *pszPhoneNumber, const TCHAR *pszText)
5039dede 80{
f19168bd 81 bool bSuccess = false;
5039dede 82
29d7515f
VK
83 InetAddress addr = InetAddress::resolveHostName(m_hostName);
84 if (addr.isValid())
5039dede 85 {
7dd6369a
VK
86 AgentConnection *conn = new AgentConnection(addr, m_port, (m_secret[0] != 0) ? AUTH_SHA1_HASH : AUTH_NONE, m_secret);
87 conn->setCommandTimeout(m_timeout);
88 if (conn->connect())
5039dede 89 {
1e84fc1d 90 const TCHAR *argv[2] = { pszPhoneNumber, pszText };
bad2adbd
VK
91 UINT32 rcc = conn->execAction(_T("SMS.Send"), 2, argv);
92 nxlog_debug(4, _T("nxagent.sms: agent action execution result: %d (%s)"), rcc, AgentErrorCodeToText(rcc));
93 if (rcc == ERR_SUCCESS)
f19168bd 94 bSuccess = true;
5039dede 95 }
7dd6369a 96 conn->decRefCount();
5039dede
AK
97 }
98 return bSuccess;
99}
100
29d7515f
VK
101/**
102 * Unload driver
103 */
9f24efb3 104extern "C" void EXPORT SMSDriverUnload()
5039dede
AK
105{
106}
107
5039dede
AK
108#ifdef _WIN32
109
29d7515f
VK
110/**
111 * DLL Entry point
112 */
5039dede
AK
113BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
114{
115 if (dwReason == DLL_PROCESS_ATTACH)
116 DisableThreadLibraryCalls(hInstance);
117 return TRUE;
118}
119
120#endif