-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.cpp
More file actions
541 lines (493 loc) · 14.6 KB
/
plugin.cpp
File metadata and controls
541 lines (493 loc) · 14.6 KB
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
* Fledge "email" notification plugin.
*
* Copyright (c) 2018 Dianomic Systems
*
* Released under the Apache 2.0 Licence
*
* Author: Amandeep Singh Arora
*/
#include <string>
#include <plugin_api.h>
#include <config_category.h>
#include <logger.h>
#include <email_config.h>
#include <version.h>
#include <string_utils.h>
#include <regex>
#define PLUGIN_NAME "email"
static const char * def_cfg = QUOTE({
"plugin" : {
"description" : "Email notification plugin",
"type" : "string",
"default" : PLUGIN_NAME,
"readonly" : "true",
"group" : "Headers" },
"email_to" : {
"description" : "The comma separated address list to send the alert to",
"type" : "string",
"default" : "alert.subscriber@dianomic.com",
"order" : "1",
"displayName" : "To address",
"group" : "Headers",
"mandatory" : "true"
},
"email_to_name" : {
"description" : "The comma separated name list to send the alert to",
"type" : "string",
"default" : "Notification alert subscriber",
"order" : "2",
"displayName" : "To name",
"group" : "Headers",
"mandatory" : "true"
},
"email_cc" : {
"description" : "The comma separated address list to send the CC alert",
"type" : "string",
"default" : "alert.subscriber@dianomic.com",
"order" : "3",
"displayName" : "CC address",
"group" : "Headers"
},
"email_cc_name" : {
"description" : "The comma separated name list to send the CC alert",
"type" : "string",
"default" : "Notification alert subscriber",
"order" : "4",
"displayName" : "CC name",
"group" : "Headers"
},
"email_bcc" : {
"description" : "The comma separated address list to send the BCC alert",
"type" : "string",
"default" : "alert.subscriber@dianomic.com",
"order" : "5",
"displayName" : "BCC address",
"group" : "Headers"
},
"email_bcc_name" : {
"description" : "The comma separated name list to send the BCC alert",
"type" : "string",
"default" : "Notification alert subscriber",
"order" : "6",
"displayName" : "BCC name",
"group" : "Headers"
},
"email_from" : {
"description" : "The address the email will come from",
"type" : "string",
"displayName" : "From address",
"default" : "dianomic.alerts@gmail.com",
"order" : "7",
"group" : "Headers",
"mandatory" : "true"
},
"email_from_name" : {
"description" : "The name used to send the alert email",
"type" : "string",
"default" : "Notification alert",
"displayName" : "From name",
"order" : "8",
"group" : "Headers",
"mandatory" : "true"
},
"subject" : {
"description" : "The email subject. Macro $NOTIFICATION_INSTANCE_NAME$ can be used to provide information about notification instance name. Macro $REASON$ can be use to provide the reason for notification.",
"type" : "string",
"displayName" : "Subject",
"order" : "9",
"default" : "Fledge alert notification sent by $NOTIFICATION_INSTANCE_NAME$ - $REASON$ ",
"group" : "Message"
},
"email_body" : {
"description" : "The email body. Macro $MESSAGE$ can be used to provide text message received from service. Macro $NOTIFICATION_INSTANCE_NAME$ can be used to provide information about notification instance name. Macro $REASON$ can be use to provide the reason for notification.",
"type" : "string",
"displayName" : "Body",
"order" : "10",
"default" : "$MESSAGE$ sent by $NOTIFICATION_INSTANCE_NAME$, Reason is $REASON$",
"group" : "Message"
},
"server" : {
"description" : "The SMTP server name/address",
"type" : "string",
"displayName" : "SMTP Server",
"order" : "11",
"default" : "smtp.gmail.com",
"group" : "Mail Server",
"mandatory" : "true"
},
"port" : {
"description" : "The SMTP server port",
"type" : "integer",
"displayName" : "SMTP Port",
"order" : "12",
"default" : "587",
"group" : "Mail Server",
"mandatory" : "true"
},
"use_ssl_tls" : {
"description" : "Use SSL/TLS for email transfer",
"type" : "boolean",
"displayName" : "SSL/TLS",
"order" : "13",
"default" : "true",
"group" : "Mail Server"
},
"username" : {
"description" : "Email account name",
"type" : "string",
"displayName" : "Username",
"order" : "14",
"default" : "dianomic.alerts@gmail.com",
"group" : "Mail Server"
},
"password" : {
"description" : "Email account password",
"type" : "password",
"displayName" : "Password",
"order" : "15",
"default" : "pass",
"group" : "Mail Server"
},
"enable": {
"description": "A switch that can be used to enable or disable execution of the email notification plugin.",
"type": "boolean",
"displayName" : "Enabled",
"default": "false",
"order" : "16",
"group" : "Headers" }
});
using namespace std;
using namespace rapidjson;
/**
* The Notification plugin interface
*/
extern "C" {
/**
* The plugin information structure
*/
static PLUGIN_INFORMATION info = {
PLUGIN_NAME, // Name
VERSION, // Version
0, // Flags
PLUGIN_TYPE_NOTIFICATION_DELIVERY, // Type
"1.0.0", // Interface version
def_cfg // Default plugin configuration
};
typedef struct
{
EmailCfg emailCfg;
bool isConfigValid;
} PLUGIN_INFO;
bool isAddressNamePairMatch = true;
extern int sendEmailMsg(const EmailCfg *emailCfg, const char *msg);
extern char *errorString(int result);
/**
* Return the information about this plugin
*/
PLUGIN_INFORMATION *plugin_info()
{
return &info;
}
/**
* Reset/init EmailCfg structure
*/
void resetConfig(EmailCfg *emailCfg)
{
emailCfg->email_from.clear();
emailCfg->email_from_name.clear();
emailCfg->email_to.clear();
emailCfg->email_to_name.clear();
emailCfg->email_cc.clear();
emailCfg->email_cc_name.clear();
emailCfg->email_bcc.clear();
emailCfg->email_bcc_name.clear();
emailCfg->email_body.clear();
emailCfg->server.clear();
emailCfg->port = 0;
emailCfg->subject.clear();
emailCfg->use_ssl_tls = false;
emailCfg->username.clear();
emailCfg->password.clear();
}
/**
* Print EmailCfg structure
*/
void printConfig(EmailCfg *emailCfg)
{
std::string to;
for(auto it = emailCfg->email_to.begin(); it != emailCfg->email_to.end(); it++ )
{
to.append(*it);
to.append(",");
}
std::string cc;
for(auto it = emailCfg->email_cc.begin(); it != emailCfg->email_cc.end(); it++ )
{
cc.append(*it);
cc.append(",");
}
std::string bcc;
for(auto it = emailCfg->email_bcc.begin(); it != emailCfg->email_bcc.end(); it++ )
{
bcc.append(*it);
bcc.append(",");
}
Logger::getLogger()->info("email_from=%s, email_to=%s email_cc=%s email_bcc=%s ",
emailCfg->email_from.c_str(),
to.c_str(),
cc.c_str(),
bcc.c_str());
Logger::getLogger()->info("server=%s, port=%d, subject=%s, body=%s use_ssl_tls=%s, username=%s, password=%s",
emailCfg->server.c_str(), emailCfg->port, emailCfg->subject.c_str(), emailCfg->email_body.c_str(),
emailCfg->use_ssl_tls?"true":"false", emailCfg->username.c_str(), emailCfg->password.c_str());
}
/**
* Tokenize sting
*/
std::vector<std::string> stringTokenize(std::string searchString ,std::regex searchPattern)
{
std::vector<std::string> vec;
auto token_start = std::sregex_iterator(searchString.begin(), searchString.end(), searchPattern);
auto token_end = std::sregex_iterator();
for (std::sregex_iterator i = token_start; i != token_end; ++i)
{
std::smatch match = *i;
std::string matchValue = match.str();
//Don't populate blank values
matchValue = StringRTrim(StringLTrim(matchValue));
if (matchValue.size())
{
vec.emplace_back(matchValue);
}
}
return vec;
}
/**
* Fill EmailCfg structure from JSON document representing email server/account config
*/
void parseConfig(ConfigCategory *config, EmailCfg *emailCfg)
{
std::regex searchPattern("[^\\,]+");
if (config->itemExists("email_from"))
{
emailCfg->email_from = StringStripWhiteSpacesAll(config->getValue("email_from"));
}
if (config->itemExists("email_from_name"))
{
emailCfg->email_from_name = config->getValue("email_from_name");
}
if (config->itemExists("email_to"))
{
emailCfg->email_to.clear();
emailCfg->email_to = stringTokenize(config->getValue("email_to"),searchPattern);
}
if (config->itemExists("email_to_name"))
{
emailCfg->email_to_name.clear();
emailCfg->email_to_name = stringTokenize(config->getValue("email_to_name"),searchPattern);
}
if (config->itemExists("email_cc"))
{
emailCfg->email_cc.clear();
emailCfg->email_cc = stringTokenize(config->getValue("email_cc"),searchPattern);
}
if (config->itemExists("email_cc_name"))
{
emailCfg->email_cc_name.clear();
emailCfg->email_cc_name = stringTokenize(config->getValue("email_cc_name"),searchPattern);
}
if (config->itemExists("email_bcc"))
{
emailCfg->email_bcc.clear();
emailCfg->email_bcc = stringTokenize(config->getValue("email_bcc"),searchPattern);
}
if (config->itemExists("email_bcc_name"))
{
emailCfg->email_bcc_name.clear();
emailCfg->email_bcc_name = stringTokenize(config->getValue("email_bcc_name"),searchPattern);
}
if (config->itemExists("email_body"))
{
emailCfg->email_body = config->getValue("email_body");
}
if (config->itemExists("server"))
{
emailCfg->server = StringStripWhiteSpacesAll(config->getValue("server"));
}
if (config->itemExists("port"))
{
emailCfg->port = (unsigned int)atoi(config->getValue("port").c_str());
}
if (config->itemExists("subject"))
{
emailCfg->subject = config->getValue("subject");
}
if (config->itemExists("use_ssl_tls"))
{
emailCfg->use_ssl_tls = config->getValue("use_ssl_tls").compare("true") ? false : true;
}
if (config->itemExists("username"))
{
emailCfg->username = config->getValue("username");
}
if (config->itemExists("password"))
{
emailCfg->password = config->getValue("password");
}
}
/**
* Validate count of email address and name pairs
*/
void validateConfig(PLUGIN_HANDLE *handle, EmailCfg *emailCfg)
{
PLUGIN_INFO *info = (PLUGIN_INFO *) handle;
info->isConfigValid = true;
// Check for complete configuration
int numRecipients = emailCfg->email_to.size() + emailCfg->email_cc.size() + emailCfg->email_bcc.size() ;
if ( numRecipients == 0)
{
info->isConfigValid = false;
Logger::getLogger()->error("No valid recipient email address(es)");
return;
}
if ( emailCfg->email_from.empty())
{
info->isConfigValid = false;
Logger::getLogger()->error("Sender email address is missing");
return;
}
if (emailCfg->server.empty() || emailCfg->port == 0)
{
info->isConfigValid = false;
Logger::getLogger()->error("Invalid Email server/port configuration");
return;
}
// Check if To address and To name have same count
if ( emailCfg->email_to.size() != emailCfg->email_to_name.size() )
{
info->isConfigValid = false;
Logger::getLogger()->error("There is a mismatch between To address and To name count.");
return;
}
// Check if CC address and CC name have same count
if ( emailCfg->email_cc.size() != emailCfg->email_cc_name.size() )
{
info->isConfigValid = false;
Logger::getLogger()->error("There is a mismatch between CC address and CC name count.");
return;
}
// Check if BCC address and BCC name have same count
if ( emailCfg->email_bcc.size() != emailCfg->email_bcc_name.size() )
{
info->isConfigValid = false;
Logger::getLogger()->error("There is a mismatch between BCC address and BCC names count.");
return;
}
}
/**
* Initialise the plugin, called to get the plugin handle and setup the
* plugin configuration
*
* @param config The configuration category for the plugin
* @return An opaque handle that is used in all subsequent calls to the plugin
*/
PLUGIN_HANDLE plugin_init(ConfigCategory* config)
{
PLUGIN_INFO *info = new PLUGIN_INFO;
// Handle plugin configuration
if (config)
{
Logger::getLogger()->info("Email plugin config=%s", config->toJSON().c_str());
info->isConfigValid = true;
resetConfig(&info->emailCfg);
parseConfig(config, &info->emailCfg);
printConfig(&info->emailCfg);
validateConfig((PLUGIN_HANDLE*)info,&info->emailCfg);
}
else
{
info->isConfigValid = false;
Logger::getLogger()->fatal("No config provided for email plugin");
}
return (PLUGIN_HANDLE)info;
}
/**
* Deliver received notification data
*
* @param handle The plugin handle returned from plugin_init
* @param deliveryName The delivery category name
* @param notificationName The notification name
* @param triggerReason The trigger reason for notification
* @param message The message from notification
*/
bool plugin_deliver(PLUGIN_HANDLE handle,
const std::string& deliveryName,
const std::string& notificationName,
const std::string& triggerReason,
const std::string& message)
{
Logger::getLogger()->info("Email notification plugin_deliver(): deliveryName=%s, notificationName=%s, triggerReason=%s, message=%s",
deliveryName.c_str(), notificationName.c_str(), triggerReason.c_str(), message.c_str());
PLUGIN_INFO *info = (PLUGIN_INFO *) handle;
// Parse JSON triggerReason
Document doc;
doc.Parse(triggerReason.c_str());
if (doc.HasParseError())
{
Logger::getLogger()->error("Email notification delivery: failure parsing JSON trigger reason '%s'", triggerReason.c_str());
return false;
}
// Replace Macros for subject
StringReplace(info->emailCfg.subject, "$NOTIFICATION_INSTANCE_NAME$", notificationName);
string reason = doc["reason"].GetString();
StringReplace(info->emailCfg.subject, "$REASON$", reason);
// Replace Macros for email body
std::string emailBody = info->emailCfg.email_body;
StringReplace(emailBody, "$MESSAGE$", message);
StringReplace(emailBody, "$REASON$", reason);
StringReplace(emailBody, "$NOTIFICATION_INSTANCE_NAME$", notificationName);
int rv = 0;
if (info->isConfigValid)
{
rv = sendEmailMsg(&info->emailCfg, emailBody.c_str());
}
else
{
Logger::getLogger()->warn("Email delivery notification aborted due to mismatch in email Id and name count");
return false;
}
if (rv)
{
Logger::getLogger()->error("Email notification failed: sendEmailMsg() returned %d, %s", rv, errorString(rv));
return false;
}
else
{
Logger::getLogger()->info("sendEmailMsg() returned SUCCESS");
return true;
}
}
/**
* Reconfigure the plugin
*/
void plugin_reconfigure(PLUGIN_HANDLE *handle, string& newConfig)
{
Logger::getLogger()->info("Email notification plugin: plugin_reconfigure()");
PLUGIN_INFO *info = (PLUGIN_INFO *) handle;
ConfigCategory config("new", newConfig);
Logger::getLogger()->info("Email plugin reconfig=%s", newConfig.c_str());
parseConfig(&config, &info->emailCfg);
validateConfig(handle,&info->emailCfg);
return;
}
/**
* Call the shutdown method in the plugin
*/
void plugin_shutdown(PLUGIN_HANDLE *handle)
{
PLUGIN_INFO *info = (PLUGIN_INFO *) handle;
delete info;
}
// End of extern "C"
};