implemented single housekeeping thread for all message wait queues
[public/netxms.git] / src / client / nxsms / nxsms.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** nxsms - send SMS via NetXMS server
4 ** Copyright (C) 2003-2015 Victor Kirhenshtein
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 Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: nxsms.cpp
21 **
22 **/
23
24 #include "nxsms.h"
25
26 /**
27 * Callback function for debug printing
28 */
29 static void DebugCallback(const TCHAR *pMsg)
30 {
31 _tprintf(_T("NXCL: %s\n"), pMsg);
32 }
33
34 #ifdef _WIN32
35 #define CMDLINE_OPTIONS "DehP:u:vw:"
36 #else
37 #define CMDLINE_OPTIONS "c:DehP:u:vw:"
38 #endif
39
40 /**
41 * main
42 */
43 int main(int argc, char *argv[])
44 {
45 TCHAR login[MAX_DB_STRING] = _T("guest"), password[MAX_DB_STRING] = _T("");
46 BOOL isDebug = FALSE, isEncrypt = FALSE;
47 DWORD rcc, timeout = 3;
48 int ch;
49
50 // Parse command line
51 opterr = 1;
52 while((ch = getopt(argc, argv, CMDLINE_OPTIONS)) != -1)
53 {
54 switch(ch)
55 {
56 case 'h': // Display help and exit
57 printf("Usage: nxsms [<options>] <server> <phone number> <message>\n"
58 "Valid options are:\n"
59 #ifndef _WIN32
60 " -c : Codepage (default is " ICONV_DEFAULT_CODEPAGE ")\n"
61 #endif
62 " -D : Turn on debug mode.\n"
63 " -e : Encrypt session.\n"
64 " -h : Display help and exit.\n"
65 " -P <password> : Specify user's password. Default is empty password.\n"
66 " -u <user> : Login to server as <user>. Default is \"guest\".\n"
67 " -v : Display version and exit.\n"
68 " -w <seconds> : Specify command timeout (default is 3 seconds).\n"
69 "\n");
70 return 1;
71 #ifndef _WIN32
72 case 'c':
73 SetDefaultCodepage(optarg);
74 break;
75 #endif
76 case 'D':
77 isDebug = TRUE;
78 break;
79 case 'e':
80 isEncrypt = TRUE;
81 break;
82 case 'u':
83 #ifdef UNICODE
84 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, login, MAX_DB_STRING);
85 login[MAX_DB_STRING - 1] = 0;
86 #else
87 nx_strncpy(login, optarg, MAX_DB_STRING);
88 #endif
89 break;
90 case 'P':
91 #ifdef UNICODE
92 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, password, MAX_DB_STRING);
93 password[MAX_DB_STRING - 1] = 0;
94 #else
95 nx_strncpy(password, optarg, MAX_DB_STRING);
96 #endif
97 break;
98 case 'w':
99 timeout = strtoul(optarg, NULL, 0);
100 if ((timeout < 1) || (timeout > 120))
101 {
102 _tprintf(_T("Invalid timeout %hs\n"), optarg);
103 return 1;
104 }
105 break;
106 case 'v':
107 _tprintf(_T("NetXMS SMS Sender Version ") NETXMS_VERSION_STRING _T("\n"));
108 return 1;
109 case '?':
110 return 1;
111 default:
112 break;
113 }
114 }
115
116 if (argc - optind < 3)
117 {
118 _tprintf(_T("Required arguments missing. Use nxsms -h for help.\n"));
119 return 1;
120 }
121
122 #ifdef _WIN32
123 WSADATA wsaData;
124
125 if (WSAStartup(2, &wsaData) != 0)
126 {
127 _tprintf(_T("Unable to initialize Windows sockets\n"));
128 return 4;
129 }
130 #endif
131
132 if (!NXCInitialize())
133 {
134 _tprintf(_T("Failed to initialize NetXMS client library\n"));
135 return 2;
136 }
137
138 if (isDebug)
139 NXCSetDebugCallback(DebugCallback);
140
141 #ifdef UNICODE
142 WCHAR whost[256];
143 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, argv[optind], -1, whost, 256);
144 whost[255] = 0;
145 #define _HOST whost
146 #else
147 #define _HOST argv[optind]
148 #endif
149
150 NXCSession *session = new NXCSession();
151 rcc = session->connect(_HOST, login, password, isEncrypt ? NXCF_ENCRYPT : 0, _T("nxsms/") NETXMS_VERSION_STRING);
152 if (rcc != RCC_SUCCESS)
153 {
154 _tprintf(_T("Unable to connect to server: %s\n"), NXCGetErrorText(rcc));
155 delete session;
156 NXCShutdown();
157 return 2;
158 }
159
160 session->setCommandTimeout(timeout * 1000);
161
162 #ifdef UNICODE
163 WCHAR *rcpt = WideStringFromMBString(argv[optind + 1]);
164 WCHAR *text = WideStringFromMBString(argv[optind + 2]);
165 rcc = ((ServerController *)session->getController(CONTROLLER_SERVER))->sendSMS(rcpt, text);
166 free(rcpt);
167 free(text);
168 #else
169 rcc = ((ServerController *)session->getController(CONTROLLER_SERVER))->sendSMS(argv[optind + 1], argv[optind + 2]);
170 #endif
171 if (rcc != RCC_SUCCESS)
172 {
173 _tprintf(_T("Unable to send SMS: %s\n"), NXCGetErrorText(rcc));
174 }
175
176 delete session;
177 NXCShutdown();
178 return (rcc == RCC_SUCCESS) ? 0 : 5;
179 }