738f9f0f7549ed2b54fc5f756c09678b232053b6
[public/netxms.git] / src / libnxcl / actions.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 2004, 2005, 2006, 2007, 2008 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: actions.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26
27 //
28 // Fill action record from message
29 //
30
31 static void ActionFromMsg(CSCPMessage *pMsg, NXC_ACTION *pAction)
32 {
33 pAction->bIsDisabled = pMsg->GetVariableShort(VID_IS_DISABLED);
34 pAction->iType = pMsg->GetVariableShort(VID_ACTION_TYPE);
35 pAction->pszData = pMsg->GetVariableStr(VID_ACTION_DATA);
36 pMsg->GetVariableStr(VID_EMAIL_SUBJECT, pAction->szEmailSubject, MAX_EMAIL_SUBJECT_LEN);
37 pMsg->GetVariableStr(VID_ACTION_NAME, pAction->szName, MAX_OBJECT_NAME);
38 pMsg->GetVariableStr(VID_RCPT_ADDR, pAction->szRcptAddr, MAX_RCPT_ADDR_LEN);
39 }
40
41
42 //
43 // Process CMD_ACTION_DB_UPDATE message
44 //
45
46 void ProcessActionUpdate(NXCL_Session *pSession, CSCPMessage *pMsg)
47 {
48 NXC_ACTION action;
49 DWORD dwCode;
50
51 dwCode = pMsg->GetVariableLong(VID_NOTIFICATION_CODE);
52 action.dwId = pMsg->GetVariableLong(VID_ACTION_ID);
53 if (dwCode != NX_NOTIFY_ACTION_DELETED)
54 ActionFromMsg(pMsg, &action);
55 pSession->callEventHandler(NXC_EVENT_NOTIFICATION, dwCode, &action);
56 }
57
58
59 //
60 // Load all actions from server
61 //
62
63 DWORD LIBNXCL_EXPORTABLE NXCLoadActions(NXC_SESSION hSession, DWORD *pdwNumActions, NXC_ACTION **ppActionList)
64 {
65 CSCPMessage msg, *pResponse;
66 DWORD dwRqId, dwRetCode = RCC_SUCCESS, dwNumActions = 0, dwActionId = 0;
67 NXC_ACTION *pList = NULL;
68
69 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
70
71 msg.SetCode(CMD_LOAD_ACTIONS);
72 msg.SetId(dwRqId);
73 ((NXCL_Session *)hSession)->SendMsg(&msg);
74
75 dwRetCode = ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
76 if (dwRetCode == RCC_SUCCESS)
77 {
78 do
79 {
80 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_ACTION_DATA, dwRqId);
81 if (pResponse != NULL)
82 {
83 dwActionId = pResponse->GetVariableLong(VID_ACTION_ID);
84 if (dwActionId != 0) // 0 is end of list indicator
85 {
86 pList = (NXC_ACTION *)realloc(pList, sizeof(NXC_ACTION) * (dwNumActions + 1));
87 pList[dwNumActions].dwId = dwActionId;
88 ActionFromMsg(pResponse, &pList[dwNumActions]);
89 dwNumActions++;
90 }
91 delete pResponse;
92 }
93 else
94 {
95 dwRetCode = RCC_TIMEOUT;
96 dwActionId = 0;
97 }
98 }
99 while(dwActionId != 0);
100 }
101
102 // Destroy results on failure or save on success
103 if (dwRetCode == RCC_SUCCESS)
104 {
105 *ppActionList = pList;
106 *pdwNumActions = dwNumActions;
107 }
108 else
109 {
110 safe_free(pList);
111 *ppActionList = NULL;
112 *pdwNumActions = 0;
113 }
114
115 return dwRetCode;
116 }
117
118
119 //
120 // Create new action on server
121 //
122
123 DWORD LIBNXCL_EXPORTABLE NXCCreateAction(NXC_SESSION hSession, TCHAR *pszName, DWORD *pdwNewId)
124 {
125 CSCPMessage msg, *pResponse;
126 DWORD dwRetCode, dwRqId;
127
128 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
129
130 msg.SetCode(CMD_CREATE_ACTION);
131 msg.SetId(dwRqId);
132 msg.SetVariable(VID_ACTION_NAME, pszName);
133 ((NXCL_Session *)hSession)->SendMsg(&msg);
134
135 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
136 if (pResponse != NULL)
137 {
138 dwRetCode = pResponse->GetVariableLong(VID_RCC);
139 if (dwRetCode == RCC_SUCCESS)
140 *pdwNewId = pResponse->GetVariableLong(VID_ACTION_ID);
141 delete pResponse;
142 }
143 else
144 {
145 dwRetCode = RCC_TIMEOUT;
146 }
147 return dwRetCode;
148 }
149
150
151 //
152 // Delete action by ID
153 //
154
155 DWORD LIBNXCL_EXPORTABLE NXCDeleteAction(NXC_SESSION hSession, DWORD dwActionId)
156 {
157 CSCPMessage msg;
158 DWORD dwRqId;
159
160 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
161
162 msg.SetCode(CMD_DELETE_ACTION);
163 msg.SetId(dwRqId);
164 msg.SetVariable(VID_ACTION_ID, dwActionId);
165 ((NXCL_Session *)hSession)->SendMsg(&msg);
166
167 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
168 }
169
170
171 //
172 // Modify action record
173 //
174
175 DWORD LIBNXCL_EXPORTABLE NXCModifyAction(NXC_SESSION hSession, NXC_ACTION *pAction)
176 {
177 CSCPMessage msg;
178 DWORD dwRqId;
179
180 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
181
182 // Fill in request
183 msg.SetCode(CMD_MODIFY_ACTION);
184 msg.SetId(dwRqId);
185 msg.SetVariable(VID_IS_DISABLED, (WORD)pAction->bIsDisabled);
186 msg.SetVariable(VID_ACTION_ID, pAction->dwId);
187 msg.SetVariable(VID_ACTION_TYPE, (WORD)pAction->iType);
188 msg.SetVariable(VID_ACTION_DATA, pAction->pszData);
189 msg.SetVariable(VID_EMAIL_SUBJECT, pAction->szEmailSubject);
190 msg.SetVariable(VID_ACTION_NAME, pAction->szName);
191 msg.SetVariable(VID_RCPT_ADDR, pAction->szRcptAddr);
192
193 ((NXCL_Session *)hSession)->SendMsg(&msg);
194
195 // Wait for response
196 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
197 }