1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
// Copyright (C) 2018, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Duplicati.Library.Interface;
using Duplicati.Library.Modules.Builtin.ResultSerialization;
using Duplicati.Library.Utility;
namespace Duplicati.Library.Modules.Builtin
{
/// <summary>
/// A helper module that contains all shared code used in the various reporting modules
/// </summary>
public abstract class ReportHelper : Interface.IGenericCallbackModule
{
/// <summary>
/// The tag used for logging
/// </summary>
private static readonly string LOGTAG = Logging.Log.LogTagFromType<ReportHelper>();
/// <summary>
/// Name of the option used to specify subject
/// </summary>
protected abstract string SubjectOptionName { get; }
/// <summary>
/// Name of the option used to specify the body
/// </summary>
protected abstract string BodyOptionName { get; }
/// <summary>
/// Name of the option used to specify the level on which the operation is activated
/// </summary>
protected abstract string ActionLevelOptionName { get; }
/// <summary>
/// Name of the option used to specify if reports are sent for other operations than backups
/// </summary>
protected abstract string ActionOnAnyOperationOptionName { get; }
/// <summary>
/// Name of the option used to specify the log level
/// </summary>
protected abstract string LogLevelOptionName { get; }
/// <summary>
/// Name of the option used to specify the log filter
/// </summary>
protected abstract string LogFilterOptionName { get; }
/// <summary>
/// Name of the option used to specify the maximum number of log lines to include
/// </summary>
protected abstract string LogLinesOptionName { get; }
/// <summary>
/// Name of the option used to the output format
/// </summary>
protected abstract string ResultFormatOptionName { get; }
/// <summary>
/// The default subject or title line
/// </summary>
protected virtual string DEFAULT_SUBJECT { get; }= "Duplicati %OPERATIONNAME% report for %backup-name%";
/// <summary>
/// The default report level
/// </summary>
protected virtual string DEFAULT_LEVEL { get; } = "all";
/// <summary>
/// The default report body
/// </summary>
protected virtual string DEFAULT_BODY { get; } = "%RESULT%";
/// <summary>
/// The default maximum number of log lines
/// </summary>
protected virtual int DEFAULT_LOGLINES { get; } = 100;
/// <summary>
/// The default log level
/// </summary>
protected virtual Logging.LogMessageType DEFAULT_LOG_LEVEL { get; } = Logging.LogMessageType.Warning;
/// <summary>
/// The default export format
/// </summary>
protected virtual ResultExportFormat DEFAULT_EXPORT_FORMAT { get; } = ResultExportFormat.Duplicati;
/// <summary>
/// The module key
/// </summary>
public abstract string Key { get; }
/// <summary>
/// The module display name
/// </summary>
public abstract string DisplayName { get; }
/// <summary>
/// The module description
/// </summary>
public abstract string Description { get; }
/// <summary>
/// The module default load setting
/// </summary>
public abstract bool LoadAsDefault { get; }
/// <summary>
/// The list of supported commands
/// </summary>
public abstract IList<ICommandLineArgument> SupportedCommands { get; }
/// <summary>
/// Returns the format used by the serializer
/// </summary>
protected ResultExportFormat ExportFormat => m_resultFormatSerializer.Format;
/// <summary>
/// The cached name of the operation
/// </summary>
protected string m_operationname;
/// <summary>
/// The cached remote url
/// </summary>
protected string m_remoteurl;
/// <summary>
/// The cached local path
/// </summary>
protected string[] m_localpath;
/// <summary>
/// The cached set of options
/// </summary>
protected IDictionary<string, string> m_options;
/// <summary>
/// The parsed result level
/// </summary>
protected string m_parsedresultlevel = string.Empty;
/// <summary>
/// The maxmimum number of log lines to include
/// </summary>
protected int m_maxmimumLogLines;
/// <summary>
/// A value indicating if this instance is configured
/// </summary>
private bool m_isConfigured;
/// <summary>
/// <summary>
/// The mail subject
/// </summary>
private string m_subject;
/// <summary>
/// The mail body
/// </summary>
private string m_body;
/// <summary>
/// The mail send level
/// </summary>
private string[] m_levels;
/// <summary>
/// True to send all operations
/// </summary>
private bool m_sendAll;
/// <summary>
/// The log scope that should be disposed
/// </summary>
private IDisposable m_logscope;
/// <summary>
/// The log storage
/// </summary>
private Utility.FileBackedStringList m_logstorage;
/// <summary>
/// Serializer to use when serializing the message.
/// </summary>
private IResultFormatSerializer m_resultFormatSerializer;
/// <summary>
/// Configures the module
/// </summary>
/// <returns><c>true</c>, if module should be used, <c>false</c> otherwise.</returns>
/// <param name="commandlineOptions">A set of commandline options passed to Duplicati</param>
protected abstract bool ConfigureModule(IDictionary<string, string> commandlineOptions);
/// <summary>
/// Sends the email message
/// </summary>
/// <param name="subject">The subject line.</param>
/// <param name="body">The message body.</param>
protected abstract void SendMessage(string subject, string body);
/// <summary>
/// This method is the interception where the module can interact with the execution environment and modify the settings.
/// </summary>
/// <param name="commandlineOptions">A set of commandline options passed to Duplicati</param>
public void Configure(IDictionary<string, string> commandlineOptions)
{
if (!ConfigureModule(commandlineOptions))
return;
m_isConfigured = true;
commandlineOptions.TryGetValue(SubjectOptionName, out m_subject);
commandlineOptions.TryGetValue(BodyOptionName, out m_body);
m_options = commandlineOptions;
string tmp;
commandlineOptions.TryGetValue(ActionLevelOptionName, out tmp);
if (!string.IsNullOrEmpty(tmp))
m_levels =
tmp
.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(x => x.Trim())
.ToArray();
if (m_levels == null || m_levels.Length == 0)
m_levels =
DEFAULT_LEVEL
.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(x => x.Trim())
.ToArray();
m_sendAll = Utility.Utility.ParseBoolOption(commandlineOptions, ActionOnAnyOperationOptionName);
ResultExportFormat resultFormat;
if (!commandlineOptions.TryGetValue(ResultFormatOptionName, out var tmpResultFormat))
resultFormat = DEFAULT_EXPORT_FORMAT;
else if (!Enum.TryParse(tmpResultFormat, true, out resultFormat))
resultFormat = DEFAULT_EXPORT_FORMAT;
m_resultFormatSerializer = ResultFormatSerializerProvider.GetSerializer(resultFormat);
commandlineOptions.TryGetValue(LogLinesOptionName, out var loglinestr);
if (!int.TryParse(loglinestr, out m_maxmimumLogLines))
m_maxmimumLogLines = DEFAULT_LOGLINES;
if (string.IsNullOrEmpty(m_subject))
m_subject = DEFAULT_SUBJECT;
if (string.IsNullOrEmpty(m_body))
m_body = DEFAULT_BODY;
m_options.TryGetValue(LogFilterOptionName, out var logfilterstring);
var filter = Utility.FilterExpression.ParseLogFilter(logfilterstring);
var logLevel = Utility.Utility.ParseEnumOption(m_options, LogLevelOptionName, DEFAULT_LOG_LEVEL);
m_logstorage = new FileBackedStringList();
m_logscope = Logging.Log.StartScope(m => m_logstorage.Add(m.AsString(true)), m => {
if (filter.Matches(m.FilterTag, out var result, out var match))
return result;
else if (m.Level < logLevel)
return false;
return true;
});
}
/// <summary>
/// Called when the operation starts
/// </summary>
/// <param name="operationname">The full name of the operation</param>
/// <param name="remoteurl">The remote backend url</param>
/// <param name="localpath">The local path, if required</param>
public virtual void OnStart(string operationname, ref string remoteurl, ref string[] localpath)
{
m_operationname = operationname;
m_remoteurl = remoteurl;
m_localpath = localpath;
}
/// <summary>
/// Helper method to perform template expansion
/// </summary>
/// <returns>The expanded template.</returns>
/// <param name="input">The input template.</param>
/// <param name="result">The result object.</param>
/// <param name="subjectline">If set to <c>true</c>, the result is intended for a subject or title line.</param>
protected virtual string ReplaceTemplate(string input, object result, bool subjectline)
{
// For JSON, ignore the template and just use the contents
if (ExportFormat == ResultExportFormat.Json)
{
var extra = new Dictionary<string, string>();
if (input.IndexOf("%OPERATIONNAME%", StringComparison.OrdinalIgnoreCase) >= 0)
extra["OperationName"] = m_operationname;
if (input.IndexOf("%REMOTEURL%", StringComparison.OrdinalIgnoreCase) >= 0)
extra["RemoteUrl"] = m_remoteurl;
if (input.IndexOf("%LOCALPATH%", StringComparison.OrdinalIgnoreCase) >= 0 && m_localpath != null)
extra["LocalPath"] = string.Join(System.IO.Path.PathSeparator.ToString(), m_localpath);
if (input.IndexOf("%PARSEDRESULT%", StringComparison.OrdinalIgnoreCase) >= 0)
extra["ParsedResult"] = m_parsedresultlevel;
if (input.IndexOf("%backup-name%", StringComparison.OrdinalIgnoreCase) >= 0)
{
if (m_options.ContainsKey("backup-name"))
extra["backup-name"] = m_options["backup-name"];
else
extra["backup-name"] = System.IO.Path.GetFileNameWithoutExtension(Duplicati.Library.Utility.Utility.getEntryAssembly().Location);
}
foreach (KeyValuePair<string, string> kv in m_options)
if (input.IndexOf($"%{kv.Key}%", StringComparison.OrdinalIgnoreCase) >= 0)
extra[kv.Key] = kv.Value;
return m_resultFormatSerializer.Serialize(result, LogLines, extra);
}
else
{
input = Regex.Replace(input, "\\%OPERATIONNAME\\%", m_operationname ?? "", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
input = Regex.Replace(input, "\\%REMOTEURL\\%", m_remoteurl ?? "", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
input = Regex.Replace(input, "\\%LOCALPATH\\%", m_localpath == null ? "" : string.Join(System.IO.Path.PathSeparator.ToString(), m_localpath), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
input = Regex.Replace(input, "\\%PARSEDRESULT\\%", m_parsedresultlevel ?? "", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
if (subjectline)
{
input = Regex.Replace(input, "\\%RESULT\\%", m_parsedresultlevel ?? "", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
else
{
if (input.IndexOf("%RESULT%", StringComparison.OrdinalIgnoreCase) >= 0)
input = Regex.Replace(input, "\\%RESULT\\%", m_resultFormatSerializer.Serialize(result, LogLines, null), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
foreach (KeyValuePair<string, string> kv in m_options)
input = Regex.Replace(input, "\\%" + kv.Key + "\\%", kv.Value ?? "", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
if (!m_options.ContainsKey("backup-name"))
input = Regex.Replace(input, "\\%backup-name\\%", System.IO.Path.GetFileNameWithoutExtension(Duplicati.Library.Utility.Utility.getEntryAssembly().Location) ?? "", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
input = Regex.Replace(input, "\\%[^\\%]+\\%", "");
return input;
}
}
/// <summary>
/// Gets the filtered set of log lines
/// </summary>
protected IEnumerable<string> LogLines
{
get
{
var logdata = m_logstorage.AsEnumerable();
if (m_maxmimumLogLines > 0)
{
logdata = logdata.Take(m_maxmimumLogLines);
if (m_logstorage.Count > m_maxmimumLogLines)
logdata = logdata.Concat(new string[] { $"... and {m_logstorage.Count - m_maxmimumLogLines} more" });
}
return logdata;
}
}
public void OnFinish(object result)
{
// Dispose the current log scope
if (m_logscope != null)
{
try { m_logscope.Dispose(); }
catch { }
m_logscope = null;
}
if (!m_isConfigured)
return;
//If we do not report this action, then skip
if (!m_sendAll && !string.Equals(m_operationname, "Backup", StringComparison.OrdinalIgnoreCase))
return;
ParsedResultType level;
if (result is Exception)
level = ParsedResultType.Fatal;
else if (result != null && result is Library.Interface.IBasicResults)
level = ((IBasicResults)result).ParsedResult;
else
level = ParsedResultType.Error;
m_parsedresultlevel = level.ToString();
if (string.Equals(m_operationname, "Backup", StringComparison.OrdinalIgnoreCase))
{
if (!m_levels.Any(x => string.Equals(x, "all", StringComparison.OrdinalIgnoreCase)))
{
//Check if this level should send mail
if (!m_levels.Any(x => string.Equals(x, level.ToString(), StringComparison.OrdinalIgnoreCase)))
return;
}
}
try
{
string body = m_body;
string subject = m_subject;
if (body != DEFAULT_BODY && System.IO.Path.IsPathRooted(body) && System.IO.File.Exists(body))
body = System.IO.File.ReadAllText(body);
body = ReplaceTemplate(body, result, false);
subject = ReplaceTemplate(subject, result, true);
SendMessage(subject, body);
}
catch (Exception ex)
{
Exception top = ex;
var sb = new StringBuilder();
while (top != null)
{
if (sb.Length != 0)
sb.Append("--> ");
sb.AppendFormat("{0}: {1}{2}", top.GetType().FullName, top.Message, Environment.NewLine);
top = top.InnerException;
}
Logging.Log.WriteWarningMessage(LOGTAG, "ReportSubmitError", ex, Strings.ReportHelper.SendMessageFailedError(sb.ToString()));
}
}
}
}
|