forked from d0ugal/znc_mailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.cpp
445 lines (328 loc) · 12.3 KB
/
mailer.cpp
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
/**
* ZNC Mailer Module
*
* A ZNC Module that uses the Unix mail command to send mentions and private
* messages to an email address when you are not connected to ZNC.
*
* Copyright (c) 2011 Dougal Matthews
* Licensed under the MIT license
* Edited by Nox (2013)
*/
#include <Chan.h>
#include <IRCSock.h>
#include <list>
#include <main.h>
#include <Modules.h>
#include <Nick.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <User.h>
// Convert an int into a string.
CString intToString (int value) {
std::stringstream ss;
ss << value;
CString sValue = ss.str();
return sValue;
}
// Forward de
class CMailerTimer: public CTimer {
public:
CMailerTimer(CModule* pModule, unsigned int uInterval, unsigned int uCycles, const CString& sLabel, const CString& sDescription) : CTimer(pModule, uInterval, uCycles, sLabel, sDescription) {}
virtual ~CMailerTimer() {}
protected:
virtual void RunJob();
};
class CMailer : public CModule {
protected:
CUser *ConnectedUser;
list<CString> MessagesList;
CString NotificationSubject;
CString NotificationEmail;
unsigned int MaxNotifications;
bool DebugMode;
public:
MODCONSTRUCTOR(CMailer) {
ConnectedUser = GetUser();
DebugMode = false;
}
virtual ~CMailer() {}
void DebugPrint(string sMessage){
if (DebugMode){
PutModule(sMessage);
}
}
virtual bool OnLoad(const CString& sArgs, CString& sErrorMsg) {
/*
* On module load, check for debug being passed in as an arguement.
*
* Then setup the timer for batch emails and prompt the user for their
* email address.
*/
VCString tokens;
int token_count = sArgs.Split(" ", tokens, false);
if (token_count >= 1){
CString action = tokens[0].AsLower();
if (action == "debug"){
PutModule("DEBUG ON");
DebugMode = true;
}
}
// Recover config vars.
NotificationEmail = GetNV("email");
NotificationSubject = GetNV("subject");
unsigned int interval = atoi(GetNV("interval").c_str());
MaxNotifications = atoi(GetNV("maxNotifications").c_str());
if (DebugMode) {
PutModule("NV: email: " + NotificationEmail);
PutModule("NV: subject: " + NotificationSubject);
PutModule("NV: interval: " + intToString(interval));
PutModule("NV: maxNotifications: " + intToString(MaxNotifications));
}
// Set to default if vars are missing.
if (interval <= 0) {
// Default to 20 mins, debug to 60 seconds.
interval = 1200;
if (DebugMode){
interval = 60;
}
SetNV("interval", "1200");
}
if (MaxNotifications <= 0) {
// Default to 50.
MaxNotifications = 50;
SetNV("maxNotifications", "50");
}
AddTimer(new CMailerTimer(this, interval, 0, "Mail", "Send emails every "+intToString(interval)+" mins"));
if (NotificationSubject == "") {
NotificationSubject = "IRC Notification";
SetNV("subject", "IRC Notification");
PutModule("Default subject : 'IRC Notification' (change whith 'subject <subject>')");
}
if (NotificationEmail == "") {
PutModule("Please tell me what email address you want notifications to be sent to with 'email <address>'");
}
return true;
}
virtual EModRet OnChanMsg(CNick& Nick, CChan& Channel, CString& sMessage) {
Handle(Nick, Channel.GetName(), sMessage);
return CONTINUE;
}
virtual EModRet OnPrivMsg(CNick& Nick, CString& sMessage) {
Handle(Nick, "PRIVATE", sMessage);
return CONTINUE;
}
void OnModCommand(const CString& command)
{
VCString tokens;
int token_count = command.Split(" ", tokens, false);
if (token_count < 1)
{
return;
}
CString action = tokens[0].AsLower();
if (action == "email"){
if (token_count < 2)
{
PutModule("Email: " + NotificationEmail);
PutModule("Usage: email <email address>");
return;
}
NotificationEmail = tokens[1].AsLower();
SetNV("email", NotificationEmail);
PutModule("email address set to : "+ NotificationEmail);
} else if (action == "subject"){
if (token_count < 2)
{
PutModule("Subject: " + NotificationSubject);
PutModule("Usage: subject <email subject>");
return;
}
NotificationSubject = tokens[1];
for (int i = 2; i < token_count; i++) {
NotificationSubject += " " + tokens[i];
}
SetNV("subject", NotificationSubject);
PutModule("email subject set to : "+ NotificationSubject);
} else if (action == "interval") {
if (token_count < 2)
{
PutModule("Interval: " + GetNV("interval"));
PutModule("Usage: interval <seconds>");
return;
}
SetNV("interval", tokens[1]);
PutModule("interval set to : "+ tokens[1] + " , will be effective after reloading mailer : '/msg *status reloadmod mailer'");
} else if (action == "notifmax") {
if (token_count < 2)
{
PutModule("maxNotifications: " + intToString(MaxNotifications));
PutModule("Usage: notifmax <number>");
return;
}
MaxNotifications = atoi(tokens[1].c_str());
SetNV("maxNotifications", tokens[1]);
PutModule("maxNotifications set to : "+ tokens[1]);
} else if (action == "help") {
PutModule("Commands :");
PutModule(" email <email address>");
PutModule(" set the email to sent notifications to.");
PutModule(" subject <email subject>");
PutModule(" set the subject of the notification, default IRC Notification.");
PutModule(" interval <seconds>");
PutModule(" set the time between the first message and the mail notification, default 1200 (20 minutes).");
PutModule(" Reload module required ('/msg *status reloadmod mailer')");
PutModule(" notifmax <number>");
PutModule(" set the number of notification in one mail, default 50.");
PutModule(" ");
PutModule("View the documentation at http://znc-mailer.readthedocs.org/");
} else {
if (!DebugMode){
PutModule("Error: invalid command, try `help`");
}
}
DebugCommands(command);
}
void DebugCommands(const CString& command){
if (!DebugMode){
return;
}
VCString tokens;
int token_count = command.Split(" ", tokens, false);
if (token_count < 1)
{
return;
}
CString action = tokens[0].AsLower();
if (action == "testemail"){
CString message;
message = "This is a testing email.";
Send(message);
} else if (action == "testfull"){
CString message = "Testing 1";
MessagesList.push_front(message);
CString message2 = "Testing 2";
MessagesList.push_front(message2);
CString message3 = "Testing 3";
MessagesList.push_front(message3);
BatchSend();
} else if (action == "testshowqueue"){
list<CString>::iterator it;
CString message;
for ( it=MessagesList.begin() ; it != MessagesList.end(); it++ ){
DebugPrint(*it);
}
} else if (action == "testbatchsend"){
BatchSend();
}
}
void Handle(CNick& Nick, const CString& location, CString& sMessage){
/*
* Method for handling all incoming messages that we are considering to
* email to the user. All private messages and all of those that
* SendNotification returns true for trigger an email.
*/
if (SendNotification(Nick, sMessage) || location == "PRIVATE"){
// Build up a string containing the current time.
CString string_time;
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (buffer,80,"(%x) %X",timeinfo);
string_time = buffer;
// Finally construct the message as it will be added to the email.
// and add it to a list (read queue) of messages to be sent.
CString message = string_time + ": <" + location + ":" + Nick.GetNick() + "> " + sMessage + "\n";
MessagesList.push_back(message);
DebugPrint("Added message...");
DebugPrint(message);
// Finally, check to see if we have the max number, and if we do,
// pop a message off the front to delete the oldest.
if (MessagesList.size() > MaxNotifications){
MessagesList.pop_front();
}
}
}
bool SendNotification(CNick& Nick, CString& sMessage){
/*
* Being passed in the nick name and the message, return true if an
* email notification should be sent to the user or not. At the moment
* this is a simple test that checks for a case insenstive username
* match which is a bit flawed as 1d0ugal1 still matches d0ugal.
*/
// Don't send notifications if DebugMode is off and the ConnectedUser is not attached.
if (!DebugMode && ConnectedUser->IsUserAttached()){
return false;
}
CString UserNick = ConnectedUser->GetNick().AsLower();
size_t found = sMessage.AsLower().find(UserNick);
if (found!=string::npos){
return true;
} else {
return false;
}
}
void BatchSend(){
/*
* Job to send all of the email messages in the list queue.
*/
DebugPrint("Preparing to batch send");
if (MessagesList.size() <= 0){
DebugPrint("No messages");
return;
}
if (!DebugMode && ConnectedUser->IsUserAttached()){
// There is a case, that if the user recieves a message, then
// conencts, there are some queue items waiting to be sent. Because
// we jump out early they are never cleared until they are sent
// when the user disconnects.
MessagesList.clear();
return;
}
list<CString>::iterator it;
CString message;
for ( it=MessagesList.begin() ; it != MessagesList.end(); it++ ){
message = message + *it;
}
Send(message);
MessagesList.clear();
}
void Send(CString sMessage){
/*
* Wrapper to pipe the email body to the mail command. Fairly naieve
* about its purpose and will fail if it doesn't have a destination
* email or can't open the mail command (i.e. it isn't installed)
*/
DebugPrint("Preparing to send email.");
if (NotificationEmail == ""){
PutModule("Unable to send email, no address set.");
return;
}
CString command;
command = "mail -s '" + NotificationSubject + "' " + NotificationEmail;
FILE *email= popen(command.c_str(), "w");
if (!email){
PutModule("Problems with mail command.");
return;
}
DebugPrint("Opened mail command. About to write message to it.");
fprintf(email, "%s", (char*) sMessage.c_str());
pclose(email);
if (DebugMode){
DebugPrint(command);
}
DebugPrint(sMessage);
DebugPrint("...Sent");
}
};
void CMailerTimer::RunJob(){
/*
* Simple call to trigger the BatchSend method periodically.
*/
CMailer *p = (CMailer *)m_pModule;
p->BatchSend();
}
MODULEDEFS(CMailer, "To be used to send mentions as an email")