added NXSL function AgentExecuteAction
[public/netxms.git] / src / smsdrv / nxagent / main.cpp
1 /*
2 ** SMS Driver for sending SMS via NetXMS agent
3 ** Copyright (C) 2007-2016 Victor Kirhenshtein
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
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
31 /**
32 * Configuration
33 */
34 static TCHAR m_hostName[256] = _T("localhost");
35 static UINT16 m_port = 4700;
36 static TCHAR m_secret[256] = _T("");
37 static UINT32 m_timeout = 30000; // Default timeout is 30 seconds
38
39 /**
40 * Initialize driver
41 * pszInitArgs format: hostname,port,timeout,secret
42 */
43 extern "C" bool EXPORT SMSDriverInit(const TCHAR *pszInitArgs, Config *config)
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 {
51 eptr = _tcschr(ptr, ',');
52 if (eptr != NULL)
53 *eptr = 0;
54 switch(field)
55 {
56 case 0: // Host name
57 nx_strncpy(m_hostName, ptr, 256);
58 break;
59 case 1: // Port
60 m_port = (WORD)_tcstoul(ptr, NULL, 0);
61 break;
62 case 2: // Timeout
63 m_timeout = _tcstoul(ptr, NULL, 0) * 1000;
64 break;
65 case 3: // Secret
66 nx_strncpy(m_secret, ptr, 256);
67 break;
68 default:
69 break;
70 }
71 }
72 free(temp);
73 return true;
74 }
75
76 /**
77 * Send SMS
78 */
79 extern "C" bool EXPORT SMSDriverSend(const TCHAR *pszPhoneNumber, const TCHAR *pszText)
80 {
81 bool bSuccess = false;
82
83 InetAddress addr = InetAddress::resolveHostName(m_hostName);
84 if (addr.isValid())
85 {
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())
89 {
90 const TCHAR *argv[2] = { pszPhoneNumber, pszText };
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)
94 bSuccess = true;
95 }
96 conn->decRefCount();
97 }
98 return bSuccess;
99 }
100
101 /**
102 * Unload driver
103 */
104 extern "C" void EXPORT SMSDriverUnload()
105 {
106 }
107
108 #ifdef _WIN32
109
110 /**
111 * DLL Entry point
112 */
113 BOOL 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