1 /*
2  *  This file is part of nzbget. See <http://nzbget.net>.
3  *
4  *  Copyright (C) 2005 Bo Cordes Petersen <placebodk@users.sourceforge.net>
5  *  Copyright (C) 2007-2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 #ifndef MESSAGEBASE_H
23 #define MESSAGEBASE_H
24 
25 static const int32 NZBMESSAGE_SIGNATURE = 0x6E7A6231; // = "nzb-XX" (protocol version)
26 static const int NZBREQUESTFILENAMESIZE = 512;
27 static const int NZBREQUESTPASSWORDSIZE = 32;
28 
29 /**
30 * NZBGet communication protocol uses only two basic data types: integer and char.
31 * Integer values are passed using network byte order (Big-Endian).
32 * Use function "htonl" and "ntohl" to convert integers to/from machine
33 * (host) byte order.
34 * All char-strings ends with nullptr-char.
35 *
36 * NOTE:
37 * NZBGet communication protocol is intended for usage only by NZBGet itself.
38 * The communication works only if server and client has the same version.
39 * The compatibility with previous program versions is not provided.
40 * Third-party programs should use JSON-RPC or XML-RPC to communicate with NZBGet.
41 */
42 
43 // Possible values for field "m_type" of struct "SNZBRequestBase":
44 enum ERemoteRequest
45 {
46 	rrDownload = 1,
47 	rrPauseUnpause,
48 	rrList,
49 	rrSetDownloadRate,
50 	rrDumpDebug,
51 	rrEditQueue,
52 	rrLog,
53 	rrShutdown,
54 	rrReload,
55 	rrVersion,
56 	rrPostQueue,
57 	rrWriteLog,
58 	rrScan,
59 	rrHistory
60 };
61 
62 // Possible values for field "m_action" of struct "SNZBPauseUnpauseRequest":
63 enum ERemotePauseUnpauseAction
64 {
65 	rpDownload = 1, // pause/unpause download queue
66 	rpPostProcess, // pause/unpause post-processor queue
67 	rpScan // pause/unpause scan of incoming nzb-directory
68 };
69 
70 // Possible values for field "m_matchMode" of struct "SNZBEditQueueRequest":
71 enum ERemoteMatchMode
72 {
73 	rmId = 1, // ID
74 	rmName, // Name
75 	rmRegEx // RegEx
76 };
77 
78 // The basic SNZBRequestBase struct, used in all requests
79 struct SNzbRequestBase
80 {
81 	int32 m_signature; // Signature must be NZBMESSAGE_SIGNATURE in integer-value
82 	int32 m_structSize; // Size of the entire struct
83 	int32 m_type; // Message type, see enum in NZBMessageRequest-namespace
84 	char m_username[NZBREQUESTPASSWORDSIZE]; // User name
85 	char m_password[NZBREQUESTPASSWORDSIZE]; // Password
86 };
87 
88 // The basic SNZBResposneBase struct, used in all responses
89 struct SNzbResponseBase
90 {
91 	int32 m_signature; // Signature must be NZBMESSAGE_SIGNATURE in integer-value
92 	int32 m_structSize; // Size of the entire struct
93 };
94 
95 // A download request
96 struct SNzbDownloadRequest
97 {
98 	SNzbRequestBase m_messageBase; // Must be the first in the struct
99 	char m_nzbFilename[NZBREQUESTFILENAMESIZE]; // Name of nzb-file. For URLs can be empty, then the filename is read from URL download response
100 	char m_category[NZBREQUESTFILENAMESIZE]; // Category, can be empty
101 	int32 m_addFirst; // 1 - add file to the top of download queue
102 	int32 m_addPaused; // 1 - pause added files
103 	int32 m_priority; // Priority for files (0 - default)
104 	int32 m_dupeScore; // Duplicate score
105 	int32 m_dupeMode; // Duplicate mode (EDupeMode)
106 	char m_dupeKey[NZBREQUESTFILENAMESIZE]; // Duplicate key
107 	int32 m_trailingDataLength; // Length of nzb-file in bytes
108 	//char m_content[m_trailingDataLength]; // variable sized
109 };
110 
111 // A download response
112 struct SNzbDownloadResponse
113 {
114 	SNzbResponseBase m_messageBase; // Must be the first in the struct
115 	int32 m_success; // 0 - command failed, 1 - command executed successfully
116 	int32 m_trailingDataLength; // Length of Text-string (m_szText), following to this record
117 	//char m_text[m_trailingDataLength]; // variable sized
118 };
119 
120 // A list and status request
121 struct SNzbListRequest
122 {
123 	SNzbRequestBase m_messageBase; // Must be the first in the struct
124 	int32 m_fileList; // 1 - return file list
125 	int32 m_serverState; // 1 - return server state
126 	int32 m_matchMode; // File/Group match mode, see enum eRemoteMatchMode (only values eRemoteMatchModeID (no filter) and eRemoteMatchModeRegEx are allowed)
127 	int32 m_matchGroup; // 0 - match files; 1 - match nzbs (when m_iMatchMode == eRemoteMatchModeRegEx)
128 	char m_pattern[NZBREQUESTFILENAMESIZE]; // RegEx Pattern (when m_iMatchMode == eRemoteMatchModeRegEx)
129 };
130 
131 // A list response
132 struct SNzbListResponse
133 {
134 	SNzbResponseBase m_messageBase; // Must be the first in the struct
135 	int32 m_entrySize; // Size of the SNZBListResponseEntry-struct
136 	int32 m_remainingSizeLo; // Remaining size in bytes, Low 32-bits of 64-bit value
137 	int32 m_remainingSizeHi; // Remaining size in bytes, High 32-bits of 64-bit value
138 	int32 m_downloadRate; // Current download speed, in Bytes pro Second
139 	int32 m_downloadLimit; // Current download limit, in Bytes pro Second
140 	int32 m_downloadPaused; // 1 - download queue is currently in paused-state
141 	int32 m_download2Paused; // 1 - download queue is currently in paused-state (second pause-register)
142 	int32 m_downloadStandBy; // 0 - there are currently downloads running, 1 - no downloads in progress (download queue paused or all download jobs completed)
143 	int32 m_postPaused; // 1 - post-processor queue is currently in paused-state
144 	int32 m_scanPaused; // 1 - scaning of incoming directory is currently in paused-state
145 	int32 m_threadCount; // Number of threads running
146 	int32 m_postJobCount; // Number of jobs in post-processor queue (including current job)
147 	int32 m_upTimeSec; // Server up time in seconds
148 	int32 m_downloadTimeSec; // Server download time in seconds (up_time - standby_time)
149 	int32 m_downloadedBytesLo; // Amount of data downloaded since server start, Low 32-bits of 64-bit value
150 	int32 m_downloadedBytesHi; // Amount of data downloaded since server start, High 32-bits of 64-bit value
151 	int32 m_regExValid; // 0 - error in RegEx-pattern, 1 - RegEx-pattern is valid (only when Request has eRemoteMatchModeRegEx)
152 	int32 m_nrTrailingNzbEntries; // Number of List-NZB-entries, following to this structure
153 	int32 m_nrTrailingPPPEntries; // Number of List-PPP-entries, following to this structure
154 	int32 m_nrTrailingFileEntries; // Number of List-File-entries, following to this structure
155 	int32 m_trailingDataLength; // Length of all List-entries, following to this structure
156 	// SNzbListResponseEntry m_nzbEntries[m_nrTrailingNZBEntries] // variable sized
157 	// SNzbListResponseEntry m_pppEntries[m_nrTrailingPPPEntries] // variable sized
158 	// SNzbListResponseEntry m_fileEntries[m_nrTrailingFileEntries] // variable sized
159 };
160 
161 // A list response nzb entry
162 struct SNzbListResponseNzbEntry
163 {
164 	int32 m_id; // NZB-ID
165 	int32 m_kind; // Item Kind (see NZBInfo::Kind)
166 	int32 m_sizeLo; // Size of all files in bytes, Low 32-bits of 64-bit value
167 	int32 m_sizeHi; // Size of all files in bytes, High 32-bits of 64-bit value
168 	int32 m_remainingSizeLo; // Size of remaining (unpaused) files in bytes, Low 32-bits of 64-bit value
169 	int32 m_remainingSizeHi; // Size of remaining (unpaused) files in bytes, High 32-bits of 64-bit value
170 	int32 m_pausedSizeLo; // Size of npaused files in bytes, Low 32-bits of 64-bit value
171 	int32 m_pausedSizeHi; // Size of paused files in bytes, High 32-bits of 64-bit value
172 	int32 m_pausedCount; // Number of paused files
173 	int32 m_remainingParCount; // Number of remaining par-files
174 	int32 m_priority; // Download priority
175 	int32 m_match; // 1 - group matches the pattern (only when Request has eRemoteMatchModeRegEx)
176 	int32 m_filenameLen; // Length of Filename-string (m_szFilename), following to this record
177 	int32 m_nameLen; // Length of Name-string (m_szName), following to this record
178 	int32 m_destDirLen; // Length of DestDir-string (m_szDestDir), following to this record
179 	int32 m_categoryLen; // Length of Category-string (m_szCategory), following to this record
180 	int32 m_queuedFilenameLen; // Length of queued file name (m_szQueuedFilename), following to this record
181 	//char m_filename[m_filenameLen]; // variable sized
182 	//char m_name[m_nameLen]; // variable sized
183 	//char m_destDir[m_destDirLen]; // variable sized
184 	//char m_dategory[m_categoryLen]; // variable sized
185 	//char m_queuedFilename[m_queuedFilenameLen]; // variable sized
186 };
187 
188 // A list response pp-parameter entry
189 struct SNzbListResponsePPPEntry
190 {
191 	int32 m_nzbIndex; // Index of NZB-Entry in m_NZBEntries-list
192 	int32 m_nameLen; // Length of Name-string (m_szName), following to this record
193 	int32 m_valueLen; // Length of Value-string (m_szValue), following to this record
194 	//char m_name[m_nameLen]; // variable sized
195 	//char m_value[m_valueLen]; // variable sized
196 };
197 
198 // A list response file entry
199 struct SNzbListResponseFileEntry
200 {
201 	int32 m_id; // Entry-ID
202 	int32 m_nzbIndex; // Index of NZB-Entry in m_NZBEntries-list
203 	int32 m_fileSizeLo; // Filesize in bytes, Low 32-bits of 64-bit value
204 	int32 m_fileSizeHi; // Filesize in bytes, High 32-bits of 64-bit value
205 	int32 m_remainingSizeLo; // Remaining size in bytes, Low 32-bits of 64-bit value
206 	int32 m_remainingSizeHi; // Remaining size in bytes, High 32-bits of 64-bit value
207 	int32 m_paused; // 1 - file is paused
208 	int32 m_filenameConfirmed; // 1 - Filename confirmed (read from article body), 0 - Filename parsed from subject (can be changed after reading of article)
209 	int32 m_activeDownloads; // Number of active downloads for this file
210 	int32 m_match; // 1 - file matches the pattern (only when Request has eRemoteMatchModeRegEx)
211 	int32 m_subjectLen; // Length of Subject-string (m_szSubject), following to this record
212 	int32 m_filenameLen; // Length of Filename-string (m_szFilename), following to this record
213 	//char m_subject[m_subjectLen]; // variable sized
214 	//char m_filename[m_filenameLen]; // variable sized
215 };
216 
217 // A log request
218 struct SNzbLogRequest
219 {
220 	SNzbRequestBase m_messageBase; // Must be the first in the struct
221 	int32 m_idFrom; // Only one of these two parameters
222 	int32 m_lines; // can be set. The another one must be set to "0".
223 };
224 
225 // A log response
226 struct SNzbLogResponse
227 {
228 	SNzbResponseBase m_messageBase; // Must be the first in the struct
229 	int32 m_entrySize; // Size of the SNZBLogResponseEntry-struct
230 	int32 m_nrTrailingEntries; // Number of Log-entries, following to this structure
231 	int32 m_trailingDataLength; // Length of all Log-entries, following to this structure
232 	// SNZBLogResponseEntry m_entries[m_trailingEntries] // variable sized
233 };
234 
235 // A log response entry
236 struct SNzbLogResponseEntry
237 {
238 	int32 m_id; // ID of Log-entry
239 	int32 m_kind; // see Message::Kind in "Log.h"
240 	int32 m_time; // time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.
241 	int32 m_textLen; // Length of Text-string (m_szText), following to this record
242 	//char m_text[m_textLen]; // variable sized
243 };
244 
245 // A Pause/Unpause request
246 struct SNzbPauseUnpauseRequest
247 {
248 	SNzbRequestBase m_messageBase; // Must be the first in the struct
249 	int32 m_pause; // 1 - server must be paused, 0 - server must be unpaused
250 	int32 m_action; // Action to be executed, see enum ERemotePauseUnpauseAction
251 };
252 
253 // A Pause/Unpause response
254 struct SNzbPauseUnpauseResponse
255 {
256 	SNzbResponseBase m_messageBase; // Must be the first in the struct
257 	int32 m_success; // 0 - command failed, 1 - command executed successfully
258 	int32 m_trailingDataLength; // Length of Text-string (m_szText), following to this record
259 	//char m_text[m_trailingDataLength]; // variable sized
260 };
261 
262 // Request setting the download rate
263 struct SNzbSetDownloadRateRequest
264 {
265 	SNzbRequestBase m_messageBase; // Must be the first in the struct
266 	int32 m_downloadRate; // Speed limit, in Bytes pro Second
267 };
268 
269 // A setting download rate response
270 struct SNzbSetDownloadRateResponse
271 {
272 	SNzbResponseBase m_messageBase; // Must be the first in the struct
273 	int32 m_success; // 0 - command failed, 1 - command executed successfully
274 	int32 m_trailingDataLength; // Length of Text-string (m_szText), following to this record
275 								//char m_text[m_trailingDataLength]; // variable sized
276 };
277 
278 // edit queue request
279 struct SNzbEditQueueRequest
280 {
281 	SNzbRequestBase m_messageBase; // Must be the first in the struct
282 	int32 m_action; // Action to be executed, see enum DownloadQueue::EEditAction
283 	int32 m_offset; // Offset to move (for m_iAction = 0)
284 	int32 m_matchMode; // File/Group match mode, see enum eRemoteMatchMode
285 	int32 m_nrTrailingIdEntries; // Number of ID-entries, following to this structure
286 	int32 m_nrTrailingNameEntries; // Number of Name-entries, following to this structure
287 	int32 m_trailingNameEntriesLen; // Length of all Name-entries, following to this structure
288 	int32 m_textLen; // Length of Text-string (m_szText), following to this record
289 	int32 m_trailingDataLength; // Length of Text-string and all ID-entries, following to this structure
290 	//char m_text[m_textLen]; // variable sized
291 	//int32 m_ids[m_nrTrailingIdEntries]; // variable sized array of IDs. For File-Actions - ID of file, for Group-Actions - ID of any file belonging to group
292 	//char* m_names[m_nrTrailingNameEntries]; // variable sized array of strings. For File-Actions - name of file incl. nzb-name as path, for Group-Actions - name of group
293 };
294 
295 // An edit queue response
296 struct SNzbEditQueueResponse
297 {
298 	SNzbResponseBase m_messageBase; // Must be the first in the struct
299 	int32 m_success; // 0 - command failed, 1 - command executed successfully
300 	int32 m_trailingDataLength; // Length of Text-string (m_szText), following to this record
301 	//char m_text[m_trailingDataLength]; // variable sized
302 };
303 
304 // Request dumping of debug info
305 struct SNzbDumpDebugRequest
306 {
307 	SNzbRequestBase m_messageBase; // Must be the first in the struct
308 };
309 
310 // Dumping of debug response
311 struct SNzbDumpDebugResponse
312 {
313 	SNzbResponseBase m_messageBase; // Must be the first in the struct
314 	int32 m_success; // 0 - command failed, 1 - command executed successfully
315 	int32 m_trailingDataLength; // Length of Text-string (m_szText), following to this record
316 	//char m_text[m_trailingDataLength]; // variable sized
317 };
318 
319 // Shutdown server request
320 struct SNzbShutdownRequest
321 {
322 	SNzbRequestBase m_messageBase; // Must be the first in the struct
323 };
324 
325 // Shutdown server response
326 struct SNzbShutdownResponse
327 {
328 	SNzbResponseBase m_messageBase; // Must be the first in the struct
329 	int32 m_success; // 0 - command failed, 1 - command executed successfully
330 	int32 m_trailingDataLength; // Length of Text-string (m_szText), following to this record
331 	//char m_text[m_trailingDataLength]; // variable sized
332 };
333 
334 // Reload server request
335 struct SNzbReloadRequest
336 {
337 	SNzbRequestBase m_messageBase; // Must be the first in the struct
338 };
339 
340 // Reload server response
341 struct SNzbReloadResponse
342 {
343 	SNzbResponseBase m_messageBase; // Must be the first in the struct
344 	int32 m_success; // 0 - command failed, 1 - command executed successfully
345 	int32 m_trailingDataLength; // Length of Text-string (m_szText), following to this record
346 	//char m_text[m_trailingDataLength]; // variable sized
347 };
348 
349 // Server version request
350 struct SNzbVersionRequest
351 {
352 	SNzbRequestBase m_messageBase; // Must be the first in the struct
353 };
354 
355 // Server version response
356 struct SNzbVersionResponse
357 {
358 	SNzbResponseBase m_messageBase; // Must be the first in the struct
359 	int32 m_success; // 0 - command failed, 1 - command executed successfully
360 	int32 m_trailingDataLength; // Length of Text-string (m_szText), following to this record
361 	//char m_text[m_trailingDataLength]; // variable sized
362 };
363 
364 // PostQueue request
365 struct SNzbPostQueueRequest
366 {
367 	SNzbRequestBase m_messageBase; // Must be the first in the struct
368 };
369 
370 // A PostQueue response
371 struct SNzbPostQueueResponse
372 {
373 	SNzbResponseBase m_messageBase; // Must be the first in the struct
374 	int32 m_entrySize; // Size of the SNZBPostQueueResponseEntry-struct
375 	int32 m_nrTrailingEntries; // Number of PostQueue-entries, following to this structure
376 	int32 m_trailingDataLength; // Length of all PostQueue-entries, following to this structure
377 	// SNZBPostQueueResponseEntry m_entries[m_nrTrailingEntries] // variable sized
378 };
379 
380 // A PostQueue response entry
381 struct SNzbPostQueueResponseEntry
382 {
383 	int32 m_id; // ID of Post-entry
384 	int32 m_stage; // See PrePostProcessor::EPostJobStage
385 	int32 m_stageProgress; // Progress of current stage, value in range 0..1000
386 	int32 m_fileProgress; // Progress of current file, value in range 0..1000
387 	int32 m_totalTimeSec; // Number of seconds this post-job is beeing processed (after it first changed the state from QUEUED).
388 	int32 m_stageTimeSec; // Number of seconds the current stage is beeing processed.
389 	int32 m_nzbFilenameLen; // Length of NZBFileName-string (m_szNZBFilename), following to this record
390 	int32 m_infoNameLen; // Length of Filename-string (m_szFilename), following to this record
391 	int32 m_destDirLen; // Length of DestDir-string (m_szDestDir), following to this record
392 	int32 m_progressLabelLen; // Length of ProgressLabel-string (m_szProgressLabel), following to this record
393 	//char m_nzbFilename[m_nzbFilenameLen]; // variable sized, may contain full path (local path on client) or only filename
394 	//char m_infoName[m_infoNameLen]; // variable sized
395 	//char m_destDir[m_destDirLen]; // variable sized
396 	//char m_progressLabel[m_progressLabelLen]; // variable sized
397 };
398 
399 // Write log request
400 struct SNzbWriteLogRequest
401 {
402 	SNzbRequestBase m_messageBase; // Must be the first in the struct
403 	int32 m_kind; // see Message::Kind in "Log.h"
404 	int32 m_trailingDataLength; // Length of nzb-file in bytes
405 	//char m_text[m_trailingDataLength]; // variable sized
406 };
407 
408 // Write log response
409 struct SNzbWriteLogResponse
410 {
411 	SNzbResponseBase m_messageBase; // Must be the first in the struct
412 	int32 m_success; // 0 - command failed, 1 - command executed successfully
413 	int32 m_trailingDataLength; // Length of Text-string (m_szText), following to this record
414 	//char m_text[m_trailingDataLength]; // variable sized
415 };
416 
417 // Scan nzb directory request
418 struct SNzbScanRequest
419 {
420 	SNzbRequestBase m_messageBase; // Must be the first in the struct
421 	int32 m_syncMode; // 0 - asynchronous Scan (the command returns immediately), 1 - synchronous Scan (the command returns when the scan is completed)
422 };
423 
424 // Scan nzb directory response
425 struct SNzbScanResponse
426 {
427 	SNzbResponseBase m_messageBase; // Must be the first in the struct
428 	int32 m_success; // 0 - command failed, 1 - command executed successfully
429 	int32 m_trailingDataLength; // Length of Text-string (m_szText), following to this record
430 	//char m_text[m_trailingDataLength]; // variable sized
431 };
432 
433 // A history request
434 struct SNzbHistoryRequest
435 {
436 	SNzbRequestBase m_messageBase; // Must be the first in the struct
437 	int32 m_hidden; // 0 - only return visible records, 1 - also return hidden records
438 };
439 
440 // history response
441 struct SNzbHistoryResponse
442 {
443 	SNzbResponseBase m_messageBase; // Must be the first in the struct
444 	int32 m_entrySize; // Size of the SNZBHistoryResponseEntry-struct
445 	int32 m_nrTrailingEntries; // Number of History-entries, following to this structure
446 	int32 m_trailingDataLength; // Length of all History-entries, following to this structure
447 	// SNZBHistoryResponseEntry m_entries[m_nrTrailingEntries] // variable sized
448 };
449 
450 // history entry
451 struct SNzbHistoryResponseEntry
452 {
453 	int32 m_id; // History-ID
454 	int32 m_kind; // Kind of Item: 1 - Collection (NZB), 2 - URL, 3 - DUP (hidden record)
455 	int32 m_time; // When the item was added to history. time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.
456 	int32 m_nicenameLen; // Length of Nicename-string (m_szNicename), following to this record
457 	// for Collection and Dup items (m_iKind = 1 or 2)
458 	int32 m_sizeLo; // Size of all files in bytes, Low 32-bits of 64-bit value
459 	int32 m_sizeHi; // Size of all files in bytes, High 32-bits of 64-bit value
460 	// for Collection items (m_iKind = 1)
461 	int32 m_fileCount; // Initial number of files included in NZB-file
462 	int32 m_parStatus; // See NZBInfo::EParStatus
463 	int32 m_scriptStatus; // See NZBInfo::EScriptStatus
464 	// for URL items (m_iKind = 2)
465 	int32 m_urlStatus; // See NZBInfo::EUrlStatus
466 	//char m_nicename[m_nicenameLen]; // variable sized
467 };
468 
469 #endif
470